{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Ouroboros.Consensus.Mempool.Impl.Pure (
implTryAddTxs
, pureGetSnapshotFor
, pureRemoveTxs
, pureSyncWithLedger
, runRemoveTxs
, runSyncWithLedger
, implSnapshotFromIS
) where
import Control.Exception (assert)
import Data.Maybe (isJust, isNothing)
import qualified Data.Set as Set
import Control.Monad (join)
import Control.Tracer
import Ouroboros.Consensus.HeaderValidation
import Ouroboros.Consensus.Ledger.Abstract
import Ouroboros.Consensus.Ledger.SupportsMempool
import Ouroboros.Consensus.Mempool.API
import Ouroboros.Consensus.Mempool.Impl.Types
import Ouroboros.Consensus.Mempool.TxSeq (TicketNo, TxTicket (..),
zeroTicketNo)
import qualified Ouroboros.Consensus.Mempool.TxSeq as TxSeq
import Ouroboros.Consensus.Util (whenJust)
import Ouroboros.Consensus.Util.IOLike
data TryAddTxs blk =
NoSpaceLeft
| TryAddTxs
(Maybe (InternalState blk))
(MempoolAddTxResult blk)
(TraceEventMempool blk)
implTryAddTxs
:: forall m blk.
( MonadSTM m
, LedgerSupportsMempool blk
, HasTxId (GenTx blk)
)
=> StrictTVar m (InternalState blk)
-> LedgerConfig blk
-> (GenTx blk -> TxSizeInBytes)
-> Tracer m (TraceEventMempool blk)
-> WhetherToIntervene
-> [GenTx blk]
-> m ([MempoolAddTxResult blk], [GenTx blk])
implTryAddTxs :: StrictTVar m (InternalState blk)
-> LedgerConfig blk
-> (GenTx blk -> TxSizeInBytes)
-> Tracer m (TraceEventMempool blk)
-> WhetherToIntervene
-> [GenTx blk]
-> m ([MempoolAddTxResult blk], [GenTx blk])
implTryAddTxs StrictTVar m (InternalState blk)
istate LedgerConfig blk
cfg GenTx blk -> TxSizeInBytes
txSize Tracer m (TraceEventMempool blk)
trcr WhetherToIntervene
wti =
[MempoolAddTxResult blk]
-> [GenTx blk] -> m ([MempoolAddTxResult blk], [GenTx blk])
go []
where
go :: [MempoolAddTxResult blk]
-> [GenTx blk] -> m ([MempoolAddTxResult blk], [GenTx blk])
go [MempoolAddTxResult blk]
acc = \case
[] -> ([MempoolAddTxResult blk], [GenTx blk])
-> m ([MempoolAddTxResult blk], [GenTx blk])
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([MempoolAddTxResult blk] -> [MempoolAddTxResult blk]
forall a. [a] -> [a]
reverse [MempoolAddTxResult blk]
acc, [])
GenTx blk
tx:[GenTx blk]
txs -> m (m ([MempoolAddTxResult blk], [GenTx blk]))
-> m ([MempoolAddTxResult blk], [GenTx blk])
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (m (m ([MempoolAddTxResult blk], [GenTx blk]))
-> m ([MempoolAddTxResult blk], [GenTx blk]))
-> m (m ([MempoolAddTxResult blk], [GenTx blk]))
-> m ([MempoolAddTxResult blk], [GenTx blk])
forall a b. (a -> b) -> a -> b
$ STM m (m ([MempoolAddTxResult blk], [GenTx blk]))
-> m (m ([MempoolAddTxResult blk], [GenTx blk]))
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
STM m a -> m a
atomically (STM m (m ([MempoolAddTxResult blk], [GenTx blk]))
-> m (m ([MempoolAddTxResult blk], [GenTx blk])))
-> STM m (m ([MempoolAddTxResult blk], [GenTx blk]))
-> m (m ([MempoolAddTxResult blk], [GenTx blk]))
forall a b. (a -> b) -> a -> b
$ do
InternalState blk
is <- StrictTVar m (InternalState blk) -> STM m (InternalState blk)
forall (m :: * -> *) a. MonadSTM m => StrictTVar m a -> STM m a
readTVar StrictTVar m (InternalState blk)
istate
case LedgerConfig blk
-> (GenTx blk -> TxSizeInBytes)
-> WhetherToIntervene
-> GenTx blk
-> InternalState blk
-> TryAddTxs blk
forall blk.
(LedgerSupportsMempool blk, HasTxId (GenTx blk)) =>
LedgerCfg (LedgerState blk)
-> (GenTx blk -> TxSizeInBytes)
-> WhetherToIntervene
-> GenTx blk
-> InternalState blk
-> TryAddTxs blk
pureTryAddTxs LedgerConfig blk
cfg GenTx blk -> TxSizeInBytes
txSize WhetherToIntervene
wti GenTx blk
tx InternalState blk
is of
TryAddTxs blk
NoSpaceLeft -> m ([MempoolAddTxResult blk], [GenTx blk])
-> STM m (m ([MempoolAddTxResult blk], [GenTx blk]))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (m ([MempoolAddTxResult blk], [GenTx blk])
-> STM m (m ([MempoolAddTxResult blk], [GenTx blk])))
-> m ([MempoolAddTxResult blk], [GenTx blk])
-> STM m (m ([MempoolAddTxResult blk], [GenTx blk]))
forall a b. (a -> b) -> a -> b
$ ([MempoolAddTxResult blk], [GenTx blk])
-> m ([MempoolAddTxResult blk], [GenTx blk])
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([MempoolAddTxResult blk] -> [MempoolAddTxResult blk]
forall a. [a] -> [a]
reverse [MempoolAddTxResult blk]
acc, GenTx blk
txGenTx blk -> [GenTx blk] -> [GenTx blk]
forall a. a -> [a] -> [a]
:[GenTx blk]
txs)
TryAddTxs Maybe (InternalState blk)
is' MempoolAddTxResult blk
result TraceEventMempool blk
ev -> do
Maybe (InternalState blk)
-> (InternalState blk -> STM m ()) -> STM m ()
forall (f :: * -> *) a.
Applicative f =>
Maybe a -> (a -> f ()) -> f ()
whenJust Maybe (InternalState blk)
is' (StrictTVar m (InternalState blk) -> InternalState blk -> STM m ()
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
StrictTVar m a -> a -> STM m ()
writeTVar StrictTVar m (InternalState blk)
istate)
m ([MempoolAddTxResult blk], [GenTx blk])
-> STM m (m ([MempoolAddTxResult blk], [GenTx blk]))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (m ([MempoolAddTxResult blk], [GenTx blk])
-> STM m (m ([MempoolAddTxResult blk], [GenTx blk])))
-> m ([MempoolAddTxResult blk], [GenTx blk])
-> STM m (m ([MempoolAddTxResult blk], [GenTx blk]))
forall a b. (a -> b) -> a -> b
$ do
Tracer m (TraceEventMempool blk) -> TraceEventMempool blk -> m ()
forall (m :: * -> *) a. Tracer m a -> a -> m ()
traceWith Tracer m (TraceEventMempool blk)
trcr TraceEventMempool blk
ev
[MempoolAddTxResult blk]
-> [GenTx blk] -> m ([MempoolAddTxResult blk], [GenTx blk])
go (MempoolAddTxResult blk
resultMempoolAddTxResult blk
-> [MempoolAddTxResult blk] -> [MempoolAddTxResult blk]
forall a. a -> [a] -> [a]
:[MempoolAddTxResult blk]
acc) [GenTx blk]
txs
pureTryAddTxs
:: ( LedgerSupportsMempool blk
, HasTxId (GenTx blk)
)
=> LedgerCfg (LedgerState blk)
-> (GenTx blk -> TxSizeInBytes)
-> WhetherToIntervene
-> GenTx blk
-> InternalState blk
-> TryAddTxs blk
pureTryAddTxs :: LedgerCfg (LedgerState blk)
-> (GenTx blk -> TxSizeInBytes)
-> WhetherToIntervene
-> GenTx blk
-> InternalState blk
-> TryAddTxs blk
pureTryAddTxs LedgerCfg (LedgerState blk)
cfg GenTx blk -> TxSizeInBytes
txSize WhetherToIntervene
wti GenTx blk
tx InternalState blk
is
| let size :: TxSizeInBytes
size = GenTx blk -> TxSizeInBytes
txSize GenTx blk
tx
curSize :: TxSizeInBytes
curSize = MempoolSize -> TxSizeInBytes
msNumBytes (MempoolSize -> TxSizeInBytes) -> MempoolSize -> TxSizeInBytes
forall a b. (a -> b) -> a -> b
$ InternalState blk -> MempoolSize
forall blk. InternalState blk -> MempoolSize
isMempoolSize InternalState blk
is
, TxSizeInBytes
curSize TxSizeInBytes -> TxSizeInBytes -> TxSizeInBytes
forall a. Num a => a -> a -> a
+ TxSizeInBytes
size TxSizeInBytes -> TxSizeInBytes -> Bool
forall a. Ord a => a -> a -> Bool
> MempoolCapacityBytes -> TxSizeInBytes
getMempoolCapacityBytes (InternalState blk -> MempoolCapacityBytes
forall blk. InternalState blk -> MempoolCapacityBytes
isCapacity InternalState blk
is)
= TryAddTxs blk
forall blk. TryAddTxs blk
NoSpaceLeft
| Bool
otherwise
= case Either (ApplyTxErr blk) (Validated (GenTx blk))
eVtx of
Right Validated (GenTx blk)
vtx ->
Bool -> TryAddTxs blk -> TryAddTxs blk
forall a. HasCallStack => Bool -> a -> a
assert (Maybe (Validated (GenTx blk)) -> Bool
forall a. Maybe a -> Bool
isJust (ValidationResult (GenTx blk) blk -> Maybe (Validated (GenTx blk))
forall invalidTx blk.
ValidationResult invalidTx blk -> Maybe (Validated (GenTx blk))
vrNewValid ValidationResult (GenTx blk) blk
vr)) (TryAddTxs blk -> TryAddTxs blk) -> TryAddTxs blk -> TryAddTxs blk
forall a b. (a -> b) -> a -> b
$
Maybe (InternalState blk)
-> MempoolAddTxResult blk -> TraceEventMempool blk -> TryAddTxs blk
forall blk.
Maybe (InternalState blk)
-> MempoolAddTxResult blk -> TraceEventMempool blk -> TryAddTxs blk
TryAddTxs
(InternalState blk -> Maybe (InternalState blk)
forall a. a -> Maybe a
Just InternalState blk
is')
(Validated (GenTx blk) -> MempoolAddTxResult blk
forall blk. Validated (GenTx blk) -> MempoolAddTxResult blk
MempoolTxAdded Validated (GenTx blk)
vtx)
(Validated (GenTx blk)
-> MempoolSize -> MempoolSize -> TraceEventMempool blk
forall blk.
Validated (GenTx blk)
-> MempoolSize -> MempoolSize -> TraceEventMempool blk
TraceMempoolAddedTx
Validated (GenTx blk)
vtx
(InternalState blk -> MempoolSize
forall blk. InternalState blk -> MempoolSize
isMempoolSize InternalState blk
is)
(InternalState blk -> MempoolSize
forall blk. InternalState blk -> MempoolSize
isMempoolSize InternalState blk
is')
)
Left ApplyTxErr blk
err ->
Bool -> TryAddTxs blk -> TryAddTxs blk
forall a. HasCallStack => Bool -> a -> a
assert (Maybe (Validated (GenTx blk)) -> Bool
forall a. Maybe a -> Bool
isNothing (ValidationResult (GenTx blk) blk -> Maybe (Validated (GenTx blk))
forall invalidTx blk.
ValidationResult invalidTx blk -> Maybe (Validated (GenTx blk))
vrNewValid ValidationResult (GenTx blk) blk
vr)) (TryAddTxs blk -> TryAddTxs blk) -> TryAddTxs blk -> TryAddTxs blk
forall a b. (a -> b) -> a -> b
$
Bool -> TryAddTxs blk -> TryAddTxs blk
forall a. HasCallStack => Bool -> a -> a
assert ([(GenTx blk, ApplyTxErr blk)] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (ValidationResult (GenTx blk) blk -> [(GenTx blk, ApplyTxErr blk)]
forall invalidTx blk.
ValidationResult invalidTx blk -> [(invalidTx, ApplyTxErr blk)]
vrInvalid ValidationResult (GenTx blk) blk
vr) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1) (TryAddTxs blk -> TryAddTxs blk) -> TryAddTxs blk -> TryAddTxs blk
forall a b. (a -> b) -> a -> b
$
Maybe (InternalState blk)
-> MempoolAddTxResult blk -> TraceEventMempool blk -> TryAddTxs blk
forall blk.
Maybe (InternalState blk)
-> MempoolAddTxResult blk -> TraceEventMempool blk -> TryAddTxs blk
TryAddTxs
Maybe (InternalState blk)
forall a. Maybe a
Nothing
(GenTx blk -> ApplyTxErr blk -> MempoolAddTxResult blk
forall blk. GenTx blk -> ApplyTxErr blk -> MempoolAddTxResult blk
MempoolTxRejected GenTx blk
tx ApplyTxErr blk
err)
(GenTx blk -> ApplyTxErr blk -> MempoolSize -> TraceEventMempool blk
forall blk.
GenTx blk -> ApplyTxErr blk -> MempoolSize -> TraceEventMempool blk
TraceMempoolRejectedTx
GenTx blk
tx
ApplyTxErr blk
err
(InternalState blk -> MempoolSize
forall blk. InternalState blk -> MempoolSize
isMempoolSize InternalState blk
is)
)
where
(Either (ApplyTxErr blk) (Validated (GenTx blk))
eVtx, ValidationResult (GenTx blk) blk
vr) = LedgerCfg (LedgerState blk)
-> (GenTx blk -> TxSizeInBytes)
-> WhetherToIntervene
-> GenTx blk
-> ValidationResult (GenTx blk) blk
-> (Either (ApplyTxErr blk) (Validated (GenTx blk)),
ValidationResult (GenTx blk) blk)
forall blk.
(LedgerSupportsMempool blk, HasTxId (GenTx blk)) =>
LedgerConfig blk
-> (GenTx blk -> TxSizeInBytes)
-> WhetherToIntervene
-> GenTx blk
-> ValidationResult (GenTx blk) blk
-> (Either (ApplyTxErr blk) (Validated (GenTx blk)),
ValidationResult (GenTx blk) blk)
extendVRNew LedgerCfg (LedgerState blk)
cfg GenTx blk -> TxSizeInBytes
txSize WhetherToIntervene
wti GenTx blk
tx (ValidationResult (GenTx blk) blk
-> (Either (ApplyTxErr blk) (Validated (GenTx blk)),
ValidationResult (GenTx blk) blk))
-> ValidationResult (GenTx blk) blk
-> (Either (ApplyTxErr blk) (Validated (GenTx blk)),
ValidationResult (GenTx blk) blk)
forall a b. (a -> b) -> a -> b
$ InternalState blk -> ValidationResult (GenTx blk) blk
forall blk invalidTx.
InternalState blk -> ValidationResult invalidTx blk
validationResultFromIS InternalState blk
is
is' :: InternalState blk
is' = ValidationResult (GenTx blk) blk -> InternalState blk
forall invalidTx blk.
ValidationResult invalidTx blk -> InternalState blk
internalStateFromVR ValidationResult (GenTx blk) blk
vr
data RemoveTxs blk =
WriteRemoveTxs (InternalState blk) (Maybe (TraceEventMempool blk))
runRemoveTxs
:: forall m blk. IOLike m
=> StrictTVar m (InternalState blk)
-> RemoveTxs blk
-> STM m (Maybe (TraceEventMempool blk))
runRemoveTxs :: StrictTVar m (InternalState blk)
-> RemoveTxs blk -> STM m (Maybe (TraceEventMempool blk))
runRemoveTxs StrictTVar m (InternalState blk)
stateVar (WriteRemoveTxs InternalState blk
is Maybe (TraceEventMempool blk)
t) = do
StrictTVar m (InternalState blk) -> InternalState blk -> STM m ()
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
StrictTVar m a -> a -> STM m ()
writeTVar StrictTVar m (InternalState blk)
stateVar InternalState blk
is
Maybe (TraceEventMempool blk)
-> STM m (Maybe (TraceEventMempool blk))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (TraceEventMempool blk)
t
pureRemoveTxs
:: ( LedgerSupportsMempool blk
, HasTxId (GenTx blk)
, ValidateEnvelope blk
)
=> LedgerConfig blk
-> MempoolCapacityBytesOverride
-> [GenTxId blk]
-> InternalState blk
-> LedgerState blk
-> RemoveTxs blk
pureRemoveTxs :: LedgerConfig blk
-> MempoolCapacityBytesOverride
-> [GenTxId blk]
-> InternalState blk
-> LedgerState blk
-> RemoveTxs blk
pureRemoveTxs LedgerConfig blk
cfg MempoolCapacityBytesOverride
capacityOverride [GenTxId blk]
txIds IS { TxSeq (Validated (GenTx blk))
isTxs :: forall blk. InternalState blk -> TxSeq (Validated (GenTx blk))
isTxs :: TxSeq (Validated (GenTx blk))
isTxs, TicketNo
isLastTicketNo :: forall blk. InternalState blk -> TicketNo
isLastTicketNo :: TicketNo
isLastTicketNo } LedgerState blk
lstate =
let toRemove :: Set (GenTxId blk)
toRemove = [GenTxId blk] -> Set (GenTxId blk)
forall a. Ord a => [a] -> Set a
Set.fromList [GenTxId blk]
txIds
txTickets' :: [TxTicket (Validated (GenTx blk))]
txTickets' = (TxTicket (Validated (GenTx blk)) -> Bool)
-> [TxTicket (Validated (GenTx blk))]
-> [TxTicket (Validated (GenTx blk))]
forall a. (a -> Bool) -> [a] -> [a]
filter
( (GenTxId blk -> Set (GenTxId blk) -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` Set (GenTxId blk)
toRemove)
(GenTxId blk -> Bool)
-> (TxTicket (Validated (GenTx blk)) -> GenTxId blk)
-> TxTicket (Validated (GenTx blk))
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GenTx blk -> GenTxId blk
forall tx. HasTxId tx => tx -> TxId tx
txId
(GenTx blk -> GenTxId blk)
-> (TxTicket (Validated (GenTx blk)) -> GenTx blk)
-> TxTicket (Validated (GenTx blk))
-> GenTxId blk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Validated (GenTx blk) -> GenTx blk
forall blk.
LedgerSupportsMempool blk =>
Validated (GenTx blk) -> GenTx blk
txForgetValidated
(Validated (GenTx blk) -> GenTx blk)
-> (TxTicket (Validated (GenTx blk)) -> Validated (GenTx blk))
-> TxTicket (Validated (GenTx blk))
-> GenTx blk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxTicket (Validated (GenTx blk)) -> Validated (GenTx blk)
forall tx. TxTicket tx -> tx
txTicketTx
)
(TxSeq (Validated (GenTx blk)) -> [TxTicket (Validated (GenTx blk))]
forall tx. TxSeq tx -> [TxTicket tx]
TxSeq.toList TxSeq (Validated (GenTx blk))
isTxs)
(SlotNo
slot, TickedLedgerState blk
ticked) = LedgerConfig blk
-> ForgeLedgerState blk -> (SlotNo, TickedLedgerState blk)
forall blk.
(UpdateLedger blk, ValidateEnvelope blk) =>
LedgerConfig blk
-> ForgeLedgerState blk -> (SlotNo, TickedLedgerState blk)
tickLedgerState LedgerConfig blk
cfg (LedgerState blk -> ForgeLedgerState blk
forall blk. LedgerState blk -> ForgeLedgerState blk
ForgeInUnknownSlot LedgerState blk
lstate)
vr :: ValidationResult (Validated (GenTx blk)) blk
vr = MempoolCapacityBytesOverride
-> LedgerConfig blk
-> SlotNo
-> TickedLedgerState blk
-> TicketNo
-> [TxTicket (Validated (GenTx blk))]
-> ValidationResult (Validated (GenTx blk)) blk
forall blk.
(LedgerSupportsMempool blk, HasTxId (GenTx blk)) =>
MempoolCapacityBytesOverride
-> LedgerConfig blk
-> SlotNo
-> TickedLedgerState blk
-> TicketNo
-> [TxTicket (Validated (GenTx blk))]
-> ValidationResult (Validated (GenTx blk)) blk
revalidateTxsFor
MempoolCapacityBytesOverride
capacityOverride
LedgerConfig blk
cfg
SlotNo
slot
TickedLedgerState blk
ticked
TicketNo
isLastTicketNo
[TxTicket (Validated (GenTx blk))]
txTickets'
is' :: InternalState blk
is' = ValidationResult (Validated (GenTx blk)) blk -> InternalState blk
forall invalidTx blk.
ValidationResult invalidTx blk -> InternalState blk
internalStateFromVR ValidationResult (Validated (GenTx blk)) blk
vr
needsTrace :: Maybe (TraceEventMempool blk)
needsTrace = if [GenTxId blk] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [GenTxId blk]
txIds
then
Maybe (TraceEventMempool blk)
forall a. Maybe a
Nothing
else
TraceEventMempool blk -> Maybe (TraceEventMempool blk)
forall a. a -> Maybe a
Just (TraceEventMempool blk -> Maybe (TraceEventMempool blk))
-> TraceEventMempool blk -> Maybe (TraceEventMempool blk)
forall a b. (a -> b) -> a -> b
$ [GenTxId blk]
-> [Validated (GenTx blk)] -> MempoolSize -> TraceEventMempool blk
forall blk.
[GenTxId blk]
-> [Validated (GenTx blk)] -> MempoolSize -> TraceEventMempool blk
TraceMempoolManuallyRemovedTxs
[GenTxId blk]
txIds
(((Validated (GenTx blk), ApplyTxErr blk) -> Validated (GenTx blk))
-> [(Validated (GenTx blk), ApplyTxErr blk)]
-> [Validated (GenTx blk)]
forall a b. (a -> b) -> [a] -> [b]
map (Validated (GenTx blk), ApplyTxErr blk) -> Validated (GenTx blk)
forall a b. (a, b) -> a
fst (ValidationResult (Validated (GenTx blk)) blk
-> [(Validated (GenTx blk), ApplyTxErr blk)]
forall invalidTx blk.
ValidationResult invalidTx blk -> [(invalidTx, ApplyTxErr blk)]
vrInvalid ValidationResult (Validated (GenTx blk)) blk
vr))
(InternalState blk -> MempoolSize
forall blk. InternalState blk -> MempoolSize
isMempoolSize InternalState blk
is')
in InternalState blk -> Maybe (TraceEventMempool blk) -> RemoveTxs blk
forall blk.
InternalState blk -> Maybe (TraceEventMempool blk) -> RemoveTxs blk
WriteRemoveTxs InternalState blk
is' Maybe (TraceEventMempool blk)
needsTrace
data SyncWithLedger blk =
NewSyncedState (InternalState blk)
(MempoolSnapshot blk TicketNo)
(Maybe (TraceEventMempool blk))
runSyncWithLedger
:: forall m blk. IOLike m
=> StrictTVar m (InternalState blk)
-> SyncWithLedger blk
-> STM m (Maybe (TraceEventMempool blk), MempoolSnapshot blk TicketNo)
runSyncWithLedger :: StrictTVar m (InternalState blk)
-> SyncWithLedger blk
-> STM
m (Maybe (TraceEventMempool blk), MempoolSnapshot blk TicketNo)
runSyncWithLedger StrictTVar m (InternalState blk)
stateVar (NewSyncedState InternalState blk
is MempoolSnapshot blk TicketNo
msp Maybe (TraceEventMempool blk)
mTrace) = do
StrictTVar m (InternalState blk) -> InternalState blk -> STM m ()
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
StrictTVar m a -> a -> STM m ()
writeTVar StrictTVar m (InternalState blk)
stateVar InternalState blk
is
(Maybe (TraceEventMempool blk), MempoolSnapshot blk TicketNo)
-> STM
m (Maybe (TraceEventMempool blk), MempoolSnapshot blk TicketNo)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (TraceEventMempool blk)
mTrace, MempoolSnapshot blk TicketNo
msp)
pureSyncWithLedger
:: (LedgerSupportsMempool blk, HasTxId (GenTx blk), ValidateEnvelope blk)
=> InternalState blk
-> LedgerState blk
-> LedgerConfig blk
-> MempoolCapacityBytesOverride
-> SyncWithLedger blk
pureSyncWithLedger :: InternalState blk
-> LedgerState blk
-> LedgerConfig blk
-> MempoolCapacityBytesOverride
-> SyncWithLedger blk
pureSyncWithLedger InternalState blk
istate LedgerState blk
lstate LedgerConfig blk
lcfg MempoolCapacityBytesOverride
capacityOverride =
let vr :: ValidationResult (Validated (GenTx blk)) blk
vr = InternalState blk
-> LedgerState blk
-> LedgerConfig blk
-> MempoolCapacityBytesOverride
-> ValidationResult (Validated (GenTx blk)) blk
forall blk.
(LedgerSupportsMempool blk, HasTxId (GenTx blk),
ValidateEnvelope blk) =>
InternalState blk
-> LedgerState blk
-> LedgerConfig blk
-> MempoolCapacityBytesOverride
-> ValidationResult (Validated (GenTx blk)) blk
validateIS InternalState blk
istate LedgerState blk
lstate LedgerConfig blk
lcfg MempoolCapacityBytesOverride
capacityOverride
removed :: [Validated (GenTx blk)]
removed = ((Validated (GenTx blk), ApplyTxErr blk) -> Validated (GenTx blk))
-> [(Validated (GenTx blk), ApplyTxErr blk)]
-> [Validated (GenTx blk)]
forall a b. (a -> b) -> [a] -> [b]
map (Validated (GenTx blk), ApplyTxErr blk) -> Validated (GenTx blk)
forall a b. (a, b) -> a
fst (ValidationResult (Validated (GenTx blk)) blk
-> [(Validated (GenTx blk), ApplyTxErr blk)]
forall invalidTx blk.
ValidationResult invalidTx blk -> [(invalidTx, ApplyTxErr blk)]
vrInvalid ValidationResult (Validated (GenTx blk)) blk
vr)
istate' :: InternalState blk
istate' = ValidationResult (Validated (GenTx blk)) blk -> InternalState blk
forall invalidTx blk.
ValidationResult invalidTx blk -> InternalState blk
internalStateFromVR ValidationResult (Validated (GenTx blk)) blk
vr
mTrace :: Maybe (TraceEventMempool blk)
mTrace = if [Validated (GenTx blk)] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Validated (GenTx blk)]
removed
then
Maybe (TraceEventMempool blk)
forall a. Maybe a
Nothing
else
TraceEventMempool blk -> Maybe (TraceEventMempool blk)
forall a. a -> Maybe a
Just (TraceEventMempool blk -> Maybe (TraceEventMempool blk))
-> TraceEventMempool blk -> Maybe (TraceEventMempool blk)
forall a b. (a -> b) -> a -> b
$ [Validated (GenTx blk)] -> MempoolSize -> TraceEventMempool blk
forall blk.
[Validated (GenTx blk)] -> MempoolSize -> TraceEventMempool blk
TraceMempoolRemoveTxs [Validated (GenTx blk)]
removed (InternalState blk -> MempoolSize
forall blk. InternalState blk -> MempoolSize
isMempoolSize InternalState blk
istate')
snapshot :: MempoolSnapshot blk TicketNo
snapshot = InternalState blk -> MempoolSnapshot blk TicketNo
forall blk.
HasTxId (GenTx blk) =>
InternalState blk -> MempoolSnapshot blk TicketNo
implSnapshotFromIS InternalState blk
istate'
in
InternalState blk
-> MempoolSnapshot blk TicketNo
-> Maybe (TraceEventMempool blk)
-> SyncWithLedger blk
forall blk.
InternalState blk
-> MempoolSnapshot blk TicketNo
-> Maybe (TraceEventMempool blk)
-> SyncWithLedger blk
NewSyncedState InternalState blk
istate' MempoolSnapshot blk TicketNo
snapshot Maybe (TraceEventMempool blk)
mTrace
pureGetSnapshotFor
:: forall blk.
( LedgerSupportsMempool blk
, HasTxId (GenTx blk)
, ValidateEnvelope blk
)
=> LedgerConfig blk
-> ForgeLedgerState blk
-> MempoolCapacityBytesOverride
-> InternalState blk
-> MempoolSnapshot blk TicketNo
pureGetSnapshotFor :: LedgerConfig blk
-> ForgeLedgerState blk
-> MempoolCapacityBytesOverride
-> InternalState blk
-> MempoolSnapshot blk TicketNo
pureGetSnapshotFor LedgerConfig blk
cfg ForgeLedgerState blk
blockLedgerState MempoolCapacityBytesOverride
capacityOverride =
InternalState blk -> MempoolSnapshot blk TicketNo
forall blk.
HasTxId (GenTx blk) =>
InternalState blk -> MempoolSnapshot blk TicketNo
implSnapshotFromIS
(InternalState blk -> MempoolSnapshot blk TicketNo)
-> (InternalState blk -> InternalState blk)
-> InternalState blk
-> MempoolSnapshot blk TicketNo
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ValidationResult (Validated (GenTx blk)) blk -> InternalState blk
forall invalidTx blk.
ValidationResult invalidTx blk -> InternalState blk
internalStateFromVR
(ValidationResult (Validated (GenTx blk)) blk -> InternalState blk)
-> (InternalState blk
-> ValidationResult (Validated (GenTx blk)) blk)
-> InternalState blk
-> InternalState blk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MempoolCapacityBytesOverride
-> LedgerConfig blk
-> ForgeLedgerState blk
-> InternalState blk
-> ValidationResult (Validated (GenTx blk)) blk
forall blk.
(LedgerSupportsMempool blk, HasTxId (GenTx blk),
ValidateEnvelope blk) =>
MempoolCapacityBytesOverride
-> LedgerConfig blk
-> ForgeLedgerState blk
-> InternalState blk
-> ValidationResult (Validated (GenTx blk)) blk
validateStateFor MempoolCapacityBytesOverride
capacityOverride LedgerConfig blk
cfg ForgeLedgerState blk
blockLedgerState
implSnapshotFromIS
:: HasTxId (GenTx blk)
=> InternalState blk
-> MempoolSnapshot blk TicketNo
implSnapshotFromIS :: InternalState blk -> MempoolSnapshot blk TicketNo
implSnapshotFromIS InternalState blk
is = MempoolSnapshot :: forall blk idx.
[(Validated (GenTx blk), idx)]
-> (idx -> [(Validated (GenTx blk), idx)])
-> (idx -> Maybe (Validated (GenTx blk)))
-> (GenTxId blk -> Bool)
-> MempoolSize
-> SlotNo
-> TickedLedgerState blk
-> MempoolSnapshot blk idx
MempoolSnapshot {
snapshotTxs :: [(Validated (GenTx blk), TicketNo)]
snapshotTxs = InternalState blk -> [(Validated (GenTx blk), TicketNo)]
forall blk.
InternalState blk -> [(Validated (GenTx blk), TicketNo)]
implSnapshotGetTxs InternalState blk
is
, snapshotTxsAfter :: TicketNo -> [(Validated (GenTx blk), TicketNo)]
snapshotTxsAfter = InternalState blk
-> TicketNo -> [(Validated (GenTx blk), TicketNo)]
forall blk.
InternalState blk
-> TicketNo -> [(Validated (GenTx blk), TicketNo)]
implSnapshotGetTxsAfter InternalState blk
is
, snapshotLookupTx :: TicketNo -> Maybe (Validated (GenTx blk))
snapshotLookupTx = InternalState blk -> TicketNo -> Maybe (Validated (GenTx blk))
forall blk.
InternalState blk -> TicketNo -> Maybe (Validated (GenTx blk))
implSnapshotGetTx InternalState blk
is
, snapshotHasTx :: GenTxId blk -> Bool
snapshotHasTx = InternalState blk -> GenTxId blk -> Bool
forall blk.
Ord (GenTxId blk) =>
InternalState blk -> GenTxId blk -> Bool
implSnapshotHasTx InternalState blk
is
, snapshotMempoolSize :: MempoolSize
snapshotMempoolSize = InternalState blk -> MempoolSize
forall blk. InternalState blk -> MempoolSize
implSnapshotGetMempoolSize InternalState blk
is
, snapshotSlotNo :: SlotNo
snapshotSlotNo = InternalState blk -> SlotNo
forall blk. InternalState blk -> SlotNo
isSlotNo InternalState blk
is
, snapshotLedgerState :: TickedLedgerState blk
snapshotLedgerState = InternalState blk -> TickedLedgerState blk
forall blk. InternalState blk -> TickedLedgerState blk
isLedgerState InternalState blk
is
}
where
implSnapshotGetTxs :: InternalState blk
-> [(Validated (GenTx blk), TicketNo)]
implSnapshotGetTxs :: InternalState blk -> [(Validated (GenTx blk), TicketNo)]
implSnapshotGetTxs = (InternalState blk
-> TicketNo -> [(Validated (GenTx blk), TicketNo)])
-> TicketNo
-> InternalState blk
-> [(Validated (GenTx blk), TicketNo)]
forall a b c. (a -> b -> c) -> b -> a -> c
flip InternalState blk
-> TicketNo -> [(Validated (GenTx blk), TicketNo)]
forall blk.
InternalState blk
-> TicketNo -> [(Validated (GenTx blk), TicketNo)]
implSnapshotGetTxsAfter TicketNo
zeroTicketNo
implSnapshotGetTxsAfter :: InternalState blk
-> TicketNo
-> [(Validated (GenTx blk), TicketNo)]
implSnapshotGetTxsAfter :: InternalState blk
-> TicketNo -> [(Validated (GenTx blk), TicketNo)]
implSnapshotGetTxsAfter IS{TxSeq (Validated (GenTx blk))
isTxs :: TxSeq (Validated (GenTx blk))
isTxs :: forall blk. InternalState blk -> TxSeq (Validated (GenTx blk))
isTxs} =
TxSeq (Validated (GenTx blk))
-> [(Validated (GenTx blk), TicketNo)]
forall tx. TxSeq tx -> [(tx, TicketNo)]
TxSeq.toTuples (TxSeq (Validated (GenTx blk))
-> [(Validated (GenTx blk), TicketNo)])
-> (TicketNo -> TxSeq (Validated (GenTx blk)))
-> TicketNo
-> [(Validated (GenTx blk), TicketNo)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TxSeq (Validated (GenTx blk)), TxSeq (Validated (GenTx blk)))
-> TxSeq (Validated (GenTx blk))
forall a b. (a, b) -> b
snd ((TxSeq (Validated (GenTx blk)), TxSeq (Validated (GenTx blk)))
-> TxSeq (Validated (GenTx blk)))
-> (TicketNo
-> (TxSeq (Validated (GenTx blk)), TxSeq (Validated (GenTx blk))))
-> TicketNo
-> TxSeq (Validated (GenTx blk))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxSeq (Validated (GenTx blk))
-> TicketNo
-> (TxSeq (Validated (GenTx blk)), TxSeq (Validated (GenTx blk)))
forall tx. TxSeq tx -> TicketNo -> (TxSeq tx, TxSeq tx)
TxSeq.splitAfterTicketNo TxSeq (Validated (GenTx blk))
isTxs
implSnapshotGetTx :: InternalState blk
-> TicketNo
-> Maybe (Validated (GenTx blk))
implSnapshotGetTx :: InternalState blk -> TicketNo -> Maybe (Validated (GenTx blk))
implSnapshotGetTx IS{TxSeq (Validated (GenTx blk))
isTxs :: TxSeq (Validated (GenTx blk))
isTxs :: forall blk. InternalState blk -> TxSeq (Validated (GenTx blk))
isTxs} = (TxSeq (Validated (GenTx blk))
isTxs TxSeq (Validated (GenTx blk))
-> TicketNo -> Maybe (Validated (GenTx blk))
forall tx. TxSeq tx -> TicketNo -> Maybe tx
`TxSeq.lookupByTicketNo`)
implSnapshotHasTx :: Ord (GenTxId blk)
=> InternalState blk
-> GenTxId blk
-> Bool
implSnapshotHasTx :: InternalState blk -> GenTxId blk -> Bool
implSnapshotHasTx IS{Set (GenTxId blk)
isTxIds :: forall blk. InternalState blk -> Set (GenTxId blk)
isTxIds :: Set (GenTxId blk)
isTxIds} = (GenTxId blk -> Set (GenTxId blk) -> Bool)
-> Set (GenTxId blk) -> GenTxId blk -> Bool
forall a b c. (a -> b -> c) -> b -> a -> c
flip GenTxId blk -> Set (GenTxId blk) -> Bool
forall a. Ord a => a -> Set a -> Bool
Set.member Set (GenTxId blk)
isTxIds
implSnapshotGetMempoolSize :: InternalState blk
-> MempoolSize
implSnapshotGetMempoolSize :: InternalState blk -> MempoolSize
implSnapshotGetMempoolSize = TxSeq (Validated (GenTx blk)) -> MempoolSize
forall tx. TxSeq tx -> MempoolSize
TxSeq.toMempoolSize (TxSeq (Validated (GenTx blk)) -> MempoolSize)
-> (InternalState blk -> TxSeq (Validated (GenTx blk)))
-> InternalState blk
-> MempoolSize
forall b c a. (b -> c) -> (a -> b) -> a -> c
. InternalState blk -> TxSeq (Validated (GenTx blk))
forall blk. InternalState blk -> TxSeq (Validated (GenTx blk))
isTxs