consensus

package
v1.4.3 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2020 License: LGPL-3.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBlockNotExist            = errors.New("block not exist in local")
	ErrConfirmsEnough           = errors.New("confirms are enough")
	ErrNoNewConfirm             = errors.New("no useful confirm")
	ErrIgnoreBlock              = errors.New("block exist in local or the height of block is too small")
	ErrInvalidBlock             = errors.New("invalid block")
	ErrSaveBlock                = errors.New("save block to db error")
	ErrSaveAccount              = errors.New("save account error")
	ErrVerifyHeaderFailed       = errors.New("verify block's header error")
	ErrVerifyBlockFailed        = errors.New("verify block error")
	ErrSnapshotIsNil            = errors.New("local deputy nodes snapshot is nil")
	ErrInvalidConfirmSigner     = errors.New("invalid confirm signer")
	ErrInvalidSignedConfirmInfo = errors.New("invalid signed data of confirm info")
	ErrExistedConfirm           = errors.New("existed confirm info")
	ErrMineGenesis              = errors.New("can not mine genesis block")
	ErrNotDeputy                = errors.New("not a deputy address in specific height")
	ErrSmallerMineTime          = errors.New("the time of block must not be smaller than parent's")
	ErrSetStableBlockToDB       = errors.New("set stable block to db error")
	ErrSaveConfirmToDB          = errors.New("save confirm to db error")
	ErrNoTermReward             = errors.New("reward value has not been set")
)
View Source
var ErrNoHeadBlock = errors.New("head block is required")

Functions

func DivideSalary

func DivideSalary(totalSalary *big.Int, am *account.Manager, t *deputynode.TermRecord) []*deputynode.DeputySalary

func GetCorrectMiner added in v1.4.0

func GetCorrectMiner(parent *types.Header, mineTime int64, mineTimeout int64, dm *deputynode.Manager) (common.Address, error)

GetCorrectMiner get the correct miner to mine a block after parent block

func GetNextMineWindow added in v1.4.0

func GetNextMineWindow(nextHeight uint32, distance uint32, parentTime int64, currentTime int64, mineTimeout int64, dm *deputynode.Manager) (int64, int64)

GetNextMineWindow get next time window to mine block. The times are timestamps in millisecond

func IsConfirmEnough

func IsConfirmEnough(block *types.Block, dm *deputynode.Manager) bool

IsConfirmEnough test if the confirms in block is enough

func IsMinedByself added in v1.4.0

func IsMinedByself(block *types.Block) bool

func IsSigExist added in v1.4.0

func IsSigExist(sigs []types.SignData, sig types.SignData) bool

IsSigExist

func SignBlock added in v1.4.0

func SignBlock(blockHash common.Hash) ([]byte, error)

SignBlock sign a block hash by node key

Types

type BlockAssembler

type BlockAssembler struct {
	// contains filtered or unexported fields
}

Assembler seal block

func NewBlockAssembler

func NewBlockAssembler(am *account.Manager, dm *deputynode.Manager, txProcessor *transaction.TxProcessor, canLoader CandidateLoader) *BlockAssembler

func (*BlockAssembler) Finalize

func (ba *BlockAssembler) Finalize(height uint32) error

Finalize increases miners' balance and fix all account changes

func (*BlockAssembler) MineBlock

func (ba *BlockAssembler) MineBlock(header *types.Header, txs types.Transactions, applyTxTimeout int64) (*types.Block, types.Transactions, error)

MineBlock packages all products into a block

func (*BlockAssembler) PrepareHeader added in v1.4.0

func (ba *BlockAssembler) PrepareHeader(parentHeader *types.Header, extra string) (*types.Header, error)

func (*BlockAssembler) RunBlock

func (ba *BlockAssembler) RunBlock(block *types.Block) (*types.Block, error)

Seal packages all products into a block

func (*BlockAssembler) Seal

func (ba *BlockAssembler) Seal(header *types.Header, txProduct *account.TxsProduct, confirms []types.SignData) *types.Block

Seal packages all products into a block

type BlockLoader

type BlockLoader interface {
	IterateUnConfirms(fn func(*types.Block))
	GetUnConfirmByHeight(height uint32, leafBlockHash common.Hash) (*types.Block, error)
	GetBlockByHash(hash common.Hash) (*types.Block, error)
	// GetBlockByHeight returns stable blocks
	GetBlockByHeight(height uint32) (*types.Block, error)
}

BlockLoader is the interface of ChainDB

type BlockMaterial

type BlockMaterial struct {
	ParentHeader *types.Header
	Time         uint32 // new block time in header
	Extra        []byte
	Txs          types.Transactions
}

