consensus

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2019 License: LGPL-3.0 Imports: 19 Imported by: 0

Documentation

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

	// ErrNotRemote is returned if GetWork be called in local mining
	// is not remote
	ErrNotRemote = errors.New("is not remote mining")
)
View Source
var (
	ErrInvalidDifficulty = errors.New("non-positive difficulty")
	ErrInvalidMixDigest  = errors.New("invalid mix digest")
	ErrInvalidPoW        = errors.New("invalid proof-of-work")
	ErrPreTime           = errors.New("parent time is smaller than time for CalculateDifficulty")
)

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 (
	ErrNoMiningWork = errors.New("no mining work available yet")
)

Functions

This section is empty.

Types

type ChainReader

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

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

	// GetHeader retrieves a block header from the database by hash and number.
	GetHeader(hash common.Hash) types.IHeader

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

	// GetBlock retrieves a block from the database by hash and number.
	GetBlock(hash common.Hash) types.IBlock
}

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

type CommonEngine

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

CommonEngine contains the common parts for consensus engines, where engine-specific logic is provided in func args as template pattern.

func NewCommonEngine

func NewCommonEngine(spec MiningSpec, diffCalc DifficultyCalculator, remote bool) *CommonEngine

NewCommonEngine returns the common engine mixin.

func (*CommonEngine) Author

func (c *CommonEngine) Author(header types.IHeader) (account.Address, error)

Author returns coinbase address.

func (*CommonEngine) CalcDifficulty

func (c *CommonEngine) CalcDifficulty(chain ChainReader, time uint64, parent types.IHeader) (*big.Int, error)

CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty that a new block should have.

func (*CommonEngine) Close

func (c *CommonEngine) Close() error

func (*CommonEngine) FindNonce

func (c *CommonEngine) FindNonce(
	work MiningWork,
	results chan<- MiningResult,
	stop <-chan struct{},
) error

FindNonce finds the desired nonce and mixhash for a given block header.

func (*CommonEngine) GetWork

func (c *CommonEngine) GetWork() (*MiningWork, error)

func (*CommonEngine) Name

func (c *CommonEngine) Name() string

Name returns the consensus engine's name.

func (*CommonEngine) Seal

func (c *CommonEngine) Seal(
	chain ChainReader,
	block types.IBlock,
	results chan<- types.IBlock,
	stop <-chan struct{}) error

Seal generates a new block for the given input block with the local miner's seal place on top.

func (*CommonEngine) SetThreads

func (c *CommonEngine) SetThreads(threads int)

func (*CommonEngine) SetWork

func (c *CommonEngine) SetWork(block types.IBlock, results chan<- types.IBlock)

func (*CommonEngine) SubmitWork

func (c *CommonEngine) SubmitWork(nonce uint64, hash, digest common.Hash) bool

func (*CommonEngine) VerifyHeader

func (c *CommonEngine) VerifyHeader(
	chain ChainReader,
	header types.IHeader,
	seal bool,
) error

VerifyHeader checks whether a header conforms to the consensus rules.

func (*CommonEngine) VerifyHeaders

func (c *CommonEngine) VerifyHeaders(
	chain ChainReader,
	headers []types.IHeader,
	seals []bool,
) (chan<- struct{}, <-chan 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).

func (*CommonEngine) VerifySeal

func (c *CommonEngine) VerifySeal(chain ChainReader, header types.IHeader, adjustedDiff *big.Int) error

VerifySeal checks whether the crypto seal on a header is valid according to the consensus rules of the given engine.

type DifficultyCalculator

type DifficultyCalculator interface {
	CalculateDifficulty(parent types.IHeader, time uint64) (*big.Int, error)
}

type Engine

type Engine interface {
	// Author retrieves the 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.IHeader) (account.Address, error)

	// VerifyHeader checks whether a header conforms to the consensus rules of a
	// given engine. Verifying the seal may be done optionally here, or explicitly
	// via the VerifySeal method.
	VerifyHeader(chain ChainReader, header types.IHeader, seal bool) 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.IHeader, seals []bool) (chan<- struct{}, <-chan error)

	// VerifySeal checks whether the crypto seal on a header is valid according to
	// the consensus rules of the given engine.
	VerifySeal(chain ChainReader, header types.IHeader, adjustedDiff *big.Int) error

	// Prepare initializes the consensus fields of a block header according to the
	// rules of a particular engine. The changes are executed inline.
	Prepare(chain ChainReader, header types.IHeader) 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.IHeader, state *state.StateDB, txs []*types.Transaction,
		receipts []*types.Receipt) (types.IBlock, error)

	// Seal generates a new sealing request for the given input block and pushes
	// the result into the given channel.
	//
	// Note, the method returns immediately and will send the result async. More
	// than one result may also be returned depending on the consensus algorithm.
	Seal(chain ChainReader, block types.IBlock, results chan<- types.IBlock, stop <-chan struct{}) error

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

	GetWork() (*MiningWork, error)

	SubmitWork(nonce uint64, hash, digest common.Hash) bool

	SetThreads(threads int)

	// Close terminates any background threads maintained by the consensus engine.
	Close() error
}

