Skip to content

Commit

Permalink
init tunnel ibc hook
Browse files Browse the repository at this point in the history
  • Loading branch information
satawatnack committed Nov 25, 2024
1 parent 74212e6 commit 84fb20e
Show file tree
Hide file tree
Showing 19 changed files with 1,960 additions and 93 deletions.
1,060 changes: 1,035 additions & 25 deletions api/band/tunnel/v1beta1/route.pulsar.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ func NewAppKeeper(
appKeepers.IBCFeeKeeper,
appKeepers.IBCKeeper.PortKeeper,
appKeepers.ScopedTunnelKeeper,
appKeepers.TransferKeeper,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

Expand Down
18 changes: 18 additions & 0 deletions proto/band/tunnel/v1beta1/route.proto
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,21 @@ message IBCPacket {
// created_at is the timestamp when the packet is created
int64 created_at = 4;
}

// IBCHookRoute is the type for an IBC hook route
message IBCHookRoute {
option (cosmos_proto.implements_interface) = "Route";

// channel_id is the IBC channel ID
string channel_id = 1 [(gogoproto.customname) = "ChannelID"];
// destination_contract_address is the destination contract address
string destination_contract_address = 2;
}

// IBCHookPacketReceipt represents a receipt for a IBC hook packet and implements the PacketReceiptI interface.
message IBCHookPacketReceipt {
option (cosmos_proto.implements_interface) = "PacketContentI";

// sequence is representing the sequence of the IBC packet.
uint64 sequence = 1;
}
1 change: 1 addition & 0 deletions scripts/tunnel/create_ibc_hook_tunnel.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bandd tx tunnel create-tunnel ibc-hook channel-0 wasm1vjq0k3fj47s8wns4a7zw5c4lsjd8l6r2kzzlpk 1 1uband 10 ./scripts/tunnel/signal_deviations.json --from requester --keyring-backend test --gas-prices 0.0025uband -y --chain-id bandchain
57 changes: 56 additions & 1 deletion x/tunnel/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func GetTxCmdCreateTunnel() *cobra.Command {
}

// add create tunnel subcommands
txCmd.AddCommand(GetTxCmdCreateTSSTunnel(), GetTxCmdCreateIBCTunnel())
txCmd.AddCommand(GetTxCmdCreateTSSTunnel(), GetTxCmdCreateIBCTunnel(), GetTxCmdCreateIBCHookTunnel())

return txCmd
}
Expand Down Expand Up @@ -155,6 +155,61 @@ func GetTxCmdCreateIBCTunnel() *cobra.Command {
return cmd
}

