Skip to content

Commit 2cd4b1d

Browse files
committed
consensus: remove Mempool.getTxSize
The 'LedgerSupportsMempool.txInBlockSize' method suffices, and is already in scope within the mempool itself. Adding sizes to the `MempoolSnapshot` interface replaces the other use of `getTxSize`, in the `NodeKernel`.
1 parent 483e931 commit 2cd4b1d

File tree

12 files changed

+34
-51
lines changed

12 files changed

+34
-51
lines changed

ouroboros-consensus-cardano/src/unstable-cardano-tools/Cardano/Tools/DBAnalyser/Analysis.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,6 @@ reproMempoolForge numBlks env = do
743743
-- one megabyte should generously accomodate two blocks' worth of txs
744744
(Mempool.MempoolCapacityBytesOverride $ Mempool.MempoolCapacityBytes $ 2^(20 :: Int))
745745
nullTracer
746-
LedgerSupportsMempool.txInBlockSize
747746

748747
void $ processAll db registry GetBlock startFrom limit Nothing (process howManyBlocks ref mempool)
749748
pure Nothing

ouroboros-consensus-cardano/test/cardano-test/Test/Consensus/Cardano/MiniProtocol/LocalTxSubmission/Server.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ tests =
8585
mempool <- Mocked.openMockedMempool
8686
capcityBytesOverride
8787
tracer
88-
LedgerSupportsMempool.txInBlockSize
8988
mempoolParams
9089

