Skip to content

Commit 0a5150b

Browse files
committed
New encode(TxIn|TxOut)WithHint methods for SerializeTablesWithHint
1 parent 297de67 commit 0a5150b

File tree

11 files changed

+104
-4
lines changed

11 files changed

+104
-4
lines changed

ouroboros-consensus-cardano/src/ouroboros-consensus-cardano/Ouroboros/Consensus/Cardano/Ledger.hs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,48 @@ instance
214214
toPlainEncoding (eraProtVerLow @era) $
215215
encodeMap encodeMemPack (eliminateCardanoTxOut (const encodeMemPack)) tbs
216216

217+
encodeTxInWithHint (HardForkLedgerState (HardForkState idx)) txin =
218+
let
219+
-- These could be made into a CAF to avoid recomputing it, but
220+
-- it is only used in serialization so it is not critical.
221+
np =
222+
(Fn $ const $ K $ Codec.CBOR.Encoding.encodeMapLen 0)
223+
:* (Fn $ const $ K $ encOne (Proxy @ShelleyEra))
224+
:* (Fn $ const $ K $ encOne (Proxy @AllegraEra))
225+
:* (Fn $ const $ K $ encOne (Proxy @MaryEra))
226+
:* (Fn $ const $ K $ encOne (Proxy @AlonzoEra))
227+
:* (Fn $ const $ K $ encOne (Proxy @BabbageEra))
228+
:* (Fn $ const $ K $ encOne (Proxy @ConwayEra))
229+
:* (Fn $ const $ K $ encOne (Proxy @DijkstraEra))
230+
:* Nil
231+
in
232+
hcollapse $ hap np $ Telescope.tip idx
233+
where
234+
encOne :: forall era. Era era => Proxy era -> Encoding
235+
encOne _ =
236+
toPlainEncoding (eraProtVerLow @era) $ encodeMemPack txin
237+
238+
encodeTxOutWithHint (HardForkLedgerState (HardForkState idx)) txout =
239+
let
240+
-- These could be made into a CAF to avoid recomputing it, but
241+
-- it is only used in serialization so it is not critical.
242+
np =
243+
(Fn $ const $ K $ Codec.CBOR.Encoding.encodeMapLen 0)
244+
:* (Fn $ const $ K $ encOne (Proxy @ShelleyEra))
245+
:* (Fn $ const $ K $ encOne (Proxy @AllegraEra))
246+
:* (Fn $ const $ K $ encOne (Proxy @MaryEra))
247+
:* (Fn $ const $ K $ encOne (Proxy @AlonzoEra))
248+
:* (Fn $ const $ K $ encOne (Proxy @BabbageEra))
249+
:* (Fn $ const $ K $ encOne (Proxy @ConwayEra))
250+
:* (Fn $ const $ K $ encOne (Proxy @DijkstraEra))
251+
:* Nil
252+
in
253+
hcollapse $ hap np $ Telescope.tip idx
254+
where
255+
encOne :: forall era. Era era => Proxy era -> Encoding
256+
encOne _ =
257+
toPlainEncoding (eraProtVerLow @era) $ eliminateCardanoTxOut (const encodeMemPack) txout
258+
217259
decodeTablesWithHint ::
218260
forall s.
219261
LedgerState (HardForkBlock (CardanoEras c)) EmptyMK ->

ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Ledger.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,8 @@ instance
367367
where
368368
encodeTablesWithHint _ (LedgerTables (ValuesMK tbs)) =
369369
toPlainEncoding (Core.eraProtVerLow @era) $ encodeMap encodeMemPack encodeMemPack tbs
370+
encodeTxInWithHint _ = toPlainEncoding (Core.eraProtVerLow @era) . encodeMemPack
371+
encodeTxOutWithHint _ = toPlainEncoding (Core.eraProtVerLow @era) . encodeMemPack
370372
decodeTablesWithHint st =
371373
let certInterns =
372374
internsFromMap $

ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/ShelleyHFC.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,8 @@ instance
503503
encOne :: Encoding
504504
encOne = toPlainEncoding (SL.eraProtVerLow @era) $ encodeMap encodeMemPack encodeMemPack tbs
505505

