consensus.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. "github.com/ethereum/go-ethereum/common"
  20. "github.com/ethereum/go-ethereum/core/state"
  21. "github.com/ethereum/go-ethereum/core/types"
  22. "github.com/ethereum/go-ethereum/params"
  23. "github.com/ethereum/go-ethereum/rpc"
  24. )
  25. // ChainReader defines a small collection of methods needed to access the local
  26. // blockchain during header and/or uncle verification.
  27. type ChainReader interface {
  28. // Config retrieves the blockchain's chain configuration.
  29. Config() *params.ChainConfig
  30. // CurrentHeader retrieves the current header from the local chain.
  31. CurrentHeader() *types.Header
  32. // GetHeader retrieves a block header from the database by hash and number.
  33. GetHeader(hash common.Hash, number uint64) *types.Header
  34. // GetHeaderByNumber retrieves a block header from the database by number.
  35. GetHeaderByNumber(number uint64) *types.Header
  36. // GetHeaderByHash retrieves a block header from the database by its hash.
  37. GetHeaderByHash(hash common.Hash) *types.Header
  38. // GetBlock retrieves a block from the database by hash and number.
  39. GetBlock(hash common.Hash, number uint64) *types.Block
  40. }
  41. // Engine is an algorithm agnostic consensus engine.
  42. type Engine interface {
  43. // Author retrieves the Ethereum address of the account that minted the given
  44. // block, which may be different from the header's coinbase if a consensus
  45. // engine is based on signatures.
  46. Author(header *types.Header) (common.Address, error)
  47. // VerifyHeader checks whether a header conforms to the consensus rules of a
  48. // given engine. Verifying the seal may be done optionally here, or explicitly
  49. // via the VerifySeal method.
  50. VerifyHeader(chain ChainReader, header *types.Header, seal bool) error
  51. // VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers
  52. // concurrently. The method returns a quit channel to abort the operations and
  53. // a results channel to retrieve the async verifications (the order is that of
  54. // the input slice).
  55. VerifyHeaders(chain ChainReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error)
  56. // VerifyUncles verifies that the given block's uncles conform to the consensus
  57. // rules of a given engine.
  58. VerifyUncles(chain ChainReader, block *types.Block) error
  59. // VerifySeal checks whether the crypto seal on a header is valid according to
  60. // the consensus rules of the given engine.
  61. VerifySeal(chain ChainReader, header *types.Header) error
  62. // Prepare initializes the consensus fields of a block header according to the
  63. // rules of a particular engine. The changes are executed inline.
  64. Prepare(chain ChainReader, header *types.Header) error
  65. // Finalize runs any post-transaction state modifications (e.g. block rewards)
  66. // and assembles the final block.
  67. //
  68. // Note, the block header and state database might be updated to reflect any
  69. // consensus rules that happen at finalization (e.g. block rewards).
  70. Finalize(chain ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction,
  71. uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error)
  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. // APIs returns the RPC APIs this consensus engine provides.
  76. APIs(chain ChainReader) []rpc.API
  77. }
  78. // PoW is a consensus engine based on proof-of-work.
  79. type PoW interface {
  80. Engine
  81. // Hashrate returns the current mining hashrate of a PoW consensus engine.
  82. Hashrate() float64
  83. }