Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ophost module #13

Merged
merged 6 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,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 @@ -151,6 +152,10 @@ import (
builderkeeper "github.com/skip-mev/pob/x/builder/keeper"
buildertypes "github.com/skip-mev/pob/x/builder/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 @@ -206,6 +211,7 @@ var (
ibcperm.AppModuleBasic{},
move.AppModuleBasic{},
builder.AppModuleBasic{},
ophost.AppModuleBasic{},
)

// module account permissions
Expand Down Expand Up @@ -288,6 +294,7 @@ type InitiaApp struct {
IBCPermKeeper *ibcpermkeeper.Keeper
MoveKeeper *movekeeper.Keeper
BuilderKeeper *builderkeeper.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 @@ -339,6 +346,7 @@ func NewInitiaApp(
authzkeeper.StoreKey, feegrant.StoreKey, icahosttypes.StoreKey,
icacontrollertypes.StoreKey, icaauthtypes.StoreKey, ibcfeetypes.StoreKey,
routertypes.StoreKey, ibcpermtypes.StoreKey, movetypes.StoreKey, buildertypes.StoreKey,
ophosttypes.StoreKey,
)
tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
memKeys := sdk.NewMemoryStoreKeys(capabilitytypes.MemStoreKey)
Expand Down Expand Up @@ -754,6 +762,16 @@ func NewInitiaApp(
)
app.BuilderKeeper = &builderKeeper

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 @@ -814,6 +832,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 @@ -849,6 +868,7 @@ func NewInitiaApp(
ibcpermtypes.ModuleName,
consensusparamtypes.ModuleName,
buildertypes.ModuleName,
ophosttypes.ModuleName,
)

app.mm.SetOrderEndBlockers(
Expand Down Expand Up @@ -880,6 +900,7 @@ func NewInitiaApp(
ibcpermtypes.ModuleName,
consensusparamtypes.ModuleName,
buildertypes.ModuleName,
ophosttypes.ModuleName,
)

// NOTE: The genutils module must occur after staking so that pools are
Expand Down Expand Up @@ -917,6 +938,7 @@ func NewInitiaApp(
ibcpermtypes.ModuleName,
consensusparamtypes.ModuleName,
buildertypes.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
}
8 changes: 8 additions & 0 deletions client/docs/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,14 @@
"Params": "MoveParams"
}
}
},
{
"url": "./tmp-swagger-gen/opinit/ophost/v1/query.swagger.json",
"operationIds": {
"rename": {
"Params": "OPHostParams"
}
}
}
]
}
2 changes: 1 addition & 1 deletion client/docs/statik/statik.go

Large diffs are not rendered by default.

Loading
Loading