chain

package
v0.26.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 14, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ChainUpdateEventType = "chain.update"
	ChainForkEventType   = "chain.fork_detected"
)
View Source
const DefaultBlockCacheCapacity = 10000

DefaultBlockCacheCapacity is the default maximum number of blocks to cache. At ~20KB per block, 10K blocks uses ~200MB of memory.

View Source
const DefaultMaxQueuedHeaders = 10_000

DefaultMaxQueuedHeaders is the fallback header queue limit when the security parameter is not yet available (e.g. before ledger is wired).

Variables

View Source
var (
	ErrIntersectNotFound            = errors.New("chain intersect not found")
	ErrRollbackBeyondEphemeralChain = errors.New(
		"cannot rollback ephemeral chain beyond memory buffer",
	)
	ErrRollbackExceedsSecurityParam = errors.New(
		"rollback depth exceeds security parameter K",
	)
	ErrIteratorChainTip = errors.New(
		"chain iterator is at chain tip",
	)
	ErrHeaderQueueFull = errors.New(
		"header queue at maximum capacity",
	)
)

Functions

This section is empty.

Types

type BlockNotFitChainTipError added in v0.4.6

type BlockNotFitChainTipError struct {
	// contains filtered or unexported fields
}

func NewBlockNotFitChainTipError added in v0.4.6

func NewBlockNotFitChainTipError(
	blockHash string,
	blockPrevHash string,
	tipHash string,
) BlockNotFitChainTipError

func (BlockNotFitChainTipError) BlockHash added in v0.22.0

func (e BlockNotFitChainTipError) BlockHash() string

func (BlockNotFitChainTipError) BlockPrevHash added in v0.22.0

func (e BlockNotFitChainTipError) BlockPrevHash() string

func (BlockNotFitChainTipError) Error added in v0.4.6

func (e BlockNotFitChainTipError) Error() string

func (BlockNotFitChainTipError) TipHash added in v0.22.0

func (e BlockNotFitChainTipError) TipHash() string

type BlockNotMatchHeaderError added in v0.4.6

type BlockNotMatchHeaderError struct {
	// contains filtered or unexported fields
}

func NewBlockNotMatchHeaderError added in v0.4.6

func NewBlockNotMatchHeaderError(
	blockHash string,
	headerHash string,
) BlockNotMatchHeaderError

func (BlockNotMatchHeaderError) Error added in v0.4.6

func (e BlockNotMatchHeaderError) Error() string

type Chain

type Chain struct {
	// contains filtered or unexported fields
}

func (*Chain) AddBlock

func (c *Chain) AddBlock(
	block ledger.Block,
	txn *database.Txn,
) error

func (*Chain) AddBlockHeader added in v0.4.6

func (c *Chain) AddBlockHeader(header ledger.BlockHeader) error

func (*Chain) AddBlocks added in v0.5.0

func (c *Chain) AddBlocks(blocks []ledger.Block) error

func (*Chain) AddRawBlocks added in v0.22.0

func (c *Chain) AddRawBlocks(blocks []RawBlock) error

AddRawBlocks adds a batch of pre-extracted blocks to the chain.

func (*Chain) BlockByPoint added in v0.4.6

func (c *Chain) BlockByPoint(
	point ocommon.Point,
	txn *database.Txn,
) (models.Block, error)

func (*Chain) ClearHeaders added in v0.22.0

func (c *Chain) ClearHeaders()

ClearHeaders removes all queued block headers. This is used when the active peer changes and stale headers from the previous peer's chainsync session no longer fit the current chain tip.

func (*Chain) FromPoint

func (c *Chain) FromPoint(
	point ocommon.Point,
	inclusive bool,
) (*ChainIterator, error)

FromPoint returns a ChainIterator starting at the specified point. If inclusive is true, the iterator will start at the specified point. Otherwise it will start at the point following the specified point

func (*Chain) HeaderCount added in v0.4.6

func (c *Chain) HeaderCount() int

func (*Chain) HeaderRange added in v0.4.6

func (c *Chain) HeaderRange(count int) (ocommon.Point, ocommon.Point)

func (*Chain) HeaderTip added in v0.4.6

func (c *Chain) HeaderTip() ochainsync.Tip

func (*Chain) MaxQueuedHeaders added in v0.22.0

func (c *Chain) MaxQueuedHeaders() int

