Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the alonzo tx witness serialization #2525

Merged
merged 1 commit into from
Oct 19, 2021
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
27 changes: 22 additions & 5 deletions eras/alonzo/impl/src/Cardano/Ledger/Alonzo/TxWitness.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh goodness 🤦 I will need to do a follow-up PR. cc @nc6

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can wait.

isPlutus lang (PlutusScript l _) = lang == l

instance
(Era era) =>
Expand Down
1 change: 1 addition & 0 deletions eras/alonzo/test-suite/cardano-ledger-alonzo-test.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file added eras/alonzo/test-suite/golden/block.cbor
Binary file not shown.
Binary file added eras/alonzo/test-suite/golden/tx.cbor
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@
-- 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)
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,
Expand All @@ -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, (@?=))

Expand Down Expand Up @@ -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
]
2 changes: 2 additions & 0 deletions eras/alonzo/test-suite/test/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ mainTests =
Canonical.tests,
CDDL.tests 5,
Golden.goldenUTxOEntryMinAda,
Golden.goldenSerialization,
plutusScriptExamples
]

Expand All @@ -47,6 +48,7 @@ fastTests =
Translation.tests,
CDDL.tests 1,
Golden.goldenUTxOEntryMinAda,
Golden.goldenSerialization,
plutusScriptExamples
]

Expand Down