|
5 | 5 | "errors"
|
6 | 6 | "math/rand"
|
7 | 7 | "testing"
|
| 8 | + "time" |
8 | 9 |
|
9 | 10 | sdkmath "cosmossdk.io/math"
|
10 | 11 |
|
@@ -405,3 +406,122 @@ func AddFinalityProvider(t *testing.T, goCtx context.Context, k btcstakingkeeper
|
405 | 406 | })
|
406 | 407 | require.NoError(t, err)
|
407 | 408 | }
|
| 409 | + |
| 410 | +func TestCorrectParamsVersionIsUsed(t *testing.T) { |
| 411 | + r := rand.New(rand.NewSource(time.Now().UnixNano())) |
| 412 | + ctrl := gomock.NewController(t) |
| 413 | + defer ctrl.Finish() |
| 414 | + |
| 415 | + btclcKeeper := types.NewMockBTCLightClientKeeper(ctrl) |
| 416 | + btccKeeper := types.NewMockBtcCheckpointKeeper(ctrl) |
| 417 | + btccKeeper.EXPECT().GetParams(gomock.Any()).Return(btcctypes.DefaultParams()).AnyTimes() |
| 418 | + keeper, ctx := testkeeper.BTCStakingKeeper(t, btclcKeeper, btccKeeper, nil) |
| 419 | + |
| 420 | + // covenant and slashing addr |
| 421 | + covenantSKs, covenantPKs, covenantQuorum := datagen.GenCovenantCommittee(r) |
| 422 | + slashingAddress, err := datagen.GenRandomBTCAddress(r, net) |
| 423 | + require.NoError(t, err) |
| 424 | + slashingPkScript, err := txscript.PayToAddrScript(slashingAddress) |
| 425 | + require.NoError(t, err) |
| 426 | + slashingChangeLockTime := uint16(101) |
| 427 | + |
| 428 | + // Generate a slashing rate in the range [0.1, 0.50] i.e., 10-50%. |
| 429 | + // NOTE - if the rate is higher or lower, it may produce slashing or change outputs |
| 430 | + // with value below the dust threshold, causing test failure. |
| 431 | + // Our goal is not to test failure due to such extreme cases here; |
| 432 | + // this is already covered in FuzzGeneratingValidStakingSlashingTx |
| 433 | + slashingRate := sdkmath.LegacyNewDecWithPrec(int64(datagen.RandomInt(r, 41)+10), 2) |
| 434 | + |
| 435 | + fp, err := datagen.GenRandomFinalityProvider(r) |
| 436 | + require.NoError(t, err) |
| 437 | + AddFinalityProvider(t, ctx, *keeper, fp) |
| 438 | + |
| 439 | + startHeight := uint32(datagen.RandomInt(r, 100)) + 1 |
| 440 | + btclcKeeper.EXPECT().GetTipInfo(gomock.Any()).Return(&btclctypes.BTCHeaderInfo{Height: startHeight}).AnyTimes() |
| 441 | + |
| 442 | + endHeight := uint32(datagen.RandomInt(r, 1000)) + startHeight + btcctypes.DefaultParams().CheckpointFinalizationTimeout + 1 |
| 443 | + stakingTime := endHeight - startHeight |
| 444 | + delSK, _, err := datagen.GenRandomBTCKeyPair(r) |
| 445 | + require.NoError(t, err) |
| 446 | + btcDel, err := datagen.GenRandomBTCDelegation( |
| 447 | + r, |
| 448 | + t, |
| 449 | + net, |
| 450 | + []bbn.BIP340PubKey{*fp.BtcPk}, |
| 451 | + delSK, |
| 452 | + covenantSKs, |
| 453 | + covenantPKs, |
| 454 | + covenantQuorum, |
| 455 | + slashingPkScript, |
| 456 | + stakingTime, startHeight, endHeight, 10000, |
| 457 | + slashingRate, |
| 458 | + slashingChangeLockTime, |
| 459 | + ) |
| 460 | + require.NoError(t, err) |
| 461 | + |
| 462 | + err = keeper.AddBTCDelegation(ctx, btcDel) |
| 463 | + require.NoError(t, err) |
| 464 | + |
| 465 | + // delegation is active as it have all covenant sigs |
| 466 | + txHash := btcDel.MustGetStakingTxHash().String() |
| 467 | + delView, err := keeper.BTCDelegation(ctx, &types.QueryBTCDelegationRequest{ |
| 468 | + StakingTxHashHex: txHash, |
| 469 | + }) |
| 470 | + require.NoError(t, err) |
| 471 | + require.NotNil(t, delView) |
| 472 | + |
| 473 | + require.True(t, delView.BtcDelegation.Active) |
| 474 | + |
| 475 | + dp := types.DefaultParams() |
| 476 | + |
| 477 | + // Generate 3 new key pairs and increase keys and quorum size in params, this |
| 478 | + // will mean new delegations will require more signatures to be active |
| 479 | + _, pk1, err := datagen.GenRandomBTCKeyPair(r) |
| 480 | + require.NoError(t, err) |
| 481 | + _, pk2, err := datagen.GenRandomBTCKeyPair(r) |
| 482 | + require.NoError(t, err) |
| 483 | + _, pk3, err := datagen.GenRandomBTCKeyPair(r) |
| 484 | + require.NoError(t, err) |
| 485 | + |
| 486 | + // Convert public keys to BIP340 format |
| 487 | + bip340pk1 := bbn.NewBIP340PubKeyFromBTCPK(pk1) |
| 488 | + bip340pk2 := bbn.NewBIP340PubKeyFromBTCPK(pk2) |
| 489 | + bip340pk3 := bbn.NewBIP340PubKeyFromBTCPK(pk3) |
| 490 | + |
| 491 | + dp.BtcActivationHeight = 10 |
| 492 | + dp.CovenantPks = append(dp.CovenantPks, *bip340pk1, *bip340pk2, *bip340pk3) |
| 493 | + dp.CovenantQuorum += 3 |
| 494 | + |
| 495 | + err = keeper.SetParams(ctx, dp) |
| 496 | + require.NoError(t, err) |
| 497 | + |
| 498 | + // check delegation is still active in every endpoint |
| 499 | + delView, err = keeper.BTCDelegation(ctx, &types.QueryBTCDelegationRequest{ |
| 500 | + StakingTxHashHex: txHash, |
| 501 | + }) |
| 502 | + require.NoError(t, err) |
| 503 | + require.NotNil(t, delView) |
| 504 | + |
| 505 | + require.True(t, delView.BtcDelegation.Active) |
| 506 | + |
| 507 | + delegationsView, err := keeper.BTCDelegations(ctx, &types.QueryBTCDelegationsRequest{ |
| 508 | + Status: types.BTCDelegationStatus_ACTIVE, |
| 509 | + }) |
| 510 | + require.NoError(t, err) |
| 511 | + require.NotNil(t, delegationsView) |
| 512 | + require.Len(t, delegationsView.BtcDelegations, 1) |
| 513 | + |
| 514 | + pagination := constructRequestWithLimit(r, 10) |
| 515 | + // Generate the initial query |
| 516 | + req := types.QueryFinalityProviderDelegationsRequest{ |
| 517 | + FpBtcPkHex: fp.BtcPk.MarshalHex(), |
| 518 | + Pagination: pagination, |
| 519 | + } |
| 520 | + |
| 521 | + fpView, err := keeper.FinalityProviderDelegations(ctx, &req) |
| 522 | + require.NoError(t, err) |
| 523 | + require.NotNil(t, fpView) |
| 524 | + require.Len(t, fpView.BtcDelegatorDelegations, 1) |
| 525 | + require.Len(t, fpView.BtcDelegatorDelegations[0].Dels, 1) |
| 526 | + require.True(t, fpView.BtcDelegatorDelegations[0].Dels[0].Active) |
| 527 | +} |
0 commit comments