chain

package
v0.1.12 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2024 License: MIT Imports: 10 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 Mainnet added in v0.1.11

func Mainnet() (*consensus.Network, types.Block)

Mainnet returns the network parameters and genesis block for the mainnet Sia blockchain.

func TestnetZen added in v0.1.11

func TestnetZen() (*consensus.Network, types.Block)

TestnetZen returns the chain parameters and genesis block for the "Zen" testnet chain.

Types

type ApplyUpdate

type ApplyUpdate struct {
	consensus.ApplyUpdate

	Block types.Block
	State consensus.State // post-application
}

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

type DB

type DB interface {
	Bucket(name []byte) DBBucket
	CreateBucket(name []byte) (DBBucket, error)
	Flush() error
	Cancel()
}

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 NewDBStore

func NewDBStore(db DB, n *consensus.Network, genesisBlock types.Block) (_ *DBStore, _ consensus.State, err error)

NewDBStore creates a new DBStore using the provided database. The tip state is also returned.

func (*DBStore) AddBlock added in v0.1.12

func (db *DBStore) AddBlock(b types.Block, bs *consensus.V1BlockSupplement)

AddBlock implements Store.

func (*DBStore) AddState added in v0.1.12

func (db *DBStore) AddState(cs consensus.State)

AddState implements Store.

func (*DBStore) AncestorTimestamp added in v0.1.12

func (db *DBStore) AncestorTimestamp(id types.BlockID) (t time.Time, ok bool)

AncestorTimestamp implements Store.

func (*DBStore) ApplyBlock added in v0.1.12

func (db *DBStore) ApplyBlock(s consensus.State, cau consensus.ApplyUpdate, mustCommit bool) (committed bool)

ApplyBlock implements Store.

func (*DBStore) BestIndex

func (db *DBStore) BestIndex(height uint64) (index types.ChainIndex, ok bool)

BestIndex implements Store.

func (*DBStore) Block added in v0.1.12

Block implements Store.

func (*DBStore) Close added in v0.1.12

func (db *DBStore) Close() error

Close flushes any uncommitted data to the underlying DB.

func (*DBStore) RevertBlock added in v0.1.12

func (db *DBStore) RevertBlock(s consensus.State, cru consensus.RevertUpdate)

RevertBlock implements Store.

func (*DBStore) State added in v0.1.12

func (db *DBStore) State(id types.BlockID) (consensus.State, bool)

State implements Store.

func (*DBStore) SupplementTipBlock added in v0.1.12

func (db *DBStore) SupplementTipBlock(b types.Block) (bs consensus.V1BlockSupplement)

SupplementTipBlock implements Store.

func (*DBStore) SupplementTipTransaction added in v0.1.12

func (db *DBStore) SupplementTipTransaction(txn types.Transaction) (ts consensus.V1TransactionSupplement)

SupplementTipTransaction implements Store.

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) AddPoolTransactions added in v0.1.11

func (m *Manager) AddPoolTransactions(txns []types.Transaction) (known bool, err error)

AddPoolTransactions validates a transaction set and adds it to the txpool. If any transaction references an element (SiacoinOutput, SiafundOutput, or FileContract) not present in the blockchain, that element must be created by a previous transaction in the set.

If any transaction in the set is invalid, the entire set is rejected and none of the transactions are added to the pool. If all of the transactions are already known to the pool, AddPoolTransactions returns true.

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) AddV2PoolTransactions added in v0.1.12

func (m *Manager) AddV2PoolTransactions(basis types.ChainIndex, txns []types.V2Transaction) (known bool, _ error)

AddV2PoolTransactions validates a transaction set and adds it to the txpool. If any transaction references an element (SiacoinOutput, SiafundOutput, or FileContract) not present in the blockchain, that element must be created by a previous transaction in the set.

If any transaction in the set is invalid, the entire set is rejected and none of the transactions are added to the pool. If all of the transactions are already known to the pool, AddV2PoolTransactions returns true.

Since v2 transactions include Merkle proofs, AddV2PoolTransactions takes an index specifying the accumulator state for which those proofs are assumed to be valid. If that index differs from the Manager's current tip, the Merkle proofs will be updated accordingly. The original transactions are not modified.

