-
Notifications
You must be signed in to change notification settings - Fork 211
/
data.go
206 lines (184 loc) · 4.43 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
package atxsdata
import (
"sync"
"sync/atomic"
"github.com/spacemeshos/go-spacemesh/common/types"
)
// minCapacity is set to 2 epochs because we are using data from current epoch for tortoise
// and at the start of current epoch we still need data from previous epoch for hare oracle.
const minCapacity = 2
type ATX struct {
Node types.NodeID
Coinbase types.Address
Weight uint64
BaseHeight, Height uint64
Nonce types.VRFPostIndex
Malicious bool
}
type Opt func(*Data)
// WithCapacity sets the number of epochs from the latest applied
// that cache will maintain in memory.
func WithCapacity(capacity types.EpochID) Opt {
return func(cache *Data) {
cache.capacity = max(minCapacity, capacity)
}
}
// WithCapacityFromLayers sets capacity to include all layers in the window.
func WithCapacityFromLayers(window, epochSize uint32) Opt {
capacity := window / epochSize
if window%epochSize != 0 {
capacity++
}
return WithCapacity(types.EpochID(capacity))
}
func New(opts ...Opt) *Data {
cache := &Data{
capacity: 2,
malicious: map[types.NodeID]struct{}{},
epochs: map[types.EpochID]epochCache{},
}
for _, opt := range opts {
opt(cache)
}
return cache
}
type Data struct {
evicted atomic.Uint32
// number of epochs to keep
// capacity is not enforced by the cache itself
capacity types.EpochID
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()
}
// OnEpoch is a notification for cache to evict epochs that are not useful
// to keep in memory.
func (d *Data) OnEpoch(applied types.EpochID) {
d.mu.Lock()
defer d.mu.Unlock()
if applied < d.capacity {
return
}
evict := applied - d.capacity
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.
func (d *Data) AddFromHeader(atx *types.ActivationTxHeader, nonce types.VRFPostIndex, malicious bool) {
d.Add(
atx.TargetEpoch(),
atx.NodeID,
atx.Coinbase,
atx.ID,
atx.GetWeight(),
atx.BaseTickHeight,
atx.TickHeight(),
nonce,
malicious,
)
}
func (d *Data) Add(
epoch types.EpochID,
node types.NodeID,
coinbase types.Address,
atx types.ATXID,
weight, baseHeight, height uint64,
nonce types.VRFPostIndex,
malicious bool,
) {
d.mu.Lock()
defer d.mu.Unlock()
if d.IsEvicted(epoch) {
return
}
ecache, exists := d.epochs[epoch]
if !exists {
ecache = epochCache{
index: map[types.ATXID]*ATX{},
}
d.epochs[epoch] = ecache
}
_, exists = ecache.index[atx]
if !exists {
atxsCounter.WithLabelValues(epoch.String()).Inc()
}
data := &ATX{
Node: node,
Coinbase: coinbase,
Weight: weight,
BaseHeight: baseHeight,
Height: height,
Nonce: nonce,
Malicious: malicious,
}
ecache.index[atx] = data
if data.Malicious {
d.malicious[node] = struct{}{}
}
}
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.
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
}
// 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
}