consensus

package
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2019 License: GPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package consensus implements different cpchain consensus engines.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnknownAncestor is returned when validating a block requires an ancestor
	// that is unknown.
	ErrUnknownAncestor = errors.New("unknown ancestor")

	// ErrPrunedAncestor is returned when validating a block requires an ancestor
	// that is known, but the state of which is not available.
	ErrPrunedAncestor = errors.New("pruned ancestor")

	// ErrFutureBlock is returned when a block's timestamp is in the future according
	// to the current node.
	ErrFutureBlock = errors.New("block in the future")

	// ErrInvalidTimestamp is returned when a block's timestamp is larger than parent's
	// timestamp + period + timeout.
	ErrInvalidTimestamp = errors.New("invalid timestamp")

	// ErrInvalidNumber is returned if a block's number doesn't equal it's parent's
	// plus one.
	ErrInvalidNumber = errors.New("invalid block number")

	// ErrNotEnoughSigs is returned if there is not enough signatures for a block.
	ErrNotEnoughSigs = errors.New("not enough signatures in block")

	// ErrUnauthorized is returned if a header is signed by a non-authorized entity.
	ErrUnauthorized = errors.New("unauthorized leader")

	// ErrNotInProposerCommittee is returned  if the account is not in proposer committee.
	ErrNotInProposerCommittee = errors.New("not in proposer committee")

	// ErrUnknownLbftState is returned if committee handler's state is unknown
	ErrUnknownLbftState = errors.New("unknown lbft state")

	// ErrInvalidSigners is returned if a block contains an invalid extra sigers bytes.
	ErrInvalidSigners = errors.New("invalid signer list on checkpoint block")

	// ErrInvalidNormalCoinbase is returned if a normal block's coinbase is 0x00.
	ErrInvalidNormalCoinbase = errors.New("invalid normal coinbase, it's 0x00")

	// ErrInvalidImpeachCoinbase is returned if an impeach block's coinbase is not 0x00.
	ErrInvalidImpeachCoinbase = errors.New("invalid impeach coinbase")

	// ErrInvalidImpeachStateRoot is returned if an impeach block's StateRoot is not equal to parents'.
	ErrInvalidImpeachStateRoot = errors.New("invalid impeach state root")

	// ErrInvalidImpeachTxsRoot is returned if an impeach block's TxsRoot is not valid.
	ErrInvalidImpeachTxsRoot = errors.New("invalid impeach txs root")

	// ErrInvalidImpeachReceiptsRoot is returned if an impeach block's ReceiptsRoot is not valid.
	ErrInvalidImpeachReceiptsRoot = errors.New("invalid impeach receipts root")

	// ErrInvalidImpeachLogsBloom is returned if an impeach block's LogsBloom is not valid.
	ErrInvalidImpeachLogsBloom = errors.New("invalid impeach LogsBloom")

	// ErrInvalidImpeachGasLimit is returned if an impeach block's GasLimit is not valid.
	ErrInvalidImpeachGasLimit = errors.New("invalid impeach GasLimit")

	// ErrInvalidImpeachGasUsed is returned if an impeach block's GasUsed is not valid.
	ErrInvalidImpeachGasUsed = errors.New("invalid impeach GasUsed")

	// ErrInvalidImpeachTimestamp is returned if an impeach block's Timestamp is not valid.
	ErrInvalidImpeachTimestamp = errors.New("invalid impeach Timestamp")

	// ErrInvalidImpeachExtra is returned if an impeach block's Extra is not valid.
	ErrInvalidImpeachExtra = errors.New("invalid impeach Extra")

	// ErrInvalidImpeachDporSnap is returned if an impeach block's DporSnap is not valid.
	ErrInvalidImpeachDporSnap = errors.New("invalid impeach DporSnap")

	// ErrInvalidImpeachTxs is returned if an impeach block contrains txs
	ErrInvalidImpeachTxs = errors.New("invalid impeach txs")

	// ErrorInvalidValidatorsList is returned if the validators list is invalid
	ErrorInvalidValidatorsList = errors.New("invalid validators list")
)

Functions

This section is empty.

Types

type ChainReadWriter

type ChainReadWriter interface {
	ChainReader
	ChainWriter
}

ChainReadWriter includes reader and writer

type ChainReader

type ChainReader interface {

	// ValidateBlockBody validates transactions in a block based on known states
	ValidateBlockBody(block *types.Block) error

	// Config retrieves the blockchain's chain configuration.
	Config() *configs.ChainConfig

	// CurrentHeader retrieves the current header from the local chain.
	CurrentHeader() *types.Header

	// CurrentBlock retrieves the current head block of the canonical chain. The
	// block is retrieved from the blockchain's internal cache.
	CurrentBlock() *types.Block

	// GetHeader retrieves a block header from the database by hash and number.
	GetHeader(hash common.Hash, number uint64) *types.Header

	// GetHeaderByNumber retrieves a block header from the database by number.
	GetHeaderByNumber(number uint64) *types.Header

	// GetHeaderByHash retrieves a block header from the database by its hash.
	GetHeaderByHash(hash common.Hash) *types.Header

	// GetBlock retrieves a block from the database by hash and number.
	GetBlock(hash common.Hash, number uint64) *types.Block

	// KnownHead returns hash and number of current head block (maybe not in local chain)
	KnownHead() (common.Hash, uint64)

	// SetKnownHead sets the known head block hash and number
	SetKnownHead(common.Hash, uint64)
}