func GetTxCmdCreateIBCHookTunnel() *cobra.Command {
cmd := &cobra.Command{
Use: "ibc-hook [channel-id] [destination-contract-address] [encoder] [initial-deposit] [interval] [signalInfos-json-file]",
Short: "Create a new IBC hook tunnel",
Args: cobra.ExactArgs(6),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

channelID := args[0]
destinationContractAddress := args[1]

encoder, err := strconv.ParseInt(args[2], 10, 32)
if err != nil {
return err
}

initialDeposit, err := sdk.ParseCoinsNormalized(args[3])
if err != nil {
return err
}

interval, err := strconv.ParseUint(args[4], 10, 64)
if err != nil {
return err
}

signalInfos, err := parseSignalDeviations(args[5])
if err != nil {
return err
}

msg, err := types.NewMsgCreateIBCHookTunnel(
signalInfos.ToSignalDeviations(),
interval,
channelID,
destinationContractAddress,
feedstypes.Encoder(encoder),
initialDeposit,
clientCtx.GetFromAddress(),
)
if err != nil {
return err
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)
return cmd
}

func GetTxCmdUpdateAndResetTunnel() *cobra.Command {
cmd := &cobra.Command{
Use: "update-and-reset-tunnel [tunnel-id] [interval] [signalDeviations-json-file] ",
Expand Down
37 changes: 20 additions & 17 deletions x/tunnel/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ type Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey

authKeeper types.AccountKeeper
bankKeeper types.BankKeeper
feedsKeeper types.FeedsKeeper
bandtssKeeper types.BandtssKeeper
ics4Wrapper types.ICS4Wrapper
portKeeper types.PortKeeper
scopedKeeper types.ScopedKeeper
authKeeper types.AccountKeeper
bankKeeper types.BankKeeper
feedsKeeper types.FeedsKeeper
bandtssKeeper types.BandtssKeeper
ics4Wrapper types.ICS4Wrapper
portKeeper types.PortKeeper
scopedKeeper types.ScopedKeeper
transferKeeper types.TransferKeeper

authority string
}
Expand All @@ -41,6 +42,7 @@ func NewKeeper(
ics4Wrapper types.ICS4Wrapper,
portKeeper types.PortKeeper,
scopedKeeper types.ScopedKeeper,
transferKeeper types.TransferKeeper,
authority string,
) Keeper {
// ensure tunnel module account is set
Expand All @@ -54,16 +56,17 @@ func NewKeeper(
}

return Keeper{
cdc: cdc,
storeKey: key,
authKeeper: authKeeper,
bankKeeper: bankKeeper,
feedsKeeper: feedsKeeper,
bandtssKeeper: bandtssKeeper,
ics4Wrapper: ics4Wrapper,
portKeeper: portKeeper,
scopedKeeper: scopedKeeper,
authority: authority,
cdc: cdc,
storeKey: key,
authKeeper: authKeeper,
bankKeeper: bankKeeper,
feedsKeeper: feedsKeeper,
bandtssKeeper: bandtssKeeper,
ics4Wrapper: ics4Wrapper,
portKeeper: portKeeper,
scopedKeeper: scopedKeeper,
transferKeeper: transferKeeper,
authority: authority,
}
}

Expand Down
2 changes: 2 additions & 0 deletions x/tunnel/keeper/keeper_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ func (k Keeper) SendPacket(ctx sdk.Context, packet types.Packet) error {
receipt, err = k.SendTSSPacket(ctx, r, packet)
case *types.IBCRoute:
receipt, err = k.SendIBCPacket(ctx, r, packet)
case *types.IBCHookRoute:
receipt, err = k.SendIBCHookPacket(ctx, r, packet, sdk.MustAccAddressFromBech32(tunnel.FeePayer))
default:
return types.ErrInvalidRoute.Wrapf("no route found for tunnel ID: %d", tunnel.ID)
}
Expand Down
51 changes: 51 additions & 0 deletions x/tunnel/keeper/keeper_paclet_ibc_hook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package keeper

import (
ibctransfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"

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

"github.com/bandprotocol/chain/v3/x/tunnel/types"
)

// SendIBCHookPacket sends a packet to the destination chain using IBC Hook
func (k Keeper) SendIBCHookPacket(
ctx sdk.Context,
route *types.IBCHookRoute,
packet types.Packet,
feePayer sdk.AccAddress,
) (types.PacketReceiptI, error) {
// create memo string for ibc transfer
memoStr, err := types.NewIBCHookMemo(
route.DestinationContractAddress,
packet.TunnelID,
packet.Sequence,
packet.Prices,
packet.CreatedAt,
).String()
if err != nil {
return nil, err
}

// create ibc transfer message
msg := ibctransfertypes.NewMsgTransfer(
ibctransfertypes.PortID,
route.ChannelID,
// TODO: align the token to send with msg transfer
sdk.NewInt64Coin("uband", 1),
feePayer.String(),
route.DestinationContractAddress,
clienttypes.ZeroHeight(),
uint64(ctx.BlockTime().UnixNano()+packetExpireTime),
memoStr,
)

// send packet
res, err := k.transferKeeper.Transfer(ctx, msg)
if err != nil {
return nil, err
}

return types.NewIBCHookPacketReceipt(res.Sequence), nil
}
50 changes: 50 additions & 0 deletions x/tunnel/keeper/keeper_paclet_ibc_hook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package keeper_test

import (
"time"

"go.uber.org/mock/gomock"

ibctransfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"

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

feedstypes "github.com/bandprotocol/chain/v3/x/feeds/types"
"github.com/bandprotocol/chain/v3/x/tunnel/types"
)

func (s *KeeperTestSuite) TestSendRouterPacket() {
ctx, k := s.ctx, s.keeper

route := &types.IBCHookRoute{
ChannelID: "channel-0",
DestinationContractAddress: "wasm1vjq0k3fj47s8wns4a7zw5c4lsjd8l6r2kzzlpk",
}

packet := types.Packet{
TunnelID: 1,
Sequence: 1,
Prices: []feedstypes.Price{},
CreatedAt: time.Now().Unix(),
}

expectedPacketReceipt := types.IBCHookPacketReceipt{
Sequence: 1,
}

s.transferKeeper.EXPECT().Transfer(ctx, gomock.Any()).Return(&ibctransfertypes.MsgTransferResponse{
Sequence: 1,
}, nil)

content, err := k.SendIBCHookPacket(
ctx,
route,
packet,
sdk.AccAddress("feePayer"),
)
s.Require().NoError(err)

receipt, ok := content.(*types.IBCHookPacketReceipt)
s.Require().True(ok)
s.Require().Equal(expectedPacketReceipt, *receipt)
}
18 changes: 11 additions & 7 deletions x/tunnel/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ type KeeperTestSuite struct {
queryServer types.QueryServer
msgServer types.MsgServer

accountKeeper *testutil.MockAccountKeeper
bankKeeper *testutil.MockBankKeeper
feedsKeeper *testutil.MockFeedsKeeper
bandtssKeeper *testutil.MockBandtssKeeper
icsWrapper *testutil.MockICS4Wrapper
portKeeper *testutil.MockPortKeeper
scopedKeeper *testutil.MockScopedKeeper
accountKeeper *testutil.MockAccountKeeper
bankKeeper *testutil.MockBankKeeper
feedsKeeper *testutil.MockFeedsKeeper
bandtssKeeper *testutil.MockBandtssKeeper
icsWrapper *testutil.MockICS4Wrapper
portKeeper *testutil.MockPortKeeper
scopedKeeper *testutil.MockScopedKeeper
transferKeeper *testutil.MockTransferKeeper

ctx sdk.Context
authority sdk.AccAddress
Expand All @@ -66,6 +67,7 @@ func (s *KeeperTestSuite) reset() {
icsWrapper := testutil.NewMockICS4Wrapper(ctrl)
portKeeper := testutil.NewMockPortKeeper(ctrl)
scopedKeeper := testutil.NewMockScopedKeeper(ctrl)
transferKeeper := testutil.NewMockTransferKeeper(ctrl)

authority := authtypes.NewModuleAddress(govtypes.ModuleName)

Expand All @@ -81,6 +83,7 @@ func (s *KeeperTestSuite) reset() {
icsWrapper,
portKeeper,
scopedKeeper,
transferKeeper,
authority.String(),
)
s.queryServer = keeper.NewQueryServer(s.keeper)
Expand All @@ -92,6 +95,7 @@ func (s *KeeperTestSuite) reset() {
s.icsWrapper = icsWrapper
s.portKeeper = portKeeper
s.scopedKeeper = scopedKeeper
s.transferKeeper = transferKeeper

s.ctx = testCtx.Ctx.WithBlockHeader(tmproto.Header{Time: time.Now().UTC()})
s.authority = authority
Expand Down
Loading

0 comments on commit 84fb20e

Please sign in to comment.