-
Notifications
You must be signed in to change notification settings - Fork 197
/
peerAuthenticationInterceptorProcessor.go
111 lines (93 loc) · 4.12 KB
/
peerAuthenticationInterceptorProcessor.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
package processor
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/heartbeat"
"github.com/ElrondNetwork/elrond-go/process"
"github.com/ElrondNetwork/elrond-go/storage"
)
// ArgPeerAuthenticationInterceptorProcessor is the argument for the interceptor processor used for peer authentication
type ArgPeerAuthenticationInterceptorProcessor struct {
PeerAuthenticationCacher storage.Cacher
PeerShardMapper process.PeerShardMapper
Marshaller marshal.Marshalizer
HardforkTrigger heartbeat.HardforkTrigger
}
// peerAuthenticationInterceptorProcessor is the processor used when intercepting peer authentication
type peerAuthenticationInterceptorProcessor struct {
peerAuthenticationCacher storage.Cacher
peerShardMapper process.PeerShardMapper
marshaller marshal.Marshalizer
hardforkTrigger heartbeat.HardforkTrigger
}
// NewPeerAuthenticationInterceptorProcessor creates a new peerAuthenticationInterceptorProcessor
func NewPeerAuthenticationInterceptorProcessor(args ArgPeerAuthenticationInterceptorProcessor) (*peerAuthenticationInterceptorProcessor, error) {
err := checkArgsPeerAuthentication(args)
if err != nil {
return nil, err
}
return &peerAuthenticationInterceptorProcessor{
peerAuthenticationCacher: args.PeerAuthenticationCacher,
peerShardMapper: args.PeerShardMapper,
marshaller: args.Marshaller,
hardforkTrigger: args.HardforkTrigger,
}, nil
}
func checkArgsPeerAuthentication(args ArgPeerAuthenticationInterceptorProcessor) error {
if check.IfNil(args.PeerAuthenticationCacher) {
return process.ErrNilPeerAuthenticationCacher
}
if check.IfNil(args.PeerShardMapper) {
return process.ErrNilPeerShardMapper
}
if check.IfNil(args.Marshaller) {
return heartbeat.ErrNilMarshaller
}
if check.IfNil(args.HardforkTrigger) {
return heartbeat.ErrNilHardforkTrigger
}
return nil
}
// Validate checks if the intercepted data can be processed
// returns nil as proper validity checks are done at intercepted data level
func (paip *peerAuthenticationInterceptorProcessor) Validate(_ process.InterceptedData, _ core.PeerID) error {
return nil
}
// Save will save the intercepted peer authentication inside the peer authentication cacher
func (paip *peerAuthenticationInterceptorProcessor) Save(data process.InterceptedData, _ core.PeerID, _ string) error {
interceptedPeerAuthenticationData, ok := data.(interceptedPeerAuthenticationMessageHandler)
if !ok {
return process.ErrWrongTypeAssertion
}
payloadBuff := interceptedPeerAuthenticationData.Payload()
payload := &heartbeat.Payload{}
err := paip.marshaller.Unmarshal(payload, payloadBuff)
if err != nil {
return err
}
isHardforkTrigger, err := paip.hardforkTrigger.TriggerReceived(nil, []byte(payload.HardforkMessage), interceptedPeerAuthenticationData.Pubkey())
if isHardforkTrigger {
return err
}
return paip.updatePeerInfo(interceptedPeerAuthenticationData.Message(), interceptedPeerAuthenticationData.SizeInBytes())
}
func (paip *peerAuthenticationInterceptorProcessor) updatePeerInfo(message interface{}, messageSize int) error {
peerAuthenticationData, ok := message.(*heartbeat.PeerAuthentication)
if !ok {
return process.ErrWrongTypeAssertion
}
pidBytes := peerAuthenticationData.GetPid()
paip.peerAuthenticationCacher.Put(peerAuthenticationData.Pubkey, message, messageSize)
paip.peerShardMapper.UpdatePeerIDPublicKeyPair(core.PeerID(pidBytes), peerAuthenticationData.GetPubkey())
log.Trace("PeerAuthentication message saved")
return nil
}
// RegisterHandler registers a callback function to be notified of incoming peer authentication
func (paip *peerAuthenticationInterceptorProcessor) RegisterHandler(_ func(topic string, hash []byte, data interface{})) {
log.Error("peerAuthenticationInterceptorProcessor.RegisterHandler", "error", "not implemented")
}
// IsInterfaceNil returns true if there is no value under the interface
func (paip *peerAuthenticationInterceptorProcessor) IsInterfaceNil() bool {
return paip == nil
}