-
Notifications
You must be signed in to change notification settings - Fork 211
/
oracle.go
317 lines (264 loc) · 9.52 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
package eligibility
import (
"bytes"
"crypto/sha256"
"encoding/binary"
"errors"
"github.com/hashicorp/golang-lru"
"github.com/nullstyle/go-xdr/xdr3"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/config"
eCfg "github.com/spacemeshos/go-spacemesh/hare/eligibility/config"
"github.com/spacemeshos/go-spacemesh/log"
"math"
"sync"
)
const vrfMsgCacheSize = 20 // numRounds per layer is <= 2. numConcurrentLayers<=10 (typically <=2) so numRounds*numConcurrentLayers <= 2*10 = 20 is a good upper bound
const activesCacheSize = 5 // we don't expect to handle more than two layers concurrently
var (
errGenesis = errors.New("no data about active nodes for genesis")
errNoContextualBlocks = errors.New("no contextually valid blocks")
)
type valueProvider interface {
Value(layer types.LayerID) (uint32, error)
}
// a func to retrieve the active set size for the provided layer
// this func is assumed to be cpu intensive and hence we cache its results
type activeSetFunc func(epoch types.EpochId, blocks map[types.BlockID]struct{}) (map[string]struct{}, error)
type signer interface {
Sign(msg []byte) ([]byte, error)
}
type goodBlocksProvider interface {
ContextuallyValidBlock(layer types.LayerID) (map[types.BlockID]struct{}, error)
}
// a function to verify the message with the signature and its public key.
type verifierFunc = func(msg, sig, pub []byte) (bool, error)
// Oracle is the hare eligibility oracle
type Oracle struct {
lock sync.Mutex
beacon valueProvider
getActiveSet activeSetFunc
vrfSigner signer
vrfVerifier verifierFunc
layersPerEpoch uint16
vrfMsgCache addGet
activesCache addGet
genesisActiveSetSize int
blocksProvider goodBlocksProvider
cfg eCfg.Config
log.Log
}
// Returns the relative layer id that w.h.p. we have agreement on its contextually valid blocks
// safe layer is defined to be the confidence param layers prior to the provided Layer
func safeLayer(layer types.LayerID, safetyParam types.LayerID) types.LayerID {
if layer <= safetyParam { // assuming genesis is zero
return config.Genesis
}
return layer - safetyParam
}
func roundedSafeLayer(layer types.LayerID, safetyParam types.LayerID,
layersPerEpoch uint16, epochOffset types.LayerID) types.LayerID {
sl := safeLayer(layer, safetyParam)
if sl == config.Genesis {
return config.Genesis
}
se := types.LayerID(sl.GetEpoch(layersPerEpoch)) // the safe epoch
roundedLayer := se*types.LayerID(layersPerEpoch) + epochOffset
if sl >= roundedLayer { // the safe layer is after the rounding threshold
return roundedLayer // round to threshold
}
if roundedLayer <= types.LayerID(layersPerEpoch) { // we can't go before genesis
return config.Genesis // just return genesis
}
// round to the previous epoch threshold
return roundedLayer - types.LayerID(layersPerEpoch)
}
// New returns a new eligibility oracle instance.
func New(beacon valueProvider, activeSetFunc activeSetFunc, vrfVerifier verifierFunc, vrfSigner signer,
layersPerEpoch uint16, genesisActiveSet int, goodBlocksProvider goodBlocksProvider,
cfg eCfg.Config, log log.Log) *Oracle {
vmc, e := lru.New(vrfMsgCacheSize)
if e != nil {
log.Panic("Could not create lru cache err=%v", e)
}
ac, e := lru.New(activesCacheSize)
if e != nil {
log.Panic("Could not create lru cache err=%v", e)
}
return &Oracle{
beacon: beacon,
getActiveSet: activeSetFunc,
vrfVerifier: vrfVerifier,
vrfSigner: vrfSigner,
layersPerEpoch: layersPerEpoch,
vrfMsgCache: vmc,
activesCache: ac,
genesisActiveSetSize: genesisActiveSet,
blocksProvider: goodBlocksProvider,
cfg: cfg,
Log: log,
}
}
type vrfMessage struct {
Beacon uint32
Round int32
Layer types.LayerID
}
func buildKey(l types.LayerID, r int32) [2]uint64 {
return [2]uint64{uint64(l), uint64(r)}
}
// buildVRFMessage builds the VRF message used as input for the BLS (msg=Beacon##Layer##Round)
func (o *Oracle) buildVRFMessage(layer types.LayerID, round int32) ([]byte, error) {
key := buildKey(layer, round)
o.lock.Lock()
// check cache
if val, exist := o.vrfMsgCache.Get(key); exist {
o.lock.Unlock()
return val.([]byte), nil
}
// get value from beacon
v, err := o.beacon.Value(layer)
if err != nil {
o.With().Error("Could not get hare beacon value", log.Err(err), log.LayerId(uint64(layer)), log.Int32("round", round))
o.lock.Unlock()
return nil, err
}
// marshal message
var w bytes.Buffer
msg := vrfMessage{Beacon: v, Round: round, Layer: layer}
_, err = xdr.Marshal(&w, &msg)
if err != nil {
o.With().Error("Fatal: could not marshal xdr", log.Err(err))
o.lock.Unlock()
return nil, err
}
val := w.Bytes()
o.vrfMsgCache.Add(key, val) // update cache
o.lock.Unlock()
return val, nil
}
func (o *Oracle) activeSetSize(layer types.LayerID) (uint32, error) {
actives, err := o.actives(layer)
if err != nil {
if err == errGenesis { // we are in genesis
return uint32(o.genesisActiveSetSize), nil
}
o.With().Error("activeSetSize erred while calling actives func", log.Err(err), log.LayerId(uint64(layer)))
return 0, err
}
return uint32(len(actives)), nil
}
// Eligible checks if Id is eligible 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) Eligible(layer types.LayerID, round int32, committeeSize int, id types.NodeId, sig []byte) (bool, error) {
msg, err := o.buildVRFMessage(layer, round)
if err != nil {
o.Error("Eligible: could not build VRF message")
return false, err
}
// validate message
res, err := o.vrfVerifier(msg, sig, id.VRFPublicKey)
if err != nil {
o.Error("Eligible: VRF verification failed: %v", err)
return false, err
}
if !res {
o.With().Info("Eligible: a node did not pass VRF signature verification",
log.String("id", id.ShortString()), log.Uint64("layer_id", uint64(layer)))
return false, nil
}
// get active set size
activeSetSize, err := o.activeSetSize(layer)
if err != nil {
return false, err
}
// require activeSetSize > 0
if activeSetSize == 0 {
o.Warning("Eligible: active set size is zero")
return false, errors.New("active set size is zero")
}
// calc hash & check threshold
sha := sha256.Sum256(sig)
shaUint32 := binary.LittleEndian.Uint32(sha[:4])
// avoid division (no floating point) & do operations on uint64 to avoid overflow
if uint64(activeSetSize)*uint64(shaUint32) > uint64(committeeSize)*uint64(math.MaxUint32) {
o.With().Info("Eligible: a node did not pass VRF eligibility",
log.String("id", id.ShortString()), log.Int("committee_size", committeeSize),
log.Uint32("active_set_size", activeSetSize), log.Int32("round", round),
log.Uint64("layer_id", uint64(layer)))
return false, nil
}
// lower or equal
return true, nil
}
// Proof returns the role proof for the current Layer & Round
func (o *Oracle) Proof(layer types.LayerID, round int32) ([]byte, error) {
msg, err := o.buildVRFMessage(layer, round)
if err != nil {
o.Error("Proof: could not build VRF message err=%v", err)
return nil, err
}
sig, err := o.vrfSigner.Sign(msg)
if err != nil {
o.Error("Proof: could not sign VRF message err=%v", err)
return nil, err
}
return sig, nil
}
// Returns a map of all active nodes in the specified layer id
func (o *Oracle) actives(layer types.LayerID) (map[string]struct{}, error) {
sl := roundedSafeLayer(layer, types.LayerID(o.cfg.ConfidenceParam), o.layersPerEpoch, types.LayerID(o.cfg.EpochOffset))
safeEp := sl.GetEpoch(o.layersPerEpoch)
// check genesis
if safeEp.IsGenesis() {
return nil, errGenesis
}
// 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()
// check cache
if val, exist := o.activesCache.Get(safeEp); exist {
o.lock.Unlock()
return val.(map[string]struct{}), nil
}
// build a map of all blocks on the current layer
mp, err := o.blocksProvider.ContextuallyValidBlock(sl)
if err != nil {
o.lock.Unlock()
return nil, err
}
// no contextually valid blocks
if len(mp) == 0 {
o.With().Error("Could not calculate hare active set size: no contextually valid blocks",
log.Uint64("layer_id", uint64(layer)), log.Uint64("epoch_id", uint64(layer.GetEpoch(o.layersPerEpoch))),
log.Uint64("safe_layer_id", uint64(sl)), log.Uint64("safe_epoch_id", uint64(safeEp)))
o.lock.Unlock()
return nil, errNoContextualBlocks
}
activeMap, err := o.getActiveSet(safeEp, mp)
if err != nil {
o.With().Error("Could not retrieve active set size", log.Err(err),
log.Uint64("layer_id", uint64(layer)), log.Uint64("epoch_id", uint64(layer.GetEpoch(o.layersPerEpoch))),
log.Uint64("safe_layer_id", uint64(sl)), log.Uint64("safe_epoch_id", uint64(safeEp)))
o.lock.Unlock()
return nil, err
}
// update
o.activesCache.Add(safeEp, activeMap)
o.lock.Unlock()
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(edId string, layer types.LayerID) (bool, error) {
actives, err := o.actives(layer)
if err != nil {
if err == errGenesis { // we are in genesis
return true, nil // all ids are active in genesis
}
o.With().Error("IsIdentityActiveOnConsensusView erred while calling actives func",
log.LayerId(uint64(layer)), log.Err(err))
return false, err
}
_, exist := actives[edId]
return exist, nil
}