Skip to content

Commit 19adf8f

Browse files
committed
[wip] - ShelleyTxSeq
1 parent 23635e4 commit 19adf8f

File tree

4 files changed

+125
-34
lines changed

4 files changed

+125
-34
lines changed

eras/shelley/impl/src/Cardano/Ledger/Shelley/BlockChain.hs

Lines changed: 85 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
{-# LANGUAGE DerivingVia #-}
55
{-# LANGUAGE FlexibleContexts #-}
66
{-# LANGUAGE FlexibleInstances #-}
7+
{-# LANGUAGE InstanceSigs #-}
78
{-# LANGUAGE MultiParamTypeClasses #-}
89
{-# LANGUAGE OverloadedStrings #-}
910
{-# LANGUAGE PatternSynonyms #-}
@@ -39,11 +40,13 @@ import Cardano.Ledger.BaseTypes (
3940
strictMaybeToMaybe,
4041
)
4142
import Cardano.Ledger.Binary (
43+
Annotated (..),
4244
Annotator (..),
4345
DecCBOR (decCBOR),
4446
Decoder,
4547
EncCBOR (..),
4648
EncCBORGroup (..),
49+
decodeAnnotated,
4750
encodeFoldableEncoder,
4851
encodeFoldableMapEncoder,
4952
encodePreEncoded,
@@ -52,7 +55,7 @@ import Cardano.Ledger.Binary (
5255
)
5356
import Cardano.Ledger.Core
5457
import Cardano.Ledger.Shelley.Era (ShelleyEra)
55-
import Cardano.Ledger.Shelley.Tx (ShelleyTx, segwitTx)
58+
import Cardano.Ledger.Shelley.Tx (ShelleyTx, segwitTx, segwitTxXXX)
5659
import Cardano.Ledger.Slot (SlotNo (..))
5760
import Control.Monad (unless)
5861
import Data.ByteString (ByteString)
@@ -188,55 +191,104 @@ bbHash (TxSeq' _ bodies wits md) =
188191
hashPart = Hash.hashToBytes . hashStrict . BSL.toStrict
189192

190193
-- | Given a size and a mapping from indices to maybe metadata,
191-
-- return a sequence whose size is the size paramater and
194+
-- return a sequence whose size is the size parameter and
192195
-- whose non-Nothing values correspond to the values in the mapping.
193196
constructMetadata ::
194-
forall era.
195197
Int ->
196198
Map Int (Annotator (TxAuxData era)) ->
197199
Seq (Maybe (Annotator (TxAuxData era)))
198-
constructMetadata n md = fmap (`Map.lookup` md) (Seq.fromList [0 .. n - 1])
200+
constructMetadata = indexLookupSeq
201+
202+
indexLookupSeq :: Int -> Map Int a -> Seq (Maybe a)
203+
indexLookupSeq n md = fmap (`Map.lookup` md) (Seq.fromList [0 .. n - 1])
204+
205+
instance
206+
( EraTx era
207+
, DecCBOR (TxBody era)
208+
, DecCBOR (TxWits era)
209+
, DecCBOR (TxAuxData era)
210+
) =>
211+
DecCBOR (ShelleyTxSeq era)
212+
where
213+
decCBOR = do
214+
Annotated (bodies :: Seq (TxBody era)) bodiesBS <- decodeAnnotated decCBOR
215+
let b = length bodies
216+
Annotated (wits :: Seq (TxWits era)) witsBS <- decodeAnnotated decCBOR
217+
Annotated (metadataMap :: Map Int (TxAuxData era)) metadataBS <-
218+
decodeAnnotated decCBOR
219+
let metadata = indexLookupSeq b metadataMap
220+
let txs = StrictSeq.forceToStrict $ Seq.zipWith3 segwitTxXXX bodies wits metadata
221+
pure $ TxSeq' txs bodiesBS witsBS metadataBS
199222

200223
-- | The parts of the Tx in Blocks that have to have DecCBOR(Annotator x) instances.
201224
-- These are exactly the parts that are SafeToHash.
202225
-- | Decode a TxSeq, used in decoding a Block.
226+
txXXX ::
227+
forall era.
228+
( EraTx era
229+
, DecCBOR (TxBody era)
230+
, DecCBOR (TxWits era)
231+
, DecCBOR (TxAuxData era)
232+
) =>
233+
Bool ->
234+
forall s.
235+
Decoder s (Annotator (ShelleyTxSeq era))
236+
txXXX _ = do
237+
d <- decCBOR @(ShelleyTxSeq era)
238+
pure undefined
239+
203240
txSeqDecoder ::
204241
forall era.
205-
EraTx era =>
242+
( EraTx era
243+
, DecCBOR (TxBody era)
244+
, DecCBOR (TxWits era)
245+
, DecCBOR (TxAuxData era)
246+
) =>
206247
Bool ->
207248
forall s.
208249
Decoder s (Annotator (ShelleyTxSeq era))
209-
txSeqDecoder lax = do
210-
(bodies, bodiesAnn) <- withSlice decCBOR
211-
(wits, witsAnn) <- withSlice decCBOR
212-
let b = length bodies
213-
inRange x = (0 <= x) && (x <= (b - 1))
214-
w = length wits
215-
(metadata, metadataAnn) <- withSlice $
216-
do
217-
m <- decCBOR
218-
unless -- TODO this PR introduces this new test, That didn't used to run in the Shelley
219-
(lax || all inRange (Map.keysSet m)) -- Era, Is it possible there might be some blocks, that should have been caught on the chain?
220-
(fail ("Some Auxiliarydata index is not in the range: 0 .. " ++ show (b - 1)))
221-
pure (constructMetadata @era b m)
250+
txSeqDecoder _ = do
251+
d <- decCBOR @(ShelleyTxSeq era)
252+
pure $ pure $ d
222253

223-
unless
224-
(lax || b == w)
225-
( fail $
226-
"different number of transaction bodies ("
227-
<> show b
228-
<> ") and witness sets ("
229-
<> show w
230-
<> ")"
231-
)
254+
-- txSeqDecoder lax = do
255+
-- (bodies, bodiesAnn) <- withSlice decCBOR
256+
-- (wits, witsAnn) <- withSlice decCBOR
257+
-- let b = length bodies
258+
-- inRange x = (0 <= x) && (x <= (b - 1))
259+
-- w = length wits
260+
-- (metadata, metadataAnn) <- withSlice $
261+
-- do
262+
-- m <- decCBOR
263+
-- unless -- TODO this PR introduces this new test, That didn't used to run in the Shelley
264+
-- (lax || all inRange (Map.keysSet m)) -- Era, Is it possible there might be some blocks, that should have been caught on the chain?
265+
-- (fail ("Some Auxiliarydata index is not in the range: 0 .. " ++ show (b - 1)))
266+
-- pure (constructMetadata @era b m)
232267

233-
let txns =
234-
sequenceA $
235-
StrictSeq.forceToStrict $
236-
Seq.zipWith3 segwitTx bodies wits metadata
237-
pure $ TxSeq' <$> txns <*> bodiesAnn <*> witsAnn <*> metadataAnn
268+
-- unless
269+
-- (lax || b == w)
270+
-- ( fail $
271+
-- "different number of transaction bodies ("
272+
-- <> show b
273+
-- <> ") and witness sets ("
274+
-- <> show w
275+
-- <> ")"
276+
-- )
238277

239-
instance EraTx era => DecCBOR (Annotator (ShelleyTxSeq era)) where
278+
-- let txns =
279+
-- sequenceA $
280+
-- StrictSeq.forceToStrict $
281+
-- Seq.zipWith3 segwitTx bodies wits metadata
282+
-- pure $ TxSeq' <$> txns <*> bodiesAnn <*> witsAnn <*> metadataAnn
283+
284+
instance
285+
( EraTx era
286+
, DecCBOR (TxBody era)
287+
, DecCBOR (TxWits era)
288+
, DecCBOR (TxAuxData era)
289+
) =>
290+
DecCBOR (Annotator (ShelleyTxSeq era))
291+
where
240292
decCBOR = txSeqDecoder False
241293

242294
slotToNonce :: SlotNo -> Nonce

eras/shelley/impl/src/Cardano/Ledger/Shelley/Tx.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module Cardano.Ledger.Shelley.Tx (
1313
sizeShelleyTxF,
1414
wireSizeShelleyTxF,
1515
segwitTx,
16+
segwitTxXXX,
1617
mkBasicShelleyTx,
1718
shelleyMinFeeTx,
1819
witsFromTxWitnesses,

eras/shelley/impl/src/Cardano/Ledger/Shelley/Tx/Internal.hs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ module Cardano.Ledger.Shelley.Tx.Internal (
4040
sizeShelleyTxF,
4141
wireSizeShelleyTxF,
4242
segwitTx,
43+
segwitTxXXX,
4344
mkBasicShelleyTx,
4445
shelleyMinFeeTx,
4546
witsFromTxWitnesses,
@@ -398,6 +399,33 @@ segwitTx
398399
(maybeToStrictMaybe metadata)
399400
fullBytes
400401

402+
segwitTxXXX ::
403+
forall era.
404+
EraTx era =>
405+
TxBody era ->
406+
TxWits era ->
407+
Maybe (TxAuxData era) ->
408+
ShelleyTx era
409+
segwitTxXXX
410+
body'
411+
witnessSet
412+
metadata =
413+
let
414+
wrappedMetadataBytes = case metadata of
415+
Nothing -> Plain.serialize Plain.encodeNull
416+
Just b -> Plain.serialize b
417+
fullBytes =
418+
Plain.serialize (Plain.encodeListLen 3)
419+
<> Plain.serialize body'
420+
<> Plain.serialize witnessSet
421+
<> wrappedMetadataBytes
422+
in
423+
unsafeConstructTxWithBytes
424+
body'
425+
witnessSet
426+
(maybeToStrictMaybe metadata)
427+
fullBytes
428+
401429
-- ========================================
402430

403431
-- | Minimum fee calculation

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import Cardano.Ledger.Binary (
2020
decodeRecordNamed,
2121
)
2222
import Cardano.Ledger.Block (Block (..))
23-
import Cardano.Ledger.Core (Era, EraSegWits (TxSeq), EraTx)
23+
import Cardano.Ledger.Core -- (Era, EraSegWits (TxSeq), EraTx)
2424
import Cardano.Ledger.Shelley.BlockChain (ShelleyTxSeq, txSeqDecoder)
2525
import Data.Typeable (Typeable)
2626

@@ -41,6 +41,11 @@ blockDecoder ::
4141
( EraTx era
4242
, TxSeq era ~ ShelleyTxSeq era
4343
, DecCBOR (Annotator h)
44+
, DecCBOR h
45+
, DecCBOR (TxBody era)
46+
, DecCBOR (TxWits era)
47+
, DecCBOR (TxAuxData era)
48+
, DecCBOR (ShelleyTxSeq era)
4449
) =>
4550
Bool ->
4651
forall s.
@@ -58,6 +63,11 @@ instance
5863
, Typeable h
5964
, TxSeq era ~ ShelleyTxSeq era
6065
, DecCBOR (Annotator h)
66+
, DecCBOR h
67+
, DecCBOR (TxBody era)
68+
, DecCBOR (TxWits era)
69+
, DecCBOR (TxAuxData era)
70+
, DecCBOR (ShelleyTxSeq era)
6171
) =>
6272
DecCBOR (Annotator (LaxBlock h era))
6373
where

0 commit comments

Comments
 (0)