Engine is an algorithm agnostic consensus engine.

type EthDifficultyCalculator

type EthDifficultyCalculator struct {
	AdjustmentCutoff  uint32
	AdjustmentFactor  uint32
	MinimumDifficulty *big.Int
}

func (*EthDifficultyCalculator) CalculateDifficulty

func (c *EthDifficultyCalculator) CalculateDifficulty(parent types.IHeader, time uint64) (*big.Int, error)

type FakeEngine

type FakeEngine struct {
	NumberToFail uint64
	Difficulty   *big.Int
	FakeDelay    time.Duration
	Err          error
}

func (*FakeEngine) Author

func (e *FakeEngine) Author(header types.IHeader) (account.Address, error)

Author retrieves the Ethereum 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.

func (*FakeEngine) CalcDifficulty

func (e *FakeEngine) CalcDifficulty(chain ChainReader, time uint64, parent types.IHeader) (*big.Int, error)

CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty that a new block should have.

func (*FakeEngine) Close

func (e *FakeEngine) Close() error

Close terminates any background threads maintained by the consensus engine.

func (*FakeEngine) Finalize

func (e *FakeEngine) Finalize(chain ChainReader, header types.IHeader, state *state.StateDB, txs []*types.Transaction,
	receipts []*types.Receipt) (types.IBlock, error)

func (*FakeEngine) GetWork

func (e *FakeEngine) GetWork() (*MiningWork, error)

func (*FakeEngine) Prepare

func (e *FakeEngine) Prepare(chain ChainReader, header types.IHeader) error

Prepare initializes the consensus fields of a block header according to the rules of a particular engine. The changes are executed inline.

func (*FakeEngine) Seal

func (e *FakeEngine) Seal(chain ChainReader, block types.IBlock, results chan<- types.IBlock, stop <-chan struct{}) error

Seal generates a new sealing request for the given input block and pushes the result into the given channel.

Note, the method returns immediately and will send the result async. More than one result may also be returned depending on the consensus algorithm.

func (*FakeEngine) SetThreads

func (e *FakeEngine) SetThreads(threads int)

func (*FakeEngine) SubmitWork

func (e *FakeEngine) SubmitWork(nonce uint64, hash, digest common.Hash) bool

func (*FakeEngine) VerifyHeader

func (e *FakeEngine) VerifyHeader(chain ChainReader, header types.IHeader, seal bool) error

VerifyHeader checks whether a header conforms to the consensus rules of a given engine. Verifying the seal may be done optionally here, or explicitly via the VerifySeal method.

func (*FakeEngine) VerifyHeaders

func (e *FakeEngine) VerifyHeaders(chain ChainReader, headers []types.IHeader, seals []bool) (chan<- struct{}, <-chan 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).

func (*FakeEngine) VerifySeal

func (e *FakeEngine) VerifySeal(chain ChainReader, header types.IHeader, adjustedDiff *big.Int) error

VerifySeal checks whether the crypto seal on a header is valid according to the consensus rules of the given engine.

type MiningResult

type MiningResult struct {
	Digest common.Hash
	Result []byte
	Nonce  uint64
}

MiningResult represents the found digest and result bytes.

type MiningSpec

type MiningSpec struct {
	Name       string
	HashAlgo   func(result *ShareCache) error
	VerifySeal func(chain ChainReader, header types.IHeader, adjustedDiff *big.Int) error
}

MiningSpec contains a PoW algo's basic info and hash algo

type MiningWork

type MiningWork struct {
	HeaderHash common.Hash
	Number     uint64
	Difficulty *big.Int
}

MiningWork represents the params of mining work.

type PoW

type PoW interface {
	Engine
	// Hashrate returns the current mining hashrate of a PoW consensus engine.
	FindNonce(work MiningWork, results chan<- MiningResult, stop <-chan struct{}) error
	Name() string
}

PoW is the quarkchain version of PoW consensus engine, with a conveninent method for remote miners.

type ShareCache

type ShareCache struct {
	Digest []byte
	Result []byte
	Height uint64
	Hash   []byte
	Nonce  uint64
	Seed   []byte
}

Directories

Path Synopsis
Modified from go-ethereum under GNU Lesser General Public License Modified from go-ethereum under GNU Lesser General Public License
Modified from go-ethereum under GNU Lesser General Public License Modified from go-ethereum under GNU Lesser General Public License

Jump to

Keyboard shortcuts

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