-
Notifications
You must be signed in to change notification settings - Fork 38
/
calls.go
166 lines (123 loc) · 2.96 KB
/
calls.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
164
165
166
package daughters
import (
"sync"
"github.com/nspcc-dev/neofs-node/pkg/services/reputation"
eigentrustcalc "github.com/nspcc-dev/neofs-node/pkg/services/reputation/eigentrust/calculator"
)
// Put saves daughter peer's trust to its provider for the epoch.
func (x *Storage) Put(epoch uint64, trust reputation.Trust) {
var s *DaughterStorage
x.mtx.Lock()
{
s = x.mItems[epoch]
if s == nil {
s = &DaughterStorage{
mItems: make(map[reputation.PeerID]*DaughterTrusts, 1),
}
x.mItems[epoch] = s
}
}
x.mtx.Unlock()
s.put(trust)
}
// DaughterTrusts returns daughter trusts for the epoch.
//
// Returns false if there is no data for the epoch and daughter.
func (x *Storage) DaughterTrusts(epoch uint64, daughter reputation.PeerID) (*DaughterTrusts, bool) {
var (
s *DaughterStorage
ok bool
)
x.mtx.RLock()
{
s, ok = x.mItems[epoch]
}
x.mtx.RUnlock()
if !ok {
return nil, false
}
return s.daughterTrusts(daughter)
}
// AllDaughterTrusts returns daughter iterator for the epoch.
//
// Returns false if there is no data for the epoch and daughter.
func (x *Storage) AllDaughterTrusts(epoch uint64) (*DaughterStorage, bool) {
x.mtx.RLock()
defer x.mtx.RUnlock()
s, ok := x.mItems[epoch]
return s, ok
}
// DaughterStorage maps IDs of daughter peers to repositories of the local trusts to their providers.
type DaughterStorage struct {
mtx sync.RWMutex
mItems map[reputation.PeerID]*DaughterTrusts
}
// Iterate passes IDs of the daughter peers with their trusts to h.
//
// Returns errors from h directly.
func (x *DaughterStorage) Iterate(h eigentrustcalc.PeerTrustsHandler) (err error) {
x.mtx.RLock()
{
for daughter, daughterTrusts := range x.mItems {
if err = h(daughter, daughterTrusts); err != nil {
break
}
}
}
x.mtx.RUnlock()
return
}
func (x *DaughterStorage) put(trust reputation.Trust) {
var dt *DaughterTrusts
x.mtx.Lock()
{
trusting := trust.TrustingPeer()
dt = x.mItems[trusting]
if dt == nil {
dt = &DaughterTrusts{
mItems: make(map[reputation.PeerID]reputation.Trust, 1),
}
x.mItems[trusting] = dt
}
}
x.mtx.Unlock()
dt.put(trust)
}
func (x *DaughterStorage) daughterTrusts(id reputation.PeerID) (dt *DaughterTrusts, ok bool) {
x.mtx.RLock()
{
dt, ok = x.mItems[id]
}
x.mtx.RUnlock()
return
}
// DaughterTrusts represents in-memory storage of local trusts
// of the daughter peer to its providers.
//
// Maps IDs of daughter's providers to the local trusts to them.
type DaughterTrusts struct {
mtx sync.RWMutex
mItems map[reputation.PeerID]reputation.Trust
}
func (x *DaughterTrusts) put(trust reputation.Trust) {
x.mtx.Lock()
{
x.mItems[trust.Peer()] = trust
}
x.mtx.Unlock()
}
// Iterate passes all stored trusts to h.
//
// Returns errors from h directly.
func (x *DaughterTrusts) Iterate(h reputation.TrustHandler) (err error) {
x.mtx.RLock()
{
for _, trust := range x.mItems {
if err = h(trust); err != nil {
break
}
}
}
x.mtx.RUnlock()
return
}