dpor

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2018 License: GPL-3.0 Imports: 29 Imported by: 0

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)

	// MaxSizeOfRecentSigners is the size of the RecentSigners.
	// TODO: @shiyc MaxSizeOfRecentSigners is about to be removed later
	//MaxSizeOfRecentValidators is the size of the RecentValidators
	//MaxSizeOfRecentProposers is the size of the RecentProposers
	MaxSizeOfRecentSigners    = 200
	MaxSizeOfRecentValidators = 200
	MaxSizeOfRecentProposers  = 200
)

Variables

View Source
var (
	DporDifficulty = big.NewInt(1) // Block difficulty for out-of-turn signatures
)
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")
)

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 IsCheckPoint

func IsCheckPoint(number uint64, termLen uint64, viewLen uint64) bool

IsCheckPoint returns if a given block number is in a checkpoint with given termLen and viewLen

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

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

func NewFakeDelayer

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

func NewFakeFailer

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

func NewFaker

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

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

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

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

func (*Dpor) CalcDifficulty

func (d *Dpor) 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 (*Dpor) Client

func (d *Dpor) Client() backend.ClientBackend

func (*Dpor) Coinbase

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

Coinbase returns current coinbase

func (*Dpor) CreateImpeachBlock

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

CreateImpeachBlock creates an impeachment block

func (*Dpor) CurrentSnap

func (d *Dpor) CurrentSnap() *DporSnapshot

CurrentSnap returns current dpor snapshot

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

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

func (d *Dpor) GAPIs(chain consensus.ChainReader) []grpc.GApi

GAPIs is APIs for dpor.

func (*Dpor) GetCalcRptInfo

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

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

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 signs a Mac

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 returns if already signed the 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) IsMiner

func (d *Dpor) IsMiner() bool

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

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

func (*Dpor) ProposersOfTerm

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

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)

func (*Dpor) SetClient

func (d *Dpor) SetClient(client backend.ClientBackend)

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

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

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

TermOf returns the term number of given block number

func (*Dpor) UpdateFinalSigsCache

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

Update the signature to final signature cache(two kinds of sigs, one for prepared, another for final)

func (*Dpor) UpdatePrepareSigsCache

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

Update the signature to prepare signature cache(two kinds of sigs, one for prepared, another for final)

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

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

func (*Dpor) ValidatorsOfTerm

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

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 if in preprepared state, verify basic fields if in prepared state, verify if enough prepare sigs if in committed state, verify if enough commit sigs

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

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
	// RecentSigners *lru.ARCCache    `json:"signers"`
	RecentSigners    map[uint64][]common.Address `json:"signers"`    // Set of recent signers
	RecentProposers  map[uint64][]common.Address `json:"proposers"`  // Set of recent proposers
	RecentValidators map[uint64][]common.Address `json:"validators"` // Set of recent validators
	Client           backend.ClientBackend       `json:"-"`
	// contains filtered or unexported fields
}

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

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

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

FutureSignerViewOf returns the future signer view with given signer address and block number TODO: @shiyc need to remove it later

func (*DporSnapshot) FutureSignersOf

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

FutureSignersOf returns future signers 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) 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) IsFutureSignerOf

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

IsFutureSignerOf returns if an address is a future signer in the given block number TODO: @shiyc need to remove it later

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

func (s *DporSnapshot) ProposerViewOf(proposer common.Address, number uint64) (int, error)

ProposerViewOf returns the proposer's view(turn) with given proposer's address and block number

func (*DporSnapshot) ProposersOf

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

func (*DporSnapshot) StartBlockNumberOfTerm

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

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

type Mode

type Mode uint

Mode defines the type a dpor engine makes.

const (
	NormalMode Mode = iota
	FakeMode
	DoNothingFakeMode
)

type Signatures

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

Signatures stores sigs in a block

func (*Signatures) GetSig

func (s *Signatures) GetSig(addr common.Address) (sig []byte, ok bool)

GetSig gets addr's sig

func (*Signatures) SetSig

func (s *Signatures) SetSig(addr common.Address, sig []byte)

SetSig sets addr's sig

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