-
Notifications
You must be signed in to change notification settings - Fork 672
/
staker.go
123 lines (107 loc) · 3.5 KB
/
staker.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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package state
import (
"bytes"
"time"
"github.com/google/btree"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/crypto/bls"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
)
var _ btree.LessFunc[*Staker] = (*Staker).Less
// StakerIterator defines an interface for iterating over a set of stakers.
type StakerIterator interface {
// Next attempts to move the iterator to the next staker. It returns false
// once there are no more stakers to return.
Next() bool
// Value returns the current staker. Value should only be called after a
// call to Next which returned true.
Value() *Staker
// Release any resources associated with the iterator. This must be called
// after the interator is no longer needed.
Release()
}
// Staker contains all information required to represent a validator or
// delegator in the current and pending validator sets.
// Invariant: Staker's size is bounded to prevent OOM DoS attacks.
type Staker struct {
TxID ids.ID
NodeID ids.NodeID
PublicKey *bls.PublicKey
SubnetID ids.ID
Weight uint64
StartTime time.Time
EndTime time.Time
PotentialReward uint64
// NextTime is the next time this staker will be moved from a validator set.
// If the staker is in the pending validator set, NextTime will equal
// StartTime. If the staker is in the current validator set, NextTime will
// equal EndTime.
NextTime time.Time
// Priority specifies how to break ties between stakers with the same
// NextTime. This ensures that stakers created by the same transaction type
// are grouped together. The ordering of these groups is documented in
// [priorities.go] and depends on if the stakers are in the pending or
// current validator set.
Priority txs.Priority
}
// A *Staker is considered to be less than another *Staker when:
//
// 1. If its NextTime is before the other's.
// 2. If the NextTimes are the same, the *Staker with the lesser priority is the
// lesser one.
// 3. If the priorities are also the same, the one with the lesser txID is
// lesser.
func (s *Staker) Less(than *Staker) bool {
if s.NextTime.Before(than.NextTime) {
return true
}
if than.NextTime.Before(s.NextTime) {
return false
}
if s.Priority < than.Priority {
return true
}
if than.Priority < s.Priority {
return false
}
return bytes.Compare(s.TxID[:], than.TxID[:]) == -1
}
func NewCurrentStaker(txID ids.ID, staker txs.Staker, potentialReward uint64) (*Staker, error) {
publicKey, _, err := staker.PublicKey()
if err != nil {
return nil, err
}
endTime := staker.EndTime()
return &Staker{
TxID: txID,
NodeID: staker.NodeID(),
PublicKey: publicKey,
SubnetID: staker.SubnetID(),
Weight: staker.Weight(),
StartTime: staker.StartTime(),
EndTime: endTime,
PotentialReward: potentialReward,
NextTime: endTime,
Priority: staker.CurrentPriority(),
}, nil
}
func NewPendingStaker(txID ids.ID, staker txs.Staker) (*Staker, error) {
publicKey, _, err := staker.PublicKey()
if err != nil {
return nil, err
}
startTime := staker.StartTime()
return &Staker{
TxID: txID,
NodeID: staker.NodeID(),
PublicKey: publicKey,
SubnetID: staker.SubnetID(),
Weight: staker.Weight(),
StartTime: startTime,
EndTime: staker.EndTime(),
NextTime: startTime,
Priority: staker.PendingPriority(),
}, nil
}