From 3619c22c33d52aab55ade4a7e24f7d4ac2e496b4 Mon Sep 17 00:00:00 2001 From: teodanciu Date: Thu, 30 Jan 2025 15:02:35 +0000 Subject: [PATCH] [shelley] - ShelleyTxSeq --- eras/shelley/impl/CHANGELOG.md | 1 + .../src/Cardano/Ledger/Shelley/BlockChain.hs | 42 +++++++++++++++++-- .../impl/src/Cardano/Ledger/Shelley/Tx.hs | 1 + .../src/Cardano/Ledger/Shelley/Tx/Internal.hs | 28 +++++++++++++ 4 files changed, 69 insertions(+), 3 deletions(-) diff --git a/eras/shelley/impl/CHANGELOG.md b/eras/shelley/impl/CHANGELOG.md index 073ff12377b..56b47455fb8 100644 --- a/eras/shelley/impl/CHANGELOG.md +++ b/eras/shelley/impl/CHANGELOG.md @@ -2,6 +2,7 @@ ## 1.16.0.0 +* Add `segWitTx` * Rename `segwitTx` to `segWitAnnTx` * Made the fields of predicate failures and environments lazy * Changed the type of `sgSecurityParam` to `NonZero Word64` diff --git a/eras/shelley/impl/src/Cardano/Ledger/Shelley/BlockChain.hs b/eras/shelley/impl/src/Cardano/Ledger/Shelley/BlockChain.hs index 0724a4a1a02..41e0a2587c5 100644 --- a/eras/shelley/impl/src/Cardano/Ledger/Shelley/BlockChain.hs +++ b/eras/shelley/impl/src/Cardano/Ledger/Shelley/BlockChain.hs @@ -39,11 +39,13 @@ import Cardano.Ledger.BaseTypes ( strictMaybeToMaybe, ) import Cardano.Ledger.Binary ( + Annotated (..), Annotator (..), DecCBOR (decCBOR), Decoder, EncCBOR (..), EncCBORGroup (..), + decodeAnnotated, encodeFoldableEncoder, encodeFoldableMapEncoder, encodePreEncoded, @@ -52,7 +54,7 @@ import Cardano.Ledger.Binary ( ) import Cardano.Ledger.Core import Cardano.Ledger.Shelley.Era (ShelleyEra) -import Cardano.Ledger.Shelley.Tx (ShelleyTx, segWitAnnTx) +import Cardano.Ledger.Shelley.Tx (ShelleyTx, segWitAnnTx, segWitTx) import Cardano.Ledger.Slot (SlotNo (..)) import Control.Monad (unless) import Data.ByteString (ByteString) @@ -191,11 +193,13 @@ bbHash (TxSeq' _ bodies wits md) = -- return a sequence whose size is the size paramater and -- whose non-Nothing values correspond to the values in the mapping. constructMetadata :: - forall era. Int -> Map Int (Annotator (TxAuxData era)) -> Seq (Maybe (Annotator (TxAuxData era))) -constructMetadata n md = fmap (`Map.lookup` md) (Seq.fromList [0 .. n - 1]) +constructMetadata = indexLookupSeq + +indexLookupSeq :: Int -> Map Int a -> Seq (Maybe a) +indexLookupSeq n md = fmap (`Map.lookup` md) (Seq.fromList [0 .. n - 1]) -- | The parts of the Tx in Blocks that have to have DecCBOR(Annotator x) instances. -- These are exactly the parts that are SafeToHash. @@ -239,6 +243,38 @@ txSeqDecoder lax = do instance EraTx era => DecCBOR (Annotator (ShelleyTxSeq era)) where decCBOR = txSeqDecoder False +instance + ( EraTx era + , DecCBOR (TxBody era) + , DecCBOR (TxWits era) + , DecCBOR (TxAuxData era) + ) => + DecCBOR (ShelleyTxSeq era) + where + decCBOR = do + Annotated (bodies :: Seq (TxBody era)) bodiesBS <- decodeAnnotated decCBOR + let b = length bodies + inRange x = (0 <= x) && (x <= (b - 1)) + Annotated (wits :: Seq (TxWits era)) witsBS <- decodeAnnotated decCBOR + let w = length wits + Annotated (metadataMap :: Map Int (TxAuxData era)) metadataBS <- + decodeAnnotated decCBOR + let metadata = indexLookupSeq b metadataMap + + unless + (b == w) + ( fail $ + "different number of transaction bodies (" + <> show b + <> ") and witness sets (" + <> show w + <> ")" + ) + let txs = + StrictSeq.forceToStrict $ + Seq.zipWith3 segWitTx bodies wits metadata + pure $ TxSeq' txs bodiesBS witsBS metadataBS + slotToNonce :: SlotNo -> Nonce slotToNonce (SlotNo s) = mkNonceFromNumber s diff --git a/eras/shelley/impl/src/Cardano/Ledger/Shelley/Tx.hs b/eras/shelley/impl/src/Cardano/Ledger/Shelley/Tx.hs index 13f821da55e..37d5514969f 100644 --- a/eras/shelley/impl/src/Cardano/Ledger/Shelley/Tx.hs +++ b/eras/shelley/impl/src/Cardano/Ledger/Shelley/Tx.hs @@ -13,6 +13,7 @@ module Cardano.Ledger.Shelley.Tx ( sizeShelleyTxF, wireSizeShelleyTxF, segWitAnnTx, + segWitTx, mkBasicShelleyTx, shelleyMinFeeTx, witsFromTxWitnesses, diff --git a/eras/shelley/impl/src/Cardano/Ledger/Shelley/Tx/Internal.hs b/eras/shelley/impl/src/Cardano/Ledger/Shelley/Tx/Internal.hs index 0b1676edf8d..0d52b17aacf 100644 --- a/eras/shelley/impl/src/Cardano/Ledger/Shelley/Tx/Internal.hs +++ b/eras/shelley/impl/src/Cardano/Ledger/Shelley/Tx/Internal.hs @@ -40,6 +40,7 @@ module Cardano.Ledger.Shelley.Tx.Internal ( sizeShelleyTxF, wireSizeShelleyTxF, segWitAnnTx, + segWitTx, mkBasicShelleyTx, shelleyMinFeeTx, witsFromTxWitnesses, @@ -398,6 +399,33 @@ segWitAnnTx (maybeToStrictMaybe metadata) fullBytes +segWitTx :: + forall era. + EraTx era => + TxBody era -> + TxWits era -> + Maybe (TxAuxData era) -> + ShelleyTx era +segWitTx + body' + witnessSet + metadata = + let + wrappedMetadataBytes = case metadata of + Nothing -> Plain.serialize Plain.encodeNull + Just b -> Plain.serialize b + fullBytes = + Plain.serialize (Plain.encodeListLen 3) + <> Plain.serialize body' + <> Plain.serialize witnessSet + <> wrappedMetadataBytes + in + unsafeConstructTxWithBytes + body' + witnessSet + (maybeToStrictMaybe metadata) + fullBytes + -- ======================================== -- | Minimum fee calculation