Documentation
¶
Overview ¶
Package blockchain provides functionalities for creating, managing and accessing the blockchain state.
Index ¶
- Constants
- Variables
- func LoadBlockFromFile(name string) (types.Block, error)
- type BlockValidator
- func (v *BlockValidator) CheckAllocs() (errs []error)
- func (v *BlockValidator) CheckFields() (errs []error)
- func (v *BlockValidator) CheckPoW(opts ...types.CallOp) (errs []error)
- func (v *BlockValidator) CheckSize() (errs []error)
- func (v *BlockValidator) CheckTransactions(opts ...types.CallOp) (errs []error)
- type Blockchain
- func (b *Blockchain) APIs() jsonrpc.APISet
- func (b *Blockchain) ChainReader() types.ChainReader
- func (b *Blockchain) CreateAccount(blockNo uint64, chain types.Chainer, account types.Account) error
- func (b *Blockchain) Generate(params *types.GenerateBlockParams, opts ...types.CallOp) (types.Block, error)
- func (b *Blockchain) GetAccount(address util.String, opts ...types.CallOp) (types.Account, error)
- func (b *Blockchain) GetAccountNonce(address util.String, opts ...types.CallOp) (uint64, error)
- func (b *Blockchain) GetBestChain() types.Chainer
- func (b *Blockchain) GetBlock(number uint64, hash util.Hash) (types.Block, error)
- func (b *Blockchain) GetBlockByHash(hash util.Hash, opts ...types.CallOp) (types.Block, error)
- func (b *Blockchain) GetChainReaderByHash(hash util.Hash) types.ChainReader
- func (b *Blockchain) GetChainsReader() (readers []types.ChainReader)
- func (b *Blockchain) GetDB() elldb.DB
- func (b *Blockchain) GetEventEmitter() *emitter.Emitter
- func (b *Blockchain) GetLocators() ([]util.Hash, error)
- func (b *Blockchain) GetTransaction(hash util.Hash, opts ...types.CallOp) (types.Transaction, error)
- func (b *Blockchain) GetTxPool() types.TxPool
- func (b *Blockchain) HaveBlock(hash util.Hash) (bool, error)
- func (b *Blockchain) IsKnownBlock(hash util.Hash) (bool, string, error)
- func (b *Blockchain) IsMainChain(cr types.ChainReader) bool
- func (b *Blockchain) ListAccounts(opts ...types.CallOp) ([]types.Account, error)
- func (b *Blockchain) ListTopAccounts(n int, opts ...types.CallOp) ([]types.Account, error)
- func (b *Blockchain) NewChainFromChainInfo(ci types.ChainInfo) *Chain
- func (b *Blockchain) NewChainTraverser() *ChainTraverser
- func (b *Blockchain) NewWorldReader() *WorldReader
- func (b *Blockchain) OrphanBlocks() types.CacheReader
- func (b *Blockchain) ProcessBlock(block types.Block, opts ...types.CallOp) (types.ChainReader, error)
- func (b *Blockchain) ProcessTransactions(txs []types.Transaction, chain types.Chainer, opts ...types.CallOp) ([]common.Transition, error)
- func (b *Blockchain) SelectTransactions(maxSize int64) (selectedTxs []types.Transaction, err error)
- func (b *Blockchain) SetDB(db elldb.DB)
- func (b *Blockchain) SetEventEmitter(ee *emitter.Emitter)
- func (b *Blockchain) SetGenesisBlock(block types.Block)
- func (b *Blockchain) Up() error
- type Chain
- func (c *Chain) ChainReader() types.ChainReader
- func (c *Chain) CreateAccount(targetBlockNum uint64, account types.Account, opts ...types.CallOp) error
- func (c *Chain) Current(opts ...types.CallOp) (types.Header, error)
- func (c *Chain) GetAccount(address util.String, opts ...types.CallOp) (types.Account, error)
- func (c *Chain) GetAccounts(opts ...types.CallOp) ([]types.Account, error)
- func (c *Chain) GetBlock(number uint64, opts ...types.CallOp) (types.Block, error)
- func (c *Chain) GetID() util.String
- func (c *Chain) GetInfo() types.ChainInfo
- func (c *Chain) GetParent(opts ...types.CallOp) *Chain
- func (c *Chain) GetParentBlock() types.Block
- func (c *Chain) GetRoot() types.Block
- func (c *Chain) GetStore() types.ChainStorer
- func (c *Chain) GetTransaction(hash util.Hash, opts ...types.CallOp) (types.Transaction, error)
- func (c *Chain) HasParent(opts ...types.CallOp) bool
- func (c *Chain) NewStateTree(opts ...types.CallOp) (types.Tree, error)
- func (c *Chain) PutTransactions(txs []types.Transaction, blockNumber uint64, opts ...types.CallOp) error
- func (c *Chain) String() string
- type ChainRead
- func (r *ChainRead) Current(opts ...types.CallOp) (types.Block, error)
- func (r *ChainRead) GetBlock(number uint64, opts ...types.CallOp) (types.Block, error)
- func (r *ChainRead) GetBlockByHash(hash util.Hash, opts ...types.CallOp) (types.Block, error)
- func (r *ChainRead) GetHeader(number uint64, opts ...types.CallOp) (types.Header, error)
- func (r *ChainRead) GetHeaderByHash(hash util.Hash, opts ...types.CallOp) (types.Header, error)
- func (r *ChainRead) GetID() util.String
- func (r *ChainRead) GetParent() types.ChainReader
- func (r *ChainRead) GetParentBlock() types.Block
- func (r *ChainRead) GetRoot() types.Block
- type ChainTraverseFunc
- type ChainTraverser
- type DocType
- type ReOrgInfo
- type TxsValidator
- type VContexts
- type WorldReader
Constants ¶
const ( // MaxOrphanBlocksCacheSize is the number of blocks we can keep in the orphan block cache MaxOrphanBlocksCacheSize = 500 // MaxRejectedBlocksCacheSize is the number of blocks we can keep in the rejected block cache MaxRejectedBlocksCacheSize = 100 )
Variables ¶
var KnownTransactionTypes = []int64{ core.TxTypeBalance, core.TxTypeAlloc, }
KnownTransactionTypes are the supported transaction types
Functions ¶
Types ¶
type BlockValidator ¶
type BlockValidator struct { VContexts // contains filtered or unexported fields }
BlockValidator implements a validator for checking syntactic, contextual and state correctness of a block in relation to various parts of the system.
func NewBlockValidator ¶
func NewBlockValidator(block types.Block, txPool types.TxPool, bchain types.Blockchain, cfg *config.EngineConfig, log logger.Logger) *BlockValidator
NewBlockValidator creates and returns a BlockValidator object
func (*BlockValidator) CheckAllocs ¶
func (v *BlockValidator) CheckAllocs() (errs []error)
CheckAllocs verifies allocation transactions such as transaction fees, mining rewards etc.
func (*BlockValidator) CheckFields ¶
func (v *BlockValidator) CheckFields() (errs []error)
CheckFields checks the field and their values.
func (*BlockValidator) CheckPoW ¶
func (v *BlockValidator) CheckPoW(opts ...types.CallOp) (errs []error)
CheckPoW checks the PoW and difficulty values in the header. If chain is set, the parent chain is search within the provided chain, otherwise, the best chain is searched
func (*BlockValidator) CheckSize ¶
func (v *BlockValidator) CheckSize() (errs []error)
CheckSize checks the size of the blocks
func (*BlockValidator) CheckTransactions ¶
func (v *BlockValidator) CheckTransactions(opts ...types.CallOp) (errs []error)
CheckTransactions validates all transactions in the block in relation to the block's destined chain.
type Blockchain ¶
type Blockchain struct {
// contains filtered or unexported fields
}
Blockchain represents the Ellcrys blockchain. It provides functionalities for interacting with the underlying database and primitives.
func New ¶
func New(txPool types.TxPool, cfg *config.EngineConfig, log logger.Logger) *Blockchain
New creates a Blockchain instance.
func (*Blockchain) ChainReader ¶
func (b *Blockchain) ChainReader() types.ChainReader
ChainReader creates a chain reader for best/main chain
func (*Blockchain) CreateAccount ¶
func (b *Blockchain) CreateAccount(blockNo uint64, chain types.Chainer, account types.Account) error
CreateAccount creates an account that is associated with the given block number and chain.
func (*Blockchain) Generate ¶
func (b *Blockchain) Generate(params *types.GenerateBlockParams, opts ...types.CallOp) (types.Block, error)
Generate produces a valid block for a target chain. By default the main chain is used but a different chain can be passed in as a CallOp.
func (*Blockchain) GetAccount ¶
GetAccount gets an account by its address
func (*Blockchain) GetAccountNonce ¶
GetAccountNonce gets the nonce of an account
func (*Blockchain) GetBestChain ¶
func (b *Blockchain) GetBestChain() types.Chainer
GetBestChain gets the chain that is currently considered the main chain
func (*Blockchain) GetBlock ¶
GetBlock finds a block in any chain with a matching block number and hash.
func (*Blockchain) GetBlockByHash ¶
GetBlockByHash finds a block in any chain with a matching hash.
func (*Blockchain) GetChainReaderByHash ¶
func (b *Blockchain) GetChainReaderByHash(hash util.Hash) types.ChainReader
GetChainReaderByHash returns a chain reader to a chain where a block with the given hash exists
func (*Blockchain) GetChainsReader ¶
func (b *Blockchain) GetChainsReader() (readers []types.ChainReader)
GetChainsReader gets chain reader for all known chains
func (*Blockchain) GetEventEmitter ¶
func (b *Blockchain) GetEventEmitter() *emitter.Emitter
GetEventEmitter gets the event emitter
func (*Blockchain) GetLocators ¶
func (b *Blockchain) GetLocators() ([]util.Hash, error)
GetLocators fetches a list of blockhashes used to compare and sync the local chain with a remote chain. We collect the most recent 10 block hashes and then exponentially fetch more hashes until there are no more blocks.
func (*Blockchain) GetTransaction ¶
func (b *Blockchain) GetTransaction(hash util.Hash, opts ...types.CallOp) (types.Transaction, error)
GetTransaction finds a transaction in the main chain and returns it
func (*Blockchain) GetTxPool ¶
func (b *Blockchain) GetTxPool() types.TxPool
GetTxPool gets the transaction pool
func (*Blockchain) HaveBlock ¶
func (b *Blockchain) HaveBlock(hash util.Hash) (bool, error)
HaveBlock checks whether we have a block matching the hash in any of the known chains
func (*Blockchain) IsKnownBlock ¶
IsKnownBlock checks whether a block with the has exists in at least one of all block chains and caches (e.g orphan)
func (*Blockchain) IsMainChain ¶
func (b *Blockchain) IsMainChain(cr types.ChainReader) bool
IsMainChain checks whether cr is the main chain
func (*Blockchain) ListAccounts ¶
ListAccounts list all accounts
func (*Blockchain) ListTopAccounts ¶
ListTopAccounts lists top n accounts
func (*Blockchain) NewChainFromChainInfo ¶
func (b *Blockchain) NewChainFromChainInfo(ci types.ChainInfo) *Chain
NewChainFromChainInfo creates an instance of a Chain given a NewChainFromChainInfo
func (*Blockchain) NewChainTraverser ¶
func (b *Blockchain) NewChainTraverser() *ChainTraverser
NewChainTraverser creates a new ChainTransverser instance
func (*Blockchain) NewWorldReader ¶
func (b *Blockchain) NewWorldReader() *WorldReader
NewWorldReader creates a new WorldReader
func (*Blockchain) OrphanBlocks ¶
func (b *Blockchain) OrphanBlocks() types.CacheReader
OrphanBlocks returns a cache reader for orphan blocks
func (*Blockchain) ProcessBlock ¶
func (b *Blockchain) ProcessBlock(block types.Block, opts ...types.CallOp) (types.ChainReader, error)
ProcessBlock takes a block, performs initial validations and attempts to add it to the tip of one of the known chains (main chain or forked chain). It returns a chain reader pointing to the chain in which the block was added to.
func (*Blockchain) ProcessTransactions ¶
func (b *Blockchain) ProcessTransactions(txs []types.Transaction, chain types.Chainer, opts ...types.CallOp) ([]common.Transition, error)
ProcessTransactions computes the state transition operations for each transactions that must be applied to the state tree and world state
func (*Blockchain) SelectTransactions ¶
func (b *Blockchain) SelectTransactions(maxSize int64) (selectedTxs []types.Transaction, err error)
SelectTransactions collects transactions from the head of the pool up to the specified maxSize.
func (*Blockchain) SetEventEmitter ¶
func (b *Blockchain) SetEventEmitter(ee *emitter.Emitter)
SetEventEmitter sets the event emitter
func (*Blockchain) SetGenesisBlock ¶
func (b *Blockchain) SetGenesisBlock(block types.Block)
SetGenesisBlock sets the genesis block
func (*Blockchain) Up ¶
func (b *Blockchain) Up() error
Up opens the database, initializes the store and creates the genesis block (if required)
type Chain ¶
type Chain struct {
// contains filtered or unexported fields
}
Chain represents a chain of blocks Implements types.Chainer
func NewChain ¶
NewChain creates an instance of a chain. It will create metadata object for the chain if not exists. It will return error if it is unable to do so.
func NewChainFromChainInfo ¶
func NewChainFromChainInfo(ci *core.ChainInfo, db elldb.DB, cfg *config.EngineConfig, log logger.Logger) *Chain
NewChainFromChainInfo creates a chain with a given chain info
func (*Chain) ChainReader ¶
func (c *Chain) ChainReader() types.ChainReader
ChainReader gets a chain reader for this chain
func (*Chain) CreateAccount ¶
func (c *Chain) CreateAccount(targetBlockNum uint64, account types.Account, opts ...types.CallOp) error
CreateAccount creates an account on a target block
func (*Chain) GetAccount ¶
GetAccount gets an account
func (*Chain) GetAccounts ¶
GetAccounts gets all accounts
func (*Chain) GetParentBlock ¶
GetParentBlock gets the chain's parent block if it has one
func (*Chain) GetRoot ¶
GetRoot finds the block on the main chain from which this chain or its parents/ancestors originate from.
Example: [1]-[2]-[3]-[4]-[5] Main
|__[3]-[4] Chain B |__[4] Chain C
In the example above, the Chain B is the first generation to Chain C. The root parent block of Chain C is [2].
func (*Chain) GetTransaction ¶
GetTransaction gets a transaction by hash
func (*Chain) NewStateTree ¶
NewStateTree creates a new tree seeded with the state root of the chain's tip block. For chains with no block (new chains), the state root of their parent block is used.
func (*Chain) PutTransactions ¶
func (c *Chain) PutTransactions(txs []types.Transaction, blockNumber uint64, opts ...types.CallOp) error
PutTransactions stores a collection of transactions in the chain
type ChainRead ¶
type ChainRead struct {
// contains filtered or unexported fields
}
ChainRead provides read-only access to objects belonging to a single chain.
func NewChainReader ¶
NewChainReader creates a ChainReader object
func (*ChainRead) GetBlock ¶
GetBlock finds and returns a block associated with chainID. When 0 is passed, it should return the block with the highest number
func (*ChainRead) GetBlockByHash ¶
GetBlockByHash finds and returns a block associated with chainID.
func (*ChainRead) GetHeader ¶
GetHeader gets the header of a block. When 0 is passed, it should return the header of the block with the highest number
func (*ChainRead) GetHeaderByHash ¶
GetHeaderByHash finds and returns the header of a block matching hash
func (*ChainRead) GetParent ¶
func (r *ChainRead) GetParent() types.ChainReader
GetParent returns a chain reader to the parent chain. Returns nil if chain has no parent.
func (*ChainRead) GetParentBlock ¶
GetParentBlock returns the parent block
type ChainTraverseFunc ¶
ChainTraverseFunc is the function type that runs a query against a given chain
type ChainTraverser ¶
type ChainTraverser struct {
// contains filtered or unexported fields
}
ChainTraverser allows a user to run a query function against a chain of chains. If no result is found in the start or initial chain, the parent chain is passed to the query function till we reach a chain with no parent.
func (*ChainTraverser) Query ¶
func (t *ChainTraverser) Query(qf ChainTraverseFunc) error
Query begins a chain traversal session. If false is returned, the chain's parent is searched next and so on, until a chain with no parent is encountered. If true is returned, the query ends. If error is returned, the query ends with the error from qf returned.
func (*ChainTraverser) Start ¶
func (t *ChainTraverser) Start(chain types.Chainer) *ChainTraverser
Start sets the start chain
type ReOrgInfo ¶
type ReOrgInfo struct { MainChainID string `json:"mainChainID" msgpack:"mainChainID"` BranchID string `json:"branchID" msgpack:"branchID"` BranchLen uint64 `json:"branchLen" msgpack:"branchLen"` ReOrgLen uint64 `json:"reOrgLen" msgpack:"reOrgLen"` Timestamp int64 `json:"timestamp" msgpack:"timestamp"` }
ReOrgInfo describes a re-organization event
type TxsValidator ¶
type TxsValidator struct { VContexts // contains filtered or unexported fields }
TxsValidator implements a validator for checking syntactic, contextual and state correctness of transactions in relation to various parts of the system.
func NewTxValidator ¶
func NewTxValidator(tx types.Transaction, txPool types.TxPool, bchain types.Blockchain) *TxsValidator
NewTxValidator is like NewTxsValidator except it accepts a single transaction
func NewTxsValidator ¶
func NewTxsValidator(txs []types.Transaction, txPool types.TxPool, bchain types.Blockchain) *TxsValidator
NewTxsValidator creates an instance of TxsValidator
func (*TxsValidator) CheckFields ¶
func (v *TxsValidator) CheckFields(tx types.Transaction) (errs []error)
CheckFields checks validates the transaction fields and values.
func (*TxsValidator) Validate ¶
func (v *TxsValidator) Validate(opts ...types.CallOp) (errs []error)
Validate execute validation checks against each transactions
func (*TxsValidator) ValidateTx ¶
func (v *TxsValidator) ValidateTx(tx types.Transaction, opts ...types.CallOp) []error
ValidateTx validates a single transaction coming received by the gossip handler..
type VContexts ¶
type VContexts struct {
// contains filtered or unexported fields
}
VContexts manages validation contexts
type WorldReader ¶
type WorldReader struct {
// contains filtered or unexported fields
}
WorldReader provides functionalities for reading the the main and side chains. Using a ChainTraverser, it can search for and object from a chain and the chain's grand-parents.