chain

package
v0.1.10 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrFutureBlock is returned when a block's timestamp is too far in the future.
	ErrFutureBlock = errors.New("block's timestamp is too far in the future")
)

Functions

func NewDBStore

func NewDBStore(db DB, genesisState consensus.State, genesisBlock types.Block) (*DBStore, Checkpoint, error)

NewDBStore creates a new DBStore using the provided database. The current checkpoint is also returned.

Types

type ApplyUpdate

type ApplyUpdate struct {
	Block types.Block
	State consensus.State // post-application
	Diff  consensus.BlockDiff
}

An ApplyUpdate reflects the changes to the blockchain resulting from the addition of a block.

type Checkpoint

type Checkpoint struct {
	Block types.Block
	State consensus.State
	Diff  *consensus.BlockDiff // nil if the block has not been validated
}

A Checkpoint pairs a block with its resulting chain state.

func (*Checkpoint) DecodeFrom

func (c *Checkpoint) DecodeFrom(d *types.Decoder)

DecodeFrom implements types.DecoderFrom.

func (Checkpoint) EncodeTo

func (c Checkpoint) EncodeTo(e *types.Encoder)

EncodeTo implements types.EncoderTo.

type DB

type DB interface {
	View(func(DBTx) error) error
	Update(func(DBTx) error) error
}

A DB is a generic key-value database.

type DBBucket

type DBBucket interface {
	Get(key []byte) []byte
	Put(key, value []byte) error
	Delete(key []byte) error
}

A DBBucket is a set of key-value pairs.

type DBStore

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

DBStore implements Store using a key-value database.

func (DBStore) AddCheckpoint

func (db DBStore) AddCheckpoint(c Checkpoint) error

AddCheckpoint implements Store.

func (DBStore) ApplyDiff

func (db DBStore) ApplyDiff(s consensus.State, diff consensus.BlockDiff) (mayCommit bool, err error)

ApplyDiff implements Store.

func (DBStore) BestIndex

func (db DBStore) BestIndex(height uint64) (index types.ChainIndex, err error)

BestIndex implements Store.

func (DBStore) Checkpoint

func (db DBStore) Checkpoint(id types.BlockID) (c Checkpoint, err error)

Checkpoint implements Store.

func (DBStore) RevertDiff

func (db DBStore) RevertDiff(s consensus.State, diff consensus.BlockDiff) error

RevertDiff implements Store.

func (DBStore) WithConsensus

func (db DBStore) WithConsensus(fn func(consensus.Store) error) error

WithConsensus implements Store.

type DBTx

type DBTx interface {
	Bucket(name []byte) DBBucket
	CreateBucket(name []byte) (DBBucket, error)
}

A DBTx is a transaction executed on a key-value database.

type Manager

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

A Manager tracks multiple blockchains and identifies the best valid chain.

func NewManager

func NewManager(store Store, cs consensus.State) *Manager

NewManager returns a Manager initialized with the provided Store and State.

func (*Manager) AddBlocks

func (m *Manager) AddBlocks(blocks []types.Block) error

AddBlocks adds a sequence of blocks to a tracked chain. If the blocks are valid, the chain may become the new best chain, triggering a reorg.

func (*Manager) AddSubscriber

func (m *Manager) AddSubscriber(s Subscriber, tip types.ChainIndex) error

AddSubscriber subscribes s to m, ensuring that it will receive updates when the best chain changes. If tip does not match the Manager's current tip, s is updated accordingly.

func (*Manager) Block added in v0.1.10

func (m *Manager) Block(id types.BlockID) (types.Block, error)

Block returns the block with the specified ID.

func (*Manager) BlocksForHistory

func (m *Manager) BlocksForHistory(blocks []types.Block, history []types.BlockID) ([]types.Block, error)

BlocksForHistory fills the provided slice with consecutive blocks from the best chain, starting from the "attach point" -- the first ID in the history that is present in the best chain (or, if no match is found, genesis).

The returned slice may have fewer than len(blocks) elements if the end of the best chain is reached.

func (*Manager) History

func (m *Manager) History() ([32]types.BlockID, error)

History returns a set of block IDs that span the best chain, beginning with the 10 most-recent blocks, and subsequently spaced exponentionally farther apart until reaching the genesis block.

func (*Manager) Tip

func (m *Manager) Tip() types.ChainIndex

Tip returns the tip of the best known valid chain.

func (*Manager) TipState

func (m *Manager) TipState() consensus.State

TipState returns the consensus state for the current tip.

type MemDB

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

MemDB implements DB with an in-memory map.

func NewMemDB

func NewMemDB() *MemDB

NewMemDB returns an in-memory DB for use with DBStore.

func (*MemDB) Update

func (db *MemDB) Update(fn func(DBTx) error) error

Update implements DB.

func (*MemDB) View

func (db *MemDB) View(fn func(DBTx) error) error

View implements DB.

type RevertUpdate

type RevertUpdate struct {
	Block types.Block
	State consensus.State // post-reversion, i.e. pre-application
	Diff  consensus.BlockDiff
}

A RevertUpdate reflects the changes to the blockchain resulting from the removal of a block.

type Store

type Store interface {
	WithConsensus(func(consensus.Store) error) error
	AddCheckpoint(c Checkpoint) error
	Checkpoint(id types.BlockID) (Checkpoint, error)
	BestIndex(height uint64) (types.ChainIndex, error)
	ApplyDiff(s consensus.State, diff consensus.BlockDiff) (mayCommit bool, err error)
	RevertDiff(s consensus.State, diff consensus.BlockDiff) error
}

A Store durably commits Manager-related data to storage.

type Subscriber

type Subscriber interface {
	// Implementations MUST not commit updates to persistent storage unless mayCommit is set.
	ProcessChainApplyUpdate(cau *ApplyUpdate, mayCommit bool) error
	ProcessChainRevertUpdate(cru *RevertUpdate) error
}

A Subscriber processes updates to the blockchain. Implementations must not modify or retain the provided update object.

Jump to

Keyboard shortcuts

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