Skip to content

Commit

Permalink
Merge pull request #936 from scrtlabs/v1-ibc
Browse files Browse the repository at this point in the history
Cosmwasm V1 IBC
  • Loading branch information
liorbond authored Jun 1, 2022
2 parents 6d65102 + 2e89e05 commit c48a3b7
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 16 deletions.
1 change: 1 addition & 0 deletions proto/secret/compute/v1beta1/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ message ContractInfo {
//
// AbsoluteTxPosition last_updated = 7;
// uint64 previous_code_id = 8 [(gogoproto.customname) = "PreviousCodeID"];
string ibc_port_id = 6 [ (gogoproto.customname) = "IBCPortID" ];
}

/*
Expand Down
39 changes: 39 additions & 0 deletions x/compute/internal/keeper/ibc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
host "github.com/cosmos/ibc-go/v3/modules/core/24-host"
)

// bindIbcPort will reserve the port.
// returns a string name of the port or error if we cannot bind it.
// this will fail if call twice.
func (k Keeper) bindIbcPort(ctx sdk.Context, portID string) error {
cap := k.portKeeper.BindPort(ctx, portID)
return k.ClaimCapability(ctx, cap, host.PortPath(portID))
}

// ensureIbcPort is like registerIbcPort, but it checks if we already hold the port
// before calling register, so this is safe to call multiple times.
// Returns success if we already registered or just registered and error if we cannot
// (lack of permissions or someone else has it)
func (k Keeper) ensureIbcPort(ctx sdk.Context, contractAddr sdk.AccAddress) (string, error) {
portID := PortIDForContract(contractAddr)
if _, ok := k.capabilityKeeper.GetCapability(ctx, host.PortPath(portID)); ok {
return portID, nil
}
return portID, k.bindIbcPort(ctx, portID)
}

const portIDPrefix = "wasm."

func PortIDForContract(addr sdk.AccAddress) string {
return portIDPrefix + addr.String()
}

// ClaimCapability allows the transfer module to claim a capability
// that IBC module passes to it
func (k Keeper) ClaimCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) error {
return k.capabilityKeeper.ClaimCapability(ctx, cap, name)
}
27 changes: 11 additions & 16 deletions x/compute/internal/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,16 @@ import (

// Keeper will have a reference to Wasmer with it's own data directory.
type Keeper struct {
storeKey sdk.StoreKey
cdc codec.BinaryCodec
legacyAmino codec.LegacyAmino
accountKeeper authkeeper.AccountKeeper
bankKeeper bankkeeper.Keeper

wasmer wasm.Wasmer
queryPlugins QueryPlugins
messenger MessageHandler
storeKey sdk.StoreKey
cdc codec.BinaryCodec
legacyAmino codec.LegacyAmino
accountKeeper authkeeper.AccountKeeper
bankKeeper bankkeeper.Keeper
portKeeper types.PortKeeper
capabilityKeeper types.CapabilityKeeper
wasmer wasm.Wasmer
queryPlugins QueryPlugins
messenger MessageHandler
// queryGasLimit is the max wasm gas that can be spent on executing a query with a contract
queryGasLimit uint64
serviceRouter MsgServiceRouter
Expand All @@ -73,7 +74,7 @@ func NewKeeper(
distKeeper distrkeeper.Keeper,
mintKeeper mintkeeper.Keeper,
stakingKeeper stakingkeeper.Keeper,
//serviceRouter MsgServiceRouter,
//serviceRouter MsgServiceRouter,
router sdk.Router,
homeDir string,
wasmConfig *types.WasmConfig,
Expand Down Expand Up @@ -398,12 +399,6 @@ func (k Keeper) Instantiate(ctx sdk.Context, codeID uint64, creator sdk.AccAddre
contractInfo.IBCPortID = ibcPort
}

// store contract before dispatch so that contract could be called back
historyEntry := contractInfo.InitialHistory(initMsg)
k.addToContractCodeSecondaryIndex(ctx, contractAddress, historyEntry)
k.appendToContractHistory(ctx, contractAddress, historyEntry)
k.storeContractInfo(ctx, contractAddress, &contractInfo)

ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeInstantiate,
sdk.NewAttribute(types.AttributeKeyContractAddr, contractAddress.String()),
Expand Down
17 changes: 17 additions & 0 deletions x/compute/internal/types/expected_keepers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package types

import (
sdk "github.com/cosmos/cosmos-sdk/types"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
)

// PortKeeper defines the expected IBC port keeper
type PortKeeper interface {
BindPort(ctx sdk.Context, portID string) *capabilitytypes.Capability
}

type CapabilityKeeper interface {
GetCapability(ctx sdk.Context, name string) (*capabilitytypes.Capability, bool)
ClaimCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) error
AuthenticateCapability(ctx sdk.Context, capability *capabilitytypes.Capability, name string) bool
}
5 changes: 5 additions & 0 deletions x/tokenswap/legacy/v106/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package v106

const (
ModuleName = "tokenswap"
)

0 comments on commit c48a3b7

Please sign in to comment.