Skip to content

Commit

Permalink
[shelley] - ShelleyTxSeq
Browse files Browse the repository at this point in the history
  • Loading branch information
teodanciu committed Jan 30, 2025
1 parent 61b4c03 commit 562fd85
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
1 change: 1 addition & 0 deletions eras/shelley/impl/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
28 changes: 25 additions & 3 deletions eras/shelley/impl/src/Cardano/Ledger/Shelley/BlockChain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ import Cardano.Ledger.BaseTypes (
strictMaybeToMaybe,
)
import Cardano.Ledger.Binary (
Annotated (..),
Annotator (..),
DecCBOR (decCBOR),
Decoder,
EncCBOR (..),
EncCBORGroup (..),
decodeAnnotated,
encodeFoldableEncoder,
encodeFoldableMapEncoder,
encodePreEncoded,
Expand All @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -239,6 +243,24 @@ 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
Annotated (wits :: Seq (TxWits era)) witsBS <- decodeAnnotated decCBOR
Annotated (metadataMap :: Map Int (TxAuxData era)) metadataBS <-
decodeAnnotated decCBOR
let metadata = indexLookupSeq b metadataMap
let txs = StrictSeq.forceToStrict $ Seq.zipWith3 segWitTx bodies wits metadata
pure $ TxSeq' txs bodiesBS witsBS metadataBS

slotToNonce :: SlotNo -> Nonce
slotToNonce (SlotNo s) = mkNonceFromNumber s

Expand Down
1 change: 1 addition & 0 deletions eras/shelley/impl/src/Cardano/Ledger/Shelley/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module Cardano.Ledger.Shelley.Tx (
sizeShelleyTxF,
wireSizeShelleyTxF,
segWitAnnTx,
segWitTx,
mkBasicShelleyTx,
shelleyMinFeeTx,
witsFromTxWitnesses,
Expand Down
28 changes: 28 additions & 0 deletions eras/shelley/impl/src/Cardano/Ledger/Shelley/Tx/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ module Cardano.Ledger.Shelley.Tx.Internal (
sizeShelleyTxF,
wireSizeShelleyTxF,
segWitAnnTx,
segWitTx,
mkBasicShelleyTx,
shelleyMinFeeTx,
witsFromTxWitnesses,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 562fd85

Please sign in to comment.