Skip to content

Commit 50c4c17

Browse files
teodanciulehins
authored andcommitted
Remove bytestring from Block type
1 parent a9e78ae commit 50c4c17

File tree

17 files changed

+58
-109
lines changed

17 files changed

+58
-109
lines changed

eras/alonzo/impl/src/Cardano/Ledger/Alonzo/Rules/Bbody.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ alonzoBbodyTransition =
193193
>>= \( TRC
194194
( BbodyEnv pp account
195195
, BbodyState ls b
196-
, UnserialisedBlock bh txsSeq
196+
, Block bh txsSeq
197197
)
198198
) -> do
199199
let txs = txSeqTxns txsSeq

eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Bbody.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ conwayBbodyTransition = do
278278
>>= \( TRC
279279
( _
280280
, state@(BbodyState ls _)
281-
, UnserialisedBlock _ txsSeq
281+
, Block _ txsSeq
282282
)
283283
) -> do
284284
let utxo = utxosUtxo (lsUTxOState ls)

eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/Imp/BbodySpec.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ spec = describe "BBODY" $ do
9292
tryRunImpRule @"BBODY"
9393
(BbodyEnv pp account)
9494
(BbodyState ls (BlocksMade Map.empty))
95-
(UnsafeUnserialisedBlock bhView txSeq)
95+
(Block bhView txSeq)
9696
predFailures
9797
`shouldBe` NE.fromList
9898
[ injectFailure

eras/shelley/impl/src/Cardano/Ledger/Shelley/Rules/Bbody.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ bbodyTransition =
179179
>>= \( TRC
180180
( BbodyEnv pp account
181181
, BbodyState ls b
182-
, UnserialisedBlock bhview txsSeq
182+
, Block bhview txsSeq
183183
)
184184
) -> do
185185
let txs = fromTxSeq txsSeq

eras/shelley/test-suite/bench/BenchValidation.hs

+3-3
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ benchValidate ::
9494
ValidateInput era ->
9595
IO (NewEpochState era)
9696
benchValidate (ValidateInput globals state (Block bh txs)) =
97-
let block = UnsafeUnserialisedBlock (makeHeaderView bh) txs
97+
let block = Block (makeHeaderView bh) txs
9898
in case API.applyBlockEitherNoEvents ValidateAll globals state block of
9999
Right x -> pure x
100100
Left x -> error (show x)
@@ -112,7 +112,7 @@ applyBlock ::
112112
Int ->
113113
Int
114114
applyBlock (ValidateInput globals state (Block bh txs)) n =
115-
let block = UnsafeUnserialisedBlock (makeHeaderView bh) txs
115+
let block = Block (makeHeaderView bh) txs
116116
in case API.applyBlockEitherNoEvents ValidateAll globals state block of
117117
Right x -> seq (rnf x) (n + 1)
118118
Left x -> error (show x)
@@ -122,7 +122,7 @@ benchreValidate ::
122122
ValidateInput era ->
123123
NewEpochState era
124124
benchreValidate (ValidateInput globals state (Block bh txs)) =
125-
API.applyBlockNoValidaton globals state (UnsafeUnserialisedBlock (makeHeaderView bh) txs)
125+
API.applyBlockNoValidaton globals state (Block (makeHeaderView bh) txs)
126126

127127
-- ==============================================================
128128

eras/shelley/test-suite/src/Test/Cardano/Ledger/Shelley/LaxBlock.hs

+10-11
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,7 @@
1111

1212
module Test.Cardano.Ledger.Shelley.LaxBlock where
1313

14-
import Cardano.Ledger.Binary (
15-
Annotator (..),
16-
DecCBOR (decCBOR),
17-
Decoder,
18-
ToCBOR,
19-
annotatorSlice,
20-
decodeRecordNamed,
21-
)
14+
import Cardano.Ledger.Binary
2215
import Cardano.Ledger.Block (Block (..))
2316
import Cardano.Ledger.Core (Era, EraSegWits (TxSeq), EraTx)
2417
import Cardano.Ledger.Shelley.BlockChain (ShelleyTxSeq, txSeqDecoder)
@@ -28,7 +21,13 @@ import Data.Typeable (Typeable)
2821
-- encoding of parts of the segwit.
2922
-- This is only for testing.
3023
newtype LaxBlock h era = LaxBlock (Block h era)
31-
deriving (ToCBOR)
24+
25+
deriving newtype instance
26+
( Era era
27+
, EncCBORGroup (TxSeq era)
28+
, EncCBOR h
29+
) =>
30+
ToCBOR (LaxBlock h era)
3231

3332
deriving newtype instance
3433
( EraSegWits era
@@ -45,11 +44,11 @@ blockDecoder ::
4544
Bool ->
4645
forall s.
4746
Decoder s (Annotator (Block h era))
48-
blockDecoder lax = annotatorSlice $
47+
blockDecoder lax =
4948
decodeRecordNamed "Block" (const 4) $ do
5049
header <- decCBOR
5150
txns <- txSeqDecoder lax
52-
pure $ Block' <$> header <*> txns
51+
pure $ Block <$> header <*> txns
5352

5453
deriving stock instance (Era era, Show (TxSeq era), Show h) => Show (LaxBlock h era)
5554

eras/shelley/test-suite/src/Test/Cardano/Ledger/Shelley/Rules/AdaPreservation.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ withdrawals ::
581581
EraGen era =>
582582
Block (BHeader MockCrypto) era ->
583583
Coin
584-
withdrawals (UnserialisedBlock _ txseq) =
584+
withdrawals (Block _ txseq) =
585585
F.foldl'
586586
( \c tx ->
587587
let wdrls = unWithdrawals $ tx ^. bodyTxL . withdrawalsTxBodyL

eras/shelley/test-suite/src/Test/Cardano/Ledger/Shelley/Rules/Chain.hs

+1-3
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,6 @@ chainTransition ::
301301
, State (EraRule "TICK" era) ~ NewEpochState era
302302
, Signal (EraRule "TICK" era) ~ SlotNo
303303
, Embed (PRTCL MockCrypto) (CHAIN era)
304-
, EncCBORGroup (TxSeq era)
305304
, ProtVerAtMost era 6
306305
, State (EraRule "LEDGERS" era) ~ LedgerState era
307306
, EraGov era
@@ -370,10 +369,9 @@ chainTransition =
370369
, bh
371370
)
372371

373-
let thouShaltNot = error "A block with a header view should never be hashed"
374372
BbodyState ls' bcur' <-
375373
trans @(EraRule "BBODY" era) $
376-
TRC (BbodyEnv pp' account, BbodyState ls bcur, Block' bhView txs thouShaltNot)
374+
TRC (BbodyEnv pp' account, BbodyState ls bcur, Block bhView txs)
377375

378376
let nes'' = updateNES nes' bcur' ls'
379377
bhb = bhbody bh

eras/shelley/test-suite/src/Test/Cardano/Ledger/Shelley/Rules/ClassifyTraces.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ relevantCasesAreCoveredForTrace ::
137137
Property
138138
relevantCasesAreCoveredForTrace tr = do
139139
let blockTxs :: Block (BHeader MockCrypto) era -> [Tx era]
140-
blockTxs (UnserialisedBlock _ txSeq) = toList (fromTxSeq @era txSeq)
140+
blockTxs (Block _ txSeq) = toList (fromTxSeq @era txSeq)
141141
bs = traceSignals OldestFirst tr
142142
txs = concat (blockTxs <$> bs)
143143
certsByTx_ = certsByTx @era txs

eras/shelley/test-suite/src/Test/Cardano/Ledger/Shelley/Rules/PoolReap.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ tests =
8080
poolState target = (chainNes target) ^. nesEsL . esLStateL . lsCertStateL . certPStateL
8181

8282
removedAfterPoolreap_ :: SourceSignalTarget (CHAIN era) -> Property
83-
removedAfterPoolreap_ (SourceSignalTarget {source, target, signal = (UnserialisedBlock bh _)}) =
83+
removedAfterPoolreap_ (SourceSignalTarget {source, target, signal = (Block bh _)}) =
8484
let e = (epochFromSlotNo . bheaderSlotNo . bhbody) bh
8585
in removedAfterPoolreap (poolState source) (poolState target) e
8686

eras/shelley/test-suite/src/Test/Cardano/Ledger/Shelley/Rules/TestChain.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ ledgerTraceBase chainSt block =
254254
, txs
255255
)
256256
where
257-
(UnserialisedBlock (BHeader bhb _) txSeq) = block
257+
(Block (BHeader bhb _) txSeq) = block
258258
slot = bheaderSlotNo bhb
259259
tickedChainSt = tickChainState slot chainSt
260260
nes = (nesEs . chainNes) tickedChainSt

eras/shelley/test-suite/test/Test/Cardano/Ledger/Shelley/Serialisation/Golden/Encoding.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ tests =
987987
in checkEncodingCBORAnnotated
988988
shelleyProtVer
989989
"empty_block"
990-
(Block @C bh txns)
990+
(Block @(BHeader MockCrypto) @C bh txns)
991991
( (T $ TkListLen 4)
992992
<> S bh
993993
<> T (TkListLen 0 . TkListLen 0 . TkMapLen 0)
@@ -1048,7 +1048,7 @@ tests =
10481048
in checkEncodingCBORAnnotated
10491049
shelleyProtVer
10501050
"rich_block"
1051-
(Block @C bh txns)
1051+
(Block @(BHeader MockCrypto) @C bh txns)
10521052
( (T $ TkListLen 4)
10531053
-- header
10541054
<> S bh

libs/cardano-ledger-core/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## 1.17.0.0
44

5+
* Replace `Block'` constructor with `Block`
6+
* Remove patterns: `Block`, `UnserialisedBlock` and `UnsafeUnserialisedBlock`
7+
* Add ` EncCBORGroup (TxSeq era)` and `EncCBOR h` constraints to `EncCBOR` and `ToCBOR` instances for `Block`
58
* Add `BoootstrapWitnessRaw` type
69
* Add `EraStake`, `CanGetInstantStake`, `CanSetInstantStake` , `snapShotFromInstantStake`, `resolveActiveInstantStakeCredentials`
710
* Add boolean argument to `fromCborRigorousBothAddr` for lenient `Ptr` decoding

libs/cardano-ledger-core/src/Cardano/Ledger/Block.hs

+27-79
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
{-# LANGUAGE UndecidableInstances #-}
1616

1717
module Cardano.Ledger.Block (
18-
Block (Block, Block', UnserialisedBlock, UnsafeUnserialisedBlock),
18+
Block (Block),
1919
bheader,
2020
bbody,
2121
neededTxInsForBlock,
@@ -27,17 +27,13 @@ import Cardano.Ledger.Binary (
2727
DecCBOR (decCBOR),
2828
EncCBOR (..),
2929
EncCBORGroup (..),
30-
annotatorSlice,
3130
decodeRecordNamed,
3231
encodeListLen,
33-
serialize,
32+
toPlainEncoding,
3433
)
3534
import qualified Cardano.Ledger.Binary.Plain as Plain
3635
import Cardano.Ledger.Core
37-
import Cardano.Ledger.MemoBytes (MemoBytes (Memo), decodeMemoized)
3836
import Cardano.Ledger.TxIn (TxIn (..))
39-
import qualified Data.ByteString.Lazy as BSL
40-
import qualified Data.ByteString.Short as SBS
4137
import Data.Foldable (toList)
4238
import Data.Set (Set)
4339
import qualified Data.Set as Set
@@ -47,7 +43,7 @@ import Lens.Micro ((^.))
4743
import NoThunks.Class (NoThunks (..))
4844

4945
data Block h era
50-
= Block' !h !(TxSeq era) BSL.ByteString
46+
= Block !h !(TxSeq era)
5147
deriving (Generic)
5248

5349
deriving stock instance
@@ -65,111 +61,63 @@ deriving anyclass instance
6561
) =>
6662
NoThunks (Block h era)
6763

68-
pattern Block ::
64+
instance
6965
forall era h.
7066
( Era era
7167
, EncCBORGroup (TxSeq era)
7268
, EncCBOR h
7369
) =>
74-
h ->
75-
TxSeq era ->
76-
Block h era
77-
pattern Block h txns <-
78-
Block' h txns _
79-
where
80-
Block h txns =
81-
let bytes =
82-
serialize (eraProtVerLow @era) $
83-
encodeListLen (1 + listLen txns) <> encCBOR h <> encCBORGroup txns
84-
in Block' h txns bytes
85-
86-
{-# COMPLETE Block #-}
87-
88-
-- | Access a block without its serialised bytes. This is often useful when
89-
-- we're using a 'BHeaderView' in place of the concrete header.
90-
pattern UnserialisedBlock ::
91-
h ->
92-
TxSeq era ->
93-
Block h era
94-
pattern UnserialisedBlock h txns <- Block' h txns _
95-
96-
{-# COMPLETE UnserialisedBlock #-}
97-
98-
-- | Unsafely construct a block without the ability to serialise its bytes.
99-
--
100-
-- Anyone calling this pattern must ensure that the resulting block is never
101-
-- serialised. Any uses of this pattern outside of testing code should be
102-
-- regarded with suspicion.
103-
pattern UnsafeUnserialisedBlock ::
104-
h ->
105-
TxSeq era ->
106-
Block h era
107-
pattern UnsafeUnserialisedBlock h txns <-
108-
Block' h txns _
70+
EncCBOR (Block h era)
10971
where
110-
UnsafeUnserialisedBlock h txns =
111-
let bytes = error "`UnsafeUnserialisedBlock` used to construct a block which was later serialised."
112-
in Block' h txns bytes
113-
114-
{-# COMPLETE UnsafeUnserialisedBlock #-}
115-
116-
instance (EraTx era, Typeable h) => EncCBOR (Block h era)
117-
118-
instance (EraTx era, Typeable h) => Plain.ToCBOR (Block h era) where
119-
toCBOR (Block' _ _ blockBytes) = Plain.encodePreEncoded $ BSL.toStrict blockBytes
72+
encCBOR (Block h txns) =
73+
encodeListLen (1 + listLen txns) <> encCBOR h <> encCBORGroup txns
12074

12175
instance
122-
( EraSegWits era
123-
, DecCBOR (Annotator h)
124-
, Typeable h
76+
forall era h.
77+
( Era era
78+
, EncCBORGroup (TxSeq era)
79+
, EncCBOR h
12580
) =>
126-
DecCBOR (Annotator (Block h era))
81+
Plain.ToCBOR (Block h era)
12782
where
128-
decCBOR = annotatorSlice $
129-
decodeRecordNamed "Block" (const blockSize) $ do
130-
header <- decCBOR
131-
txns <- decCBOR
132-
pure $ Block' <$> header <*> txns
133-
where
134-
blockSize =
135-
1 -- header
136-
+ fromIntegral (numSegComponents @era)
137-
138-
data BlockRaw h era = BlockRaw !h !(TxSeq era)
83+
toCBOR = toPlainEncoding (eraProtVerLow @era) . encCBOR
13984

14085
instance
14186
( EraSegWits era
14287
, DecCBOR h
14388
, DecCBOR (TxSeq era)
14489
) =>
145-
DecCBOR (BlockRaw h era)
90+
DecCBOR (Block h era)
14691
where
14792
decCBOR =
14893
decodeRecordNamed "Block" (const blockSize) $ do
14994
header <- decCBOR
15095
txns <- decCBOR
151-
pure $ BlockRaw header txns
96+
pure $ Block header txns
15297
where
15398
blockSize = 1 + fromIntegral (numSegComponents @era)
15499

155100
instance
156101
( EraSegWits era
157-
, DecCBOR h
158-
, DecCBOR (TxSeq era)
102+
, DecCBOR (Annotator h)
103+
, Typeable h
159104
) =>
160-
DecCBOR (Block h era)
105+
DecCBOR (Annotator (Block h era))
161106
where
162-
decCBOR = do
163-
Memo (BlockRaw h txSeq) bs <- decodeMemoized (decCBOR @(BlockRaw h era))
164-
pure $ Block' h txSeq (BSL.fromStrict (SBS.fromShort bs))
107+
decCBOR = decodeRecordNamed "Block" (const blockSize) $ do
108+
header <- decCBOR
109+
txns <- decCBOR
110+
pure $ Block <$> header <*> txns
111+
where
112+
blockSize = 1 + fromIntegral (numSegComponents @era)
165113

166114
bheader ::
167115
Block h era ->
168116
h
169-
bheader (Block' bh _ _) = bh
117+
bheader (Block bh _) = bh
170118

171119
bbody :: Block h era -> TxSeq era
172-
bbody (Block' _ txs _) = txs
120+
bbody (Block _ txs) = txs
173121

174122
-- | The validity of any individual block depends only on a subset
175123
-- of the UTxO stored in the ledger state. This function returns
@@ -185,7 +133,7 @@ neededTxInsForBlock ::
185133
EraSegWits era =>
186134
Block h era ->
187135
Set TxIn
188-
neededTxInsForBlock (Block' _ txsSeq _) = Set.filter isNotNewInput allTxIns
136+
neededTxInsForBlock (Block _ txsSeq) = Set.filter isNotNewInput allTxIns
189137
where
190138
txBodies = map (^. bodyTxL) $ toList $ fromTxSeq txsSeq
191139
allTxIns = Set.unions $ map (^. allInputsTxBodyF) txBodies

libs/cardano-ledger-test/src/Test/Cardano/Ledger/Examples/AlonzoBBODY.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ coldKeys = KeyPair vk sk
705705

706706
makeNaiveBlock ::
707707
forall era. EraSegWits era => [Tx era] -> Block BHeaderView era
708-
makeNaiveBlock txs = UnsafeUnserialisedBlock bhView txSeq
708+
makeNaiveBlock txs = Block bhView txSeq
709709
where
710710
bhView =
711711
BHeaderView

libs/cardano-ledger-test/src/Test/Cardano/Ledger/Generic/Same.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ instance
247247
Same era (ShelleyLedgerExamples era)
248248
where
249249
same proof x1 x2 = case (sleBlock x1, sleBlock x2) of
250-
(Block' h1 a1 _, Block' h2 a2 _) ->
250+
(Block h1 a1, Block h2 a2) ->
251251
sameWithDependency
252252
[ SomeM "Tx" (sameTx proof) (sleTx x1) (sleTx x2)
253253
, SomeM "TxSeq" (sameTxSeq proof) a1 a2

0 commit comments

Comments
 (0)