github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/chain/consensus/consensus.go (about)

     1  package consensus
     2  
     3  import (
     4  	"math/big"
     5  
     6  	"github.com/neatlab/neatio/chain/consensus/neatcon/epoch"
     7  	"github.com/neatlab/neatio/chain/core/state"
     8  	"github.com/neatlab/neatio/chain/core/types"
     9  	"github.com/neatlab/neatio/network/rpc"
    10  	"github.com/neatlab/neatio/params"
    11  	"github.com/neatlab/neatio/utilities/common"
    12  )
    13  
    14  type ChainReader interface {
    15  	Config() *params.ChainConfig
    16  
    17  	CurrentHeader() *types.Header
    18  
    19  	GetHeader(hash common.Hash, number uint64) *types.Header
    20  
    21  	GetHeaderByNumber(number uint64) *types.Header
    22  
    23  	GetHeaderByHash(hash common.Hash) *types.Header
    24  
    25  	GetBlock(hash common.Hash, number uint64) *types.Block
    26  
    27  	GetBlockByNumber(number uint64) *types.Block
    28  
    29  	GetTd(hash common.Hash, number uint64) *big.Int
    30  
    31  	CurrentBlock() *types.Block
    32  
    33  	State() (*state.StateDB, error)
    34  }
    35  
    36  type ChainValidator interface {
    37  	ValidateBlock(block *types.Block) (*state.StateDB, types.Receipts, *types.PendingOps, error)
    38  }
    39  
    40  type Engine interface {
    41  	Author(header *types.Header) (common.Address, error)
    42  
    43  	VerifyHeader(chain ChainReader, header *types.Header, seal bool) error
    44  
    45  	VerifyHeaders(chain ChainReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error)
    46  
    47  	VerifyUncles(chain ChainReader, block *types.Block) error
    48  
    49  	VerifySeal(chain ChainReader, header *types.Header) error
    50  
    51  	Prepare(chain ChainReader, header *types.Header) error
    52  
    53  	Finalize(chain ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, totalGasFee *big.Int,
    54  		uncles []*types.Header, receipts []*types.Receipt, ops *types.PendingOps) (*types.Block, error)
    55  
    56  	Seal(chain ChainReader, block *types.Block, stop <-chan struct{}) (interface{}, error)
    57  
    58  	CalcDifficulty(chain ChainReader, time uint64, parent *types.Header) *big.Int
    59  
    60  	APIs(chain ChainReader) []rpc.API
    61  
    62  	Close() error
    63  
    64  	Protocol() Protocol
    65  }
    66  
    67  type Handler interface {
    68  	NewChainHead(block *types.Block) error
    69  
    70  	HandleMsg(chID uint64, src Peer, msgBytes []byte) (bool, error)
    71  
    72  	SetBroadcaster(Broadcaster)
    73  
    74  	GetBroadcaster() Broadcaster
    75  
    76  	AddPeer(src Peer)
    77  
    78  	RemovePeer(src Peer)
    79  }
    80  
    81  type EngineStartStop interface {
    82  	Start(chain ChainReader, currentBlock func() *types.Block, hasBadBlock func(hash common.Hash) bool) error
    83  
    84  	Stop() error
    85  }
    86  
    87  type NeatCon interface {
    88  	Engine
    89  
    90  	EngineStartStop
    91  
    92  	ShouldStart() bool
    93  
    94  	IsStarted() bool
    95  
    96  	ForceStart()
    97  
    98  	GetEpoch() *epoch.Epoch
    99  
   100  	SetEpoch(ep *epoch.Epoch)
   101  
   102  	PrivateValidator() common.Address
   103  
   104  	VerifyHeaderBeforeConsensus(chain ChainReader, header *types.Header, seal bool) error
   105  }