Skip to content

Commit

Permalink
Add tests for Metadata serialisation.
Browse files Browse the repository at this point in the history
- Round-tripping of MA-era Metadata
- Check that Shelley-era metadata deserialises as MA-era.
  • Loading branch information
nc6 committed Nov 20, 2020
1 parent c9e92db commit b6c75f7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
9 changes: 5 additions & 4 deletions shelley-ma/impl/src/Cardano/Ledger/ShelleyMA/Metadata.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import Shelley.Spec.Ledger.MetaData
ValidateMetadata (..),
validMetaDatum,
)
import Shelley.Spec.Ledger.Serialization (mapFromCBOR, mapToCBOR)

-- | Raw, un-memoised metadata type
data MetadataRaw era = MetadataRaw
Expand Down Expand Up @@ -121,10 +122,10 @@ instance
encMetadataRaw ::
(Core.AnnotatedData (Core.Script era)) =>
MetadataRaw era ->
Encode ('Closed 'Dense) (MetadataRaw era)
Encode ( 'Closed 'Dense) (MetadataRaw era)
encMetadataRaw (MetadataRaw blob sp) =
Rec MetadataRaw
!> To blob
!> E mapToCBOR blob
!> E encodeFoldable sp

instance
Expand All @@ -136,13 +137,13 @@ instance
TypeMapLen ->
decode
( Ann (Emit MetadataRaw)
<*! Ann From
<*! Ann (D mapFromCBOR)
<*! Ann (Emit StrictSeq.empty)
)
TypeListLen ->
decode
( Ann (RecD MetadataRaw)
<*! Ann From
<*! Ann (D mapFromCBOR)
<*! D (sequence <$> decodeStrictSeq fromCBOR)
)
_ -> error "Failed to decode Metadata"
Expand Down
6 changes: 3 additions & 3 deletions shelley-ma/shelley-ma-test/cddl-files/shelley-ma.cddl
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,10 @@ transaction_metadatum_label = uint

transaction_metadata =
{ * transaction_metadatum_label => transaction_metadatum }
/ [{ ? 0: { * transaction_metadatum_label => transaction_metadatum }
, ? 1: [ * native_script ]
/ [ 0: { * transaction_metadatum_label => transaction_metadatum }
, 1: [ * native_script ]
; other types of metadata...
}]
]

vkeywitness = [ $vkey, $signature ]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}

module Test.Cardano.Ledger.ShelleyMA.Serialisation.CDDL
module Test.Cardano.Ledger.ShelleyMA.Serialisation.CDDL
( cddlTests,
)
where


import Cardano.Ledger.Allegra (AllegraEra)
import qualified Cardano.Ledger.Core as Core
import Cardano.Ledger.Mary (MaryEra)
import qualified Data.ByteString.Lazy as BSL
import Test.Shelley.Spec.Ledger.ConcreteCryptoTypes (C_Crypto)
import Test.Shelley.Spec.Ledger.Serialisation.CDDLUtils
( cddlTest,
cddlTest',
)


import qualified Cardano.Ledger.Core as Core
import Test.Shelley.Spec.Ledger.ConcreteCryptoTypes (C_Crypto)
import Cardano.Ledger.Mary (MaryEra)
import Cardano.Ledger.Allegra (AllegraEra)

import Test.Tasty (TestTree, withResource, testGroup)
import qualified Data.ByteString.Lazy as BSL
import Test.Tasty (TestTree, testGroup, withResource)

type A = AllegraEra C_Crypto

type M = MaryEra C_Crypto

cddlTests :: Int -> TestTree
cddlTests n = withResource combinedCDDL (const (pure ())) $ \cddl ->
testGroup "CDDL roundtrip tests" $
[ cddlTest @(Core.Value A) n "coin"
--, cddlTest @(Core.Value M) n "value"
-- , cddlTest' @(Core.TxBody M) n "transaction_body"
-- , cddlTest' @(Core.TxBody A) n "transaction_body"
, cddlTest' @(Core.Script M) n "native_script"
, cddlTest' @(Core.Script A) n "native_script"
[ cddlTest @(Core.Value A) n "coin",
-- cddlTest @(Core.Value M) n "value",
-- cddlTest' @(Core.TxBody M) n "transaction_body",
-- cddlTest' @(Core.TxBody A) n "transaction_body",
cddlTest' @(Core.Script M) n "native_script",
cddlTest' @(Core.Script A) n "native_script",
cddlTest' @(Core.Metadata M) n "transaction_metadata",
cddlTest' @(Core.Metadata A) n "transaction_metadata"
]
<*> pure cddl

Expand Down

0 comments on commit b6c75f7

Please sign in to comment.