-
Notifications
You must be signed in to change notification settings - Fork 931
/
pool.go
139 lines (125 loc) · 4.07 KB
/
pool.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
package voluntaryexits
import (
"math"
"sync"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/state"
"github.com/prysmaticlabs/prysm/v5/config/params"
types "github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"
doublylinkedlist "github.com/prysmaticlabs/prysm/v5/container/doubly-linked-list"
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v5/time/slots"
"github.com/sirupsen/logrus"
)
// PoolManager maintains pending and seen voluntary exits.
// This pool is used by proposers to insert voluntary exits into new blocks.
type PoolManager interface {
PendingExits() ([]*ethpb.SignedVoluntaryExit, error)
ExitsForInclusion(state state.ReadOnlyBeaconState, slot types.Slot) ([]*ethpb.SignedVoluntaryExit, error)
InsertVoluntaryExit(exit *ethpb.SignedVoluntaryExit)
MarkIncluded(exit *ethpb.SignedVoluntaryExit)
}
// Pool is a concrete implementation of PoolManager.
type Pool struct {
lock sync.RWMutex
pending doublylinkedlist.List[*ethpb.SignedVoluntaryExit]
m map[types.ValidatorIndex]*doublylinkedlist.Node[*ethpb.SignedVoluntaryExit]
}
// NewPool returns an initialized pool.
func NewPool() *Pool {
return &Pool{
pending: doublylinkedlist.List[*ethpb.SignedVoluntaryExit]{},
m: make(map[types.ValidatorIndex]*doublylinkedlist.Node[*ethpb.SignedVoluntaryExit]),
}
}
// PendingExits returns all objects from the pool.
func (p *Pool) PendingExits() ([]*ethpb.SignedVoluntaryExit, error) {
p.lock.RLock()
defer p.lock.RUnlock()
result := make([]*ethpb.SignedVoluntaryExit, p.pending.Len())
node := p.pending.First()
var err error
for i := 0; node != nil; i++ {
result[i], err = node.Value()
if err != nil {
return nil, err
}
node, err = node.Next()
if err != nil {
return nil, err
}
}
return result, nil
}
// ExitsForInclusion returns objects that are ready for inclusion at the given slot. This method will not
// return more than the block enforced MaxVoluntaryExits.
func (p *Pool) ExitsForInclusion(state state.ReadOnlyBeaconState, slot types.Slot) ([]*ethpb.SignedVoluntaryExit, error) {
p.lock.RLock()
length := int(math.Min(float64(params.BeaconConfig().MaxVoluntaryExits), float64(p.pending.Len())))
result := make([]*ethpb.SignedVoluntaryExit, 0, length)
node := p.pending.First()
for node != nil && len(result) < length {
exit, err := node.Value()
if err != nil {
p.lock.RUnlock()
return nil, err
}
if exit.Exit.Epoch > slots.ToEpoch(slot) {
node, err = node.Next()
if err != nil {
p.lock.RUnlock()
return nil, err
}
continue
}
validator, err := state.ValidatorAtIndexReadOnly(exit.Exit.ValidatorIndex)
if err != nil {
logrus.WithError(err).Warningf("could not get validator at index %d", exit.Exit.ValidatorIndex)
node, err = node.Next()
if err != nil {
p.lock.RUnlock()
return nil, err
}
continue
}
if err = blocks.VerifyExitAndSignature(validator, state, exit); err != nil {
logrus.WithError(err).Warning("removing invalid exit from pool")
p.lock.RUnlock()
// MarkIncluded removes the invalid exit from the pool
p.MarkIncluded(exit)
p.lock.RLock()
} else {
result = append(result, exit)
}
node, err = node.Next()
if err != nil {
p.lock.RUnlock()
return nil, err
}
}
p.lock.RUnlock()
return result, nil
}
// InsertVoluntaryExit into the pool.
func (p *Pool) InsertVoluntaryExit(exit *ethpb.SignedVoluntaryExit) {
p.lock.Lock()
defer p.lock.Unlock()
_, exists := p.m[exit.Exit.ValidatorIndex]
if exists {
return
}
p.pending.Append(doublylinkedlist.NewNode(exit))
p.m[exit.Exit.ValidatorIndex] = p.pending.Last()
}
// MarkIncluded is used when an exit has been included in a beacon block. Every block seen by this
// node should call this method to include the exit. This will remove the exit from the pool.
func (p *Pool) MarkIncluded(exit *ethpb.SignedVoluntaryExit) {
p.lock.Lock()
defer p.lock.Unlock()
node := p.m[exit.Exit.ValidatorIndex]
if node == nil {
return
}
delete(p.m, exit.Exit.ValidatorIndex)
p.pending.Remove(node)
}