-
Notifications
You must be signed in to change notification settings - Fork 211
/
data.go
243 lines (217 loc) · 5.54 KB
/
data.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package atxsdata
import (
"sync"
"sync/atomic"
"github.com/spacemeshos/go-spacemesh/common/types"
)
// SAFETY: all exported fields are read-only and are safe to read concurrently.
// Thanks to the fact that ATX is immutable, it is safe to return a pointer to it.
type ATX struct {
Node types.NodeID
Coinbase types.Address
Weight uint64
BaseHeight, Height uint64
Nonce types.VRFPostIndex
// unexported to avoid accidental unsynchronized access
// (this field is mutated by the Data under a lock and
// might only be safely read under the same lock)
malicious bool
}
func New() *Data {
return &Data{
malicious: map[types.NodeID]struct{}{},
epochs: map[types.EpochID]epochCache{},
}
}
type Data struct {
evicted atomic.Uint32
mu sync.RWMutex
malicious map[types.NodeID]struct{}
epochs map[types.EpochID]epochCache
}
type epochCache struct {
index map[types.ATXID]*ATX
}
func (d *Data) Evicted() types.EpochID {
return types.EpochID(d.evicted.Load())
}
func (d *Data) IsEvicted(epoch types.EpochID) bool {
return d.evicted.Load() >= epoch.Uint32()
}
// EvictEpoch is a notification for cache to evict epochs that are not useful
// to keep in memory.
func (d *Data) EvictEpoch(evict types.EpochID) {
d.mu.Lock()
defer d.mu.Unlock()
if d.IsEvicted(evict) {
return
}
if d.evicted.Load() < evict.Uint32() {
d.evicted.Store(evict.Uint32())
}
for epoch := range d.epochs {
if epoch <= evict {
delete(d.epochs, epoch)
atxsCounter.DeleteLabelValues(epoch.String())
}
}
}
// AddFromVerified extracts relevant fields from verified atx and adds them together with nonce and malicious flag.
// Returns the ATX that was added to the store (if any) or `nil` if it wasn't.
func (d *Data) AddFromHeader(atx *types.ActivationTxHeader, nonce types.VRFPostIndex, malicious bool) *ATX {
return d.Add(
atx.TargetEpoch(),
atx.NodeID,
atx.Coinbase,
atx.ID,
atx.GetWeight(),
atx.BaseTickHeight,
atx.TickHeight(),
nonce,
malicious,
)
}
// Add adds ATX data to the store.
// Returns whether the ATX was added to the store.
func (d *Data) AddAtx(target types.EpochID, id types.ATXID, atx *ATX) bool {
d.mu.Lock()
defer d.mu.Unlock()
if d.IsEvicted(target) {
return false
}
ecache, exists := d.epochs[target]
if !exists {
ecache = epochCache{
index: map[types.ATXID]*ATX{},
}
d.epochs[target] = ecache
}
if _, exists = ecache.index[id]; exists {
return false
}
atxsCounter.WithLabelValues(target.String()).Inc()
ecache.index[id] = atx
if atx.malicious {
d.malicious[atx.Node] = struct{}{}
}
return true
}
// Add adds ATX data to the store.
// Returns the ATX that was added to the store (if any) or `nil` if it wasn't.
func (d *Data) Add(
epoch types.EpochID,
node types.NodeID,
coinbase types.Address,
atxid types.ATXID,
weight, baseHeight, height uint64,
nonce types.VRFPostIndex,
malicious bool,
) *ATX {
atx := &ATX{
Node: node,
Coinbase: coinbase,
Weight: weight,
BaseHeight: baseHeight,
Height: height,
Nonce: nonce,
malicious: malicious,
}
if d.AddAtx(epoch, atxid, atx) {
return atx
}
return nil
}
func (d *Data) IsMalicious(node types.NodeID) bool {
d.mu.RLock()
defer d.mu.RUnlock()
_, exists := d.malicious[node]
return exists
}
func (d *Data) SetMalicious(node types.NodeID) {
d.mu.Lock()
defer d.mu.Unlock()
d.malicious[node] = struct{}{}
}
// Get returns atx data.
// SAFETY: The returned pointer MUST NOT be modified.
func (d *Data) Get(epoch types.EpochID, atx types.ATXID) *ATX {
d.mu.RLock()
defer d.mu.RUnlock()
ecache, exists := d.epochs[epoch]
if !exists {
return nil
}
data, exists := ecache.index[atx]
if !exists {
return nil
}
_, exists = d.malicious[data.Node]
data.malicious = exists
return data
}
type lockGuard struct{}
// AtxFilter is a function that filters atxs.
// The `lockGuard` prevents using the filter functions outside of the allowed context
// to prevent data races.
type AtxFilter func(*ATX, lockGuard) bool
func NotMalicious(data *ATX, _ lockGuard) bool {
return !data.malicious
}
// IterateInEpoch calls `fn` for every ATX in epoch.
// If filters are provided, only atxs that pass all filters are returned.
// SAFETY: The returned pointer MUST NOT be modified.
func (d *Data) IterateInEpoch(epoch types.EpochID, fn func(types.ATXID, *ATX), filters ...AtxFilter) {
d.mu.RLock()
defer d.mu.RUnlock()
ecache, exists := d.epochs[epoch]
if !exists {
return
}
for id, atx := range ecache.index {
if _, exists := d.malicious[atx.Node]; exists {
atx.malicious = true
}
ok := true
for _, filter := range filters {
ok = ok && filter(atx, lockGuard{})
}
if ok {
fn(id, atx)
}
}
}
func (d *Data) MissingInEpoch(epoch types.EpochID, atxs []types.ATXID) []types.ATXID {
d.mu.RLock()
defer d.mu.RUnlock()
ecache, exists := d.epochs[epoch]
if !exists {
return atxs
}
var missing []types.ATXID
for _, id := range atxs {
if _, exists := ecache.index[id]; !exists {
missing = append(missing, id)
}
}
return missing
}
// WeightForSet computes total weight of atxs in the set and returned array with
// atxs in the set that weren't used.
func (d *Data) WeightForSet(epoch types.EpochID, set []types.ATXID) (uint64, []bool) {
d.mu.RLock()
defer d.mu.RUnlock()
ecache, exists := d.epochs[epoch]
// TODO(dshulyak) bitfield is a perfect fit here
used := make([]bool, len(set))
if !exists {
return 0, used
}
var weight uint64
for i, id := range set {
if data, exists := ecache.index[id]; exists {
weight += data.Weight
used[i] = true
}
}
return weight, used
}