github.com/aergoio/aergo@v1.3.1/p2p/subproto/bp.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 type blockProducedNoticeHandler struct { 17 BaseMsgHandler 18 } 19 20 var _ p2pcommon.MessageHandler = (*blockProducedNoticeHandler)(nil) 21 22 // newNewBlockNoticeHandler creates handler for NewBlockNotice 23 func NewBlockProducedNoticeHandler(pm p2pcommon.PeerManager, peer p2pcommon.RemotePeer, logger *log.Logger, actor p2pcommon.ActorService, sm p2pcommon.SyncManager) *blockProducedNoticeHandler { 24 bh := &blockProducedNoticeHandler{BaseMsgHandler: BaseMsgHandler{protocol: p2pcommon.BlockProducedNotice, pm: pm, sm: sm, peer: peer, actor: actor, logger: logger}} 25 return bh 26 } 27 28 func (bh *blockProducedNoticeHandler) ParsePayload(rawbytes []byte) (p2pcommon.MessageBody, error) { 29 return p2putil.UnmarshalAndReturn(rawbytes, &types.BlockProducedNotice{}) 30 } 31 32 func (bh *blockProducedNoticeHandler) Handle(msg p2pcommon.Message, msgBody p2pcommon.MessageBody) { 33 remotePeer := bh.peer 34 data := msgBody.(*types.BlockProducedNotice) 35 if data.Block == nil || len(data.Block.Hash) == 0 { 36 bh.logger.Info().Str(p2putil.LogPeerName, remotePeer.Name()).Msg("invalid blockProduced notice. block is null") 37 return 38 } 39 // remove to verbose log 40 p2putil.DebugLogReceive(bh.logger, bh.protocol, msg.ID().String(), remotePeer,data) 41 42 // lru cache can accept hashable key 43 block := data.Block 44 if _, err := types.ParseToBlockID(data.GetBlock().GetHash()); err != nil { 45 // TODO add penalty score 46 bh.logger.Info().Str(p2putil.LogPeerName, remotePeer.Name()).Str("hash", enc.ToString(data.GetBlock().GetHash())).Msg("malformed blockHash") 47 return 48 } 49 // block by blockProduced notice must be new fresh block 50 remotePeer.UpdateLastNotice(data.GetBlock().GetHash(), data.BlockNo) 51 bh.sm.HandleBlockProducedNotice(bh.peer, block) 52 }