Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gitferry committed Nov 22, 2024
1 parent 85689cb commit d729190
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion testutil/btcstaking-helper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (h *Helper) GenAndApplyCustomParams(
CovenantQuorum: 3,
MinStakingValueSat: 1000,
MaxStakingValueSat: int64(4 * 10e8),
MinStakingTimeBlocks: 10,
MinStakingTimeBlocks: 400,
MaxStakingTimeBlocks: 10000,
SlashingPkScript: slashingPkScript,
MinSlashingTxFeeSat: 10,
Expand Down
30 changes: 22 additions & 8 deletions x/btcstaking/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
testhelper "github.com/babylonlabs-io/babylon/testutil/helper"
bbn "github.com/babylonlabs-io/babylon/types"
btcctypes "github.com/babylonlabs-io/babylon/x/btccheckpoint/types"
btclctypes "github.com/babylonlabs-io/babylon/x/btclightclient/types"
"github.com/babylonlabs-io/babylon/x/btcstaking/types"
)

Expand Down Expand Up @@ -736,25 +737,26 @@ func TestDoNotAllowDelegationWithoutFinalityProvider(t *testing.T) {

// We only generate a finality provider, but not insert it into KVStore. So later
// insertion of delegation should fail.
_, fpPK, err := datagen.GenRandomBTCKeyPair(r)

fp, err := datagen.GenRandomFinalityProvider(r)
require.NoError(t, err)

/*
generate and insert valid new BTC delegation
*/
delSK, _, err := datagen.GenRandomBTCKeyPair(r)
require.NoError(t, err)
stakingTimeBlocks := uint16(5)
stakingTimeBlocks := bsParams.MinStakingTimeBlocks
stakingValue := int64(2 * 10e8)
testStakingInfo := datagen.GenBTCStakingSlashingInfo(
r,
t,
h.Net,
delSK,
[]*btcec.PublicKey{fpPK},
[]*btcec.PublicKey{fp.BtcPk.MustToBTCPK()},
covenantPKs,
bsParams.CovenantQuorum,
stakingTimeBlocks,
uint16(stakingTimeBlocks),
stakingValue,
bsParams.SlashingPkScript,
bsParams.SlashingRate,
Expand Down Expand Up @@ -793,14 +795,14 @@ func TestDoNotAllowDelegationWithoutFinalityProvider(t *testing.T) {
require.NoError(t, err)

stkTxHash := testStakingInfo.StakingTx.TxHash()
unbondingTime := 100 + 1
unbondingTime := bsParams.MinUnbondingTimeBlocks
unbondingValue := stakingValue - datagen.UnbondingTxFee // TODO: parameterise fee
testUnbondingInfo := datagen.GenBTCUnbondingSlashingInfo(
r,
t,
h.Net,
delSK,
[]*btcec.PublicKey{fpPK},
[]*btcec.PublicKey{fp.BtcPk.MustToBTCPK()},
covenantPKs,
bsParams.CovenantQuorum,
wire.NewOutPoint(&stkTxHash, datagen.StakingOutIdx),
Expand All @@ -816,10 +818,9 @@ func TestDoNotAllowDelegationWithoutFinalityProvider(t *testing.T) {
h.NoError(err)

// all good, construct and send MsgCreateBTCDelegation message
fpBTCPK := bbn.NewBIP340PubKeyFromBTCPK(fpPK)
msgCreateBTCDel := &types.MsgCreateBTCDelegation{
StakerAddr: stakerAddr.String(),
FpBtcPkList: []bbn.BIP340PubKey{*fpBTCPK},
FpBtcPkList: []bbn.BIP340PubKey{*fp.BtcPk},
BtcPk: bbn.NewBIP340PubKeyFromBTCPK(delSK.PubKey()),
Pop: pop,
StakingTime: uint32(stakingTimeBlocks),
Expand All @@ -838,6 +839,19 @@ func TestDoNotAllowDelegationWithoutFinalityProvider(t *testing.T) {
_, err = h.MsgServer.CreateBTCDelegation(h.Ctx, msgCreateBTCDel)
require.Error(t, err)
require.True(t, errors.Is(err, types.ErrFpNotFound))

AddFinalityProvider(t, h.Ctx, *h.BTCStakingKeeper, fp)
inclusionHeight := uint32(100)
inclusionHeader := &btclctypes.BTCHeaderInfo{
Header: &btcHeader,
Height: inclusionHeight,
}
tipHeight := 150
mockTipHeaderInfo := &btclctypes.BTCHeaderInfo{Height: uint32(tipHeight)}
btclcKeeper.EXPECT().GetHeaderByHash(gomock.Any(), btcHeader.Hash()).Return(inclusionHeader).Times(1)
btclcKeeper.EXPECT().GetTipInfo(gomock.Any()).Return(mockTipHeaderInfo).Times(1)
_, err = h.MsgServer.CreateBTCDelegation(h.Ctx, msgCreateBTCDel)
require.NoError(t, err)
}

func TestCorrectUnbondingTimeInDelegation(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion x/btcstaking/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func DefaultParams() Params {
CovenantQuorum: quorum,
MinStakingValueSat: 1000,
MaxStakingValueSat: 10 * 10e8,
MinStakingTimeBlocks: 10,
MinStakingTimeBlocks: 400, // this should be larger than minUnbonding
MaxStakingTimeBlocks: math.MaxUint16,
SlashingPkScript: defaultSlashingPkScript(),
MinSlashingTxFeeSat: 1000,
Expand Down

0 comments on commit d729190

Please sign in to comment.