ouroboros-consensus
Safe HaskellNone
LanguageHaskell2010

Ouroboros.Consensus.Mempool.Impl.Common

Description

Synopsis

Internal state

data InternalState blk Source #

Internal state in the mempool

Constructors

IS 

Fields

  • isTxs ∷ !(TxSeq (TxMeasureWithDiffTime blk) (ValidatedTxWithDiffs blk))

    Transactions currently in the mempool

    NOTE: the total size of the transactions in isTxs may exceed the current capacity (isCapacity). When the capacity computed from the ledger has shrunk, we don't remove transactions from the Mempool to satisfy the new lower limit. We let the transactions get removed in the normal way: by becoming invalid w.r.t. the updated ledger state. We treat a Mempool over capacity in the same way as a Mempool at capacity.

  • isTxIds ∷ !(Set (GenTxId blk))

    The cached IDs of transactions currently in the mempool.

    This allows one to more quickly lookup transactions by ID from a MempoolSnapshot (see snapshotHasTx).

    This should always be in-sync with the transactions in isTxs.

  • isTxKeys ∷ !(LedgerTables blk KeysMK)

    The cached set of keys needed for the transactions currently in the mempool.

    INVARIANT: isTxKeys == foldMap (getTransactionKeySets . txForgetValidated) $ toList isTxs

  • isTxValues ∷ !(LedgerTables blk ValuesMK)

    The cached values corresponding to reading isTxKeys at isLedgerState. These values can be used unless we switch to a different ledger state. It usually happens in the forging loop that the same ledger state that was in isLedgerState is used, but ticked to a different slot so we can reuse these values.

    INVARIANT: isTxValues should be equal to getForkerAtTarget ... isLedgerState >>= f -> forkerReadTables f isTxKeys

  • isLedgerState ∷ !(TickedLedgerState blk DiffMK)

    The cached ledger state after applying the transactions in the Mempool against the chain's ledger state. New transactions will be validated against this ledger.

    INVARIANT: isLedgerState is the ledger resulting from applying the transactions in isTxs against the ledger identified isTip as tip.

  • isTip ∷ !(Point blk)

    The tip of the chain that isTxs was validated against

  • isSlotNo ∷ !SlotNo

    The most recent SlotNo that isTxs was validated against

    Note in particular that if the mempool is revalidated against a state S at slot s, then the state will be ticked (for now to the successor slot, see tickLedgerState) and isSlotNo will be set to succ s, which is different from the slot of the original ledger state, which will remain in isTip.

  • isLastTicketNo ∷ !TicketNo

    The mempool TicketNo counter.

    See vrLastTicketNo for more information.

  • isCapacity ∷ !(TxMeasure blk)

    Current maximum capacity of the Mempool. Result of computeMempoolCapacity using the current chain's TickedLedgerState.

    NOTE: this does not correspond to isLedgerState, which is the TickedLedgerState after applying the transactions in the Mempool. There might be a transaction in the Mempool triggering a change in the maximum transaction capacity of a block, which would change the Mempool's capacity (unless overridden). We don't want the Mempool's capacity to depend on its contents. The mempool is assuming all its transactions will be in the next block. So any changes caused by that block will take effect after applying it and will only affect the next block.

  • isRemovalCounter ∷ !Word64

    A monotonic counter bumped each time implRemoveTxsEvenIfValid drops a transaction. A sync carries it along its (off-lock) candidate and re-checks it against the committed state under the lock: a mismatch means a removal raced the sync, whose candidate may have resurrected the dropped tx, so it must retry. Preserved by adds and syncs, bumped only by removals.

Instances

Instances details
Generic (InternalState blk) Source # 
Instance details

Defined in Ouroboros.Consensus.Mempool.Impl.Common

Methods

fromInternalState blk → Rep (InternalState blk) x #

toRep (InternalState blk) x → InternalState blk #

(NoThunks (Validated (GenTx blk)), NoThunks (GenTxId blk), NoThunks (TickedLedgerState blk DiffMK), NoThunks (TxIn blk), NoThunks (TxOut blk), NoThunks (TxMeasurePhase1 blk), NoThunks (TxMeasurePhase2 blk), StandardHash blk, Typeable blk)NoThunks (InternalState blk) Source # 
Instance details

