[NOD-1204] Add timestamp and message number to domain messages (#854)

This commit is contained in:
Ori Newman 2020-08-10 12:55:24 +03:00 committed by GitHub
parent 53cccd405f
commit b0fecc9f87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 74 additions and 11 deletions

View File

@ -0,0 +1,24 @@
package domainmessage
import "time"
type baseMessage struct {
messageNumber uint64
receivedAt time.Time
}
func (b *baseMessage) MessageNumber() uint64 {
return b.messageNumber
}
func (b *baseMessage) SetMessageNumber(messageNumber uint64) {
b.messageNumber = messageNumber
}
func (b *baseMessage) ReceivedAt() time.Time {
return b.receivedAt
}
func (b *baseMessage) SetReceivedAt(receivedAt time.Time) {
b.receivedAt = receivedAt
}

View File

@ -6,6 +6,7 @@ package domainmessage
import ( import (
"fmt" "fmt"
"time"
) )
// MaxMessagePayload is the maximum bytes a message can be regardless of other // MaxMessagePayload is the maximum bytes a message can be regardless of other
@ -79,4 +80,8 @@ var MessageCommandToString = map[MessageCommand]string{
// are used directly in the protocol encoded message. // are used directly in the protocol encoded message.
type Message interface { type Message interface {
Command() MessageCommand Command() MessageCommand
MessageNumber() uint64
SetMessageNumber(index uint64)
ReceivedAt() time.Time
SetReceivedAt(receivedAt time.Time)
} }

View File

@ -24,6 +24,7 @@ const MaxAddressesPerMsg = 1000
// Use the AddAddress function to build up the list of known addresses when // Use the AddAddress function to build up the list of known addresses when
// sending an Addresses message to another peer. // sending an Addresses message to another peer.
type MsgAddresses struct { type MsgAddresses struct {
baseMessage
IncludeAllSubnetworks bool IncludeAllSubnetworks bool
SubnetworkID *subnetworkid.SubnetworkID SubnetworkID *subnetworkid.SubnetworkID
AddrList []*NetAddress AddrList []*NetAddress

View File

@ -42,6 +42,7 @@ type TxLoc struct {
// block message. It is used to deliver block and transaction information in // block message. It is used to deliver block and transaction information in
// response to a getdata message (MsgGetData) for a given block hash. // response to a getdata message (MsgGetData) for a given block hash.
type MsgBlock struct { type MsgBlock struct {
baseMessage
Header BlockHeader Header BlockHeader
Transactions []*MsgTx Transactions []*MsgTx
} }

View File

@ -12,6 +12,7 @@ const MaxBlockLocatorsPerMsg = 500
// locator message. It is used to find the blockLocator of a peer that is // locator message. It is used to find the blockLocator of a peer that is
// syncing with you. // syncing with you.
type MsgBlockLocator struct { type MsgBlockLocator struct {
baseMessage
BlockLocatorHashes []*daghash.Hash BlockLocatorHashes []*daghash.Hash
} }

View File

@ -5,7 +5,9 @@ package domainmessage
// syncer sent all the requested blocks. // syncer sent all the requested blocks.
// //
// This message has no payload. // This message has no payload.
type MsgDoneIBDBlocks struct{} type MsgDoneIBDBlocks struct {
baseMessage
}
// Command returns the protocol command string for the message. This is part // Command returns the protocol command string for the message. This is part
// of the Message interface implementation. // of the Message interface implementation.

View File

@ -8,6 +8,7 @@ package domainmessage
// ibdblock message. It is used to deliver block and transaction information in // ibdblock message. It is used to deliver block and transaction information in
// response to a RequestIBDBlocks message (MsgRequestIBDBlocks). // response to a RequestIBDBlocks message (MsgRequestIBDBlocks).
type MsgIBDBlock struct { type MsgIBDBlock struct {
baseMessage
*MsgBlock *MsgBlock
} }
@ -26,5 +27,5 @@ func (msg *MsgIBDBlock) MaxPayloadLength(pver uint32) uint32 {
// NewMsgIBDBlock returns a new kaspa ibdblock message that conforms to the // NewMsgIBDBlock returns a new kaspa ibdblock message that conforms to the
// Message interface. See MsgIBDBlock for details. // Message interface. See MsgIBDBlock for details.
func NewMsgIBDBlock(msgBlock *MsgBlock) *MsgIBDBlock { func NewMsgIBDBlock(msgBlock *MsgBlock) *MsgIBDBlock {
return &MsgIBDBlock{msgBlock} return &MsgIBDBlock{MsgBlock: msgBlock}
} }

View File

@ -77,8 +77,8 @@ func TestIBDBlockEncoding(t *testing.T) {
}{ }{
// Latest protocol version. // Latest protocol version.
{ {
&MsgIBDBlock{&blockOne}, &MsgIBDBlock{MsgBlock: &blockOne},
&MsgIBDBlock{&blockOne}, &MsgIBDBlock{MsgBlock: &blockOne},
blockOneBytes, blockOneBytes,
blockOneTxLocs, blockOneTxLocs,
ProtocolVersion, ProtocolVersion,

View File

@ -8,6 +8,7 @@ import (
// block inventory message. It is used to notify the network about new block // block inventory message. It is used to notify the network about new block
// by sending their hash, and let the receiving node decide if it needs it. // by sending their hash, and let the receiving node decide if it needs it.
type MsgInvRelayBlock struct { type MsgInvRelayBlock struct {
baseMessage
Hash *daghash.Hash Hash *daghash.Hash
} }

View File

@ -12,6 +12,7 @@ const MaxInvPerTxInvMsg = MaxInvPerMsg
// TxInv message. It is used to notify the network about new transactions // TxInv message. It is used to notify the network about new transactions
// by sending their ID, and let the receiving node decide if it needs it. // by sending their ID, and let the receiving node decide if it needs it.
type MsgInvTransaction struct { type MsgInvTransaction struct {
baseMessage
TxIDs []*daghash.TxID TxIDs []*daghash.TxID
} }

View File

@ -16,6 +16,7 @@ package domainmessage
// The payload for this message just consists of a nonce used for identifying // The payload for this message just consists of a nonce used for identifying
// it later. // it later.
type MsgPing struct { type MsgPing struct {
baseMessage
// Unique value associated with message that is used to identify // Unique value associated with message that is used to identify
// specific ping message. // specific ping message.
Nonce uint64 Nonce uint64

View File

@ -10,6 +10,7 @@ package domainmessage
// //
// This message was not added until protocol versions AFTER BIP0031Version. // This message was not added until protocol versions AFTER BIP0031Version.
type MsgPong struct { type MsgPong struct {
baseMessage
// Unique value associated with message that is used to identify // Unique value associated with message that is used to identify
// specific ping message. // specific ping message.
Nonce uint64 Nonce uint64

View File

@ -15,6 +15,7 @@ import (
// //
// This message has no payload. // This message has no payload.
type MsgRequestAddresses struct { type MsgRequestAddresses struct {
baseMessage
IncludeAllSubnetworks bool IncludeAllSubnetworks bool
SubnetworkID *subnetworkid.SubnetworkID SubnetworkID *subnetworkid.SubnetworkID
} }

View File

@ -9,6 +9,7 @@ import (
// and low hash. // and low hash.
// The locator is returned via a locator message (MsgBlockLocator). // The locator is returned via a locator message (MsgBlockLocator).
type MsgRequestBlockLocator struct { type MsgRequestBlockLocator struct {
baseMessage
HighHash *daghash.Hash HighHash *daghash.Hash
LowHash *daghash.Hash LowHash *daghash.Hash
} }

View File

@ -12,6 +12,7 @@ import (
// RequestIBDBlocks message. It is used to request a list of blocks starting after the // RequestIBDBlocks message. It is used to request a list of blocks starting after the
// low hash and until the high hash. // low hash and until the high hash.
type MsgRequestIBDBlocks struct { type MsgRequestIBDBlocks struct {
baseMessage
LowHash *daghash.Hash LowHash *daghash.Hash
HighHash *daghash.Hash HighHash *daghash.Hash
} }

View File

@ -5,7 +5,9 @@ package domainmessage
// more blocks. // more blocks.
// //
// This message has no payload. // This message has no payload.
type MsgRequestNextIBDBlocks struct{} type MsgRequestNextIBDBlocks struct {
baseMessage
}
// Command returns the protocol command string for the message. This is part // Command returns the protocol command string for the message. This is part
// of the Message interface implementation. // of the Message interface implementation.

View File

@ -12,6 +12,7 @@ const MsgRequestRelayBlocksHashes = MaxInvPerMsg
// RequestRelayBlocks message. It is used to request blocks as part of the block // RequestRelayBlocks message. It is used to request blocks as part of the block
// relay protocol. // relay protocol.
type MsgRequestRelayBlocks struct { type MsgRequestRelayBlocks struct {
baseMessage
Hashes []*daghash.Hash Hashes []*daghash.Hash
} }

View File

@ -4,7 +4,9 @@ package domainmessage
// RequestSelectedTip message. It is used to request the selected tip of another peer. // RequestSelectedTip message. It is used to request the selected tip of another peer.
// //
// This message has no payload. // This message has no payload.
type MsgRequestSelectedTip struct{} type MsgRequestSelectedTip struct {
baseMessage
}
// Command returns the protocol command string for the message. This is part // Command returns the protocol command string for the message. This is part
// of the Message interface implementation. // of the Message interface implementation.

View File

@ -12,6 +12,7 @@ const MaxInvPerRequestTransactionsMsg = MaxInvPerMsg
// RequestTransactions message. It is used to request transactions as part of the // RequestTransactions message. It is used to request transactions as part of the
// transactions relay protocol. // transactions relay protocol.
type MsgRequestTransactions struct { type MsgRequestTransactions struct {
baseMessage
IDs []*daghash.TxID IDs []*daghash.TxID
} }

View File

@ -8,6 +8,7 @@ import (
// selectedtip message. It is used to answer getseltip messages and tell // selectedtip message. It is used to answer getseltip messages and tell
// the asking peer what is the selected tip of this peer. // the asking peer what is the selected tip of this peer.
type MsgSelectedTip struct { type MsgSelectedTip struct {
baseMessage
// The selected tip hash of the generator of the message. // The selected tip hash of the generator of the message.
SelectedTipHash *daghash.Hash SelectedTipHash *daghash.Hash
} }

View File

@ -11,6 +11,7 @@ import (
// MsgTransactionNotFound defines a kaspa TransactionNotFound message which is sent in response to // MsgTransactionNotFound defines a kaspa TransactionNotFound message which is sent in response to
// a RequestTransactions message if any of the requested data in not available on the peer. // a RequestTransactions message if any of the requested data in not available on the peer.
type MsgTransactionNotFound struct { type MsgTransactionNotFound struct {
baseMessage
ID *daghash.TxID ID *daghash.TxID
} }

View File

@ -268,6 +268,7 @@ func NewTxOut(value uint64, scriptPubKey []byte) *TxOut {
// Use the AddTxIn and AddTxOut functions to build up the list of transaction // Use the AddTxIn and AddTxOut functions to build up the list of transaction
// inputs and outputs. // inputs and outputs.
type MsgTx struct { type MsgTx struct {
baseMessage
Version int32 Version int32
TxIn []*TxIn TxIn []*TxIn
TxOut []*TxOut TxOut []*TxOut

View File

@ -9,7 +9,9 @@ package domainmessage
// to negotiate parameters. It implements the Message interface. // to negotiate parameters. It implements the Message interface.
// //
// This message has no payload. // This message has no payload.
type MsgVerAck struct{} type MsgVerAck struct {
baseMessage
}
// Command returns the protocol command string for the message. This is part // Command returns the protocol command string for the message. This is part
// of the Message interface implementation. // of the Message interface implementation.

View File

@ -31,6 +31,7 @@ var DefaultUserAgent = fmt.Sprintf("/kaspad:%s/", version.Version())
// message (MsgVerAck). This exchange must take place before any further // message (MsgVerAck). This exchange must take place before any further
// communication is allowed to proceed. // communication is allowed to proceed.
type MsgVersion struct { type MsgVersion struct {
baseMessage
// Version of the protocol the node is using. // Version of the protocol the node is using.
ProtocolVersion uint32 ProtocolVersion uint32

View File

@ -2,6 +2,7 @@ package grpcserver
import ( import (
"io" "io"
"time"
routerpkg "github.com/kaspanet/kaspad/netadapter/router" routerpkg "github.com/kaspanet/kaspad/netadapter/router"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -61,6 +62,7 @@ func (c *gRPCConnection) sendLoop() error {
} }
func (c *gRPCConnection) receiveLoop() error { func (c *gRPCConnection) receiveLoop() error {
messageNumber := uint64(0)
for c.IsConnected() { for c.IsConnected() {
protoMessage, err := c.stream.Recv() protoMessage, err := c.stream.Recv()
if err != nil { if err != nil {
@ -75,8 +77,15 @@ func (c *gRPCConnection) receiveLoop() error {
return err return err
} }
log.Debugf("incoming '%s' message from %s", message.Command(), c) messageNumber++
log.Tracef("incoming '%s' message from %s: %s", message.Command(), c, logger.NewLogClosure(func() string { message.SetMessageNumber(messageNumber)
message.SetReceivedAt(time.Now())
log.Debugf("incoming '%s' message from %s (message number %d)", message.Command(), c,
message.MessageNumber())
log.Tracef("incoming '%s' message from %s (message number %d): %s", message.Command(),
c, message.MessageNumber(), logger.NewLogClosure(func() string {
return spew.Sdump(message) return spew.Sdump(message)
})) }))