github.com/amazechain/amc@v0.1.3/internal/consensus/consensus.go (about)

     1  // Copyright 2022 The AmazeChain Authors
     2  // This file is part of the AmazeChain library.
     3  //
     4  // The AmazeChain 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 AmazeChain 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 AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package consensus
    18  
    19  import (
    20  	"github.com/amazechain/amc/common/block"
    21  	"github.com/amazechain/amc/common/transaction"
    22  	"github.com/amazechain/amc/common/types"
    23  	"github.com/amazechain/amc/modules/rpc/jsonrpc"
    24  	"github.com/amazechain/amc/modules/state"
    25  	"github.com/amazechain/amc/params"
    26  	"github.com/holiman/uint256"
    27  )
    28  
    29  type SystemCall func(contract types.Address, data []byte) ([]byte, error)
    30  type Call func(contract types.Address, data []byte) ([]byte, error)
    31  
    32  // ChainHeaderReader defines a small collection of methods needed to access the local
    33  // blockchain during header verification.
    34  type ChainHeaderReader interface {
    35  	// Config retrieves the blockchain's chain configuration.
    36  	Config() *params.ChainConfig
    37  
    38  	// CurrentBlock retrieves the current header from the local chain.
    39  	CurrentBlock() block.IBlock
    40  
    41  	// GetHeader retrieves a block header from the database by hash and number.
    42  	GetHeader(hash types.Hash, number *uint256.Int) block.IHeader
    43  
    44  	// GetHeaderByNumber retrieves a block header from the database by number.
    45  	GetHeaderByNumber(number *uint256.Int) block.IHeader
    46  
    47  	// GetHeaderByHash retrieves a block header from the database by its hash.
    48  	GetHeaderByHash(hash types.Hash) (block.IHeader, error)
    49  
    50  	// GetTd retrieves the total difficulty from the database by hash and number.
    51  	GetTd(types.Hash, *uint256.Int) *uint256.Int
    52  
    53  	GetBlockByNumber(number *uint256.Int) (block.IBlock, error)
    54  
    55  	GetDepositInfo(address types.Address) (*uint256.Int, *uint256.Int)
    56  	GetAccountRewardUnpaid(account types.Address) (*uint256.Int, error)
    57  }
    58  
    59  // ChainReader defines a small collection of methods needed to access the local
    60  // blockchain during header and/or uncle verification.
    61  type ChainReader interface {
    62  	ChainHeaderReader
    63  
    64  	// GetBlock retrieves a block from the database by hash and number.
    65  	GetBlock(hash types.Hash, number uint64) block.IBlock
    66  	GetBlockByNumber(number *uint256.Int) (block.IBlock, error)
    67  }
    68  
    69  // Engine is an algorithm agnostic consensus engine.
    70  type Engine interface {
    71  	EngineReader
    72  
    73  	// Author retrieves the Ethereum address of the account that minted the given
    74  	// block, which may be different from the header's coinbase if a consensus
    75  	// engine is based on signatures.
    76  	//Author(header block.IHeader) (types.Address, error)
    77  
    78  	// VerifyHeader checks whether a header conforms to the consensus rules of a
    79  	// given engine. Verifying the seal may be done optionally here, or explicitly
    80  	// via the VerifySeal method.
    81  	VerifyHeader(chain ChainHeaderReader, header block.IHeader, seal bool) error
    82  
    83  	// VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers
    84  	// concurrently. The method returns a quit channel to abort the operations and
    85  	// a results channel to retrieve the async verifications (the order is that of
    86  	// the input slice).
    87  	VerifyHeaders(chain ChainHeaderReader, headers []block.IHeader, seals []bool) (chan<- struct{}, <-chan error)
    88  
    89  	// VerifyUncles verifies that the given block's uncles conform to the consensus
    90  	// rules of a given engine.
    91  	VerifyUncles(chain ChainReader, block block.IBlock) error
    92  
    93  	// Prepare initializes the consensus fields of a block header according to the
    94  	// rules of a particular engine. The changes are executed inline.
    95  	Prepare(chain ChainHeaderReader, header block.IHeader) error
    96  
    97  	// Finalize runs any post-transaction state modifications (e.g. block rewards)
    98  	// but does not assemble the block.
    99  	//
   100  	// Note: The block header and state database might be updated to reflect any
   101  	// consensus rules that happen at finalization (e.g. block rewards).
   102  	Finalize(chain ChainHeaderReader, header block.IHeader, state *state.IntraBlockState, txs []*transaction.Transaction,
   103  		uncles []block.IHeader) ([]*block.Reward, map[types.Address]*uint256.Int, error)
   104  
   105  	// FinalizeAndAssemble runs any post-transaction state modifications (e.g. block
   106  	// rewards) and assembles the final block.
   107  	//
   108  	// Note: The block header and state database might be updated to reflect any
   109  	// consensus rules that happen at finalization (e.g. block rewards).
   110  	FinalizeAndAssemble(chain ChainHeaderReader, header block.IHeader, state *state.IntraBlockState, txs []*transaction.Transaction,
   111  		uncles []block.IHeader, receipts []*block.Receipt) (block.IBlock, []*block.Reward, map[types.Address]*uint256.Int, error)
   112  
   113  	//Rewards(tx kv.RwTx, header block.IHeader, state *state.IntraBlockState, setRewards bool) ([]*block.Reward, error)
   114  
   115  	// Seal generates a new sealing request for the given input block and pushes
   116  	// the result into the given channel.
   117  	//
   118  	// Note, the method returns immediately and will send the result async. More
   119  	// than one result may also be returned depending on the consensus algorithm.
   120  	Seal(chain ChainHeaderReader, block block.IBlock, results chan<- block.IBlock, stop <-chan struct{}) error
   121  
   122  	// SealHash returns the hash of a block prior to it being sealed.
   123  	SealHash(header block.IHeader) types.Hash
   124  
   125  	// CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty
   126  	// that a new block should have.
   127  	CalcDifficulty(chain ChainHeaderReader, time uint64, parent block.IHeader) *uint256.Int
   128  
   129  	//Type() params.ConsensusType
   130  
   131  	// APIs returns the RPC APIs this consensus engine provides.
   132  	APIs(chain ChainReader) []jsonrpc.API
   133  
   134  	// Close terminates any background threads maintained by the consensus engine.
   135  	Close() error
   136  }
   137  
   138  // EngineReader are read-only methods of the consensus engine
   139  // All of these methods should have thread-safe implementations
   140  type EngineReader interface {
   141  	// Author retrieves the Ethereum address of the account that minted the given
   142  	// block, which may be different from the header's coinbase if a consensus
   143  	// engine is based on signatures.
   144  	Author(header block.IHeader) (types.Address, error)
   145  
   146  	// Service transactions are free and don't pay baseFee after EIP-1559
   147  	IsServiceTransaction(sender types.Address, syscall SystemCall) bool
   148  
   149  	Type() params.ConsensusType
   150  }
   151  
   152  var (
   153  	SystemAddress = types.HexToAddress("0xffffFFFfFFffffffffffffffFfFFFfffFFFfFFfE")
   154  )