github.com/Gessiux/neatchain@v1.3.1/chain/consensus/neatcon/handler.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 neatcon 18 19 import ( 20 "errors" 21 22 "github.com/Gessiux/neatchain/chain/consensus" 23 ntcTypes "github.com/Gessiux/neatchain/chain/consensus/neatcon/types" 24 "github.com/Gessiux/neatchain/chain/core/types" 25 "github.com/Gessiux/neatchain/chain/log" 26 "github.com/Gessiux/neatchain/params" 27 ) 28 29 var ( 30 // errDecodeFailed is returned when decode message fails 31 errDecodeFailed = errors.New("fail to decode neatcon message") 32 ) 33 34 // Protocol implements consensus.Engine.Protocol 35 func (sb *backend) Protocol() consensus.Protocol { 36 37 sb.logger.Info("NeatCon backend started") 38 39 var protocolName string 40 if sb.chainConfig.NeatChainId == params.MainnetChainConfig.NeatChainId || sb.chainConfig.NeatChainId == params.TestnetChainConfig.NeatChainId { 41 protocolName = "neatchain" //we also use "neatchain" if the net is "testnet" 42 } else { 43 protocolName = "neatchain_" + sb.chainConfig.NeatChainId 44 } 45 46 return consensus.Protocol{ 47 Name: protocolName, 48 Versions: []uint{64}, 49 Lengths: []uint64{64}, 50 } 51 } 52 53 // HandleMsg implements consensus.Handler.HandleMsg 54 func (sb *backend) HandleMsg(chID uint64, src consensus.Peer, msgBytes []byte) (bool, error) { 55 sb.coreMu.Lock() 56 defer sb.coreMu.Unlock() 57 58 sb.core.consensusReactor.Receive(chID, src, msgBytes) 59 60 return false, nil 61 } 62 63 // SetBroadcaster implements consensus.Handler.SetBroadcaster 64 func (sb *backend) SetBroadcaster(broadcaster consensus.Broadcaster) { 65 66 //sb.logger.Infof("NeatCon SetBroadcaster: %p", broadcaster) 67 sb.broadcaster = broadcaster 68 } 69 70 func (sb *backend) GetBroadcaster() consensus.Broadcaster { 71 72 sb.logger.Infof("NeatCon GetBroadcaster: %p", sb.broadcaster) 73 return sb.broadcaster 74 } 75 76 func (sb *backend) NewChainHead(block *types.Block) error { 77 sb.coreMu.RLock() 78 defer sb.coreMu.RUnlock() 79 if !sb.coreStarted { 80 return ErrStoppedEngine 81 } 82 go ntcTypes.FireEventFinalCommitted(sb.core.EventSwitch(), ntcTypes.EventDataFinalCommitted{block.NumberU64()}) 83 return nil 84 } 85 86 func (sb *backend) GetLogger() log.Logger { 87 return sb.logger 88 } 89 90 func (sb *backend) AddPeer(src consensus.Peer) { 91 92 sb.core.consensusReactor.AddPeer(src) 93 sb.logger.Debug("Peer successful added into Consensus Reactor") 94 95 /* 96 if !sb.shouldStart { 97 sb.logger.Debug("Consensus Engine (NeatCon) does not plan to start") 98 return 99 } 100 101 102 for i := 0; i < 10; i++ { 103 sb.coreMu.RLock() 104 started := sb.coreStarted 105 sb.coreMu.RUnlock() 106 107 if started { 108 sb.core.consensusReactor.AddPeer(src) 109 sb.logger.Debug("Peer successful added into Consensus Reactor") 110 return 111 } else { 112 time.Sleep(1 * time.Second) 113 } 114 } 115 116 sb.logger.Error("Wait for 10 sec, Consensus Engine (NeatCon) still not start, unable to add the peer to Engine") 117 */ 118 } 119 120 func (sb *backend) RemovePeer(src consensus.Peer) { 121 sb.core.consensusReactor.RemovePeer(src, nil) 122 }