From 7efeb3dae161febd432e7608a84e21259606809f Mon Sep 17 00:00:00 2001 From: Mateusz Galazyn Date: Tue, 24 Nov 2026 00:07:26 +0100 Subject: [PATCH] Keep the on-chain bytes of Babbage and Conway witness datums Converting a Babbage or Conway witness datum went through upgradeData, which rebuilds the datum from the Plutus data and so re-encodes it. A datum that was not in Plutus's own encoding, such as a list with a definite length, was stored under its on-chain hash but with re-encoded bytes, which do not hash to that value. Spend redeemers were re-encoded in the same way. Alonzo, Babbage and Conway datums now go through dataToBinaryData, which keeps the bytes the ledger decoded, and coerce, which only changes the era label, as the ledger does for inline datums when it upgrades outputs. Nothing is decoded or re-encoded on the way. --- CHANGELOG.md | 2 ++ src/Kupo/Data/Cardano/BinaryData.hs | 13 +++++----- test/Test/Kupo/Data/CardanoSpec.hs | 38 +++++++++++++++++++++++++++-- 3 files changed, 44 insertions(+), 9 deletions(-) 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