-
Notifications
You must be signed in to change notification settings - Fork 211
/
oracle.go
306 lines (282 loc) · 9.67 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
package miner
import (
"bytes"
"errors"
"fmt"
"sort"
"sync"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/datastore"
"github.com/spacemeshos/go-spacemesh/events"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/proposals"
"github.com/spacemeshos/go-spacemesh/signing"
"github.com/spacemeshos/go-spacemesh/sql"
"github.com/spacemeshos/go-spacemesh/sql/ballots"
"github.com/spacemeshos/go-spacemesh/system"
)
var (
errMinerHasNoATXInPreviousEpoch = errors.New("miner has no ATX in previous epoch")
errZeroEpochWeight = errors.New("zero total weight for epoch")
errEmptyActiveSet = errors.New("empty active set for epoch")
)
type EpochEligibility struct {
Epoch types.EpochID
Atx types.ATXID
ActiveSet types.ATXIDList
Proofs map[types.LayerID][]types.VotingEligibility
Slots uint32
}
// Oracle provides proposal eligibility proofs for the miner.
type Oracle struct {
cfg config
clock layerClock
cdb *datastore.CachedDB
vrfSigner *signing.VRFSigner
syncState system.SyncStateProvider
log log.Log
mu sync.Mutex
cache *EpochEligibility
}
func newMinerOracle(cfg config, c layerClock, cdb *datastore.CachedDB, vrfSigner *signing.VRFSigner, ss system.SyncStateProvider, log log.Log) *Oracle {
return &Oracle{
cfg: cfg,
clock: c,
cdb: cdb,
vrfSigner: vrfSigner,
syncState: ss,
log: log,
cache: &EpochEligibility{},
}
}
// ProposalEligibility returns the miner's ATXID and the active set for the layer's epoch, along with the list of eligibility
// proofs for that layer.
func (o *Oracle) ProposalEligibility(lid types.LayerID, beacon types.Beacon, nonce types.VRFPostIndex) (*EpochEligibility, error) {
o.mu.Lock()
defer o.mu.Unlock()
if legacy := types.GetLegacyLayer(); legacy != 0 && lid.Uint32() == legacy+1 {
o.cache = &EpochEligibility{}
}
epoch := lid.GetEpoch()
if lid <= types.GetEffectiveGenesis() {
o.log.With().Panic("eligibility should not be queried during genesis", lid, epoch)
}
o.log.With().Debug("asked for proposal eligibility",
log.Stringer("requested epoch", epoch),
log.Stringer("cached epoch", o.cache.Epoch),
)
var layerProofs []types.VotingEligibility
if o.cache.Epoch == epoch { // use the cached value
layerProofs = o.cache.Proofs[lid]
o.log.With().Debug("got cached eligibility",
log.Stringer("requested epoch", epoch),
log.Stringer("cached epoch", o.cache.Epoch),
log.Int("num proposals", len(layerProofs)),
)
return o.cache, nil
}
ee, err := o.calcEligibilityProofs(lid, epoch, beacon, nonce)
if err != nil {
return nil, err
}
events.EmitEligibilities(ee.Epoch, beacon, ee.Atx, uint32(len(ee.ActiveSet)), ee.Proofs)
o.cache = ee
return ee, nil
}
func (o *Oracle) activesFromFirstBlock(targetEpoch types.EpochID, ownAtx types.ATXID, ownWeight uint64) (uint64, []types.ATXID, error) {
if ownAtx == types.EmptyATXID || ownWeight == 0 {
o.log.Fatal("invalid miner atx")
}
activeSet, err := ActiveSetFromEpochFirstBlock(o.cdb, targetEpoch)
if err != nil {
return 0, nil, err
}
_, totalWeight, own, err := infoFromActiveSet(o.cdb, o.vrfSigner.NodeID(), activeSet)
if err != nil {
return 0, nil, err
}
if own == types.EmptyATXID {
// miner is not included in the active set derived from the epoch's first block
activeSet = append(activeSet, ownAtx)
totalWeight += ownWeight
}
return totalWeight, activeSet, nil
}
func infoFromActiveSet(cdb *datastore.CachedDB, nodeID types.NodeID, activeSet []types.ATXID) (uint64, uint64, types.ATXID, error) {
var (
total, ownWeight uint64
ownAtx types.ATXID
)
for _, id := range activeSet {
hdr, err := cdb.GetAtxHeader(id)
if err != nil {
return 0, 0, types.EmptyATXID, fmt.Errorf("new ndoe get atx hdr: %w", err)
}
if hdr.NodeID == nodeID {
ownAtx = id
ownWeight = hdr.GetWeight()
}
total += hdr.GetWeight()
}
return ownWeight, total, ownAtx, nil
}
func (o *Oracle) activeSet(targetEpoch types.EpochID) (uint64, uint64, types.ATXID, []types.ATXID, error) {
var (
ownWeight, totalWeight uint64
ownAtx types.ATXID
atxids []types.ATXID
)
epochStart := o.clock.LayerToTime(targetEpoch.FirstLayer())
numOmitted := 0
if err := o.cdb.IterateEpochATXHeaders(targetEpoch, func(header *types.ActivationTxHeader) error {
if header.NodeID == o.cfg.nodeID {
ownWeight = header.GetWeight()
ownAtx = header.ID
}
grade, err := GradeAtx(o.cdb, header.NodeID, header.Received, epochStart, o.cfg.networkDelay)
if err != nil {
return err
}
if grade != Good && header.NodeID != o.cfg.nodeID {
o.log.With().Info("atx omitted from active set",
header.ID,
log.Int("grade", int(grade)),
log.Stringer("smesher", header.NodeID),
log.Bool("own", header.NodeID == o.cfg.nodeID),
log.Time("received", header.Received),
log.Time("epoch_start", epochStart),
)
numOmitted++
return nil
}
totalWeight += header.GetWeight()
atxids = append(atxids, header.ID)
return nil
}); err != nil {
return 0, 0, types.EmptyATXID, nil, err
}
if ownAtx == types.EmptyATXID || ownWeight == 0 {
return 0, 0, types.EmptyATXID, nil, errMinerHasNoATXInPreviousEpoch
}
if total := numOmitted + len(atxids); total == 0 {
return 0, 0, types.EmptyATXID, nil, errEmptyActiveSet
} else if numOmitted*100/total > 100-o.cfg.goodAtxPct {
// if the node is not synced during `targetEpoch-1`, it doesn't have the correct receipt timestamp
// for all the atx and malfeasance proof. this active set is not usable.
// TODO: change after timing info of ATXs and malfeasance proofs is sync'ed from peers as well
var err error
totalWeight, atxids, err = o.activesFromFirstBlock(targetEpoch, ownAtx, ownWeight)
if err != nil {
return 0, 0, types.EmptyATXID, nil, err
}
o.log.With().Info("miner not synced during prior epoch, active set from first block",
log.Int("all atx", total),
log.Int("num omitted", numOmitted),
log.Int("num block atx", len(atxids)),
)
} else {
o.log.With().Info("active set selected for proposal",
log.Int("num atx", len(atxids)),
log.Int("num omitted", numOmitted),
log.Int("min atx good pct", o.cfg.goodAtxPct),
)
}
return ownWeight, totalWeight, ownAtx, atxids, nil
}
// calcEligibilityProofs calculates the eligibility proofs of proposals for the miner in the given epoch
// and returns the proofs along with the epoch's active set.
func (o *Oracle) calcEligibilityProofs(lid types.LayerID, epoch types.EpochID, beacon types.Beacon, nonce types.VRFPostIndex) (*EpochEligibility, error) {
ref, err := ballots.RefBallot(o.cdb, epoch, o.vrfSigner.NodeID())
if err != nil && !errors.Is(err, sql.ErrNotFound) {
return nil, err
}
var (
minerWeight, totalWeight uint64
ownAtx types.ATXID
activeSet []types.ATXID
)
if ref == nil {
minerWeight, totalWeight, ownAtx, activeSet, err = o.activeSet(epoch)
} else {
activeSet = ref.ActiveSet
o.log.With().Info("use active set from ref ballot",
ref.ID(),
log.Int("num atx", len(activeSet)),
)
minerWeight, totalWeight, ownAtx, err = infoFromActiveSet(o.cdb, o.vrfSigner.NodeID(), activeSet)
}
if err != nil {
return nil, err
}
if totalWeight == 0 {
return nil, errZeroEpochWeight
}
if len(activeSet) == 0 {
return nil, errEmptyActiveSet
}
o.log.With().Debug("calculating eligibility",
epoch,
beacon,
log.Uint64("weight", minerWeight),
log.Uint64("total weight", totalWeight),
)
var numEligibleSlots uint32
if ref == nil {
numEligibleSlots, err = proposals.GetLegacyNumEligible(lid, minerWeight, o.cfg.minActiveSetWeight, totalWeight, o.cfg.layerSize, o.cfg.layersPerEpoch)
if err != nil {
return nil, fmt.Errorf("oracle get num slots: %w", err)
}
} else {
numEligibleSlots = ref.EpochData.EligibilityCount
}
eligibilityProofs := map[types.LayerID][]types.VotingEligibility{}
for counter := uint32(0); counter < numEligibleSlots; counter++ {
message, err := proposals.SerializeVRFMessage(beacon, epoch, nonce, counter)
if err != nil {
o.log.With().Fatal("failed to serialize VRF msg", log.Err(err))
}
vrfSig := o.vrfSigner.Sign(message)
eligibleLayer := proposals.CalcEligibleLayer(epoch, o.cfg.layersPerEpoch, vrfSig)
eligibilityProofs[eligibleLayer] = append(eligibilityProofs[eligibleLayer], types.VotingEligibility{
J: counter,
Sig: vrfSig,
})
o.log.Debug(fmt.Sprintf("signed vrf message, counter: %v, vrfSig: %s, layer: %v", counter, vrfSig, eligibleLayer))
}
o.log.With().Info("proposal eligibility for an epoch",
epoch,
beacon,
log.Uint64("weight", minerWeight),
log.Uint64("min activeset weight", o.cfg.minActiveSetWeight),
log.Uint64("total weight", totalWeight),
log.Uint32("total num slots", numEligibleSlots),
log.Int("num layers eligible", len(eligibilityProofs)),
log.Array("layers to num proposals", log.ArrayMarshalerFunc(func(encoder log.ArrayEncoder) error {
// Sort the layer map to log the layer data in order
keys := make([]types.LayerID, 0, len(eligibilityProofs))
for k := range eligibilityProofs {
keys = append(keys, k)
}
sort.Slice(keys, func(i, j int) bool {
return keys[i].Before(keys[j])
})
for _, lyr := range keys {
encoder.AppendObject(log.ObjectMarshallerFunc(func(encoder log.ObjectEncoder) error {
encoder.AddUint32("layer", lyr.Uint32())
encoder.AddInt("slots", len(eligibilityProofs[lyr]))
return nil
}))
}
return nil
})))
sort.Slice(activeSet, func(i, j int) bool {
return bytes.Compare(activeSet[i].Bytes(), activeSet[j].Bytes()) < 0
})
return &EpochEligibility{
Epoch: epoch,
Atx: ownAtx,
ActiveSet: activeSet,
Proofs: eligibilityProofs,
Slots: numEligibleSlots,
}, nil
}