Defined in Ouroboros.Consensus.Mempool.Impl.Common

type Rep (InternalState blk) Source # 
Instance details

Defined in Ouroboros.Consensus.Mempool.Impl.Common

data ValidatedTxWithDiffs blk Source #

We cache the differences produced by validating each transaction, as the current differences in UTxO-HD do not depend on which block the transaction was validated. This means that these differences cannot be "stale", as long as the transaction is considered, the differences will be the same. If we extend UTxO-HD to consider more differences, this might be violated and we will have to reconsider what we can cache and what we can't.

Instances

Instances details
Generic (ValidatedTxWithDiffs blk) Source # 
Instance details

Defined in Ouroboros.Consensus.Mempool.Impl.Common

Associated Types

type Rep (ValidatedTxWithDiffs blk) 
Instance details

Defined in Ouroboros.Consensus.Mempool.Impl.Common

type Rep (ValidatedTxWithDiffs blk) = D1 ('MetaData "ValidatedTxWithDiffs" "Ouroboros.Consensus.Mempool.Impl.Common" "ouroboros-consensus-4.0.0.0-inplace" 'False) (C1 ('MetaCons "ValidatedTxWithDiffs" 'PrefixI 'True) (S1 ('MetaSel ('Just "validatedTx") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Validated (GenTx blk))) :*: S1 ('MetaSel ('Just "validatedTxDiffs") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (LedgerTables blk DiffMK))))
(NoThunks (Validated (GenTx blk)), NoThunks (TxIn blk), NoThunks (TxOut blk))NoThunks (ValidatedTxWithDiffs blk) Source # 
Instance details

Defined in Ouroboros.Consensus.Mempool.Impl.Common

type Rep (ValidatedTxWithDiffs blk) Source # 
Instance details

Defined in Ouroboros.Consensus.Mempool.Impl.Common

type Rep (ValidatedTxWithDiffs blk) = D1 ('MetaData "ValidatedTxWithDiffs" "Ouroboros.Consensus.Mempool.Impl.Common" "ouroboros-consensus-4.0.0.0-inplace" 'False) (C1 ('MetaCons "ValidatedTxWithDiffs" 'PrefixI 'True) (S1 ('MetaSel ('Just "validatedTx") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Validated (GenTx blk))) :*: S1 ('MetaSel ('Just "validatedTxDiffs") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (LedgerTables blk DiffMK))))

isMempoolSizeTxLimits blk ⇒ InternalState blk → MempoolSize Source #

\( O(1) \). Return the number of transactions in the internal state of the Mempool paired with their total size in bytes.

Mempool environment

data MempoolEnv (m ∷ TypeType) blk Source #

The mempool environment captures all the associated variables wrt the Mempool and is accessed by the Mempool interface on demand to perform the different operations.

Constructors

MempoolEnv 

Fields

Ledger interface

newtype LedgerInterface (m ∷ TypeType) blk Source #

Abstract interface needed to run a Mempool.

data MempoolLedgerDBView (m ∷ TypeType) blk Source #

Constructors

MempoolLedgerDBView 

Fields

chainDBLedgerInterface ∷ ∀ (m ∷ TypeType) blk. (IOLike m, IsLedger LedgerState blk)ChainDB m blk → LedgerInterface m blk Source #

Create a LedgerInterface from a ChainDB.

Validation

data RevalidateTxsResult blk Source #

Constructors

RevalidateTxsResult 

Fields

computeSnapshot Source #

Arguments

(LedgerSupportsMempool blk, HasTxId (GenTx blk)) 
LedgerConfig blk 
SlotNo 
TickedLedgerState blk DiffMK

The ticked ledger state againt which txs will be revalidated

LedgerTables blk ValuesMK

The tables with all the inputs for the transactions

→ [TxTicket (TxMeasureWithDiffTime blk) (Validated (GenTx blk))] 
MempoolSnapshot blk 

Compute snapshot is largely the same as revalidate the transactions but we ignore the diffs.

revalidateTxsFor Source #

Arguments

(Monad m, LedgerSupportsMempool blk, HasTxId (GenTx blk)) 
ReadOnlyForker m LedgerState blk

The forker to read the transactions' inputs from.

MempoolCapacityBytesOverride 
LedgerConfig blk 
SlotNo 
TickedLedgerState blk DiffMK

The ticked ledger state againt which txs will be revalidated

TicketNo

isLastTicketNo and vrLastTicketNo

Word64

The removal generation to stamp on the result (see isRemovalCounter).

→ [TxTicket (TxMeasureWithDiffTime blk) (ValidatedTxWithDiffs blk)] 
→ m (RevalidateTxsResult blk) 

Revalidate the given transactions against the given ticked ledger state, producing a new InternalState.

Note that this function will perform revalidation so it is expected that the transactions given to it were previously applied, for example if we are revalidating the whole set of transactions onto a new state, or if we remove some transactions and revalidate the remaining ones.

revalidateTxsFor' Source #

Arguments

(Monad m, LedgerSupportsMempool blk, HasTxId (GenTx blk)) 
ReadOnlyForker m LedgerState blk

The forker to read the delta txs' inputs from.

MempoolCapacityBytesOverride 
LedgerConfig blk 
SlotNo 
RevalidateTxsResult blk

The result so far: its state is extended in place with the delta (no full rebuild), and its removed txs are carried forward so the loop accumulates them without any bookkeeping of its own. The candidate already carries the base it was revalidated against (via isLedgerState/isTip), so the base ledger need not be passed separately.

TicketNo

The new isLastTicketNo (the mempool's current ticket counter).

→ [TxTicket (TxMeasureWithDiffTime blk) (ValidatedTxWithDiffs blk)]

The delta txs, in ascending TicketNo order.

→ m (RevalidateTxsResult blk) 

The general revalidation step: reapply a delta of already-validated txs on top of the candidate carried in the given RevalidateTxsResult, without reprocessing what it already holds, appending any newly-removed txs to those carried in. revalidateTxsFor is the special case that starts from an empty candidate.

deltaTxTickets are the txs added since the candidate was revalidated, in ascending ticket order. Their inputs are read from frk here rather than by the caller — the keys to read are derived from the txs anyway. Seeding the delta from the candidate's post-reapply ledger (isLedgerState) lets a sync shrink its work off the lock and hold the lock only for a small final delta (implSyncWithLedger).

validateNewTransaction Source #

Arguments

(LedgerSupportsMempool blk, HasTxId (GenTx blk)) 
LedgerConfig blk 
WhetherToIntervene 
GenTx blk 
TxMeasure blk 
LedgerTables blk ValuesMK

Values to cache if success

TickedLedgerState blk ValuesMK

This state is the internal state with the tables for this transaction advanced through the diffs in the internal state. One could think we can create this value here, but it is needed for some other uses like calling txMeasure before this function.

InternalState blk 
→ (Either (ApplyTxErr blk) (Validated (GenTx blk), LedgerTables blk DiffMK), DiffTimeMeasureInternalState blk) 

Extend InternalState with a new transaction (one which we have not previously validated) that may or may not be valid in this ledger state.

Tracing

data MempoolRejectionDetails Source #

Constructors

MempoolRejectedByLedger

The ledger's MEMPOOL rule rejected the tx

MempoolRejectedByTimeoutSoft !DiffTime

The tx violated mempoolTimeoutSoft

It did not violate mempoolTimeoutHard, since that would raise an exception instead of merely rejecting the tx (not even constructing a MempoolTxRejected).

data TraceEventMempool blk Source #

Events traced by the Mempool.

Constructors

TraceMempoolAddedTx 

Fields

  • (Validated (GenTx blk))

    New, valid transaction that was added to the Mempool.

  • MempoolSize

    The size of the Mempool before adding the transaction.

  • MempoolSize

    The size of the Mempool after adding the transaction.

TraceMempoolRejectedTx 

Fields

TraceMempoolRemoveTxs 

Fields

  • [(GenTx blk, ApplyTxErr blk)]

    Previously valid transactions that are no longer valid because of changes in the ledger state (details are in the provided ApplyTxErr). These transactions have been removed from the Mempool.

  • MempoolSize

    The current size of the Mempool.

TraceMempoolManuallyRemovedTxs 

Fields

  • (NonEmpty (GenTxId blk))

    Transactions that have been manually removed from the Mempool.

  • [GenTx blk]

    Previously valid transactions that are no longer valid because they dependend on transactions that were manually removed from the Mempool. These transactions have also been removed from the Mempool.

    This list shares not transactions with the list of manually removed transactions.

  • MempoolSize

    The current size of the Mempool.

TraceMempoolSynced

Emitted when the mempool is adjusted after the tip has changed.

Fields

TraceMempoolSyncNotNeeded (Point blk)

A sync is not needed, as the point at the tip of the LedgerDB and the point at the mempool are the same.

TraceMempoolAttemptingAdd (GenTx blk)

We will try to add a transaction.

TraceMempoolTipMovedBetweenSTMBlocks

When performing a re-sync we will read the LedgerDB tip twice. This trace will be emitted if in between those two steps the LedgerDB moved to an alternative fork. It is completely innocuous but we would like to double check that it happens very rarely or almost never.

Instances

Instances details
(Eq (GenTx blk), Eq (Validated (GenTx blk)), Eq (GenTxId blk), Eq (ApplyTxErr blk), StandardHash blk)Eq (TraceEventMempool blk) Source # 
Instance details

Defined in Ouroboros.Consensus.Mempool.Impl.Common

Generic (TraceEventMempool blk) Source # 
Instance details

Defined in Ouroboros.Consensus.Mempool.Impl.Common

Associated Types

type Rep (TraceEventMempool blk) 
Instance details

Defined in Ouroboros.Consensus.Mempool.Impl.Common

type Rep (TraceEventMempool blk) = D1 ('MetaData "TraceEventMempool" "Ouroboros.Consensus.Mempool.Impl.Common" "ouroboros-consensus-4.0.0.0-inplace" 'False) (((C1 ('MetaCons "TraceMempoolAddedTx" 'PrefixI 'False) (S1 ('MetaSel ('NothingMaybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Validated (GenTx blk))) :*: (S1 ('MetaSel ('NothingMaybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 MempoolSize) :*: S1 ('MetaSel ('NothingMaybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 MempoolSize))) :+: C1 ('MetaCons "TraceMempoolRejectedTx" 'PrefixI 'False) ((S1 ('MetaSel ('NothingMaybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (GenTx blk)) :*: S1 ('MetaSel ('NothingMaybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (ApplyTxErr blk))) :*: (S1 ('MetaSel ('NothingMaybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 MempoolRejectionDetails) :*: S1 ('MetaSel ('NothingMaybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 MempoolSize)))) :+: (C1 ('MetaCons "TraceMempoolRemoveTxs" 'PrefixI 'False) (S1 ('MetaSel ('NothingMaybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 [(GenTx blk, ApplyTxErr blk)]) :*: S1 ('MetaSel ('NothingMaybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 MempoolSize)) :+: C1 ('MetaCons "TraceMempoolManuallyRemovedTxs" 'PrefixI 'False) (S1 ('MetaSel ('NothingMaybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (NonEmpty (GenTxId blk))) :*: (S1 ('MetaSel ('NothingMaybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 [GenTx blk]) :*: S1 ('MetaSel ('NothingMaybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 MempoolSize))))) :+: ((C1 ('MetaCons "TraceMempoolSynced" 'PrefixI 'False) (S1 ('MetaSel ('NothingMaybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 EnclosingTimed)) :+: C1 ('MetaCons "TraceMempoolSyncNotNeeded" 'PrefixI 'False) (S1 ('MetaSel ('NothingMaybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Point blk)))) :+: (C1 ('MetaCons "TraceMempoolAttemptingAdd" 'PrefixI 'False) (S1 ('MetaSel ('NothingMaybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (GenTx blk))) :+: C1 ('MetaCons "TraceMempoolTipMovedBetweenSTMBlocks" 'PrefixI 'False) (U1TypeType))))

Methods

fromTraceEventMempool blk → Rep (TraceEventMempool blk) x #

toRep (TraceEventMempool blk) x → TraceEventMempool blk #

(Show (GenTx blk), Show (Validated (GenTx blk)), Show (GenTxId blk), Show (ApplyTxErr blk), StandardHash blk)Show (TraceEventMempool blk) Source # 
Instance details

Defined in Ouroboros.Consensus.Mempool.Impl.Common

type Rep (TraceEventMempool blk) Source # 
Instance details

Defined in Ouroboros.Consensus.Mempool.Impl.Common

type Rep (TraceEventMempool blk) = D1 ('MetaData "TraceEventMempool" "Ouroboros.Consensus.Mempool.Impl.Common" "ouroboros-consensus-4.0.0.0-inplace" 'False) (((C1 ('MetaCons "TraceMempoolAddedTx" 'PrefixI 'False) (S1 ('MetaSel ('NothingMaybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Validated (GenTx blk))) :*: (S1 ('MetaSel ('NothingMaybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 MempoolSize) :*: S1 ('MetaSel ('NothingMaybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 MempoolSize))) :+: C1 ('MetaCons "TraceMempoolRejectedTx" 'PrefixI 'False) ((S1 ('MetaSel ('NothingMaybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (GenTx blk)) :*: S1 ('MetaSel ('NothingMaybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (ApplyTxErr blk))) :*: (S1 ('MetaSel ('NothingMaybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 MempoolRejectionDetails) :*: S1 ('MetaSel ('NothingMaybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 MempoolSize)))) :+: (C1 ('MetaCons "TraceMempoolRemoveTxs" 'PrefixI 'False) (S1 ('MetaSel ('NothingMaybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 [(GenTx blk, ApplyTxErr blk)]) :*: S1 ('MetaSel ('NothingMaybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 MempoolSize)) :+: C1 ('MetaCons "TraceMempoolManuallyRemovedTxs" 'PrefixI 'False) (S1 ('MetaSel ('NothingMaybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (NonEmpty (GenTxId blk))) :*: (S1 ('MetaSel ('NothingMaybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 [GenTx blk]) :*: S1 ('MetaSel ('NothingMaybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 MempoolSize))))) :+: ((C1 ('MetaCons "TraceMempoolSynced" 'PrefixI 'False) (S1 ('MetaSel ('NothingMaybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 EnclosingTimed)) :+: C1 ('MetaCons "TraceMempoolSyncNotNeeded" 'PrefixI 'False) (S1 ('MetaSel ('NothingMaybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Point blk)))) :+: (C1 ('MetaCons "TraceMempoolAttemptingAdd" 'PrefixI 'False) (S1 ('MetaSel ('NothingMaybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (GenTx blk))) :+: C1 ('MetaCons "TraceMempoolTipMovedBetweenSTMBlocks" 'PrefixI 'False) (U1TypeType))))

Conversions

snapshotFromIS(LedgerSupportsMempool blk, HasTxId (GenTx blk))InternalState blk → MempoolSnapshot blk Source #

Create a Mempool Snapshot from a given Internal State of the mempool.

The internal state already maintains both the sequence of transactions (isTxs) and the set of their ids (isTxIds), so this is O(1): it only wraps the structures that are there, without recomputing either of them.

snapshotFromValidTxs(LedgerSupportsMempool blk, HasTxId (GenTx blk)) ⇒ [TxTicket (TxMeasureWithDiffTime blk) (Validated (GenTx blk))] → Point blk → SlotNoMempoolSnapshot blk Source #

Create a Mempool Snapshot from a list of validated transactions.

Unlike snapshotFromIS this has to build the transaction sequence and the set of transaction ids, so it is O(n) in the number of transactions and computes the txid of every one of them. Only use it when there is no internal state to take those from, for example after revalidating transactions in computeSnapshot.

Ticking a ledger state

tickLedgerState(UpdateLedger blk, ValidateEnvelope blk)LedgerConfig blk → ForgeLedgerState blk → (SlotNo, TickedLedgerState blk DiffMK) Source #

Tick the LedgerState using the given BlockSlot.