func (*Manager) BestIndex added in v0.1.12

func (m *Manager) BestIndex(height uint64) (types.ChainIndex, bool)

BestIndex returns the index of the block at the specified height within the best chain.

func (*Manager) Block added in v0.1.10

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

Block returns the block with the specified ID.

func (*Manager) BlocksForHistory

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

BlocksForHistory returns up to max 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). It also returns the number of blocks between the end of the returned slice and the current tip.

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) PoolTransaction added in v0.1.11

func (m *Manager) PoolTransaction(id types.TransactionID) (types.Transaction, bool)

PoolTransaction returns the transaction with the specified ID, if it is currently in the pool.

func (*Manager) PoolTransactions added in v0.1.11

func (m *Manager) PoolTransactions() []types.Transaction

PoolTransactions returns the transactions currently in the txpool. Any prefix of the returned slice constitutes a valid transaction set.

func (*Manager) RecommendedFee added in v0.1.11

func (m *Manager) RecommendedFee() types.Currency

RecommendedFee returns the recommended fee (per weight unit) to ensure a high probability of inclusion in the next block.

func (*Manager) RemoveSubscriber added in v0.1.12

func (m *Manager) RemoveSubscriber(s Subscriber)

RemoveSubscriber unsubscribes s from m.

func (*Manager) State added in v0.1.12

func (m *Manager) State(id types.BlockID) (consensus.State, bool)

State returns the state with the specified ID.

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.

func (*Manager) TransactionsForPartialBlock added in v0.1.12

func (m *Manager) TransactionsForPartialBlock(missing []types.Hash256) (txns []types.Transaction, v2txns []types.V2Transaction)

TransactionsForPartialBlock returns the transactions in the txpool with the specified hashes.

func (*Manager) UnconfirmedParents added in v0.1.11

func (m *Manager) UnconfirmedParents(txn types.Transaction) []types.Transaction

UnconfirmedParents returns the transactions in the txpool that are referenced by txn.

func (*Manager) V2PoolTransaction added in v0.1.12

func (m *Manager) V2PoolTransaction(id types.TransactionID) (types.V2Transaction, bool)

V2PoolTransaction returns the v2 transaction with the specified ID, if it is currently in the pool.

func (*Manager) V2PoolTransactions added in v0.1.12

func (m *Manager) V2PoolTransactions() []types.V2Transaction

V2PoolTransactions returns the v2 transactions currently in the txpool. Any prefix of the returned slice constitutes a valid transaction set.

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) Bucket added in v0.1.12

func (db *MemDB) Bucket(name []byte) DBBucket

Bucket implements DB.

func (*MemDB) Cancel added in v0.1.12

func (db *MemDB) Cancel()

Cancel implements DB.

func (*MemDB) CreateBucket added in v0.1.12

func (db *MemDB) CreateBucket(name []byte) (DBBucket, error)

CreateBucket implements DB.

func (*MemDB) Flush added in v0.1.12

func (db *MemDB) Flush() error

Flush implements DB.

type RevertUpdate

type RevertUpdate struct {
	consensus.RevertUpdate

	Block types.Block
	State consensus.State // post-reversion, i.e. pre-application
}

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

type Store

type Store interface {
	BestIndex(height uint64) (types.ChainIndex, bool)
	SupplementTipTransaction(txn types.Transaction) consensus.V1TransactionSupplement
	SupplementTipBlock(b types.Block) consensus.V1BlockSupplement

	Block(id types.BlockID) (types.Block, *consensus.V1BlockSupplement, bool)
	AddBlock(b types.Block, bs *consensus.V1BlockSupplement)
	State(id types.BlockID) (consensus.State, bool)
	AddState(cs consensus.State)
	AncestorTimestamp(id types.BlockID) (time.Time, bool)

	// Except when mustCommit is set, ApplyBlock and RevertBlock are free to
	// commit whenever they see fit.
	ApplyBlock(s consensus.State, cau consensus.ApplyUpdate, mustCommit bool) (committed bool)
	RevertBlock(s consensus.State, cru consensus.RevertUpdate)
}

A Store durably commits Manager-related data to storage. I/O errors must be handled internally, e.g. by panicking or calling os.Exit.

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