From 2cb72c6d16c22a6d068b0c55603a459e8f753845 Mon Sep 17 00:00:00 2001 From: beer-1 <147697694+beer-1@users.noreply.github.com> Date: Fri, 10 Nov 2023 18:46:41 +0900 Subject: [PATCH] add ophost module --- app/app.go | 22 +++++++++++ app/hook/bridge_hook.go | 81 +++++++++++++++++++++++++++++++++++++++++ go.mod | 5 ++- go.sum | 10 +++-- 4 files changed, 112 insertions(+), 6 deletions(-) create mode 100644 app/hook/bridge_hook.go diff --git a/app/app.go b/app/app.go index 2f3077a7..512ea95d 100644 --- a/app/app.go +++ b/app/app.go @@ -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" @@ -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" ) @@ -211,6 +216,7 @@ var ( ibcperm.AppModuleBasic{}, move.AppModuleBasic{}, auction.AppModuleBasic{}, + ophost.AppModuleBasic{}, ) // module account permissions @@ -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 @@ -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) @@ -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. @@ -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 @@ -854,6 +873,7 @@ func NewInitiaApp( ibcpermtypes.ModuleName, consensusparamtypes.ModuleName, auctiontypes.ModuleName, + ophosttypes.ModuleName, ) app.mm.SetOrderEndBlockers( @@ -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 @@ -922,6 +943,7 @@ func NewInitiaApp( ibcpermtypes.ModuleName, consensusparamtypes.ModuleName, auctiontypes.ModuleName, + ophosttypes.ModuleName, ) app.mm.RegisterInvariants(app.CrisisKeeper) diff --git a/app/hook/bridge_hook.go b/app/hook/bridge_hook.go new file mode 100644 index 00000000..2ae52118 --- /dev/null +++ b/app/hook/bridge_hook.go @@ -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 +} diff --git a/go.mod b/go.mod index f0b0ef4a..6f4f899b 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/go.sum b/go.sum index d77c7429..084e5712 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -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=