Skip to content

Commit 374a78a

Browse files
Expose transaction size to tx submission (#4926)
* Added callback providing tx size to txSubmissionInbound * changelog update
1 parent d900a38 commit 374a78a

File tree

7 files changed

+31
-11
lines changed

7 files changed

+31
-11
lines changed

ouroboros-network/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
* Added `daMinBigLedgerPeersForTrustedState` to `ArgumentsExtra` when starting diffusion.
2424
It is used by `outboundConnectionsState` when signaling trust state when syncing in
2525
Genesis mode. Default value is provided by the Configuration module.
26+
* `txSubmissionInbound` takes an additional callback which exposes
27+
CBOR-encoded transaction size as it is when transmitted over the
28+
network, except for some top level wrapping (cf. PR#4926 description)
2629

2730
### Non-Breaking changes
2831

ouroboros-network/sim-tests-lib/Test/Ouroboros/Network/TxSubmission/AppV1.hs

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ txSubmissionSimulation tracer maxUnacked outboundTxs
138138
maxUnacked
139139
(getMempoolReader inboundMempool)
140140
(getMempoolWriter inboundMempool)
141+
getTxSize
141142
NodeToNodeV_7
142143

143144
prop_txSubmission :: Positive Word16

ouroboros-network/src/Ouroboros/Network/TxSubmission/Inbound.hs

+11-3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import Network.TypedProtocol.Pipelined (N, Nat (..), natToInt)
4444
import Ouroboros.Network.NodeToNode.Version (NodeToNodeVersion)
4545
import Ouroboros.Network.Protocol.Limits
4646
import Ouroboros.Network.Protocol.TxSubmission2.Server
47+
import Ouroboros.Network.SizeInBytes
4748
import Ouroboros.Network.Protocol.TxSubmission2.Type
4849
import Ouroboros.Network.TxSubmission.Inbound.Types (ProcessedTxCount (..),
4950
TraceTxSubmissionInbound (..), TxSubmissionMempoolWriter (..),
@@ -135,9 +136,10 @@ txSubmissionInbound
135136
-> NumTxIdsToAck -- ^ Maximum number of unacknowledged txids allowed
136137
-> TxSubmissionMempoolReader txid tx idx m
137138
-> TxSubmissionMempoolWriter txid tx idx m
139+
-> (tx -> SizeInBytes) -- ^ get size of CBOR encoded transaction
138140
-> NodeToNodeVersion
139141
-> TxSubmissionServerPipelined txid tx m ()
140-
txSubmissionInbound tracer (NumTxIdsToAck maxUnacked) mpReader mpWriter _version =
142+
txSubmissionInbound tracer (NumTxIdsToAck maxUnacked) mpReader mpWriter txSize _version =
141143
TxSubmissionServerPipelined $ do
142144
#ifdef TXSUBMISSION_DELAY
143145
-- make the client linger before asking for tx's and expending
@@ -262,8 +264,14 @@ txSubmissionInbound tracer (NumTxIdsToAck maxUnacked) mpReader mpWriter _version
262264
-- for. We should never get a tx we did not ask for. We take a strict
263265
-- approach to this and check it.
264266
--
265-
let txsMap :: Map txid tx
266-
txsMap = Map.fromList [ (txId tx, tx) | tx <- txs ]
267+
let availableTxidsMap = availableTxids st
268+
txsMap :: Map txid tx
269+
txsMap = Map.fromList [ (txId', assert sizesMatch tx)
270+
| tx <- txs
271+
, let txId' = txId tx
272+
calcSize = Just $ txSize tx
273+
advertisedSize = availableTxidsMap Map.!? txId'
274+
sizesMatch = calcSize == advertisedSize]
267275

268276
txidsReceived = Map.keysSet txsMap
269277
txidsRequested = Set.fromList txids

ouroboros-network/src/Ouroboros/Network/TxSubmission/Inbound/Registry.hs

+3-2
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,15 @@ withPeer
9999
-> TxSubmissionMempoolReader txid tx idx m
100100
-> peeraddr
101101
-- ^ new peer
102+
-> (tx -> SizeInBytes)
102103
-> (PeerTxAPI m txid tx -> m a)
103104
-- ^ callback which gives access to `PeerTxStateAPI`
104105
-> m a
105106
withPeer tracer
106107
channelsVar
107108
sharedStateVar
108109
TxSubmissionMempoolReader { mempoolGetSnapshot }
109-
peeraddr io =
110+
peeraddr txSize io =
110111
bracket
111112
(do -- create a communication channel
112113
!peerTxAPI <-
@@ -211,7 +212,7 @@ withPeer tracer
211212
-- ^ received txs
212213
-> m ()
213214
handleReceivedTxs txids txs =
214-
collectTxs tracer sharedStateVar peeraddr txids txs
215+
collectTxs tracer sharedStateVar peeraddr txids txs txSize
215216

216217

217218
decisionLogicThread

ouroboros-network/src/Ouroboros/Network/TxSubmission/Inbound/Server.hs

-2
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,6 @@ txSubmissionInboundV2
182182

183183
unless (Map.keysSet received `Set.isSubsetOf` requested) $
184184
throwIO ProtocolErrorTxNotRequested
185-
-- TODO: all sizes of txs which were announced earlier with
186-
-- `MsgReplyTxIds` must be verified.
187185

188186
handleReceivedTxs requested received
189187
k

ouroboros-network/src/Ouroboros/Network/TxSubmission/Inbound/State.hs

+12-4
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,12 @@ collectTxsImpl
306306
=> peeraddr
307307
-> Set txid -- ^ set of requested txids
308308
-> Map txid tx -- ^ received txs
309+
-> (tx -> SizeInBytes)
309310
-> SharedTxState peeraddr txid tx
310311
-> SharedTxState peeraddr txid tx
311312
-- ^ number of `txid`s to be acknowledged, `tx`s to be added to
312313
-- the mempool and updated state.
313-
collectTxsImpl peeraddr requestedTxIds receivedTxs
314+
collectTxsImpl peeraddr requestedTxIds receivedTxs txSize
314315
st@SharedTxState { peerTxStates } =
315316

316317
-- using `alterF` so the update of `PeerTxState` is done in one lookup
@@ -327,8 +328,14 @@ collectTxsImpl peeraddr requestedTxIds receivedTxs
327328
-> ( SharedTxState peeraddr txid tx
328329
, PeerTxState txid tx
329330
)
330-
fn ps = (st'', ps'')
331+
fn ps = assert allSizesMatch (st'', ps'')
331332
where
333+
allSizesMatch = Map.foldlWithKey'
334+
step True
335+
(availableTxIds ps `Map.restrictKeys` Map.keysSet receivedTxs)
336+
step acc txid advSize =
337+
acc && advSize == txSize (receivedTxs Map.! txid)
338+
332339
notReceived = requestedTxIds Set.\\ Map.keysSet receivedTxs
333340

334341
-- add received `tx`s to buffered map
@@ -452,11 +459,12 @@ collectTxs
452459
-> peeraddr
453460
-> Set txid -- ^ set of requested txids
454461
-> Map txid tx -- ^ received txs
462+
-> (tx -> SizeInBytes)
455463
-> m ()
456464
-- ^ number of txids to be acknowledged and txs to be added to the
457465
-- mempool
458-
collectTxs tracer sharedVar peeraddr txidsRequested txsMap = do
466+
collectTxs tracer sharedVar peeraddr txidsRequested txsMap txSize = do
459467
st <- atomically $
460468
stateTVar sharedVar
461-
((\a -> (a,a)) . collectTxsImpl peeraddr txidsRequested txsMap)
469+
((\a -> (a,a)) . collectTxsImpl peeraddr txidsRequested txsMap txSize)
462470
traceWith tracer (TraceSharedTxState "collectTxs" st)

ouroboros-network/src/Ouroboros/Network/TxSubmission/Outbound.hs

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import Ouroboros.Network.ControlMessage (ControlMessage, ControlMessageSTM,
2727
timeoutWithControlMessage)
2828
import Ouroboros.Network.NodeToNode.Version (NodeToNodeVersion)
2929
import Ouroboros.Network.Protocol.TxSubmission2.Client
30+
import Ouroboros.Network.SizeInBytes
3031
import Ouroboros.Network.Protocol.TxSubmission2.Type
3132
import Ouroboros.Network.TxSubmission.Mempool.Reader (MempoolSnapshot (..),
3233
TxSubmissionMempoolReader (..))

0 commit comments

Comments
 (0)