@@ -36,11 +36,14 @@ const (
36
36
37
37
candidate1Name = "candidate1"
38
38
candidate2Name = "candidate2"
39
- selfStake = "1200000000000000000000000"
40
- initVotes = "1272000000000000063948846"
41
- vote = "100000000000000000000"
42
- autoStakeVote = "103801784016923925869"
43
- initBalance = "100000000000000000000000000"
39
+ )
40
+
41
+ var (
42
+ selfStake , _ = big .NewInt (0 ).SetString ("1200000000000000000000000" , 10 )
43
+ cand1Votes , _ = big .NewInt (0 ).SetString ("1635067133824581908640994" , 10 )
44
+ vote , _ = big .NewInt (0 ).SetString ("100000000000000000000" , 10 )
45
+ autoStakeVote , _ = big .NewInt (0 ).SetString ("103801784016923925869" , 10 )
46
+ initBalance , _ = big .NewInt (0 ).SetString ("100000000000000000000000000" , 10 )
44
47
)
45
48
46
49
var (
@@ -57,14 +60,14 @@ func TestNativeStaking(t *testing.T) {
57
60
identityset .Address (23 ).String (),
58
61
identityset .Address (23 ).String (),
59
62
"test1" ,
60
- selfStake ,
63
+ selfStake . String () ,
61
64
},
62
65
{
63
66
identityset .Address (24 ).String (),
64
67
identityset .Address (25 ).String (),
65
68
identityset .Address (25 ).String (),
66
69
"test2" ,
67
- selfStake ,
70
+ selfStake . String () ,
68
71
},
69
72
}
70
73
@@ -93,10 +96,10 @@ func TestNativeStaking(t *testing.T) {
93
96
cand2PriKey := identityset .PrivateKey (1 )
94
97
95
98
register1 , err := testutil .SignedCandidateRegister (1 , candidate1Name , cand1Addr .String (), cand1Addr .String (),
96
- cand1Addr .String (), selfStake , 1 , false , nil , gasLimit , gasPrice , cand1PriKey )
99
+ cand1Addr .String (), selfStake . String (), 91 , true , nil , gasLimit , gasPrice , cand1PriKey )
97
100
require .NoError (err )
98
101
register2 , err := testutil .SignedCandidateRegister (1 , candidate2Name , cand2Addr .String (), cand2Addr .String (),
99
- cand2Addr .String (), selfStake , 1 , false , nil , gasLimit , gasPrice , cand2PriKey )
102
+ cand2Addr .String (), selfStake . String () , 1 , false , nil , gasLimit , gasPrice , cand2PriKey )
100
103
require .NoError (err )
101
104
102
105
fixedTime := time .Unix (cfg .Genesis .Timestamp , 0 )
@@ -106,8 +109,8 @@ func TestNativeStaking(t *testing.T) {
106
109
[]action.SealedEnvelope {register2 }, fixedTime ))
107
110
108
111
// check candidate state
109
- require .NoError (checkCandidateState (sf , candidate1Name , cand1Addr .String (), selfStake , initVotes , cand1Addr ))
110
- require .NoError (checkCandidateState (sf , candidate1Name , cand1Addr .String (), selfStake , initVotes , cand1Addr ))
112
+ require .NoError (checkCandidateState (sf , candidate1Name , cand1Addr .String (), selfStake , cand1Votes , cand1Addr ))
113
+ require .NoError (checkCandidateState (sf , candidate2Name , cand2Addr .String (), selfStake , selfStake , cand2Addr ))
111
114
112
115
// check candidate account state
113
116
require .NoError (checkAccountState (cfg , sf , register1 , true , initBalance , cand1Addr ))
@@ -126,21 +129,19 @@ func TestNativeStaking(t *testing.T) {
126
129
voter2Addr := identityset .Address (3 )
127
130
voter2PriKey := identityset .PrivateKey (3 )
128
131
129
- cs1 , err := testutil .SignedCreateStake (1 , candidate1Name , vote , 1 , false ,
132
+ cs1 , err := testutil .SignedCreateStake (1 , candidate1Name , vote . String () , 1 , false ,
130
133
nil , gasLimit , gasPrice , voter1PriKey )
131
134
require .NoError (err )
132
- cs2 , err := testutil .SignedCreateStake (1 , candidate1Name , vote , 1 , false ,
135
+ cs2 , err := testutil .SignedCreateStake (1 , candidate1Name , vote . String () , 1 , false ,
133
136
nil , gasLimit , gasPrice , voter2PriKey )
134
137
require .NoError (err )
135
138
136
139
require .NoError (createAndCommitBlock (bc , []address.Address {voter1Addr , voter2Addr },
137
140
[]action.SealedEnvelope {cs1 , cs2 }, fixedTime ))
138
141
139
142
// check candidate state
140
- wv , _ := big .NewInt (0 ).SetString (vote , 10 )
141
- expectedVotes , _ := big .NewInt (0 ).SetString (initVotes , 10 )
142
- expectedVotes .Add (expectedVotes , big .NewInt (0 ).Mul (wv , big .NewInt (2 )))
143
- require .NoError (checkCandidateState (sf , candidate1Name , cand1Addr .String (), selfStake , expectedVotes .String (), cand1Addr ))
143
+ expectedVotes := big .NewInt (0 ).Add (cand1Votes , big .NewInt (0 ).Mul (vote , big .NewInt (2 )))
144
+ require .NoError (checkCandidateState (sf , candidate1Name , cand1Addr .String (), selfStake , expectedVotes , cand1Addr ))
144
145
145
146
// check voter account state
146
147
require .NoError (checkAccountState (cfg , sf , cs1 , false , initBalance , voter1Addr ))
@@ -162,14 +163,13 @@ func TestNativeStaking(t *testing.T) {
162
163
cc , err := testutil .SignedChangeCandidate (2 , candidate2Name , voter2BucketIndex , nil ,
163
164
gasLimit , gasPrice , voter2PriKey )
164
165
require .NoError (err )
165
-
166
166
require .NoError (createAndCommitBlock (bc , []address.Address {voter2Addr }, []action.SealedEnvelope {cc }, fixedTime ))
167
167
168
168
// check candidate state
169
- expectedVotes , _ = big .NewInt (0 ).SetString ( initVotes , 10 )
170
- expectedVotes . Add ( expectedVotes , wv )
171
- require . NoError ( checkCandidateState ( sf , candidate1Name , cand1Addr . String (), selfStake , expectedVotes . String (), cand1Addr ) )
172
- require .NoError (checkCandidateState (sf , candidate2Name , cand2Addr .String (), selfStake , expectedVotes . String () , cand2Addr ))
169
+ expectedVotes = big .NewInt (0 ).Add ( cand1Votes , vote )
170
+ require . NoError ( checkCandidateState ( sf , candidate1Name , cand1Addr . String (), selfStake , expectedVotes , cand1Addr ) )
171
+ expectedVotes = big . NewInt ( 0 ). Add ( selfStake , vote )
172
+ require .NoError (checkCandidateState (sf , candidate2Name , cand2Addr .String (), selfStake , expectedVotes , cand2Addr ))
173
173
174
174
// transfer stake
175
175
ts , err := testutil .SignedTransferStake (2 , voter2Addr .String (), voter1BucketIndex , nil , gasLimit , gasPrice , voter1PriKey )
@@ -191,7 +191,7 @@ func TestNativeStaking(t *testing.T) {
191
191
require .Equal (voter1BucketIndex , bis [1 ])
192
192
193
193
// deposit to stake
194
- ds , err := testutil .SignedDepositToStake (3 , voter2BucketIndex , vote , nil , gasLimit , gasPrice , voter2PriKey )
194
+ ds , err := testutil .SignedDepositToStake (3 , voter2BucketIndex , vote . String () , nil , gasLimit , gasPrice , voter2PriKey )
195
195
require .NoError (err )
196
196
require .NoError (createAndCommitBlock (bc , []address.Address {voter2Addr }, []action.SealedEnvelope {ds }, fixedTime ))
197
197
r , err := dao .GetReceiptByActionHash (ds .Hash (), 6 )
@@ -205,21 +205,16 @@ func TestNativeStaking(t *testing.T) {
205
205
require .NoError (createAndCommitBlock (bc , []address.Address {voter2Addr }, []action.SealedEnvelope {rs }, fixedTime ))
206
206
207
207
// check candidate state
208
- expectedVotes , _ = big .NewInt (0 ).SetString (initVotes , 10 )
209
- wv , _ = big .NewInt (0 ).SetString (autoStakeVote , 10 )
210
- expectedVotes .Add (expectedVotes , wv )
211
- require .NoError (checkCandidateState (sf , candidate2Name , cand2Addr .String (), selfStake , expectedVotes .String (), cand2Addr ))
208
+ expectedVotes = big .NewInt (0 ).Add (selfStake , autoStakeVote )
209
+ require .NoError (checkCandidateState (sf , candidate2Name , cand2Addr .String (), selfStake , expectedVotes , cand2Addr ))
212
210
213
211
// deposit to stake again
214
- ds , err = testutil .SignedDepositToStake (3 , voter2BucketIndex , vote , nil , gasLimit , gasPrice , voter2PriKey )
212
+ ds , err = testutil .SignedDepositToStake (3 , voter2BucketIndex , vote . String () , nil , gasLimit , gasPrice , voter2PriKey )
215
213
require .NoError (err )
216
214
require .NoError (createAndCommitBlock (bc , []address.Address {voter2Addr }, []action.SealedEnvelope {ds }, fixedTime ))
217
215
218
216
// check voter account state
219
- voterBalance , _ := big .NewInt (0 ).SetString (initBalance , 10 )
220
- vote , _ := big .NewInt (0 ).SetString (vote , 10 )
221
- voterBalance .Sub (voterBalance , vote )
222
- require .NoError (checkAccountState (cfg , sf , ds , false , voterBalance .String (), voter2Addr ))
217
+ require .NoError (checkAccountState (cfg , sf , ds , false , big .NewInt (0 ).Sub (initBalance , vote ), voter2Addr ))
223
218
224
219
// unstake voter stake
225
220
us , err := testutil .SignedReclaimStake (false , 4 , voter1BucketIndex , nil , gasLimit , gasPrice , voter2PriKey )
@@ -232,47 +227,39 @@ func TestNativeStaking(t *testing.T) {
232
227
require .NoError (createAndCommitBlock (bc , []address.Address {voter2Addr }, []action.SealedEnvelope {us }, unstakeTime ))
233
228
234
229
// check candidate state
235
- expectedVotes , _ = big .NewInt (0 ).SetString (initVotes , 10 )
236
- require .NoError (checkCandidateState (sf , candidate1Name , cand1Addr .String (), selfStake , expectedVotes .String (), cand1Addr ))
230
+ require .NoError (checkCandidateState (sf , candidate1Name , cand1Addr .String (), selfStake , cand1Votes , cand1Addr ))
237
231
238
232
// unstake self stake
239
233
us , err = testutil .SignedReclaimStake (false , 2 , selfstakeIndex1 , nil , gasLimit , gasPrice , cand1PriKey )
240
234
require .NoError (err )
241
235
require .NoError (createAndCommitBlock (bc , []address.Address {cand1Addr }, []action.SealedEnvelope {us }, unstakeTime ))
242
236
243
237
// check candidate state
244
- expectedVotes = big .NewInt (0 )
245
- expectedSelfStake := big .NewInt (0 )
246
- require .NoError (checkCandidateState (sf , candidate1Name , cand1Addr .String (), expectedSelfStake .String (),
247
- expectedVotes .String (), cand1Addr ))
238
+ require .NoError (checkCandidateState (sf , candidate1Name , cand1Addr .String (), selfStake , cand1Votes , cand1Addr ))
248
239
249
240
// withdraw stake
250
241
ws , err := testutil .SignedReclaimStake (true , 3 , selfstakeIndex1 , nil , gasLimit , gasPrice , cand1PriKey )
251
242
require .NoError (err )
252
243
require .NoError (createAndCommitBlock (bc , []address.Address {cand1Addr }, []action.SealedEnvelope {ws }, unstakeTime ))
253
244
r , err = dao .GetReceiptByActionHash (ws .Hash (), 12 )
254
245
require .NoError (err )
255
- require .Equal (uint64 (iotextypes .ReceiptStatus_ErrWithdrawBeforeMaturity ), r .Status )
246
+ require .Equal (uint64 (iotextypes .ReceiptStatus_ErrWithdrawBeforeUnstake ), r .Status )
256
247
257
248
require .NoError (createAndCommitBlock (bc , []address.Address {cand1Addr }, []action.SealedEnvelope {ws }, unstakeTime .Add (cfg .Genesis .WithdrawWaitingPeriod )))
258
249
259
250
// check buckets
260
251
_ , err = sf .State (& bis , protocol .NamespaceOption (staking .StakingNameSpace ),
261
252
protocol .KeyOption (addrKeyWithPrefix (cand1Addr , _voterIndex )))
262
- require .Error (err )
263
- require .Equal (state . ErrStateNotExist , errors . Cause ( err ))
253
+ require .NoError (err )
254
+ require .Equal (1 , len ( bis ))
264
255
265
256
_ , err = sf .State (& bis , protocol .NamespaceOption (staking .StakingNameSpace ),
266
257
protocol .KeyOption (addrKeyWithPrefix (cand1Addr , _candIndex )))
267
258
require .NoError (err )
268
- require .Equal (1 , len (bis ))
259
+ require .Equal (2 , len (bis ))
269
260
270
261
// check candidate account state
271
- candidateBalance , _ := big .NewInt (0 ).SetString (initBalance , 10 )
272
- registrationFee , ok := new (big.Int ).SetString (cfg .Genesis .RegistrationConsts .Fee , 10 )
273
- require .True (ok )
274
- candidateBalance .Sub (candidateBalance , registrationFee )
275
- require .NoError (checkAccountState (cfg , sf , ws , false , candidateBalance .String (), cand1Addr ))
262
+ require .NoError (checkAccountState (cfg , sf , ws , true , big .NewInt (0 ).Sub (initBalance , selfStake ), cand1Addr ))
276
263
}
277
264
278
265
cfg := config .Default
@@ -318,9 +305,9 @@ func addrKeyWithPrefix(voterAddr address.Address, prefix byte) []byte {
318
305
func checkCandidateState (
319
306
sr protocol.StateReader ,
320
307
expectedName ,
321
- expectedOwnerAddr ,
308
+ expectedOwnerAddr string ,
322
309
expectedSelfStake ,
323
- expectedVotes string ,
310
+ expectedVotes * big. Int ,
324
311
candidateAddr address.Address ,
325
312
) error {
326
313
var cand staking.Candidate
@@ -333,10 +320,10 @@ func checkCandidateState(
333
320
if expectedOwnerAddr != cand .Owner .String () {
334
321
return errors .New ("Owner address does not match" )
335
322
}
336
- if expectedSelfStake != cand .SelfStake . String () {
323
+ if expectedSelfStake . Cmp ( cand .SelfStake ) != 0 {
337
324
return errors .New ("self stake does not match" )
338
325
}
339
- if expectedVotes != cand .Votes . String () {
326
+ if expectedVotes . Cmp ( cand .Votes ) != 0 {
340
327
return errors .New ("votes does not match" )
341
328
}
342
329
return nil
@@ -347,7 +334,7 @@ func checkAccountState(
347
334
sr protocol.StateReader ,
348
335
act action.SealedEnvelope ,
349
336
registrationFee bool ,
350
- expectedBalance string ,
337
+ expectedBalance * big. Int ,
351
338
accountAddr address.Address ,
352
339
) error {
353
340
cost , err := act .Cost ()
@@ -362,7 +349,7 @@ func checkAccountState(
362
349
if err != nil {
363
350
return err
364
351
}
365
- if expectedBalance != big . NewInt ( 0 ) .Add (acct1 .Balance , cost ). String () {
352
+ if expectedBalance . Cmp ( cost .Add (cost , acct1 .Balance )) != 0 {
366
353
return errors .New ("balance does not match" )
367
354
}
368
355
return nil
0 commit comments