506+
encodeTxInWithHint _ = toPlainEncoding (SL.eraProtVerLow @era) . encodeMemPack
507+
encodeTxOutWithHint _ = toPlainEncoding (SL.eraProtVerLow @era) . encodeMemPack
506508
decodeTablesWithHint ::
507509
forall s.
508510
LedgerState (HardForkBlock '[ShelleyBlock proto era]) EmptyMK ->

ouroboros-consensus-cardano/src/unstable-cardano-testlib/Test/ThreadNet/Infra/ShelleyBasedHardFork.hs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,34 @@ instance
548548
)
549549
tbs
550550

551+
encodeTxInWithHint (HardForkLedgerState (HardForkState idx)) txin =
552+
let
553+
np =
554+
(Fn $ const $ K $ encOne (Proxy @era1))
555+
:* (Fn $ const $ K $ encOne (Proxy @era2))
556+
:* Nil
557+
in
558+
hcollapse $ hap np $ Telescope.tip idx
559+
where
560+
encOne :: forall era. SL.Era era => Proxy era -> Encoding
561+
encOne _ =
562+
toPlainEncoding (SL.eraProtVerLow @era) $ encodeMemPack (getShelleyHFCTxIn txin)
563+
564+
encodeTxOutWithHint (HardForkLedgerState (HardForkState idx)) txout0 =
565+
let
566+
np =
567+
(Fn $ const $ K $ encOne (Proxy @era1))
568+
:* (Fn $ const $ K $ encOne (Proxy @era2))
569+
:* Nil
570+
in
571+
hcollapse $ hap np $ Telescope.tip idx
572+
where
573+
encOne :: forall era. SL.Era era => Proxy era -> Encoding
574+
encOne _ =
575+
toPlainEncoding (SL.eraProtVerLow @era) $ case txout0 of
576+
Z txout -> encodeMemPack $ unwrapTxOut txout
577+
S (Z txout) -> encodeMemPack $ unwrapTxOut txout
578+
551579
decodeTablesWithHint ::
552580
forall s.
553581
LedgerState (HardForkBlock (ShelleyBasedHardForkEras proto1 era1 proto2 era2)) EmptyMK ->

ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/HardFork/Combinator.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,8 @@ instance SerialiseHFC '[BlockA, BlockB]
479479
instance SerializeTablesWithHint (LedgerState (HardForkBlock '[BlockA, BlockB])) where
480480
encodeTablesWithHint = defaultEncodeTablesWithHint
481481
decodeTablesWithHint = defaultDecodeTablesWithHint
482+
encodeTxInWithHint = defaultEncodeMemPackWithHint
483+
encodeTxOutWithHint = defaultEncodeMemPackWithHint
482484

483485
instance
484486
IndexedMemPack

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Ledger/Dual.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,8 @@ instance
11221122
where
11231123
encodeTablesWithHint = defaultEncodeTablesWithHint
11241124
decodeTablesWithHint = defaultDecodeTablesWithHint
1125+
encodeTxInWithHint = defaultEncodeMemPackWithHint
1126+
encodeTxOutWithHint = defaultEncodeMemPackWithHint
11251127

11261128
instance
11271129
( Bridge m a

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Ledger/Extended.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,4 +383,6 @@ instance
383383

384384
instance SerializeTablesWithHint (LedgerState blk) => SerializeTablesWithHint (ExtLedgerState blk) where
385385
decodeTablesWithHint st = castLedgerTables <$> decodeTablesWithHint (ledgerState st)
386+
encodeTxInWithHint st = encodeTxInWithHint (ledgerState st)
387+
encodeTxOutWithHint st = encodeTxOutWithHint (ledgerState st)
386388
encodeTablesWithHint st tbs = encodeTablesWithHint (ledgerState st) (castLedgerTables tbs)

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Ledger/Tables.hs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ module Ouroboros.Consensus.Ledger.Tables
176176
, SerializeTablesWithHint (..)
177177
, defaultDecodeTablesWithHint
178178
, defaultEncodeTablesWithHint
179+
, defaultEncodeMemPackWithHint
179180
, valuesMKDecoder
180181
, valuesMKEncoder
181182

@@ -322,6 +323,14 @@ class SerializeTablesWithHint l where
322323
SerializeTablesHint (LedgerTables l ValuesMK) ->
323324
LedgerTables l ValuesMK ->
324325
CBOR.Encoding
326+
encodeTxInWithHint ::
327+
SerializeTablesHint (LedgerTables l ValuesMK) ->
328+
TxIn l ->
329+
CBOR.Encoding
330+
encodeTxOutWithHint ::
331+
SerializeTablesHint (LedgerTables l ValuesMK) ->
332+
TxOut l ->
333+
CBOR.Encoding
325334
decodeTablesWithHint ::
326335
SerializeTablesHint (LedgerTables l ValuesMK) ->
327336
CBOR.Decoder s (LedgerTables l ValuesMK)
@@ -339,19 +348,22 @@ defaultEncodeTablesWithHint ::
339348
SerializeTablesHint (LedgerTables l ValuesMK) ->
340349
LedgerTables l ValuesMK ->
341350
CBOR.Encoding
342-
defaultEncodeTablesWithHint _ (LedgerTables (ValuesMK tbs)) =
351+
defaultEncodeTablesWithHint h (LedgerTables (ValuesMK tbs)) =
343352
mconcat
344353
[ CBOR.encodeMapLen (fromIntegral $ Map.size tbs)
345354
, Map.foldMapWithKey
346355
( \k v ->
347356
mconcat
348-
[ CBOR.encodeBytes (packByteString k)
349-
, CBOR.encodeBytes (packByteString v)
357+
[ defaultEncodeMemPackWithHint h k
358+
, defaultEncodeMemPackWithHint h v
350359
]
351360
)
352361
tbs
353362
]
354363

364+
defaultEncodeMemPackWithHint :: MemPack a => p -> a -> CBOR.Encoding
365+
defaultEncodeMemPackWithHint _ k = CBOR.encodeBytes (packByteString k)
366+
355367
defaultDecodeTablesWithHint ::
356368
(Ord (TxIn l), MemPack (TxIn l), MemPack (TxOut l)) =>
357369
SerializeTablesHint (LedgerTables l ValuesMK) ->
@@ -414,8 +426,10 @@ instance IndexedMemPack (TrivialLedgerTables l EmptyMK) Void where
414426
indexedPackM _ = packM
415427
indexedUnpackM _ = unpackM
416428

417-
instance SerializeTablesWithHint (TrivialLedgerTables l) where
429+
instance (MemPack (TxIn l), MemPack (TxOut l)) => SerializeTablesWithHint (TrivialLedgerTables l) where
418430
decodeTablesWithHint _ = do
419431
_ <- CBOR.decodeMapLen
420432
pure (LedgerTables $ ValuesMK Map.empty)
433+
encodeTxInWithHint = defaultEncodeMemPackWithHint
434+
encodeTxOutWithHint = defaultEncodeMemPackWithHint
421435
encodeTablesWithHint _ _ = CBOR.encodeMapLen 0

ouroboros-consensus/src/unstable-consensus-testlib/Test/Util/LedgerStateOnlyTables.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ instance
7878
instance (Ord k, MemPack k, MemPack v) => SerializeTablesWithHint (LedgerState (OTBlock k v)) where
7979
encodeTablesWithHint = defaultEncodeTablesWithHint
8080
decodeTablesWithHint = defaultDecodeTablesWithHint
81+
encodeTxInWithHint = defaultEncodeMemPackWithHint
82+
encodeTxOutWithHint = defaultEncodeMemPackWithHint
8183

8284
{-------------------------------------------------------------------------------
8385
Stowable

ouroboros-consensus/src/unstable-mock-block/Ouroboros/Consensus/Mock/Ledger/Block.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,8 @@ instance IndexedMemPack (LedgerState (SimpleBlock c ext) EmptyMK) Mock.TxOut whe
538538
instance SerializeTablesWithHint (LedgerState (SimpleBlock c ext)) where
539539
encodeTablesWithHint = defaultEncodeTablesWithHint
540540
decodeTablesWithHint = defaultDecodeTablesWithHint
541+
encodeTxInWithHint = defaultEncodeMemPackWithHint
542+
encodeTxOutWithHint = defaultEncodeMemPackWithHint
541543

542544
instance HasLedgerTables (LedgerState (SimpleBlock c ext)) where
543545
projectLedgerTables = simpleLedgerTables

0 commit comments

Comments
 (0)