From 2195d2b531650af10f940491ea5d9d33558a84b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20W=C3=B3jtowicz?= Date: Thu, 1 Aug 2024 13:39:36 +0200 Subject: [PATCH] Added method to compute over-the-wire CBOR encoded transaction size --- .../impl/src/Cardano/Ledger/Allegra/Tx.hs | 3 +++ .../alonzo/impl/src/Cardano/Ledger/Alonzo/Tx.hs | 17 +++++++++++++---- .../impl/src/Cardano/Ledger/Babbage/Tx.hs | 7 ++++++- .../conway/impl/src/Cardano/Ledger/Conway/Tx.hs | 8 +++++++- eras/mary/impl/src/Cardano/Ledger/Mary/Tx.hs | 3 +++ .../impl/src/Cardano/Ledger/Shelley/Tx.hs | 5 ++++- libs/cardano-ledger-core/CHANGELOG.md | 4 ++++ .../cardano-ledger-core.cabal | 2 +- .../src/Cardano/Ledger/Core.hs | 6 +++++- 9 files changed, 46 insertions(+), 9 deletions(-) diff --git a/eras/allegra/impl/src/Cardano/Ledger/Allegra/Tx.hs b/eras/allegra/impl/src/Cardano/Ledger/Allegra/Tx.hs index 23da633d77..525c91b102 100644 --- a/eras/allegra/impl/src/Cardano/Ledger/Allegra/Tx.hs +++ b/eras/allegra/impl/src/Cardano/Ledger/Allegra/Tx.hs @@ -59,6 +59,9 @@ instance Crypto c => EraTx (AllegraEra c) where sizeTxF = sizeShelleyTxF {-# INLINE sizeTxF #-} + wireSizeTxF = sizeShelleyTxF + {-# INLINE wireSizeTxF #-} + validateNativeScript = validateTimelock {-# INLINE validateNativeScript #-} diff --git a/eras/alonzo/impl/src/Cardano/Ledger/Alonzo/Tx.hs b/eras/alonzo/impl/src/Cardano/Ledger/Alonzo/Tx.hs index 52ea88c6c0..6440ba3367 100644 --- a/eras/alonzo/impl/src/Cardano/Ledger/Alonzo/Tx.hs +++ b/eras/alonzo/impl/src/Cardano/Ledger/Alonzo/Tx.hs @@ -11,6 +11,7 @@ {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE StandaloneDeriving #-} {-# LANGUAGE TypeApplications #-} @@ -186,9 +187,12 @@ instance Crypto c => EraTx (AlonzoEra c) where auxDataTxL = auxDataAlonzoTxL {-# INLINE auxDataTxL #-} - sizeTxF = sizeAlonzoTxF + sizeTxF = sizeAlonzoTxF toCBORForSizeComputation {-# INLINE sizeTxF #-} + wireSizeTxF = sizeAlonzoTxF encCBOR + {-# INLINE wireSizeTxF #-} + validateNativeScript = validateTimelock {-# INLINE validateNativeScript #-} @@ -236,13 +240,18 @@ auxDataAlonzoTxL = lens auxiliaryData (\tx txTxAuxData -> tx {auxiliaryData = tx {-# INLINEABLE auxDataAlonzoTxL #-} -- | txsize computes the length of the serialised bytes -sizeAlonzoTxF :: forall era. EraTx era => SimpleGetter (AlonzoTx era) Integer -sizeAlonzoTxF = +sizeAlonzoTxF :: + forall era b. + (EraTx era, Num b) => + -- | encoding for CBOR size or mempool submission + (AlonzoTx era -> Encoding) -> + SimpleGetter (AlonzoTx era) b +sizeAlonzoTxF enc = to $ fromIntegral . LBS.length . serialize (eraProtVerLow @era) - . toCBORForSizeComputation + . enc {-# INLINEABLE sizeAlonzoTxF #-} isValidAlonzoTxL :: Lens' (AlonzoTx era) IsValid diff --git a/eras/babbage/impl/src/Cardano/Ledger/Babbage/Tx.hs b/eras/babbage/impl/src/Cardano/Ledger/Babbage/Tx.hs index 03180d62ee..63477b7686 100644 --- a/eras/babbage/impl/src/Cardano/Ledger/Babbage/Tx.hs +++ b/eras/babbage/impl/src/Cardano/Ledger/Babbage/Tx.hs @@ -9,6 +9,8 @@ module Cardano.Ledger.Babbage.Tx ( ) where +import Cardano.Ledger.Binary (EncCBOR (encCBOR)) + import Cardano.Ledger.Allegra.Tx (validateTimelock) import Cardano.Ledger.Alonzo.Tx as X import Cardano.Ledger.Alonzo.TxSeq ( @@ -47,9 +49,12 @@ instance Crypto c => EraTx (BabbageEra c) where auxDataTxL = auxDataAlonzoTxL {-# INLINE auxDataTxL #-} - sizeTxF = sizeAlonzoTxF + sizeTxF = sizeAlonzoTxF X.toCBORForSizeComputation {-# INLINE sizeTxF #-} + wireSizeTxF = sizeAlonzoTxF encCBOR + {-# INLINE wireSizeTxF #-} + validateNativeScript = validateTimelock {-# INLINE validateNativeScript #-} diff --git a/eras/conway/impl/src/Cardano/Ledger/Conway/Tx.hs b/eras/conway/impl/src/Cardano/Ledger/Conway/Tx.hs index b44038cc36..f96f53048d 100644 --- a/eras/conway/impl/src/Cardano/Ledger/Conway/Tx.hs +++ b/eras/conway/impl/src/Cardano/Ledger/Conway/Tx.hs @@ -12,6 +12,8 @@ module Cardano.Ledger.Conway.Tx ( ) where +import Cardano.Ledger.Binary (EncCBOR (encCBOR)) + import Cardano.Ledger.Allegra.Tx (validateTimelock) import Cardano.Ledger.Alonzo.Core (AlonzoEraTxWits) import Cardano.Ledger.Alonzo.Tx ( @@ -21,6 +23,7 @@ import Cardano.Ledger.Alonzo.Tx ( isValidAlonzoTxL, mkBasicAlonzoTx, sizeAlonzoTxF, + toCBORForSizeComputation, witsAlonzoTxL, ) import Cardano.Ledger.Alonzo.TxSeq ( @@ -62,9 +65,12 @@ instance Crypto c => EraTx (ConwayEra c) where auxDataTxL = auxDataAlonzoTxL {-# INLINE auxDataTxL #-} - sizeTxF = sizeAlonzoTxF + sizeTxF = sizeAlonzoTxF toCBORForSizeComputation {-# INLINE sizeTxF #-} + wireSizeTxF = sizeAlonzoTxF encCBOR + {-# INLINE wireSizeTxF #-} + validateNativeScript = validateTimelock {-# INLINE validateNativeScript #-} diff --git a/eras/mary/impl/src/Cardano/Ledger/Mary/Tx.hs b/eras/mary/impl/src/Cardano/Ledger/Mary/Tx.hs index d9e166e9f5..50d812738e 100644 --- a/eras/mary/impl/src/Cardano/Ledger/Mary/Tx.hs +++ b/eras/mary/impl/src/Cardano/Ledger/Mary/Tx.hs @@ -47,6 +47,9 @@ instance Crypto c => EraTx (MaryEra c) where sizeTxF = sizeShelleyTxF {-# INLINE sizeTxF #-} + wireSizeTxF = sizeShelleyTxF + {-# INLINE wireSizeTxF #-} + validateNativeScript = validateTimelock {-# INLINE validateNativeScript #-} diff --git a/eras/shelley/impl/src/Cardano/Ledger/Shelley/Tx.hs b/eras/shelley/impl/src/Cardano/Ledger/Shelley/Tx.hs index baced876e0..f305502c4e 100644 --- a/eras/shelley/impl/src/Cardano/Ledger/Shelley/Tx.hs +++ b/eras/shelley/impl/src/Cardano/Ledger/Shelley/Tx.hs @@ -164,7 +164,7 @@ auxDataShelleyTxL = {-# INLINEABLE auxDataShelleyTxL #-} -- | Size getter for `ShelleyTx`. -sizeShelleyTxF :: Era era => SimpleGetter (ShelleyTx era) Integer +sizeShelleyTxF :: (Era era, Num b) => SimpleGetter (ShelleyTx era) b sizeShelleyTxF = to (\(TxConstr (Memo _ bytes)) -> fromIntegral $ SBS.length bytes) {-# INLINEABLE sizeShelleyTxF #-} @@ -200,6 +200,9 @@ instance Crypto c => EraTx (ShelleyEra c) where sizeTxF = sizeShelleyTxF {-# INLINE sizeTxF #-} + wireSizeTxF = sizeShelleyTxF + {-# INLINE wireSizeTxF #-} + validateNativeScript = validateMultiSig {-# INLINE validateNativeScript #-} diff --git a/libs/cardano-ledger-core/CHANGELOG.md b/libs/cardano-ledger-core/CHANGELOG.md index b2f6108b2c..3a51b5b72d 100644 --- a/libs/cardano-ledger-core/CHANGELOG.md +++ b/libs/cardano-ledger-core/CHANGELOG.md @@ -1,5 +1,9 @@ # Version history for `cardano-ledger-core` +## 1.14.2.0 + +* Add `wireSizeTxF` to `EraTx` class + ## 1.14.1.0 ### `testlib` diff --git a/libs/cardano-ledger-core/cardano-ledger-core.cabal b/libs/cardano-ledger-core/cardano-ledger-core.cabal index bb33f7badc..b0d569a141 100644 --- a/libs/cardano-ledger-core/cardano-ledger-core.cabal +++ b/libs/cardano-ledger-core/cardano-ledger-core.cabal @@ -1,6 +1,6 @@ cabal-version: 3.0 name: cardano-ledger-core -version: 1.14.1.0 +version: 1.14.2.0 license: Apache-2.0 maintainer: operations@iohk.io author: IOHK diff --git a/libs/cardano-ledger-core/src/Cardano/Ledger/Core.hs b/libs/cardano-ledger-core/src/Cardano/Ledger/Core.hs index e2fe27b18a..96b3eb5101 100644 --- a/libs/cardano-ledger-core/src/Cardano/Ledger/Core.hs +++ b/libs/cardano-ledger-core/src/Cardano/Ledger/Core.hs @@ -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) @@ -153,8 +153,12 @@ class auxDataTxL :: Lens' (Tx era) (StrictMaybe (AuxiliaryData era)) + -- | For 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