-
Notifications
You must be signed in to change notification settings - Fork 12
/
accounts.go
237 lines (184 loc) · 6.78 KB
/
accounts.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
package account
import (
"bytes"
"io"
"sort"
"github.com/iotaledger/hive.go/core/safemath"
"github.com/iotaledger/hive.go/ds"
"github.com/iotaledger/hive.go/ds/shrinkingmap"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/runtime/syncutils"
"github.com/iotaledger/hive.go/serializer/v2"
"github.com/iotaledger/hive.go/serializer/v2/stream"
iotago "github.com/iotaledger/iota.go/v4"
)
// Accounts represent a collection of accounts and their pools.
type Accounts struct {
accountPools *shrinkingmap.ShrinkingMap[iotago.AccountID, *Pool]
totalStake iotago.BaseToken
totalValidatorStake iotago.BaseToken
mutex syncutils.RWMutex
}
// NewAccounts creates a new Weights instance.
func NewAccounts() *Accounts {
return &Accounts{
accountPools: shrinkingmap.New[iotago.AccountID, *Pool](),
}
}
func (a *Accounts) Has(id iotago.AccountID) bool {
return a.accountPools.Has(id)
}
func (a *Accounts) Size() int {
return a.accountPools.Size()
}
func (a *Accounts) IDs() []iotago.AccountID {
a.mutex.RLock()
defer a.mutex.RUnlock()
ids := make([]iotago.AccountID, 0, a.accountPools.Size())
a.accountPools.ForEachKey(func(id iotago.AccountID) bool {
ids = append(ids, id)
return true
})
return ids
}
// Get returns the weight of the given identity.
func (a *Accounts) Get(id iotago.AccountID) (pool *Pool, exists bool) {
return a.accountPools.Get(id)
}
// setWithoutLocking sets the weight of the given identity.
func (a *Accounts) setWithoutLocking(id iotago.AccountID, pool *Pool) error {
value, created := a.accountPools.GetOrCreate(id, func() *Pool {
return pool
})
var safeMathErr error
if !created {
// if there was already an entry, we need to subtract the former
// stake first and set the new value
if a.totalStake, safeMathErr = safemath.SafeSub(a.totalStake, value.PoolStake); safeMathErr != nil {
return ierrors.Wrapf(safeMathErr, "failed to subtract pool stake from total stake for account %s", id.String())
}
if a.totalValidatorStake, safeMathErr = safemath.SafeSub(a.totalValidatorStake, value.ValidatorStake); safeMathErr != nil {
return ierrors.Wrapf(safeMathErr, "failed to subtract validator stake from total validator stake for account %s", id.String())
}
a.accountPools.Set(id, pool)
}
if a.totalStake, safeMathErr = safemath.SafeAdd(a.totalStake, pool.PoolStake); safeMathErr != nil {
return ierrors.Wrapf(safeMathErr, "failed to add pool stake to total stake for account %s", id.String())
}
if a.totalValidatorStake, safeMathErr = safemath.SafeAdd(a.totalValidatorStake, pool.ValidatorStake); safeMathErr != nil {
return ierrors.Wrapf(safeMathErr, "failed to add validator stake to total validator stake for account %s", id.String())
}
return nil
}
// Set sets the weight of the given identity.
func (a *Accounts) Set(id iotago.AccountID, pool *Pool) error {
a.mutex.Lock()
defer a.mutex.Unlock()
return a.setWithoutLocking(id, pool)
}
func (a *Accounts) TotalStake() iotago.BaseToken {
a.mutex.RLock()
defer a.mutex.RUnlock()
return a.totalStake
}
func (a *Accounts) TotalValidatorStake() iotago.BaseToken {
a.mutex.RLock()
defer a.mutex.RUnlock()
return a.totalValidatorStake
}
// ForEach iterates over all weights and calls the given callback for each of them.
func (a *Accounts) ForEach(callback func(id iotago.AccountID, pool *Pool) bool) {
a.accountPools.ForEach(callback)
}
// SeatedAccounts creates a new SeatedAccounts instance,
// that maintains the seat indices of re-elected members from the previous committee, if provided.
func (a *Accounts) SeatedAccounts(optPrevCommittee ...*SeatedAccounts) *SeatedAccounts {
// If optPrevCommittee not given, use empty SeatedAccounts instance as default to skip the behavior to handle re-elected members.
prevCommittee := NewSeatedAccounts(NewAccounts())
if len(optPrevCommittee) > 0 {
prevCommittee = optPrevCommittee[0]
}
newCommittee := NewSeatedAccounts(a)
// If previous committee exists the seats need to be assigned and re-elected committee members must retain their SeatIndex.
// Keep track of re-elected members' seat indices.
takenSeatIndices := ds.NewSet[SeatIndex]()
// Assign re-elected members' SeatIndex.
a.ForEach(func(id iotago.AccountID, _ *Pool) bool {
if seatIndex, seatExists := prevCommittee.GetSeat(id); seatExists {
newCommittee.Set(seatIndex, id)
takenSeatIndices.Add(seatIndex)
}
return true
})
// Sort members lexicographically.
committeeAccountIDs := a.IDs()
sort.Slice(committeeAccountIDs, func(i int, j int) bool {
return bytes.Compare(committeeAccountIDs[i][:], committeeAccountIDs[j][:]) < 0
})
// Assign SeatIndex to new committee members.
currentSeatIndex := SeatIndex(0)
for _, memberID := range committeeAccountIDs {
// If SeatIndex is taken by re-elected member, then increment it.
for takenSeatIndices.Has(currentSeatIndex) {
currentSeatIndex++
}
// Assign SeatIndex of a fresh committee member.
if _, seatExists := prevCommittee.GetSeat(memberID); !seatExists {
newCommittee.Set(currentSeatIndex, memberID)
currentSeatIndex++
}
}
return newCommittee
}
func AccountsFromBytes(b []byte) (*Accounts, int, error) {
reader := stream.NewByteReader(b)
a, err := AccountsFromReader(reader)
if err != nil {
return nil, 0, ierrors.Wrap(err, "unable to read accounts from bytes")
}
return a, reader.BytesRead(), nil
}
func AccountsFromReader(reader io.Reader) (*Accounts, error) {
a := NewAccounts()
if err := stream.ReadCollection(reader, serializer.SeriLengthPrefixTypeAsUint32, func(i int) error {
accountID, err := stream.Read[iotago.AccountID](reader)
if err != nil {
return ierrors.Wrapf(err, "unable to read accountID at index %d", i)
}
pool, err := stream.ReadObject(reader, poolBytesLength, PoolFromBytes)
if err != nil {
return ierrors.Wrapf(err, "unable to read pool at index %d", i)
}
if err := a.setWithoutLocking(accountID, pool); err != nil {
return ierrors.Wrapf(err, "failed to set pool for account %s", accountID.String())
}
return nil
}); err != nil {
return nil, ierrors.Wrap(err, "failed to read account data")
}
return a, nil
}
func (a *Accounts) Bytes() ([]byte, error) {
a.mutex.RLock()
defer a.mutex.RUnlock()
byteBuffer := stream.NewByteBuffer()
if err := stream.WriteCollection(byteBuffer, serializer.SeriLengthPrefixTypeAsUint32, func() (elementsCount int, err error) {
var innerErr error
a.ForEach(func(id iotago.AccountID, pool *Pool) bool {
if innerErr = stream.Write(byteBuffer, id); innerErr != nil {
return false
}
if innerErr = stream.WriteObject(byteBuffer, pool, (*Pool).Bytes); innerErr != nil {
return false
}
return true
})
if innerErr != nil {
return 0, innerErr
}
return a.accountPools.Size(), nil
}); err != nil {
return nil, ierrors.Wrap(err, "failed to write accounts")
}
return byteBuffer.Bytes()
}