Skip to content

Commit

Permalink
Refactor getMempoolSize
Browse files Browse the repository at this point in the history
  • Loading branch information
intricate committed Dec 12, 2019
1 parent c5ca96f commit fce5cce
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
7 changes: 7 additions & 0 deletions ouroboros-consensus/src/Ouroboros/Consensus/Mempool/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,13 @@ data MempoolSize = MempoolSize
-- ^ The summed byte size of all the transactions in the mempool.
} deriving (Eq, Show)

instance Semigroup MempoolSize where
MempoolSize xt xb <> MempoolSize yt yb = MempoolSize (xt + yt) (xb + yb)

instance Monoid MempoolSize where
mempty = MempoolSize { msNumTxs = 0, msNumBytes = 0 }
mappend = (<>)

-- | Events traced by the Mempool.
data TraceEventMempool blk
= TraceMempoolAddTxs
Expand Down
21 changes: 10 additions & 11 deletions ouroboros-consensus/src/Ouroboros/Consensus/Mempool/Impl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -465,17 +465,16 @@ implGetSnapshot MempoolEnv{mpEnvStateVar} = do
getMempoolSize :: (IOLike m, ApplyTx blk)
=> MempoolEnv m blk
-> STM m MempoolSize
getMempoolSize MempoolEnv{mpEnvStateVar} = do
IS { isTxs } <- readTVar mpEnvStateVar
pure $ Foldable.foldl'
(\MempoolSize { msNumTxs, msNumBytes } tx ->
MempoolSize
{ msNumTxs = msNumTxs + 1
, msNumBytes = msNumBytes + txSize tx
}
)
MempoolSize { msNumTxs = 0, msNumBytes = 0 }
isTxs
getMempoolSize MempoolEnv{mpEnvStateVar} =
txsToMempoolSize . isTxs <$> readTVar mpEnvStateVar

-- | Given a 'Foldable' of transactions, calculate what the 'MempoolSize'
-- would be if a mempool were to consist /only/ of those transactions.
txsToMempoolSize :: (Foldable t, ApplyTx blk) => t (GenTx blk) -> MempoolSize
txsToMempoolSize = foldMap toMempoolSize
where
toMempoolSize :: ApplyTx blk => GenTx blk -> MempoolSize
toMempoolSize tx = MempoolSize { msNumTxs = 1, msNumBytes = txSize tx }

{-------------------------------------------------------------------------------
MempoolSnapshot Implementation
Expand Down

0 comments on commit fce5cce

Please sign in to comment.