-
Notifications
You must be signed in to change notification settings - Fork 202
/
message.go
91 lines (75 loc) · 2.14 KB
/
message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package memp2p
import (
"encoding/binary"
"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go/p2p"
)
var _ p2p.MessageP2P = (*message)(nil)
// Message represents a message to be sent through the in-memory network
// simulated by the Network struct.
type message struct {
from []byte
data []byte
seqNo []byte
topic string
signature []byte
key []byte
peer core.PeerID
payloadField []byte
timestampField int64
}
// NewMessage constructs a new Message instance from arguments
func newMessage(topic string, data []byte, peerID core.PeerID, seqNo uint64) *message {
empty := make([]byte, 0)
seqNoBytes := make([]byte, 8)
binary.BigEndian.PutUint64(seqNoBytes, seqNo)
return &message{
from: []byte(peerID),
data: data,
seqNo: seqNoBytes,
topic: topic,
signature: empty,
key: []byte(peerID),
peer: peerID,
}
}
// From returns the message originator's peer ID
func (msg *message) From() []byte {
return msg.from
}
// Data returns the message payload
func (msg *message) Data() []byte {
return msg.data
}
// SeqNo returns the message sequence number
func (msg *message) SeqNo() []byte {
return msg.seqNo
}
// Topic returns the topic on which the message was sent
func (msg *message) Topic() string {
return msg.topic
}
// Signature returns the message signature
func (msg *message) Signature() []byte {
return msg.signature
}
// Key returns the message public key (if it can not be recovered from From field)
func (msg *message) Key() []byte {
return msg.key
}
// Peer returns the peer that originated the message
func (msg *message) Peer() core.PeerID {
return msg.peer
}
// Payload returns the encapsulated message along with meta data such as timestamp
func (msg *message) Payload() []byte {
return msg.payloadField
}
// Timestamp returns the message timestamp to prevent endless re-processing of the same message
func (msg *message) Timestamp() int64 {
return msg.timestampField
}
// IsInterfaceNil returns true if there is no value under the interface
func (msg *message) IsInterfaceNil() bool {
return msg == nil
}