chain

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2025 License: MIT Imports: 12 Imported by: 28

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(HostAnnouncement))

ForEachHostAnnouncement calls fn on each host announcement in a block.

func ForEachV2HostAnnouncement added in v0.4.0

func ForEachV2HostAnnouncement(b types.Block, fn func(types.PublicKey, []NetAddress))

ForEachV2HostAnnouncement calls fn on each v2 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 TestnetAnagami added in v0.2.3

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

TestnetAnagami returns the chain parameters and genesis block for the "Anagami" testnet chain.

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) PruneBlock added in v0.8.0

func (db *DBStore) PruneBlock(id types.BlockID)

PruneBlock implements Store.

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 {
	PublicKey  types.PublicKey `json:"publicKey"`
	NetAddress string          `json:"netAddress"`
}

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) FromArbitraryData added in v0.4.0

func (ha *HostAnnouncement) FromArbitraryData(arb []byte) bool

FromArbitraryData decodes a host announcement from arbitrary data.

func (HostAnnouncement) ToArbitraryData added in v0.0.2

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

ToArbitraryData encodes a host announcement as arbitrary data.

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, opts ...ManagerOption) *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) UpdateV2TransactionSet added in v0.6.0

func (m *Manager) UpdateV2TransactionSet(txns []types.V2Transaction, from, to types.ChainIndex) ([]types.V2Transaction, error)

UpdateV2TransactionSet updates the basis of a transaction set from "from" to "to". If from and to are equal, the transaction set is returned as-is. Any transactions that were confirmed are removed from the set. Any ephemeral state elements that were created by an update are updated.

If it is undesirable to modify the transaction set, deep-copy it before calling this method.

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.

func (*Manager) V2TransactionSet added in v0.3.1

func (m *Manager) V2TransactionSet(basis types.ChainIndex, txn types.V2Transaction) (types.ChainIndex, []types.V2Transaction, error)

V2TransactionSet returns the full transaction set and basis necessary for broadcasting a transaction. If the provided basis does not match the current tip, the transaction will be updated. The transaction set includes the parents and the transaction itself in an order valid for broadcasting.

type ManagerOption added in v0.3.1

type ManagerOption func(*Manager)

A ManagerOption sets an optional parameter on a Manager.

func WithLog added in v0.3.1

func WithLog(l *zap.Logger) ManagerOption

WithLog sets the logger used by the Manager.

func WithPruneTarget added in v0.8.0

func WithPruneTarget(n uint64) ManagerOption

WithPruneTarget sets the target number of blocks to store.

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 NetAddress added in v0.4.0

type NetAddress struct {
	Protocol Protocol `json:"protocol"`
	Address  string   `json:"address"`
}

A NetAddress is a pair of protocol and address that a host may be reached on

func (*NetAddress) DecodeFrom added in v0.4.0

func (na *NetAddress) DecodeFrom(d *types.Decoder)

DecodeFrom implements types.DecoderFrom.

func (NetAddress) EncodeTo added in v0.4.0

func (na NetAddress) EncodeTo(e *types.Encoder)

EncodeTo implements types.EncoderTo.

type Protocol added in v0.4.0

type Protocol string

A Protocol is a string identifying a network protocol that a host may be reached on.

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)
	PruneBlock(id types.BlockID)
	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.

type V2HostAnnouncement added in v0.4.0

type V2HostAnnouncement []NetAddress

A V2HostAnnouncement lists all the network addresses a host may be reached on

func (*V2HostAnnouncement) FromAttestation added in v0.4.0

func (ha *V2HostAnnouncement) FromAttestation(a types.Attestation) error

FromAttestation decodes a host announcement from an attestation.

func (V2HostAnnouncement) ToAttestation added in v0.4.0

ToAttestation encodes a host announcement as an attestation.

Jump to

Keyboard shortcuts

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