chain

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2024 License: MIT Imports: 11 Imported by: 14

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 ForEachHostAnnouncement added in v0.0.2

func ForEachHostAnnouncement(b types.Block, fn func(types.PublicKey, HostAnnouncement))

ForEachHostAnnouncement calls fn on each host announcement in a block.

func Mainnet

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

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

func TestnetZen

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

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

AddBlock implements Store.

func (*DBStore) AddState

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

AddState implements Store.

func (*DBStore) AncestorTimestamp

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

AncestorTimestamp implements Store.

func (*DBStore) ApplyBlock

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

ApplyBlock implements Store.

func (*DBStore) BestIndex

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

BestIndex implements Store.

func (*DBStore) Block

Block implements Store.

func (*DBStore) Flush added in v0.0.4

func (db *DBStore) Flush() error

Flush flushes any uncommitted data to the underlying DB.

func (*DBStore) RevertBlock

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

RevertBlock implements Store.

func (*DBStore) State

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

State implements Store.

func (*DBStore) SupplementTipBlock

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

SupplementTipBlock implements Store.

func (*DBStore) SupplementTipTransaction

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

SupplementTipTransaction implements Store.

type HostAnnouncement added in v0.0.2

type HostAnnouncement struct {
	NetAddress string
}

A HostAnnouncement represents a signed announcement of a host's network address. Announcements may be made via arbitrary data (in a v1 transaction) or via attestation (in a v2 transaction).

func (HostAnnouncement) ToArbitraryData added in v0.0.2

func (ha HostAnnouncement) ToArbitraryData(sk types.PrivateKey) []byte

ToArbitraryData encodes a host announcement as arbitrary data.

func (HostAnnouncement) ToAttestation added in v0.0.2

ToAttestation encodes a host announcement as an attestation.

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

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) AddV2PoolTransactions

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 and none of their memory is retained.

func (*Manager) BestIndex

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

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, maxBlocks 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) OnReorg added in v0.0.4

func (m *Manager) OnReorg(fn func(types.ChainIndex)) (cancel func())

OnReorg adds fn to the set of functions that are called whenever the best chain changes. It returns a function that removes fn from the set.

The supplied function must not block or call any Manager methods.

func (*Manager) PoolTransaction

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

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

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) State

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

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

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

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

func (*Manager) UpdatesSince added in v0.0.4

func (m *Manager) UpdatesSince(index types.ChainIndex, maxBlocks int) (rus []RevertUpdate, aus []ApplyUpdate, err error)

UpdatesSince returns at most max updates on the path between index and the Manager's current tip.

func (*Manager) V2PoolTransaction

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

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

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

Bucket implements DB.

func (*MemDB) Cancel

func (db *MemDB) Cancel()

Cancel implements DB.

func (*MemDB) CreateBucket

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

CreateBucket implements DB.

func (*MemDB) Flush

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)

	// ApplyBlock and RevertBlock are free to commit whenever they see fit.
	ApplyBlock(s consensus.State, cau consensus.ApplyUpdate)
	RevertBlock(s consensus.State, cru consensus.RevertUpdate)
	Flush() error
}

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

Jump to

Keyboard shortcuts

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