consensus.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // Package consensus implements different Ethereum consensus engines.
  17. package consensus
  18. import (
  19. "math/big"
  20. "time"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/core/state"
  23. "github.com/ethereum/go-ethereum/core/types"
  24. "github.com/ethereum/go-ethereum/params"
  25. "github.com/ethereum/go-ethereum/rpc"
  26. )
  27. var (
  28. SystemAddress = common.HexToAddress("0xffffFFFfFFffffffffffffffFfFFFfffFFFfFFfE")
  29. )
  30. // ChainHeaderReader defines a small collection of methods needed to access the local
  31. // blockchain during header verification.
  32. type ChainHeaderReader interface {
  33. // Config retrieves the blockchain's chain configuration.
  34. Config() *params.ChainConfig
  35. // CurrentHeader retrieves the current header from the local chain.
  36. CurrentHeader() *types.Header
  37. // GetHeader retrieves a block header from the database by hash and number.
  38. GetHeader(hash common.Hash, number uint64) *types.Header
  39. // GetHeaderByNumber retrieves a block header from the database by number.
  40. GetHeaderByNumber(number uint64) *types.Header
  41. // GetHeaderByHash retrieves a block header from the database by its hash.
  42. GetHeaderByHash(hash common.Hash) *types.Header
  43. // GetHighestVerifiedHeader retrieves the highest header verified.
  44. GetHighestVerifiedHeader() *types.Header
  45. }
  46. // ChainReader defines a small collection of methods needed to access the local
  47. // blockchain during header and/or uncle verification.
  48. type ChainReader interface {
  49. ChainHeaderReader
  50. // GetBlock retrieves a block from the database by hash and number.
  51. GetBlock(hash common.Hash, number uint64) *types.Block
  52. }
  53. // Engine is an algorithm agnostic consensus engine.
  54. type Engine interface {
  55. // Author retrieves the Ethereum address of the account that minted the given
  56. // block, which may be different from the header's coinbase if a consensus
  57. // engine is based on signatures.
  58. Author(header *types.Header) (common.Address, error)
  59. // VerifyHeader checks whether a header conforms to the consensus rules of a
  60. // given engine. Verifying the seal may be done optionally here, or explicitly
  61. // via the VerifySeal method.
  62. VerifyHeader(chain ChainHeaderReader, header *types.Header, seal bool) error
  63. // VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers
  64. // concurrently. The method returns a quit channel to abort the operations and
  65. // a results channel to retrieve the async verifications (the order is that of
  66. // the input slice).
  67. VerifyHeaders(chain ChainHeaderReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error)
  68. // VerifyUncles verifies that the given block's uncles conform to the consensus
  69. // rules of a given engine.
  70. VerifyUncles(chain ChainReader, block *types.Block) error
  71. // Prepare initializes the consensus fields of a block header according to the
  72. // rules of a particular engine. The changes are executed inline.
  73. Prepare(chain ChainHeaderReader, header *types.Header) error
  74. BeforePackTx(chain ChainHeaderReader, header *types.Header, state *state.StateDB, txs *[]*types.Transaction,
  75. uncles []*types.Header, receipts *[]*types.Receipt) error
  76. BeforeValidateTx(chain ChainHeaderReader, header *types.Header, state *state.StateDB, txs *[]*types.Transaction,
  77. uncles []*types.Header, receipts *[]*types.Receipt, systemTxs *[]*types.Transaction, usedGas *uint64) error
  78. // Finalize runs any post-transaction state modifications (e.g. block rewards)
  79. // but does not assemble the block.
  80. //
  81. // Note: The block header and state database might be updated to reflect any
  82. // consensus rules that happen at finalization (e.g. block rewards).
  83. Finalize(chain ChainHeaderReader, header *types.Header, state *state.StateDB, txs *[]*types.Transaction,
  84. uncles []*types.Header, receipts *[]*types.Receipt, systemTxs *[]*types.Transaction, usedGas *uint64) error
  85. // FinalizeAndAssemble runs any post-transaction state modifications (e.g. block
  86. // rewards) and assembles the final block.
  87. //
  88. // Note: The block header and state database might be updated to reflect any
  89. // consensus rules that happen at finalization (e.g. block rewards).
  90. FinalizeAndAssemble(chain ChainHeaderReader, header *types.Header, state *state.StateDB, txs []*types.Transaction,
  91. uncles []*types.Header, receipts []*types.Receipt) (*types.Block, []*types.Receipt, error)
  92. // Seal generates a new sealing request for the given input block and pushes
  93. // the result into the given channel.
  94. //
  95. // Note, the method returns immediately and will send the result async. More
  96. // than one result may also be returned depending on the consensus algorithm.
  97. Seal(chain ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error
  98. // SealHash returns the hash of a block prior to it being sealed.
  99. SealHash(header *types.Header) common.Hash
  100. // CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty
  101. // that a new block should have.
  102. CalcDifficulty(chain ChainHeaderReader, time uint64, parent *types.Header) *big.Int
  103. // APIs returns the RPC APIs this consensus engine provides.
  104. APIs(chain ChainHeaderReader) []rpc.API
  105. // Delay returns the max duration the miner can commit txs
  106. Delay(chain ChainReader, header *types.Header) *time.Duration
  107. // Close terminates any background threads maintained by the consensus engine.
  108. Close() error
  109. }
  110. // PoW is a consensus engine based on proof-of-work.
  111. type PoW interface {
  112. Engine
  113. // Hashrate returns the current mining hashrate of a PoW consensus engine.
  114. Hashrate() float64
  115. }
  116. type PoSA interface {
  117. Engine
  118. IsSystemTransaction(tx *types.Transaction, header *types.Header) (bool, error)
  119. IsSystemContract(to *common.Address) bool
  120. EnoughDistance(chain ChainReader, header *types.Header) bool
  121. IsLocalBlock(header *types.Header) bool
  122. AllowLightProcess(chain ChainReader, currentHeader *types.Header) bool
  123. }