-
Notifications
You must be signed in to change notification settings - Fork 4
/
accessor.go
88 lines (74 loc) · 2.49 KB
/
accessor.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
package memstor
import (
"github.com/insolar/assured-ledger/ledger-core/network/consensus/common/endpoints"
"github.com/insolar/assured-ledger/ledger-core/network/consensus/gcpv2/api/census"
"github.com/insolar/assured-ledger/ledger-core/network/consensus/gcpv2/api/member"
"github.com/insolar/assured-ledger/ledger-core/network/consensus/gcpv2/api/profiles"
"github.com/insolar/assured-ledger/ledger-core/network/nodeinfo"
"github.com/insolar/assured-ledger/ledger-core/pulse"
"github.com/insolar/assured-ledger/ledger-core/reference"
)
type Accessor struct {
snapshot *Snapshot
refIndex map[reference.Global]member.Index
addrIndex map[string]member.Index
}
func (a *Accessor) GetPopulation() census.OnlinePopulation {
return a.snapshot.population
}
func (a *Accessor) GetPulseNumber() pulse.Number {
return a.snapshot.GetPulseNumber()
}
func (a *Accessor) FindNodeByAddr(address string) profiles.ActiveNode {
idx, ok := a.addrIndex[address]
if !ok {
return nil
}
// NB! local node is not allowed to address itself
return a.snapshot.population.GetProfile(idx)
}
func (a *Accessor) FindNodeByRef(ref reference.Global) profiles.ActiveNode {
idx, ok := a.refIndex[ref]
if !ok {
return nil
}
n := a.snapshot.population.GetProfile(idx)
if n == nil && idx == 0 {
return a.snapshot.population.GetLocalProfile()
}
return n
}
func (a *Accessor) addAddr(endpoint endpoints.Outbound, idx member.Index) {
a.addrIndex[endpoint.GetNameAddress().String()] = idx
}
func NewAccessor(snapshot *Snapshot) *Accessor {
result := &Accessor{
snapshot: snapshot,
}
local := snapshot.population.GetLocalProfile()
profiles := snapshot.population.GetProfiles()
n := len(profiles)
if n <= 1 {
// this is a special case for both joiner (before consensus local node is not listed) and one node population
localRef := nodeinfo.NodeRef(local)
result.refIndex = map[reference.Global]member.Index{localRef:0}
return result
}
result.refIndex = make(map[reference.Global]member.Index, n)
result.addrIndex = make(map[string]member.Index, n - 1) // local node is not allowed to address itself
for _, node := range snapshot.population.GetProfiles() {
ref := nodeinfo.NodeRef(node)
idx := node.GetIndex()
result.refIndex[ref] = idx
if local.GetIndex() == idx {
// local node is not allowed to address itself
continue
}
ns := node.GetStatic()
result.addAddr(ns.GetDefaultEndpoint(), idx)
for _, na := range ns.GetExtension().GetExtraEndpoints() {
result.addAddr(na, idx)
}
}
return result
}