OSDN Git Service

cdabdf9cb12d81e01765b1e77a36d6c5a7821e0f
[bytom/vapor.git] / mining / consensus / consensus.go
1 package consensus
2
3 import (
4         "math/big"
5
6         "github.com/vapor/common"
7         "github.com/vapor/protocol/bc"
8         "github.com/vapor/protocol/bc/types"
9 )
10
11 // ChainReader defines a small collection of methods needed to access the local
12 // blockchain during header and/or uncle verification.
13 type ChainReader interface {
14         // Config retrieves the blockchain's chain configuration.
15         //Config() *params.ChainConfig
16
17         // CurrentHeader retrieves the current header from the local chain.
18         CurrentHeader() *types.BlockHeader
19
20         // GetHeader retrieves a block header from the database by hash and number.
21         GetHeader(hash bc.Hash, number uint64) *types.BlockHeader
22
23         // GetHeaderByNumber retrieves a block header from the database by number.
24         GetHeaderByNumber(number uint64) *types.BlockHeader
25
26         // GetHeaderByHash retrieves a block header from the database by its hash.
27         GetHeaderByHash(hash bc.Hash) *types.BlockHeader
28
29         // GetBlock retrieves a block from the database by hash and number.
30         GetBlock(hash bc.Hash, number uint64) *types.Block
31 }
32
33 // Engine is an algorithm agnostic consensus engine.
34 type Engine interface {
35         // Author retrieves the Ethereum address of the account that minted the given
36         // block, which may be different from the header's coinbase if a consensus
37         // engine is based on signatures.
38         Author(header *types.BlockHeader) (common.Address, error)
39
40         // VerifyHeader checks whether a header conforms to the consensus rules of a
41         // given engine. Verifying the seal may be done optionally here, or explicitly
42         // via the VerifySeal method.
43         VerifyHeader(chain ChainReader, header *types.BlockHeader, seal bool) error
44
45         // VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers
46         // concurrently. The method returns a quit channel to abort the operations and
47         // a results channel to retrieve the async verifications (the order is that of
48         // the input slice).
49         VerifyHeaders(chain ChainReader, headers []*types.BlockHeader, seals []bool) (chan<- struct{}, <-chan error)
50
51         // VerifyUncles verifies that the given block's uncles conform to the consensus
52         // rules of a given engine.
53         VerifyUncles(chain ChainReader, block *types.Block) error
54
55         // VerifySeal checks whether the crypto seal on a header is valid according to
56         // the consensus rules of the given engine.
57         VerifySeal(chain ChainReader, header *types.BlockHeader) error
58
59         // Prepare initializes the consensus fields of a block header according to the
60         // rules of a particular engine. The changes are executed inline.
61         Prepare(chain ChainReader, header *types.BlockHeader) error
62
63         // Finalize runs any post-transaction state modifications (e.g. block rewards)
64         // and assembles the final block.
65         // Note: The block header and state database might be updated to reflect any
66         // consensus rules that happen at finalization (e.g. block rewards).
67         /*
68                 Finalize(chain ChainReader, header *types.BlockHeader , state *state.StateDB, txs []*types.Transaction,
69                         uncles []*types.BlockHeader , receipts []*types.Receipt) (*types.Block, error)
70         */
71
72         // Seal generates a new block for the given input block with the local miner's
73         // seal place on top.
74         Seal(chain ChainReader, block *types.Block, stop <-chan struct{}) (*types.Block, error)
75
76         // CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty
77         // that a new block should have.
78         CalcDifficulty(chain ChainReader, time uint64, parent *types.BlockHeader) *big.Int
79
80         // APIs returns the RPC APIs this consensus engine provides.
81         //APIs(chain ChainReader) []rpc.API
82 }