-
Notifications
You must be signed in to change notification settings - Fork 652
/
validator.go
72 lines (58 loc) · 1.59 KB
/
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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package validators
import (
"math"
"github.com/ava-labs/avalanchego/ids"
safemath "github.com/ava-labs/avalanchego/utils/math"
)
var _ Validator = (*validator)(nil)
// Validator is the minimal description of someone that can be sampled.
type Validator interface {
// ID returns the node ID of this validator
ID() ids.NodeID
// Weight that can be used for weighted sampling. If this validator is
// validating the primary network, returns the amount of AVAX staked.
Weight() uint64
}
// validator is a struct that contains the base values required by the validator
// interface.
type validator struct {
nodeID ids.NodeID
weight uint64
// index is used to efficiently remove validators from the validator set. It
// represents the index of this validator in the vdrSlice and weights
// arrays.
index int
}
func (v *validator) ID() ids.NodeID {
return v.nodeID
}
func (v *validator) Weight() uint64 {
return v.weight
}
func (v *validator) addWeight(weight uint64) {
newTotalWeight, err := safemath.Add64(weight, v.weight)
if err != nil {
newTotalWeight = math.MaxUint64
}
v.weight = newTotalWeight
}
func (v *validator) removeWeight(weight uint64) {
newTotalWeight, err := safemath.Sub(v.weight, weight)
if err != nil {
newTotalWeight = 0
}
v.weight = newTotalWeight
}
// NewValidator returns a validator object that implements the Validator
// interface
func NewValidator(
nodeID ids.NodeID,
weight uint64,
) Validator {
return &validator{
nodeID: nodeID,
weight: weight,
}
}