Skip to content

Commit

Permalink
Expose over-the-wire encoded transaction size (#1211)
Browse files Browse the repository at this point in the history
# 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
  • Loading branch information
crocodile-dentist authored Sep 23, 2024
2 parents 35adef5 + d2e7058 commit 97676e9
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 3 deletions.
4 changes: 2 additions & 2 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!--
A new scriv changelog fragment.
Uncomment the section that is right (remove the HTML comment wrapper).
-->

<!--
### Patch
- A bullet item for the Patch category.
-->
<!--
### Non-Breaking
- A bullet item for the Non-Breaking category.
-->
### Breaking

- Implement wireTxSize of LedgerSupportsMempool instantiations for Byron and Shelley

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -146,6 +146,8 @@ instance (ShelleyCompatible proto era, TxLimits (ShelleyBlock proto era))

reapplyTx = reapplyShelleyTx

wireTxSize (ShelleyTx _ tx) = fromIntegral $ tx ^. wireSizeTxF

txForgetValidated (ShelleyValidatedTx txid vtx) = ShelleyTx txid (SL.extractTx vtx)

mkShelleyTx :: forall era proto. ShelleyBasedEra era => Tx era -> GenTx (ShelleyBlock proto era)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!--
A new scriv changelog fragment.
Uncomment the section that is right (remove the HTML comment wrapper).
-->

<!--
### Patch
- A bullet item for the Patch category.
-->

### Non-Breaking

- Pass wireTxSize binding to txSubmission{Inbound, Outbound} which
enables transaction submission handlers to conveniently work with
sizes of byte encoded transactions for network dissemination.

<!--
### Breaking
- A bullet item for the Breaking category.
-->
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,7 @@ mkApps kernel Tracers {..} mkCodecs ByteLimits {..} genChainSyncTimeout lopBucke
(getSharedTxStateVar kernel)
(mapTxSubmissionMempoolReader txForgetValidated
$ getMempoolReader (getMempool kernel))
wireTxSize
them $ \api ->
runServer (newTxSubmissionServer api)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!--
A new scriv changelog fragment.
Uncomment the section that is right (remove the HTML comment wrapper).
-->

<!--
### Patch
- A bullet item for the Patch category.
-->
<!--
### Non-Breaking
- A bullet item for the Non-Breaking category.
-->
### Breaking

- Added txWireSize method to LedgerSupportsMempool to provide
a CBOR-encoded transaction size as it is when transmitted
over the network.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ instance CanHardFork xs => LedgerSupportsMempool (HardForkBlock xs) where
(WrapValidatedGenTx vtx)
tls

wireTxSize =
hcollapse
. hcmap proxySingle (K . wireTxSize)
. getOneEraGenTx
. getHardForkGenTx

txForgetValidated =
HardForkGenTx
. OneEraGenTx
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,8 @@ instance Bridge m a => LedgerSupportsMempool (DualBlock m a) where
tickedDualLedgerStateBridge
}

wireTxSize = wireTxSize . dualGenTxMain

txForgetValidated vtx =
DualGenTx {
dualGenTxMain = txForgetValidated vDualGenTxMain
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
--
Expand Down Expand Up @@ -111,6 +112,10 @@ class ( UpdateLedger blk
-> TickedLedgerState blk
-> Except (ApplyTxErr blk) (TickedLedgerState blk)

-- | Return the size of a serialised transaction as it is transmitted
-- across the network.
wireTxSize :: GenTx blk -> Network.SizeInBytes

-- | Discard the evidence that transaction has been previously validated
txForgetValidated :: Validated (GenTx blk) -> GenTx blk

Expand Down

0 comments on commit 97676e9

Please sign in to comment.