-
Notifications
You must be signed in to change notification settings - Fork 12
/
topstakers.go
290 lines (228 loc) · 10.1 KB
/
topstakers.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package topstakers
import (
"bytes"
"sort"
"time"
"github.com/iotaledger/hive.go/ds"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/hive.go/runtime/module"
"github.com/iotaledger/hive.go/runtime/options"
"github.com/iotaledger/hive.go/runtime/syncutils"
"github.com/iotaledger/iota-core/pkg/core/account"
"github.com/iotaledger/iota-core/pkg/protocol/engine"
"github.com/iotaledger/iota-core/pkg/protocol/engine/accounts"
"github.com/iotaledger/iota-core/pkg/protocol/engine/blocks"
"github.com/iotaledger/iota-core/pkg/protocol/sybilprotection/activitytracker"
"github.com/iotaledger/iota-core/pkg/protocol/sybilprotection/activitytracker/activitytrackerv1"
"github.com/iotaledger/iota-core/pkg/protocol/sybilprotection/seatmanager"
"github.com/iotaledger/iota-core/pkg/storage/prunable/epochstore"
iotago "github.com/iotaledger/iota.go/v4"
)
// SeatManager is a sybil protection module for the engine that manages the weights of actors according to their stake.
type SeatManager struct {
apiProvider iotago.APIProvider
events *seatmanager.Events
committeeStore epochstore.Store[*account.SeatedAccounts]
committeeMutex syncutils.RWMutex
activityTracker activitytracker.ActivityTracker
optsOnlineCommitteeStartup []iotago.AccountID
module.Module
}
// NewProvider returns a new sybil protection provider that uses the ProofOfStake module.
func NewProvider(opts ...options.Option[SeatManager]) module.Provider[*engine.Engine, seatmanager.SeatManager] {
return module.Provide(func(e *engine.Engine) seatmanager.SeatManager {
return options.Apply(
&SeatManager{
Module: e.NewSubModule("SeatManager"),
apiProvider: e,
events: seatmanager.NewEvents(),
committeeStore: e.Storage.Committee(),
}, opts, func(s *SeatManager) {
activityTracker := activitytrackerv1.NewActivityTracker(e)
s.activityTracker = activityTracker
s.events.OnlineCommitteeSeatAdded.LinkTo(activityTracker.Events.OnlineCommitteeSeatAdded)
s.events.OnlineCommitteeSeatRemoved.LinkTo(activityTracker.Events.OnlineCommitteeSeatRemoved)
e.Events.SeatManager.LinkTo(s.events)
e.ConstructedEvent().OnTrigger(func() {
// We need to mark validators as active upon solidity of blocks as otherwise we would not be able to
// recover if no node was part of the online committee anymore.
e.Events.PostSolidFilter.BlockAllowed.Hook(func(block *blocks.Block) {
// Only track identities that are part of the committee.
committee, exists := s.CommitteeInSlot(block.ID().Slot())
if !exists {
panic(ierrors.Errorf("committee not selected for slot %d, but received block in that slot", block.ID().Slot()))
}
seat, exists := committee.GetSeat(block.ProtocolBlock().Header.IssuerID)
if exists {
s.activityTracker.MarkSeatActive(seat, block.ProtocolBlock().Header.IssuerID, block.IssuingTime())
}
s.events.BlockProcessed.Trigger(block)
})
s.ShutdownEvent().OnTrigger(func() {
s.StoppedEvent().Trigger()
})
s.InitializedEvent().Trigger()
})
s.ConstructedEvent().Trigger()
})
})
}
var _ seatmanager.SeatManager = &SeatManager{}
func (s *SeatManager) RotateCommittee(epoch iotago.EpochIndex, candidates accounts.AccountsData) (*account.SeatedAccounts, error) {
s.committeeMutex.Lock()
defer s.committeeMutex.Unlock()
if len(candidates) == 0 {
return nil, ierrors.New("candidates must not be empty")
}
committeeAccounts, err := s.selectNewCommitteeAccounts(epoch, candidates)
if err != nil {
return nil, ierrors.Wrap(err, "error while selecting new committee")
}
// Load previous committee only if target epoch is not zero.
var prevCommittee *account.SeatedAccounts
if epoch > 0 {
prevCommittee, err = s.committeeStore.Load(epoch - 1)
if err != nil {
return nil, err
}
}
// If there is no previous committee , then we can assign seats by default.
if prevCommittee == nil {
newCommittee := committeeAccounts.SeatedAccounts()
if err := s.committeeStore.Store(epoch, newCommittee); err != nil {
return nil, ierrors.Wrapf(err, "error while storing committee for epoch %d", epoch)
}
return newCommittee, nil
}
newCommittee := committeeAccounts.SeatedAccounts(prevCommittee)
if err := s.committeeStore.Store(epoch, newCommittee); err != nil {
return nil, ierrors.Wrapf(err, "error while storing committee for epoch %d", epoch)
}
return newCommittee, nil
}
// CommitteeInSlot returns the set of validators selected to be part of the committee in the given slot.
func (s *SeatManager) CommitteeInSlot(slot iotago.SlotIndex) (*account.SeatedAccounts, bool) {
s.committeeMutex.RLock()
defer s.committeeMutex.RUnlock()
return s.committeeInEpoch(s.apiProvider.APIForSlot(slot).TimeProvider().EpochFromSlot(slot))
}
// CommitteeInEpoch returns the set of validators selected to be part of the committee in the given epoch.
func (s *SeatManager) CommitteeInEpoch(epoch iotago.EpochIndex) (*account.SeatedAccounts, bool) {
s.committeeMutex.RLock()
defer s.committeeMutex.RUnlock()
return s.committeeInEpoch(epoch)
}
func (s *SeatManager) committeeInEpoch(epoch iotago.EpochIndex) (*account.SeatedAccounts, bool) {
c, err := s.committeeStore.Load(epoch)
if err != nil {
panic(ierrors.Wrapf(err, "failed to load committee for epoch %d", epoch))
}
if c == nil {
return nil, false
}
return c, true
}
// OnlineCommittee returns the set of validators selected to be part of the committee that has been seen recently.
func (s *SeatManager) OnlineCommittee() ds.Set[account.SeatIndex] {
return s.activityTracker.OnlineCommittee()
}
func (s *SeatManager) SeatCountInSlot(slot iotago.SlotIndex) int {
epoch := s.apiProvider.APIForSlot(slot).TimeProvider().EpochFromSlot(slot)
return s.SeatCountInEpoch(epoch)
}
func (s *SeatManager) SeatCountInEpoch(epoch iotago.EpochIndex) int {
s.committeeMutex.RLock()
defer s.committeeMutex.RUnlock()
// TODO: this function is a hot path as it is called for every single block. Maybe accessing the storage is too slow.
if committee, exists := s.committeeInEpoch(epoch); exists {
return committee.SeatCount()
}
return int(s.apiProvider.APIForEpoch(epoch).ProtocolParameters().TargetCommitteeSize())
}
func (s *SeatManager) Shutdown() {
s.StoppedEvent().Trigger()
}
func (s *SeatManager) InitializeCommittee(epoch iotago.EpochIndex, activityTime time.Time) error {
s.committeeMutex.Lock()
defer s.committeeMutex.Unlock()
committee, err := s.committeeStore.Load(epoch)
if err != nil {
return ierrors.Wrapf(err, "failed to load PoA committee for epoch %d", epoch)
}
committeeAccounts, err := committee.Accounts()
if err != nil {
return ierrors.Wrapf(err, "failed to retrieve accounts of the committee for epoch %d", epoch)
}
onlineValidators := committeeAccounts.IDs()
if len(s.optsOnlineCommitteeStartup) > 0 {
onlineValidators = s.optsOnlineCommitteeStartup
}
for _, v := range onlineValidators {
seat, exists := committee.GetSeat(v)
if !exists {
// Only track identities that are part of the committee.
continue
}
s.activityTracker.MarkSeatActive(seat, v, activityTime)
}
return nil
}
func (s *SeatManager) ReuseCommittee(currentEpoch iotago.EpochIndex, targetEpoch iotago.EpochIndex) (*account.SeatedAccounts, error) {
s.committeeMutex.Lock()
defer s.committeeMutex.Unlock()
currentCommittee, exists := s.committeeInEpoch(currentEpoch)
if !exists {
// that should never happen as it is already the fallback strategy
panic(ierrors.Errorf("committee for current epoch %d not found", currentEpoch))
}
if currentCommittee.SeatCount() == 0 {
return nil, ierrors.New("committee must not be empty")
}
newCommittee, err := currentCommittee.Reuse()
if err != nil {
return nil, ierrors.Wrapf(err, "failed to reuse committee from epoch %d", currentEpoch)
}
err = s.committeeStore.Store(targetEpoch, newCommittee)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to set committee for epoch %d", targetEpoch)
}
return newCommittee, nil
}
func (s *SeatManager) selectNewCommitteeAccounts(epoch iotago.EpochIndex, candidates accounts.AccountsData) (*account.Accounts, error) {
sort.Slice(candidates, func(i int, j int) bool {
// Prioritize the candidate that has a larger pool stake.
if candidates[i].ValidatorStake+candidates[i].DelegationStake != candidates[j].ValidatorStake+candidates[j].DelegationStake {
return candidates[i].ValidatorStake+candidates[i].DelegationStake > candidates[j].ValidatorStake+candidates[j].DelegationStake
}
// Prioritize the candidate that has a larger validator stake.
if candidates[i].ValidatorStake != candidates[j].ValidatorStake {
return candidates[i].ValidatorStake > candidates[j].ValidatorStake
}
// Prioritize the candidate that declares a longer staking period.
if candidates[i].StakeEndEpoch != candidates[j].StakeEndEpoch {
return candidates[i].StakeEndEpoch > candidates[j].StakeEndEpoch
}
// Prioritize the candidate that has smaller FixedCost.
if candidates[i].FixedCost != candidates[j].FixedCost {
return candidates[i].FixedCost < candidates[j].FixedCost
}
// two candidates never have the same account ID because they come in a map
return bytes.Compare(candidates[i].ID[:], candidates[j].ID[:]) > 0
})
// We try to select up to targetCommitteeSize candidates to be part of the committee. If there are fewer candidates
// than required, then we select all of them and the committee size will be smaller than targetCommitteeSize.
committeeSize := lo.Min(len(candidates), int(s.apiProvider.APIForEpoch(epoch).ProtocolParameters().TargetCommitteeSize()))
// Create new Accounts instance that only included validators selected to be part of the committee.
newCommitteeAccounts := account.NewAccounts()
for _, candidateData := range candidates[:committeeSize] {
if err := newCommitteeAccounts.Set(candidateData.ID, &account.Pool{
PoolStake: candidateData.ValidatorStake + candidateData.DelegationStake,
ValidatorStake: candidateData.ValidatorStake,
FixedCost: candidateData.FixedCost,
}); err != nil {
return nil, ierrors.Wrapf(err, "error while setting pool for committee candidate %s", candidateData.ID.String())
}
}
return newCommitteeAccounts, nil
}