Skip to content

Commit

Permalink
refund btc header submission
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianElvis committed Oct 3, 2024
1 parent c538635 commit 98ffde5
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 12 deletions.
1 change: 1 addition & 0 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ func (ak *AppKeepers) InitKeepers(
appCodec,
runtime.NewKVStoreService(keys[btclightclienttypes.StoreKey]),
*btcConfig,
&ak.IncentiveKeeper,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

Expand Down
7 changes: 4 additions & 3 deletions test/e2e/configurer/chain/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,10 @@ func (n *NodeConfig) FinalizeSealedEpochs(startEpoch uint64, lastEpoch uint64) {
tx2 := datagen.CreatOpReturnTransaction(r, p2)
opReturn2 := datagen.CreateBlockWithTransaction(r, opReturn1.HeaderBytes.ToBlockHeader(), tx2)

n.InsertHeader(&opReturn1.HeaderBytes)
n.InsertHeader(&opReturn2.HeaderBytes)

n.SubmitRefundableTxWithAssertion(func() {
n.InsertHeader(&opReturn1.HeaderBytes)
n.InsertHeader(&opReturn2.HeaderBytes)
})
n.SubmitRefundableTxWithAssertion(func() {
n.InsertProofs(opReturn1.SpvProof, opReturn2.SpvProof)
})
Expand Down
12 changes: 10 additions & 2 deletions testutil/keeper/btclightclient.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
"context"
"testing"

"cosmossdk.io/core/header"
Expand All @@ -26,6 +27,10 @@ import (
btclightclientt "github.com/babylonlabs-io/babylon/x/btclightclient/types"
)

type MockIncentiveKeeper struct{}

func (mik MockIncentiveKeeper) IndexRefundableMsg(ctx context.Context, msg sdk.Msg) {}

func BTCLightClientKeeper(t testing.TB) (*btclightclientk.Keeper, sdk.Context) {
k, ctx, _ := BTCLightClientKeeperWithCustomParams(t, btclightclientt.DefaultParams())
return k, ctx
Expand All @@ -40,7 +45,10 @@ func NewBTCHeaderBytesList(chain []*wire.BlockHeader) []bbn.BTCHeaderBytes {
return chainBytes
}

func BTCLightClientKeeperWithCustomParams(t testing.TB, p btclightclientt.Params) (*btclightclientk.Keeper, sdk.Context, corestore.KVStoreService) {
func BTCLightClientKeeperWithCustomParams(
t testing.TB,
p btclightclientt.Params,
) (*btclightclientk.Keeper, sdk.Context, corestore.KVStoreService) {
storeKey := storetypes.NewKVStoreKey(btclightclientt.StoreKey)

db := dbm.NewMemDB()
Expand All @@ -52,12 +60,12 @@ func BTCLightClientKeeperWithCustomParams(t testing.TB, p btclightclientt.Params
cdc := codec.NewProtoCodec(registry)

testCfg := bbn.ParseBtcOptionsFromConfig(bapp.TmpAppOptions())

stServ := runtime.NewKVStoreService(storeKey)
k := btclightclientk.NewKeeper(
cdc,
stServ,
testCfg,
&MockIncentiveKeeper{},
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

Expand Down
5 changes: 3 additions & 2 deletions x/btccheckpoint/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func NewMsgServerImpl(keeper Keeper) types.MsgServer {
}

// TODO at some point add proper logging of error
// TODO emit some events for external consumers. Those should be probably emited
// TODO emit some events for external consumers. Those should be probably emitted
// at EndBlockerCallback
func (ms msgServer) InsertBTCSpvProof(ctx context.Context, req *types.MsgInsertBTCSpvProof) (*types.MsgInsertBTCSpvProofResponse, error) {

Expand Down Expand Up @@ -92,7 +92,8 @@ 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.
// At this point, the BTC checkpoint is considered the first valid submission
// for the epoch.
// Thus, we can safely consider this message as refundable
ms.k.incentiveKeeper.IndexRefundableMsg(sdkCtx, req)

Expand Down
3 changes: 3 additions & 0 deletions x/btclightclient/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type (
cdc codec.BinaryCodec
storeService corestoretypes.KVStoreService
hooks types.BTCLightClientHooks
iKeeper types.IncentiveKeeper
btcConfig bbn.BtcConfig
bl *types.BtcLightClient
authority string
Expand All @@ -34,6 +35,7 @@ func NewKeeper(
cdc codec.BinaryCodec,
storeService corestoretypes.KVStoreService,
btcConfig bbn.BtcConfig,
iKeeper types.IncentiveKeeper,
authority string,
) Keeper {
bl := types.NewBtcLightClientFromParams(btcConfig.NetParams())
Expand All @@ -42,6 +44,7 @@ func NewKeeper(
cdc: cdc,
storeService: storeService,
hooks: nil,
iKeeper: iKeeper,
btcConfig: btcConfig,
bl: bl,
authority: authority,
Expand Down
8 changes: 7 additions & 1 deletion x/btclightclient/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,16 @@ func (m msgServer) InsertHeaders(ctx context.Context, msg *types.MsgInsertHeader
}

err := m.k.InsertHeadersWithHookAndEvents(sdkCtx, msg.Headers)

if err != nil {
return nil, err
}

// At this point, the headers have been inserted, and the inserted
// headers extend the current chain or a fork that is longer than
// the current chain.
// Thus, we can safely consider this message as refundable
m.k.iKeeper.IndexRefundableMsg(sdkCtx, msg)

return &types.MsgInsertHeadersResponse{}, nil
}

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

import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
)

type BTCLightClientHooks interface {
AfterBTCRollBack(ctx context.Context, headerInfo *BTCHeaderInfo) // Must be called after the chain is rolled back
AfterBTCRollForward(ctx context.Context, headerInfo *BTCHeaderInfo) // Must be called after the chain is rolled forward
AfterBTCHeaderInserted(ctx context.Context, headerInfo *BTCHeaderInfo) // Must be called after a header is inserted
}

type IncentiveKeeper interface {
IndexRefundableMsg(ctx context.Context, msg sdk.Msg)
}
6 changes: 4 additions & 2 deletions x/btcstaking/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,10 @@ func (ms msgServer) AddCovenantSigs(goCtx context.Context, req *types.MsgAddCove

// at this point, the covenant signatures are verified and are not duplicated.
// Thus, we can safely consider this message as refundable
// TODO: currently we refund tx fee for covenant signatures even if the BTC
// delegation already has a covenant quorum. Should we refund in this case?
// NOTE: currently we refund tx fee for covenant signatures even if the BTC
// delegation already has a covenant quorum. This is to ensure that covenant
// members do not spend transaction fee, even if they submit covenant signatures
// late.
ms.iKeeper.IndexRefundableMsg(ctx, req)

return &types.MsgAddCovenantSigsResponse{}, nil
Expand Down
4 changes: 2 additions & 2 deletions x/finality/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (ms msgServer) AddFinalitySig(goCtx context.Context, req *types.MsgAddFinal
existingSig, err := ms.GetSig(ctx, req.BlockHeight, fpPK)
if err == nil && existingSig.Equals(req.FinalitySig) {
ms.Logger(ctx).Debug("Received duplicated finiality vote", "block height", req.BlockHeight, "finality provider", req.FpBtcPk)
// exactly same vote alreay exists, return success to the provider
// exactly same vote already exists, return success to the provider
return &types.MsgAddFinalitySigResponse{}, nil
}

Expand Down Expand Up @@ -178,7 +178,7 @@ func (ms msgServer) AddFinalitySig(goCtx context.Context, req *types.MsgAddFinal
}

// at this point, the finality signature is 1) valid, 2) over a canonical block,
// and 3) not duplicated
// and 3) not duplicated.
// Thus, we can safely consider this message as refundable
ms.IncentiveKeeper.IndexRefundableMsg(ctx, req)

Expand Down

0 comments on commit 98ffde5

Please sign in to comment.