github.com/aergoio/aergo@v1.3.1/p2p/p2pcommon/message.go (about)

     1  /*
     2   * @file
     3   * @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  //go:generate mockgen -source=message.go -package=p2pmock -destination=../p2pmock/mock_message.go
     7  package p2pcommon
     8  
     9  import (
    10  	"github.com/aergoio/aergo/types"
    11  	"github.com/golang/protobuf/proto"
    12  	"time"
    13  )
    14  
    15  // Message is unit structure transferred from a peer to another peer.
    16  type Message interface {
    17  	Subprotocol() SubProtocol
    18  
    19  	// Length is length of payload
    20  	Length() uint32
    21  
    22  	// Timestamp is when this message was created with unixNano format
    23  	Timestamp() int64
    24  
    25  	// ID is 16 bytes unique identifier
    26  	ID() MsgID
    27  
    28  	// OriginalID is message id of request which trigger this message. it will be all zero, if message is request or notice.
    29  	OriginalID() MsgID
    30  
    31  	// Payload is MessageBody struct, marshaled by google protocol buffer v3. object is determined by Subprotocol
    32  	Payload() []byte
    33  }
    34  
    35  // MessageBody is content of p2p message.
    36  // The actual data types are varied by subprotocol, so
    37  // For version 0.3.x, it is just wrapper of proto.Message
    38  type MessageBody interface {
    39  	proto.Message
    40  }
    41  
    42  // MessageHandler handle incoming message.
    43  // The Handler belongs to RemotePeer, i.e. every connected remote peer has its own handlers. Each handler handles messages
    44  // assigned to it sequentially, but multiple handlers can handle their own messages concurrently.
    45  type MessageHandler interface {
    46  	AddAdvice(advice HandlerAdvice)
    47  	ParsePayload([]byte) (MessageBody, error)
    48  	CheckAuth(msg Message, msgBody MessageBody) error
    49  	Handle(msg Message, msgBody MessageBody)
    50  	PreHandle()
    51  	PostHandle(msg Message, msgBody MessageBody)
    52  }
    53  
    54  type HandlerAdvice interface {
    55  	PreHandle()
    56  	PostHandle(msg Message, msgBody MessageBody)
    57  }
    58  
    59  type AsyncHandler interface {
    60  	HandleOrNot(msg Message, msgBody MessageBody) bool
    61  	Handle(msg Message, msgBody MessageBody, ttl time.Duration)
    62  }
    63  // MsgSigner sign or verify p2p message
    64  // this is not used since v0.3, but interface is not removed for future version.
    65  type MsgSigner interface {
    66  	// signMsg calculate signature and fill related fields in msg(peerID, pubkey, signature or etc)
    67  	SignMsg(msg *types.P2PMessage) error
    68  	// verifyMsg check signature is valid
    69  	VerifyMsg(msg *types.P2PMessage, senderID types.PeerID) error
    70  }
    71  
    72  // ResponseReceiver is handler function for the corresponding response message.
    73  // It returns true when receiver handled it, or false if this receiver is not the expected handler.
    74  type ResponseReceiver func(Message, MessageBody) bool