github.com/bigzoro/my_simplechain@v0.0.0-20240315012955-8ad0a2a29bb9/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 Ethereum consensus engines. 18 package consensus 19 20 import ( 21 "github.com/bigzoro/my_simplechain/common" 22 "github.com/bigzoro/my_simplechain/core/types" 23 ) 24 25 // Constants to match up protocol versions and messages 26 const ( 27 Eth63 = 63 28 Eth64 = 64 29 ) 30 31 var ( 32 EthProtocol = Protocol{ 33 Name: "eth", 34 Versions: []uint{Eth64, Eth63}, 35 Lengths: map[uint]uint64{Eth64: 17, Eth63: 17}, 36 } 37 ) 38 39 // Protocol defines the protocol of the consensus 40 type Protocol struct { 41 // Official short name of the protocol used during capability negotiation. 42 Name string 43 // Supported versions of the eth protocol (first is primary). 44 Versions []uint 45 // Number of implemented message corresponding to different protocol versions. 46 Lengths map[uint]uint64 47 } 48 49 // Broadcaster defines the interface to enqueue blocks to fetcher and find peer 50 type Broadcaster interface { 51 // Enqueue add a block into fetcher queue 52 Enqueue(id string, block *types.Block) 53 // FindPeers retrives peers by addresses 54 FindPeers(map[common.Address]bool) map[common.Address]Peer 55 // FindRoute 56 FindRoute([]common.Address, int, int) map[common.Address]Peer 57 // BroadcastBlock 58 BroadcastBlock(block *types.Block, propagate bool) 59 } 60 61 type Sealer interface { 62 // Execute block and return executed block 63 //Execute(block *types.Block) (*types.Block, error) 64 65 OnTimeout() 66 67 OnCommit(blockNum uint64, txNum int) 68 } 69 70 // Peer defines the interface to communicate with peer 71 type Peer interface { 72 // Send sends the message to this peer 73 Send(msgcode uint64, data interface{}) error 74 AsyncSendNewBlockHash(block *types.Block) 75 MarkTransaction(hash common.Hash) 76 } 77 78 type TxPool interface { 79 // Init light block by txpool 80 InitLightBlock(pBlock *types.LightBlock) bool 81 CheckAndSetSender(blocks types.Blocks) error 82 } 83 84 type ChainWriter interface { 85 Execute(block *types.Block) (*types.Block, error) 86 WriteBlock(block *types.Block) error 87 CommitBlock(block *types.Block) 88 }