ouroboros-consensus
Safe HaskellNone
LanguageHaskell2010

Ouroboros.Consensus.Leios.TxCache.Reference

Description

The reference implementation of the LeiosTxCache: a bounded, in-memory index over the bodies of recent Leios EBs the node took in, and the txs it holds that those bodies reference, with incremental eviction. Simple and obviously correct, so that a faster implementation can be tested for observational equivalence to it.

This is deliberately independent of the on-disk LeiosDb: the two are separate caches with different eviction policies. This index retains only what is referenced by the bodies taken in at the maxLeiosPointCount freshest points, so it stays small enough to keep resident in memory (a lookup is disk-latency-free); the LeiosDb retains far more (everything referenced by the acquired EBs younger than the immutable tip). Because they are independent stores, sometimes the node will re-fetch and re-validate a tx that has been evicted from the LeiosTxCache even while the LeiosDb still holds it; that is acceptable.

The type parameters are payloads the index does not interpret:

  • a is what we record for a tx that has been inserted (fetched) but not yet applied; () in the node, or the tx's bytes in a test.
  • v is what we record for a tx that has been applied. Kept polymorphic so the index is not coupled to any ledger type.
  • b is the body taken in at a point. It must at least carry the body's TxHashes, via ReferencesTxsByHash, so eviction can find its txs without touching storage.
Synopsis

Index

data LeiosTxCacheIndex a v b Source #

The index.

Constructors

MkLeiosTxCacheIndex 

Fields

data TrackedTx a v Source #

A tx the index holds.

Constructors

MkTrackedTx 

Fields

  • ttYoungestReference ∷ !LeiosPoint

    The youngest retained point that this tx landed at, or whose body was taken in while the index held this tx and references it. Points are evicted oldest first, so the index forgets this tx when it evicts this point.

  • ttState ∷ !(TxState a v)
     
  • ttLocation ∷ !(StrictMaybe TxLocation)

    The youngest retained point this tx landed at, with the tx's offset in that point's body.

Operations

evictOlderThanFoldl' ∷ ReferencesTxsByHash b ⇒ SlotNo → w → (w → TxHash → w) → LeiosTxCacheIndex a v b → (LeiosTxCacheIndex a v b, Set LeiosPoint, w) Source #

Evict every retained point whose slot is strictly older than the boundary, oldest first, as insertBodyAndFoldl' evicts the oldest point. Returns the evicted points, and folds over the txs forgotten with them, walking each evicted body from its first tx to its last and visiting each forgotten tx once: at its first offset in the body of its youngest reference. Advances prunedSlot to the boundary (monotonically), after which insertBodyAndFoldl' retains no point that old.

insertBodyAndFoldl' Source #

Arguments

∷ ReferencesTxsByHash b 
⇒ Int

The capacity to report as ibsCacheCapacity; the pure index has no allocation of its own, so callers pass the production table's.

→ LeiosPoint 
→ b 
→ w 
→ (w → TxOffset → TxHash → BytesSize → Maybe TxLocation → w) 
→ LeiosTxCacheIndex a v b 
→ (LeiosTxCacheIndex a v b, (Maybe LeiosTxCacheInsertBodySummary, w)) 

Retain this point with its body, make it the youngest reference of each tx the body references that the index holds, if it is younger, and summarise that. A no-op when the point is already retained, when it is strictly older than prunedSlot (one the cache has already been pruned past; see evictOlderThanFoldl'), or when maxLeiosPointCount points are retained and it is older than all of them. Otherwise, when maxLeiosPointCount points are retained, it first evicts the oldest (least slot, then least EB hash).

Either way, fold over its txs with their locations, as they are after any eviction and before the insertion.

insertLandedTx ∷ LeiosPoint → TxOffset → TxHash → TxState a v → LeiosTxCacheIndex a v b → (LeiosTxCacheIndex a v b, TxArrivalPrior) Source #

Record that this point now holds this tx, the one its body lists at this offset, with its state. A no-op if the index does not (still) retain the point.

A tx the index does not hold is recorded with this point as its youngest reference and its location. For one it holds, this point becomes its youngest reference if it is younger than the tx's existing youngest reference, and its location if it is younger than the tx's existing location or the tx has none. Either way, the tx is recorded as validated if it already has been, or if this says so.

lookupTx ∷ TxHash → LeiosTxCacheIndex a v b → StrictMaybe (TxState a v) Source #

The state of this tx, if the index holds it.

lookupTxLocation ∷ TxHash → LeiosTxCacheIndex a v b → Maybe TxLocation Source #

Where this tx landed, if it landed at a retained point.