Skip to content

Commit 666ce77

Browse files
authored
Merge pull request #5733 from IntersectMBO/jj/block-serialization
Dijkstra era CDDL for `block`, add `block_body`
2 parents 69d5aa3 + a8805c4 commit 666ce77

29 files changed

Lines changed: 386 additions & 369 deletions

File tree

eras/allegra/impl/src/Cardano/Ledger/Allegra/BlockBody.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,21 @@ module Cardano.Ledger.Allegra.BlockBody () where
1111

1212
import Cardano.Ledger.Allegra.Era (AllegraEra)
1313
import Cardano.Ledger.Allegra.Tx ()
14+
import Cardano.Ledger.BaseTypes (ProtVer (..))
15+
import Cardano.Ledger.Binary (EncCBORGroup (..), serialize')
1416
import Cardano.Ledger.Core (EraBlockBody (..))
1517
import Cardano.Ledger.Shelley.BlockBody (
1618
ShelleyBlockBody,
1719
mkBasicBlockBodyShelley,
1820
shelleyBlockBodyHash,
1921
txSeqBlockBodyShelleyL,
2022
)
23+
import qualified Data.ByteString as BS
2124

2225
instance EraBlockBody AllegraEra where
2326
type BlockBody AllegraEra = ShelleyBlockBody AllegraEra
2427
mkBasicBlockBody = mkBasicBlockBodyShelley
2528
txSeqBlockBodyL = txSeqBlockBodyShelleyL
2629
hashBlockBody = shelleyBlockBodyHash
2730
numSegComponents = 3
31+
blockBodySize (ProtVer v _) = BS.length . serialize' v . encCBORGroup

eras/alonzo/impl/src/Cardano/Ledger/Alonzo/BlockBody/Internal.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ module Cardano.Ledger.Alonzo.BlockBody.Internal (
3434
import qualified Cardano.Crypto.Hash as Hash
3535
import Cardano.Ledger.Alonzo.Era
3636
import Cardano.Ledger.Alonzo.Tx (AlonzoEraTx (..), IsValid (..))
37+
import Cardano.Ledger.BaseTypes (ProtVer (..))
3738
import Cardano.Ledger.Binary (
3839
Annotator (..),
3940
DecCBOR (..),
@@ -43,13 +44,15 @@ import Cardano.Ledger.Binary (
4344
encodeFoldableMapEncoder,
4445
encodePreEncoded,
4546
serialize,
47+
serialize',
4648
withSlice,
4749
)
4850
import Cardano.Ledger.Core
4951
import Cardano.Ledger.Shelley.BlockBody (auxDataSeqDecoder)
5052
import Control.DeepSeq (NFData)
5153
import Control.Monad (unless)
5254
import Data.ByteString (ByteString)
55+
import qualified Data.ByteString as BS
5356
import Data.ByteString.Builder (Builder, shortByteString, toLazyByteString)
5457
import qualified Data.ByteString.Lazy as BSL
5558
import Data.Coerce (coerce)
@@ -97,6 +100,7 @@ instance EraBlockBody AlonzoEra where
97100
txSeqBlockBodyL = txSeqBlockBodyAlonzoL
98101
hashBlockBody = abbHash
99102
numSegComponents = 4
103+
blockBodySize (ProtVer v _) = BS.length . serialize' v . encCBORGroup
100104

101105
mkBasicBlockBodyAlonzo ::
102106
( SafeToHash (TxWits era)

eras/babbage/impl/src/Cardano/Ledger/Babbage/BlockBody.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ module Cardano.Ledger.Babbage.BlockBody where
66
import Cardano.Ledger.Alonzo.BlockBody
77
import Cardano.Ledger.Babbage.Era
88
import Cardano.Ledger.Babbage.Tx ()
9+
import Cardano.Ledger.BaseTypes (ProtVer (..))
10+
import Cardano.Ledger.Binary (EncCBORGroup (..), serialize')
911
import Cardano.Ledger.Core
12+
import qualified Data.ByteString as BS
1013

1114
instance EraBlockBody BabbageEra where
1215
type BlockBody BabbageEra = AlonzoBlockBody BabbageEra
1316
mkBasicBlockBody = mkBasicBlockBodyAlonzo
1417
txSeqBlockBodyL = txSeqBlockBodyAlonzoL
1518
hashBlockBody = alonzoBlockBodyHash
1619
numSegComponents = 4
20+
blockBodySize (ProtVer v _) = BS.length . serialize' v . encCBORGroup

eras/conway/impl/cardano-ledger-conway.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ library
104104
build-depends:
105105
aeson >=2.2,
106106
base >=4.18 && <5,
107+
bytestring,
107108
cardano-crypto-class,
108109
cardano-data >=1.3,
109110
cardano-ledger-allegra ^>=1.10,

eras/conway/impl/src/Cardano/Ledger/Conway/BlockBody.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
module Cardano.Ledger.Conway.BlockBody where
55

66
import Cardano.Ledger.Alonzo.BlockBody
7+
import Cardano.Ledger.BaseTypes (ProtVer (..))
8+
import Cardano.Ledger.Binary (EncCBORGroup (..), serialize')
79
import Cardano.Ledger.Conway.Era
810
import Cardano.Ledger.Conway.Tx ()
911
import Cardano.Ledger.Core
12+
import qualified Data.ByteString as BS
1013

1114
instance EraBlockBody ConwayEra where
1215
type BlockBody ConwayEra = AlonzoBlockBody ConwayEra
1316
mkBasicBlockBody = mkBasicBlockBodyAlonzo
1417
txSeqBlockBodyL = txSeqBlockBodyAlonzoL
1518
hashBlockBody = alonzoBlockBodyHash
1619
numSegComponents = 4
20+
blockBodySize (ProtVer v _) = BS.length . serialize' v . encCBORGroup

eras/dijkstra/impl/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
* Add `getDijkstraScriptsProvided`
1616
* Add `MissingRequiredGuards` constructor to `DijkstraUtxowPredFailure`
1717
* Add `DijkstraUtxoEnv` and use it as `Environemnt` in `STS` instance of `UTXOW`
18+
* Refactor `DijkstraBlockBody` to use `MemoBytes` for memoized serialization
19+
* Add `blockBodySize` implementation for `DijkstraEra`
20+
* Add `DijkstraBlockBodyRaw`, `MkDijkstraBlockBody`
1821
* Add `ApplyTick` instance for `DijkstraEra`
1922
* Add `WrongNetworkInDirectDeposit` constructor to `DijkstraUtxoPredFailure`
2023
* Add `SubWrongNetworkInDirectDeposit` constructor to `DijkstraSubUtxoPredFailure`
@@ -51,8 +54,14 @@
5154
* Remove `NoThunks` instance for `DijkstraContextError`
5255
* Make `DijkstraContextError` constructors lazy
5356

57+
### cddl
58+
59+
* Add `peras_certificate`, `block_body`
60+
5461
### testlib
5562

63+
* Add `ToExpr` instance for `DijkstraBlockBody`
64+
* Add `DecCBOR` instance for `DijkstraBlockBodyRaw`
5665
* Add `genNonEmptyAccountBalanceIntervals`
5766
* In `Test.Cardano.Ledger.Dijkstra.Examples`:
5867
- Remove `mkDijkstraBasedExampleTx`, `mkDijkstraBasedExampleTxBody`

eras/dijkstra/impl/cardano-ledger-dijkstra.cabal

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ library
3030
exposed-modules:
3131
Cardano.Ledger.Dijkstra
3232
Cardano.Ledger.Dijkstra.BlockBody
33+
Cardano.Ledger.Dijkstra.BlockBody.Internal
3334
Cardano.Ledger.Dijkstra.Core
3435
Cardano.Ledger.Dijkstra.Era
3536
Cardano.Ledger.Dijkstra.Genesis
@@ -49,7 +50,6 @@ library
4950
Cardano.Ledger.Dijkstra.UTxO
5051

5152
other-modules:
52-
Cardano.Ledger.Dijkstra.BlockBody.Internal
5353
Cardano.Ledger.Dijkstra.Forecast
5454
Cardano.Ledger.Dijkstra.Rules.Bbody
5555
Cardano.Ledger.Dijkstra.Rules.Cert
@@ -111,7 +111,7 @@ library
111111
cardano-ledger-babbage ^>=1.14,
112112
cardano-ledger-binary ^>=1.9,
113113
cardano-ledger-conway ^>=1.23,
114-
cardano-ledger-core:{cardano-ledger-core, internal} >=1.20,
114+
cardano-ledger-core:{cardano-ledger-core, internal} >=1.21,
115115
cardano-ledger-mary,
116116
cardano-ledger-shelley,
117117
cardano-slotting,
@@ -210,10 +210,16 @@ library cddl
210210
-Wunused-packages
211211

212212
build-depends:
213+
QuickCheck,
214+
antigen,
213215
base,
214216
cardano-ledger-conway:cddl,
217+
cardano-ledger-core:cddl,
215218
cardano-ledger-dijkstra,
219+
cborg,
220+
cuddle,
216221
heredoc,
222+
quickcheck-transformer,
217223
text,
218224

219225
executable generate-cddl

eras/dijkstra/impl/cddl/data/dijkstra.cddl

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
11
; This file was auto-generated using generate-cddl. Please do not modify it directly!
22

3-
; Valid blocks must also satisfy the following two constraints:
4-
; 1) the length of transaction_bodies and transaction_witness_sets must be
5-
; the same
6-
; 2) every transaction_index must be strictly smaller than the length of
7-
; transaction_bodies
8-
block =
9-
[ header
10-
, transaction_bodies : [* transaction_body]
11-
, transaction_witness_sets : [* transaction_witness_set]
12-
, auxiliary_data_set : {* transaction_index => auxiliary_data}
13-
, invalid_transactions : [* transaction_index]
14-
]
15-
3+
block = [header, block_body]
164

175
transaction =
186
[ transaction_body, transaction_witness_set, true, auxiliary_data/ nil
@@ -99,6 +87,20 @@ protocol_version = [major_protocol_version, uint .size 4]
9987

10088
major_protocol_version = 0 .. 13
10189

90+
; Note that every transaction_index must be strictly smaller than the length of transaction_bodies
91+
block_body =
92+
[ invalid_transactions : invalid_transactions/ nil
93+
, transactions : [* transaction]
94+
, peras_certificate : peras_certificate
95+
]
96+
97+
98+
invalid_transactions = nonempty_set<transaction_index>
99+
100+
nonempty_set<a0> = #6.258([+ a0])/ [+ a0]
101+
102+
transaction_index = uint .size 2
103+
102104
transaction_body =
103105
{ 0 : set<transaction_input>
104106
, 1 : [* transaction_output]
@@ -504,8 +506,6 @@ positive_int64 = 1 .. max_int64
504506
; representation for empty redeemers.
505507
script_data_hash = hash32
506508

507-
nonempty_set<a0> = #6.258([+ a0])/ [+ a0]
508-
509509
guards = nonempty_set<addr_keyhash>/ nonempty_oset<credential>
510510

511511
network_id = 0/ 1
@@ -783,5 +783,5 @@ auxiliary_data_map =
783783
)
784784

785785

786-
transaction_index = uint .size 2
786+
peras_certificate = bytes/ nil
787787

eras/dijkstra/impl/cddl/lib/Cardano/Ledger/Dijkstra/HuddleSpec.hs

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,19 @@ module Cardano.Ledger.Dijkstra.HuddleSpec (
3434

3535
import Cardano.Ledger.Conway.HuddleSpec hiding ()
3636
import Cardano.Ledger.Dijkstra (DijkstraEra)
37+
import Cardano.Ledger.Huddle.Gen (genArrayTerm)
38+
import Codec.CBOR.Cuddle.CBOR.Gen (generateFromName)
39+
import Codec.CBOR.Cuddle.CDDL.CBORGenerator (CBORGen, WrappedTerm (..), liftAntiGen, withAntiGen)
40+
import Codec.CBOR.Term (Term (..))
41+
import Control.Monad (zipWithM)
3742
import Data.Proxy (Proxy (..))
3843
import Data.Text ()
39-
import Data.Word (Word64)
44+
import Data.Text qualified as T
45+
import Data.Word (Word16, Word64)
46+
import Test.AntiGen (withAnnotation, (|!))
47+
import Test.QuickCheck (choose, shuffle)
48+
import Test.QuickCheck qualified as QC
49+
import Test.QuickCheck.GenT (liftGen)
4050
import Text.Heredoc
4151
import Prelude hiding ((/))
4252

@@ -854,27 +864,62 @@ instance HuddleRule "header" DijkstraEra where
854864

855865
instance HuddleRule "block" DijkstraEra where
856866
huddleRuleNamed pname p =
867+
pname
868+
=.= arr
869+
[ a $ huddleRule @"header" p
870+
, a $ huddleRule @"block_body" p
871+
]
872+
873+
instance HuddleRule "peras_certificate" DijkstraEra where
874+
huddleRuleNamed pname _era = pname =.= VBytes / VNil
875+
876+
instance HuddleRule "invalid_transactions" DijkstraEra where
877+
huddleRuleNamed pname era = pname =.= huddleRule1 @"nonempty_set" era (huddleRule @"transaction_index" era)
878+
879+
instance HuddleRule "block_body" DijkstraEra where
880+
huddleRuleNamed pname era =
857881
comment
858-
[str|Valid blocks must also satisfy the following two constraints:
859-
| 1) the length of transaction_bodies and transaction_witness_sets must be
860-
| the same
861-
| 2) every transaction_index must be strictly smaller than the length of
862-
| transaction_bodies
882+
[str|Note that every transaction_index must be strictly smaller than the length of transaction_bodies
863883
|]
884+
$ withCBORGen blockBodyGen
864885
$ pname
865886
=.= arr
866-
[ a $ huddleRule @"header" p
867-
, "transaction_bodies" ==> arr [0 <+ a (huddleRule @"transaction_body" p)]
868-
, "transaction_witness_sets" ==> arr [0 <+ a (huddleRule @"transaction_witness_set" p)]
869-
, "auxiliary_data_set"
870-
==> mp
871-
[ 0
872-
<+ asKey (huddleRule @"transaction_index" p)
873-
==> huddleRule @"auxiliary_data" p
874-
]
875-
, "invalid_transactions" ==> arr [0 <+ a (huddleRule @"transaction_index" p)]
887+
[ "invalid_transactions" ==> huddleRule @"invalid_transactions" era / VNil
888+
, "transactions" ==> arr [0 <+ a (huddleRule @"transaction" era)]
889+
, "peras_certificate" ==> huddleRule @"peras_certificate" era
876890
]
877891

892+
blockBodyGen :: CBORGen WrappedTerm
893+
blockBodyGen = do
894+
numTxs <- liftGen . QC.sized $ \s -> choose (0 :: Int, s)
895+
txs <-
896+
mapM
897+
(\i -> withAntiGen (withAnnotation (T.pack $ show i)) $ generateFromName "transaction")
898+
[0 .. numTxs - 1]
899+
invalidIxIxs <-
900+
if numTxs == 0
901+
then pure []
902+
else do
903+
n <-
904+
liftAntiGen $
905+
choose (0, numTxs) |! choose (numTxs + 1, 2 * numTxs)
906+
txIndices <- liftGen $ shuffle [0 .. toInteger numTxs - 1]
907+
-- We need this so that a zapped `n` still produces indices
908+
txIndicesOverflow <- liftGen $ shuffle txIndices
909+
let
910+
txIndicesWithOverflow = take n $ txIndices <> txIndicesOverflow
911+
faultyIndex pos i =
912+
withAnnotation (T.pack $ show pos) $
913+
pure i
914+
|! choose (toInteger numTxs + 1, toInteger $ maxBound @Word16)
915+
liftAntiGen $
916+
withAnnotation "invalid_transactions" $
917+
zipWithM faultyIndex [0 :: Int ..] txIndicesWithOverflow
918+
invalidTxIxsTerm <- genArrayTerm $ TInteger . toInteger <$> invalidIxIxs
919+
txsTerm <- withAntiGen (withAnnotation "transactions") $ genArrayTerm txs
920+
perasCertTerm <- generateFromName "peras_certificate"
921+
S <$> liftGen (genArrayTerm [invalidTxIxsTerm, txsTerm, perasCertTerm])
922+
878923
instance HuddleRule "auxiliary_scripts" DijkstraEra where
879924
huddleRuleNamed = auxiliaryScriptsRule
880925

0 commit comments

Comments
 (0)