Skip to content

Commit

Permalink
add ophost module
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Nov 10, 2023
1 parent 78f050d commit 2cb72c6
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 6 deletions.
22 changes: 22 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ import (
// this line is used by starport scaffolding # stargate/app/moduleImport

"github.com/initia-labs/initia/app/ante"
"github.com/initia-labs/initia/app/hook"
authzmodule "github.com/initia-labs/initia/x/authz/module"
"github.com/initia-labs/initia/x/bank"
bankkeeper "github.com/initia-labs/initia/x/bank/keeper"
Expand Down Expand Up @@ -156,6 +157,10 @@ import (
auctionkeeper "github.com/skip-mev/block-sdk/x/auction/keeper"
auctiontypes "github.com/skip-mev/block-sdk/x/auction/types"

"github.com/initia-labs/OPinit/x/ophost"
ophostkeeper "github.com/initia-labs/OPinit/x/ophost/keeper"
ophosttypes "github.com/initia-labs/OPinit/x/ophost/types"

// unnamed import of statik for swagger UI support
_ "github.com/initia-labs/initia/client/docs/statik"
)
Expand Down Expand Up @@ -211,6 +216,7 @@ var (
ibcperm.AppModuleBasic{},
move.AppModuleBasic{},
auction.AppModuleBasic{},
ophost.AppModuleBasic{},
)

// module account permissions
Expand Down Expand Up @@ -293,6 +299,7 @@ type InitiaApp struct {
IBCPermKeeper *ibcpermkeeper.Keeper
MoveKeeper *movekeeper.Keeper
AuctionKeeper *auctionkeeper.Keeper // x/builder keeper used to process bids for TOB auctions
OPHostKeeper *ophostkeeper.Keeper

// make scoped keepers public for test purposes
ScopedIBCKeeper capabilitykeeper.ScopedKeeper
Expand Down Expand Up @@ -344,6 +351,7 @@ func NewInitiaApp(
authzkeeper.StoreKey, feegrant.StoreKey, icahosttypes.StoreKey,
icacontrollertypes.StoreKey, icaauthtypes.StoreKey, ibcfeetypes.StoreKey,
routertypes.StoreKey, ibcpermtypes.StoreKey, movetypes.StoreKey, auctiontypes.StoreKey,
ophosttypes.StoreKey,
)
tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
memKeys := sdk.NewMemoryStoreKeys(capabilitytypes.MemStoreKey)
Expand Down Expand Up @@ -759,6 +767,16 @@ func NewInitiaApp(
)
app.AuctionKeeper = &auctionKeeper

opHostKeeper := ophostkeeper.NewKeeper(
app.appCodec,
app.keys[ophosttypes.StoreKey],
app.AccountKeeper,
app.BankKeeper,
ophosttypes.NewBridgeHooks(hook.NewBridgeHook(app.IBCKeeper.ChannelKeeper, app.IBCPermKeeper)),
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
app.OPHostKeeper = &opHostKeeper

// Register the proposal types
// Deprecated: Avoid adding new handlers, instead use the new proposal flow
// by granting the governance module the right to execute the message.
Expand Down Expand Up @@ -819,6 +837,7 @@ func NewInitiaApp(
ibcfee.NewAppModule(*app.IBCFeeKeeper),
router.NewAppModule(app.RouterKeeper),
ibcperm.NewAppModule(*app.IBCPermKeeper),
ophost.NewAppModule(*app.OPHostKeeper),
)

// During begin block slashing happens after distr.BeginBlocker so that
Expand Down Expand Up @@ -854,6 +873,7 @@ func NewInitiaApp(
ibcpermtypes.ModuleName,
consensusparamtypes.ModuleName,
auctiontypes.ModuleName,
ophosttypes.ModuleName,
)

app.mm.SetOrderEndBlockers(
Expand Down Expand Up @@ -885,6 +905,7 @@ func NewInitiaApp(
ibcpermtypes.ModuleName,
consensusparamtypes.ModuleName,
auctiontypes.ModuleName,
ophosttypes.ModuleName,
)

// NOTE: The genutils module must occur after staking so that pools are
Expand Down Expand Up @@ -922,6 +943,7 @@ func NewInitiaApp(
ibcpermtypes.ModuleName,
consensusparamtypes.ModuleName,
auctiontypes.ModuleName,
ophosttypes.ModuleName,
)

app.mm.RegisterInvariants(app.CrisisKeeper)
Expand Down
81 changes: 81 additions & 0 deletions app/hook/bridge_hook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package hook

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

ibctransfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"

ophosttypes "github.com/initia-labs/OPinit/x/ophost/types"
)

var _ ophosttypes.BridgeHook = BridgeHook{}

type BridgeHook struct {
IBCChannelKeeper ChannelKeeper
IBCPermKeeper PermKeeper
}

type ChannelKeeper interface {
GetNextSequenceSend(ctx sdk.Context, portID, channelID string) (uint64, bool)
}

type PermKeeper interface {
SetChannelRelayer(ctx sdk.Context, channel string, relayer sdk.AccAddress)
}

func NewBridgeHook(channelKeeper ChannelKeeper, permKeeper PermKeeper) BridgeHook {
return BridgeHook{channelKeeper, permKeeper}
}

func (h BridgeHook) BridgeCreated(
ctx sdk.Context,
bridgeId uint64,
bridgeConfig ophosttypes.BridgeConfig,
) error {
channelID := string(bridgeConfig.Metadata)
if channeltypes.IsValidChannelID(channelID) {
if seq, ok := h.IBCChannelKeeper.GetNextSequenceSend(ctx, ibctransfertypes.PortID, channelID); !ok {
return channeltypes.ErrChannelNotFound.Wrap("failed to register permissioned relayer")
} else if seq != 1 {
return channeltypes.ErrChannelExists.Wrap("cannot register permissioned relayer for the channel in use")
}

challenger, err := sdk.AccAddressFromBech32(bridgeConfig.Challenger)
if err != nil {
return err
}

// register challenger as channel relayer
h.IBCPermKeeper.SetChannelRelayer(ctx, channelID, challenger)
}

return nil
}

func (h BridgeHook) BridgeChallengerUpdated(
ctx sdk.Context,
bridgeId uint64,
bridgeConfig ophosttypes.BridgeConfig,
) error {
channelID := string(bridgeConfig.Metadata)
if channeltypes.IsValidChannelID(channelID) {
challenger, err := sdk.AccAddressFromBech32(bridgeConfig.Challenger)
if err != nil {
return err
}

// update relayer to a new challenger
h.IBCPermKeeper.SetChannelRelayer(ctx, channelID, challenger)
}

return nil
}

func (h BridgeHook) BridgeProposerUpdated(
ctx sdk.Context,
bridgeId uint64,
bridgeConfig ophosttypes.BridgeConfig,
) error {
return nil
}
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
cosmossdk.io/api v0.3.1
cosmossdk.io/errors v1.0.0
cosmossdk.io/math v1.1.2
cosmossdk.io/simapp v0.0.0-20230323161446-0af178d721ff
cosmossdk.io/simapp v0.0.0-20230831152633-2e9e5d6eea24
cosmossdk.io/tools/rosetta v0.2.1
github.com/IGLOU-EU/go-wildcard v1.0.3
github.com/armon/go-metrics v0.4.1
Expand All @@ -16,12 +16,13 @@ require (
github.com/cosmos/cosmos-sdk v0.47.5
github.com/cosmos/go-bip39 v1.0.0
github.com/cosmos/gogoproto v1.4.10
github.com/cosmos/ibc-go/v7 v7.2.0
github.com/cosmos/ibc-go/v7 v7.3.1
github.com/gogo/protobuf v1.3.2
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.3
github.com/gorilla/mux v1.8.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/initia-labs/OPinit v0.1.2-beta.0.0.20231110091824-89efdc572fa2
// we also need to update `LIBINITIAVM_VERSION` of images/private/Dockerfile#5
github.com/initia-labs/initiavm v0.1.2-beta.1
github.com/novifinancial/serde-reflection/serde-generate/runtime/golang v0.0.0-20220519162058-e5cd3c3b3f3a
Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ cosmossdk.io/log v1.2.1 h1:Xc1GgTCicniwmMiKwDxUjO4eLhPxoVdI9vtMW8Ti/uk=
cosmossdk.io/log v1.2.1/go.mod h1:GNSCc/6+DhFIj1aLn/j7Id7PaO8DzNylUZoOYBL9+I4=
cosmossdk.io/math v1.1.2 h1:ORZetZCTyWkI5GlZ6CZS28fMHi83ZYf+A2vVnHNzZBM=
cosmossdk.io/math v1.1.2/go.mod h1:l2Gnda87F0su8a/7FEKJfFdJrM0JZRXQaohlgJeyQh0=
cosmossdk.io/simapp v0.0.0-20230323161446-0af178d721ff h1:P1ialzTepD1oxdNPYc5N8Eggq3RdejZq3cJs8YYMs9Y=
cosmossdk.io/simapp v0.0.0-20230323161446-0af178d721ff/go.mod h1:AKzx6Mb544LjJ9RHmGFHjY9rEOLiUAi8I0F727TR0dY=
cosmossdk.io/simapp v0.0.0-20230831152633-2e9e5d6eea24 h1:A+LHM41uss8WNJ+qZCdvCjtiG6UiIN7ZqmPZz6R25wc=
cosmossdk.io/simapp v0.0.0-20230831152633-2e9e5d6eea24/go.mod h1:1kVkzonF2p6SYsSMXXURz/kx50QWArqazCk1Myulu8g=
cosmossdk.io/tools/rosetta v0.2.1 h1:ddOMatOH+pbxWbrGJKRAawdBkPYLfKXutK9IETnjYxw=
cosmossdk.io/tools/rosetta v0.2.1/go.mod h1:Pqdc1FdvkNV3LcNIkYWt2RQY6IP1ge6YWZk8MhhO9Hw=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
Expand Down Expand Up @@ -399,8 +399,8 @@ github.com/cosmos/gogoproto v1.4.10 h1:QH/yT8X+c0F4ZDacDv3z+xE3WU1P1Z3wQoLMBRJoK
github.com/cosmos/gogoproto v1.4.10/go.mod h1:3aAZzeRWpAwr+SS/LLkICX2/kDFyaYVzckBDzygIxek=
github.com/cosmos/iavl v0.20.0 h1:fTVznVlepH0KK8NyKq8w+U7c2L6jofa27aFX6YGlm38=
github.com/cosmos/iavl v0.20.0/go.mod h1:WO7FyvaZJoH65+HFOsDir7xU9FWk2w9cHXNW1XHcl7A=
github.com/cosmos/ibc-go/v7 v7.2.0 h1:dx0DLUl7rxdyZ8NiT6UsrbzKOJx/w7s+BOaewFRH6cg=
github.com/cosmos/ibc-go/v7 v7.2.0/go.mod h1:OOcjKIRku/j1Xs1RgKK0yvKRrJ5iFuZYMetR1n3yMlc=
github.com/cosmos/ibc-go/v7 v7.3.1 h1:bil1IjnHdyWDASFYKfwdRiNtFP6WK3osW7QFEAgU4I8=
github.com/cosmos/ibc-go/v7 v7.3.1/go.mod h1:wvx4pPBofe5ZdMNV3OFRxSI4auEP5Qfqf8JXLLNV04g=
github.com/cosmos/ics23/go v0.10.0 h1:iXqLLgp2Lp+EdpIuwXTYIQU+AiHj9mOC2X9ab++bZDM=
github.com/cosmos/ics23/go v0.10.0/go.mod h1:ZfJSmng/TBNTBkFemHHHj5YY7VAU/MBU980F4VU1NG0=
github.com/cosmos/keyring v1.2.0 h1:8C1lBP9xhImmIabyXW4c3vFjjLiBdGCmfLUfeZlV1Yo=
Expand Down Expand Up @@ -782,6 +782,8 @@ github.com/influxdata/promql/v2 v2.12.0/go.mod h1:fxOPu+DY0bqCTCECchSRtWfc+0X19y
github.com/influxdata/roaring v0.4.13-0.20180809181101-fc520f41fab6/go.mod h1:bSgUQ7q5ZLSO+bKBGqJiCBGAl+9DxyW63zLTujjUlOE=
github.com/influxdata/tdigest v0.0.0-20181121200506-bf2b5ad3c0a9/go.mod h1:Js0mqiSBE6Ffsg94weZZ2c+v/ciT8QRHFOap7EKDrR0=
github.com/influxdata/usage-client v0.0.0-20160829180054-6d3895376368/go.mod h1:Wbbw6tYNvwa5dlB6304Sd+82Z3f7PmVZHVKU637d4po=
github.com/initia-labs/OPinit v0.1.2-beta.0.0.20231110091824-89efdc572fa2 h1:N2FTlUD9vgbj2ur1/b30IwT94LrOFeJYpcP3zqlYIrE=
github.com/initia-labs/OPinit v0.1.2-beta.0.0.20231110091824-89efdc572fa2/go.mod h1:MPnFU2x4xzeT3L8K3lc0wkOl09o/oekEabPHIwWdqJg=
github.com/initia-labs/initiavm v0.1.2-beta.1 h1:2ady0ofGJ0Zz495RzXBQxCCFmfHlNdroYUeCuEq5o6A=
github.com/initia-labs/initiavm v0.1.2-beta.1/go.mod h1:aQt4lImZWF9xj7Fm978n0IoeDdKWXTg1CSq6O4WoSD8=
github.com/jackpal/go-nat-pmp v1.0.2/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc=
Expand Down

0 comments on commit 2cb72c6

Please sign in to comment.