github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/chain/consensus/neatcon/handler.go (about)

     1  package neatcon
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/neatlab/neatio/chain/consensus"
     7  	ntcTypes "github.com/neatlab/neatio/chain/consensus/neatcon/types"
     8  	"github.com/neatlab/neatio/chain/core/types"
     9  	"github.com/neatlab/neatio/chain/log"
    10  	"github.com/neatlab/neatio/params"
    11  )
    12  
    13  var (
    14  	errDecodeFailed = errors.New("fail to decode neatcon message")
    15  )
    16  
    17  func (sb *backend) Protocol() consensus.Protocol {
    18  
    19  	sb.logger.Info("NeatCon backend protocol")
    20  
    21  	var protocolName string
    22  	if sb.chainConfig.NeatChainId == params.MainnetChainConfig.NeatChainId || sb.chainConfig.NeatChainId == params.TestnetChainConfig.NeatChainId {
    23  		protocolName = "neatio"
    24  	} else {
    25  		protocolName = "neatio_" + sb.chainConfig.NeatChainId
    26  	}
    27  
    28  	return consensus.Protocol{
    29  		Name:     protocolName,
    30  		Versions: []uint{64},
    31  		Lengths:  []uint64{64},
    32  	}
    33  }
    34  
    35  func (sb *backend) HandleMsg(chID uint64, src consensus.Peer, msgBytes []byte) (bool, error) {
    36  	sb.coreMu.Lock()
    37  	defer sb.coreMu.Unlock()
    38  
    39  	sb.core.consensusReactor.Receive(chID, src, msgBytes)
    40  
    41  	return false, nil
    42  }
    43  
    44  func (sb *backend) SetBroadcaster(broadcaster consensus.Broadcaster) {
    45  
    46  	sb.broadcaster = broadcaster
    47  }
    48  
    49  func (sb *backend) GetBroadcaster() consensus.Broadcaster {
    50  
    51  	return sb.broadcaster
    52  }
    53  
    54  func (sb *backend) NewChainHead(block *types.Block) error {
    55  	sb.coreMu.RLock()
    56  	defer sb.coreMu.RUnlock()
    57  	if !sb.coreStarted {
    58  		return ErrStoppedEngine
    59  	}
    60  	go ntcTypes.FireEventFinalCommitted(sb.core.EventSwitch(), ntcTypes.EventDataFinalCommitted{block.NumberU64()})
    61  	return nil
    62  }
    63  
    64  func (sb *backend) GetLogger() log.Logger {
    65  	return sb.logger
    66  }
    67  
    68  func (sb *backend) AddPeer(src consensus.Peer) {
    69  
    70  	sb.core.consensusReactor.AddPeer(src)
    71  	sb.logger.Debug("Peer successful added into Consensus Reactor")
    72  
    73  }
    74  
    75  func (sb *backend) RemovePeer(src consensus.Peer) {
    76  	sb.core.consensusReactor.RemovePeer(src, nil)
    77  }