From 9ab85598f0cf1c780665b8b3bc52bf7dc2506b10 Mon Sep 17 00:00:00 2001 From: Jared Corduan Date: Tue, 19 Oct 2021 15:01:58 -0400 Subject: [PATCH] Fix the alonzo tx witness serialization Missing witness group should be entirely absent on the wire, not encoded as an empty list. We were encoding an empty list in the scenario that one type of script was present, but another type was absent, since the "Omit" check was checking too see if there were no scripts at all. --- .../src/Cardano/Ledger/Alonzo/TxWitness.hs | 27 ++++++++++++++---- .../cardano-ledger-alonzo-test.cabal | 1 + eras/alonzo/test-suite/golden/block.cbor | Bin 0 -> 1865 bytes eras/alonzo/test-suite/golden/tx.cbor | Bin 0 -> 865 bytes .../test/Test/Cardano/Ledger/Alonzo/Golden.hs | 17 +++++++++++ eras/alonzo/test-suite/test/Tests.hs | 2 ++ 6 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 eras/alonzo/test-suite/golden/block.cbor create mode 100644 eras/alonzo/test-suite/golden/tx.cbor diff --git a/eras/alonzo/impl/src/Cardano/Ledger/Alonzo/TxWitness.hs b/eras/alonzo/impl/src/Cardano/Ledger/Alonzo/TxWitness.hs index 5750acf81a7..6494eb27019 100644 --- a/eras/alonzo/impl/src/Cardano/Ledger/Alonzo/TxWitness.hs +++ b/eras/alonzo/impl/src/Cardano/Ledger/Alonzo/TxWitness.hs @@ -50,7 +50,7 @@ import Cardano.Binary ) import Cardano.Ledger.Alonzo.Data (Data, DataHash, hashData) import Cardano.Ledger.Alonzo.Language (Language (..)) -import Cardano.Ledger.Alonzo.Scripts (ExUnits (..), Script (..), Tag, isPlutusScript) +import Cardano.Ledger.Alonzo.Scripts (ExUnits (..), Script (..), Tag) import qualified Cardano.Ledger.Core as Core import Cardano.Ledger.Era (Era (Crypto), ValidateScript, hashScript) import Cardano.Ledger.Keys @@ -346,13 +346,25 @@ encodeWitnessRaw vkeys boots scripts dats rdmrs = !> Omit null (Key 2 $ setEncode boots) !> Omit null - (Key 1 $ E (encodeFoldable . mapMaybe unwrapTS . Map.elems) timelocks) + ( Key 1 $ + E + (encodeFoldable . mapMaybe unwrapTS . Map.elems) + (Map.filter isTimelock scripts) + ) !> Omit null - (Key 3 $ E (encodeFoldable . mapMaybe unwrapPS1 . Map.elems) plutusScripts) + ( Key 3 $ + E + (encodeFoldable . mapMaybe unwrapPS1 . Map.elems) + (Map.filter (isPlutus PlutusV1) scripts) + ) !> Omit null - (Key 6 $ E (encodeFoldable . mapMaybe unwrapPS2 . Map.elems) plutusScripts) + ( Key 6 $ + E + (encodeFoldable . mapMaybe unwrapPS2 . Map.elems) + (Map.filter (isPlutus PlutusV2) scripts) + ) !> Omit nullDats (Key 4 $ E toCBOR dats) !> Omit nullRedeemers (Key 5 $ To rdmrs) where @@ -362,7 +374,12 @@ encodeWitnessRaw vkeys boots scripts dats rdmrs = unwrapPS1 _ = Nothing unwrapPS2 (PlutusScript PlutusV2 x) = Just x unwrapPS2 _ = Nothing - (plutusScripts, timelocks) = Map.partition isPlutusScript scripts + + isTimelock (TimelockScript _) = True + isTimelock (PlutusScript _ _) = False + + isPlutus _ (TimelockScript _) = True + isPlutus lang (PlutusScript l _) = lang == l instance (Era era) => diff --git a/eras/alonzo/test-suite/cardano-ledger-alonzo-test.cabal b/eras/alonzo/test-suite/cardano-ledger-alonzo-test.cabal index 4329d5270fc..27f0b6caf1a 100644 --- a/eras/alonzo/test-suite/cardano-ledger-alonzo-test.cabal +++ b/eras/alonzo/test-suite/cardano-ledger-alonzo-test.cabal @@ -18,6 +18,7 @@ extra-source-files: cddl-files/alonzo.cddl cddl-files/mock/crypto.cddl cddl-files/mock/extras.cddl + golden/*.cbor source-repository head type: git diff --git a/eras/alonzo/test-suite/golden/block.cbor b/eras/alonzo/test-suite/golden/block.cbor new file mode 100644 index 0000000000000000000000000000000000000000..5808f3e47f5f49342cf00553740e33c80b43712e GIT binary patch literal 1865 zcmai!X*iS%6vy9p#*CRMWb2yCFwr)n7)vhMvyDk5iMNF;*)_w=FqV|}wO&i9Y)PJ( zh&EeUVicu9gBnFlS|};klBMXT>*{{IAM!ls+xb8L|L-~HJSPb`jmAS{A+2PKy3glI z$q~ZtG&kKC#Y;|Oz$APoHu-i@yrR-Ih@6)C?iCk5o%6%xPlmQf$ZWm93^zGhZ?b~@X=0?=O z!CF-O*7Bd6Q|tjxlZ<^sq|a6=6UxwpyqZ-4bG+^DWm_<7Aeh z{t4U^@%e8pove$4MXl>(%lMOiN?qwVWg-DL-#we}>Z`1?=1qA~U(1D~s-Y_D9Z&IK z#&LzWwA@m&KC+vwFXW8hJ7F<{^|a1?imN5~3N5q;3y!`Cn3@}=-EyT+NK+2}iSoOp zZQL)n8#YIeJ%LPFpBBa9lYbD4NuOd$B%b!PD%W+}idHch7hZ&hoLxo4Qy?@0^hlxS?f>FB$XLW`l+DwE{6m|QDr zLFM$=0yQIKuuDAy{$WY#mb06*rTpnXqf$QQ)g!y-RUMZ~kHz6TgAdkr)X2fd!#%!0 zm2uK_Dy-E{^uj&we+TlVX*bpee|7&5fW=h~HT#hqnP7F>l6sB3QvVZi{X6X~6cfX< zMx6wSl1FA{%m8sp-K$+#uJ>}{+iXr^9)`bK=}OdI?6r3q`>#K&p1~E~oj#TgPD5c% z>U+3y;TxW`0aq{PJvGp|am!+7Q|9%}>~U@LZ$paZlE1`m&5!oHY18-KMV$~6pkl{! z^;H=Nb@rO~p%CGPjju<1Dd($3UH@4z{CB;Za*-gXH2o?bZKly=lhs0fH zVck|44MIoV_t})F&c=(k^{4~VSdBAVWl=SiyTIV^9QsW*WmW#dD z`kk@yhooY08>|IVL^(cyL&+{Pt z4srW3j@|j;hS_zmZBrG=xBNTJAMks_z0AW#{)+fi0Z literal 0 HcmV?d00001 diff --git a/eras/alonzo/test-suite/golden/tx.cbor b/eras/alonzo/test-suite/golden/tx.cbor new file mode 100644 index 0000000000000000000000000000000000000000..07fa93f4aec98354d0a942ded4727be0e09e3f0c GIT binary patch literal 865 zcmZo!$I#dmq3}*L>f9U$O@)%vS^sB$j6bH5bLF>+h9!1(kG z#nt;S=053YV4dK#$B{QHyl#r)+P<4wUl4XTlO*$VW)_y_ zCWa=42pN2qcQNCak5D-JNNVHBZ%n$4hd0HUn4PYd{a!yUXq}y~z@fe**&Ee9jI1~0 zni#nvWFO$OrxCw34NZB;`FX{udBvs0dIkCUIZoBJO>9jJ3qkH@=yq@Moz$p*m{*?b zn8hzN{Kwyg#@gYNMUON2JnOBB8D@B4u3D+#quUL+y2khRW}0I z8lqXMIaD1NvPkkTVBlcl13L4E%(FcHpj$CZ8Yf%il?wiSy1`&E`u@@4V(9~H8!ggOy93-+njdB_Q-;(g$cF=PthWZJ3?We;LMdByNdo_dwD4H)NSkF zt)CPA&-^HKbJIJ6BiFrdbh8sy!q2z_oLH^1)c2g*wnR5+@rnyt`MkxcMxy1ikDQlT z9q?G#9eGPL!l5y&aBJh14O#{QU6r$1kAHgVQNJzyxyOsYPv^O1?QB0MYp=;jJCArSx^`+F(? literal 0 HcmV?d00001 diff --git a/eras/alonzo/test-suite/test/Test/Cardano/Ledger/Alonzo/Golden.hs b/eras/alonzo/test-suite/test/Test/Cardano/Ledger/Alonzo/Golden.hs index 5a0076b825c..b7b8327696e 100644 --- a/eras/alonzo/test-suite/test/Test/Cardano/Ledger/Alonzo/Golden.hs +++ b/eras/alonzo/test-suite/test/Test/Cardano/Ledger/Alonzo/Golden.hs @@ -6,9 +6,11 @@ -- Description : Golden Tests for the Mary era module Test.Cardano.Ledger.Alonzo.Golden ( goldenUTxOEntryMinAda, + goldenSerialization, ) where +import Cardano.Binary (serialize) import Cardano.Ledger.Alonzo (AlonzoEra) import Cardano.Ledger.Alonzo.Data (Data (..), hashData) import Cardano.Ledger.Alonzo.Rules.Utxo (utxoEntrySize) @@ -16,8 +18,10 @@ import Cardano.Ledger.Alonzo.TxBody (TxOut (..)) import Cardano.Ledger.BaseTypes (StrictMaybe (..)) import Cardano.Ledger.Coin (Coin (..)) import Cardano.Ledger.Mary.Value (Value (..), valueFromList) +import qualified Data.ByteString.Lazy as BSL import Data.Char (chr) import Plutus.V1.Ledger.Api (Data (..)) +import Test.Cardano.Ledger.Alonzo.Examples.Consensus (ledgerExamplesAlonzo) import Test.Cardano.Ledger.EraBuffet (StandardCrypto) import Test.Cardano.Ledger.Mary.Golden ( largestName, @@ -29,6 +33,7 @@ import Test.Cardano.Ledger.Mary.Golden smallestName, ) import Test.Cardano.Ledger.Shelley.Examples.Cast (aliceAddr, bobAddr, carlAddr) +import qualified Test.Cardano.Ledger.Shelley.Examples.Consensus as SLE import Test.Tasty (TestTree, testGroup) import Test.Tasty.HUnit (testCase, (@?=)) @@ -157,3 +162,15 @@ goldenUTxOEntryMinAda = -- we can divide minUTxOValue by 29 and round. utxoEntrySize @(AlonzoEra StandardCrypto) (TxOut aliceAddr (Value 0 mempty) SNothing) @?= 29 ] + +goldenSerialization :: TestTree +goldenSerialization = + testGroup + "golden tests - serialization" + [ testCase "Alonzo Block" $ do + expected <- (BSL.readFile "golden/block.cbor") + serialize (SLE.sleBlock ledgerExamplesAlonzo) @?= expected, + testCase "Alonzo Tx" $ do + expected <- (BSL.readFile "golden/tx.cbor") + serialize (SLE.sleTx ledgerExamplesAlonzo) @?= expected + ] diff --git a/eras/alonzo/test-suite/test/Tests.hs b/eras/alonzo/test-suite/test/Tests.hs index 6cc2b2c6266..ec6526083b5 100644 --- a/eras/alonzo/test-suite/test/Tests.hs +++ b/eras/alonzo/test-suite/test/Tests.hs @@ -36,6 +36,7 @@ mainTests = Canonical.tests, CDDL.tests 5, Golden.goldenUTxOEntryMinAda, + Golden.goldenSerialization, plutusScriptExamples ] @@ -47,6 +48,7 @@ fastTests = Translation.tests, CDDL.tests 1, Golden.goldenUTxOEntryMinAda, + Golden.goldenSerialization, plutusScriptExamples ]