BlockMaterial is used for mine a new block

type CandidateLoader

type CandidateLoader interface {
	LoadTopCandidates(blockHash common.Hash) types.DeputyNodes
	LoadRefundCandidates(height uint32) ([]common.Address, error)
}

type Config

type Config struct {
	// Show every forks change
	LogForks bool
	// RewardManager is the owner of reward setting precompiled contract
	RewardManager common.Address
	ChainID       uint16
	MineTimeout   uint64
	MinerExtra    string // Extra data in mined block header. It is shorter than 256bytes
}

Config holds consensus options.

type Confirmer

type Confirmer struct {
	// contains filtered or unexported fields
}

Confirmer process the confirm logic

func NewConfirmer

func NewConfirmer(dm *deputynode.Manager, blockLoader BlockLoader, confirmStore confirmWriter, stableLoader StableBlockStore) *Confirmer

func (*Confirmer) BatchConfirmStable

func (c *Confirmer) BatchConfirmStable(startHeight, endHeight uint32) []*network.BlockConfirmData

BatchConfirmStable confirm and broadcast unsigned stable blocks one by one

func (*Confirmer) NeedConfirmList added in v1.4.0

func (c *Confirmer) NeedConfirmList(startHeight, endHeight uint32) []network.GetConfirmInfo

NeedConfirmList

func (*Confirmer) SaveConfirm

func (c *Confirmer) SaveConfirm(block *types.Block, sigList []types.SignData) (*types.Block, error)

SaveConfirm save a confirm to store, then return a new block

func (*Confirmer) SetLastSig

func (c *Confirmer) SetLastSig(block *types.Block)

SetLastSig

func (*Confirmer) TryConfirm

func (c *Confirmer) TryConfirm(block *types.Block) (types.SignData, bool)

TryConfirm try to sign and save a confirm into a received block

type DPoVP

type DPoVP struct {
	// contains filtered or unexported fields
}

DPoVP process the fork logic

func NewDPoVP added in v1.4.0

func NewDPoVP(config Config, db protocol.ChainDB, dm *deputynode.Manager, am *account.Manager, loader transaction.ParentBlockLoader, txPool *txpool.TxPool, txGuard *txpool.TxGuard) *DPoVP

func (*DPoVP) CurrentBlock

func (dp *DPoVP) CurrentBlock() *types.Block

func (*DPoVP) FetchRemoteConfirms added in v1.4.0

func (dp *DPoVP) FetchRemoteConfirms(startHeight, endHeight uint32, delay time.Duration)

FetchRemoteConfirms fetch confirms from remote peer after the [delay] seconds

func (*DPoVP) InsertBlock

func (dp *DPoVP) InsertBlock(rawBlock *types.Block) (*types.Block, error)

func (*DPoVP) InsertConfirms added in v1.4.0

func (dp *DPoVP) InsertConfirms(height uint32, blockHash common.Hash, sigList []types.SignData) error

func (*DPoVP) LoadRefundCandidates added in v1.4.0

func (dp *DPoVP) LoadRefundCandidates(height uint32) ([]common.Address, error)

LoadRefundCandidates get the address list of candidates who need to refund

func (*DPoVP) LoadTopCandidates

func (dp *DPoVP) LoadTopCandidates(blockHash common.Hash) types.DeputyNodes

SnapshotDeputyNodes get next epoch deputy nodes for snapshot block

func (*DPoVP) MineBlock

func (dp *DPoVP) MineBlock(txProcessTimeout int64) (*types.Block, error)

func (*DPoVP) StableBlock

func (dp *DPoVP) StableBlock() *types.Block

func (*DPoVP) SubscribeConfirm

func (dp *DPoVP) SubscribeConfirm(ch chan *network.BlockConfirmData) subscribe.Subscription

SubscribeConfirm subscribe the new confirm notification

func (*DPoVP) SubscribeCurrent

func (dp *DPoVP) SubscribeCurrent(ch chan *types.Block) subscribe.Subscription

SubscribeCurrent subscribe the current block update notification. The blocks may be not continuous

func (*DPoVP) SubscribeFetchConfirm

func (dp *DPoVP) SubscribeFetchConfirm(ch chan []network.GetConfirmInfo) subscribe.Subscription

SubscribeFetchConfirm subscribe fetch block confirms

func (*DPoVP) SubscribeStable

func (dp *DPoVP) SubscribeStable(ch chan *types.Block) subscribe.Subscription

SubscribeStable subscribe the stable block update notification

