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.ChainReaderFactory
- 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.ChainReaderFactory
- func (b *Blockchain) GetChainsReader() (readers []types.ChainReaderFactory)
- 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) IsMainChain(cr types.ChainReaderFactory) 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.ChainReaderFactory, error)
- func (b *Blockchain) ProcessTransactions(txs []types.Transaction, chain types.Chainer, opts ...types.CallOp) ([]common.Transition, error)
- func (b *Blockchain) SetBestChain(c *Chain)
- func (b *Blockchain) SetCoinbase(coinbase *crypto.Key)
- 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.ChainReaderFactory
- 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) GetMinedBlocks(args *core.ArgGetMinedBlock, opts ...types.CallOp) ([]*core.MinedBlock, bool, error)
- 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) PutMinedBlock(block types.Block, opts ...types.CallOp) error
- func (c *Chain) PutTransactions(txs []types.Transaction, blockNumber uint64, opts ...types.CallOp) error
- func (c Chain) String() string
- type ChainReader
- func (r *ChainReader) Current(opts ...types.CallOp) (types.Block, error)
- func (r *ChainReader) GetAccount(address util.String, opts ...types.CallOp) (types.Account, error)
- func (r *ChainReader) GetBlock(number uint64, opts ...types.CallOp) (types.Block, error)
- func (r *ChainReader) GetBlockByHash(hash util.Hash, opts ...types.CallOp) (types.Block, error)
- func (r *ChainReader) GetHeader(number uint64, opts ...types.CallOp) (types.Header, error)
- func (r *ChainReader) GetHeaderByHash(hash util.Hash, opts ...types.CallOp) (types.Header, error)
- func (r *ChainReader) GetID() util.String
- func (r *ChainReader) GetParent() types.ChainReaderFactory
- func (r *ChainReader) GetParentBlock() types.Block
- func (r *ChainReader) 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 (
// GenesisBlockFileName is the name of the file that contains the genesis block
GenesisBlockFileName = "genesis.json"
)
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.ChainReaderFactory
ChainReader creates a chain reader to read the 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.ChainReaderFactory
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.ChainReaderFactory)
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 block hashes 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. The genesis block must be added as the last hash if not already included.
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) IsMainChain ¶
func (b *Blockchain) IsMainChain(cr types.ChainReaderFactory) 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.ChainReaderFactory, 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) SetBestChain ¶
func (b *Blockchain) SetBestChain(c *Chain)
SetBestChain sets the current main chain
func (*Blockchain) SetCoinbase ¶
func (b *Blockchain) SetCoinbase(coinbase *crypto.Key)
SetCoinbase sets the coinbase key that is used to identify the current blockchain instance
func (*Blockchain) SetEventEmitter ¶
func (b *Blockchain) SetEventEmitter(ee *emitter.Emitter)
getBlockByHash finds and returns a block by hash only 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.ChainReaderFactory
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) GetMinedBlocks ¶
func (c *Chain) GetMinedBlocks(args *core.ArgGetMinedBlock, opts ...types.CallOp) ([]*core.MinedBlock, bool, error)
GetMinedBlocks fetches mined blocks. It allows the query to be adjusted using values in args.
- args.Limit forces only a limited number of results to be returned.
- args.CreatorPubKey filters out results that do not match a given public key.
- args.LastHash allows only records after the given hash to be returned. Useful for pagination.
It returns a slice of mined blocks and also a boolean that indicates whether there are more records.
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) PutMinedBlock ¶
PutMinedBlock records a block mined by the block creator
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 ChainReader ¶
type ChainReader struct {
// contains filtered or unexported fields
}
ChainReader provides read-only access to objects belonging to a single chain. It implements ChainReaderFactory interface
func NewChainReader ¶
func NewChainReader(ch *Chain) *ChainReader
NewChainReader creates a ChainReader object
func (*ChainReader) GetAccount ¶
GetAccount gets an account
func (*ChainReader) GetBlock ¶
GetBlock finds and returns a block associated with chainID. When 0 is passed, it should return the block with the highest number
func (*ChainReader) GetBlockByHash ¶
GetBlockByHash finds and returns a block associated with chainID.
func (*ChainReader) 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 (*ChainReader) GetHeaderByHash ¶
GetHeaderByHash finds and returns the header of a block matching hash
func (*ChainReader) GetParent ¶
func (r *ChainReader) GetParent() types.ChainReaderFactory
GetParent returns a chain reader to the parent chain. Returns nil if chain has no parent.
func (*ChainReader) GetParentBlock ¶
func (r *ChainReader) GetParentBlock() types.Block
GetParentBlock returns the parent block
func (*ChainReader) GetRoot ¶
func (r *ChainReader) GetRoot() types.Block
GetRoot fetches the root block of this chain. If the chain has more than one parents/ancestors, it will traverse the parents to return the root 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.
func (*WorldReader) GetAccount ¶
func (r *WorldReader) GetAccount(chain types.Chainer, address util.String, opts ...types.CallOp) (types.Account, error)
GetAccount gets an account by the given address. If chain is nil, it will check in the main chain. If chain is provided, it will check within the chain or traverse the chain's parent and ancestors to find the account.