-
Notifications
You must be signed in to change notification settings - Fork 199
/
singleDataInterceptor.go
163 lines (140 loc) · 5.47 KB
/
singleDataInterceptor.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package interceptors
import (
"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/ElrondNetwork/elrond-go/common"
"github.com/ElrondNetwork/elrond-go/debug/resolver"
"github.com/ElrondNetwork/elrond-go/p2p"
"github.com/ElrondNetwork/elrond-go/process"
)
// ArgSingleDataInterceptor is the argument for the single-data interceptor
type ArgSingleDataInterceptor struct {
Topic string
DataFactory process.InterceptedDataFactory
Processor process.InterceptorProcessor
Throttler process.InterceptorThrottler
AntifloodHandler process.P2PAntifloodHandler
WhiteListRequest process.WhiteListHandler
PreferredPeersHolder process.PreferredPeersHolderHandler
CurrentPeerId core.PeerID
}
// SingleDataInterceptor is used for intercepting packed multi data
type SingleDataInterceptor struct {
*baseDataInterceptor
factory process.InterceptedDataFactory
whiteListRequest process.WhiteListHandler
}
// NewSingleDataInterceptor hooks a new interceptor for single data
func NewSingleDataInterceptor(arg ArgSingleDataInterceptor) (*SingleDataInterceptor, error) {
if len(arg.Topic) == 0 {
return nil, process.ErrEmptyTopic
}
if check.IfNil(arg.DataFactory) {
return nil, process.ErrNilInterceptedDataFactory
}
if check.IfNil(arg.Processor) {
return nil, process.ErrNilInterceptedDataProcessor
}
if check.IfNil(arg.Throttler) {
return nil, process.ErrNilInterceptorThrottler
}
if check.IfNil(arg.AntifloodHandler) {
return nil, process.ErrNilAntifloodHandler
}
if check.IfNil(arg.WhiteListRequest) {
return nil, process.ErrNilWhiteListHandler
}
if check.IfNil(arg.PreferredPeersHolder) {
return nil, process.ErrNilPreferredPeersHolder
}
if len(arg.CurrentPeerId) == 0 {
return nil, process.ErrEmptyPeerID
}
singleDataIntercept := &SingleDataInterceptor{
baseDataInterceptor: &baseDataInterceptor{
throttler: arg.Throttler,
antifloodHandler: arg.AntifloodHandler,
topic: arg.Topic,
currentPeerId: arg.CurrentPeerId,
processor: arg.Processor,
preferredPeersHolder: arg.PreferredPeersHolder,
debugHandler: resolver.NewDisabledInterceptorResolver(),
},
factory: arg.DataFactory,
whiteListRequest: arg.WhiteListRequest,
}
return singleDataIntercept, nil
}
// ProcessReceivedMessage is the callback func from the p2p.Messenger and will be called each time a new message was received
// (for the topic this validator was registered to)
func (sdi *SingleDataInterceptor) ProcessReceivedMessage(message p2p.MessageP2P, fromConnectedPeer core.PeerID) error {
sdi.mutDebugHandler.RLock()
defer sdi.mutDebugHandler.RUnlock()
err := sdi.preProcessMesage(message, fromConnectedPeer)
if err != nil {
return err
}
interceptedData, err := sdi.factory.Create(message.Data())
if err != nil {
sdi.throttler.EndProcessing()
//this situation is so severe that we need to black list the peers
reason := "can not create object from received bytes, topic " + sdi.topic + ", error " + err.Error()
sdi.antifloodHandler.BlacklistPeer(message.Peer(), reason, common.InvalidMessageBlacklistDuration)
sdi.antifloodHandler.BlacklistPeer(fromConnectedPeer, reason, common.InvalidMessageBlacklistDuration)
return err
}
sdi.receivedDebugInterceptedData(interceptedData)
err = interceptedData.CheckValidity()
if err != nil {
sdi.throttler.EndProcessing()
sdi.processDebugInterceptedData(interceptedData, err)
isWrongVersion := err == process.ErrInvalidTransactionVersion || err == process.ErrInvalidChainID
if isWrongVersion {
//this situation is so severe that we need to black list de peers
reason := "wrong version of received intercepted data, topic " + sdi.topic + ", error " + err.Error()
sdi.antifloodHandler.BlacklistPeer(message.Peer(), reason, common.InvalidMessageBlacklistDuration)
sdi.antifloodHandler.BlacklistPeer(fromConnectedPeer, reason, common.InvalidMessageBlacklistDuration)
}
return err
}
errOriginator := sdi.antifloodHandler.IsOriginatorEligibleForTopic(message.Peer(), sdi.topic)
isWhiteListed := sdi.whiteListRequest.IsWhiteListed(interceptedData)
if !isWhiteListed && errOriginator != nil {
log.Trace("got message from peer on topic only for validators",
"originator", p2p.PeerIdToShortString(message.Peer()), "topic",
sdi.topic, "err", errOriginator)
sdi.throttler.EndProcessing()
return errOriginator
}
isForCurrentShard := interceptedData.IsForCurrentShard()
shouldProcess := isForCurrentShard || isWhiteListed
if !shouldProcess {
sdi.throttler.EndProcessing()
log.Trace("intercepted data is for other shards",
"pid", p2p.MessageOriginatorPid(message),
"seq no", p2p.MessageOriginatorSeq(message),
"topic", message.Topic(),
"hash", interceptedData.Hash(),
"is for current shard", isForCurrentShard,
"is white listed", isWhiteListed,
)
return nil
}
go func() {
sdi.processInterceptedData(interceptedData, message)
sdi.throttler.EndProcessing()
}()
return nil
}
// RegisterHandler registers a callback function to be notified on received data
func (sdi *SingleDataInterceptor) RegisterHandler(handler func(topic string, hash []byte, data interface{})) {
sdi.processor.RegisterHandler(handler)
}
// Close returns nil
func (sdi *SingleDataInterceptor) Close() error {
return nil
}
// IsInterfaceNil returns true if there is no value under the interface
func (sdi *SingleDataInterceptor) IsInterfaceNil() bool {
return sdi == nil
}