MaxQueuedHeaders returns the maximum number of headers that may be queued. The limit is the larger of securityParam * 2 and DefaultMaxQueuedHeaders. Using the default as a floor ensures the queue is large enough for the chainsync/blockfetch pipeline: headers arrive much faster than blocks, so the queue must accommodate several blockfetch batches worth of headers beyond the accumulation threshold to avoid drops that break the header chain.

func (*Chain) NotifyIterators added in v0.22.0

func (c *Chain) NotifyIterators()

NotifyIterators wakes all blocked iterators waiting for new blocks. Call this after a DB transaction that adds blocks has been committed to ensure iterators see the newly visible data.

func (*Chain) RecentPoints added in v0.22.0

func (c *Chain) RecentPoints(count int) []ocommon.Point

RecentPoints returns up to count recent chain points in descending order (most recent first) using the in-memory chain state. This includes the current tip and, for non-persistent chains, any blocks stored in the in-memory buffer. For persistent chains, it walks backwards through the database using block indices.

This method is useful for building intersection point lists that remain accurate even when the blob store has not yet been fully flushed, since the chain's in-memory tip is always up-to-date.

func (*Chain) Rollback

func (c *Chain) Rollback(point ocommon.Point) error

func (*Chain) Tip

func (c *Chain) Tip() ochainsync.Tip

type ChainBlockEvent

type ChainBlockEvent struct {
	Point ocommon.Point
	Block models.Block
}

type ChainForkEvent added in v0.21.0

type ChainForkEvent struct {
	// ForkPoint is the common ancestor where the chains diverge
	ForkPoint ocommon.Point
	// ForkDepth is the number of blocks rolled back from the canonical chain
	ForkDepth uint64
	// AlternateHead is the tip of the competing chain
	AlternateHead ocommon.Point
	// CanonicalHead is the tip of the current canonical chain
	CanonicalHead ocommon.Point
}

ChainForkEvent is emitted when a chain fork is detected. This allows subscribers to monitor fork activity for alerting and metrics.

type ChainId added in v0.11.0

type ChainId uint64

type ChainIterator

type ChainIterator struct {
	// contains filtered or unexported fields
}

func (*ChainIterator) Cancel added in v0.20.0

func (ci *ChainIterator) Cancel()

func (*ChainIterator) Next

func (ci *ChainIterator) Next(blocking bool) (*ChainIteratorResult, error)

type ChainIteratorResult

type ChainIteratorResult struct {
	Point    ocommon.Point
	Block    models.Block
	Rollback bool
}

type ChainManager added in v0.11.0

type ChainManager struct {
	// contains filtered or unexported fields
}

func NewManager added in v0.11.0

func NewManager(
	db *database.Database,
	eventBus *event.EventBus,
	promRegistry ...prometheus.Registerer,
) (*ChainManager, error)

func (*ChainManager) BlockByPoint added in v0.11.0

func (cm *ChainManager) BlockByPoint(
	point ocommon.Point,
	txn *database.Txn,
) (models.Block, error)

func (*ChainManager) Chain added in v0.11.0

func (cm *ChainManager) Chain(id ChainId) *Chain

func (*ChainManager) NewChain added in v0.11.0

func (cm *ChainManager) NewChain(point ocommon.Point) (*Chain, error)

NewChain creates a new Chain that forks from the primary chain at the specified point. This is useful for managing outbound ChainSync clients

func (*ChainManager) NewChainFromIntersect added in v0.11.0

func (cm *ChainManager) NewChainFromIntersect(
	points []ocommon.Point,
) (*Chain, error)

NewChainFromIntersect creates a new Chain that forks the primary chain at the latest common point.

func (*ChainManager) PrimaryChain added in v0.11.0

func (cm *ChainManager) PrimaryChain() *Chain

func (*ChainManager) SetLedger added in v0.20.0

func (cm *ChainManager) SetLedger(
	ledgerState interface{ SecurityParam() int },
)

type ChainRollbackEvent

type ChainRollbackEvent struct {
	Point            ocommon.Point
	RolledBackBlocks []models.Block // Blocks that were rolled back, in reverse order (newest first)
}

type RawBlock added in v0.22.0

type RawBlock struct {
	Slot        uint64
	Hash        []byte
	BlockNumber uint64
	Type        uint
	PrevHash    []byte
	Cbor        []byte
}

RawBlock contains pre-extracted block fields for direct storage without requiring a full ledger.Block decode.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL