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

     1  /*
     2   * @file
     3   * @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  package subproto
     7  
     8  import (
     9  	"errors"
    10  	"github.com/aergoio/aergo-lib/log"
    11  	"github.com/aergoio/aergo/consensus"
    12  	"github.com/aergoio/aergo/p2p/p2pcommon"
    13  	"github.com/aergoio/aergo/p2p/p2putil"
    14  	"github.com/aergoio/aergo/types"
    15  )
    16  
    17  var (
    18  	ErrConsensusAccessorNotReady = errors.New("consensus accessor is not ready")
    19  )
    20  
    21  type getClusterRequestHandler struct {
    22  	BaseMsgHandler
    23  
    24  	consAcc consensus.ConsensusAccessor
    25  }
    26  
    27  var _ p2pcommon.MessageHandler = (*getClusterRequestHandler)(nil)
    28  
    29  type getClusterResponseHandler struct {
    30  	BaseMsgHandler
    31  }
    32  
    33  var _ p2pcommon.MessageHandler = (*getClusterResponseHandler)(nil)
    34  
    35  // NewGetClusterReqHandler creates handler for PingRequest
    36  func NewGetClusterReqHandler(pm p2pcommon.PeerManager, peer p2pcommon.RemotePeer, logger *log.Logger, actor p2pcommon.ActorService, consAcc consensus.ConsensusAccessor) *getClusterRequestHandler {
    37  	ph := &getClusterRequestHandler{
    38  		BaseMsgHandler: BaseMsgHandler{protocol: p2pcommon.GetClusterRequest, pm: pm, peer: peer, actor: actor, logger: logger},
    39  		consAcc:        consAcc,
    40  	}
    41  	return ph
    42  }
    43  
    44  func (ph *getClusterRequestHandler) ParsePayload(rawbytes []byte) (p2pcommon.MessageBody, error) {
    45  	return p2putil.UnmarshalAndReturn(rawbytes, &types.GetClusterInfoRequest{})
    46  }
    47  
    48  func (ph *getClusterRequestHandler) Handle(msg p2pcommon.Message, msgBody p2pcommon.MessageBody) {
    49  	//peerID := ph.peer.ID()
    50  	remotePeer := ph.peer
    51  	data := msgBody.(*types.GetClusterInfoRequest)
    52  	p2putil.DebugLogReceive(ph.logger, ph.protocol, msg.ID().String(), remotePeer, data)
    53  
    54  	var resp *types.GetClusterInfoResponse
    55  
    56  	// GetClusterInfo from consensus
    57  	if ph.consAcc == nil {
    58  		resp = &types.GetClusterInfoResponse{Error: ErrConsensusAccessorNotReady.Error()}
    59  	} else {
    60  		resp = ph.consAcc.ClusterInfo(data.BestBlockHash)
    61  	}
    62  
    63  	remotePeer.SendMessage(remotePeer.MF().NewMsgResponseOrder(msg.ID(), p2pcommon.GetClusterResponse, resp))
    64  }
    65  
    66  // NewGetClusterRespHandler creates handler for PingRequest
    67  func NewGetClusterRespHandler(pm p2pcommon.PeerManager, peer p2pcommon.RemotePeer, logger *log.Logger, actor p2pcommon.ActorService) *getClusterResponseHandler {
    68  	ph := &getClusterResponseHandler{BaseMsgHandler{protocol: p2pcommon.GetClusterResponse, pm: pm, peer: peer, actor: actor, logger: logger}}
    69  	return ph
    70  }
    71  
    72  func (ph *getClusterResponseHandler) ParsePayload(rawbytes []byte) (p2pcommon.MessageBody, error) {
    73  	return p2putil.UnmarshalAndReturn(rawbytes, &types.GetClusterInfoResponse{})
    74  }
    75  
    76  func (ph *getClusterResponseHandler) Handle(msg p2pcommon.Message, msgBody p2pcommon.MessageBody) {
    77  	remotePeer := ph.peer
    78  	data := msgBody.(*types.GetClusterInfoResponse)
    79  	p2putil.DebugLogReceiveResponse(ph.logger, ph.protocol, msg.ID().String(), msg.OriginalID().String(), remotePeer, data)
    80  
    81  	if !remotePeer.GetReceiver(msg.OriginalID())(msg, data) {
    82  		// ignore dangling response
    83  		// TODO add penalty if needed
    84  		remotePeer.ConsumeRequest(msg.OriginalID())
    85  	}
    86  
    87  }