-
Notifications
You must be signed in to change notification settings - Fork 199
/
peerTypeProvider.go
152 lines (126 loc) · 4.33 KB
/
peerTypeProvider.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
package peer
import (
"sync"
"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/ElrondNetwork/elrond-go-core/data"
"github.com/ElrondNetwork/elrond-go/common"
"github.com/ElrondNetwork/elrond-go/epochStart/notifier"
"github.com/ElrondNetwork/elrond-go/process"
"github.com/ElrondNetwork/elrond-go/sharding"
"github.com/ElrondNetwork/elrond-go/state"
)
type peerListAndShard struct {
pType common.PeerType
pSubType core.P2PPeerSubType
pShard uint32
}
// PeerTypeProvider handles the computation of a peer type
type PeerTypeProvider struct {
nodesCoordinator process.NodesCoordinator
cache map[string]*peerListAndShard
mutCache sync.RWMutex
}
// ArgPeerTypeProvider contains all parameters needed for creating a PeerTypeProvider
type ArgPeerTypeProvider struct {
NodesCoordinator process.NodesCoordinator
StartEpoch uint32
EpochStartEventNotifier process.EpochStartEventNotifier
}
// NewPeerTypeProvider will return a new instance of PeerTypeProvider
func NewPeerTypeProvider(arg ArgPeerTypeProvider) (*PeerTypeProvider, error) {
if check.IfNil(arg.NodesCoordinator) {
return nil, process.ErrNilNodesCoordinator
}
if check.IfNil(arg.EpochStartEventNotifier) {
return nil, process.ErrNilEpochStartNotifier
}
ptp := &PeerTypeProvider{
nodesCoordinator: arg.NodesCoordinator,
cache: make(map[string]*peerListAndShard),
mutCache: sync.RWMutex{},
}
ptp.updateCache(arg.StartEpoch)
arg.EpochStartEventNotifier.RegisterHandler(ptp.epochStartEventHandler())
return ptp, nil
}
// ComputeForPubKey returns the peer type for a given public key and shard id
func (ptp *PeerTypeProvider) ComputeForPubKey(pubKey []byte) (common.PeerType, uint32, error) {
ptp.mutCache.RLock()
peerData, ok := ptp.cache[string(pubKey)]
ptp.mutCache.RUnlock()
if ok {
return peerData.pType, peerData.pShard, nil
}
return common.ObserverList, 0, nil
}
// GetAllPeerTypeInfos returns all known peer type infos
func (ptp *PeerTypeProvider) GetAllPeerTypeInfos() []*state.PeerTypeInfo {
ptp.mutCache.RLock()
defer ptp.mutCache.RUnlock()
peerTypeInfos := make([]*state.PeerTypeInfo, 0, len(ptp.cache))
for pkString, peerListAndShardVal := range ptp.cache {
peerTypeInfos = append(peerTypeInfos, &state.PeerTypeInfo{
PublicKey: pkString,
PeerType: string(peerListAndShardVal.pType),
PeerSubType: peerListAndShardVal.pSubType,
ShardId: peerListAndShardVal.pShard,
})
}
return peerTypeInfos
}
func (ptp *PeerTypeProvider) epochStartEventHandler() sharding.EpochStartActionHandler {
subscribeHandler := notifier.NewHandlerForEpochStart(
func(hdr data.HeaderHandler) {
log.Trace("epochStartEventHandler - refreshCache forced",
"nonce", hdr.GetNonce(),
"shard", hdr.GetShardID(),
"round", hdr.GetRound(),
"epoch", hdr.GetEpoch())
ptp.updateCache(hdr.GetEpoch())
},
func(_ data.HeaderHandler) {},
common.IndexerOrder,
)
return subscribeHandler
}
func (ptp *PeerTypeProvider) updateCache(epoch uint32) {
newCache := ptp.createNewCache(epoch)
ptp.mutCache.Lock()
ptp.cache = newCache
ptp.mutCache.Unlock()
}
func (ptp *PeerTypeProvider) createNewCache(
epoch uint32,
) map[string]*peerListAndShard {
newCache := make(map[string]*peerListAndShard)
nodesMapEligible, err := ptp.nodesCoordinator.GetAllEligibleValidatorsPublicKeys(epoch)
if err != nil {
log.Debug("peerTypeProvider - GetAllEligibleValidatorsPublicKeys failed", "epoch", epoch)
}
computePeerTypeAndShardId(newCache, nodesMapEligible, common.EligibleList)
nodesMapWaiting, err := ptp.nodesCoordinator.GetAllWaitingValidatorsPublicKeys(epoch)
if err != nil {
log.Debug("peerTypeProvider - GetAllWaitingValidatorsPublicKeys failed", "epoch", epoch)
}
computePeerTypeAndShardId(newCache, nodesMapWaiting, common.WaitingList)
return newCache
}
func computePeerTypeAndShardId(
newCache map[string]*peerListAndShard,
validatorsMap map[uint32][][]byte,
currentPeerType common.PeerType,
) {
for shardID, shardValidators := range validatorsMap {
for _, val := range shardValidators {
newCache[string(val)] = &peerListAndShard{
pType: currentPeerType,
pShard: shardID,
}
}
}
}
// IsInterfaceNil returns true if there is no value under the interface
func (ptp *PeerTypeProvider) IsInterfaceNil() bool {
return ptp == nil
}