dpor

package
v0.5.3-beta.1 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2021 License: GPL-3.0 Imports: 31 Imported by: 10

Documentation

Overview

Package dpor implements the dpor consensus engine.

Index

Constants

View Source
const (

	// TermDistBetweenElectionAndMining is the the term gap between election and mining.
	TermDistBetweenElectionAndMining = 2 // TermDistBetweenElectionAndMining = effective term - current term(last block)

	//MaxSizeOfRecentValidators is the size of the RecentValidators
	MaxSizeOfRecentValidators = 200

	//MaxSizeOfRecentProposers is the size of the RecentProposers
	MaxSizeOfRecentProposers = 200
)

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

	// ErrInvalidGasLimit is returned if the gasLimit of a block is invalid
	ErrInvalidGasLimit = errors.New("invalid gas limit for 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.

Functions

This section is empty.

Types

type API

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

API is a user facing RPC API to allow controlling the signer and voting mechanisms of the proof-of-authority scheme.

func (*API) GetProposers

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

GetProposers retrieves the Proposers at a given block.

func (*API) GetRNodes

func (api *API) GetRNodes() ([]common.Address, error)

GetRNodes retrieves current RNodes.

func (*API) GetSnapshot

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

GetSnapshot retrieves the state Snapshot at a given block.

func (*API) GetSnapshotAtHash

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

GetSnapshotAtHash retrieves the state Snapshot at a given block.

func (*API) GetValidators

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

GetValidators retrieves the Validators at a given block.

type BroadcastBlockFn

type BroadcastBlockFn func(block *types.Block, prop bool)

BroadcastBlockFn broadcasts a block to normal peers(not pbft replicas)

type Dpor

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

Dpor is the proof-of-reputation consensus engine proposed to support the cpchain testnet.

func New

func New(config *configs.DporConfig, db database.Database) *Dpor

New creates a Dpor proof-of-reputation consensus engine with the initial signers set to the ones provided by the user.

func NewDoNothingFaker

func NewDoNothingFaker(config *configs.DporConfig, db database.Database) *Dpor

NewDoNothingFaker creates a new fake dpor, do nothing when verifying blocks

func NewFakeDelayer

func NewFakeDelayer(config *configs.DporConfig, db database.Database, delay time.Duration) *Dpor

NewFakeDelayer creates a new fake dpor, delays when verifying blocks

func NewFakeFailer

func NewFakeFailer(config *configs.DporConfig, db database.Database, fail uint64) *Dpor

NewFakeFailer creates a new fake dpor, always fails when verifying blocks

func NewFaker

func NewFaker(config *configs.DporConfig, db database.Database) *Dpor

NewFaker creates a new fake dpor

func NewPbftFaker

func NewPbftFaker(config *configs.DporConfig, db database.Database) *Dpor

NewPbftFaker creates a new fake dpor to work with pbft, not in use now

func (*Dpor) APIs

func (d *Dpor) APIs(chain consensus.ChainReader) []rpc.API

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

func (*Dpor) Author

func (d *Dpor) Author(header *types.Header) (common.Address, error)

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

func (*Dpor) Authorize

func (d *Dpor) Authorize(signer common.Address, signFn backend.SignFn)

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

func (*Dpor) BlockDelay

func (d *Dpor) BlockDelay() time.Duration

BlockDelay returns max delay of preprepare block propagation

func (*Dpor) BroadcastBlock

func (d *Dpor) BroadcastBlock(block *types.Block, prop bool)

BroadcastBlock broadcasts a block to normal peers(not pbft replicas)

func (*Dpor) CanMakeBlock

func (d *Dpor) CanMakeBlock(chain consensus.ChainReader, coinbase common.Address, parent *types.Header) bool

CanMakeBlock checks if the given coinbase is ready to propose a block

func (*Dpor) Coinbase

func (d *Dpor) Coinbase() common.Address

Coinbase returns current coinbase

func (*Dpor) CreateFailbackImpeachBlocks

func (d *Dpor) CreateFailbackImpeachBlocks() (firstImpeachment *types.Block, secondImpeachment *types.Block, err error)

CreateFailbackImpeachBlocks creates impeachment blocks with failback timestamps

func (*Dpor) CreateImpeachBlock

func (d *Dpor) CreateImpeachBlock() (*types.Block, error)

CreateImpeachBlock creates an impeachment block

func (*Dpor) CreateImpeachBlockAt

func (d *Dpor) CreateImpeachBlockAt(parentHeader *types.Header) (*types.Block, error)

CreateImpeachBlockAt creates an impeachment block

func (*Dpor) CurrentSnap

func (d *Dpor) CurrentSnap() *DporSnapshot

CurrentSnap returns current dpor snapshot

func (*Dpor) ECRecoverProposer

func (d *Dpor) ECRecoverProposer(header *types.Header) (common.Address, error)

ECRecoverProposer recovers a proposer address from the seal of given header

func (*Dpor) ECRecoverSigs

func (d *Dpor) ECRecoverSigs(header *types.Header, state consensus.State) ([]common.Address, []types.DporSignature, error)

ECRecoverSigs recovers signer address and corresponding signature, it ignores empty signature and return empty addresses if one of the sigs are illegal TODO: refactor this, return a map[common.Address]dpor.Signature

func (*Dpor) Faulty

func (d *Dpor) Faulty() uint64

Faulty returns the number of faulty nodes

func (*Dpor) Finalize

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

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

func (*Dpor) FutureTermOf

func (d *Dpor) FutureTermOf(number uint64) uint64

FutureTermOf returns the future term number of given block number

func (*Dpor) GetBlockFromChain

func (d *Dpor) GetBlockFromChain(hash common.Hash, number uint64) *types.Block

GetBlockFromChain implements DporService.GetBlockFromChain

func (*Dpor) GetBlockReward

func (d *Dpor) GetBlockReward(blockNum uint64) *big.Int

GetBlockReward returns block reward according to block number

func (*Dpor) GetCalcRptInfo

func (d *Dpor) GetCalcRptInfo(address common.Address, addresses []common.Address, blockNum uint64) int64

GetCalcRptInfo get the rpt value of an address at specific block number

func (*Dpor) GetCandidateBackend

func (d *Dpor) GetCandidateBackend() campaign.CandidateService

func (*Dpor) GetCurrentBlock

func (d *Dpor) GetCurrentBlock() *types.Block

GetCurrentBlock returns current block

func (*Dpor) GetMac

func (d *Dpor) GetMac() (mac string, sig []byte, err error)

GetMac composes a message authentication code and signs it

func (*Dpor) GetRNodes

func (d *Dpor) GetRNodes() ([]common.Address, error)

func (*Dpor) GetRptBackend

func (d *Dpor) GetRptBackend() rpt.RptService

func (*Dpor) HandleMinedBlock

func (d *Dpor) HandleMinedBlock(block *types.Block) error

HandleMinedBlock receives a block to add to handler's pending block channel

func (*Dpor) HasBlockInChain

func (d *Dpor) HasBlockInChain(hash common.Hash, number uint64) bool

HasBlockInChain returns if a block is in local chain

func (*Dpor) IfSigned

func (d *Dpor) IfSigned(number uint64) (common.Hash, bool)

IfSigned checks if already signed a block

func (*Dpor) ImpeachTimeout

func (d *Dpor) ImpeachTimeout() time.Duration

ImpeachTimeout returns impeach time out

func (*Dpor) InsertChain

func (d *Dpor) InsertChain(block *types.Block) error

InsertChain inserts a block to chain

func (*Dpor) IsCurrentOrFutureProposer

func (d *Dpor) IsCurrentOrFutureProposer(address common.Address) bool

IsCurrentOrFutureProposer checks if an address is a proposer in the period between current term and future term

func (*Dpor) IsDefaultProposer

func (d *Dpor) IsDefaultProposer(address common.Address) bool

IsDefaultProposer returns if the given address is a default proposer

func (*Dpor) IsMiner

func (d *Dpor) IsMiner() bool

IsMiner returns if local coinbase is a miner(proposer or validator)

func (*Dpor) IsProposerOf

func (d *Dpor) IsProposerOf(addr common.Address, number uint64) (bool, error)

IsProposerOf returns whether the given address is the proposer for the block number

func (*Dpor) IsToCampaign

func (d *Dpor) IsToCampaign() bool

IsToCampaign returns if it is time to campaign

func (*Dpor) IsValidator

func (d *Dpor) IsValidator() bool

IsValidator returns if the node is running as a validator

func (*Dpor) MarkAsSigned

func (d *Dpor) MarkAsSigned(number uint64, hash common.Hash) error

MarkAsSigned marks signed a hash as signed

func (*Dpor) Mode

func (d *Dpor) Mode() Mode

Mode returns dpor mode

func (*Dpor) PbftStatus

func (d *Dpor) PbftStatus() *consensus.PbftStatus

PbftStatus returns current state of dpor

func (*Dpor) PeerInfos

func (d *Dpor) PeerInfos() ([]*backend.PeerInfo, error)

func (*Dpor) Period

func (d *Dpor) Period() time.Duration

Period returns period of block generation

func (*Dpor) PrepareBlock

func (d *Dpor) PrepareBlock(chain consensus.ChainReader, header *types.Header) error

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

func (*Dpor) ProposerOf

func (d *Dpor) ProposerOf(number uint64) (common.Address, error)

ProposerOf returns the proposer of the specified block number by rpt and election calculation

func (*Dpor) ProposersOf

func (d *Dpor) ProposersOf(number uint64) ([]common.Address, error)

ProposersOf returns proposers of given block number

func (*Dpor) ProposersOfTerm

func (d *Dpor) ProposersOfTerm(term uint64) ([]common.Address, error)

ProposersOfTerm returns proposers of given term TODO: same as above

func (*Dpor) Protocol

func (d *Dpor) Protocol() consensus.Protocol

Protocol returns Dpor p2p protocol

func (*Dpor) Seal

func (d *Dpor) 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. NB please populate the correct field values. we are now removing some fields such as nonce.

func (*Dpor) SetAsMiner

func (d *Dpor) SetAsMiner(isMiner bool)

SetAsMiner sets local coinbase as a miner

func (*Dpor) SetAsValidator

func (d *Dpor) SetAsValidator(isValidator bool)

SetAsValidator sets the consensus engine working as a validator

func (*Dpor) SetCampaignBackend

func (d *Dpor) SetCampaignBackend(campaignContract common.Address, backend backend.ClientBackend)

func (*Dpor) SetChain

func (d *Dpor) SetChain(blockchain consensus.ChainReadWriter)

SetChain is called by test file to assign the value of Dpor.chain, as well as DPor.currentSnapshot

func (*Dpor) SetChainConfig

func (d *Dpor) SetChainConfig(blockchain consensus.ChainReadWriter)

Set chainconfig is called for unittest

func (*Dpor) SetConfig

func (d *Dpor) SetConfig(conf *configs.DporConfig)

func (*Dpor) SetCurrentSnap

func (d *Dpor) SetCurrentSnap(snap *DporSnapshot)

SetCurrentSnap sets current dpor snapshot

func (*Dpor) SetHandler

func (d *Dpor) SetHandler(handler *backend.Handler) error

SetHandler sets dpor.handler

func (*Dpor) SetRNodeBackend

func (d *Dpor) SetRNodeBackend(rNodeContract common.Address, backend backend.ClientBackend)

func (*Dpor) SetRptBackend

func (d *Dpor) SetRptBackend(rptContract common.Address, backend backend.ClientBackend)

func (*Dpor) SetToCampaign

func (d *Dpor) SetToCampaign(isToCampaign bool)

SetToCampaign sets isToCampaign as true

func (*Dpor) SetupAdmission

func (d *Dpor) SetupAdmission(ac admission.ApiBackend)

SetupAdmission setups admission backend

func (*Dpor) SetupAsValidator

func (d *Dpor) SetupAsValidator(blockchain consensus.ChainReadWriter, server *p2p.Server, pmBroadcastBlockFn BroadcastBlockFn, pmSyncFromPeerFn SyncFromPeerFn, pmSyncFromBestPeerFn SyncFromBestPeerFn)

func (*Dpor) SignHash

func (d *Dpor) SignHash(hash []byte) ([]byte, error)

SignHash signs a hash msg with dpor coinbase account

func (*Dpor) SignHeader

func (d *Dpor) SignHeader(header *types.Header, state consensus.State) error

SignHeader signs the header and adds all known sigs to header

func (*Dpor) StartMining

func (d *Dpor) StartMining(blockchain consensus.ChainReadWriter, server *p2p.Server, pmBroadcastBlockFn BroadcastBlockFn, pmSyncFromPeerFn SyncFromPeerFn, pmSyncFromBestPeerFn SyncFromBestPeerFn)

StartMining starts to create a handler and start it.

func (*Dpor) State

func (d *Dpor) State() consensus.State

State returns current pbft phrase, one of (PrePrepare, Prepare, Commit).

func (*Dpor) Status

func (d *Dpor) Status() *consensus.PbftStatus

Status returns a pbft replica's status

func (*Dpor) StatusUpdate

func (d *Dpor) StatusUpdate() error

StatusUpdate updates status of dpor

func (*Dpor) StopMining

func (d *Dpor) StopMining()

StopMining stops dpor engine

func (*Dpor) SyncFrom

func (d *Dpor) SyncFrom(p *p2p.Peer)

SyncFrom tries to sync blocks from given peer

func (*Dpor) Synchronize

func (d *Dpor) Synchronize()

Synchronize tries to sync blocks from best peer

func (*Dpor) TermLength

func (d *Dpor) TermLength() uint64

TermLength returns term length

func (*Dpor) TermOf

func (d *Dpor) TermOf(number uint64) uint64

TermOf returns the term number of given block number

func (*Dpor) TryCampaign

func (d *Dpor) TryCampaign()

TryCampaign tries to start campaign

func (*Dpor) UpdateFinalSigsCache

func (d *Dpor) UpdateFinalSigsCache(validator common.Address, hash common.Hash, sig types.DporSignature)

UpdateFinalSigsCache updates final(commit) signature of a validator for a block in cache

func (*Dpor) UpdatePrepareSigsCache

func (d *Dpor) UpdatePrepareSigsCache(validator common.Address, hash common.Hash, sig types.DporSignature)

UpdatePrepareSigsCache updates prepare signature of a validator for a block in cache

func (*Dpor) ValidateBlock

func (d *Dpor) ValidateBlock(block *types.Block, verifySigs bool, verifyProposers bool) error

ValidateBlock validates a basic field excepts seal of a block.

func (*Dpor) ValidatorsNum

func (d *Dpor) ValidatorsNum() uint64

ValidatorsNum returns number of validators

func (*Dpor) ValidatorsOf

func (d *Dpor) ValidatorsOf(number uint64) ([]common.Address, error)

ValidatorsOf returns validators of given block number

func (*Dpor) ValidatorsOfTerm

func (d *Dpor) ValidatorsOfTerm(term uint64) ([]common.Address, error)

ValidatorsOfTerm returns validators of given term TODO: this only returns validators known recently from cache, does not retrieve block from local chain to get needed information. maybe i'll add it later.

func (*Dpor) VerifyHeader

func (d *Dpor) VerifyHeader(chain consensus.ChainReader, header *types.Header, verifySigs bool, refHeader *types.Header) error

VerifyHeader checks whether a header conforms to the consensus rules.

func (*Dpor) VerifyHeaderWithState

func (d *Dpor) VerifyHeaderWithState(header *types.Header, state consensus.State) error

VerifyHeaderWithState verifies the given header TODO: review this!

func (*Dpor) VerifyHeaders

func (d *Dpor) VerifyHeaders(chain consensus.ChainReader, headers []*types.Header, verifySigs []bool, refHeaders []*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 (*Dpor) VerifyProposerOf

func (d *Dpor) VerifyProposerOf(signer common.Address, term uint64) (bool, error)

VerifyProposerOf verifies if an address is a proposer of given term

func (*Dpor) VerifySeal

func (d *Dpor) VerifySeal(chain consensus.ChainReader, header *types.Header, refHeader *types.Header) error

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

func (*Dpor) VerifySigs

func (d *Dpor) VerifySigs(chain consensus.ChainReader, header *types.Header, refHeader *types.Header) error

VerifySigs checks if header has enough signatures of validators.

func (*Dpor) VerifyValidatorOf

func (d *Dpor) VerifyValidatorOf(signer common.Address, term uint64) (bool, error)

VerifyValidatorOf verifies if an address is a validator of given term

func (*Dpor) ViewLength

func (d *Dpor) ViewLength() uint64

ViewLength returns view length

type DporSnapshot

type DporSnapshot struct {
	Mode             Mode                        `json:"mode"`
	Number           uint64                      `json:"number"`     // Block number where the Snapshot was created
	Hash             common.Hash                 `json:"hash"`       // Block hash where the Snapshot was created
	Candidates       []common.Address            `json:"candidates"` // Set of candidates read from campaign contract
	RecentProposers  map[uint64][]common.Address `json:"proposers"`  // Set of recent proposers
	RecentValidators map[uint64][]common.Address `json:"validators"` // Set of recent validators
	// contains filtered or unexported fields
}

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

func NewSnapshot

func NewSnapshot(config *configs.DporConfig, number uint64, hash common.Hash, proposers []common.Address,
	validators []common.Address, mode Mode) *DporSnapshot

func (*DporSnapshot) ApplyHeader

func (s *DporSnapshot) ApplyHeader(header *types.Header, ifUpdateCommittee bool, candidateService campaign.CandidateService, rptService rpt.RptService) error

func (*DporSnapshot) FutureProposerViewOf

func (s *DporSnapshot) FutureProposerViewOf(signer common.Address, number uint64) (int, error)

FutureProposerViewOf returns the future signer view with given signer address and block number

func (*DporSnapshot) FutureProposersOf

func (s *DporSnapshot) FutureProposersOf(number uint64) []common.Address

FutureProposersOf returns future proposers of given block number

func (*DporSnapshot) FutureTermOf

func (s *DporSnapshot) FutureTermOf(blockNum uint64) uint64

FutureTermOf returns future term idx with given block number

func (*DporSnapshot) FutureValidatorsOf

func (s *DporSnapshot) FutureValidatorsOf(number uint64) []common.Address

FutureValidatorsOf returns future validators of given block number

func (*DporSnapshot) GetRecentProposers

func (s *DporSnapshot) GetRecentProposers(term uint64) []common.Address

func (*DporSnapshot) InturnOf

func (s *DporSnapshot) InturnOf(number uint64, signer common.Address) bool

InturnOf returns if a signer at a given block height is in-turn or not

func (*DporSnapshot) IsFutureProposerOf

func (s *DporSnapshot) IsFutureProposerOf(proposer common.Address, number uint64) bool

IsFutureProposerOf returns if an address is a future proposer in the given block number

func (*DporSnapshot) IsProposerOf

func (s *DporSnapshot) IsProposerOf(signer common.Address, number uint64) (bool, error)

IsProposerOf returns if an address is a proposer in the given block number

func (*DporSnapshot) IsValidatorOf

func (s *DporSnapshot) IsValidatorOf(validator common.Address, number uint64) bool

IsValidatorOf returns if an address is a validator in the given block number

func (*DporSnapshot) ProposersOf

func (s *DporSnapshot) ProposersOf(number uint64) []common.Address

ProposersOf returns proposers of given block number

func (*DporSnapshot) StartBlockNumberOfTerm

func (s *DporSnapshot) StartBlockNumberOfTerm(term uint64) uint64

StartBlockNumberOfTerm returns the first block number of a term

func (*DporSnapshot) Term

func (s *DporSnapshot) Term() uint64

Term returns the term index of current block number, which is 0-based

func (*DporSnapshot) TermOf

func (s *DporSnapshot) TermOf(blockNum uint64) uint64

TermOf returns the term index of given block number

func (*DporSnapshot) UpdateCandidates

func (s *DporSnapshot) UpdateCandidates(candidateService campaign.CandidateService, seed int64) error

func (*DporSnapshot) UpdateProposers

func (s *DporSnapshot) UpdateProposers(rpts rpt.RptList, seed int64, rptService rpt.RptService)

func (*DporSnapshot) UpdateRpts

func (s *DporSnapshot) UpdateRpts(rptService rpt.RptService) (rpt.RptList, error)

func (*DporSnapshot) ValidatorViewOf

func (s *DporSnapshot) ValidatorViewOf(validator common.Address, number uint64) (int, error)

ValidatorViewOf returns validator's view with given validator's address and block number

func (*DporSnapshot) ValidatorsOf

func (s *DporSnapshot) ValidatorsOf(number uint64) []common.Address

ValidatorsOf returns validators of given block number

type Mode

type Mode uint

Mode defines the type a dpor engine makes.

const (
	NormalMode Mode = iota
	FakeMode
	DoNothingFakeMode
	PbftFakeMode
)

DporMode

type SyncFromBestPeerFn

type SyncFromBestPeerFn func()

SyncFromBestPeerFn tries to sync blocks from best peer

type SyncFromPeerFn

type SyncFromPeerFn func(p *p2p.Peer)

SyncFromPeerFn tries to sync blocks from given peer

Directories

Path Synopsis
Package election implements dpor's election method.
Package election implements dpor's election method.

Jump to

Keyboard shortcuts

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