-
Notifications
You must be signed in to change notification settings - Fork 199
/
singleDataInterceptor.go
144 lines (123 loc) · 4.19 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
package interceptors
import (
"sync"
"github.com/ElrondNetwork/elrond-go/core/check"
"github.com/ElrondNetwork/elrond-go/debug/resolver"
"github.com/ElrondNetwork/elrond-go/p2p"
"github.com/ElrondNetwork/elrond-go/process"
)
// SingleDataInterceptor is used for intercepting packed multi data
type SingleDataInterceptor struct {
topic string
factory process.InterceptedDataFactory
processor process.InterceptorProcessor
throttler process.InterceptorThrottler
whiteListRequested process.WhiteListHandler
antifloodHandler process.P2PAntifloodHandler
mutInterceptedDebugHandler sync.RWMutex
interceptedDebugHandler process.InterceptedDebugHandler
}
// NewSingleDataInterceptor hooks a new interceptor for single data
func NewSingleDataInterceptor(
topic string,
factory process.InterceptedDataFactory,
processor process.InterceptorProcessor,
throttler process.InterceptorThrottler,
antifloodHandler process.P2PAntifloodHandler,
whiteListRequested process.WhiteListHandler,
) (*SingleDataInterceptor, error) {
if len(topic) == 0 {
return nil, process.ErrEmptyTopic
}
if check.IfNil(factory) {
return nil, process.ErrNilInterceptedDataFactory
}
if check.IfNil(processor) {
return nil, process.ErrNilInterceptedDataProcessor
}
if check.IfNil(throttler) {
return nil, process.ErrNilInterceptorThrottler
}
if check.IfNil(antifloodHandler) {
return nil, process.ErrNilAntifloodHandler
}
if check.IfNil(whiteListRequested) {
return nil, process.ErrNilWhiteListHandler
}
singleDataIntercept := &SingleDataInterceptor{
topic: topic,
factory: factory,
processor: processor,
throttler: throttler,
antifloodHandler: antifloodHandler,
whiteListRequested: whiteListRequested,
}
singleDataIntercept.interceptedDebugHandler = resolver.NewDisabledInterceptorResolver()
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 p2p.PeerID) error {
sdi.mutInterceptedDebugHandler.RLock()
defer sdi.mutInterceptedDebugHandler.RUnlock()
err := preProcessMesage(sdi.throttler, sdi.antifloodHandler, message, fromConnectedPeer, sdi.topic)
if err != nil {
return err
}
interceptedData, err := sdi.factory.Create(message.Data())
if err != nil {
sdi.throttler.EndProcessing()
return err
}
receivedDebugInterceptedData(sdi.interceptedDebugHandler, interceptedData, sdi.topic)
err = interceptedData.CheckValidity()
if err != nil {
sdi.throttler.EndProcessing()
processDebugInterceptedData(sdi.interceptedDebugHandler, interceptedData, sdi.topic, err)
return err
}
isForCurrentShard := interceptedData.IsForCurrentShard()
isWhiteListed := sdi.whiteListRequested.IsWhiteListed(interceptedData)
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),
"topics", message.Topics(),
"hash", interceptedData.Hash(),
"is for current shard", isForCurrentShard,
"is white listed", isWhiteListed,
)
return nil
}
wgProcess := &sync.WaitGroup{}
wgProcess.Add(1)
go func() {
wgProcess.Wait()
sdi.throttler.EndProcessing()
}()
go processInterceptedData(
sdi.processor,
sdi.interceptedDebugHandler,
interceptedData,
sdi.topic,
wgProcess,
message,
)
return nil
}
// SetInterceptedDebugHandler will set a new intercepted debug handler
func (sdi *SingleDataInterceptor) SetInterceptedDebugHandler(handler process.InterceptedDebugHandler) error {
if check.IfNil(handler) {
return process.ErrNilInterceptedDebugHandler
}
sdi.mutInterceptedDebugHandler.Lock()
sdi.interceptedDebugHandler = handler
sdi.mutInterceptedDebugHandler.Unlock()
return nil
}
// IsInterfaceNil returns true if there is no value under the interface
func (sdi *SingleDataInterceptor) IsInterfaceNil() bool {
return sdi == nil
}