Documentation
¶
Index ¶
- Constants
- Variables
- func CalcDifficulty(snap *Snapshot, signer common.Address) *big.Int
- func DposRLP(header *types.Header, chainId *big.Int) []byte
- func FindAncientHeader(header *types.Header, ite uint64, chain consensus.ChainHeaderReader, ...) *types.Header
- func ParseValidators(validatorsBytes []byte) ([]common.Address, error)
- func SealHash(header *types.Header, chainId *big.Int) (hash common.Hash)
- type API
- func (api *API) GetSnapshot(number *rpc.BlockNumber) (*Snapshot, error)
- func (api *API) GetSnapshotAtHash(hash common.Hash) (*Snapshot, error)
- func (api *API) GetValidators(number *rpc.BlockNumber) ([]common.Address, error)
- func (api *API) GetValidatorsAtHash(hash common.Hash) ([]common.Address, error)
- func (api *API) Status() (*status, error)
- type Dpos
- func (p *Dpos) APIs(chain consensus.ChainHeaderReader) []rpc.API
- func (p *Dpos) Author(header *types.Header) (common.Address, error)
- func (p *Dpos) Authorize(val common.Address, signFn SignerFn, signTxFn SignerTxFn) bool
- func (p *Dpos) CalcDifficulty(chain consensus.ChainHeaderReader, time uint64, parent *types.Header) *big.Int
- func (p *Dpos) CanCreate() bool
- func (p *Dpos) CheckHasInTurn(chain consensus.ChainHeaderReader, coinbases []common.Address, ...) common.Address
- func (p *Dpos) Close() error
- func (p *Dpos) CreateChallengeFinish(validator common.Address, provider common.Address, seed uint64, ...) (*types.Transaction, error)
- func (p *Dpos) Delay(chain consensus.ChainReader, header *types.Header, leftOver *time.Duration) *time.Duration
- func (p *Dpos) EnoughDistance(chain consensus.ChainReader, header *types.Header) bool
- func (p *Dpos) Finalize(chain consensus.ChainHeaderReader, header *types.Header, state vm.StateDB, ...) error
- func (p *Dpos) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, ...) (b *types.Block, rs []*types.Receipt, err error)
- func (p *Dpos) IsLocalBlock(header *types.Header) bool
- func (p *Dpos) IsSysTransaction(tx *types.Transaction, header *types.Header) (bool, error)
- func (p *Dpos) IsSystemContract(to *common.Address) bool
- func (p *Dpos) IsSystemTransaction(tx *types.Transaction, header *types.Header) (bool, error)
- func (p *Dpos) NextInTurnValidator(chain consensus.ChainHeaderReader, header *types.Header) (common.Address, error)
- func (p *Dpos) PreHandle(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB) error
- func (p *Dpos) Prepare(chain consensus.ChainHeaderReader, header *types.Header) error
- func (p *Dpos) Seal(chain consensus.ChainHeaderReader, block *types.Block, ...) error
- func (p *Dpos) SealHash(header *types.Header) common.Hash
- func (p *Dpos) SetStateFn(fn StateFn)
- func (p *Dpos) SignRecently(chain consensus.ChainReader, parent *types.Header) (bool, error)
- func (p *Dpos) SignSeed(header *types.Header, seed uint64) ([]byte, error)
- func (p *Dpos) TryCreateChallenge(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB) (*types.Transaction, uint64, common.Address, error, int)
- func (p *Dpos) ValidateTx(tx *types.Transaction, header *types.Header, parentState *state.StateDB) error
- func (p *Dpos) VerifyHeader(chain consensus.ChainHeaderReader, header *types.Header) error
- func (p *Dpos) VerifyHeaders(chain consensus.ChainHeaderReader, headers []*types.Header) (chan<- struct{}, <-chan error)
- func (p *Dpos) VerifyRequests(header *types.Header, Requests [][]byte) error
- func (p *Dpos) VerifySeal(chain consensus.ChainReader, header *types.Header) error
- func (p *Dpos) VerifyUncles(chain consensus.ChainReader, block *types.Block) error
- type ProviderInfos
- type SignerFn
- type SignerTxFn
- type Snapshot
- type StateFn
- type VoteInfo
Constants ¶
const ( Running uint8 = iota Punish Pause Stop )
const ( NotStart uint8 = iota Create Success Fail )
const ( DirectionFrom blacklistDirection = iota DirectionTo DirectionBoth )
Variables ¶
var ( //ProviderFactoryAddr = common.Address{} StakeThreshold = new(big.Int).Mul(big.NewInt(1000), big.NewInt(params.Ether)) LuckyRate = big.NewInt(6) LuckyPorRate = big.NewInt(4) MaxStorage = big.NewInt(1) MaxMemory = big.NewInt(4) )
var ( // ErrInvalidTimestamp is returned if the timestamp of a block is lower than // the previous block's timestamp + the minimum block period. ErrInvalidTimestamp = errors.New("invalid timestamp") )
Various error messages to mark blocks invalid. These should be private to prevent engine specific errors from being referenced in the remainder of the codebase, inherently breaking if the engine is swapped out. Please put common error types into the consensus package.
Functions ¶
func CalcDifficulty ¶
CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty that a new block should have based on the previous blocks in the chain and the current signer.
func DposRLP ¶
DposRLP returns the rlp bytes which needs to be signed for the dpos sealing. The RLP to sign consists of the entire header apart from the 65 byte signature contained at the end of the extra data.
Note, the method requires the extra data to be at least 65 bytes, otherwise it panics. This is done to avoid accidentally using both forms (signature present or not), which could be abused to produce different hashes for the same header.
func FindAncientHeader ¶
Types ¶
type API ¶
type API struct {
// contains filtered or unexported fields
}
API is a user facing RPC API to allow controlling the validator and voting mechanisms of the proof-of-authority scheme.
func (*API) GetSnapshot ¶
func (api *API) GetSnapshot(number *rpc.BlockNumber) (*Snapshot, error)
GetSnapshot retrieves the state snapshot at a given block.
func (*API) GetSnapshotAtHash ¶
GetSnapshotAtHash retrieves the state snapshot at a given block.
func (*API) GetValidators ¶
GetValidators retrieves the list of validators at the specified block.
func (*API) GetValidatorsAtHash ¶
GetValidatorsAtHash retrieves the list of authorized validators at the specified block.
type Dpos ¶
type Dpos struct {
// contains filtered or unexported fields
}
Dpos is the consensus engine of BSC
func New ¶
func New( chainConfig *params.ChainConfig, db ethdb.Database, ethAPI *ethapi.BlockChainAPI, genesisHash common.Hash, ) *Dpos
New creates a Dpos consensus engine.
func (*Dpos) APIs ¶
func (p *Dpos) APIs(chain consensus.ChainHeaderReader) []rpc.API
APIs implements consensus.Engine, returning the user facing RPC API to query snapshot.
func (*Dpos) Authorize ¶
func (p *Dpos) decreaseMissedBlocksCounter(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB) error {
// method
method := "decreaseMissedBlocksCounter"
data, err := p.abi[systemcontract.PunishV1ContractName].Pack(method, new(big.Int).SetUint64(p.config.Epoch))
if err != nil {
log.Error("Can't pack data for decreaseMissedBlocksCounter", "error", err)
return err
}
// call contract
nonce := state.GetNonce(header.Coinbase)
msg := core.NewMessage(header.Coinbase, systemcontract.GetPunishAddr(header.Number, p.chainConfig), nonce, new(big.Int), math.MaxUint64, new(big.Int), data, nil, true)
if _, err := vmcaller.ExecuteMsg(msg, state, header, newChainContext(chain, p), p.chainConfig); err != nil {
log.Error("Can't decrease missed blocks counter for validator", "err", err)
return err
}
return nil
}
Authorize injects a private key into the consensus engine to mint new blocks with.
func (*Dpos) CalcDifficulty ¶
func (p *Dpos) CalcDifficulty(chain consensus.ChainHeaderReader, time uint64, parent *types.Header) *big.Int
CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty that a new block should have based on the previous blocks in the chain and the current signer.
func (*Dpos) CanCreate ¶
CanCreate determines where a given address can create a new contract.
This will queries the system Developers contract, by DIRECTLY to get the target slot value of the contract, it means that it's strongly relative to the layout of the Developers contract's state variables
func (*Dpos) CheckHasInTurn ¶
func (*Dpos) Close ¶
Close implements consensus.Engine. It's a noop for dpos as there are no background threads.
func (*Dpos) CreateChallengeFinish ¶
func (*Dpos) EnoughDistance ¶
func (*Dpos) FinalizeAndAssemble ¶
func (*Dpos) IsSysTransaction ¶
IsSysTransaction checks whether a specific transaction is a system transaction.
func (*Dpos) IsSystemTransaction ¶
func (*Dpos) NextInTurnValidator ¶ added in v1.0.4
func (p *Dpos) NextInTurnValidator(chain consensus.ChainHeaderReader, header *types.Header) (common.Address, error)
NextInTurnValidator return the next in-turn validator for header
func (*Dpos) Prepare ¶
Prepare implements consensus.Engine, preparing all the consensus fields of the header for running the transactions on top.
func (*Dpos) Seal ¶
func (p *Dpos) Seal(chain consensus.ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error
Seal implements consensus.Engine, attempting to create a sealed block using the local signing credentials.
func (*Dpos) SetStateFn ¶
SetStateFn sets the function to get state.
func (*Dpos) SignRecently ¶
func (*Dpos) TryCreateChallenge ¶
func (*Dpos) ValidateTx ¶
func (p *Dpos) ValidateTx(tx *types.Transaction, header *types.Header, parentState *state.StateDB) error
ValidateTx do a consensus-related validation on the given transaction at the given header and state. the parentState must be the state of the header's parent block.
func (*Dpos) VerifyHeader ¶
VerifyHeader checks whether a header conforms to the consensus rules.
func (*Dpos) VerifyHeaders ¶
func (p *Dpos) VerifyHeaders(chain consensus.ChainHeaderReader, headers []*types.Header) (chan<- struct{}, <-chan error)
VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers. The method returns a quit channel to abort the operations and a results channel to retrieve the async verifications (the order is that of the input slice).
func (*Dpos) VerifyRequests ¶ added in v1.0.4
func (*Dpos) VerifySeal ¶
VerifySeal implements consensus.Engine, checking whether the signature contained in the header satisfies the consensus protocol requirements.
func (*Dpos) VerifyUncles ¶
VerifyUncles implements consensus.Engine, always returning an error for any uncles as this consensus mechanism doesn't permit uncles.
type ProviderInfos ¶
type SignerTxFn ¶
type SignerTxFn func(accounts.Account, *types.Transaction, *big.Int) (*types.Transaction, error)
type Snapshot ¶
type Snapshot struct {
Number uint64 `json:"number"` // Block number where the snapshot was created
Hash common.Hash `json:"hash"` // Block hash where the snapshot was created
Validators map[common.Address]struct{} `json:"validators"` // Set of authorized validators at this moment
Recents map[uint64]common.Address `json:"recents"` // Set of recent validators for spam protections
RecentForkHashes map[uint64]string `json:"recent_fork_hashes"` // Set of recent forkHash
// contains filtered or unexported fields
}
Snapshot is the state of the validatorSet at a given point.