github.com/aergoio/aergo@v1.3.1/p2p/subproto/raftstub.go (about)

     1  /*
     2   * @file
     3   * @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  package subproto
     7  
     8  import (
     9  	"github.com/aergoio/aergo-lib/log"
    10  	"github.com/aergoio/aergo/internal/enc"
    11  	"github.com/aergoio/aergo/p2p/p2pcommon"
    12  	"github.com/aergoio/aergo/p2p/p2putil"
    13  	"github.com/aergoio/aergo/types"
    14  )
    15  
    16  // raftBPNoticeDiscardHandler silently discard blk notice. It is for raft block producer, since raft BP receive notice from raft HTTPS
    17  type raftBPNoticeDiscardHandler struct {
    18  	BaseMsgHandler
    19  }
    20  
    21  var _ p2pcommon.MessageHandler = (*raftBPNoticeDiscardHandler)(nil)
    22  
    23  // newNewBlockNoticeHandler creates handler for NewBlockNotice
    24  func NewBPNoticeDiscardHandler(pm p2pcommon.PeerManager, peer p2pcommon.RemotePeer, logger *log.Logger, actor p2pcommon.ActorService, sm p2pcommon.SyncManager) p2pcommon.MessageHandler {
    25  	bh := &raftBPNoticeDiscardHandler{BaseMsgHandler: BaseMsgHandler{protocol: p2pcommon.BlockProducedNotice, pm: pm, sm: sm, peer: peer, actor: actor, logger: logger}}
    26  	return bh
    27  }
    28  
    29  func (bh *raftBPNoticeDiscardHandler) ParsePayload(rawbytes []byte) (p2pcommon.MessageBody, error) {
    30  	return p2putil.UnmarshalAndReturn(rawbytes, &types.BlockProducedNotice{})
    31  }
    32  
    33  func (bh *raftBPNoticeDiscardHandler) Handle(msg p2pcommon.Message, msgBody p2pcommon.MessageBody) {
    34  	remotePeer := bh.peer
    35  	data := msgBody.(*types.BlockProducedNotice)
    36  	if data.GetBlock() == nil || len(data.GetBlock().Hash) == 0 {
    37  		bh.logger.Info().Str(p2putil.LogPeerName, remotePeer.Name()).Msg("invalid blockProduced notice. block is null")
    38  		return
    39  	}
    40  	// just update last status
    41  	remotePeer.UpdateLastNotice(data.GetBlock().GetHash(), data.BlockNo)
    42  }
    43  
    44  // raftBPNoticeDiscardHandler silently discard blk notice. It is for raft block producer, since raft BP receive notice from raft HTTPS
    45  type raftNewBlkNoticeDiscardHandler struct {
    46  	BaseMsgHandler
    47  }
    48  
    49  var _ p2pcommon.MessageHandler = (*raftNewBlkNoticeDiscardHandler)(nil)
    50  
    51  // newNewBlockNoticeHandler creates handler for NewBlockNotice
    52  func NewBlkNoticeDiscardHandler(pm p2pcommon.PeerManager, peer p2pcommon.RemotePeer, logger *log.Logger, actor p2pcommon.ActorService, sm p2pcommon.SyncManager) p2pcommon.MessageHandler {
    53  	bh := &raftNewBlkNoticeDiscardHandler{BaseMsgHandler: BaseMsgHandler{protocol: p2pcommon.NewBlockNotice, pm: pm, sm: sm, peer: peer, actor: actor, logger: logger}}
    54  	return bh
    55  }
    56  
    57  func (bh *raftNewBlkNoticeDiscardHandler) ParsePayload(rawbytes []byte) (p2pcommon.MessageBody, error) {
    58  	return p2putil.UnmarshalAndReturn(rawbytes, &types.NewBlockNotice{})
    59  }
    60  
    61  func (bh *raftNewBlkNoticeDiscardHandler) Handle(msg p2pcommon.Message, msgBody p2pcommon.MessageBody) {
    62  	remotePeer := bh.peer
    63  	data := msgBody.(*types.NewBlockNotice)
    64  
    65  	if _, err := types.ParseToBlockID(data.BlockHash); err != nil {
    66  		// TODO Add penalty score and break
    67  		bh.logger.Info().Str(p2putil.LogPeerName, remotePeer.Name()).Str("hash", enc.ToString(data.BlockHash)).Msg("malformed blockHash")
    68  		return
    69  	}
    70  
    71  	// just update last status
    72  	remotePeer.UpdateLastNotice(data.BlockHash, data.BlockNo)
    73  }