Skip to content

Commit

Permalink
more refundable tx
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianElvis committed Oct 3, 2024
1 parent bdfd748 commit 73735ff
Show file tree
Hide file tree
Showing 22 changed files with 168 additions and 57 deletions.
1 change: 1 addition & 0 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ func (ak *AppKeepers) InitKeepers(
// setting the finality keeper as nil for now
// need to set it after finality keeper is initiated
nil,
&ak.IncentiveKeeper,
btcNetParams,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
Expand Down
38 changes: 18 additions & 20 deletions test/e2e/btc_staking_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,16 @@ func (s *BTCStakingTestSuite) Test2SubmitCovenantSignature() {
s.NoError(err)

for i := 0; i < int(s.covenantQuorum); i++ {
nonValidatorNode.AddCovenantSigs(
covenantSlashingSigs[i].CovPk,
stakingTxHash,
covenantSlashingSigs[i].AdaptorSigs,
bbn.NewBIP340SignatureFromBTCSig(covUnbondingSigs[i]),
covenantUnbondingSlashingSigs[i].AdaptorSigs,
)
nonValidatorNode.SubmitRefundableTxWithAssertion(func() {
// add covenant sigs
nonValidatorNode.AddCovenantSigs(
covenantSlashingSigs[i].CovPk,
stakingTxHash,
covenantSlashingSigs[i].AdaptorSigs,
bbn.NewBIP340SignatureFromBTCSig(covUnbondingSigs[i]),
covenantUnbondingSlashingSigs[i].AdaptorSigs,
)
})
// wait for a block so that above txs take effect
nonValidatorNode.WaitForNextBlock()
}
Expand Down Expand Up @@ -346,12 +349,10 @@ func (s *BTCStakingTestSuite) Test3CommitPublicRandomnessAndSubmitFinalitySignat
s.NoError(err)
eotsSig := bbn.NewSchnorrEOTSSigFromModNScalar(sig)

// balance before the finality signature is submitted
submitterBalanceBefore, err := nonValidatorNode.QueryBalances(s.cacheFP.Addr)
s.NoError(err)

// submit finality signature
nonValidatorNode.AddFinalitySig(s.cacheFP.BtcPk, activatedHeight, &randListInfo.PRList[idx], *randListInfo.ProofList[idx].ToProto(), appHash, eotsSig)
nonValidatorNode.SubmitRefundableTxWithAssertion(func() {
// submit finality signature
nonValidatorNode.AddFinalitySig(s.cacheFP.BtcPk, activatedHeight, &randListInfo.PRList[idx], *randListInfo.ProofList[idx].ToProto(), appHash, eotsSig)
})

// ensure vote is eventually cast
var finalizedBlocks []*ftypes.IndexedBlock
Expand All @@ -363,11 +364,6 @@ func (s *BTCStakingTestSuite) Test3CommitPublicRandomnessAndSubmitFinalitySignat
s.Equal(appHash.Bytes(), finalizedBlocks[0].AppHash)
s.T().Logf("the block %d is finalized", activatedHeight)

// ensure the tx fee is refunded and the balance is not changed
submitterBalanceAfter, err := nonValidatorNode.QueryBalances(s.cacheFP.Addr)
s.NoError(err)
s.Equal(submitterBalanceBefore, submitterBalanceAfter)

// ensure finality provider has received rewards after the block is finalised
fpRewardGauges, err := nonValidatorNode.QueryRewardGauge(fpBabylonAddr)
s.NoError(err)
Expand Down Expand Up @@ -473,8 +469,10 @@ func (s *BTCStakingTestSuite) Test5SubmitStakerUnbonding() {
delUnbondingSig, err := activeDel.SignUnbondingTx(params, s.net, s.delBTCSK)
s.NoError(err)

// submit the message for creating BTC undelegation
nonValidatorNode.BTCUndelegate(&stakingTxHash, delUnbondingSig)
nonValidatorNode.SubmitRefundableTxWithAssertion(func() {
// submit the message for creating BTC undelegation
nonValidatorNode.BTCUndelegate(&stakingTxHash, delUnbondingSig)
})
// wait for a block so that above txs take effect
nonValidatorNode.WaitForNextBlock()

Expand Down
37 changes: 23 additions & 14 deletions test/e2e/btc_staking_pre_approval_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,16 @@ func (s *BTCStakingPreApprovalTestSuite) Test2SubmitCovenantSignature() {
s.NoError(err)

for i := 0; i < int(s.covenantQuorum); i++ {
nonValidatorNode.AddCovenantSigs(
covenantSlashingSigs[i].CovPk,
stakingTxHash,
covenantSlashingSigs[i].AdaptorSigs,
bbn.NewBIP340SignatureFromBTCSig(covUnbondingSigs[i]),
covenantUnbondingSlashingSigs[i].AdaptorSigs,
)
nonValidatorNode.SubmitRefundableTxWithAssertion(func() {
nonValidatorNode.AddCovenantSigs(
covenantSlashingSigs[i].CovPk,
stakingTxHash,
covenantSlashingSigs[i].AdaptorSigs,
bbn.NewBIP340SignatureFromBTCSig(covUnbondingSigs[i]),
covenantUnbondingSlashingSigs[i].AdaptorSigs,
)
})

// wait for a block so that above txs take effect
nonValidatorNode.WaitForNextBlock()
}
Expand Down Expand Up @@ -266,10 +269,12 @@ func (s *BTCStakingPreApprovalTestSuite) Test3SendStakingTransctionInclusionProo
s.NoError(err)
stakingTxHash := stakingMsgTx.TxHash()

nonValidatorNode.AddBTCDelegationInclusionProof(
&stakingTxHash,
s.cachedInclusionProof,
)
nonValidatorNode.SubmitRefundableTxWithAssertion(func() {
nonValidatorNode.AddBTCDelegationInclusionProof(
&stakingTxHash,
s.cachedInclusionProof,
)
})

nonValidatorNode.WaitForNextBlock()
nonValidatorNode.WaitForNextBlock()
Expand Down Expand Up @@ -366,7 +371,9 @@ func (s *BTCStakingPreApprovalTestSuite) Test4CommitPublicRandomnessAndSubmitFin
s.NoError(err)
eotsSig := bbn.NewSchnorrEOTSSigFromModNScalar(sig)
// submit finality signature
nonValidatorNode.AddFinalitySig(s.cacheFP.BtcPk, activatedHeight, &randListInfo.PRList[idx], *randListInfo.ProofList[idx].ToProto(), appHash, eotsSig)
nonValidatorNode.SubmitRefundableTxWithAssertion(func() {
nonValidatorNode.AddFinalitySig(s.cacheFP.BtcPk, activatedHeight, &randListInfo.PRList[idx], *randListInfo.ProofList[idx].ToProto(), appHash, eotsSig)
})

// ensure vote is eventually cast
var finalizedBlocks []*ftypes.IndexedBlock
Expand Down Expand Up @@ -483,8 +490,10 @@ func (s *BTCStakingPreApprovalTestSuite) Test5SubmitStakerUnbonding() {
delUnbondingSig, err := activeDel.SignUnbondingTx(params, s.net, s.delBTCSK)
s.NoError(err)

// submit the message for creating BTC undelegation
nonValidatorNode.BTCUndelegate(&stakingTxHash, delUnbondingSig)
nonValidatorNode.SubmitRefundableTxWithAssertion(func() {
// submit the message for creating BTC undelegation
nonValidatorNode.BTCUndelegate(&stakingTxHash, delUnbondingSig)
})
// wait for a block so that above txs take effect
nonValidatorNode.WaitForNextBlock()

Expand Down
23 changes: 22 additions & 1 deletion test/e2e/configurer/chain/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,10 @@ func (n *NodeConfig) FinalizeSealedEpochs(startEpoch uint64, lastEpoch uint64) {

n.InsertHeader(&opReturn1.HeaderBytes)
n.InsertHeader(&opReturn2.HeaderBytes)
n.InsertProofs(opReturn1.SpvProof, opReturn2.SpvProof)

n.SubmitRefundableTxWithAssertion(func() {
n.InsertProofs(opReturn1.SpvProof, opReturn2.SpvProof)
})

n.WaitForCondition(func() bool {
ckpt, err := n.QueryRawCheckpoint(checkpoint.Ckpt.EpochNum)
Expand Down Expand Up @@ -444,3 +447,21 @@ func (n *NodeConfig) TxGovVote(from string, propID int, option govv1.VoteOption,

n.LogActionF("successfully submitted vote %s to prop %d", option, propID)
}

// submitRefundableTxWithAssertion submits a refundable transaction,
// and asserts that the tx fee is refunded
func (n *NodeConfig) SubmitRefundableTxWithAssertion(
f func(),
) {
// balance before submitting the refundable tx
submitterBalanceBefore, err := n.QueryBalances(n.PublicAddress)
require.NoError(n.t, err)

// submit refundable tx
f()

// ensure the tx fee is refunded and the balance is not changed
submitterBalanceAfter, err := n.QueryBalances(n.PublicAddress)
require.NoError(n.t, err)
require.Equal(n.t, submitterBalanceBefore, submitterBalanceAfter)
}
2 changes: 2 additions & 0 deletions testutil/keeper/btcstaking.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func BTCStakingKeeper(
btclcKeeper types.BTCLightClientKeeper,
btccKeeper types.BtcCheckpointKeeper,
finalityKeeper types.FinalityKeeper,
iKeeper types.IncentiveKeeper,
) (*keeper.Keeper, sdk.Context) {
storeKey := storetypes.NewKVStoreKey(types.StoreKey)

Expand All @@ -45,6 +46,7 @@ func BTCStakingKeeper(
btclcKeeper,
btccKeeper,
finalityKeeper,
iKeeper,
&chaincfg.SimNetParams,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
Expand Down
4 changes: 4 additions & 0 deletions x/btccheckpoint/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ func (ms msgServer) InsertBTCSpvProof(ctx context.Context, req *types.MsgInsertB
return nil, err
}

// At this point, the BTC checkpoint is considered the first valid one for the epoch.
// Thus, we can safely consider this message as refundable
ms.k.incentiveKeeper.IndexRefundableMsg(sdkCtx, req)

return &types.MsgInsertBTCSpvProofResponse{}, nil
}

Expand Down
3 changes: 3 additions & 0 deletions x/btccheckpoint/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package types

import (
"context"

txformat "github.com/babylonlabs-io/babylon/btctxformatter"
bbn "github.com/babylonlabs-io/babylon/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

type BTCLightClientKeeper interface {
Expand Down Expand Up @@ -40,4 +42,5 @@ type CheckpointingKeeper interface {

type IncentiveKeeper interface {
RewardBTCTimestamping(ctx context.Context, epoch uint64, rewardDistInfo *RewardDistInfo)
IndexRefundableMsg(ctx context.Context, msg sdk.Msg)
}
4 changes: 4 additions & 0 deletions x/btccheckpoint/types/mock_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

txformat "github.com/babylonlabs-io/babylon/btctxformatter"
bbn "github.com/babylonlabs-io/babylon/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

type MockBTCLightClientKeeper struct {
Expand Down Expand Up @@ -97,3 +98,6 @@ func (ck MockCheckpointingKeeper) SetCheckpointForgotten(ctx context.Context, ep

func (ik *MockIncentiveKeeper) RewardBTCTimestamping(ctx context.Context, epoch uint64, rewardDistInfo *RewardDistInfo) {
}

func (ik *MockIncentiveKeeper) IndexRefundableMsg(ctx context.Context, msg sdk.Msg) {
}
2 changes: 1 addition & 1 deletion x/btcstaking/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestGenesis(t *testing.T) {
Params: []*types.Params{&p},
}

k, ctx := keepertest.BTCStakingKeeper(t, nil, nil, nil)
k, ctx := keepertest.BTCStakingKeeper(t, nil, nil, nil, nil)
btcstaking.InitGenesis(ctx, *k, genesisState)
got := btcstaking.ExportGenesis(ctx, *k)
require.NotNil(t, got)
Expand Down
2 changes: 1 addition & 1 deletion x/btcstaking/keeper/btc_height_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func FuzzBTCHeightIndex(f *testing.F) {

// mock BTC light client
btclcKeeper := types.NewMockBTCLightClientKeeper(ctrl)
keeper, ctx := keepertest.BTCStakingKeeper(t, btclcKeeper, nil, nil)
keeper, ctx := keepertest.BTCStakingKeeper(t, btclcKeeper, nil, nil, nil)

// randomise Babylon height and BTC height
babylonHeight := datagen.RandomInt(r, 100)
Expand Down
16 changes: 8 additions & 8 deletions x/btcstaking/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func FuzzActivatedHeight(f *testing.F) {
r := rand.New(rand.NewSource(seed))

// Setup keeper and context
keeper, ctx := testkeeper.BTCStakingKeeper(t, nil, nil, nil)
keeper, ctx := testkeeper.BTCStakingKeeper(t, nil, nil, nil, nil)
ctx = sdk.UnwrapSDKContext(ctx)

// not activated yet
Expand All @@ -54,7 +54,7 @@ func FuzzFinalityProviders(f *testing.F) {
r := rand.New(rand.NewSource(seed))

// Setup keeper and context
keeper, ctx := testkeeper.BTCStakingKeeper(t, nil, nil, nil)
keeper, ctx := testkeeper.BTCStakingKeeper(t, nil, nil, nil, nil)
ctx = sdk.UnwrapSDKContext(ctx)

// Generate random finality providers and add them to kv store
Expand Down Expand Up @@ -119,7 +119,7 @@ func FuzzFinalityProvider(f *testing.F) {
f.Fuzz(func(t *testing.T, seed int64) {
r := rand.New(rand.NewSource(seed))
// Setup keeper and context
keeper, ctx := testkeeper.BTCStakingKeeper(t, nil, nil, nil)
keeper, ctx := testkeeper.BTCStakingKeeper(t, nil, nil, nil, nil)
ctx = sdk.UnwrapSDKContext(ctx)

// Generate random finality providers and add them to kv store
Expand Down Expand Up @@ -175,7 +175,7 @@ func FuzzPendingBTCDelegations(f *testing.F) {
btclcKeeper := types.NewMockBTCLightClientKeeper(ctrl)
btccKeeper := types.NewMockBtcCheckpointKeeper(ctrl)
btccKeeper.EXPECT().GetParams(gomock.Any()).Return(btcctypes.DefaultParams()).AnyTimes()
keeper, ctx := testkeeper.BTCStakingKeeper(t, btclcKeeper, btccKeeper, nil)
keeper, ctx := testkeeper.BTCStakingKeeper(t, btclcKeeper, btccKeeper, nil, nil)

// covenant and slashing addr
covenantSKs, covenantPKs, covenantQuorum := datagen.GenCovenantCommittee(r)
Expand Down Expand Up @@ -279,7 +279,7 @@ func FuzzFinalityProviderPowerAtHeight(f *testing.F) {
r := rand.New(rand.NewSource(seed))

// Setup keeper and context
keeper, ctx := testkeeper.BTCStakingKeeper(t, nil, nil, nil)
keeper, ctx := testkeeper.BTCStakingKeeper(t, nil, nil, nil, nil)

// random finality provider
fp, err := datagen.GenRandomFinalityProvider(r)
Expand Down Expand Up @@ -328,7 +328,7 @@ func FuzzFinalityProviderCurrentVotingPower(f *testing.F) {
r := rand.New(rand.NewSource(seed))

// Setup keeper and context
keeper, ctx := testkeeper.BTCStakingKeeper(t, nil, nil, nil)
keeper, ctx := testkeeper.BTCStakingKeeper(t, nil, nil, nil, nil)

// random finality provider
fp, err := datagen.GenRandomFinalityProvider(r)
Expand Down Expand Up @@ -380,7 +380,7 @@ func FuzzActiveFinalityProvidersAtHeight(f *testing.F) {
btclcKeeper.EXPECT().GetTipInfo(gomock.Any()).Return(&btclctypes.BTCHeaderInfo{Height: 10}).AnyTimes()
btccKeeper := types.NewMockBtcCheckpointKeeper(ctrl)
btccKeeper.EXPECT().GetParams(gomock.Any()).Return(btcctypes.DefaultParams()).AnyTimes()
keeper, ctx := testkeeper.BTCStakingKeeper(t, btclcKeeper, btccKeeper, nil)
keeper, ctx := testkeeper.BTCStakingKeeper(t, btclcKeeper, btccKeeper, nil, nil)

// covenant and slashing addr
covenantSKs, covenantPKs, covenantQuorum := datagen.GenCovenantCommittee(r)
Expand Down Expand Up @@ -500,7 +500,7 @@ func FuzzFinalityProviderDelegations(f *testing.F) {
btclcKeeper := types.NewMockBTCLightClientKeeper(ctrl)
btccKeeper := types.NewMockBtcCheckpointKeeper(ctrl)
btccKeeper.EXPECT().GetParams(gomock.Any()).Return(btcctypes.DefaultParams()).AnyTimes()
keeper, ctx := testkeeper.BTCStakingKeeper(t, btclcKeeper, btccKeeper, nil)
keeper, ctx := testkeeper.BTCStakingKeeper(t, btclcKeeper, btccKeeper, nil, nil)

// covenant and slashing addr
covenantSKs, covenantPKs, covenantQuorum := datagen.GenCovenantCommittee(r)
Expand Down
3 changes: 3 additions & 0 deletions x/btcstaking/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type (
btclcKeeper types.BTCLightClientKeeper
btccKeeper types.BtcCheckpointKeeper
FinalityKeeper types.FinalityKeeper
iKeeper types.IncentiveKeeper

hooks types.BtcStakingHooks

Expand All @@ -39,6 +40,7 @@ func NewKeeper(
btclcKeeper types.BTCLightClientKeeper,
btccKeeper types.BtcCheckpointKeeper,
finalityKeeper types.FinalityKeeper,
iKeeper types.IncentiveKeeper,

btcNet *chaincfg.Params,
authority string,
Expand All @@ -50,6 +52,7 @@ func NewKeeper(
btclcKeeper: btclcKeeper,
btccKeeper: btccKeeper,
FinalityKeeper: finalityKeeper,
iKeeper: iKeeper,

hooks: nil,

Expand Down
9 changes: 7 additions & 2 deletions x/btcstaking/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,16 @@ func NewHelper(
btccKeeper *types.MockBtcCheckpointKeeper,
finalityKeeper *types.MockFinalityKeeper,
) *Helper {
k, ctx := keepertest.BTCStakingKeeper(t, btclcKeeper, btccKeeper, finalityKeeper)
ctrl := gomock.NewController(t)

// mock refundable messages
iKeeper := types.NewMockIncentiveKeeper(ctrl)
iKeeper.EXPECT().IndexRefundableMsg(gomock.Any(), gomock.Any()).AnyTimes()

k, ctx := keepertest.BTCStakingKeeper(t, btclcKeeper, btccKeeper, finalityKeeper, iKeeper)
ctx = ctx.WithHeaderInfo(header.Info{Height: 1})
msgSrvr := keeper.NewMsgServerImpl(*k)

ctrl := gomock.NewController(t)
mockedHooks := types.NewMockBtcStakingHooks(ctrl)
mockedHooks.EXPECT().AfterFinalityProviderActivated(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
k.SetHooks(mockedHooks)
Expand Down
Loading

0 comments on commit 73735ff

Please sign in to comment.