forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.go
70 lines (57 loc) · 1.61 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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package validators
import (
"math"
"github.com/dim4egster/qmallgo/ids"
safemath "github.com/dim4egster/qmallgo/utils/math"
)
var _ Validator = &validator{}
// 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
}
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.Sub64(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,
}
}
// GenerateRandomValidator creates a random validator with the provided weight
func GenerateRandomValidator(weight uint64) Validator {
return NewValidator(
ids.GenerateTestNodeID(),
weight,
)
}