-
Notifications
You must be signed in to change notification settings - Fork 210
/
oracle.go
516 lines (454 loc) · 17.5 KB
/
oracle.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
package eligibility
import (
"context"
"errors"
"fmt"
"sync"
lru "github.com/hashicorp/golang-lru"
"github.com/spacemeshos/fixed"
"github.com/spacemeshos/go-spacemesh/common/types"
eCfg "github.com/spacemeshos/go-spacemesh/hare/eligibility/config"
"github.com/spacemeshos/go-spacemesh/log"
)
const (
vrfMsgCacheSize = 20 // numRounds per layer is <= 2. numConcurrentLayers<=10 (typically <=2) so numRounds*numConcurrentLayers <= 2*10 = 20 is a good upper bound
activesCacheSize = 5 // we don't expect to handle more than two layers concurrently
maxSupportedN = 1073741824 // higher values result in an overflow
)
type valueProvider interface {
Value(context.Context, types.EpochID) (uint32, error)
}
type signer interface {
Sign(msg []byte) []byte
}
type addGet interface {
Add(key, value interface{}) (evicted bool)
Get(key interface{}) (value interface{}, ok bool)
}
type atxProvider interface {
// GetMinerWeightsInEpochFromView gets the active set (node IDs) for a set of blocks
GetMinerWeightsInEpochFromView(targetEpoch types.EpochID, blocks map[types.BlockID]struct{}) (map[string]uint64, error)
// GetEpochAtxs is used to get the tortoise active set for an epoch
GetEpochAtxs(types.EpochID) ([]types.ATXID, error)
// GetAtxHeader returns the ATX header for an ATX ID
GetAtxHeader(types.ATXID) (*types.ActivationTxHeader, error)
}
type meshProvider interface {
// LayerContextuallyValidBlocks returns the set of contextually valid blocks from the mesh for a given layer
LayerContextuallyValidBlocks(context.Context, types.LayerID) (map[types.BlockID]struct{}, error)
}
// a function to verify the message with the signature and its public key.
type verifierFunc = func(pub, msg, sig []byte) bool
// Oracle is the hare eligibility oracle.
type Oracle struct {
lock sync.Mutex
beacon valueProvider
atxdb atxProvider
meshdb meshProvider
vrfSigner signer
vrfVerifier verifierFunc
layersPerEpoch uint32
vrfMsgCache addGet
activesCache addGet
cfg eCfg.Config
log.Log
}
// Returns a range of safe layers that should be used to construct the Hare active set for a given target layer
// Safe layer is a layer prior to the input layer on which w.h.p. we have agreement (i.e., on its contextually valid
// blocks), defined to be confidence param number of layers prior to the input layer.
func safeLayerRange(
targetLayer types.LayerID,
safetyParam,
layersPerEpoch,
epochOffset uint32,
) (safeLayerStart, safeLayerEnd types.LayerID) {
// prevent overflow
if targetLayer.Uint32() <= safetyParam {
return types.GetEffectiveGenesis(), types.GetEffectiveGenesis()
}
safeLayer := targetLayer.Sub(safetyParam)
safeEpoch := safeLayer.GetEpoch()
safeLayerStart = safeEpoch.FirstLayer()
safeLayerEnd = safeLayerStart.Add(epochOffset)
// If the safe layer is in the first epochOffset layers of an epoch,
// return a range from the beginning of the previous epoch
if safeLayer.Before(safeLayerEnd) {
// prevent overflow
if safeLayerStart.Uint32() <= layersPerEpoch {
return types.GetEffectiveGenesis(), types.GetEffectiveGenesis()
}
safeLayerStart = safeLayerStart.Sub(layersPerEpoch)
safeLayerEnd = safeLayerEnd.Sub(layersPerEpoch)
}
// If any portion of the range is in the genesis layers, just return the effective genesis
if !safeLayerStart.After(types.GetEffectiveGenesis()) {
return types.GetEffectiveGenesis(), types.GetEffectiveGenesis()
}
return
}
// New returns a new eligibility oracle instance.
func New(
beacon valueProvider,
atxdb atxProvider,
meshdb meshProvider,
vrfVerifier verifierFunc,
vrfSigner signer,
layersPerEpoch uint32,
cfg eCfg.Config,
logger log.Log) *Oracle {
vmc, err := lru.New(vrfMsgCacheSize)
if err != nil {
logger.With().Panic("could not create lru cache", log.Err(err))
}
ac, err := lru.New(activesCacheSize)
if err != nil {
logger.With().Panic("could not create lru cache", log.Err(err))
}
return &Oracle{
beacon: beacon,
atxdb: atxdb,
meshdb: meshdb,
vrfVerifier: vrfVerifier,
vrfSigner: vrfSigner,
layersPerEpoch: layersPerEpoch,
vrfMsgCache: vmc,
activesCache: ac,
cfg: cfg,
Log: logger,
}
}
// GetEpochBeacon returns the beacon for the specified epoch.
func (o *Oracle) GetEpochBeacon(ctx context.Context, epoch types.EpochID) bool {
_, err := o.beacon.Value(ctx, epoch)
return err == nil
}
type vrfMessage struct {
Beacon uint32
Round uint32
Layer types.LayerID
}
func buildKey(l types.LayerID, r uint32) [2]uint64 {
return [2]uint64{uint64(l.Uint32()), uint64(r)}
}
// buildVRFMessage builds the VRF message used as input for the BLS (msg=Beacon##Layer##Round).
func (o *Oracle) buildVRFMessage(ctx context.Context, layer types.LayerID, round uint32) ([]byte, error) {
key := buildKey(layer, round)
o.lock.Lock()
defer o.lock.Unlock()
// check cache
if val, exist := o.vrfMsgCache.Get(key); exist {
return val.([]byte), nil
}
// get value from beacon
v, err := o.beacon.Value(ctx, layer.GetEpoch())
if err != nil {
o.WithContext(ctx).With().Error("could not get hare beacon value for epoch",
log.Err(err),
layer,
layer.GetEpoch(),
log.Uint32("round", round))
return nil, fmt.Errorf("get beacon: %w", err)
}
// marshal message
msg := vrfMessage{Beacon: v, Round: round, Layer: layer}
buf, err := types.InterfaceToBytes(&msg)
if err != nil {
o.WithContext(ctx).With().Panic("failed to encode", log.Err(err))
}
o.vrfMsgCache.Add(key, buf)
return buf, nil
}
func (o *Oracle) totalWeight(ctx context.Context, layer types.LayerID) (uint64, error) {
actives, err := o.actives(ctx, layer)
if err != nil {
o.WithContext(ctx).With().Error("totalWeight erred while calling actives func", log.Err(err), layer)
return 0, err
}
var totalWeight uint64
for _, w := range actives {
totalWeight += w
}
return totalWeight, nil
}
func (o *Oracle) minerWeight(ctx context.Context, layer types.LayerID, id types.NodeID) (uint64, error) {
actives, err := o.actives(ctx, layer)
if err != nil {
o.With().Error("minerWeight erred while calling actives func", log.Err(err), layer)
return 0, err
}
w, ok := actives[id.Key]
if !ok {
o.With().Debug("miner is not active in specified layer",
log.Int("active_set_size", len(actives)),
log.String("actives", fmt.Sprintf("%v", actives)),
layer, log.String("id.Key", id.Key),
)
return 0, errors.New("miner is not active in specified layer")
}
return w, nil
}
func calcVrfFrac(vrfSig []byte) fixed.Fixed {
return fixed.FracFromBytes(vrfSig[:8])
}
func (o *Oracle) prepareEligibilityCheck(ctx context.Context, layer types.LayerID, round uint32, committeeSize int,
id types.NodeID, vrfSig []byte) (n int, p, vrfFrac fixed.Fixed, done bool, err error) {
logger := o.WithContext(ctx).WithFields(
layer,
id,
log.Uint32("round", round),
log.Int("committee_size", committeeSize))
if committeeSize < 1 {
logger.Error("committee size must be positive (received %d)", committeeSize)
return 0, fixed.Fixed{}, fixed.Fixed{}, true, nil
}
msg, err := o.buildVRFMessage(ctx, layer, round)
if err != nil {
logger.Error("eligibility: could not build vrf message")
return 0, fixed.Fixed{}, fixed.Fixed{}, true, err
}
// validate message
if !o.vrfVerifier(id.VRFPublicKey, msg, vrfSig) {
logger.With().Info("eligibility: a node did not pass vrf signature verification",
id,
layer)
return 0, fixed.Fixed{}, fixed.Fixed{}, true, nil
}
// get active set size
totalWeight, err := o.totalWeight(ctx, layer)
if err != nil {
return 0, fixed.Fixed{}, fixed.Fixed{}, true, err
}
// require totalWeight > 0
if totalWeight == 0 {
logger.Warning("eligibility: total weight is zero")
return 0, fixed.Fixed{}, fixed.Fixed{}, true, errors.New("total weight is zero")
}
// calc hash & check threshold
minerWeight, err := o.minerWeight(ctx, layer, id)
if err != nil {
return 0, fixed.Fixed{}, fixed.Fixed{}, true, err
}
logger.With().Info("preparing eligibility check",
log.Uint64("miner_weight", minerWeight),
log.Uint64("total_weight", totalWeight),
)
n = int(minerWeight)
// ensure miner weight fits in int
if uint64(n) != minerWeight {
logger.Panic(fmt.Sprintf("minerWeight overflows int (%d)", minerWeight))
}
// calc p
if committeeSize > int(totalWeight) {
logger.With().Debug("committee size is greater than total weight",
log.Int("committee_size", committeeSize),
log.Uint64("total_weight", totalWeight),
)
totalWeight *= uint64(committeeSize)
n *= committeeSize
}
p = fixed.DivUint64(uint64(committeeSize), totalWeight)
if minerWeight > maxSupportedN {
return 0, fixed.Fixed{}, fixed.Fixed{}, false,
fmt.Errorf("miner weight exceeds supported maximum (id: %v, weight: %d, max: %d",
id, minerWeight, maxSupportedN)
}
return n, p, calcVrfFrac(vrfSig), false, nil
}
// Validate validates the number of eligibilities of ID on the given Layer where msg is the VRF message, sig is the role
// proof and assuming commSize as the expected committee size.
func (o *Oracle) Validate(ctx context.Context, layer types.LayerID, round uint32, committeeSize int, id types.NodeID, sig []byte, eligibilityCount uint16) (bool, error) {
n, p, vrfFrac, done, err := o.prepareEligibilityCheck(ctx, layer, round, committeeSize, id, sig)
if done || err != nil {
return false, err
}
defer func() {
if msg := recover(); msg != nil {
o.WithContext(ctx).With().Error("panic in validate",
log.String("msg", fmt.Sprint(msg)),
log.Int("n", n),
log.String("p", p.String()),
log.String("vrf_frac", vrfFrac.String()),
)
o.Panic("%s", msg)
}
}()
x := int(eligibilityCount)
if !fixed.BinCDF(n, p, x-1).GreaterThan(vrfFrac) && vrfFrac.LessThan(fixed.BinCDF(n, p, x)) {
return true, nil
}
o.WithContext(ctx).With().Warning("eligibility: node did not pass vrf eligibility threshold",
layer,
log.Uint32("round", round),
log.Int("committee_size", committeeSize),
id,
log.Uint64("eligibility_count", uint64(eligibilityCount)),
log.Int("n", n),
log.String("p", p.String()),
log.String("vrf_frac", vrfFrac.String()),
log.Int("x", x),
)
return false, nil
}
// CalcEligibility calculates the number of eligibilities of ID on the given Layer where msg is the VRF message, sig is
// the role proof and assuming commSize as the expected committee size.
func (o *Oracle) CalcEligibility(ctx context.Context, layer types.LayerID, round uint32, committeeSize int,
id types.NodeID, vrfSig []byte) (uint16, error) {
n, p, vrfFrac, done, err := o.prepareEligibilityCheck(ctx, layer, round, committeeSize, id, vrfSig)
if done {
return 0, err
}
defer func() {
if msg := recover(); msg != nil {
o.With().Error("panic in calc eligibility",
layer, layer.GetEpoch(), log.Uint32("round_id", round),
log.String("msg", fmt.Sprint(msg)),
log.Int("committee_size", committeeSize),
log.Int("n", n),
log.String("p", fmt.Sprintf("%g", p.Float())),
log.String("vrf_frac", fmt.Sprintf("%g", vrfFrac.Float())),
)
o.Panic("%s", msg)
}
}()
o.With().Info("params",
layer, layer.GetEpoch(), log.Uint32("round_id", round),
log.Int("committee_size", committeeSize),
log.Int("n", n),
log.String("p", fmt.Sprintf("%g", p.Float())),
log.String("vrf_frac", fmt.Sprintf("%g", vrfFrac.Float())),
)
for x := 0; x < n; x++ {
if fixed.BinCDF(n, p, x).GreaterThan(vrfFrac) {
return uint16(x), nil
}
}
return uint16(n), nil
}
// Proof returns the role proof for the current Layer & Round.
func (o *Oracle) Proof(ctx context.Context, layer types.LayerID, round uint32) ([]byte, error) {
msg, err := o.buildVRFMessage(ctx, layer, round)
if err != nil {
o.WithContext(ctx).With().Error("proof: could not build vrf message", log.Err(err))
return nil, err
}
return o.vrfSigner.Sign(msg), nil
}
// Returns a map of all active node IDs in the specified layer id.
func (o *Oracle) actives(ctx context.Context, targetLayer types.LayerID) (map[string]uint64, error) {
logger := o.WithContext(ctx).WithFields(
log.FieldNamed("target_layer", targetLayer),
log.FieldNamed("target_layer_epoch", targetLayer.GetEpoch()))
logger.Debug("hare oracle getting active set")
// lock until any return
// note: no need to lock per safeEp - we do not expect many concurrent requests per safeEp (max two)
o.lock.Lock()
defer o.lock.Unlock()
// we first try to get the hare active set for a range of safe layers: start with the set of active blocks
safeLayerStart, safeLayerEnd := safeLayerRange(
targetLayer, o.cfg.ConfidenceParam, o.layersPerEpoch, o.cfg.EpochOffset)
logger.With().Debug("safe layer range",
log.FieldNamed("safe_layer_start", safeLayerStart),
log.FieldNamed("safe_layer_end", safeLayerEnd),
log.FieldNamed("safe_layer_start_epoch", safeLayerStart.GetEpoch()),
log.FieldNamed("safe_layer_end_epoch", safeLayerEnd.GetEpoch()),
log.Uint32("confidence_param", o.cfg.ConfidenceParam),
log.Uint32("epoch_offset", o.cfg.EpochOffset),
log.Uint64("layers_per_epoch", uint64(o.layersPerEpoch)),
log.FieldNamed("effective_genesis", types.GetEffectiveGenesis()))
// check cache first
// as long as epochOffset < layersPerEpoch, we expect safeLayerStart and safeLayerEnd to be in the same epoch.
// if not, don't attempt to cache on the basis of a single epoch since the safe layer range will span multiple
// epochs.
if safeLayerStart.GetEpoch() == safeLayerEnd.GetEpoch() {
if val, exist := o.activesCache.Get(safeLayerStart.GetEpoch()); exist {
activeMap := val.(map[string]uint64)
logger.With().Debug("found value in cache for safe layer start epoch",
log.FieldNamed("safe_layer_start", safeLayerStart),
log.FieldNamed("safe_layer_start_epoch", safeLayerStart.GetEpoch()),
log.Int("count", len(activeMap)))
return activeMap, nil
}
logger.With().Debug("no value in cache for safe layer start epoch",
log.FieldNamed("safe_layer_start", safeLayerStart),
log.FieldNamed("safe_layer_start_epoch", safeLayerStart.GetEpoch()))
} else {
// this is an error because GetMinerWeightsInEpochFromView, below, will probably also fail if it's true
// TODO: should we panic or return an error instead?
logger.With().Error("safe layer range spans multiple epochs, not caching active set results",
log.FieldNamed("safe_layer_start", safeLayerStart),
log.FieldNamed("safe_layer_end", safeLayerEnd),
log.FieldNamed("safe_layer_start_epoch", safeLayerStart.GetEpoch()),
log.FieldNamed("safe_layer_end_epoch", safeLayerEnd.GetEpoch()))
}
activeBlockIDs := make(map[types.BlockID]struct{})
for layerID := safeLayerStart; !layerID.After(safeLayerEnd); layerID = layerID.Add(1) {
layerBlockIDs, err := o.meshdb.LayerContextuallyValidBlocks(ctx, layerID)
if err != nil {
return nil, fmt.Errorf("error getting active blocks for layer %v for target layer %v: %w",
layerID, targetLayer, err)
}
for blockID := range layerBlockIDs {
activeBlockIDs[blockID] = struct{}{}
}
}
logger.With().Debug("got blocks in safe layer range, reading active set from blocks",
log.Int("count", len(activeBlockIDs)))
// now read the set of ATXs referenced by these blocks
// TODO: can the set of blocks ever span multiple epochs?
hareActiveSet, err := o.atxdb.GetMinerWeightsInEpochFromView(safeLayerStart.GetEpoch(), activeBlockIDs)
if err != nil {
return nil, fmt.Errorf("error getting ATXs for target layer %v: %w", targetLayer, err)
}
if len(hareActiveSet) > 0 {
logger.With().Debug("successfully got hare active set for layer range",
log.Int("count", len(hareActiveSet)))
o.activesCache.Add(safeLayerStart.GetEpoch(), hareActiveSet)
return hareActiveSet, nil
}
// if we failed to get a Hare active set, we fall back on reading the Tortoise active set targeting this epoch
// TODO: do we want to cache tortoise active set too?
if safeLayerStart.GetEpoch().IsGenesis() {
logger.With().Info("no hare active set for genesis layer range, reading tortoise set for epoch instead",
targetLayer.GetEpoch())
} else {
logger.With().Warning("no hare active set for layer range, reading tortoise set for epoch instead",
targetLayer.GetEpoch())
}
atxs, err := o.atxdb.GetEpochAtxs(targetLayer.GetEpoch() - 1)
if err != nil {
return nil, fmt.Errorf("can't get atxs for an epoch %d: %w", targetLayer.GetEpoch()-1, err)
}
logger.With().Debug("got tortoise atxs", log.Int("count", len(atxs)))
if len(atxs) == 0 {
return nil, fmt.Errorf("empty active set for layer %v in epoch %v",
targetLayer, targetLayer.GetEpoch())
}
// extract the nodeIDs and weights
activeMap := make(map[string]uint64, len(atxs))
for _, atxid := range atxs {
atxHeader, err := o.atxdb.GetAtxHeader(atxid)
if err != nil {
return nil, fmt.Errorf("inconsistent state: error getting atx header %v for target layer %v: %w", atxid, targetLayer, err)
}
activeMap[atxHeader.NodeID.Key] = atxHeader.GetWeight()
}
logger.With().Debug("got tortoise active set", log.Int("count", len(activeMap)))
// update cache and return
o.activesCache.Add(targetLayer.GetEpoch(), activeMap)
return activeMap, nil
}
// IsIdentityActiveOnConsensusView returns true if the provided identity is active on the consensus view derived
// from the specified layer, false otherwise.
func (o *Oracle) IsIdentityActiveOnConsensusView(ctx context.Context, edID string, layer types.LayerID) (bool, error) {
o.WithContext(ctx).With().Debug("hare oracle checking for active identity")
defer func() {
o.WithContext(ctx).With().Debug("hare oracle active identity check complete")
}()
actives, err := o.actives(ctx, layer)
if err != nil {
o.WithContext(ctx).With().Error("error getting active set", layer, log.Err(err))
return false, err
}
_, exist := actives[edID]
return exist, nil
}