@@ -927,15 +927,10 @@ func TestProtocol_HandleUnstake(t *testing.T) {
927
927
initBalance int64
928
928
selfstaking bool
929
929
// action fields
930
- index uint64
931
- gasPrice * big.Int
932
- gasLimit uint64
933
- nonce uint64
930
+ index uint64
934
931
// block context
935
- blkHeight uint64
936
932
blkTimestamp time.Time
937
933
ctxTimestamp time.Time
938
- blkGasLimit uint64
939
934
// clear flag for inMemCandidates
940
935
clear bool
941
936
// need new p
@@ -953,13 +948,8 @@ func TestProtocol_HandleUnstake(t *testing.T) {
953
948
101 ,
954
949
false ,
955
950
0 ,
956
- big .NewInt (unit .Qev ),
957
- 10000 ,
958
- 1 ,
959
- 1 ,
960
951
time .Now (),
961
952
time .Now (),
962
- 10000 ,
963
953
false ,
964
954
true ,
965
955
nil ,
@@ -974,13 +964,8 @@ func TestProtocol_HandleUnstake(t *testing.T) {
974
964
101 ,
975
965
false ,
976
966
0 ,
977
- big .NewInt (unit .Qev ),
978
- 10000 ,
979
- 1 ,
980
- 1 ,
981
967
time .Now (),
982
968
time .Now (),
983
- 10000 ,
984
969
false ,
985
970
false ,
986
971
nil ,
@@ -996,13 +981,8 @@ func TestProtocol_HandleUnstake(t *testing.T) {
996
981
101 ,
997
982
false ,
998
983
1 ,
999
- big .NewInt (unit .Qev ),
1000
- 10000 ,
1001
- 1 ,
1002
- 1 ,
1003
984
time .Now (),
1004
985
time .Now (),
1005
- 10000 ,
1006
986
false ,
1007
987
true ,
1008
988
nil ,
@@ -1017,13 +997,8 @@ func TestProtocol_HandleUnstake(t *testing.T) {
1017
997
101 ,
1018
998
false ,
1019
999
0 ,
1020
- big .NewInt (unit .Qev ),
1021
- 10000 ,
1022
- 1 ,
1023
- 1 ,
1024
1000
time .Now (),
1025
1001
time .Now (),
1026
- 10000 ,
1027
1002
true ,
1028
1003
true ,
1029
1004
nil ,
@@ -1038,13 +1013,8 @@ func TestProtocol_HandleUnstake(t *testing.T) {
1038
1013
101 ,
1039
1014
false ,
1040
1015
0 ,
1041
- big .NewInt (unit .Qev ),
1042
- 10000 ,
1043
- 1 ,
1044
- 1 ,
1045
1016
time .Now (),
1046
1017
time .Now (),
1047
- 10000 ,
1048
1018
false ,
1049
1019
true ,
1050
1020
nil ,
@@ -1059,13 +1029,8 @@ func TestProtocol_HandleUnstake(t *testing.T) {
1059
1029
101 ,
1060
1030
false ,
1061
1031
0 ,
1062
- big .NewInt (unit .Qev ),
1063
- 10000 ,
1064
- 1 ,
1065
- 1 ,
1066
1032
time .Now (),
1067
1033
time .Now ().Add (time .Duration (1 ) * 24 * time .Hour ),
1068
- 10000 ,
1069
1034
false ,
1070
1035
true ,
1071
1036
nil ,
@@ -1081,29 +1046,34 @@ func TestProtocol_HandleUnstake(t *testing.T) {
1081
1046
101 ,
1082
1047
false ,
1083
1048
0 ,
1084
- big .NewInt (unit .Qev ),
1085
- 10000 ,
1086
- 1 ,
1087
- 1 ,
1088
1049
time .Now (),
1089
1050
time .Now ().Add (time .Duration (1 ) * 24 * time .Hour ),
1090
- 10000 ,
1091
1051
false ,
1092
1052
true ,
1093
1053
nil ,
1094
1054
iotextypes .ReceiptStatus_Success ,
1095
1055
},
1096
1056
}
1097
1057
1058
+ var (
1059
+ ctx context.Context
1060
+ gasPrice = big .NewInt (unit .Qev )
1061
+ gasLimit uint64 = 10000
1062
+ nonce uint64 = 1
1063
+ blkHeight uint64 = 1
1064
+ )
1065
+
1098
1066
for _ , test := range tests {
1099
1067
if test .newProtocol {
1100
1068
sm , p , candidate , _ = initAll (t , ctrl )
1101
1069
} else {
1102
1070
candidate = candidate2
1103
1071
}
1104
- ctx , createCost := initCreateStake (t , sm , test .caller , test .initBalance , big .NewInt (unit .Qev ), test .gasLimit , test .nonce , test .blkHeight , test .blkTimestamp , test .blkGasLimit , p , candidate , test .amount , test .autoStake )
1105
- act , err := action .NewUnstake (test .nonce , test .index ,
1106
- nil , test .gasLimit , test .gasPrice )
1072
+
1073
+ var createCost * big.Int
1074
+ ctx , createCost = initCreateStake (t , sm , test .caller , test .initBalance , big .NewInt (unit .Qev ), gasLimit , nonce , blkHeight , test .blkTimestamp , gasLimit , p , candidate , test .amount , test .autoStake )
1075
+ act , err := action .NewUnstake (nonce , test .index ,
1076
+ nil , gasLimit , gasPrice )
1107
1077
require .NoError (err )
1108
1078
if test .blkTimestamp != test .ctxTimestamp {
1109
1079
blkCtx := protocol .MustGetBlockCtx (ctx )
@@ -1160,11 +1130,67 @@ func TestProtocol_HandleUnstake(t *testing.T) {
1160
1130
require .NoError (err )
1161
1131
actCost , err := act .Cost ()
1162
1132
require .NoError (err )
1163
- require .Equal (test . nonce , caller .Nonce )
1133
+ require .Equal (nonce , caller .Nonce )
1164
1134
total := big .NewInt (0 )
1165
1135
require .Equal (unit .ConvertIotxToRau (test .initBalance ), total .Add (total , caller .Balance ).Add (total , actCost ).Add (total , createCost ))
1166
1136
}
1167
1137
}
1138
+
1139
+ // verify bucket unstaked
1140
+ vb , err := getBucket (sm , 0 )
1141
+ require .NoError (err )
1142
+ require .True (vb .isUnstaked ())
1143
+
1144
+ unstake , err := action .NewUnstake (nonce + 1 , 0 , nil , gasLimit , gasPrice )
1145
+ require .NoError (err )
1146
+ changeCand , err := action .NewChangeCandidate (nonce + 1 , candidate2 .Name , 0 , nil , gasLimit , gasPrice )
1147
+ require .NoError (err )
1148
+ deposit , err := action .NewDepositToStake (nonce + 1 , 0 , "10000" , nil , gasLimit , gasPrice )
1149
+ require .NoError (err )
1150
+ restake , err := action .NewRestake (nonce + 1 , 0 , 0 , false , nil , gasLimit , gasPrice )
1151
+ require .NoError (err )
1152
+
1153
+ unstakedBucketTests := []struct {
1154
+ act action.Action
1155
+ greenland bool
1156
+ status iotextypes.ReceiptStatus
1157
+ }{
1158
+ // unstake an already unstaked bucket again not allowed
1159
+ {unstake , true , iotextypes .ReceiptStatus_ErrInvalidBucketType },
1160
+ // change candidate for an unstaked bucket not allowed
1161
+ {changeCand , true , iotextypes .ReceiptStatus_ErrInvalidBucketType },
1162
+ // deposit to unstaked bucket not allowed
1163
+ {deposit , true , iotextypes .ReceiptStatus_ErrInvalidBucketType },
1164
+ // restake an unstaked bucket not allowed
1165
+ {restake , true , iotextypes .ReceiptStatus_ErrInvalidBucketType },
1166
+ // restake an unstaked bucket is allowed pre-Greenland
1167
+ {restake , false , iotextypes .ReceiptStatus_ErrNotEnoughBalance },
1168
+ }
1169
+
1170
+ for _ , v := range unstakedBucketTests {
1171
+ greenland := genesis .Default
1172
+ if v .greenland {
1173
+ blkCtx := protocol .MustGetBlockCtx (ctx )
1174
+ greenland .GreenlandBlockHeight = blkCtx .BlockHeight
1175
+ }
1176
+ ctx = protocol .WithBlockchainCtx (ctx , protocol.BlockchainCtx {
1177
+ Genesis : greenland ,
1178
+ })
1179
+ _ , err = p .Start (ctx , sm )
1180
+ require .NoError (err )
1181
+ r , err := p .Handle (ctx , v .act , sm )
1182
+ require .NoError (err )
1183
+ require .EqualValues (v .status , r .Status )
1184
+
1185
+ if ! v .greenland {
1186
+ // pre-Greenland allows restaking an unstaked bucket, and it is considered staked afterwards
1187
+ vb , err := getBucket (sm , 0 )
1188
+ require .NoError (err )
1189
+ require .True (vb .StakeStartTime .Unix () != 0 )
1190
+ require .True (vb .UnstakeStartTime .Unix () != 0 )
1191
+ require .False (vb .isUnstaked ())
1192
+ }
1193
+ }
1168
1194
}
1169
1195
1170
1196
func TestProtocol_HandleWithdrawStake (t * testing.T ) {
0 commit comments