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