9190
mempool `should_process` [ _137 ]

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,6 @@ initInternalState NodeKernelArgs { tracers, chainDB, registry, cfg
379379
(configLedger cfg)
380380
mempoolCapacityOverride
381381
(mempoolTracer tracers)
382-
txInBlockSize
383382

384383
fetchClientRegistry <- newFetchClientRegistry
385384

@@ -732,8 +731,8 @@ getMempoolReader mempool = MempoolReader.TxSubmissionMempoolReader
732731
snapshotHasTx } =
733732
MempoolReader.MempoolSnapshot
734733
{ mempoolTxIdsAfter = \idx ->
735-
[ (txId (txForgetValidated tx), idx', getTxSize mempool (txForgetValidated tx))
736-
| (tx, idx') <- snapshotTxsAfter idx
734+
[ (txId (txForgetValidated tx), idx', sz)
735+
| (tx, idx', sz) <- snapshotTxsAfter idx
737736
]
738737
, mempoolLookupTx = snapshotLookupTx
739738
, mempoolHasTx = snapshotHasTx

ouroboros-consensus/bench/mempool-bench/Main.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ openMempoolWithCapacity capacity =
139139
(Mempool.unByteSize capacity)
140140
)
141141
Tracer.nullTracer
142-
TestBlock.txSize
143142
Mocked.MempoolAndModelParams {
144143
Mocked.immpInitialState = TestBlock.initialLedgerState
145144
, Mocked.immpLedgerConfig = TestBlock.sampleLedgerConfig

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Mempool/API.hs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,6 @@ data Mempool m blk = Mempool {
207207
-- capacity, i.e., we won't admit new transactions until some have been
208208
-- removed because they have become invalid.
209209
, getCapacity :: STM m Cap.MempoolCapacityBytes
210-
211-
-- | Return the post-serialisation size in bytes of a 'GenTx'.
212-
, getTxSize :: GenTx blk -> TxSizeInBytes
213210
}
214211

215212
{-------------------------------------------------------------------------------
@@ -335,7 +332,8 @@ data MempoolSnapshot blk = MempoolSnapshot {
335332
-- | Get all transactions (oldest to newest) in the mempool snapshot,
336333
-- along with their ticket number, which are associated with a ticket
337334
-- number greater than the one provided.
338-
, snapshotTxsAfter :: TicketNo -> [(Validated (GenTx blk), TicketNo)]
335+
, snapshotTxsAfter ::
336+
TicketNo -> [(Validated (GenTx blk), TicketNo, TxSizeInBytes)]
339337

340338
-- | Get a specific transaction from the mempool snapshot by its ticket
341339
-- number, if it exists.

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Mempool/Impl/Common.hs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ data MempoolEnv m blk = MempoolEnv {
187187
, mpEnvAddTxsRemoteFifo :: MVar m ()
188188
, mpEnvAddTxsAllFifo :: MVar m ()
189189
, mpEnvTracer :: Tracer m (TraceEventMempool blk)
190-
, mpEnvTxSize :: GenTx blk -> TxSizeInBytes
191190
, mpEnvCapacityOverride :: MempoolCapacityBytesOverride
192191
}
193192

@@ -200,9 +199,8 @@ initMempoolEnv :: ( IOLike m
200199
-> LedgerConfig blk
201200
-> MempoolCapacityBytesOverride
202201
-> Tracer m (TraceEventMempool blk)
203-
-> (GenTx blk -> TxSizeInBytes)
204202
-> m (MempoolEnv m blk)
205-
initMempoolEnv ledgerInterface cfg capacityOverride tracer txSize = do
203+
initMempoolEnv ledgerInterface cfg capacityOverride tracer = do
206204
st <- atomically $ getCurrentLedgerState ledgerInterface
207205
let (slot, st') = tickLedgerState cfg (ForgeInUnknownSlot st)
208206
isVar <- newTVarIO $ initInternalState capacityOverride TxSeq.zeroTicketNo slot st'
@@ -215,7 +213,6 @@ initMempoolEnv ledgerInterface cfg capacityOverride tracer txSize = do
215213
, mpEnvAddTxsRemoteFifo = addTxRemoteFifo
216214
, mpEnvAddTxsAllFifo = addTxAllFifo
217215
, mpEnvTracer = tracer
218-
, mpEnvTxSize = txSize
219216
, mpEnvCapacityOverride = capacityOverride
220217
}
221218

@@ -323,14 +320,13 @@ extendVRPrevApplied cfg txTicket vr =
323320
-- again.
324321
extendVRNew :: (LedgerSupportsMempool blk, HasTxId (GenTx blk))
325322
=> LedgerConfig blk
326-
-> (GenTx blk -> TxSizeInBytes)
327323
-> WhetherToIntervene
328324
-> GenTx blk
329325
-> ValidationResult (GenTx blk) blk
330326
-> ( Either (ApplyTxErr blk) (Validated (GenTx blk))
331327
, ValidationResult (GenTx blk) blk
332328
)
333-
extendVRNew cfg txSize wti tx vr = assert (isNothing vrNewValid) $
329+
extendVRNew cfg wti tx vr = assert (isNothing vrNewValid) $
334330
case runExcept (applyTx cfg wti vrSlotNo tx vrAfter) of
335331
Left err ->
336332
( Left err
@@ -339,7 +335,7 @@ extendVRNew cfg txSize wti tx vr = assert (isNothing vrNewValid) $
339335
)
340336
Right (st', vtx) ->
341337
( Right vtx
342-
, vr { vrValid = vrValid :> TxTicket vtx nextTicketNo (txSize tx)
338+
, vr { vrValid = vrValid :> TxTicket vtx nextTicketNo sz
343339
, vrValidTxIds = Set.insert (txId tx) vrValidTxIds
344340
, vrNewValid = Just vtx
345341
, vrAfter = st'
@@ -359,6 +355,8 @@ extendVRNew cfg txSize wti tx vr = assert (isNothing vrNewValid) $
359355

360356
nextTicketNo = succ vrLastTicketNo
361357

358+
sz = txInBlockSize tx
359+
362360
{-------------------------------------------------------------------------------
363361
Conversions
364362
-------------------------------------------------------------------------------}
@@ -428,11 +426,13 @@ snapshotFromIS is = MempoolSnapshot {
428426
where
429427
implSnapshotGetTxs :: InternalState blk
430428
-> [(Validated (GenTx blk), TicketNo)]
431-
implSnapshotGetTxs = flip implSnapshotGetTxsAfter TxSeq.zeroTicketNo
429+
implSnapshotGetTxs is' =
430+
map (\(a, b, _c) -> (a, b))
431+
$ implSnapshotGetTxsAfter is' TxSeq.zeroTicketNo
432432

433433
implSnapshotGetTxsAfter :: InternalState blk
434434
-> TicketNo
435-
-> [(Validated (GenTx blk), TicketNo)]
435+
-> [(Validated (GenTx blk), TicketNo, TxSizeInBytes)]
436436
implSnapshotGetTxsAfter IS{isTxs} =
437437
TxSeq.toTuples . snd . TxSeq.splitAfterTicketNo isTxs
438438

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Mempool/Init.hs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,9 @@ openMempool ::
3939
-> LedgerConfig blk
4040
-> MempoolCapacityBytesOverride
4141
-> Tracer m (TraceEventMempool blk)
42-
-> (GenTx blk -> TxSizeInBytes)
4342
-> m (Mempool m blk)
44-
openMempool registry ledger cfg capacityOverride tracer txSize = do
45-
env <- initMempoolEnv ledger cfg capacityOverride tracer txSize
43+
openMempool registry ledger cfg capacityOverride tracer = do
44+
env <- initMempoolEnv ledger cfg capacityOverride tracer
4645
forkSyncStateOnTipPointChange registry env
4746
return $ mkMempool env
4847

@@ -91,10 +90,9 @@ openMempoolWithoutSyncThread ::
9190
-> LedgerConfig blk
9291
-> MempoolCapacityBytesOverride
9392
-> Tracer m (TraceEventMempool blk)
94-
-> (GenTx blk -> TxSizeInBytes)
9593
-> m (Mempool m blk)
96-
openMempoolWithoutSyncThread ledger cfg capacityOverride tracer txSize =
97-
mkMempool <$> initMempoolEnv ledger cfg capacityOverride tracer txSize
94+
openMempoolWithoutSyncThread ledger cfg capacityOverride tracer =
95+
mkMempool <$> initMempoolEnv ledger cfg capacityOverride tracer
9896

9997
mkMempool ::
10098
( IOLike m
@@ -104,19 +102,17 @@ mkMempool ::
104102
)
105103
=> MempoolEnv m blk -> Mempool m blk
106104
mkMempool mpEnv = Mempool
107-
{ addTx = implAddTx istate remoteFifo allFifo cfg txSize trcr
105+
{ addTx = implAddTx istate remoteFifo allFifo cfg trcr
108106
, removeTxs = implRemoveTxs mpEnv
109107
, syncWithLedger = implSyncWithLedger mpEnv
110108
, getSnapshot = snapshotFromIS <$> readTVar istate
111109
, getSnapshotFor = \fls -> pureGetSnapshotFor cfg fls co <$> readTVar istate
112110
, getCapacity = isCapacity <$> readTVar istate
113-
, getTxSize = txSize
114111
}
115112
where MempoolEnv { mpEnvStateVar = istate
116113
, mpEnvAddTxsRemoteFifo = remoteFifo
117114
, mpEnvAddTxsAllFifo = allFifo
118115
, mpEnvLedgerCfg = cfg
119-
, mpEnvTxSize = txSize
120116
, mpEnvTracer = trcr
121117
, mpEnvCapacityOverride = co
122118
} = mpEnv

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Mempool/TxSeq.hs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,13 @@ toList (TxSeq ftree) = Foldable.toList ftree
231231

232232
-- | Convert a 'TxSeq' to a list of pairs of transactions and their
233233
-- associated 'TicketNo's.
234-
toTuples :: TxSeq tx -> [(tx, TicketNo)]
234+
toTuples :: TxSeq tx -> [(tx, TicketNo, TxSizeInBytes)]
235235
toTuples (TxSeq ftree) = fmap
236-
(\ticket -> (txTicketTx ticket, txTicketNo ticket))
236+
(\ticket ->
237+
( txTicketTx ticket
238+
, txTicketNo ticket
239+
, txTicketTxSizeInBytes ticket)
240+
)
237241
(Foldable.toList ftree)
238242

239243
-- | \( O(1) \). Return the 'MempoolSize' of the given 'TxSeq'.

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Mempool/Update.hs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,13 @@ implAddTx ::
4444
-- ^ The FIFO for all remote peers and local clients
4545
-> LedgerConfig blk
4646
-- ^ The configuration of the ledger.
47-
-> (GenTx blk -> TxSizeInBytes)
48-
-- ^ The function to calculate the size of a
49-
-- transaction.
5047
-> Tracer m (TraceEventMempool blk)
5148
-> AddTxOnBehalfOf
5249
-- ^ Whether we're acting on behalf of a remote peer or a local client.
5350
-> GenTx blk
5451
-- ^ The transaction to add to the mempool.
5552
-> m (MempoolAddTxResult blk)
56-
implAddTx istate remoteFifo allFifo cfg txSize trcr onbehalf tx =
53+
implAddTx istate remoteFifo allFifo cfg trcr onbehalf tx =
5754
-- To ensure fair behaviour between threads that are trying to add
5855
-- transactions, we make them all queue in a fifo. Only the one at the head
5956
-- of the queue gets to actually wait for space to get freed up in the
@@ -87,7 +84,7 @@ implAddTx istate remoteFifo allFifo cfg txSize trcr onbehalf tx =
8784
where
8885
implAddTx' = do
8986
(result, ev) <- atomically $ do
90-
outcome <- implTryAddTx istate cfg txSize
87+
outcome <- implTryAddTx istate cfg
9188
(whetherToIntervene onbehalf)
9289
tx
9390
case outcome of
@@ -144,16 +141,13 @@ implTryAddTx ::
144141
-- ^ The InternalState TVar.
145142
-> LedgerConfig blk
146143
-- ^ The configuration of the ledger.
147-
-> (GenTx blk -> TxSizeInBytes)
148-
-- ^ The function to calculate the size of a
149-
-- transaction.
150144
-> WhetherToIntervene
151145
-> GenTx blk
152146
-- ^ The transaction to add to the mempool.
153147
-> STM m (TryAddTx blk)
154-
implTryAddTx istate cfg txSize wti tx = do
148+
implTryAddTx istate cfg wti tx = do
155149
is <- readTVar istate
156-
let outcome = pureTryAddTx cfg txSize wti tx is
150+
let outcome = pureTryAddTx cfg wti tx is
157151
case outcome of
158152
TryAddTx (Just is') _ _ -> writeTVar istate is'
159153
_ -> return ()
@@ -172,15 +166,13 @@ pureTryAddTx ::
172166
)
173167
=> LedgerCfg (LedgerState blk)
174168
-- ^ The ledger configuration.
175-
-> (GenTx blk -> TxSizeInBytes)
176-
-- ^ The function to claculate the size of a transaction.
177169
-> WhetherToIntervene
178170
-> GenTx blk
179171
-- ^ The transaction to add to the mempool.
180172
-> InternalState blk
181173
-- ^ The current internal state of the mempool.
182174
-> TryAddTx blk
183-
pureTryAddTx cfg txSize wti tx is
175+
pureTryAddTx cfg wti tx is
184176
-- We add the transaction if there is at least one byte free left in the
185177
-- mempool.
186178
| let curSize = msNumBytes $ isMempoolSize is
@@ -213,7 +205,7 @@ pureTryAddTx cfg txSize wti tx is
213205
| otherwise
214206
= NoSpaceLeft
215207
where
216-
(eVtx, vr) = extendVRNew cfg txSize wti tx $ validationResultFromIS is
208+
(eVtx, vr) = extendVRNew cfg wti tx $ validationResultFromIS is
217209
is' = internalStateFromVR vr
218210

219211
{-------------------------------------------------------------------------------

ouroboros-consensus/src/unstable-mempool-test-utils/Test/Consensus/Mempool/Mocked.hs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,9 @@ openMockedMempool ::
5858
)
5959
=> Mempool.MempoolCapacityBytesOverride
6060
-> Tracer IO (Mempool.TraceEventMempool blk)
61-
-> (Ledger.GenTx blk -> Mempool.TxSizeInBytes)
6261
-> InitialMempoolAndModelParams blk
6362
-> IO (MockedMempool IO blk)
64-
openMockedMempool capacityOverride tracer txSizeImpl initialParams = do
63+
openMockedMempool capacityOverride tracer initialParams = do
6564
currentLedgerStateTVar <- newTVarIO (immpInitialState initialParams)
6665
let ledgerItf = Mempool.LedgerInterface {
6766
Mempool.getCurrentLedgerState = readTVar currentLedgerStateTVar
@@ -71,7 +70,6 @@ openMockedMempool capacityOverride tracer txSizeImpl initialParams = do
7170
(immpLedgerConfig initialParams)
7271
capacityOverride
7372
tracer
74-
txSizeImpl
7573
pure MockedMempool {
7674
getLedgerInterface = ledgerItf
7775
, getLedgerStateTVar = currentLedgerStateTVar

0 commit comments

Comments
 (0)