-
Notifications
You must be signed in to change notification settings - Fork 211
/
eligibility_validator.go
216 lines (205 loc) · 6.42 KB
/
eligibility_validator.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
package proposals
import (
"context"
"fmt"
"github.com/spacemeshos/fixed"
"github.com/spacemeshos/go-spacemesh/atxsdata"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/miner/minweight"
"github.com/spacemeshos/go-spacemesh/p2p/pubsub"
"github.com/spacemeshos/go-spacemesh/system"
)
// Validator validates the eligibility of a Ballot.
// the validation focuses on eligibility only and assumes the Ballot to be valid otherwise.
type Validator struct {
minActiveSetWeight []types.EpochMinimalActiveWeight
avgLayerSize uint32
layersPerEpoch uint32
tortoise tortoiseProvider
atxsdata *atxsdata.Data
clock layerClock
beacons system.BeaconCollector
logger log.Log
vrfVerifier vrfVerifier
}
// ValidatorOpt for configuring Validator.
type ValidatorOpt func(h *Validator)
// NewEligibilityValidator returns a new EligibilityValidator.
func NewEligibilityValidator(
avgLayerSize, layersPerEpoch uint32,
minActiveSetWeight []types.EpochMinimalActiveWeight,
clock layerClock,
tortoise tortoiseProvider,
atxsdata *atxsdata.Data,
bc system.BeaconCollector,
lg log.Log,
vrfVerifier vrfVerifier,
opts ...ValidatorOpt,
) *Validator {
v := &Validator{
minActiveSetWeight: minActiveSetWeight,
avgLayerSize: avgLayerSize,
layersPerEpoch: layersPerEpoch,
tortoise: tortoise,
atxsdata: atxsdata,
clock: clock,
beacons: bc,
logger: lg,
vrfVerifier: vrfVerifier,
}
for _, opt := range opts {
opt(v)
}
return v
}
// CheckEligibility checks that a ballot is eligible in the layer that it specifies.
func (v *Validator) CheckEligibility(ctx context.Context, ballot *types.Ballot, weight uint64) error {
if len(ballot.EligibilityProofs) == 0 {
return fmt.Errorf(
"%w: empty eligibility list is invalid (ballot %s)",
pubsub.ErrValidationReject,
ballot.ID(),
)
}
atx := v.atxsdata.Get(ballot.Layer.GetEpoch(), ballot.AtxID)
if atx == nil {
return fmt.Errorf(
"failed to load atx from cache with epoch %d %s",
ballot.Layer.GetEpoch(),
ballot.AtxID.ShortString(),
)
}
if atx.Node != ballot.SmesherID {
return fmt.Errorf(
"%w: referenced atx %s belongs to a different smesher %s",
pubsub.ErrValidationReject,
atx.Node.ShortString(),
ballot.SmesherID.ShortString(),
)
}
var (
data *types.EpochData
err error
)
if ballot.EpochData != nil && ballot.Layer.GetEpoch() == v.clock.CurrentLayer().GetEpoch() {
data, err = v.validateReference(ballot, atx.Weight, weight)
} else {
data, err = v.validateSecondary(ballot)
}
if err != nil {
return err
}
for i, proof := range ballot.EligibilityProofs {
if proof.J >= data.EligibilityCount {
return fmt.Errorf("%w: proof counter larger than number of slots (%d) numEligibleBallots (%d)",
pubsub.ErrValidationReject, proof.J, data.EligibilityCount)
}
if i != 0 && proof.J <= ballot.EligibilityProofs[i-1].J {
return fmt.Errorf(
"%w: proofs are out of order: %d <= %d",
pubsub.ErrValidationReject,
proof.J,
ballot.EligibilityProofs[i-1].J,
)
}
if !v.vrfVerifier.Verify(ballot.SmesherID,
MustSerializeVRFMessage(data.Beacon, ballot.Layer.GetEpoch(), atx.Nonce, proof.J), proof.Sig) {
return fmt.Errorf(
"%w: proof contains incorrect VRF signature. beacon: %v, epoch: %v, counter: %v, vrfSig: %s",
pubsub.ErrValidationReject,
data.Beacon.ShortString(),
ballot.Layer.GetEpoch(),
proof.J,
proof.Sig,
)
}
if eligible := CalcEligibleLayer(ballot.Layer.GetEpoch(), v.layersPerEpoch, proof.Sig); ballot.Layer != eligible {
return fmt.Errorf("%w: ballot has incorrect layer index. ballot layer (%v), eligible layer (%v)",
pubsub.ErrValidationReject, ballot.Layer, eligible)
}
}
v.logger.WithContext(ctx).With().Debug("ballot eligibility verified",
ballot.ID(),
ballot.Layer,
ballot.Layer.GetEpoch(),
data.Beacon,
)
v.beacons.ReportBeaconFromBallot(ballot.Layer.GetEpoch(), ballot, data.Beacon,
fixed.DivUint64(atx.Weight, uint64(data.EligibilityCount)))
return nil
}
// validateReference executed for reference ballots in latest epoch.
func (v *Validator) validateReference(
ballot *types.Ballot,
weight, totalWeight uint64,
) (*types.EpochData, error) {
if ballot.EpochData.Beacon == types.EmptyBeacon {
return nil, fmt.Errorf("%w: beacon is missing in ref ballot %v", pubsub.ErrValidationReject, ballot.ID())
}
if totalWeight == 0 {
return nil, fmt.Errorf("%w: empty active set in ref ballot %v", pubsub.ErrValidationReject, ballot.ID())
}
numEligibleSlots, err := GetNumEligibleSlots(
weight,
minweight.Select(ballot.Layer.GetEpoch(), v.minActiveSetWeight),
totalWeight,
v.avgLayerSize,
v.layersPerEpoch,
)
if err != nil {
return nil, fmt.Errorf("%w: %v", pubsub.ErrValidationReject, err)
}
if ballot.EpochData.EligibilityCount != numEligibleSlots {
return nil, fmt.Errorf(
"%w: ballot has incorrect eligibility count expected %v, got: %v",
pubsub.ErrValidationReject,
numEligibleSlots,
ballot.EpochData.EligibilityCount,
)
}
return ballot.EpochData, nil
}
// validateSecondary executed for non-reference ballots in latest epoch and all ballots in past epochs.
func (v *Validator) validateSecondary(ballot *types.Ballot) (*types.EpochData, error) {
if ballot.RefBallot == types.EmptyBallotID {
if ballot.EpochData == nil {
return nil, fmt.Errorf(
"%w: epoch data is missing in ref ballot %v",
pubsub.ErrValidationReject,
ballot.ID(),
)
}
return ballot.EpochData, nil
}
refdata := v.tortoise.GetBallot(ballot.RefBallot)
if refdata == nil {
return nil, fmt.Errorf("ref ballot is missing %v", ballot.RefBallot)
}
if refdata.ATXID != ballot.AtxID {
return nil, fmt.Errorf(
"%w: ballot (%v/%v) should be sharing atx with a reference ballot (%v/%v)",
pubsub.ErrValidationReject,
ballot.ID(),
ballot.AtxID,
refdata.ID,
refdata.ATXID,
)
}
if refdata.Smesher != ballot.SmesherID {
return nil, fmt.Errorf(
"%w: mismatched smesher id with refballot in ballot %v",
pubsub.ErrValidationReject,
ballot.ID(),
)
}
if refdata.Layer.GetEpoch() != ballot.Layer.GetEpoch() {
return nil, fmt.Errorf(
"%w: ballot %v targets mismatched epoch %d",
pubsub.ErrValidationReject,
ballot.ID(),
ballot.Layer.GetEpoch(),
)
}
return &types.EpochData{Beacon: refdata.Beacon, EligibilityCount: refdata.Eligiblities}, nil
}