consensus

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

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

Functions

This section is empty.

Types

type ChainReadWriter

type ChainReadWriter interface {
	ChainReader
	ChainWriter
}

ChainReadWriter includes reader and writer

type ChainReader

type ChainReader interface {
	// Config retrieves the blockchain's chain configuration.
	Config() *configs.ChainConfig

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

	// 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
}

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

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

	// CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty
	// that a new block should have.
	CalcDifficulty(chain ChainReader, time uint64, parent *types.Header) *big.Int

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

	// GAPIs returns the GRPC APIs this consensus engine provides.
	GAPIs(chain ChainReader) []grpc.GApi
}

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, msg p2p.Msg) 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

	// Preprepared 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
	Preprepared

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

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

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

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