diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ee48232..b5eaf2a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Kupo/Data/Cardano/BinaryData.hs b/src/Kupo/Data/Cardano/BinaryData.hs index b500b3c2..a5cf0062 100644 --- a/src/Kupo/Data/Cardano/BinaryData.hs +++ b/src/Kupo/Data/Cardano/BinaryData.hs @@ -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 diff --git a/test/Test/Kupo/Data/CardanoSpec.hs b/test/Test/Kupo/Data/CardanoSpec.hs index ca4de60b..029d0cdc 100644 --- a/test/Test/Kupo/Data/CardanoSpec.hs +++ b/test/Test/Kupo/Data/CardanoSpec.hs @@ -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 @@ -43,7 +50,8 @@ import Kupo.Data.Cardano , unsafeScriptHashFromBytes ) import Test.Hspec - ( Spec + ( Expectation + , Spec , context , parallel , shouldBe @@ -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 @@ -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 -> @@ -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