ChainReader defines a small collection of methods needed to access the local blockchain.

type ChainWriter

type ChainWriter interface {

	// InsertChain inserts blocks to chain, returns fail index and error
	InsertChain(chain types.Blocks) (int, error)
}

ChainWriter reads and writes pending block to blockchain

type Engine

type Engine interface {
	// Author retrieves the cpchain address of the account that minted the given
	// block, which may be different from the header's coinbase if a consensus
	// engine is based on signatures.
	Author(header *types.Header) (common.Address, error)

	// VerifyHeader checks whether a header conforms to the consensus rules of a
	// given engine. Verifying the sigs may be done optionally here, or explicitly
	// via the VerifySeal method.
	// `refHeader' points to the original header, but `header' only points to a copy.
	VerifyHeader(chain ChainReader, header *types.Header, verifySigs bool, refHeader *types.Header) error

	// VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers
	// concurrently. 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).
	VerifyHeaders(chain ChainReader, headers []*types.Header, verifySigs []bool, refHeaders []*types.Header) (chan<- struct{}, <-chan error)

	// VerifySigs checks whether the crypto seal on a header is valid according to
	// the consensus rules of the given engine.
	VerifySigs(chain ChainReader, header *types.Header, refHeader *types.Header) error

	// CanMakeBlock returns true or false to indicate whether the consensus engine can make new block.
	CanMakeBlock(chain ChainReader, coinbase common.Address, parent *types.Header) bool

	// TryCampaign checks whether it is time to participate proposer campaign and try to participate if it is true
	TryCampaign()

	// PrepareBlock initializes the consensus fields of a block header according to the
	// rules of a particular engine. The changes are executed inline.
	PrepareBlock(chain ChainReader, header *types.Header) error

	// Finalize runs any post-transaction state modifications (e.g. block rewards)
	// and assembles the final block.
	// Note: The block header and state database might be updated to reflect any
	// consensus rules that happen at finalization (e.g. block rewards).
	Finalize(chain ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction,
		uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error)

	// Seal generates a new block for the given input block with the local miner's
	// seal place on top.
	Seal(chain ChainReader, block *types.Block, stop <-chan struct{}) (*types.Block, error)

	// APIs returns the RPC APIs this consensus engine provides.
	APIs(chain ChainReader) []rpc.API
}

Engine is an algorithm agnostic consensus engine.

type PbftEngine

type PbftEngine interface {
	Engine

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

	// Start starts engine
	Start() error

	// Stop stops all
	Stop() error

	Protocol() Protocol
}

PbftEngine is a pbft based consensus engine

type PbftStatus

type PbftStatus struct {
	State State
	Head  *types.Header
}

PbftStatus represents a state of a dpor replica

type Proposer

type Proposer interface {
	Engine
}

Proposer is used to produce a block in our PV(Producer-Validator) model.

type Protocol

type Protocol interface {
	Version() uint

	Length() uint64

	Available() bool

	AddPeer(version int, p *p2p.Peer, rw p2p.MsgReadWriter) (string, bool, bool, error)

	RemovePeer(id string)

	HandleMsg(id string, version int, p *p2p.Peer, rw p2p.MsgReadWriter, msg p2p.Msg) (string, error)

	NodeInfo() interface{}
}

Protocol represents interfaces a protocol can provide

type State

type State uint8

State is the pbft state

const (
	// Idle state is served as the first state in PBFT, ready to receive the proposed block
	Idle State = iota

	// Prepare state is the second state. The validator can enter this state after receiving proposed block (pre-prepare) message.
	// It is ready to send prepare messages
	Prepare

	// Commit state is the third state. The validator can enter it after collecting prepare certificate
	// It is about to broadcast commit messages
	Commit

	// ImpeachPrepare The validator transit to impeach pre-prepared state whenever the timer expires
	// It is about to broadcast impeach prepare messages
	ImpeachPrepare

	// ImpeachCommit Once a impeach prepare certificate is collected, a validator enters impeach prepared state
	ImpeachCommit

	// Validate state
	Validate
)

func (State) String added in v0.3.1

func (s State) String() string

type Validator

type Validator interface {

	// ValidateBlock validates a basic field excepts seal of a block.
	ValidateBlock(chain ChainReader, block *types.Block) error

	// SignHeader signs a header in given state.
	SignHeader(chain ChainReader, header *types.Header, state State) error
}

Validator is used to validate and sign a block

Directories

Path Synopsis
Package dpor implements the dpor consensus engine.
Package dpor implements the dpor consensus engine.
election
Package election implements dpor's election method.
Package election implements dpor's election method.
rpt

Jump to

Keyboard shortcuts

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