github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/consensus/consensus.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  // Package consensus implements different Sberex consensus engines.
    13  package consensus
    14  
    15  import (
    16  	"github.com/Sberex/go-sberex/common"
    17  	"github.com/Sberex/go-sberex/core/state"
    18  	"github.com/Sberex/go-sberex/core/types"
    19  	"github.com/Sberex/go-sberex/params"
    20  	"github.com/Sberex/go-sberex/rpc"
    21  	"math/big"
    22  )
    23  
    24  // ChainReader defines a small collection of methods needed to access the local
    25  // blockchain during header and/or uncle verification.
    26  type ChainReader interface {
    27  	// Config retrieves the blockchain's chain configuration.
    28  	Config() *params.ChainConfig
    29  
    30  	// CurrentHeader retrieves the current header from the local chain.
    31  	CurrentHeader() *types.Header
    32  
    33  	// GetHeader retrieves a block header from the database by hash and number.
    34  	GetHeader(hash common.Hash, number uint64) *types.Header
    35  
    36  	// GetHeaderByNumber retrieves a block header from the database by number.
    37  	GetHeaderByNumber(number uint64) *types.Header
    38  
    39  	// GetHeaderByHash retrieves a block header from the database by its hash.
    40  	GetHeaderByHash(hash common.Hash) *types.Header
    41  
    42  	// GetBlock retrieves a block from the database by hash and number.
    43  	GetBlock(hash common.Hash, number uint64) *types.Block
    44  }
    45  
    46  // Engine is an algorithm agnostic consensus engine.
    47  type Engine interface {
    48  	// Author retrieves the Sberex address of the account that minted the given
    49  	// block, which may be different from the header's coinbase if a consensus
    50  	// engine is based on signatures.
    51  	Author(header *types.Header) (common.Address, error)
    52  
    53  	// VerifyHeader checks whether a header conforms to the consensus rules of a
    54  	// given engine. Verifying the seal may be done optionally here, or explicitly
    55  	// via the VerifySeal method.
    56  	VerifyHeader(chain ChainReader, header *types.Header, seal bool) error
    57  
    58  	// VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers
    59  	// concurrently. The method returns a quit channel to abort the operations and
    60  	// a results channel to retrieve the async verifications (the order is that of
    61  	// the input slice).
    62  	VerifyHeaders(chain ChainReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error)
    63  
    64  	// VerifyUncles verifies that the given block's uncles conform to the consensus
    65  	// rules of a given engine.
    66  	VerifyUncles(chain ChainReader, block *types.Block) error
    67  
    68  	// VerifySeal checks whether the crypto seal on a header is valid according to
    69  	// the consensus rules of the given engine.
    70  	VerifySeal(chain ChainReader, header *types.Header) error
    71  
    72  	// Prepare initializes the consensus fields of a block header according to the
    73  	// rules of a particular engine. The changes are executed inline.
    74  	Prepare(chain ChainReader, header *types.Header) error
    75  
    76  	// Finalize runs any post-transaction state modifications (e.g. block rewards)
    77  	// and assembles the final block.
    78  	// Note: The block header and state database might be updated to reflect any
    79  	// consensus rules that happen at finalization (e.g. block rewards).
    80  	Finalize(chain ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction,
    81  		uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error)
    82  
    83  	// Seal generates a new block for the given input block with the local miner's
    84  	// seal place on top.
    85  	Seal(chain ChainReader, block *types.Block, stop <-chan struct{}) (*types.Block, error)
    86  
    87  	// CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty
    88  	// that a new block should have.
    89  	CalcDifficulty(chain ChainReader, time uint64, parent *types.Header) *big.Int
    90  
    91  	// APIs returns the RPC APIs this consensus engine provides.
    92  	APIs(chain ChainReader) []rpc.API
    93  }
    94  
    95  // PoW is a consensus engine based on proof-of-work.
    96  type PoW interface {
    97  	Engine
    98  
    99  	// Hashrate returns the current mining hashrate of a PoW consensus engine.
   100  	Hashrate() float64
   101  }