func (*DPoVP) TxGuard added in v1.4.0

func (dp *DPoVP) TxGuard() *txpool.TxGuard

func (*DPoVP) TxProcessor

func (dp *DPoVP) TxProcessor() *transaction.TxProcessor

func (*DPoVP) UpdateStable

func (dp *DPoVP) UpdateStable(block *types.Block) (bool, error)

UpdateStable check if the block can be stable. Then send notification and return true if the stable block changed

func (*DPoVP) VerifyAndSeal

func (dp *DPoVP) VerifyAndSeal(block *types.Block) (*types.Block, error)

VerifyAndSeal verify block then create a new block

type ForkManager

type ForkManager struct {
	// contains filtered or unexported fields
}

ForkManager process the fork logic

func NewForkManager

func NewForkManager(dm *deputynode.Manager, db BlockLoader, stable *types.Block) *ForkManager

func (*ForkManager) ChooseNewFork

func (fm *ForkManager) ChooseNewFork(stableBlock *types.Block) *types.Block

ChooseNewFork choose a fork and return the last block on the fork. It would return the current stable block if there is no unstable block

func (*ForkManager) GetHeadBlock

func (fm *ForkManager) GetHeadBlock() *types.Block

CurrentBlock get latest block on current fork

func (*ForkManager) SetHeadBlock

func (fm *ForkManager) SetHeadBlock(block *types.Block)

CurrentBlock get latest block on current fork

func (*ForkManager) UpdateFork added in v1.4.0

func (fm *ForkManager) UpdateFork(newBlock, stableBlock *types.Block) bool

UpdateFork check if the current fork can be update, or switch to a better fork. Return true if the current block changed

func (*ForkManager) UpdateForkForConfirm added in v1.4.0

func (fm *ForkManager) UpdateForkForConfirm(stableBlock *types.Block) bool

UpdateForkForConfirm switch to a better fork if the current fork is not exist. Return true if the new current block changed

type StableBlockStore added in v1.4.0

type StableBlockStore interface {
	LoadLatestBlock() (*types.Block, error)
	SetStableBlock(hash common.Hash) ([]*types.Block, error)
}

StableBlockStore is the interface of ChainDB

type StableManager

type StableManager struct {
	// contains filtered or unexported fields
}

StableManager process the fork logic

func NewStableManager

func NewStableManager(dm *deputynode.Manager, store StableBlockStore) *StableManager

func (*StableManager) StableBlock

func (sm *StableManager) StableBlock() *types.Block

StableBlock get latest stable block

func (*StableManager) UpdateStable

func (sm *StableManager) UpdateStable(block *types.Block) (bool, []*types.Block, error)

UpdateStable check if the block can be stable. Return true if the stable block changed, and return the pruned uncle blocks

type TxGuard added in v1.4.0

type TxGuard interface {
	ExistTxs(startBlockHash common.Hash, txs types.Transactions) bool
}

type Validator

type Validator struct {
	// contains filtered or unexported fields
}

Validator verify block

func NewValidator

func NewValidator(mineTimeout uint64, blockLoader BlockLoader, dm *deputynode.Manager, txGuard TxGuard, canLoader CandidateLoader) *Validator

func (*Validator) JudgeDeputy

func (v *Validator) JudgeDeputy(newBlock *types.Block) bool

JudgeDeputy check if the deputy node is evil by his new block

func (*Validator) VerifyAfterTxProcess

func (v *Validator) VerifyAfterTxProcess(block, computedBlock *types.Block) error

VerifyAfterTxProcess verify the block data which computed from transactions

func (*Validator) VerifyBeforeTxProcess

func (v *Validator) VerifyBeforeTxProcess(block *types.Block, chainId uint16) error

VerifyBeforeTxProcess verify the block data which has no relationship with the transaction processing result

func (*Validator) VerifyConfirmPacket

func (v *Validator) VerifyConfirmPacket(height uint32, blockHash common.Hash, sigList []types.SignData) ([]types.SignData, error)

VerifyConfirmPacket verify the confirm data in block body, return valid new confirms and last confirm verification error

func (*Validator) VerifyMiner added in v1.4.0

func (v *Validator) VerifyMiner(header *types.Header, parent *types.Header) error

VerifyMiner verify the miner slot of deputy node

func (*Validator) VerifyNewConfirms

func (v *Validator) VerifyNewConfirms(block *types.Block, sigList []types.SignData, dm *deputynode.Manager) ([]types.SignData, error)

verifyConfirm verify the confirm data, return valid new confirms and last confirm verification error

Jump to

Keyboard shortcuts

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