6
6
7
7
"github.com/stretchr/testify/require"
8
8
9
+ "github.com/iotaledger/hive.go/lo"
9
10
"github.com/iotaledger/iota-core/pkg/core/account"
10
11
iotago "github.com/iotaledger/iota.go/v4"
11
12
"github.com/iotaledger/iota.go/v4/tpkg"
@@ -20,13 +21,11 @@ func TestAccounts(t *testing.T) {
20
21
21
22
// check "Set"
22
23
for id , stake := range issuers {
23
- if err := accounts .Set (id , & account.Pool {
24
- PoolStake : iotago . BaseToken ( stake ) ,
24
+ require . NoError ( t , accounts .Set (id , & account.Pool {
25
+ PoolStake : stake ,
25
26
ValidatorStake : iotago .BaseToken (stake ) * 2 ,
26
27
FixedCost : iotago .Mana (stake ) * 3 ,
27
- }); err != nil {
28
- t .Fatal (err )
29
- }
28
+ }))
30
29
}
31
30
32
31
// check "Size"
@@ -57,13 +56,13 @@ func TestAccounts(t *testing.T) {
57
56
require .True (t , accounts .Has (id ))
58
57
59
58
// check "Get"
60
- account , exists := accounts .Get (id )
59
+ member , exists := accounts .Get (id )
61
60
require .True (t , exists )
62
61
63
62
// check the account itself
64
- require .Equal (t , stake , account .PoolStake )
65
- require .Equal (t , stake * 2 , account .ValidatorStake )
66
- require .Equal (t , iotago .Mana (stake )* 3 , account .FixedCost )
63
+ require .Equal (t , stake , member .PoolStake )
64
+ require .Equal (t , stake * 2 , member .ValidatorStake )
65
+ require .Equal (t , iotago .Mana (stake )* 3 , member .FixedCost )
67
66
}
68
67
69
68
// check total stakes
@@ -74,11 +73,11 @@ func TestAccounts(t *testing.T) {
74
73
account1 , exists := accounts .Get (accountIDs [0 ])
75
74
require .True (t , exists )
76
75
77
- accounts .Set (accountIDs [0 ], & account.Pool {
76
+ require . NoError ( t , accounts .Set (accountIDs [0 ], & account.Pool {
78
77
PoolStake : 10 * account1 .PoolStake ,
79
78
ValidatorStake : 10 * account1 .ValidatorStake ,
80
79
FixedCost : 10 * account1 .FixedCost ,
81
- })
80
+ }))
82
81
83
82
// check if the updated total stakes are correct
84
83
require .Equal (t , (totalStake - account1 .PoolStake )+ (account1 .PoolStake * 10 ), accounts .TotalStake ())
@@ -107,12 +106,8 @@ func TestAccounts(t *testing.T) {
107
106
108
107
// check "SelectCommittee"
109
108
110
- // get 1 issuer
111
- seated := accounts .SeatedAccounts (accountIDs [0 ])
112
- require .Equal (t , 1 , seated .SeatCount ())
113
-
114
109
// get all issuers
115
- seated = accounts .SeatedAccounts (accountIDs ... )
110
+ seated : = accounts .SeatedAccounts ()
116
111
require .Equal (t , len (accountIDs ), seated .SeatCount ())
117
112
118
113
/*
@@ -124,14 +119,82 @@ func TestAccounts(t *testing.T) {
124
119
*/
125
120
}
126
121
122
+ func TestAccounts_SeatedAccounts (t * testing.T ) {
123
+ committeeSize := 20
124
+
125
+ prevCommitteeIssuers , _ := generateAccounts (committeeSize )
126
+ newCommitteeIssuers , _ := generateAccounts (committeeSize / 2 - 1 )
127
+
128
+ newCommitteeAccounts := account .NewAccounts ()
129
+ prevCommitteeAccounts := account .NewAccounts ()
130
+
131
+ idx := 0
132
+ for id , stake := range prevCommitteeIssuers {
133
+ // half of the accounts from the previous committee should also be added to the new committee
134
+ if idx % 2 == 0 || idx == 1 {
135
+ require .NoError (t , newCommitteeAccounts .Set (id , & account.Pool {
136
+ PoolStake : stake ,
137
+ ValidatorStake : iotago .BaseToken (stake ) * 2 ,
138
+ FixedCost : iotago .Mana (stake ) * 3 ,
139
+ }))
140
+ }
141
+
142
+ require .NoError (t , prevCommitteeAccounts .Set (id , & account.Pool {
143
+ PoolStake : stake ,
144
+ ValidatorStake : iotago .BaseToken (stake ) * 2 ,
145
+ FixedCost : iotago .Mana (stake ) * 3 ,
146
+ }))
147
+
148
+ idx ++
149
+ }
150
+
151
+ // Add remaining accounts to new committee accounts.
152
+ for id , stake := range newCommitteeIssuers {
153
+ // half of the accounts from the previous committee should also be added to the new committee
154
+ require .NoError (t , newCommitteeAccounts .Set (id , & account.Pool {
155
+ PoolStake : stake ,
156
+ ValidatorStake : iotago .BaseToken (stake ) * 2 ,
157
+ FixedCost : iotago .Mana (stake ) * 3 ,
158
+ }))
159
+ }
160
+
161
+ // Get prev committee and assign seats by default.
162
+ prevCommittee := prevCommitteeAccounts .SeatedAccounts ()
163
+
164
+ // New committee seats should be assigned taking previous committee into account.
165
+ newCommittee := newCommitteeAccounts .SeatedAccounts (prevCommittee )
166
+
167
+ prevCommitteeAccounts .ForEach (func (id iotago.AccountID , _ * account.Pool ) bool {
168
+ if seatIndex , exists := newCommittee .GetSeat (id ); exists {
169
+ require .Equal (t , lo .Return1 (prevCommittee .GetSeat (id )), seatIndex )
170
+ }
171
+
172
+ return true
173
+ })
174
+
175
+ // Make sure that the size of the committee and SeatIndex values are sane.
176
+ require .Equal (t , newCommittee .SeatCount (), committeeSize )
177
+
178
+ // Check that SeatIndices are within <0; committeeSize) range.
179
+ newCommitteeAccounts .ForEach (func (id iotago.AccountID , _ * account.Pool ) bool {
180
+ // SeatIndex must be smaller than CommitteeSize.
181
+ require .Less (t , int (lo .Return1 (newCommittee .GetSeat (id ))), committeeSize )
182
+
183
+ // SeatIndex must be bigger or equal to 0.
184
+ require .GreaterOrEqual (t , int (lo .Return1 (newCommittee .GetSeat (id ))), 0 )
185
+
186
+ return true
187
+ })
188
+ }
189
+
127
190
func generateAccounts (count int ) (map [iotago.AccountID ]iotago.BaseToken , iotago.BaseToken ) {
128
191
seenIDs := make (map [iotago.AccountID ]bool )
129
192
issuers := make (map [iotago.AccountID ]iotago.BaseToken )
130
193
131
194
var totalStake iotago.BaseToken
132
195
133
196
for i := 0 ; i < count ; i ++ {
134
- id := iotago .AccountID ([ 32 ] byte { tpkg .RandByte ()} )
197
+ id := iotago .AccountID (tpkg .Rand32ByteArray () )
135
198
if _ , exist := seenIDs [id ]; exist {
136
199
i --
137
200
continue
0 commit comments