github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/consensus/protocol.go (about)

     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  
    17  // Package consensus implements different INT Chain consensus engines.
    18  package consensus
    19  
    20  import (
    21  	"github.com/intfoundation/intchain/common"
    22  	"github.com/intfoundation/intchain/core/types"
    23  	"math/big"
    24  )
    25  
    26  // Constants to match up protocol versions and messages
    27  const (
    28  	Eth62 = 62
    29  	Eth63 = 63
    30  )
    31  
    32  var (
    33  	EthProtocol = Protocol{
    34  		Name:     "eth",
    35  		Versions: []uint{Eth62, Eth63},
    36  		Lengths:  []uint64{17, 8},
    37  	}
    38  )
    39  
    40  // Protocol defines the protocol of the consensus
    41  type Protocol struct {
    42  	// Official short name of the protocol used during capability negotiation.
    43  	Name string
    44  	// Supported versions of the intprotocol protocol (first is primary).
    45  	Versions []uint
    46  	// Number of implemented message corresponding to different protocol versions.
    47  	Lengths []uint64
    48  }
    49  
    50  // Broadcaster defines the interface to enqueue blocks to fetcher and find peer
    51  type Broadcaster interface {
    52  	// Enqueue add a block into fetcher queue
    53  	Enqueue(id string, block *types.Block)
    54  	// FindPeers retrives peers by addresses
    55  	FindPeers(map[common.Address]bool) map[common.Address]Peer
    56  	// BroadcastBlock broadcast Block
    57  	BroadcastBlock(block *types.Block, propagate bool)
    58  	// BroadcastMessage broadcast Message to P2P network
    59  	BroadcastMessage(msgcode uint64, data interface{})
    60  	// Find the Bad Preimages and send request to best peer for correction
    61  	TryFixBadPreimages()
    62  }
    63  
    64  // Peer defines the interface to communicate with peer
    65  type Peer interface {
    66  	// Send sends the message to this peer
    67  	Send(msgcode uint64, data interface{}) error
    68  	//Send block to this peer
    69  	SendNewBlock(block *types.Block, td *big.Int) error
    70  	// GetPeerState return the Peer State during consensus
    71  	GetPeerState() PeerState
    72  	// GetKey return the short Public Key of peer
    73  	GetKey() string
    74  	// GetConsensusKey return the publicc key of peer for consensus
    75  	GetConsensusKey() string
    76  	// PeerState set the Peer State
    77  	SetPeerState(ps PeerState)
    78  }
    79  
    80  type PeerState interface {
    81  	GetHeight() uint64
    82  	Disconnect()
    83  }