-
Notifications
You must be signed in to change notification settings - Fork 199
/
messageProcessor.go
79 lines (67 loc) · 2.42 KB
/
messageProcessor.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
package process
import (
"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/ElrondNetwork/elrond-go-core/marshal"
"github.com/ElrondNetwork/elrond-go-crypto"
"github.com/ElrondNetwork/elrond-go/heartbeat"
"github.com/ElrondNetwork/elrond-go/heartbeat/data"
"github.com/ElrondNetwork/elrond-go/p2p"
)
// MessageProcessor is the struct that will handle heartbeat message verifications and conversion between
// heartbeatMessageInfo and HeartbeatDTO
type MessageProcessor struct {
peerSignatureHandler crypto.PeerSignatureHandler
marshalizer marshal.Marshalizer
networkShardingCollector heartbeat.NetworkShardingCollector
}
// NewMessageProcessor will return a new instance of MessageProcessor
func NewMessageProcessor(
peerSignatureHandler crypto.PeerSignatureHandler,
marshalizer marshal.Marshalizer,
networkShardingCollector heartbeat.NetworkShardingCollector,
) (*MessageProcessor, error) {
if check.IfNil(peerSignatureHandler) {
return nil, heartbeat.ErrNilPeerSignatureHandler
}
if check.IfNil(marshalizer) {
return nil, heartbeat.ErrNilMarshaller
}
if check.IfNil(networkShardingCollector) {
return nil, heartbeat.ErrNilNetworkShardingCollector
}
return &MessageProcessor{
peerSignatureHandler: peerSignatureHandler,
marshalizer: marshalizer,
networkShardingCollector: networkShardingCollector,
}, nil
}
// CreateHeartbeatFromP2PMessage will return a heartbeat if all the checks pass
func (mp *MessageProcessor) CreateHeartbeatFromP2PMessage(message p2p.MessageP2P) (*data.Heartbeat, error) {
if check.IfNil(message) {
return nil, heartbeat.ErrNilMessage
}
if message.Data() == nil {
return nil, heartbeat.ErrNilDataToProcess
}
hbRecv := &data.Heartbeat{}
err := mp.marshalizer.Unmarshal(hbRecv, message.Data())
if err != nil {
return nil, err
}
err = verifyLengths(hbRecv)
if err != nil {
return nil, err
}
err = mp.peerSignatureHandler.VerifyPeerSignature(hbRecv.Pubkey, core.PeerID(hbRecv.Pid), hbRecv.Signature)
if err != nil {
return nil, err
}
mp.networkShardingCollector.UpdatePeerIDInfo(message.Peer(), hbRecv.Pubkey, hbRecv.ShardID)
mp.networkShardingCollector.PutPeerIdSubType(message.Peer(), core.P2PPeerSubType(hbRecv.PeerSubType))
return hbRecv, nil
}
// IsInterfaceNil returns true if there is no value under the interface
func (mp *MessageProcessor) IsInterfaceNil() bool {
return mp == nil
}