Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

- At startup, Kupo adds three empty columns to the `inputs` table of an existing database, to prepare the indexing of Dijkstra sub-transactions. The change is automatic and instant, and needs no action.

- Kupo now returns datums and redeemers from Babbage and Conway transaction witnesses with their on-chain bytes, as it already does for inline datums and other eras. Earlier versions returned a re-encoding of the same value, which cannot be used as-is to spend an output locked by that datum's hash. Databases built by earlier versions keep the re-encoded bytes.

#### Removed

N/A
Expand Down
13 changes: 6 additions & 7 deletions src/Kupo/Data/Cardano/BinaryData.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,27 @@ unsafeBinaryDataFromBytes =
either (error . toText) identity . Ledger.makeBinaryData . toShort
{-# INLINABLE unsafeBinaryDataFromBytes #-}

-- | Keeps the decoded bytes; 'coerce' only changes the era label, as the
-- ledger's translateDatum does.
fromAlonzoData
:: HasCallStack
=> Ledger.Data AlonzoEra
:: Ledger.Data AlonzoEra
-> BinaryData
fromAlonzoData =
unsafeBinaryDataFromBytes . Ledger.originalBytes
coerce . Ledger.dataToBinaryData
{-# INLINEABLE fromAlonzoData #-}

fromBabbageData
:: Ledger.Data BabbageEra
-> BinaryData
fromBabbageData =
Ledger.dataToBinaryData
. (Ledger.upgradeData :: Ledger.Data ConwayEra -> Ledger.Data DijkstraEra)
. Ledger.upgradeData
coerce . Ledger.dataToBinaryData
{-# INLINEABLE fromBabbageData #-}

fromConwayData
:: Ledger.Data ConwayEra
-> BinaryData
fromConwayData =
Ledger.dataToBinaryData . Ledger.upgradeData
coerce . Ledger.dataToBinaryData
{-# INLINEABLE fromConwayData #-}

fromDijkstraData
Expand Down
38 changes: 36 additions & 2 deletions test/Test/Kupo/Data/CardanoSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ module Test.Kupo.Data.CardanoSpec
import Kupo.Prelude

import Kupo.Data.Cardano
( addressFromBytes
( BinaryData
, addressFromBytes
, assetNameFromText
, assetNameToText
, binaryDataToBytes
, datumHashFromText
, datumHashToText
, fromAlonzoData
, fromBabbageData
, fromConwayData
, fromDijkstraData
, hashBinaryData
, hashScript
, headerHashFromText
, metadataFromJson
Expand Down Expand Up @@ -43,7 +50,8 @@ import Kupo.Data.Cardano
, unsafeScriptHashFromBytes
)
import Test.Hspec
( Spec
( Expectation
, Spec
, context
, parallel
, shouldBe
Expand Down Expand Up @@ -74,11 +82,13 @@ import Test.QuickCheck
, (===)
)

import qualified Cardano.Ledger.Plutus.Data as Ledger
import qualified Data.Aeson as Json
import qualified Data.Aeson.Encoding as Json
import qualified Data.Aeson.Types as Json
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy.Char8 as BL8
import qualified Kupo.Data.Database as DB

spec :: Spec
spec = parallel $ do
Expand All @@ -101,6 +111,18 @@ spec = parallel $ do
forAll genDatumHash $ \x ->
datumHashFromText (datumHashToText x) === Just x

context "BinaryData" $ do
-- Definite-length list; Plutus would re-encode it as an indefinite-length one.
let bytes = BS.pack [0x82, 0x01, 0x02]
specify "Alonzo witness datum keeps its on-chain bytes" $
keepsOnChainBytes @AlonzoEra fromAlonzoData bytes
specify "Babbage witness datum keeps its on-chain bytes" $
keepsOnChainBytes @BabbageEra fromBabbageData bytes
specify "Conway witness datum keeps its on-chain bytes" $
keepsOnChainBytes @ConwayEra fromConwayData bytes
specify "Dijkstra witness datum keeps its on-chain bytes" $
keepsOnChainBytes @DijkstraEra fromDijkstraData bytes

context "Script" $ do
prop "∀(x :: Script). scriptFromBytes (scriptToBytes x) === x" $
forAll genScript $ \x ->
Expand Down Expand Up @@ -183,3 +205,15 @@ spec = parallel $ do
genBytes :: Int -> Gen ByteString
genBytes n =
BS.pack <$> vectorOf n arbitrary

keepsOnChainBytes
:: forall era. (Era era, HasCallStack)
=> (Ledger.Data era -> BinaryData)
-> ByteString
-> Expectation
keepsOnChainBytes convert bytes = do
let datum = unsafeDecodeCborAnn @era "Data" decCBOR (fromStrict bytes)
let binaryData = convert datum
binaryDataToBytes binaryData `shouldBe` bytes
DB.binaryData (DB.binaryDataToRow (hashBinaryData binaryData) binaryData) `shouldBe` bytes
hashBinaryData binaryData `shouldBe` Ledger.hashData datum
Loading