@@ -10,8 +10,6 @@ module Ouroboros.Consensus.Mempool.Update
1010 ) where
1111
1212import Cardano.Slotting.Slot
13- import Control.Concurrent.Class.MonadMVar (withMVar )
14- import Control.Monad (void )
1513import Control.Monad.Except (runExcept )
1614import Control.Tracer
1715import qualified Data.Foldable as Foldable
@@ -20,7 +18,6 @@ import qualified Data.List.NonEmpty as NE
2018import Data.Maybe (fromMaybe )
2119import qualified Data.Measure as Measure
2220import qualified Data.Set as Set
23- import Data.Void
2421import Ouroboros.Consensus.HeaderValidation
2522import Ouroboros.Consensus.Ledger.Abstract
2623import Ouroboros.Consensus.Ledger.SupportsMempool
@@ -29,9 +26,11 @@ import Ouroboros.Consensus.Mempool.Capacity
2926import Ouroboros.Consensus.Mempool.Impl.Common
3027import Ouroboros.Consensus.Mempool.TxSeq (TxTicket (.. ))
3128import qualified Ouroboros.Consensus.Mempool.TxSeq as TxSeq
29+ import Ouroboros.Consensus.Storage.LedgerDB.Forker hiding (trace )
3230import Ouroboros.Consensus.Util (whenJust )
3331import Ouroboros.Consensus.Util.Enclose
3432import Ouroboros.Consensus.Util.IOLike hiding (withMVar )
33+ import Ouroboros.Consensus.Util.NormalForm.StrictMVar
3534import Ouroboros.Consensus.Util.STM
3635import Ouroboros.Network.Block
3736
@@ -154,7 +153,7 @@ doAddTx mpEnv wti tx =
154153 doAddTx' Nothing
155154 where
156155 MempoolEnv
157- { mpEnvLedger = ldgrInterface
156+ { mpEnvForker = forker
158157 , mpEnvLedgerCfg = cfg
159158 , mpEnvStateVar = istate
160159 , mpEnvTracer = trcr
@@ -172,31 +171,14 @@ doAddTx mpEnv wti tx =
172171
173172 res <- withTMVarAnd istate additionalCheck $
174173 \ is () -> do
175- mTbs <- getLedgerTablesAtFor ldgrInterface (isTip is) (getTransactionKeySets tx)
176- case mTbs of
177- Just tbs -> do
178- traceWith trcr $ TraceMempoolLedgerFound (isTip is)
179- case pureTryAddTx cfg wti tx is tbs of
180- NotEnoughSpaceLeft -> do
181- pure (Retry (isMempoolSize is), is)
182- Processed outcome@ (TransactionProcessingResult is' _ _) -> do
183- pure (OK outcome, fromMaybe is is')
184- Nothing -> do
185- traceWith trcr $ TraceMempoolLedgerNotFound (isTip is)
186- -- We couldn't retrieve the values because the state is no longer on
187- -- the db. We need to resync.
188- pure (Resync , is)
189- case res of
190- Retry s' -> doAddTx' (Just s')
191- OK outcome -> pure outcome
192- Resync -> do
193- void $ implSyncWithLedger mpEnv
194- doAddTx' mbPrevSize
195-
196- data WithTMVarOutcome retry ok
197- = Retry ! retry
198- | OK ok
199- | Resync
174+ frkr <- readMVar forker
175+ tbs <- castLedgerTables <$> roforkerReadTables frkr (castLedgerTables $ getTransactionKeySets tx)
176+ case pureTryAddTx cfg wti tx is tbs of
177+ NotEnoughSpaceLeft -> do
178+ pure (Left (isMempoolSize is), is)
179+ Processed outcome@ (TransactionProcessingResult is' _ _) -> do
180+ pure (Right outcome, fromMaybe is is')
181+ either (doAddTx' . Just ) pure res
200182
201183pureTryAddTx ::
202184 ( LedgerSupportsMempool blk
@@ -324,9 +306,9 @@ implRemoveTxsEvenIfValid ::
324306 MempoolEnv m blk ->
325307 NE. NonEmpty (GenTxId blk ) ->
326308 m ()
327- implRemoveTxsEvenIfValid mpEnv toRemove = do
328- (out :: WithTMVarOutcome Void () ) <- withTMVarAnd istate ( const $ getCurrentLedgerState ldgrInterface) $
329- \ is ls -> do
309+ implRemoveTxsEvenIfValid mpEnv toRemove =
310+ withTMVar istate $
311+ \ is -> do
330312 let toKeep =
331313 filter
332314 ( (`notElem` Set. fromList (NE. toList toRemove))
@@ -335,33 +317,25 @@ implRemoveTxsEvenIfValid mpEnv toRemove = do
335317 . txTicketTx
336318 )
337319 (TxSeq. toList $ isTxs is)
338- (slot, ticked) = tickLedgerState cfg (ForgeInUnknownSlot ls)
339320 toKeep' = Foldable. foldMap' (getTransactionKeySets . txForgetValidated . TxSeq. txTicketTx) toKeep
340- mTbs <- getLedgerTablesAtFor ldgrInterface (castPoint (getTip ls)) toKeep'
341- case mTbs of
342- Nothing -> pure (Resync , is)
343- Just tbs -> do
344- let (is', t) =
345- pureRemoveTxs
346- capacityOverride
347- cfg
348- slot
349- ticked
350- tbs
351- (isLastTicketNo is)
352- toKeep
353- toRemove
354- traceWith trcr t
355- pure (OK () , is')
356- case out of
357- Resync -> do
358- void $ implSyncWithLedger mpEnv
359- implRemoveTxsEvenIfValid mpEnv toRemove
360- OK () -> pure ()
321+ frkr <- readMVar forker
322+ tbs <- castLedgerTables <$> roforkerReadTables frkr (castLedgerTables toKeep')
323+ let (is', t) =
324+ pureRemoveTxs
325+ capacityOverride
326+ cfg
327+ (isSlotNo is)
328+ (isLedgerState is)
329+ tbs
330+ (isLastTicketNo is)
331+ toKeep
332+ toRemove
333+ traceWith trcr t
334+ pure (() , is')
361335 where
362336 MempoolEnv
363337 { mpEnvStateVar = istate
364- , mpEnvLedger = ldgrInterface
338+ , mpEnvForker = forker
365339 , mpEnvTracer = trcr
366340 , mpEnvLedgerCfg = cfg
367341 , mpEnvCapacityOverride = capacityOverride
@@ -415,21 +389,28 @@ implSyncWithLedger ::
415389 MempoolEnv m blk ->
416390 m (MempoolSnapshot blk )
417391implSyncWithLedger mpEnv = encloseTimedWith (TraceMempoolSynced >$< mpEnvTracer mpEnv) $ do
418- ( res :: WithTMVarOutcome Void ( MempoolSnapshot blk )) <-
392+ res <-
419393 withTMVarAnd istate (const $ getCurrentLedgerState ldgrInterface) $
420- \ is ls -> do
421- let (slot, ls') = tickLedgerState cfg $ ForgeInUnknownSlot ls
422- if pointHash (isTip is) == castHash (getTipHash ls) && isSlotNo is == slot
423- then do
424- -- The tip didn't change, put the same state.
425- traceWith trcr $ TraceMempoolSyncNotNeeded (isTip is)
426- pure (OK (snapshotFromIS is), is)
427- else do
428- -- We need to revalidate
429- let pt = castPoint (getTip ls)
430- mTbs <- getLedgerTablesAtFor ldgrInterface pt (isTxKeys is)
431- case mTbs of
432- Just tbs -> do
394+ \ is (ls, meFrk) -> do
395+ eFrk <- meFrk
396+ case eFrk of
397+ Left {} -> pure (Left () , is)
398+ Right frk -> do
399+ let (slot, ls') = tickLedgerState cfg $ ForgeInUnknownSlot ls
400+ if pointHash (isTip is) == castHash (getTipHash ls) && isSlotNo is == slot
401+ then do
402+ -- The tip didn't change, put the same state.
403+ traceWith trcr $ TraceMempoolSyncNotNeeded (isTip is)
404+ pure (Right (snapshotFromIS is), is)
405+ else do
406+ -- We need to revalidate
407+ modifyMVar_
408+ forkerMVar
409+ ( \ oldFrk -> do
410+ roforkerClose oldFrk
411+ pure frk
412+ )
413+ tbs <- castLedgerTables <$> roforkerReadTables frk (castLedgerTables $ isTxKeys is)
433414 let (is', mTrace) =
434415 pureSyncWithLedger
435416 capacityOverride
@@ -439,16 +420,12 @@ implSyncWithLedger mpEnv = encloseTimedWith (TraceMempoolSynced >$< mpEnvTracer
439420 tbs
440421 is
441422 whenJust mTrace (traceWith trcr)
442- pure (OK (snapshotFromIS is'), is')
443- Nothing -> do
444- -- If the point is gone, resync
445- pure (Resync , is)
446- case res of
447- OK v -> pure v
448- Resync -> implSyncWithLedger mpEnv
423+ pure (Right (snapshotFromIS is'), is')
424+ either (const $ implSyncWithLedger mpEnv) pure res
449425 where
450426 MempoolEnv
451427 { mpEnvStateVar = istate
428+ , mpEnvForker = forkerMVar
452429 , mpEnvLedger = ldgrInterface
453430 , mpEnvTracer = trcr
454431 , mpEnvLedgerCfg = cfg
0 commit comments