github.com/lino-network/lino@v0.6.11/x/validator/handler_test.go (about) 1 package validator 2 3 // import ( 4 // "strconv" 5 // "testing" 6 7 // "github.com/lino-network/lino/types" 8 // "github.com/lino-network/lino/x/validator/model" 9 // "github.com/stretchr/testify/assert" 10 // "github.com/tendermint/tendermint/crypto" 11 // "github.com/tendermint/tendermint/crypto/secp256k1" 12 13 // sdk "github.com/cosmos/cosmos-sdk/types" 14 // ) 15 16 // func TestRegisterBasic(t *testing.T) { 17 // ctx, am, valManager, voteManager, gm := setupTest(t, 0) 18 // handler := NewHandler(am, valManager, voteManager, &gm) 19 // err := valManager.InitGenesis(ctx) 20 // if err != nil { 21 // panic(err) 22 // } 23 24 // valParam, _ := valManager.paramHolder.GetValidatorParam(ctx) 25 // minBalance := types.NewCoinFromInt64(1 * types.Decimals) 26 // user1 := createTestAccount(ctx, am, "user1", minBalance.Plus(valParam.ValidatorMinCommittingDeposit)) 27 28 // // let user1 register as voter first 29 // err = voteManager.AddVoter(ctx, "user1", valParam.ValidatorMinVotingDeposit) 30 // if err != nil { 31 // panic(err) 32 // } 33 34 // // let user1 register as validator 35 // deposit := coinToString(valParam.ValidatorMinCommittingDeposit) 36 // valKey := secp256k1.GenPrivKey().PubKey() 37 // msg := NewValidatorDepositMsg("user1", deposit, valKey, "") 38 // result := handler(ctx, msg) 39 // assert.Equal(t, sdk.Result{}, result) 40 41 // // check acc1's money has been withdrawn 42 // acc1Balance, _ := am.GetSavingFromUsername(ctx, user1) 43 // assert.Equal(t, acc1Balance, minBalance) 44 // assert.Equal(t, true, valManager.DoesValidatorExist(ctx, user1)) 45 46 // // now user1 should be the only validator 47 // verifyList, _ := valManager.storage.GetValidatorList(ctx) 48 // assert.Equal(t, verifyList.LowestPower, valParam.ValidatorMinCommittingDeposit) 49 // assert.Equal(t, 1, len(verifyList.OncallValidators)) 50 // assert.Equal(t, 1, len(verifyList.AllValidators)) 51 // assert.Equal(t, user1, verifyList.OncallValidators[0]) 52 // assert.Equal(t, user1, verifyList.AllValidators[0]) 53 54 // // make sure the validator's account info (power&pubKey) is correct 55 // verifyAccount, _ := valManager.storage.GetValidator(ctx, user1) 56 // assert.Equal(t, valParam.ValidatorMinCommittingDeposit, verifyAccount.Deposit) 57 // assert.Equal(t, valKey, verifyAccount.PubKey) 58 // } 59 60 // func TestRegisterFeeNotEnough(t *testing.T) { 61 // ctx, am, valManager, voteManager, gm := setupTest(t, 0) 62 // handler := NewHandler(am, valManager, voteManager, &gm) 63 // err := valManager.InitGenesis(ctx) 64 // if err != nil { 65 // panic(err) 66 // } 67 68 // valParam, _ := valManager.paramHolder.GetValidatorParam(ctx) 69 // minBalance := types.NewCoinFromInt64(1 * types.Decimals) 70 // createTestAccount(ctx, am, "user1", minBalance.Plus(valParam.ValidatorMinCommittingDeposit).Plus(valParam.ValidatorMinVotingDeposit)) 71 72 // // let user1 register as validator 73 // deposit := coinToString(valParam.ValidatorMinCommittingDeposit.Minus(types.NewCoinFromInt64(1000))) 74 // valKey := secp256k1.GenPrivKey().PubKey() 75 // msg := NewValidatorDepositMsg("user1", deposit, valKey, "") 76 77 // result := handler(ctx, msg) 78 // assert.Equal(t, ErrInsufficientDeposit().Result(), result) 79 80 // // let user register as voter 81 // err = voteManager.AddVoter(ctx, "user1", valParam.ValidatorMinVotingDeposit) 82 // if err != nil { 83 // panic(err) 84 // } 85 86 // result2 := handler(ctx, msg) 87 // assert.Equal(t, ErrInsufficientDeposit().Result(), result2) 88 89 // verifyList, _ := valManager.storage.GetValidatorList(ctx) 90 // assert.Equal(t, 0, len(verifyList.OncallValidators)) 91 // assert.Equal(t, 0, len(verifyList.AllValidators)) 92 // } 93 94 // func TestRevokeBasic(t *testing.T) { 95 // ctx, am, valManager, voteManager, gm := setupTest(t, 0) 96 // handler := NewHandler(am, valManager, voteManager, &gm) 97 // err := valManager.InitGenesis(ctx) 98 // if err != nil { 99 // panic(err) 100 // } 101 102 // valParam, _ := valManager.paramHolder.GetValidatorParam(ctx) 103 // minBalance := types.NewCoinFromInt64(1 * types.Decimals) 104 // user1 := createTestAccount(ctx, am, "user1", minBalance.Plus(valParam.ValidatorMinCommittingDeposit)) 105 106 // // let user1 register as voter first 107 // err = voteManager.AddVoter(ctx, "user1", valParam.ValidatorMinVotingDeposit) 108 // if err != nil { 109 // panic(err) 110 // } 111 112 // // let user1 register as validator 113 // valKey := secp256k1.GenPrivKey().PubKey() 114 // deposit := coinToString(valParam.ValidatorMinCommittingDeposit) 115 // msg := NewValidatorDepositMsg("user1", deposit, valKey, "") 116 // result := handler(ctx, msg) 117 // assert.Equal(t, sdk.Result{}, result) 118 119 // // now user1 should be the only validator 120 // verifyList, _ := valManager.storage.GetValidatorList(ctx) 121 // assert.Equal(t, user1, verifyList.OncallValidators[0]) 122 // assert.Equal(t, user1, verifyList.AllValidators[0]) 123 124 // // let user1 revoke candidancy 125 // msg2 := NewValidatorRevokeMsg("user1") 126 // result2 := handler(ctx, msg2) 127 // assert.Equal(t, sdk.Result{}, result2) 128 129 // verifyList2, _ := valManager.storage.GetValidatorList(ctx) 130 // assert.Equal(t, 0, len(verifyList2.OncallValidators)) 131 // assert.Equal(t, 0, len(verifyList2.AllValidators)) 132 133 // } 134 135 // func TestRevokeNonExistUser(t *testing.T) { 136 // ctx, am, valManager, voteManager, gm := setupTest(t, 0) 137 // handler := NewHandler(am, valManager, voteManager, &gm) 138 // err := valManager.InitGenesis(ctx) 139 // if err != nil { 140 // panic(err) 141 // } 142 143 // // let user1(not exists) revoke candidancy 144 // msg := NewValidatorRevokeMsg("user1") 145 // result := handler(ctx, msg) 146 // assert.Equal(t, model.ErrValidatorNotFound().Result(), result) 147 // } 148 149 // // this is the same situation as we find Byzantine and replace the Byzantine 150 // func TestRevokeOncallValidatorAndSubstitutionExists(t *testing.T) { 151 // ctx, am, valManager, voteManager, gm := setupTest(t, 0) 152 // handler := NewHandler(am, valManager, voteManager, &gm) 153 // err := valManager.InitGenesis(ctx) 154 // if err != nil { 155 // panic(err) 156 // } 157 158 // valParam, _ := valManager.paramHolder.GetValidatorParam(ctx) 159 // minBalance := types.NewCoinFromInt64(100000 * types.Decimals) 160 161 // // create 21 test users 162 // users := make([]types.AccountKey, 24) 163 // valKeys := make([]crypto.PubKey, 24) 164 // for i := 0; i < 24; i++ { 165 // users[i] = createTestAccount(ctx, am, "user"+strconv.Itoa(i+1), minBalance.Plus(valParam.ValidatorMinCommittingDeposit)) 166 167 // // let user register as voter first 168 // err := voteManager.AddVoter(ctx, types.AccountKey("user"+strconv.Itoa(i+1)), valParam.ValidatorMinVotingDeposit) 169 // if err != nil { 170 // panic(err) 171 // } 172 173 // // they will deposit min committing deposit + 10,20,30...200, 210, 220, 230, 240 174 // valMinCommitDeposit, _ := valParam.ValidatorMinCommittingDeposit.ToInt64() 175 // num := int64((i+1)*10) + valMinCommitDeposit/types.Decimals 176 // deposit := strconv.FormatInt(num, 10) 177 // valKeys[i] = secp256k1.GenPrivKey().PubKey() 178 // msg := NewValidatorDepositMsg("user"+strconv.Itoa(i+1), deposit, valKeys[i], "") 179 // result := handler(ctx, msg) 180 // assert.Equal(t, sdk.Result{}, result) 181 // } 182 183 // lst, _ := valManager.storage.GetValidatorList(ctx) 184 // assert.Equal(t, 21, len(lst.OncallValidators)) 185 // assert.Equal(t, 24, len(lst.AllValidators)) 186 // assert.Equal(t, valParam.ValidatorMinCommittingDeposit.Plus(types.NewCoinFromInt64(40*types.Decimals)), lst.LowestPower) 187 // assert.Equal(t, users[3], lst.LowestValidator) 188 189 // // lowest validator depoist coins will change the ranks 190 // deposit := types.LNO("15") 191 // msg := NewValidatorDepositMsg("user4", deposit, valKeys[3], "") 192 // result := handler(ctx, msg) 193 194 // lst2, _ := valManager.storage.GetValidatorList(ctx) 195 // assert.Equal(t, sdk.Result{}, result) 196 // assert.Equal(t, valParam.ValidatorMinCommittingDeposit.Plus(types.NewCoinFromInt64(50*types.Decimals)), lst2.LowestPower) 197 // assert.Equal(t, users[4], lst2.LowestValidator) 198 199 // // now user1, 2, 3 are substitutions 200 // // user2 can only withdraw 1-20 coins 201 // withdrawMsg := NewValidatorWithdrawMsg("user2", "100") 202 // resultWithdraw := handler(ctx, withdrawMsg) 203 // assert.Equal(t, ErrIllegalWithdraw().Result(), resultWithdraw) 204 205 // withdrawMsg2 := NewValidatorWithdrawMsg("user2", coinToString(valParam.ValidatorMinWithdraw)) 206 // resultWithdraw2 := handler(ctx, withdrawMsg2) 207 // assert.Equal(t, sdk.Result{}, resultWithdraw2) 208 // //revoke a non oncall valodator wont change anything related to oncall list 209 // revokeMsg := NewValidatorRevokeMsg("user2") 210 // result2 := handler(ctx, revokeMsg) 211 // assert.Equal(t, sdk.Result{}, result2) 212 213 // lst3, _ := valManager.storage.GetValidatorList(ctx) 214 // assert.Equal(t, valParam.ValidatorMinCommittingDeposit.Plus(types.NewCoinFromInt64(50*types.Decimals)), lst3.LowestPower) 215 // assert.Equal(t, users[4], lst3.LowestValidator) 216 // assert.Equal(t, 23, len(lst3.AllValidators)) 217 218 // // now only user1(min + 10) and user3(min + 30) are substitutions 219 // // the lowest oncall user is user5 with (min + 50) power 220 // // revoke user6 (could be byzantine) will make user3 (min + 30) join oncall 221 // // list become the lowest validator 222 // revokeMsg2 := NewValidatorRevokeMsg("user6") 223 // result3 := handler(ctx, revokeMsg2) 224 // assert.Equal(t, sdk.Result{}, result3) 225 226 // lst4, _ := valManager.storage.GetValidatorList(ctx) 227 // assert.Equal(t, valParam.ValidatorMinCommittingDeposit.Plus(types.NewCoinFromInt64(30*types.Decimals)), lst4.LowestPower) 228 // assert.Equal(t, users[2], lst4.LowestValidator) 229 // assert.Equal(t, 22, len(lst4.AllValidators)) 230 // } 231 232 // func TestRevokeAndDepositAgain(t *testing.T) { 233 // ctx, am, valManager, voteManager, gm := setupTest(t, 0) 234 // handler := NewHandler(am, valManager, voteManager, &gm) 235 // err := valManager.InitGenesis(ctx) 236 // if err != nil { 237 // panic(err) 238 // } 239 240 // valParam, _ := valManager.paramHolder.GetValidatorParam(ctx) 241 // minBalance := types.NewCoinFromInt64(1 * types.Decimals) 242 // createTestAccount(ctx, am, "user1", minBalance.Plus(valParam.ValidatorMinCommittingDeposit).Plus(valParam.ValidatorMinCommittingDeposit)) 243 244 // // let user1 register as voter first 245 // err = voteManager.AddVoter(ctx, "user1", valParam.ValidatorMinVotingDeposit) 246 // if err != nil { 247 // panic(err) 248 // } 249 250 // // let user1 register as validator 251 // valKey := secp256k1.GenPrivKey().PubKey() 252 // deposit := coinToString(valParam.ValidatorMinCommittingDeposit) 253 // msg := NewValidatorDepositMsg("user1", deposit, valKey, "") 254 // result := handler(ctx, msg) 255 // assert.Equal(t, sdk.Result{}, result) 256 257 // lst, _ := valManager.storage.GetValidatorList(ctx) 258 // assert.Equal(t, 1, len(lst.AllValidators)) 259 // assert.Equal(t, 1, len(lst.OncallValidators)) 260 261 // // let user1 revoke candidancy 262 // msg2 := NewValidatorRevokeMsg("user1") 263 // result2 := handler(ctx, msg2) 264 // assert.Equal(t, sdk.Result{}, result2) 265 266 // lstEmpty, _ := valManager.storage.GetValidatorList(ctx) 267 // assert.Equal(t, 0, len(lstEmpty.AllValidators)) 268 // assert.Equal(t, 0, len(lstEmpty.OncallValidators)) 269 270 // // deposit again 271 // msg3 := NewValidatorDepositMsg("user1", deposit, valKey, "") 272 // result3 := handler(ctx, msg3) 273 274 // lst2, _ := valManager.storage.GetValidatorList(ctx) 275 // assert.Equal(t, sdk.Result{}, result3) 276 // assert.Equal(t, 1, len(lst2.AllValidators)) 277 // assert.Equal(t, 1, len(lst2.OncallValidators)) 278 279 // } 280 281 // func TestWithdrawBasic(t *testing.T) { 282 // ctx, am, valManager, voteManager, gm := setupTest(t, 0) 283 // handler := NewHandler(am, valManager, voteManager, &gm) 284 // err := valManager.InitGenesis(ctx) 285 // if err != nil { 286 // panic(err) 287 // } 288 289 // valParam, _ := valManager.paramHolder.GetValidatorParam(ctx) 290 // minBalance := types.NewCoinFromInt64(1 * types.Decimals) 291 // user1 := createTestAccount(ctx, am, "user1", minBalance.Plus(valParam.ValidatorMinCommittingDeposit)) 292 293 // // let user1 register as voter first 294 // err = voteManager.AddVoter(ctx, "user1", valParam.ValidatorMinVotingDeposit) 295 // if err != nil { 296 // panic(err) 297 // } 298 299 // // let user1 register as validator 300 // valKey := secp256k1.GenPrivKey().PubKey() 301 // deposit := coinToString(valParam.ValidatorMinCommittingDeposit) 302 // msg := NewValidatorDepositMsg("user1", deposit, valKey, "") 303 // result := handler(ctx, msg) 304 // assert.Equal(t, sdk.Result{}, result) 305 306 // // now user1 should be the only validator 307 // verifyList, _ := valManager.storage.GetValidatorList(ctx) 308 // assert.Equal(t, user1, verifyList.OncallValidators[0]) 309 // assert.Equal(t, user1, verifyList.AllValidators[0]) 310 311 // // user1 cannot withdraw if is oncall validator 312 // withdrawMsg := NewValidatorWithdrawMsg("user1", coinToString(valParam.ValidatorMinWithdraw)) 313 // result2 := handler(ctx, withdrawMsg) 314 // assert.Equal(t, ErrIllegalWithdraw().Result(), result2) 315 316 // } 317 318 // func TestDepositBasic(t *testing.T) { 319 // ctx, am, valManager, voteManager, gm := setupTest(t, 0) 320 // handler := NewHandler(am, valManager, voteManager, &gm) 321 // err := valManager.InitGenesis(ctx) 322 // if err != nil { 323 // panic(err) 324 // } 325 326 // // create test user 327 // valParam, _ := valManager.paramHolder.GetValidatorParam(ctx) 328 // minBalance := types.NewCoinFromInt64(1 * types.Decimals) 329 // user1 := createTestAccount(ctx, am, "user1", minBalance.Plus(valParam.ValidatorMinCommittingDeposit)) 330 331 // // let user1 register as voter first 332 // err = voteManager.AddVoter(ctx, "user1", valParam.ValidatorMinVotingDeposit) 333 // if err != nil { 334 // panic(err) 335 // } 336 337 // // let user1 register as validator 338 // valKey := secp256k1.GenPrivKey().PubKey() 339 // deposit := coinToString(valParam.ValidatorMinCommittingDeposit) 340 // msg := NewValidatorDepositMsg("user1", deposit, valKey, "") 341 // result := handler(ctx, msg) 342 // assert.Equal(t, sdk.Result{}, result) 343 344 // // check acc1's money has been withdrawn 345 // acc1Balance, _ := am.GetSavingFromUsername(ctx, user1) 346 // assert.Equal(t, acc1Balance, minBalance) 347 // assert.Equal(t, true, valManager.DoesValidatorExist(ctx, user1)) 348 349 // verifyList, _ := valManager.storage.GetValidatorList(ctx) 350 // assert.Equal(t, valParam.ValidatorMinCommittingDeposit, verifyList.LowestPower) 351 // assert.Equal(t, 1, len(verifyList.OncallValidators)) 352 // assert.Equal(t, 1, len(verifyList.AllValidators)) 353 // assert.Equal(t, user1, verifyList.OncallValidators[0]) 354 // assert.Equal(t, user1, verifyList.AllValidators[0]) 355 356 // // check deposit and power is correct 357 // validator, _ := valManager.storage.GetValidator(ctx, user1) 358 // assert.Equal(t, true, validator.Deposit.IsEqual(valParam.ValidatorMinCommittingDeposit)) 359 // } 360 361 // func TestCommittingDepositExceedVotingDeposit(t *testing.T) { 362 // ctx, am, valManager, voteManager, gm := setupTest(t, 0) 363 // handler := NewHandler(am, valManager, voteManager, &gm) 364 // err := valManager.InitGenesis(ctx) 365 // if err != nil { 366 // panic(err) 367 // } 368 369 // // create test user 370 // valParam, _ := valManager.paramHolder.GetValidatorParam(ctx) 371 // minBalance := types.NewCoinFromInt64(1000 * types.Decimals) 372 // createTestAccount(ctx, am, "user1", minBalance.Plus(valParam.ValidatorMinVotingDeposit)) 373 374 // // let user1 register as voter first 375 // err = voteManager.AddVoter(ctx, "user1", valParam.ValidatorMinVotingDeposit) 376 // if err != nil { 377 // panic(err) 378 // } 379 380 // // let user1 register as validator 381 // valKey := secp256k1.GenPrivKey().PubKey() 382 // deposit := coinToString(valParam.ValidatorMinVotingDeposit.Plus(types.NewCoinFromInt64(2 * types.Decimals))) 383 // msg := NewValidatorDepositMsg("user1", deposit, valKey, "") 384 // result := handler(ctx, msg) 385 // assert.Equal(t, ErrUnbalancedAccount().Result(), result) 386 // } 387 388 // func TestDepositWithoutLinoAccount(t *testing.T) { 389 // ctx, am, valManager, voteManager, gm := setupTest(t, 0) 390 // handler := NewHandler(am, valManager, voteManager, &gm) 391 // err := valManager.InitGenesis(ctx) 392 // if err != nil { 393 // panic(err) 394 // } 395 // valParam, _ := valManager.paramHolder.GetValidatorParam(ctx) 396 397 // valKey := secp256k1.GenPrivKey().PubKey() 398 // msg := NewValidatorDepositMsg("qwqwndqwnd", coinToString(valParam.ValidatorMinWithdraw), valKey, "") 399 // result := handler(ctx, msg) 400 // assert.Equal(t, ErrAccountNotFound().Result(), result) 401 // } 402 403 // func TestValidatorReplacement(t *testing.T) { 404 // ctx, am, valManager, voteManager, gm := setupTest(t, 0) 405 // handler := NewHandler(am, valManager, voteManager, &gm) 406 // err := valManager.InitGenesis(ctx) 407 // if err != nil { 408 // panic(err) 409 // } 410 411 // valParam, _ := valManager.paramHolder.GetValidatorParam(ctx) 412 // minBalance := types.NewCoinFromInt64(100000 * types.Decimals) 413 414 // // create 21 test users 415 // users := make([]types.AccountKey, 21) 416 // valKeys := make([]crypto.PubKey, 21) 417 // for i := 0; i < 21; i++ { 418 // users[i] = createTestAccount(ctx, am, "user"+strconv.Itoa(i+1), minBalance.Plus(valParam.ValidatorMinCommittingDeposit)) 419 // err := voteManager.AddVoter(ctx, types.AccountKey("user"+strconv.Itoa(i+1)), valParam.ValidatorMinVotingDeposit) 420 // if err != nil { 421 // panic(err) 422 // } 423 424 // // they will deposit min committing deposit + 10,20,30...200, 210, 220, 230, 240 425 // valMinCommitDeposit, _ := valParam.ValidatorMinCommittingDeposit.ToInt64() 426 // num := int64((i+1)*10) + valMinCommitDeposit/types.Decimals 427 // deposit := strconv.FormatInt(num, 10) 428 // valKeys[i] = secp256k1.GenPrivKey().PubKey() 429 // msg := NewValidatorDepositMsg("user"+strconv.Itoa(i+1), deposit, valKeys[i], "") 430 // result := handler(ctx, msg) 431 // assert.Equal(t, sdk.Result{}, result) 432 // } 433 434 // // check validator list, the lowest power is 10 435 // verifyList, _ := valManager.storage.GetValidatorList(ctx) 436 // assert.Equal(t, true, 437 // verifyList.LowestPower.IsEqual(valParam.ValidatorMinCommittingDeposit.Plus(types.NewCoinFromInt64(10*types.Decimals)))) 438 // assert.Equal(t, users[0], verifyList.LowestValidator) 439 // assert.Equal(t, 21, len(verifyList.OncallValidators)) 440 // assert.Equal(t, 21, len(verifyList.AllValidators)) 441 442 // // create a user failed to join oncall validator list (not enough power) 443 // createTestAccount(ctx, am, "noPowerUser", minBalance.Plus(valParam.ValidatorMinCommittingDeposit)) 444 // err = voteManager.AddVoter(ctx, "noPowerUser", valParam.ValidatorMinVotingDeposit) 445 // if err != nil { 446 // panic(err) 447 // } 448 449 // // let user1 register as validator 450 // valKey := secp256k1.GenPrivKey().PubKey() 451 // deposit := coinToString(valParam.ValidatorMinCommittingDeposit) 452 // msg := NewValidatorDepositMsg("noPowerUser", deposit, valKey, "") 453 // result := handler(ctx, msg) 454 // assert.Equal(t, sdk.Result{}, result) 455 456 // //check the user hasn't been added to oncall validators but in the pool 457 // verifyList2, _ := valManager.storage.GetValidatorList(ctx) 458 // assert.Equal(t, sdk.Result{}, result) 459 // assert.Equal(t, true, 460 // verifyList2.LowestPower.IsEqual(valParam.ValidatorMinCommittingDeposit.Plus(types.NewCoinFromInt64(10*types.Decimals)))) 461 // assert.Equal(t, users[0], verifyList2.LowestValidator) 462 // assert.Equal(t, 21, len(verifyList2.OncallValidators)) 463 // assert.Equal(t, 22, len(verifyList2.AllValidators)) 464 465 // // create a user success to join oncall validator list 466 // createTestAccount(ctx, am, "powerfulUser", minBalance.Plus(valParam.ValidatorMinCommittingDeposit)) 467 // // let user register as voter first 468 // err = voteManager.AddVoter(ctx, "powerfulUser", valParam.ValidatorMinVotingDeposit) 469 // if err != nil { 470 // panic(err) 471 // } 472 473 // //check the user has been added to oncall validators and in the pool 474 // valKey = secp256k1.GenPrivKey().PubKey() 475 // deposit = coinToString(valParam.ValidatorMinCommittingDeposit.Plus(types.NewCoinFromInt64(88 * types.Decimals))) 476 // msg = NewValidatorDepositMsg("powerfulUser", deposit, valKey, "") 477 // result = handler(ctx, msg) 478 // assert.Equal(t, sdk.Result{}, result) 479 480 // verifyList3, _ := valManager.storage.GetValidatorList(ctx) 481 // assert.Equal(t, true, 482 // verifyList3.LowestPower.IsEqual(valParam.ValidatorMinCommittingDeposit.Plus(types.NewCoinFromInt64(20*types.Decimals)))) 483 // assert.Equal(t, users[1], verifyList3.LowestValidator) 484 // assert.Equal(t, 21, len(verifyList3.OncallValidators)) 485 // assert.Equal(t, 23, len(verifyList3.AllValidators)) 486 487 // // check user0 has been replaced, and powerful user has been added 488 // flag := false 489 // for _, username := range verifyList3.OncallValidators { 490 // if username == "powerfulUser" { 491 // flag = true 492 // } 493 // if username == "user0" { 494 // assert.Fail(t, "User0 should have been replaced") 495 // } 496 // } 497 // if !flag { 498 // assert.Fail(t, "Powerful user should have been added") 499 // } 500 // } 501 502 // func TestRemoveBasic(t *testing.T) { 503 // ctx, am, valManager, voteManager, gm := setupTest(t, 0) 504 // handler := NewHandler(am, valManager, voteManager, &gm) 505 // err := valManager.InitGenesis(ctx) 506 // if err != nil { 507 // panic(err) 508 // } 509 510 // // create two test users 511 // valParam, _ := valManager.paramHolder.GetValidatorParam(ctx) 512 513 // minBalance := types.NewCoinFromInt64(1 * types.Decimals) 514 // goodUser := createTestAccount(ctx, am, "goodUser", minBalance.Plus(valParam.ValidatorMinCommittingDeposit)) 515 // createTestAccount(ctx, am, "badUser", minBalance.Plus(valParam.ValidatorMinCommittingDeposit)) 516 517 // valKey1 := secp256k1.GenPrivKey().PubKey() 518 // valKey2 := secp256k1.GenPrivKey().PubKey() 519 520 // err = voteManager.AddVoter(ctx, "goodUser", valParam.ValidatorMinVotingDeposit) 521 // if err != nil { 522 // panic(err) 523 // } 524 // err = voteManager.AddVoter(ctx, "badUser", valParam.ValidatorMinVotingDeposit) 525 // if err != nil { 526 // panic(err) 527 // } 528 529 // // let both users register as validator 530 // msg1 := NewValidatorDepositMsg("goodUser", coinToString(valParam.ValidatorMinCommittingDeposit), valKey1, "") 531 // msg2 := NewValidatorDepositMsg("badUser", coinToString(valParam.ValidatorMinCommittingDeposit), valKey2, "") 532 // handler(ctx, msg1) 533 // handler(ctx, msg2) 534 535 // verifyList, _ := valManager.storage.GetValidatorList(ctx) 536 // assert.Equal(t, 2, len(verifyList.OncallValidators)) 537 // assert.Equal(t, 2, len(verifyList.AllValidators)) 538 539 // err = valManager.RemoveValidatorFromAllLists(ctx, "badUser") 540 // if err != nil { 541 // panic(err) 542 // } 543 // verifyList2, _ := valManager.storage.GetValidatorList(ctx) 544 // assert.Equal(t, 1, len(verifyList2.OncallValidators)) 545 // assert.Equal(t, 1, len(verifyList2.AllValidators)) 546 // assert.Equal(t, goodUser, verifyList2.OncallValidators[0]) 547 // assert.Equal(t, goodUser, verifyList2.AllValidators[0]) 548 // } 549 550 // func TestRegisterWithDupKey(t *testing.T) { 551 // ctx, am, valManager, voteManager, gm := setupTest(t, 0) 552 // handler := NewHandler(am, valManager, voteManager, &gm) 553 // err := valManager.InitGenesis(ctx) 554 // if err != nil { 555 // panic(err) 556 // } 557 558 // valParam, _ := valManager.paramHolder.GetValidatorParam(ctx) 559 560 // minBalance := types.NewCoinFromInt64(1 * types.Decimals) 561 // createTestAccount(ctx, am, "user1", minBalance.Plus(valParam.ValidatorMinCommittingDeposit)) 562 // createTestAccount(ctx, am, "user2", minBalance.Plus(valParam.ValidatorMinCommittingDeposit)) 563 564 // valKey1 := secp256k1.GenPrivKey().PubKey() 565 566 // err = voteManager.AddVoter(ctx, "user1", valParam.ValidatorMinVotingDeposit) 567 // if err != nil { 568 // panic(err) 569 // } 570 // err = voteManager.AddVoter(ctx, "user2", valParam.ValidatorMinVotingDeposit) 571 // if err != nil { 572 // panic(err) 573 // } 574 575 // // let both users register as validator 576 // msg1 := NewValidatorDepositMsg("user1", coinToString(valParam.ValidatorMinCommittingDeposit), valKey1, "") 577 // msg2 := NewValidatorDepositMsg("user2", coinToString(valParam.ValidatorMinCommittingDeposit), valKey1, "") 578 // handler(ctx, msg1) 579 580 // result2 := handler(ctx, msg2) 581 // assert.Equal(t, ErrValidatorPubKeyAlreadyExist().Result(), result2) 582 583 // } 584 585 // func TestAddFrozenMoney(t *testing.T) { 586 // ctx, am, valManager, _, gm := setupTest(t, 0) 587 // err := valManager.InitGenesis(ctx) 588 // if err != nil { 589 // panic(err) 590 // } 591 592 // minBalance := types.NewCoinFromInt64(1 * types.Decimals) 593 // user := createTestAccount(ctx, am, "user", minBalance) 594 595 // testCases := []struct { 596 // testName string 597 // times int64 598 // interval int64 599 // returnedCoin types.Coin 600 // expectedFrozenListLen int 601 // expectedFrozenMoney types.Coin 602 // expectedFrozenTimes int64 603 // expectedFrozenInterval int64 604 // }{ 605 // { 606 // testName: "return coin to user", 607 // times: 10, 608 // interval: 2, 609 // returnedCoin: types.NewCoinFromInt64(100), 610 // expectedFrozenListLen: 1, 611 // expectedFrozenMoney: types.NewCoinFromInt64(100), 612 // expectedFrozenTimes: 10, 613 // expectedFrozenInterval: 2, 614 // }, 615 // { 616 // testName: "return coin to user again", 617 // times: 100000, 618 // interval: 20000, 619 // returnedCoin: types.NewCoinFromInt64(100000), 620 // expectedFrozenListLen: 2, 621 // expectedFrozenMoney: types.NewCoinFromInt64(100000), 622 // expectedFrozenTimes: 100000, 623 // expectedFrozenInterval: 20000, 624 // }, 625 // } 626 627 // for _, tc := range testCases { 628 // err := returnCoinTo( 629 // ctx, "user", &gm, am, tc.times, tc.interval, tc.returnedCoin) 630 // if err != nil { 631 // t.Errorf("%s: failed to return coin, got err %v", tc.testName, err) 632 // } 633 634 // addr, err := am.GetAddress(ctx, user) 635 // if err != nil { 636 // t.Errorf("%s: failed to get address, got err %v", tc.testName, err) 637 // } 638 // lst, err := am.GetFrozenMoneyList(ctx, addr) 639 // if err != nil { 640 // t.Errorf("%s: failed to get frozen money list, got err %v", tc.testName, err) 641 // } 642 // if len(lst) != tc.expectedFrozenListLen { 643 // t.Errorf("%s: diff list len, got %v, want %v", tc.testName, len(lst), tc.expectedFrozenListLen) 644 // } 645 // if !lst[len(lst)-1].Amount.IsEqual(tc.expectedFrozenMoney) { 646 // t.Errorf("%s: diff amount, got %v, want %v", tc.testName, lst[len(lst)-1].Amount, tc.expectedFrozenMoney) 647 // } 648 // if lst[len(lst)-1].Times != tc.expectedFrozenTimes { 649 // t.Errorf("%s: diff times, got %v, want %v", tc.testName, lst[len(lst)-1].Times, tc.expectedFrozenTimes) 650 // } 651 // if lst[len(lst)-1].Interval != tc.expectedFrozenInterval { 652 // t.Errorf("%s: diff interval, got %v, want %v", tc.testName, lst[len(lst)-1].Interval, tc.expectedFrozenInterval) 653 // } 654 // } 655 // }