Skip to content

Commit

Permalink
IBC: bug fixes for creating IBC channels (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianElvis authored Nov 21, 2022
1 parent 07b32d5 commit c0165ab
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 21 deletions.
30 changes: 28 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ import (
"github.com/babylonchain/babylon/x/zoneconcierge"
zckeeper "github.com/babylonchain/babylon/x/zoneconcierge/keeper"
zctypes "github.com/babylonchain/babylon/x/zoneconcierge/types"
transfer "github.com/cosmos/ibc-go/v5/modules/apps/transfer"
ibctransferkeeper "github.com/cosmos/ibc-go/v5/modules/apps/transfer/keeper"
ibctransfertypes "github.com/cosmos/ibc-go/v5/modules/apps/transfer/types"
ibc "github.com/cosmos/ibc-go/v5/modules/core"
ibcclientkeeper "github.com/cosmos/ibc-go/v5/modules/core/02-client/keeper"
porttypes "github.com/cosmos/ibc-go/v5/modules/core/05-port/types"
Expand Down Expand Up @@ -160,6 +163,7 @@ var (

// IBC-related
ibc.AppModuleBasic{},
transfer.AppModuleBasic{},
zoneconcierge.AppModuleBasic{},
)

Expand All @@ -171,6 +175,7 @@ var (
stakingtypes.BondedPoolName: {authtypes.Burner, authtypes.Staking},
stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking},
govtypes.ModuleName: {authtypes.Burner},
ibctransfertypes.ModuleName: {authtypes.Minter, authtypes.Burner},
// TODO: decide ZonConcierge's permissions here
zctypes.ModuleName: {authtypes.Minter, authtypes.Burner},
}
Expand Down Expand Up @@ -220,11 +225,13 @@ type BabylonApp struct {
CheckpointingKeeper checkpointingkeeper.Keeper

// IBC-related modules
IBCKeeper *ibckeeper.Keeper // IBC Keeper must be a pointer in the app, so we can SetRouter on it correctly
ZoneConciergeKeeper zckeeper.Keeper // for cross-chain fungible token transfers
IBCKeeper *ibckeeper.Keeper // IBC Keeper must be a pointer in the app, so we can SetRouter on it correctly
TransferKeeper ibctransferkeeper.Keeper // for cross-chain fungible token transfers
ZoneConciergeKeeper zckeeper.Keeper // for cross-chain fungible token transfers

// make scoped keepers public for test purposes
ScopedIBCKeeper capabilitykeeper.ScopedKeeper
ScopedTransferKeeper capabilitykeeper.ScopedKeeper
ScopedZoneConciergeKeeper capabilitykeeper.ScopedKeeper

// the module manager
Expand Down Expand Up @@ -279,6 +286,7 @@ func NewBabylonApp(
checkpointingtypes.StoreKey,
// IBC-related modules
ibchost.StoreKey,
ibctransfertypes.StoreKey,
zctypes.StoreKey,
)
tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
Expand Down Expand Up @@ -306,6 +314,7 @@ func NewBabylonApp(

// grant capabilities for the ibc and ibc-transfer modules
scopedIBCKeeper := app.CapabilityKeeper.ScopeToModule(ibchost.ModuleName)
scopedTransferKeeper := app.CapabilityKeeper.ScopeToModule(ibctransfertypes.ModuleName)
scopedZoneConciergeKeeper := app.CapabilityKeeper.ScopeToModule(zctypes.ModuleName)

// Applications that wish to enforce statically created ScopedKeepers should call `Seal` after creating
Expand Down Expand Up @@ -422,8 +431,18 @@ func NewBabylonApp(
app.IBCKeeper = ibcKeeper
app.ZoneConciergeKeeper = *zcKeeper

// Create Transfer Keepers
app.TransferKeeper = ibctransferkeeper.NewKeeper(
appCodec, keys[ibctransfertypes.StoreKey], app.GetSubspace(ibctransfertypes.ModuleName),
app.IBCKeeper.ChannelKeeper, app.IBCKeeper.ChannelKeeper, &app.IBCKeeper.PortKeeper,
app.AccountKeeper, app.BankKeeper, scopedTransferKeeper,
)
transferModule := transfer.NewAppModule(app.TransferKeeper)

// Create static IBC router, add ibc-tranfer module route, then set and seal it
ibcRouter := porttypes.NewRouter()
transferIBCModule := transfer.NewIBCModule(app.TransferKeeper)
ibcRouter.AddRoute(ibctransfertypes.ModuleName, transferIBCModule)
zcIBCModule := zoneconcierge.NewIBCModule(app.ZoneConciergeKeeper)
ibcRouter.AddRoute(zctypes.ModuleName, zcIBCModule)
// Setting Router will finalize all routes by sealing router
Expand Down Expand Up @@ -522,6 +541,7 @@ func NewBabylonApp(
checkpointing.NewAppModule(appCodec, app.CheckpointingKeeper, app.AccountKeeper, app.BankKeeper),
// IBC-related modules
ibc.NewAppModule(app.IBCKeeper),
transferModule,
zoneconcierge.NewAppModule(appCodec, app.ZoneConciergeKeeper, app.AccountKeeper, app.BankKeeper),
)

Expand All @@ -543,6 +563,7 @@ func NewBabylonApp(
checkpointingtypes.ModuleName,
// IBC-related modules
ibchost.ModuleName,
ibctransfertypes.ModuleName,
zctypes.ModuleName,
)
// TODO: there will be an architecture design on whether to modify slashing/evidence, specifically
Expand All @@ -564,6 +585,7 @@ func NewBabylonApp(
checkpointingtypes.ModuleName,
// IBC-related modules
ibchost.ModuleName,
ibctransfertypes.ModuleName,
zctypes.ModuleName,
)
// Babylon does not want EndBlock processing in staking
Expand All @@ -587,6 +609,7 @@ func NewBabylonApp(
checkpointingtypes.ModuleName,
// IBC-related modules
ibchost.ModuleName,
ibctransfertypes.ModuleName,
zctypes.ModuleName,
)

Expand Down Expand Up @@ -625,6 +648,7 @@ func NewBabylonApp(
checkpointing.NewAppModule(appCodec, app.CheckpointingKeeper, app.AccountKeeper, app.BankKeeper),
// IBC-related modules
ibc.NewAppModule(app.IBCKeeper),
transferModule,
zoneconcierge.NewAppModule(appCodec, app.ZoneConciergeKeeper, app.AccountKeeper, app.BankKeeper),
)

Expand Down Expand Up @@ -672,6 +696,7 @@ func NewBabylonApp(

app.ScopedIBCKeeper = scopedIBCKeeper
app.ScopedZoneConciergeKeeper = scopedZoneConciergeKeeper
app.ScopedTransferKeeper = scopedTransferKeeper

return app
}
Expand Down Expand Up @@ -855,6 +880,7 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino
paramsKeeper.Subspace(checkpointingtypes.ModuleName)
// IBC-related modules
paramsKeeper.Subspace(ibchost.ModuleName)
paramsKeeper.Subspace(ibctransfertypes.ModuleName)
paramsKeeper.Subspace(zctypes.ModuleName)

return paramsKeeper
Expand Down
3 changes: 0 additions & 3 deletions cmd/babylond/cmd/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,6 @@ func TestnetGenesisParams(maxActiveValidators uint32, btcConfirmationDepth uint6
}
genParams.StakingParams.MaxValidators = maxActiveValidators
genParams.StakingParams.BondDenom = genParams.NativeCoinMetadatas[0].Base
// Babylon should enforce this value to be 0. However Cosmos enforces it to be positive so we use the smallest value 1
// Instead the timing of unbonding is decided by checkpoint states
genParams.StakingParams.UnbondingTime = 1

genParams.MintParams = minttypes.DefaultParams()
genParams.MintParams.MintDenom = genParams.NativeCoinMetadatas[0].Base
Expand Down
15 changes: 13 additions & 2 deletions cmd/babylond/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,19 +332,30 @@ func InitTestnet(
if err != nil {
return err
}
addr, _, err := testutil.GenerateSaveCoinKey(kb, "test-spending-key", "", true, algo)
addr, secret, err := testutil.GenerateSaveCoinKey(kb, "test-spending-key", "", true, algo)
if err != nil {
_ = os.RemoveAll(outputDir)
return err
}

// save mnemonic words for this key
info := map[string]string{"secret": secret}
cliPrint, err := json.Marshal(info)
if err != nil {
return err
}
if err = writeFile(fmt.Sprintf("%v.json", "additional_key_seed"), nodeDir, cliPrint); err != nil {
return err
}

coins := sdk.Coins{
sdk.NewCoin("testtoken", sdk.NewInt(1000000000)),
sdk.NewCoin(genesisParams.NativeCoinMetadatas[0].Base, sdk.NewInt(500000000)),
sdk.NewCoin(genesisParams.NativeCoinMetadatas[0].Base, sdk.NewInt(1000000000000)),
}

genBalances = append(genBalances, banktypes.Balance{Address: addr.String(), Coins: coins.Sort()})
genAccounts = append(genAccounts, authtypes.NewBaseAccount(addr, nil, 0, 0))

}
}

Expand Down
18 changes: 12 additions & 6 deletions x/zoneconcierge/keeper/ibc_heartbeat.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package keeper

import (
"fmt"
"time"

sdkerrors "cosmossdk.io/errors"
metrics "github.com/armon/go-metrics"
"github.com/babylonchain/babylon/x/zoneconcierge/types"
Expand Down Expand Up @@ -35,12 +38,12 @@ func (k Keeper) SendHeartbeatIBCPacket(ctx sdk.Context, channel channeltypes.Ide
// See spec for this logic: https://github.com/cosmos/ibc/tree/master/spec/app/ics-020-fungible-token-transfer#packet-relay
channelCap, ok := k.scopedKeeper.GetCapability(ctx, host.ChannelCapabilityPath(sourcePort, sourceChannel))
if !ok {
return sdkerrors.Wrap(channeltypes.ErrChannelCapabilityNotFound, "module does not own channel capability")
return sdkerrors.Wrapf(channeltypes.ErrChannelCapabilityNotFound, "module does not own channel capability: sourcePort: %s, sourceChannel: %s", sourcePort, sourceChannel)
}

// timeout
curHeight := clienttypes.GetSelfHeight(ctx)
curHeight.RevisionHeight += 100 // TODO: parameterise timeout
timeoutTime := uint64(ctx.BlockHeader().Time.Add(time.Hour * 24).UnixNano()) // TODO: parameterise
zeroheight := clienttypes.ZeroHeight()

// construct packet
// note that the data is not allowed to be empty
Expand All @@ -52,13 +55,16 @@ func (k Keeper) SendHeartbeatIBCPacket(ctx sdk.Context, channel channeltypes.Ide
sourceChannel,
destinationPort,
destinationChannel,
curHeight, // if the packet is not relayed after thit height, then the packet will be timeout
uint64(0), // no need to set timeout timestamp if timeout height is set
zeroheight, // no need to set timeout height if timeout timestamp is set
timeoutTime, // if the packet is not relayed after this time, then the packet will be time out
)

// send packet
if err := k.ics4Wrapper.SendPacket(ctx, channelCap, packet); err != nil {
return err
// Failed/timeout packet should not make the system crash
k.Logger(ctx).Error(fmt.Sprintf("failed to send heartbeat IBC packet to channel %v port %s: %v", destinationChannel, destinationPort, err))
} else {
k.Logger(ctx).Info(fmt.Sprintf("successfully sent heartbeat IBC packet to channel %v port %s", destinationChannel, destinationPort))
}

// metrics stuff
Expand Down
12 changes: 4 additions & 8 deletions x/zoneconcierge/module_ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ func (im IBCModule) OnChanOpenAck(
_,
counterpartyVersion string,
) error {
if counterpartyVersion != types.Version {
return sdkerrors.Wrapf(types.ErrInvalidVersion, "invalid counterparty version: %s, expected %s", counterpartyVersion, types.Version)
}
// // TODO (Babylon): check version consistency (this requires modifying CZ code)
// if counterpartyVersion != types.Version {
// return sdkerrors.Wrapf(types.ErrInvalidVersion, "invalid counterparty version: %s, expected %s", counterpartyVersion, types.Version)
// }
return nil
}

Expand Down Expand Up @@ -150,11 +151,6 @@ func (im IBCModule) OnAcknowledgementPacket(

// this line is used by starport scaffolding # oracle/packet/module/ack

var modulePacketData types.ZoneconciergePacketData
if err := modulePacketData.Unmarshal(modulePacket.GetData()); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "cannot unmarshal packet data: %s", err.Error())
}

var eventType string

// // TODO (Babylon): Dispatch and process packet
Expand Down

0 comments on commit c0165ab

Please sign in to comment.