backend

package
v0.0.0-...-37c0745 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2019 License: GPL-3.0 Imports: 33 Imported by: 0

Documentation

Overview

Package backend implements the Delegated-Proof-of-Stake consensus engine.

Index

Constants

This section is empty.

Variables

View Source
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")

	ErrWaitForPrevBlock = errors.New("wait for last block arrived")
	ErrMintFutureBlock  = errors.New("mint the future block")
	//ErrMismatchSignerAndValidator = errors.New("mismatch block signer and validator")
	ErrInvalidBlockValidator = errors.New("invalid block validator")
	ErrInvalidMintBlockTime  = errors.New("invalid time to mint the block")
)

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.

View Source
var (
	MinDelegateBalance  = big.NewFloat(10 * 1e18)  //10QCT
	MinCandidateBalance = big.NewFloat(100 * 1e18) //100QCT
	UnlockInterval      = big.NewInt(10)           //10 Block

)
View Source
var (
	ErrMismatchSignerAndValidator = errors.New("mismatch block signer and validator")
	ErrNilBlockHeader             = errors.New("nil block header returned")
)

Functions

func CalcDifficulty

func CalcDifficulty(epoch *EpochContext, signer common.Address, 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 New

func New(config *bft.Config, privateKey *ecdsa.PrivateKey, db qctdb.Database) consensus.DBFT

New creates an Ethereum backend for BFT core engine.

func SeedHash

func SeedHash(block uint64) []byte

SeedHash is the seed to use for generating a verification cache and the mining dataset.

Types

type API

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

API is a user facing RPC API to dump BFT state

func (*API) Candidates

func (api *API) Candidates() map[common.Address]bool

Candidates returns the current candidates the node tries to uphold and vote on.

func (*API) Discard

func (api *API) Discard(address common.Address)

Discard drops a currently running candidate, stopping the validator from casting further votes (either for or against).

func (*API) GetCFDs

func (api *API) GetCFDs(number *rpc.BlockNumber) (map[string]map[string]*big.Int, error)

GetCFDs retrieves all candidates from their delegates at specified block

func (*API) GetDposCandidate

func (api *API) GetDposCandidate(addr common.Address, number *rpc.BlockNumber) (types.CandidateContext, error)

GetDposCandidates retrieves the list of the candidates at specified block

func (*API) GetDposCandidates

func (api *API) GetDposCandidates(maxcnt, number *rpc.BlockNumber) ([]types.CandidateContext, error)

GetCandidates retrieves maxcnt result of the list of the candidates at specified block

func (*API) GetDposValidators

func (api *API) GetDposValidators(number *rpc.BlockNumber) ([]common.Address, error)

copy from dpos.api GetDposValidators retrieves the list of the validators at specified block

func (*API) GetMintCnt

func (api *API) GetMintCnt(addr common.Address, number *rpc.BlockNumber) (int64, error)

GetMintCnt retrieves the mint cnt of the validator at specified block

func (*API) GetMintCnts

func (api *API) GetMintCnts(maxcnt, number *rpc.BlockNumber) ([]types.MintCntAddress, error)

GetMintCnt retrieves maxcnt result of the mint cnt of all validators at specified block

func (*API) GetSnapshot

func (api *API) GetSnapshot(number *rpc.BlockNumber) (*Snapshot, error)

GetSnapshot retrieves the state snapshot at a given block.

func (*API) GetSnapshotAtHash

func (api *API) GetSnapshotAtHash(hash common.Hash) (*Snapshot, error)

GetSnapshotAtHash retrieves the state snapshot at a given block.

func (*API) GetSortableAddresses

func (api *API) GetSortableAddresses(number *rpc.BlockNumber) (types.SortableAddresses, error)

func (*API) GetValidators

func (api *API) GetValidators(number *rpc.BlockNumber) ([]common.Address, error)

GetValidators retrieves the list of authorized validators at the specified block.

func (*API) GetValidatorsAtHash

func (api *API) GetValidatorsAtHash(hash common.Hash) ([]common.Address, error)

GetValidatorsAtHash retrieves the state snapshot at a given block.

func (*API) GetVote

func (api *API) GetVote(addr common.Address, number *rpc.BlockNumber) (map[string]types.VoteContext, error)

GetVote retrieves delegator to candidate at specified block

func (*API) GetVotes

func (api *API) GetVotes(number *rpc.BlockNumber) (map[string]types.VoteContext, error)

GetVotes retrieves all delegator to their candidate at specified block

func (*API) Propose

func (api *API) Propose(address common.Address, auth bool)

Propose injects a new authorization candidate that the validator will attempt to push through.

type DPOS

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

dpos is the Delegated-Proof-of-Stake consensus engine

func NewDPOS

func NewDPOS(config *params.DPOSConfig, db qctdb.Database) *DPOS

New creates a Delegated-Proof-of-Stake consensus engine with the initial signers set to the ones provided by the user.

func (*DPOS) APIs

func (c *DPOS) APIs(chain consensus.ChainReader) []rpc.API

APIs implements consensus.Engine, returning the user facing RPC API to allow controlling the signer voting.

func (*DPOS) Author

func (c *DPOS) Author(header *types.Header) (common.Address, error)

Author implements consensus.Engine, returning the Quickchain address recovered from the signature in the header's extra-data section.

func (*DPOS) Authorize

func (c *DPOS) Authorize(signer common.Address, signFn SignerFn)

Authorize injects a private key into the consensus engine to mint new blocks with.

func (*DPOS) CalcDifficulty

func (c *DPOS) CalcDifficulty(chain consensus.ChainReader, 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) DB

func (c *DPOS) DB() qctdb.Database

func (*DPOS) Finalize

func (c *DPOS) Finalize(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt, dposContext *types.DposContext) (*types.Block, error)

Finalize implements consensus.Engine, ensuring no uncles are set, nor block rewards given, and returns the final block.

func (*DPOS) JudgeTx

func (c *DPOS) JudgeTx(chain consensus.ChainReader, header *types.Header, tx *types.Transaction, from common.Address) error

Judge the create contract Transaction from a no authorized account for dpos engine.

func (*DPOS) Prepare

func (c *DPOS) Prepare(chain consensus.ChainReader, header *types.Header) error

Prepare implements consensus.Engine, preparing all the consensus fields of the header for running the transactions on top.

func (*DPOS) Seal

func (c *DPOS) Seal(chain consensus.ChainReader, block *types.Block, stop <-chan struct{}) (*types.Block, error)

Seal implements consensus.Engine, attempting to create a sealed block using the local signing credentials.

func (*DPOS) VerifyHeader

func (c *DPOS) VerifyHeader(chain consensus.ChainReader, header *types.Header, seal bool) error

VerifyHeader checks whether a header conforms to the consensus rules.

func (*DPOS) VerifyHeaders

func (c *DPOS) VerifyHeaders(chain consensus.ChainReader, headers []*types.Header, seals []bool) (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) VerifySeal

func (c *DPOS) VerifySeal(chain consensus.ChainReader, header *types.Header) error

VerifySeal implements consensus.Engine, checking whether the signature contained in the header satisfies the consensus protocol requirements.

func (*DPOS) VerifyUncles

func (c *DPOS) VerifyUncles(chain consensus.ChainReader, block *types.Block) error

VerifyUncles implements consensus.Engine, always returning an error for any uncles as this consensus mechanism doesn't permit uncles.

type EpochContext

type EpochContext struct {
	TimeStamp   int64
	DposContext *types.DposContext

	Number  uint64                      `json:"number"`  // Block number
	Hash    common.Hash                 `json:"hash"`    // Block hash
	Signers map[common.Address]struct{} `json:"signers"` // Set of authorized signers at this moment
	Recents map[uint64]common.Address   `json:"recents"` // Set of recent signers for spam protections
	// contains filtered or unexported fields
}

func (*EpochContext) CommitScores

func (ec *EpochContext) CommitScores(scores map[common.Address]*big.Int) error

CommitScores commit candidate score into candidate tree in every epoch elec process

type SignerFn

type SignerFn func(accounts.Account, []byte) ([]byte, error)

SignerFn is a signer callback function to request a hash to be signed by a backing account.

type Snapshot

type Snapshot struct {
	Epoch uint64 // The number of blocks after which to checkpoint and reset the pending votes

	Number uint64                   // Block number where the snapshot was created
	Hash   common.Hash              // Block hash where the snapshot was created
	Votes  []*Vote                  // List of votes cast in chronological order
	Tally  map[common.Address]Tally // Current vote tally to avoid recalculating
	ValSet bft.ValidatorSet         // Set of authorized validators at this moment

	DposContext *types.DposContext
	StateDB     *state.StateDB
}

Snapshot is the state of the authorization voting at a given point in time.

func (*Snapshot) CommitScores

func (ec *Snapshot) CommitScores(scores map[common.Address]*big.Int) error

CommitScores commit candidate score into candidate tree in every epoch elec process

func (*Snapshot) MarshalJSON

func (s *Snapshot) MarshalJSON() ([]byte, error)

Marshal to a json byte array

func (*Snapshot) UnmarshalJSON

func (s *Snapshot) UnmarshalJSON(b []byte) error

Unmarshal from a json byte array

type Tally

type Tally struct {
	Authorize bool `json:"authorize"` // Whether the vote it about authorizing or kicking someone
	Votes     int  `json:"votes"`     // Number of votes until now wanting to pass the proposal
}

Tally is a simple vote tally to keep the current score of votes. Votes that go against the proposal aren't counted since it's equivalent to not voting.

type Vote

type Vote struct {
	Validator common.Address `json:"validator"` // Authorized validator that cast this vote
	Block     uint64         `json:"block"`     // Block number the vote was cast in (expire old votes)
	Address   common.Address `json:"address"`   // Account being voted on to change its authorization
	Authorize bool           `json:"authorize"` // Whether to authorize or deauthorize the voted account
}

Vote represents a single vote that an authorized validator made to modify the list of authorizations.

Jump to

Keyboard shortcuts

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