Documentation ¶
Index ¶
- Constants
- Variables
- func DeleteLargerHeightBlocks(bcStore store.BlockchainStore, largerHeight uint64, rp *recoveryPoint) error
- func OverwriteStaleBlocks(bcStore store.BlockchainStore, staleHash common.Hash, rp *recoveryPoint) error
- func ValidateBlockHeader(header *types.BlockHeader, engine consensus.Engine, ...) error
- type BlockIndex
- type BlockLeaves
- func (bf *BlockLeaves) Add(index *BlockIndex)
- func (bf *BlockLeaves) Count() int
- func (bf *BlockLeaves) GetBestBlockIndex() *BlockIndex
- func (bf *BlockLeaves) GetBlockIndexByHash(hash common.Hash) *BlockIndex
- func (bf *BlockLeaves) GetWorstBlockIndex() *BlockIndex
- func (bf *BlockLeaves) IsBestBlockIndex(index *BlockIndex) bool
- func (bf *BlockLeaves) PurgeAsync(bcStore store.BlockchainStore, callback func(error))
- func (bf *BlockLeaves) Remove(hash common.Hash)
- type Blockchain
- func (bc *Blockchain) AccountDB() database.Database
- func (bc *Blockchain) AddBlockLeaves(blockIndex *BlockIndex)
- func (bc *Blockchain) ApplyDebtWithoutVerify(statedb *state.Statedb, d *types.Debt, coinbase common.Address) error
- func (bc *Blockchain) ApplyTransaction(tx *types.Transaction, txIndex int, coinbase common.Address, ...) (*types.Receipt, error)
- func (bc *Blockchain) CurrentBlock() *types.Block
- func (bc *Blockchain) CurrentHeader() *types.BlockHeader
- func (bc *Blockchain) Genesis() *types.Block
- func (bc *Blockchain) GetBlockByHash(hash common.Hash) *types.Block
- func (bc *Blockchain) GetCurrentInfo() (*types.Block, *state.Statedb, error)
- func (bc *Blockchain) GetCurrentState() (*state.Statedb, error)
- func (bc *Blockchain) GetHeadRollbackEventManager() *event.EventManager
- func (bc *Blockchain) GetHeaderByHash(hash common.Hash) *types.BlockHeader
- func (bc *Blockchain) GetHeaderByHeight(height uint64) *types.BlockHeader
- func (bc *Blockchain) GetShardNumber() (uint, error)
- func (bc *Blockchain) GetState(root common.Hash) (*state.Statedb, error)
- func (bc *Blockchain) GetStateByRootAndBlockHash(root, blockHash common.Hash) (*state.Statedb, error)
- func (bc *Blockchain) GetStore() store.BlockchainStore
- func (bc *Blockchain) PutCurrentHeader(header *types.BlockHeader)
- func (bc *Blockchain) PutTd(td *big.Int)
- func (bc *Blockchain) RemoveBlockLeaves(hash common.Hash)
- func (bc *Blockchain) UpdateCurrentBlock(block *types.Block)
- func (bc *Blockchain) WriteBlock(block *types.Block, txPool *Pool) error
- func (bc *Blockchain) WriteHeader(*types.BlockHeader) error
- type CachedTxs
- type ConcurrentDebtMap
- type DebtPool
- func (dp *DebtPool) AddDebt(debt *types.Debt) error
- func (dp *DebtPool) AddDebtArray(debts []*types.Debt)
- func (dp *DebtPool) DoCheckingDebt()
- func (dp *DebtPool) DoMulCheckingDebt() error
- func (dp *DebtPool) DoMulCheckingDebtHandler(d *types.Debt) error
- func (dp *DebtPool) GetDebtByHash(hash common.Hash) *types.Debt
- func (dp *DebtPool) GetDebtCount(processing, pending bool) int
- func (dp *DebtPool) GetDebts(processing, pending bool) []*types.Debt
- func (dp *DebtPool) GetProcessableDebts(size int) ([]*types.Debt, int)
- func (dp *DebtPool) RemoveDebtByHash(hash common.Hash)
- type EventPool
- type Genesis
- type GenesisInfo
- type HeaderChain
- type Pool
- type TransactionPool
- func (pool *TransactionPool) AddTransaction(tx *types.Transaction) error
- func (pool *TransactionPool) GetPendingTxCount() int
- func (pool *TransactionPool) GetProcessableTransactions(size int) ([]*types.Transaction, int)
- func (pool *TransactionPool) GetTransaction(txHash common.Hash) *types.Transaction
- func (pool *TransactionPool) GetTransactions(processing, pending bool) []*types.Transaction
- func (pool *TransactionPool) GetTxCount() int
- func (pool *TransactionPool) RemoveTransaction(txHash common.Hash)
- type TransactionPoolConfig
Constants ¶
const ( // BlockTransactionNumberLimit block transaction number limit, 1000 simple transactions are about 152kb // If for block size as 100KB, it could contains about 5k transactions BlockTransactionNumberLimit = 5000 // BlockByteLimit is the limit of size in bytes BlockByteLimit = 1024 * 1024 )
const CachedBlocks = uint64(24000)
const MaxBlockHeightGap = 40
const PercentDelete = 20 // once CachedTxs reach max, 1/PercentDelete of capacity will be randomly deleted
Variables ¶
var ( // ErrBlockAlreadyExists is returned when inserted block already exists ErrBlockAlreadyExists = errors.New("block already exists") // ErrBlockStateHashMismatch is returned when the calculated account state hash of block // does not match the state root hash in block header. ErrBlockStateHashMismatch = errors.New("block state hash mismatch") // ErrBlockReceiptHashMismatch is returned when the calculated receipts hash of block // does not match the receipts root hash in block header. ErrBlockReceiptHashMismatch = errors.New("block receipts hash mismatch") // ErrBlockEmptyTxs is returned when writing a block with empty transactions. ErrBlockEmptyTxs = errors.New("empty transactions in block") // ErrBlockCreateTimeNull is returned when block create time is nil ErrBlockCreateTimeNull = errors.New("block must have create time") // ErrBlockCreateTimeInFuture is returned when block create time is ahead of 10 seconds of now ErrBlockCreateTimeInFuture = errors.New("future block. block time is ahead 10 seconds of now") // ErrBlockTooManyTxs is returned when block have too many txs ErrBlockTooManyTxs = errors.New("block have too many transactions") // ErrBlockExtraDataNotEmpty is returned when the block extra data is not empty. ErrBlockExtraDataNotEmpty = errors.New("block extra data is not empty") // ErrNotSupported is returned when unsupported method invoked. ErrNotSupported = errors.New("not supported function") )
var ( // ErrGenesisHashMismatch is returned when the genesis block hash between the store and memory mismatch. ErrGenesisHashMismatch = errors.New("genesis block hash mismatch") // ErrGenesisNotFound is returned when genesis block not found in the store. ErrGenesisNotFound = errors.New("genesis block not found") )
var CachedCapacity = CachedBlocks * 500
var DebtManagerPoolCapacity = 100000
DebtManagerPoolCapacity capacity for to-be-sent debt in real test. the memory usage for 100000 will be about 150MB
var DebtPoolCapacity = 100000
DebtPoolCapacity we need bigger capacity to hold more debt in real test. the memory usage for 100000 will be about 150MB
var ToConfirmedDebtCapacity = 100000
ToConfirmedDebtCapacity capacity for to confirmed debt map in real test. the memory usage for 100000 will be about 150MB
Functions ¶
func DeleteLargerHeightBlocks ¶
func DeleteLargerHeightBlocks(bcStore store.BlockchainStore, largerHeight uint64, rp *recoveryPoint) error
DeleteLargerHeightBlocks deletes the height-to-hash mappings with larger height in the canonical chain.
func OverwriteStaleBlocks ¶
func OverwriteStaleBlocks(bcStore store.BlockchainStore, staleHash common.Hash, rp *recoveryPoint) error
OverwriteStaleBlocks overwrites the stale canonical height-to-hash mappings.
func ValidateBlockHeader ¶
func ValidateBlockHeader(header *types.BlockHeader, engine consensus.Engine, bcStore store.BlockchainStore, chainReader consensus.ChainReader) error
ValidateBlockHeader validates the specified header.
Types ¶
type BlockIndex ¶
type BlockIndex struct {
// contains filtered or unexported fields
}
BlockIndex is the index of the block chain
func NewBlockIndex ¶
NewBlockIndex constructs and returns a BlockIndex instance
type BlockLeaves ¶
type BlockLeaves struct {
// contains filtered or unexported fields
}
BlockLeaves is the block leaves used for block forking Note that BlockLeaves is not thread safe
func NewBlockLeaves ¶
func NewBlockLeaves() *BlockLeaves
NewBlockLeaves constructs and returns a NewBlockLeaves instance
func (*BlockLeaves) Add ¶
func (bf *BlockLeaves) Add(index *BlockIndex)
Add adds the specified block index to the block leaves
func (*BlockLeaves) Count ¶
func (bf *BlockLeaves) Count() int
Count returns the number of the block indices in the block leaves
func (*BlockLeaves) GetBestBlockIndex ¶
func (bf *BlockLeaves) GetBestBlockIndex() *BlockIndex
GetBestBlockIndex gets the best block index in the block leaves
func (*BlockLeaves) GetBlockIndexByHash ¶
func (bf *BlockLeaves) GetBlockIndexByHash(hash common.Hash) *BlockIndex
GetBlockIndexByHash gets the block index with the specified hash in the block leaves
func (*BlockLeaves) GetWorstBlockIndex ¶
func (bf *BlockLeaves) GetWorstBlockIndex() *BlockIndex
GetWorstBlockIndex gets the worst block index in the block leaves.
func (*BlockLeaves) IsBestBlockIndex ¶
func (bf *BlockLeaves) IsBestBlockIndex(index *BlockIndex) bool
IsBestBlockIndex indicates whether the given block index is the best compared with all indices in the block leaves
func (*BlockLeaves) PurgeAsync ¶
func (bf *BlockLeaves) PurgeAsync(bcStore store.BlockchainStore, callback func(error))
PurgeAsync purges the worst chain in forking tree.
func (*BlockLeaves) Remove ¶
func (bf *BlockLeaves) Remove(hash common.Hash)
Remove removes the block index of the specified hash from the block leaves
type Blockchain ¶
type Blockchain struct {
// contains filtered or unexported fields
}
Blockchain represents the blockchain with a genesis block. The Blockchain manages blocks insertion, deletion, reorganizations and persistence with a given database. This is a thread safe structure. we must keep all of its parameters are thread safe too.
func NewBlockchain ¶
func NewBlockchain(bcStore store.BlockchainStore, accountStateDB database.Database, recoveryPointFile string, engine consensus.Engine, verifier types.DebtVerifier, startHeight int) (*Blockchain, error)
NewBlockchain returns an initialized blockchain with the given store and account state DB.
func NewTestBlockchain ¶
func NewTestBlockchain() *Blockchain
func NewTestBlockchainWithVerifier ¶
func NewTestBlockchainWithVerifier(verifier types.DebtVerifier) *Blockchain
func (*Blockchain) AccountDB ¶
func (bc *Blockchain) AccountDB() database.Database
AccountDB returns the account state database in blockchain.
func (*Blockchain) AddBlockLeaves ¶
func (bc *Blockchain) AddBlockLeaves(blockIndex *BlockIndex)
func (*Blockchain) ApplyDebtWithoutVerify ¶
func (bc *Blockchain) ApplyDebtWithoutVerify(statedb *state.Statedb, d *types.Debt, coinbase common.Address) error
ApplyDebtWithoutVerify applies a debt and update statedb.
func (*Blockchain) ApplyTransaction ¶
func (bc *Blockchain) ApplyTransaction(tx *types.Transaction, txIndex int, coinbase common.Address, statedb *state.Statedb, blockHeader *types.BlockHeader) (*types.Receipt, error)
ApplyTransaction applies a transaction, changes corresponding statedb and generates its receipt
func (*Blockchain) CurrentBlock ¶
func (bc *Blockchain) CurrentBlock() *types.Block
CurrentBlock returns the HEAD block of the blockchain.
func (*Blockchain) CurrentHeader ¶
func (bc *Blockchain) CurrentHeader() *types.BlockHeader
CurrentHeader returns the HEAD block header of the blockchain.
func (*Blockchain) Genesis ¶
func (bc *Blockchain) Genesis() *types.Block
Genesis returns the genesis block of blockchain.
func (*Blockchain) GetBlockByHash ¶
func (bc *Blockchain) GetBlockByHash(hash common.Hash) *types.Block
GetBlockByHash retrieves a block by hash.
func (*Blockchain) GetCurrentInfo ¶
GetCurrentInfo return the current block and current state info
func (*Blockchain) GetCurrentState ¶
func (bc *Blockchain) GetCurrentState() (*state.Statedb, error)
GetCurrentState returns the state DB of the current block.
func (*Blockchain) GetHeadRollbackEventManager ¶ added in v1.0.2
func (bc *Blockchain) GetHeadRollbackEventManager() *event.EventManager
func (*Blockchain) GetHeaderByHash ¶
func (bc *Blockchain) GetHeaderByHash(hash common.Hash) *types.BlockHeader
GetHeaderByHash retrieves a block header by hash.
func (*Blockchain) GetHeaderByHeight ¶
func (bc *Blockchain) GetHeaderByHeight(height uint64) *types.BlockHeader
GetHeaderByHeight retrieves a block header by height.
func (*Blockchain) GetShardNumber ¶
func (bc *Blockchain) GetShardNumber() (uint, error)
GetShardNumber returns the shard number of blockchain.
func (*Blockchain) GetStateByRootAndBlockHash ¶
func (bc *Blockchain) GetStateByRootAndBlockHash(root, blockHash common.Hash) (*state.Statedb, error)
GetStateByRootAndBlockHash will panic, since not supported
func (*Blockchain) GetStore ¶
func (bc *Blockchain) GetStore() store.BlockchainStore
GetStore returns the blockchain store instance.
func (*Blockchain) PutCurrentHeader ¶ added in v1.0.2
func (bc *Blockchain) PutCurrentHeader(header *types.BlockHeader)
The following functions are only supported in lightclient
func (*Blockchain) PutTd ¶ added in v1.0.2
func (bc *Blockchain) PutTd(td *big.Int)
func (*Blockchain) RemoveBlockLeaves ¶
func (bc *Blockchain) RemoveBlockLeaves(hash common.Hash)
func (*Blockchain) UpdateCurrentBlock ¶
func (bc *Blockchain) UpdateCurrentBlock(block *types.Block)
UpdateCurrentBlock updates the HEAD block of the blockchain.
func (*Blockchain) WriteBlock ¶
func (bc *Blockchain) WriteBlock(block *types.Block, txPool *Pool) error
WriteBlock writes the specified block to the blockchain store.
func (*Blockchain) WriteHeader ¶
func (bc *Blockchain) WriteHeader(*types.BlockHeader) error
WriteHeader writes the specified head to the blockchain store, only used in lightchain.
type CachedTxs ¶ added in v1.2.5
type CachedTxs struct {
// contains filtered or unexported fields
}
func NewCachedTxs ¶ added in v1.2.5
10 * 60 * 60s / 15(s) (block) * 500txs/block = 1.2M txs 500 txs = 10KB, so total 1.2M txs will take up 24MB size
type ConcurrentDebtMap ¶
type ConcurrentDebtMap struct {
// contains filtered or unexported fields
}
func NewConcurrentDebtMap ¶
func NewConcurrentDebtMap(capacity int) *ConcurrentDebtMap
type DebtPool ¶
type DebtPool struct { *Pool // contains filtered or unexported fields }
DebtPool debt pool
func NewDebtPool ¶
func NewDebtPool(chain blockchain, verifier types.DebtVerifier) *DebtPool
func (*DebtPool) AddDebtArray ¶
func (*DebtPool) DoCheckingDebt ¶
func (dp *DebtPool) DoCheckingDebt()
func (*DebtPool) DoMulCheckingDebt ¶
DoMulCheckingDebt use multiple threads to validate debts
func (*DebtPool) DoMulCheckingDebtHandler ¶
DoMulCheckingDebtHandler DoMulCheckingDebt handler
func (*DebtPool) GetDebtCount ¶
func (*DebtPool) GetProcessableDebts ¶
func (*DebtPool) RemoveDebtByHash ¶
type EventPool ¶
type EventPool struct {
// contains filtered or unexported fields
}
EventPool event pool
func NewEventPool ¶
func NewEventPool(capacity uint, mainChainStore store.BlockchainStore, chain blockchain, abi *listener.ContractEventABI) (*EventPool, error)
NewEventPool creates and returns an event pool.
type Genesis ¶
type Genesis struct {
// contains filtered or unexported fields
}
Genesis represents the genesis block in the blockchain.
func GetGenesis ¶
func GetGenesis(info *GenesisInfo) *Genesis
GetGenesis gets the genesis block according to accounts' balance
func (*Genesis) GetShardNumber ¶
GetShardNumber gets the shard number of genesis
func (*Genesis) InitializeAndValidate ¶
func (genesis *Genesis) InitializeAndValidate(bcStore store.BlockchainStore, accountStateDB database.Database) error
InitializeAndValidate writes the genesis block in the blockchain store if unavailable. Otherwise, check if the existing genesis block is valid in the blockchain store.
type GenesisInfo ¶
type GenesisInfo struct { // Accounts accounts info for genesis block used for test // map key is account address -> value is account balance Accounts map[common.Address]*big.Int `json:"accounts,omitempty"` // Difficult initial difficult for mining. Use bigger difficult as you can. Because block is chosen by total difficult Difficult int64 `json:"difficult"` // ShardNumber is the shard number of genesis block. ShardNumber uint `json:"shard"` // CreateTimestamp is the initial time of genesis CreateTimestamp *big.Int `json:"timestamp"` // Consensus consensus type Consensus types.ConsensusType `json:"consensus"` // Validators istanbul consensus validators Validators []common.Address `json:"validators"` // master account Masteraccount common.Address `json:"master"` // balance of the master account Balance *big.Int `json:"balance"` }
GenesisInfo genesis info for generating genesis block, it could be used for initializing account balance
func NewGenesisInfo ¶
func (*GenesisInfo) Hash ¶
func (info *GenesisInfo) Hash() common.Hash
Hash returns GenesisInfo hash
type HeaderChain ¶
type HeaderChain struct {
// contains filtered or unexported fields
}
HeaderChain represents the block header chain that is shared by the archive node and light node. This is a non-thread safe structure.
func NewHeaderChain ¶
func NewHeaderChain(bcStore store.BlockchainStore) (*HeaderChain, error)
NewHeaderChain returns a new instance of the HeaderChain structure.
func (*HeaderChain) WriteHeader ¶
func (hc *HeaderChain) WriteHeader(newHeader *types.BlockHeader) error
WriteHeader writes a new block header into the header chain. It requires the new header's parent header is the HEAD header in the chain.
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
Pool is a thread-safe container for block object received from the network or submitted locally. An object will be removed from the pool once included in a blockchain or pending time too long (> timeoutDuration).
func NewPool ¶
func NewPool(capacity int, chain blockchain, getObjectFromBlock getObjectFromBlockFunc, canRemove canRemoveFunc, log *log.SeeleLog, objectValidation objectValidationFunc, afterAdd afterAddFunc, cachedTxs *CachedTxs) *Pool
NewPool creates and returns a transaction pool.
func (*Pool) GetObject ¶
GetObject returns a transaction if it is contained in the pool and nil otherwise.
func (*Pool) HandleChainHeaderChanged ¶
HandleChainHeaderChanged reinjects txs into pool in case of blockchain forked.
func (*Pool) HandleChainReversed ¶
func (*Pool) SetLogLevel ¶
type TransactionPool ¶
type TransactionPool struct {
*Pool
}
TransactionPool is a thread-safe container for transactions received from the network or submitted locally. A transaction will be removed from the pool once included in a blockchain or pending time too long (> transactionTimeoutDuration).
func NewTransactionPool ¶
func NewTransactionPool(config TransactionPoolConfig, chain blockchain) *TransactionPool
NewTransactionPool creates and returns a transaction pool.
func (*TransactionPool) AddTransaction ¶
func (pool *TransactionPool) AddTransaction(tx *types.Transaction) error
AddTransaction adds a single transaction into the pool if it is valid and returns nil. Otherwise, return the error.
func (*TransactionPool) GetPendingTxCount ¶
func (pool *TransactionPool) GetPendingTxCount() int
GetPendingTxCount returns the total number of pending transactions in the transaction pool.
func (*TransactionPool) GetProcessableTransactions ¶
func (pool *TransactionPool) GetProcessableTransactions(size int) ([]*types.Transaction, int)
GetProcessableTransactions retrieves processable transactions from pool.
func (*TransactionPool) GetTransaction ¶
func (pool *TransactionPool) GetTransaction(txHash common.Hash) *types.Transaction
GetTransaction returns a transaction if it is contained in the pool and nil otherwise.
func (*TransactionPool) GetTransactions ¶
func (pool *TransactionPool) GetTransactions(processing, pending bool) []*types.Transaction
GetTransactions returns the transactions in the transaction pool.
func (*TransactionPool) GetTxCount ¶
func (pool *TransactionPool) GetTxCount() int
GetTxCount returns the total number of transactions in the transaction pool.
func (*TransactionPool) RemoveTransaction ¶
func (pool *TransactionPool) RemoveTransaction(txHash common.Hash)
RemoveTransaction removes transaction of specified transaction hash from pool
type TransactionPoolConfig ¶
type TransactionPoolConfig struct {
Capacity int // Maximum number of transactions in the pool.
}
TransactionPoolConfig is the configuration of the transaction pool.
func DefaultTxPoolConfig ¶
func DefaultTxPoolConfig() *TransactionPoolConfig
DefaultTxPoolConfig returns the default configuration of the transaction pool.