-
Notifications
You must be signed in to change notification settings - Fork 211
/
oracle.go
194 lines (168 loc) · 6.51 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
package miner
import (
"errors"
"fmt"
"sort"
"sync"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/datastore"
"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/atxs"
)
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 oracleCache struct {
epoch types.EpochID
atx *types.ActivationTxHeader
activeSet []types.ATXID
proofs map[types.LayerID][]types.VotingEligibility
}
// Oracle provides proposal eligibility proofs for the miner.
type Oracle struct {
avgLayerSize uint32
layersPerEpoch uint32
cdb *datastore.CachedDB
vrfSigner *signing.VRFSigner
nodeID types.NodeID
log log.Log
mu sync.Mutex
cache oracleCache
}
func newMinerOracle(layerSize, layersPerEpoch uint32, cdb *datastore.CachedDB, vrfSigner *signing.VRFSigner, nodeID types.NodeID, log log.Log) *Oracle {
return &Oracle{
avgLayerSize: layerSize,
layersPerEpoch: layersPerEpoch,
cdb: cdb,
vrfSigner: vrfSigner,
nodeID: nodeID,
log: log,
}
}
// GetProposalEligibility 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) GetProposalEligibility(lid types.LayerID, beacon types.Beacon, nonce types.VRFPostIndex) (types.ATXID, []types.ATXID, []types.VotingEligibility, error) {
o.mu.Lock()
defer o.mu.Unlock()
epoch := lid.GetEpoch()
logger := o.log.WithFields(lid,
log.Named("requested_epoch", epoch),
log.Named("cached_epoch", o.cache.epoch))
if epoch.IsGenesis() {
logger.With().Panic("eligibility should not be queried during genesis", lid, epoch)
}
logger.Info("asked for proposal eligibility")
var layerProofs []types.VotingEligibility
if o.cache.epoch == epoch { // use the cached value
layerProofs = o.cache.proofs[lid]
logger.With().Info("got cached eligibility", log.Int("num_proposals", len(layerProofs)))
return o.cache.atx.ID, o.cache.activeSet, layerProofs, nil
}
// calculate the proof
atx, err := o.getOwnEpochATX(epoch)
if err != nil {
if errors.Is(err, sql.ErrNotFound) {
return *types.EmptyATXID, nil, nil, errMinerHasNoATXInPreviousEpoch
}
return *types.EmptyATXID, nil, nil, fmt.Errorf("failed to get valid atx for node for target epoch %d: %w", epoch, err)
}
newProofs, activeSet, err := o.calcEligibilityProofs(atx.GetWeight(), epoch, beacon, nonce)
if err != nil {
logger.With().Error("failed to calculate eligibility proofs", log.Err(err))
return *types.EmptyATXID, nil, nil, err
}
o.cache = oracleCache{
epoch: epoch,
atx: atx,
activeSet: activeSet,
proofs: newProofs,
}
layerProofs = o.cache.proofs[lid]
logger.With().Info("got eligibility for proposals", log.Int("num_proposals", len(layerProofs)))
return o.cache.atx.ID, o.cache.activeSet, layerProofs, nil
}
func (o *Oracle) getOwnEpochATX(targetEpoch types.EpochID) (*types.ActivationTxHeader, error) {
publishEpoch := targetEpoch - 1
atxID, err := atxs.GetIDByEpochAndNodeID(o.cdb, publishEpoch, o.nodeID)
if err != nil {
o.log.With().Warning("failed to find ATX ID for node",
log.Named("publish_epoch", publishEpoch),
log.Named("atx_node_id", o.nodeID),
log.Err(err))
return nil, fmt.Errorf("get ATX ID: %w", err)
}
atx, err := o.cdb.GetAtxHeader(atxID)
if err != nil {
o.log.With().Error("failed to get ATX header",
log.Named("publish_epoch", publishEpoch),
log.Named("atx_node_id", o.nodeID),
log.Err(err))
return nil, fmt.Errorf("get ATX header: %w", err)
}
return atx, 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(weight uint64, epoch types.EpochID, beacon types.Beacon, nonce types.VRFPostIndex) (map[types.LayerID][]types.VotingEligibility, []types.ATXID, error) {
logger := o.log.WithFields(epoch, beacon, log.Uint64("weight", weight))
// get the previous epoch's total weight
totalWeight, activeSet, err := o.cdb.GetEpochWeight(epoch)
if err != nil {
return nil, nil, fmt.Errorf("failed to get epoch %v weight: %w", epoch, err)
}
if totalWeight == 0 {
return nil, nil, errZeroEpochWeight
}
if len(activeSet) == 0 {
return nil, nil, errEmptyActiveSet
}
logger = logger.WithFields(log.Uint64("total_weight", totalWeight))
logger.Info("calculating eligibility")
numEligibleSlots, err := proposals.GetNumEligibleSlots(weight, totalWeight, o.avgLayerSize, o.layersPerEpoch)
if err != nil {
logger.With().Error("failed to get number of eligible proposals", log.Err(err))
return nil, nil, fmt.Errorf("oracle get num slots: %w", err)
}
eligibilityProofs := map[types.LayerID][]types.VotingEligibility{}
for counter := uint32(0); counter < numEligibleSlots; counter++ {
message, err := proposals.SerializeVRFMessage(beacon, epoch, nonce, counter)
if err != nil {
logger.With().Fatal("failed to serialize VRF msg", log.Err(err))
}
vrfSig := o.vrfSigner.Sign(message)
eligibleLayer := proposals.CalcEligibleLayer(epoch, o.layersPerEpoch, vrfSig)
eligibilityProofs[eligibleLayer] = append(eligibilityProofs[eligibleLayer], types.VotingEligibility{
J: counter,
Sig: vrfSig,
})
logger.Debug("signed vrf message, counter: %v, vrfSig: %v, layer: %v",
counter, types.BytesToHash(vrfSig).ShortString(), eligibleLayer)
}
logger.With().Info("proposal eligibility calculated",
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
})))
return eligibilityProofs, activeSet, nil
}