From 87e0867608ff8694287d5a087e7c20218fd9b8f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20W=C3=B3jtowicz?= Date: Thu, 8 Aug 2024 10:02:56 +0200 Subject: [PATCH 1/3] Added txWireSize method to TxLimits class The new method leverages a corresponding method recently exposed by ledger side to provide encoded transaction sizes to eg. diffusion layer. --- cabal.project | 4 ++-- .../src/byron/Ouroboros/Consensus/Byron/Ledger/Mempool.hs | 6 ++++++ .../shelley/Ouroboros/Consensus/Shelley/Ledger/Mempool.hs | 8 +++++++- .../Ouroboros/Consensus/ByronSpec/Ledger/Mempool.hs | 1 + .../Ouroboros/Consensus/Network/NodeToNode.hs | 1 + .../Test/Consensus/HardFork/Combinator/A.hs | 1 + .../Test/Consensus/HardFork/Combinator/B.hs | 1 + .../mempool-bench/Bench/Consensus/Mempool/TestBlock.hs | 1 + .../Ouroboros/Consensus/HardFork/Combinator/Mempool.hs | 6 ++++++ .../Ouroboros/Consensus/Ledger/Dual.hs | 1 + .../Ouroboros/Consensus/Ledger/SupportsMempool.hs | 5 +++++ .../Ouroboros/Consensus/Mock/Ledger/Block.hs | 1 + .../Test/Consensus/Mempool/Fairness/TestBlock.hs | 1 + 13 files changed, 34 insertions(+), 3 deletions(-) diff --git a/cabal.project b/cabal.project index f78ed827ac..77242c571e 100644 --- a/cabal.project +++ b/cabal.project @@ -59,8 +59,8 @@ if impl(ghc >= 9.10) source-repository-package type: git location: https://github.com/IntersectMBO/ouroboros-network - tag: d900a38c55e02f5eed8c8d6d6a4671cd8c5acc6a - --sha256: sha256-VVccbWFmd9GlL2N/xNsKtXg2U2asGc4fIX1lLEo+Ar8= + tag: 388cc6906b83f41ac2da192b1fd89ab986b4af74 + --sha256: sha256-LUwryrP5jK+/c4lDitJf/oKg/DqLgbIc68bn83FsHI0= subdir: cardano-client cardano-ping diff --git a/ouroboros-consensus-cardano/src/byron/Ouroboros/Consensus/Byron/Ledger/Mempool.hs b/ouroboros-consensus-cardano/src/byron/Ouroboros/Consensus/Byron/Ledger/Mempool.hs index 8200ddf8c1..42d3c708a5 100644 --- a/ouroboros-consensus-cardano/src/byron/Ouroboros/Consensus/Byron/Ledger/Mempool.hs +++ b/ouroboros-consensus-cardano/src/byron/Ouroboros/Consensus/Byron/Ledger/Mempool.hs @@ -73,6 +73,7 @@ import Ouroboros.Consensus.Ledger.Abstract import Ouroboros.Consensus.Ledger.SupportsMempool import Ouroboros.Consensus.Util (ShowProxy (..)) import Ouroboros.Consensus.Util.Condense +import Ouroboros.Network.SizeInBytes as Network {------------------------------------------------------------------------------- Transactions @@ -127,6 +128,11 @@ instance LedgerSupportsMempool ByronBlock where instance TxLimits ByronBlock where type TxMeasure ByronBlock = IgnoringOverflow ByteSize32 + txWireSize = fromIntegral + . Strict.length + . CC.mempoolPayloadRecoverBytes + . toMempoolPayload + blockCapacityTxMeasure _cfg st = IgnoringOverflow $ ByteSize32 diff --git a/ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Mempool.hs b/ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Mempool.hs index ce57f1bb3c..3facb48d92 100644 --- a/ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Mempool.hs +++ b/ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Mempool.hs @@ -39,7 +39,7 @@ module Ouroboros.Consensus.Shelley.Ledger.Mempool ( import qualified Cardano.Crypto.Hash as Hash import qualified Cardano.Ledger.Allegra.Rules as AllegraEra import Cardano.Ledger.Alonzo.Core (Tx, TxSeq, bodyTxL, eraProtVerLow, - fromTxSeq, ppMaxBBSizeL, ppMaxBlockExUnitsL, sizeTxF) + fromTxSeq, ppMaxBBSizeL, ppMaxBlockExUnitsL, sizeTxF, wireSizeTxF) import qualified Cardano.Ledger.Alonzo.Rules as AlonzoEra import Cardano.Ledger.Alonzo.Scripts (ExUnits, ExUnits', pointWiseExUnits, unWrapExUnits) @@ -385,16 +385,19 @@ instance MaxTxSizeUTxO (ConwayEra c) where instance ShelleyCompatible p (ShelleyEra c) => TxLimits (ShelleyBlock p (ShelleyEra c)) where type TxMeasure (ShelleyBlock p (ShelleyEra c)) = IgnoringOverflow ByteSize32 + txWireSize (ShelleyTx _ tx) = fromIntegral (tx ^. wireSizeTxF) txMeasure _cfg st tx = runValidation $ txInBlockSize st tx blockCapacityTxMeasure _cfg = txsMaxBytes instance ShelleyCompatible p (AllegraEra c) => TxLimits (ShelleyBlock p (AllegraEra c)) where type TxMeasure (ShelleyBlock p (AllegraEra c)) = IgnoringOverflow ByteSize32 + txWireSize (ShelleyTx _ tx) = fromIntegral (tx ^. wireSizeTxF) txMeasure _cfg st tx = runValidation $ txInBlockSize st tx blockCapacityTxMeasure _cfg = txsMaxBytes instance ShelleyCompatible p (MaryEra c) => TxLimits (ShelleyBlock p (MaryEra c)) where type TxMeasure (ShelleyBlock p (MaryEra c)) = IgnoringOverflow ByteSize32 + txWireSize (ShelleyTx _ tx) = fromIntegral (tx ^. wireSizeTxF) txMeasure _cfg st tx = runValidation $ txInBlockSize st tx blockCapacityTxMeasure _cfg = txsMaxBytes @@ -485,6 +488,7 @@ instance ( ShelleyCompatible p (AlonzoEra c) ) => TxLimits (ShelleyBlock p (AlonzoEra c)) where type TxMeasure (ShelleyBlock p (AlonzoEra c)) = AlonzoMeasure + txWireSize (ShelleyTx _ tx) = fromIntegral (tx ^. wireSizeTxF) txMeasure _cfg st tx = runValidation $ txMeasureAlonzo st tx blockCapacityTxMeasure _cfg = blockCapacityAlonzoMeasure @@ -582,6 +586,7 @@ instance ( ShelleyCompatible p (BabbageEra c) ) => TxLimits (ShelleyBlock p (BabbageEra c)) where type TxMeasure (ShelleyBlock p (BabbageEra c)) = ConwayMeasure + txWireSize (ShelleyTx _ tx) = fromIntegral (tx ^. wireSizeTxF) txMeasure _cfg st tx = runValidation $ txMeasureBabbage st tx blockCapacityTxMeasure _cfg = blockCapacityConwayMeasure @@ -589,5 +594,6 @@ instance ( ShelleyCompatible p (ConwayEra c) ) => TxLimits (ShelleyBlock p (ConwayEra c)) where type TxMeasure (ShelleyBlock p (ConwayEra c)) = ConwayMeasure + txWireSize (ShelleyTx _ tx) = fromIntegral (tx ^. wireSizeTxF) txMeasure _cfg st tx = runValidation $ txMeasureConway st tx blockCapacityTxMeasure _cfg = blockCapacityConwayMeasure diff --git a/ouroboros-consensus-cardano/src/unstable-byronspec/Ouroboros/Consensus/ByronSpec/Ledger/Mempool.hs b/ouroboros-consensus-cardano/src/unstable-byronspec/Ouroboros/Consensus/ByronSpec/Ledger/Mempool.hs index 24f9e2453b..f2512ae12d 100644 --- a/ouroboros-consensus-cardano/src/unstable-byronspec/Ouroboros/Consensus/ByronSpec/Ledger/Mempool.hs +++ b/ouroboros-consensus-cardano/src/unstable-byronspec/Ouroboros/Consensus/ByronSpec/Ledger/Mempool.hs @@ -57,6 +57,7 @@ instance TxLimits ByronSpecBlock where type TxMeasure ByronSpecBlock = IgnoringOverflow ByteSize32 -- Dummy values, as these are not used in practice. + txWireSize = const . fromIntegral $ (0 :: Int) blockCapacityTxMeasure _cfg _st = IgnoringOverflow $ ByteSize32 1 txMeasure _cfg _st _tx = pure $ IgnoringOverflow $ ByteSize32 0 diff --git a/ouroboros-consensus-diffusion/src/ouroboros-consensus-diffusion/Ouroboros/Consensus/Network/NodeToNode.hs b/ouroboros-consensus-diffusion/src/ouroboros-consensus-diffusion/Ouroboros/Consensus/Network/NodeToNode.hs index d9de522cd7..6d0aef8f34 100644 --- a/ouroboros-consensus-diffusion/src/ouroboros-consensus-diffusion/Ouroboros/Consensus/Network/NodeToNode.hs +++ b/ouroboros-consensus-diffusion/src/ouroboros-consensus-diffusion/Ouroboros/Consensus/Network/NodeToNode.hs @@ -748,6 +748,7 @@ mkApps kernel Tracers {..} mkCodecs ByteLimits {..} genChainSyncTimeout lopBucke (getSharedTxStateVar kernel) (mapTxSubmissionMempoolReader txForgetValidated $ getMempoolReader (getMempool kernel)) + txWireSize them $ \api -> runServer (newTxSubmissionServer api) diff --git a/ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/HardFork/Combinator/A.hs b/ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/HardFork/Combinator/A.hs index 4aa3b65074..4c24bc5edc 100644 --- a/ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/HardFork/Combinator/A.hs +++ b/ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/HardFork/Combinator/A.hs @@ -330,6 +330,7 @@ instance LedgerSupportsMempool BlockA where instance TxLimits BlockA where type TxMeasure BlockA = IgnoringOverflow ByteSize32 + txWireSize = const . fromIntegral $ (0 :: Int) blockCapacityTxMeasure _cfg _st = IgnoringOverflow $ ByteSize32 $ 100 * 1024 -- arbitrary txMeasure _cfg _st _tx = pure $ IgnoringOverflow $ ByteSize32 0 diff --git a/ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/HardFork/Combinator/B.hs b/ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/HardFork/Combinator/B.hs index 7c45c64137..3119f87bf9 100644 --- a/ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/HardFork/Combinator/B.hs +++ b/ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/HardFork/Combinator/B.hs @@ -266,6 +266,7 @@ instance LedgerSupportsMempool BlockB where instance TxLimits BlockB where type TxMeasure BlockB = IgnoringOverflow ByteSize32 + txWireSize = const . fromIntegral $ (0 :: Int) blockCapacityTxMeasure _cfg _st = IgnoringOverflow $ ByteSize32 $ 100 * 1024 -- arbitrary txMeasure _cfg _st _tx = pure $ IgnoringOverflow $ ByteSize32 0 diff --git a/ouroboros-consensus/bench/mempool-bench/Bench/Consensus/Mempool/TestBlock.hs b/ouroboros-consensus/bench/mempool-bench/Bench/Consensus/Mempool/TestBlock.hs index 2d865028bb..df84700e7b 100644 --- a/ouroboros-consensus/bench/mempool-bench/Bench/Consensus/Mempool/TestBlock.hs +++ b/ouroboros-consensus/bench/mempool-bench/Bench/Consensus/Mempool/TestBlock.hs @@ -151,6 +151,7 @@ instance Ledger.LedgerSupportsMempool TestBlock where instance Ledger.TxLimits TestBlock where type TxMeasure TestBlock = Ledger.IgnoringOverflow Ledger.ByteSize32 + txWireSize = fromIntegral . Ledger.unByteSize32 . txSize -- We tweaked this in such a way that we test the case in which we exceed the -- maximum mempool capacity. The value used here depends on 'txInBlockSize'. blockCapacityTxMeasure _cfg _st = diff --git a/ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/HardFork/Combinator/Mempool.hs b/ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/HardFork/Combinator/Mempool.hs index a987cb7c32..d7a08275ec 100644 --- a/ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/HardFork/Combinator/Mempool.hs +++ b/ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/HardFork/Combinator/Mempool.hs @@ -119,6 +119,12 @@ instance CanHardFork xs => LedgerSupportsMempool (HardForkBlock xs) where instance CanHardFork xs => TxLimits (HardForkBlock xs) where type TxMeasure (HardForkBlock xs) = HardForkTxMeasure xs + txWireSize = + hcollapse + . hcmap proxySingle (K . txWireSize) + . getOneEraGenTx + . getHardForkGenTx + blockCapacityTxMeasure HardForkLedgerConfig{..} (TickedHardForkLedgerState transition hardForkState) diff --git a/ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Ledger/Dual.hs b/ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Ledger/Dual.hs index a5dc517634..3aefd79c5d 100644 --- a/ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Ledger/Dual.hs +++ b/ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Ledger/Dual.hs @@ -625,6 +625,7 @@ instance Bridge m a => LedgerSupportsMempool (DualBlock m a) where instance Bridge m a => TxLimits (DualBlock m a) where type TxMeasure (DualBlock m a) = TxMeasure m + txWireSize = txWireSize . dualGenTxMain txMeasure DualLedgerConfig{..} TickedDualLedgerState{..} DualGenTx{..} = do mapExcept (inj +++ id) $ txMeasure dualLedgerConfigMain tickedDualLedgerStateMain dualGenTxMain diff --git a/ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Ledger/SupportsMempool.hs b/ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Ledger/SupportsMempool.hs index 3a218241f3..11b494ae0b 100644 --- a/ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Ledger/SupportsMempool.hs +++ b/ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Ledger/SupportsMempool.hs @@ -37,6 +37,7 @@ import NoThunks.Class import Ouroboros.Consensus.Block.Abstract import Ouroboros.Consensus.Ledger.Abstract import Ouroboros.Consensus.Ticked +import Ouroboros.Network.SizeInBytes as Network -- | Generalized transaction -- @@ -179,6 +180,10 @@ class ( Measure (TxMeasure blk) -- | The (possibly multi-dimensional) size of a transaction in a block. type TxMeasure blk + -- | The size of the transaction from the perspective of diffusion layer + -- + txWireSize :: GenTx blk -> Network.SizeInBytes + -- | The various sizes (bytes, Plutus script ExUnits, etc) of a tx /when it's -- in a block/ -- diff --git a/ouroboros-consensus/src/unstable-mock-block/Ouroboros/Consensus/Mock/Ledger/Block.hs b/ouroboros-consensus/src/unstable-mock-block/Ouroboros/Consensus/Mock/Ledger/Block.hs index c006b0e6f0..30a2978c44 100644 --- a/ouroboros-consensus/src/unstable-mock-block/Ouroboros/Consensus/Mock/Ledger/Block.hs +++ b/ouroboros-consensus/src/unstable-mock-block/Ouroboros/Consensus/Mock/Ledger/Block.hs @@ -438,6 +438,7 @@ instance MockProtocolSpecific c ext instance TxLimits (SimpleBlock c ext) where type TxMeasure (SimpleBlock c ext) = IgnoringOverflow ByteSize32 + txWireSize = fromIntegral . unByteSize32 . txSize -- Large value so that the Mempool tests never run out of capacity when they -- don't override it. -- diff --git a/ouroboros-consensus/test/consensus-test/Test/Consensus/Mempool/Fairness/TestBlock.hs b/ouroboros-consensus/test/consensus-test/Test/Consensus/Mempool/Fairness/TestBlock.hs index 82131b3479..1fe4bda4cc 100644 --- a/ouroboros-consensus/test/consensus-test/Test/Consensus/Mempool/Fairness/TestBlock.hs +++ b/ouroboros-consensus/test/consensus-test/Test/Consensus/Mempool/Fairness/TestBlock.hs @@ -91,6 +91,7 @@ instance Ledger.LedgerSupportsMempool TestBlock where instance Ledger.TxLimits TestBlock where type TxMeasure TestBlock = Ledger.IgnoringOverflow Ledger.ByteSize32 + txWireSize = fromIntegral . Ledger.unByteSize32 . txSize . unGenTx blockCapacityTxMeasure _cfg _st = -- The tests will override this value. By using 1, @computeMempoolCapacity@ -- can be exactly what each test requests. From 37607c52249e9a6e86de4571c7a1bb925bf7be55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20W=C3=B3jtowicz?= Date: Thu, 8 Aug 2024 10:21:54 +0200 Subject: [PATCH 2/3] update changelog --- ...08_125131_marcin.wojtowicz_tx_wire_size.md | 21 +++++++++++++++++ ...08_125632_marcin.wojtowicz_tx_wire_size.md | 23 +++++++++++++++++++ ...08_101916_marcin.wojtowicz_tx_wire_size.md | 23 +++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 ouroboros-consensus-cardano/changelog.d/20240808_125131_marcin.wojtowicz_tx_wire_size.md create mode 100644 ouroboros-consensus-diffusion/changelog.d/20240808_125632_marcin.wojtowicz_tx_wire_size.md create mode 100644 ouroboros-consensus/changelog.d/20240808_101916_marcin.wojtowicz_tx_wire_size.md diff --git a/ouroboros-consensus-cardano/changelog.d/20240808_125131_marcin.wojtowicz_tx_wire_size.md b/ouroboros-consensus-cardano/changelog.d/20240808_125131_marcin.wojtowicz_tx_wire_size.md new file mode 100644 index 0000000000..6d83f5fa2b --- /dev/null +++ b/ouroboros-consensus-cardano/changelog.d/20240808_125131_marcin.wojtowicz_tx_wire_size.md @@ -0,0 +1,21 @@ + + + + +### Breaking + +- Implement txWireSize of TxLimits instantiations for Byron and Shelley diff --git a/ouroboros-consensus-diffusion/changelog.d/20240808_125632_marcin.wojtowicz_tx_wire_size.md b/ouroboros-consensus-diffusion/changelog.d/20240808_125632_marcin.wojtowicz_tx_wire_size.md new file mode 100644 index 0000000000..b774e09a01 --- /dev/null +++ b/ouroboros-consensus-diffusion/changelog.d/20240808_125632_marcin.wojtowicz_tx_wire_size.md @@ -0,0 +1,23 @@ + + + + +### Non-Breaking + +- Provide txWireSize to tx-submission protocol + + diff --git a/ouroboros-consensus/changelog.d/20240808_101916_marcin.wojtowicz_tx_wire_size.md b/ouroboros-consensus/changelog.d/20240808_101916_marcin.wojtowicz_tx_wire_size.md new file mode 100644 index 0000000000..69ad0fb64c --- /dev/null +++ b/ouroboros-consensus/changelog.d/20240808_101916_marcin.wojtowicz_tx_wire_size.md @@ -0,0 +1,23 @@ + + + + +### Breaking + +- Added txWireSize method to TxLimits class to provide + a CBOR-encoded transaction size as it is when transmitted + over the network. From f2f5a433bfb3d36beafd8c5f07f90408fc253304 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20W=C3=B3jtowicz?= Date: Tue, 24 Sep 2024 09:28:44 +0200 Subject: [PATCH 3/3] use public API --- .../ouroboros-consensus-diffusion.cabal | 1 - .../Ouroboros/Consensus/NodeKernel.hs | 8 +++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/ouroboros-consensus-diffusion/ouroboros-consensus-diffusion.cabal b/ouroboros-consensus-diffusion/ouroboros-consensus-diffusion.cabal index 930a333854..82f6b6dd50 100644 --- a/ouroboros-consensus-diffusion/ouroboros-consensus-diffusion.cabal +++ b/ouroboros-consensus-diffusion/ouroboros-consensus-diffusion.cabal @@ -99,7 +99,6 @@ library serialise ^>=0.2, si-timers ^>=1.5, strict-stm ^>=1.5, - strict-mvar ^>=1.5, text, time, transformers, diff --git a/ouroboros-consensus-diffusion/src/ouroboros-consensus-diffusion/Ouroboros/Consensus/NodeKernel.hs b/ouroboros-consensus-diffusion/src/ouroboros-consensus-diffusion/Ouroboros/Consensus/NodeKernel.hs index e5ac089e07..2ab1e7bc11 100644 --- a/ouroboros-consensus-diffusion/src/ouroboros-consensus-diffusion/Ouroboros/Consensus/NodeKernel.hs +++ b/ouroboros-consensus-diffusion/src/ouroboros-consensus-diffusion/Ouroboros/Consensus/NodeKernel.hs @@ -27,7 +27,6 @@ module Ouroboros.Consensus.NodeKernel ( ) where -import qualified Control.Concurrent.Class.MonadMVar.Strict as StrictSTM import qualified Control.Concurrent.Class.MonadSTM as LazySTM import qualified Control.Concurrent.Class.MonadSTM.Strict as StrictSTM import Control.DeepSeq (force) @@ -43,7 +42,6 @@ import Data.Functor ((<&>)) import Data.Hashable (Hashable) import Data.List.NonEmpty (NonEmpty) import Data.Map.Strict (Map) -import qualified Data.Map.Strict as Map import Data.Maybe (isJust, mapMaybe) import Data.Proxy import qualified Data.Text as Text @@ -114,8 +112,8 @@ import Ouroboros.Network.TxSubmission.Inbound (TxSubmissionMempoolWriter) import qualified Ouroboros.Network.TxSubmission.Inbound as Inbound import Ouroboros.Network.TxSubmission.Inbound.Registry - (SharedTxStateVar, TxChannels (..), TxChannelsVar, - decisionLogicThread, newSharedTxStateVar) + (SharedTxStateVar, TxChannelsVar, + decisionLogicThread, newSharedTxStateVar, newTxChannelsVar) import Ouroboros.Network.TxSubmission.Mempool.Reader (TxSubmissionMempoolReader) import qualified Ouroboros.Network.TxSubmission.Mempool.Reader as MempoolReader @@ -294,7 +292,7 @@ initNodeKernel args@NodeKernelArgs { registry, cfg, tracers ps_POLICY_PEER_SHARE_STICKY_TIME ps_POLICY_PEER_SHARE_MAX_PEERS - txChannelsVar <- StrictSTM.newMVar (TxChannels Map.empty) + txChannelsVar <- newTxChannelsVar sharedTxStateVar <- newSharedTxStateVar case gnkaGetLoEFragment genesisArgs of