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