Skip to content

Commit 9a3532f

Browse files
Expose over-the-wire encoded transaction size (#1211)
# Description This change introduces a new method wireSizeTx for the LedgerSupportsMempool class. It provides actual CBOR encoded transaction size as it is when transmitted over the network, which the difffusion layer could exploit. Also note that: - New code should be properly tested (even if it does not add new features). - The fix for a regression should include a test that reproduces said regression. IntersectMBO/cardano-ledger#4521 IntersectMBO/ouroboros-network#4926
2 parents 35adef5 + f2f5a43 commit 9a3532f

File tree

18 files changed

+104
-9
lines changed

18 files changed

+104
-9
lines changed

cabal.project

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ if impl(ghc >= 9.10)
5959
source-repository-package
6060
type: git
6161
location: https://github.com/IntersectMBO/ouroboros-network
62-
tag: d900a38c55e02f5eed8c8d6d6a4671cd8c5acc6a
63-
--sha256: sha256-VVccbWFmd9GlL2N/xNsKtXg2U2asGc4fIX1lLEo+Ar8=
62+
tag: 388cc6906b83f41ac2da192b1fd89ab986b4af74
63+
--sha256: sha256-LUwryrP5jK+/c4lDitJf/oKg/DqLgbIc68bn83FsHI0=
6464
subdir:
6565
cardano-client
6666
cardano-ping
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!--
2+
A new scriv changelog fragment.
3+
4+
Uncomment the section that is right (remove the HTML comment wrapper).
5+
-->
6+
7+
<!--
8+
### Patch
9+
10+
- A bullet item for the Patch category.
11+
12+
-->
13+
<!--
14+
### Non-Breaking
15+
16+
- A bullet item for the Non-Breaking category.
17+
18+
-->
19+
### Breaking
20+
21+
- Implement txWireSize of TxLimits instantiations for Byron and Shelley

ouroboros-consensus-cardano/src/byron/Ouroboros/Consensus/Byron/Ledger/Mempool.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ import Ouroboros.Consensus.Ledger.Abstract
7373
import Ouroboros.Consensus.Ledger.SupportsMempool
7474
import Ouroboros.Consensus.Util (ShowProxy (..))
7575
import Ouroboros.Consensus.Util.Condense
76+
import Ouroboros.Network.SizeInBytes as Network
7677

7778
{-------------------------------------------------------------------------------
7879
Transactions
@@ -127,6 +128,11 @@ instance LedgerSupportsMempool ByronBlock where
127128
instance TxLimits ByronBlock where
128129
type TxMeasure ByronBlock = IgnoringOverflow ByteSize32
129130

131+
txWireSize = fromIntegral
132+
. Strict.length
133+
. CC.mempoolPayloadRecoverBytes
134+
. toMempoolPayload
135+
130136
blockCapacityTxMeasure _cfg st =
131137
IgnoringOverflow
132138
$ ByteSize32

ouroboros-consensus-cardano/src/shelley/Ouroboros/Consensus/Shelley/Ledger/Mempool.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ module Ouroboros.Consensus.Shelley.Ledger.Mempool (
3939
import qualified Cardano.Crypto.Hash as Hash
4040
import qualified Cardano.Ledger.Allegra.Rules as AllegraEra
4141
import Cardano.Ledger.Alonzo.Core (Tx, TxSeq, bodyTxL, eraProtVerLow,
42-
fromTxSeq, ppMaxBBSizeL, ppMaxBlockExUnitsL, sizeTxF)
42+
fromTxSeq, ppMaxBBSizeL, ppMaxBlockExUnitsL, sizeTxF, wireSizeTxF)
4343
import qualified Cardano.Ledger.Alonzo.Rules as AlonzoEra
4444
import Cardano.Ledger.Alonzo.Scripts (ExUnits, ExUnits',
4545
pointWiseExUnits, unWrapExUnits)
@@ -385,16 +385,19 @@ instance MaxTxSizeUTxO (ConwayEra c) where
385385

386386
instance ShelleyCompatible p (ShelleyEra c) => TxLimits (ShelleyBlock p (ShelleyEra c)) where
387387
type TxMeasure (ShelleyBlock p (ShelleyEra c)) = IgnoringOverflow ByteSize32
388+
txWireSize (ShelleyTx _ tx) = fromIntegral (tx ^. wireSizeTxF)
388389
txMeasure _cfg st tx = runValidation $ txInBlockSize st tx
389390
blockCapacityTxMeasure _cfg = txsMaxBytes
390391

391392
instance ShelleyCompatible p (AllegraEra c) => TxLimits (ShelleyBlock p (AllegraEra c)) where
392393
type TxMeasure (ShelleyBlock p (AllegraEra c)) = IgnoringOverflow ByteSize32
394+
txWireSize (ShelleyTx _ tx) = fromIntegral (tx ^. wireSizeTxF)
393395
txMeasure _cfg st tx = runValidation $ txInBlockSize st tx
394396
blockCapacityTxMeasure _cfg = txsMaxBytes
395397

396398
instance ShelleyCompatible p (MaryEra c) => TxLimits (ShelleyBlock p (MaryEra c)) where
397399
type TxMeasure (ShelleyBlock p (MaryEra c)) = IgnoringOverflow ByteSize32
400+
txWireSize (ShelleyTx _ tx) = fromIntegral (tx ^. wireSizeTxF)
398401
txMeasure _cfg st tx = runValidation $ txInBlockSize st tx
399402
blockCapacityTxMeasure _cfg = txsMaxBytes
400403

@@ -485,6 +488,7 @@ instance ( ShelleyCompatible p (AlonzoEra c)
485488
) => TxLimits (ShelleyBlock p (AlonzoEra c)) where
486489

487490
type TxMeasure (ShelleyBlock p (AlonzoEra c)) = AlonzoMeasure
491+
txWireSize (ShelleyTx _ tx) = fromIntegral (tx ^. wireSizeTxF)
488492
txMeasure _cfg st tx = runValidation $ txMeasureAlonzo st tx
489493
blockCapacityTxMeasure _cfg = blockCapacityAlonzoMeasure
490494

@@ -582,12 +586,14 @@ instance ( ShelleyCompatible p (BabbageEra c)
582586
) => TxLimits (ShelleyBlock p (BabbageEra c)) where
583587

584588
type TxMeasure (ShelleyBlock p (BabbageEra c)) = ConwayMeasure
589+
txWireSize (ShelleyTx _ tx) = fromIntegral (tx ^. wireSizeTxF)
585590
txMeasure _cfg st tx = runValidation $ txMeasureBabbage st tx
586591
blockCapacityTxMeasure _cfg = blockCapacityConwayMeasure
587592

588593
instance ( ShelleyCompatible p (ConwayEra c)
589594
) => TxLimits (ShelleyBlock p (ConwayEra c)) where
590595

591596
type TxMeasure (ShelleyBlock p (ConwayEra c)) = ConwayMeasure
597+
txWireSize (ShelleyTx _ tx) = fromIntegral (tx ^. wireSizeTxF)
592598
txMeasure _cfg st tx = runValidation $ txMeasureConway st tx
593599
blockCapacityTxMeasure _cfg = blockCapacityConwayMeasure

ouroboros-consensus-cardano/src/unstable-byronspec/Ouroboros/Consensus/ByronSpec/Ledger/Mempool.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ instance TxLimits ByronSpecBlock where
5757
type TxMeasure ByronSpecBlock = IgnoringOverflow ByteSize32
5858

5959
-- Dummy values, as these are not used in practice.
60+
txWireSize = const . fromIntegral $ (0 :: Int)
6061
blockCapacityTxMeasure _cfg _st = IgnoringOverflow $ ByteSize32 1
6162

6263
txMeasure _cfg _st _tx = pure $ IgnoringOverflow $ ByteSize32 0
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!--
2+
A new scriv changelog fragment.
3+
4+
Uncomment the section that is right (remove the HTML comment wrapper).
5+
-->
6+
7+
<!--
8+
### Patch
9+
10+
- A bullet item for the Patch category.
11+
12+
-->
13+
14+
### Non-Breaking
15+
16+
- Provide txWireSize to tx-submission protocol
17+
18+
<!--
19+
### Breaking
20+
21+
- A bullet item for the Breaking category.
22+
23+
-->

ouroboros-consensus-diffusion/ouroboros-consensus-diffusion.cabal

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ library
9999
serialise ^>=0.2,
100100
si-timers ^>=1.5,
101101
strict-stm ^>=1.5,
102-
strict-mvar ^>=1.5,
103102
text,
104103
time,
105104
transformers,

ouroboros-consensus-diffusion/src/ouroboros-consensus-diffusion/Ouroboros/Consensus/Network/NodeToNode.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,7 @@ mkApps kernel Tracers {..} mkCodecs ByteLimits {..} genChainSyncTimeout lopBucke
748748
(getSharedTxStateVar kernel)
749749
(mapTxSubmissionMempoolReader txForgetValidated
750750
$ getMempoolReader (getMempool kernel))
751+
txWireSize
751752
them $ \api ->
752753
runServer (newTxSubmissionServer api)
753754

ouroboros-consensus-diffusion/src/ouroboros-consensus-diffusion/Ouroboros/Consensus/NodeKernel.hs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ module Ouroboros.Consensus.NodeKernel (
2727
) where
2828

2929

30-
import qualified Control.Concurrent.Class.MonadMVar.Strict as StrictSTM
3130
import qualified Control.Concurrent.Class.MonadSTM as LazySTM
3231
import qualified Control.Concurrent.Class.MonadSTM.Strict as StrictSTM
3332
import Control.DeepSeq (force)
@@ -43,7 +42,6 @@ import Data.Functor ((<&>))
4342
import Data.Hashable (Hashable)
4443
import Data.List.NonEmpty (NonEmpty)
4544
import Data.Map.Strict (Map)
46-
import qualified Data.Map.Strict as Map
4745
import Data.Maybe (isJust, mapMaybe)
4846
import Data.Proxy
4947
import qualified Data.Text as Text
@@ -114,8 +112,8 @@ import Ouroboros.Network.TxSubmission.Inbound
114112
(TxSubmissionMempoolWriter)
115113
import qualified Ouroboros.Network.TxSubmission.Inbound as Inbound
116114
import Ouroboros.Network.TxSubmission.Inbound.Registry
117-
(SharedTxStateVar, TxChannels (..), TxChannelsVar,
118-
decisionLogicThread, newSharedTxStateVar)
115+
(SharedTxStateVar, TxChannelsVar,
116+
decisionLogicThread, newSharedTxStateVar, newTxChannelsVar)
119117
import Ouroboros.Network.TxSubmission.Mempool.Reader
120118
(TxSubmissionMempoolReader)
121119
import qualified Ouroboros.Network.TxSubmission.Mempool.Reader as MempoolReader
@@ -294,7 +292,7 @@ initNodeKernel args@NodeKernelArgs { registry, cfg, tracers
294292
ps_POLICY_PEER_SHARE_STICKY_TIME
295293
ps_POLICY_PEER_SHARE_MAX_PEERS
296294

297-
txChannelsVar <- StrictSTM.newMVar (TxChannels Map.empty)
295+
txChannelsVar <- newTxChannelsVar
298296
sharedTxStateVar <- newSharedTxStateVar
299297

300298
case gnkaGetLoEFragment genesisArgs of

ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/HardFork/Combinator/A.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ instance LedgerSupportsMempool BlockA where
330330

331331
instance TxLimits BlockA where
332332
type TxMeasure BlockA = IgnoringOverflow ByteSize32
333+
txWireSize = const . fromIntegral $ (0 :: Int)
333334
blockCapacityTxMeasure _cfg _st = IgnoringOverflow $ ByteSize32 $ 100 * 1024 -- arbitrary
334335
txMeasure _cfg _st _tx = pure $ IgnoringOverflow $ ByteSize32 0
335336

0 commit comments

Comments
 (0)