Skip to content

Commit

Permalink
Merge pull request #4521 from IntersectMBO/mwojtowicz/tx-wire-size
Browse files Browse the repository at this point in the history
Added method to compute over-the-wire CBOR encoded transaction size
  • Loading branch information
crocodile-dentist authored Aug 8, 2024
2 parents 6c9f88f + d1b8e6c commit 94752aa
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 2 deletions.
4 changes: 4 additions & 0 deletions eras/allegra/impl/src/Cardano/Ledger/Allegra/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import Cardano.Ledger.Shelley.Tx (
mkBasicShelleyTx,
shelleyMinFeeTx,
sizeShelleyTxF,
wireSizeShelleyTxF,
witsShelleyTxL,
)
import qualified Data.Set as Set (map)
Expand All @@ -59,6 +60,9 @@ instance Crypto c => EraTx (AllegraEra c) where
sizeTxF = sizeShelleyTxF
{-# INLINE sizeTxF #-}

wireSizeTxF = wireSizeShelleyTxF
{-# INLINE wireSizeTxF #-}

validateNativeScript = validateTimelock
{-# INLINE validateNativeScript #-}

Expand Down
2 changes: 2 additions & 0 deletions eras/alonzo/impl/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 1.10.1.0

* Added `wireSizeAlonzoTxF`

### `testlib`

* Export `fixupRedeemerIndices` from `Alonzo.ImpTest`
Expand Down
22 changes: 21 additions & 1 deletion eras/alonzo/impl/src/Cardano/Ledger/Alonzo/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ module Cardano.Ledger.Alonzo.Tx (
witsAlonzoTxL,
auxDataAlonzoTxL,
sizeAlonzoTxF,
wireSizeAlonzoTxF,
isValidAlonzoTxL,
txdats',
txscripts',
Expand Down Expand Up @@ -146,6 +147,7 @@ import Data.Maybe.Strict (
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Typeable (Typeable)
import Data.Word (Word32)
import GHC.Generics (Generic)
import Lens.Micro hiding (set)
import NoThunks.Class (NoThunks)
Expand Down Expand Up @@ -189,6 +191,9 @@ instance Crypto c => EraTx (AlonzoEra c) where
sizeTxF = sizeAlonzoTxF
{-# INLINE sizeTxF #-}

wireSizeTxF = wireSizeAlonzoTxF
{-# INLINE wireSizeTxF #-}

validateNativeScript = validateTimelock
{-# INLINE validateNativeScript #-}

Expand Down Expand Up @@ -235,7 +240,7 @@ auxDataAlonzoTxL :: Lens' (AlonzoTx era) (StrictMaybe (TxAuxData era))
auxDataAlonzoTxL = lens auxiliaryData (\tx txTxAuxData -> tx {auxiliaryData = txTxAuxData})
{-# INLINEABLE auxDataAlonzoTxL #-}

-- | txsize computes the length of the serialised bytes
-- | txsize computes the length of the serialised bytes (for estimations)
sizeAlonzoTxF :: forall era. EraTx era => SimpleGetter (AlonzoTx era) Integer
sizeAlonzoTxF =
to $
Expand All @@ -245,6 +250,21 @@ sizeAlonzoTxF =
. toCBORForSizeComputation
{-# INLINEABLE sizeAlonzoTxF #-}

-- | txsize computes the length of the serialised bytes (actual size)
wireSizeAlonzoTxF :: forall era. EraTx era => SimpleGetter (AlonzoTx era) Word32
wireSizeAlonzoTxF =
to $
checkedFromIntegral
. LBS.length
. serialize (eraProtVerLow @era)
. encCBOR
where
checkedFromIntegral n =
if n <= fromIntegral (maxBound :: Word32)
then fromIntegral n
else error $ "Impossible: Size of the transaction is too big: " ++ show n
{-# INLINEABLE wireSizeAlonzoTxF #-}

isValidAlonzoTxL :: Lens' (AlonzoTx era) IsValid
isValidAlonzoTxL = lens isValid (\tx valid -> tx {isValid = valid})
{-# INLINEABLE isValidAlonzoTxL #-}
Expand Down
3 changes: 3 additions & 0 deletions eras/babbage/impl/src/Cardano/Ledger/Babbage/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ instance Crypto c => EraTx (BabbageEra c) where
sizeTxF = sizeAlonzoTxF
{-# INLINE sizeTxF #-}

wireSizeTxF = wireSizeAlonzoTxF
{-# INLINE wireSizeTxF #-}

validateNativeScript = validateTimelock
{-# INLINE validateNativeScript #-}

Expand Down
4 changes: 4 additions & 0 deletions eras/conway/impl/src/Cardano/Ledger/Conway/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Cardano.Ledger.Alonzo.Tx (
isValidAlonzoTxL,
mkBasicAlonzoTx,
sizeAlonzoTxF,
wireSizeAlonzoTxF,
witsAlonzoTxL,
)
import Cardano.Ledger.Alonzo.TxSeq (
Expand Down Expand Up @@ -65,6 +66,9 @@ instance Crypto c => EraTx (ConwayEra c) where
sizeTxF = sizeAlonzoTxF
{-# INLINE sizeTxF #-}

wireSizeTxF = wireSizeAlonzoTxF
{-# INLINE wireSizeTxF #-}

validateNativeScript = validateTimelock
{-# INLINE validateNativeScript #-}

Expand Down
4 changes: 4 additions & 0 deletions eras/mary/impl/src/Cardano/Ledger/Mary/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import Cardano.Ledger.Shelley.Tx (
mkBasicShelleyTx,
shelleyMinFeeTx,
sizeShelleyTxF,
wireSizeShelleyTxF,
witsShelleyTxL,
)

Expand All @@ -47,6 +48,9 @@ instance Crypto c => EraTx (MaryEra c) where
sizeTxF = sizeShelleyTxF
{-# INLINE sizeTxF #-}

wireSizeTxF = wireSizeShelleyTxF
{-# INLINE wireSizeTxF #-}

validateNativeScript = validateTimelock
{-# INLINE validateNativeScript #-}

Expand Down
2 changes: 2 additions & 0 deletions eras/shelley/impl/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

* Add `translateToShelleyLedgerStateFromUtxo`

* Added `wireSizeShelleyTxF`

### `testlib`

* Add `submitFailingTxM`
Expand Down
13 changes: 13 additions & 0 deletions eras/shelley/impl/src/Cardano/Ledger/Shelley/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module Cardano.Ledger.Shelley.Tx (
witsShelleyTxL,
auxDataShelleyTxL,
sizeShelleyTxF,
wireSizeShelleyTxF,
segwitTx,
mkBasicShelleyTx,
shelleyMinFeeTx,
Expand Down Expand Up @@ -86,6 +87,7 @@ import Data.Maybe.Strict (
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Typeable (Typeable)
import Data.Word (Word32)
import GHC.Generics (Generic)
import Lens.Micro (Lens', SimpleGetter, lens, to, (^.))
import NoThunks.Class (NoThunks (..))
Expand Down Expand Up @@ -168,6 +170,14 @@ sizeShelleyTxF :: Era era => SimpleGetter (ShelleyTx era) Integer
sizeShelleyTxF = to (\(TxConstr (Memo _ bytes)) -> fromIntegral $ SBS.length bytes)
{-# INLINEABLE sizeShelleyTxF #-}

wireSizeShelleyTxF :: Era era => SimpleGetter (ShelleyTx era) Word32
wireSizeShelleyTxF = to $ \(TxConstr (Memo _ bytes)) ->
let n = SBS.length bytes
in if n <= fromIntegral (maxBound :: Word32)
then fromIntegral n
else error $ "Impossible: Size of the transaction is too big: " ++ show n
{-# INLINEABLE wireSizeShelleyTxF #-}

mkShelleyTx :: EraTx era => ShelleyTxRaw era -> ShelleyTx era
mkShelleyTx = TxConstr . memoBytes . encodeShelleyTxRaw
{-# INLINEABLE mkShelleyTx #-}
Expand Down Expand Up @@ -200,6 +210,9 @@ instance Crypto c => EraTx (ShelleyEra c) where
sizeTxF = sizeShelleyTxF
{-# INLINE sizeTxF #-}

wireSizeTxF = wireSizeShelleyTxF
{-# INLINE wireSizeTxF #-}

validateNativeScript = validateMultiSig
{-# INLINE validateNativeScript #-}

Expand Down
1 change: 1 addition & 0 deletions libs/cardano-ledger-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Change default implementation of `translateEra`
* Add `EraGenesis` and `Genesis` type family. New `NoGenesis` type to be used for eras that do not have a genesis file
* Move `ensureMinCoinTxOut` from `cardano-ledger-api` to `Cardano.Ledger.Tools`
* Add `wireSizeTxF` to `EraTx` class

### `testlib`

Expand Down
6 changes: 5 additions & 1 deletion libs/cardano-ledger-core/src/Cardano/Ledger/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ import Data.Maybe.Strict (StrictMaybe)
import Data.Sequence.Strict (StrictSeq)
import Data.Set (Set)
import Data.Void (Void)
import Data.Word (Word64)
import Data.Word (Word32, Word64)
import GHC.Stack (HasCallStack)
import Lens.Micro
import NoThunks.Class (NoThunks)
Expand Down Expand Up @@ -153,8 +153,12 @@ class

auxDataTxL :: Lens' (Tx era) (StrictMaybe (AuxiliaryData era))

-- | For fee calculation and estimations of impact on block space
sizeTxF :: SimpleGetter (Tx era) Integer

-- | For end use by eg. diffusion layer in transaction submission protocol
wireSizeTxF :: SimpleGetter (Tx era) Word32

-- | Using information from the transaction validate the supplied native script.
validateNativeScript :: Tx era -> NativeScript era -> Bool

Expand Down

0 comments on commit 94752aa

Please sign in to comment.