Skip to content

Commit

Permalink
Add serialisation code for ValidatedTx.
Browse files Browse the repository at this point in the history
This serialisation is only for use when transmitting Tx from node to
node, or wallet to node.
  • Loading branch information
nc6 committed Jul 20, 2021
1 parent b75367f commit 0ab8d78
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions alonzo/impl/src/Cardano/Ledger/Alonzo/Tx.hs
Expand Up @@ -66,6 +66,7 @@ module Cardano.Ledger.Alonzo.Tx
segwitTx,
-- Other
toCBORForSizeComputation,
toCBORForMempoolSubmission,
)
where

Expand All @@ -89,6 +90,7 @@ import Cardano.Ledger.Alonzo.Scripts
( CostModel,
ExUnits (..),
Prices,
Script,
Tag (..),
txscriptfee,
)
Expand Down Expand Up @@ -507,3 +509,68 @@ segwitTx
witnessSet
isval
(maybeToStrictMaybe metadata)

--------------------------------------------------------------------------------
-- Mempool Serialisation
--
-- We do not store the Tx bytes for the following reasons:
-- - A Tx serialised in this way never forms part of any hashed structure, hence
-- we do not worry about the serialisation changing and thus seeing a new
-- hash.
-- - The three principal components of this Tx already store their own bytes;
-- here we simply concatenate them. The final component, `IsValidating`, is
-- just a flag and very cheap to serialise.
--------------------------------------------------------------------------------

-- | Encode to CBOR for the purposes of transmission from node to node, or from
-- wallet to node.
--
-- Note that this serialisation is neither the serialisation used on-chain
-- (where Txs are deconstructed using segwit), nor the serialisation used for
-- computing the transaction size (which omits the `IsValidating` field for
-- compatibility with Mary - see 'toCBORForSizeComputation').
toCBORForMempoolSubmission ::
( Typeable era,
ToCBOR (Core.TxBody era),
ToCBOR (Core.AuxiliaryData era)
) =>
ValidatedTx era ->
Encoding
toCBORForMempoolSubmission
ValidatedTx {body, wits, auxiliaryData, isValidating} =
encode $
Rec ValidatedTx
!> To body
!> To wits
!> To isValidating
!> E (encodeNullMaybe toCBOR . strictMaybeToMaybe) auxiliaryData

instance
( Typeable era,
ToCBOR (Core.TxBody era),
ToCBOR (Core.AuxiliaryData era)
) =>
ToCBOR (ValidatedTx era)
where
toCBOR = toCBORForMempoolSubmission

instance
( Era era,
FromCBOR (Annotator (Core.TxBody era)),
FromCBOR (Annotator (Core.AuxiliaryData era)),
FromCBOR (Annotator (Core.Witnesses era)),
ValidateScript era,
Core.Script era ~ Script era
) =>
FromCBOR (Annotator (ValidatedTx era))
where
fromCBOR =
decode $
Ann (RecD ValidatedTx)
<*! From
<*! From
<*! Ann From
<*! D
( sequence . maybeToStrictMaybe
<$> decodeNullMaybe fromCBOR
)

0 comments on commit 0ab8d78

Please sign in to comment.