Skip to content

Commit

Permalink
feat:add ibc stack
Browse files Browse the repository at this point in the history
  • Loading branch information
tbruyelle committed Sep 27, 2024
1 parent 908e29d commit 9fd24cd
Show file tree
Hide file tree
Showing 9 changed files with 405 additions and 35 deletions.
9 changes: 9 additions & 0 deletions ante/ante.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package ante

import (
ibcante "github.com/cosmos/ibc-go/v7/modules/core/ante"
ibckeeper "github.com/cosmos/ibc-go/v7/modules/core/keeper"

errorsmod "cosmossdk.io/errors"

"github.com/cosmos/cosmos-sdk/codec"
Expand All @@ -16,6 +19,7 @@ import (
type HandlerOptions struct {
ante.HandlerOptions
Codec codec.BinaryCodec
IBCkeeper *ibckeeper.Keeper
StakingKeeper *stakingkeeper.Keeper
TxFeeChecker ante.TxFeeChecker
}
Expand All @@ -30,6 +34,10 @@ func NewAnteHandler(opts HandlerOptions) (sdk.AnteHandler, error) {
if opts.SignModeHandler == nil {
return nil, errorsmod.Wrap(atomoneerrors.ErrLogic, "sign mode handler is required for AnteHandler")
}
if opts.IBCkeeper == nil {
return nil, errorsmod.Wrap(atomoneerrors.ErrLogic, "IBC keeper is required for AnteHandler")
}

if opts.StakingKeeper == nil {
return nil, errorsmod.Wrap(atomoneerrors.ErrNotFound, "staking param store is required for AnteHandler")
}
Expand All @@ -52,6 +60,7 @@ func NewAnteHandler(opts HandlerOptions) (sdk.AnteHandler, error) {
ante.NewSigGasConsumeDecorator(opts.AccountKeeper, sigGasConsumer),
ante.NewSigVerificationDecorator(opts.AccountKeeper, opts.SignModeHandler),
ante.NewIncrementSequenceDecorator(opts.AccountKeeper),
ibcante.NewRedundantRelayDecorator(opts.IBCkeeper),
}

return sdk.ChainAnteDecorators(anteDecorators...), nil
Expand Down
3 changes: 3 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"github.com/cometbft/cometbft/libs/log"
tmos "github.com/cometbft/cometbft/libs/os"

ibctesting "github.com/cosmos/ibc-go/v7/testing"

autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"

Expand Down Expand Up @@ -62,6 +64,7 @@ var (
var (
_ runtime.AppI = (*AtomOneApp)(nil)
_ servertypes.Application = (*AtomOneApp)(nil)
_ ibctesting.TestingApp = (*AtomOneApp)(nil)
)

// AtomOneApp extends an ABCI application, but with most of its parameters exported.
Expand Down
23 changes: 23 additions & 0 deletions app/app_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package atomone

import (
ibckeeper "github.com/cosmos/ibc-go/v7/modules/core/keeper"
ibctestingtypes "github.com/cosmos/ibc-go/v7/testing/types"

capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper"
)

// GetStakingKeeper implements the TestingApp interface. Needed for ICS.
func (app *AtomOneApp) GetStakingKeeper() ibctestingtypes.StakingKeeper { //nolint:nolintlint
return app.StakingKeeper
}

// GetIBCKeeper implements the TestingApp interface.
func (app *AtomOneApp) GetIBCKeeper() *ibckeeper.Keeper { //nolint:nolintlint
return app.IBCKeeper
}

// GetScopedIBCKeeper implements the TestingApp interface.
func (app *AtomOneApp) GetScopedIBCKeeper() capabilitykeeper.ScopedKeeper { //nolint:nolintlint
return app.ScopedIBCKeeper
}
203 changes: 185 additions & 18 deletions app/gov_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/pflag"

ibcclienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
paramscutils "github.com/cosmos/cosmos-sdk/x/params/client/utils"
Expand All @@ -19,14 +24,16 @@ import (
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

govclient "github.com/atomone-hub/atomone/x/gov/client"
"github.com/atomone-hub/atomone/x/gov/client/cli"
govcli "github.com/atomone-hub/atomone/x/gov/client/cli"
govv1beta1 "github.com/atomone-hub/atomone/x/gov/types/v1beta1"
)

var (
paramsChangeProposalHandler = govclient.NewProposalHandler(newSubmitParamChangeProposalTxCmd)
upgradeProposalHandler = govclient.NewProposalHandler(newCmdSubmitLegacyUpgradeProposal)
cancelUpgradeProposalHandler = govclient.NewProposalHandler(newCmdSubmitLegacyCancelUpgradeProposal)
paramsChangeProposalHandler = govclient.NewProposalHandler(newSubmitParamChangeProposalTxCmd)
upgradeProposalHandler = govclient.NewProposalHandler(newCmdSubmitLegacyUpgradeProposal)
cancelUpgradeProposalHandler = govclient.NewProposalHandler(newCmdSubmitLegacyCancelUpgradeProposal)
updateIBCClientProposalHandler = govclient.NewProposalHandler(newCmdSubmitUpdateIBCClientProposal)
upgradeIBCProposalHandler = govclient.NewProposalHandler(NewCmdSubmitUpgradeProposal)
)

func init() {
Expand Down Expand Up @@ -167,7 +174,7 @@ func newCmdSubmitLegacyUpgradeProposal() *cobra.Command {

from := clientCtx.GetFromAddress()

depositStr, err := cmd.Flags().GetString(cli.FlagDeposit)
depositStr, err := cmd.Flags().GetString(govcli.FlagDeposit)
if err != nil {
return err
}
Expand All @@ -185,9 +192,9 @@ func newCmdSubmitLegacyUpgradeProposal() *cobra.Command {
},
}

cmd.Flags().String(cli.FlagTitle, "", "title of proposal")
cmd.Flags().String(cli.FlagDescription, "", "description of proposal") //nolint:staticcheck // we are intentionally using a deprecated flag here.
cmd.Flags().String(cli.FlagDeposit, "", "deposit of proposal")
cmd.Flags().String(govcli.FlagTitle, "", "title of proposal")
cmd.Flags().String(govcli.FlagDescription, "", "description of proposal") //nolint:staticcheck // we are intentionally using a deprecated flag here.
cmd.Flags().String(govcli.FlagDeposit, "", "deposit of proposal")
cmd.Flags().Int64(FlagUpgradeHeight, 0, "The height at which the upgrade must happen")
cmd.Flags().String(FlagUpgradeInfo, "", "Info for the upgrade plan such as new version download urls, etc.")
cmd.Flags().Bool(FlagNoValidate, false, "Skip validation of the upgrade info")
Expand Down Expand Up @@ -215,7 +222,7 @@ func newCmdSubmitLegacyCancelUpgradeProposal() *cobra.Command {
}
from := clientCtx.GetFromAddress()

depositStr, err := cmd.Flags().GetString(cli.FlagDeposit)
depositStr, err := cmd.Flags().GetString(govcli.FlagDeposit)
if err != nil {
return err
}
Expand All @@ -225,12 +232,12 @@ func newCmdSubmitLegacyCancelUpgradeProposal() *cobra.Command {
return err
}

title, err := cmd.Flags().GetString(cli.FlagTitle)
title, err := cmd.Flags().GetString(govcli.FlagTitle)
if err != nil {
return err
}

description, err := cmd.Flags().GetString(cli.FlagDescription) //nolint:staticcheck // we are intentionally using a deprecated flag here.
description, err := cmd.Flags().GetString(govcli.FlagDescription) //nolint:staticcheck // we are intentionally using a deprecated flag here.
if err != nil {
return err
}
Expand All @@ -246,11 +253,11 @@ func newCmdSubmitLegacyCancelUpgradeProposal() *cobra.Command {
},
}

cmd.Flags().String(cli.FlagTitle, "", "title of proposal")
cmd.Flags().String(cli.FlagDescription, "", "description of proposal") //nolint:staticcheck // we are intentionally using a deprecated flag here.
cmd.Flags().String(cli.FlagDeposit, "", "deposit of proposal")
cmd.MarkFlagRequired(cli.FlagTitle) //nolint:errcheck
cmd.MarkFlagRequired(cli.FlagDescription) //nolint:staticcheck,errcheck // we are intentionally using a deprecated flag here.
cmd.Flags().String(govcli.FlagTitle, "", "title of proposal")
cmd.Flags().String(govcli.FlagDescription, "", "description of proposal") //nolint:staticcheck // we are intentionally using a deprecated flag here.
cmd.Flags().String(govcli.FlagDeposit, "", "deposit of proposal")
cmd.MarkFlagRequired(govcli.FlagTitle) //nolint:errcheck
cmd.MarkFlagRequired(govcli.FlagDescription) //nolint:staticcheck,errcheck // we are intentionally using a deprecated flag here.

return cmd
}
Expand All @@ -268,12 +275,12 @@ func getDefaultDaemonName() string {
}

func parseArgsToContent(fs *pflag.FlagSet, name string) (govv1beta1.Content, error) {
title, err := fs.GetString(cli.FlagTitle)
title, err := fs.GetString(govcli.FlagTitle)
if err != nil {
return nil, err
}

description, err := fs.GetString(cli.FlagDescription) //nolint:staticcheck // we are intentionally using a deprecated flag here.
description, err := fs.GetString(govcli.FlagDescription) //nolint:staticcheck // we are intentionally using a deprecated flag here.
if err != nil {
return nil, err
}
Expand All @@ -292,3 +299,163 @@ func parseArgsToContent(fs *pflag.FlagSet, name string) (govv1beta1.Content, err
content := upgradetypes.NewSoftwareUpgradeProposal(title, description, plan)
return content, nil
}

// newCmdSubmitUpdateIBCClientProposal implements a command handler for submitting an update IBC client proposal transaction.
func newCmdSubmitUpdateIBCClientProposal() *cobra.Command {
cmd := &cobra.Command{
Use: "update-client [subject-client-id] [substitute-client-id]",
Args: cobra.ExactArgs(2),
Short: "Submit an update IBC client proposal",
Long: "Submit an update IBC client proposal along with an initial deposit.\n" +
"Please specify a subject client identifier you want to update..\n" +
"Please specify the substitute client the subject client will be updated to.",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

title, err := cmd.Flags().GetString(govcli.FlagTitle) //nolint:staticcheck // need this till full govv1 conversion.

Check failure on line 318 in app/gov_handlers.go

View workflow job for this annotation

GitHub Actions / golangci-lint

directive `//nolint:staticcheck // need this till full govv1 conversion.` is unused for linter "staticcheck" (nolintlint)

Check failure on line 318 in app/gov_handlers.go

View workflow job for this annotation

GitHub Actions / Analyze

directive `//nolint:staticcheck // need this till full govv1 conversion.` is unused for linter "staticcheck" (nolintlint)
if err != nil {
return err
}

description, err := cmd.Flags().GetString(govcli.FlagDescription) //nolint:staticcheck // need this till full govv1 conversion.
if err != nil {
return err
}

subjectClientID := args[0]
substituteClientID := args[1]

content := ibcclienttypes.NewClientUpdateProposal(title, description, subjectClientID, substituteClientID)

from := clientCtx.GetFromAddress()

depositStr, err := cmd.Flags().GetString(govcli.FlagDeposit)
if err != nil {
return err
}
deposit, err := sdk.ParseCoinsNormalized(depositStr)
if err != nil {
return err
}

msg, err := govv1beta1.NewMsgSubmitProposal(content, deposit, from)
if err != nil {
return err
}

if err = msg.ValidateBasic(); err != nil {
return err
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

cmd.Flags().String(govcli.FlagTitle, "", "title of proposal") //nolint:staticcheck // need this till full govv1 conversion.

Check failure on line 357 in app/gov_handlers.go

View workflow job for this annotation

GitHub Actions / golangci-lint

directive `//nolint:staticcheck // need this till full govv1 conversion.` is unused for linter "staticcheck" (nolintlint)

Check failure on line 357 in app/gov_handlers.go

View workflow job for this annotation

GitHub Actions / Analyze

directive `//nolint:staticcheck // need this till full govv1 conversion.` is unused for linter "staticcheck" (nolintlint)
cmd.Flags().String(govcli.FlagDescription, "", "description of proposal") //nolint:staticcheck // need this till full govv1 conversion.
cmd.Flags().String(govcli.FlagDeposit, "", "deposit of proposal")

return cmd
}

// NewCmdSubmitUpgradeProposal implements a command handler for submitting an upgrade IBC client proposal transaction.
func NewCmdSubmitUpgradeProposal() *cobra.Command {
cmd := &cobra.Command{
Use: "ibc-upgrade [name] [height] [path/to/upgraded_client_state.json] [flags]",
Args: cobra.ExactArgs(3),
Short: "Submit an IBC upgrade proposal",
Long: "Submit an IBC client breaking upgrade proposal along with an initial deposit.\n" +
"The client state specified is the upgraded client state representing the upgraded chain\n" +
`Example Upgraded Client State JSON:
{
"@type":"/ibc.lightclients.tendermint.v1.ClientState",
"chain_id":"testchain1",
"unbonding_period":"1814400s",
"latest_height":{"revision_number":"0","revision_height":"2"},
"proof_specs":[{"leaf_spec":{"hash":"SHA256","prehash_key":"NO_HASH","prehash_value":"SHA256","length":"VAR_PROTO","prefix":"AA=="},"inner_spec":{"child_order":[0,1],"child_size":33,"min_prefix_length":4,"max_prefix_length":12,"empty_child":null,"hash":"SHA256"},"max_depth":0,"min_depth":0},{"leaf_spec":{"hash":"SHA256","prehash_key":"NO_HASH","prehash_value":"SHA256","length":"VAR_PROTO","prefix":"AA=="},"inner_spec":{"child_order":[0,1],"child_size":32,"min_prefix_length":1,"max_prefix_length":1,"empty_child":null,"hash":"SHA256"},"max_depth":0,"min_depth":0}],
"upgrade_path":["upgrade","upgradedIBCState"],
}
`,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
cdc := codec.NewProtoCodec(clientCtx.InterfaceRegistry)

title, err := cmd.Flags().GetString(govcli.FlagTitle) //nolint:staticcheck // need this till full govv1 conversion.

Check failure on line 389 in app/gov_handlers.go

View workflow job for this annotation

GitHub Actions / golangci-lint

directive `//nolint:staticcheck // need this till full govv1 conversion.` is unused for linter "staticcheck" (nolintlint)

Check failure on line 389 in app/gov_handlers.go

View workflow job for this annotation

GitHub Actions / Analyze

directive `//nolint:staticcheck // need this till full govv1 conversion.` is unused for linter "staticcheck" (nolintlint)
if err != nil {
return err
}

description, err := cmd.Flags().GetString(govcli.FlagDescription) //nolint:staticcheck // need this till full govv1 conversion.
if err != nil {
return err
}

name := args[0]

height, err := strconv.ParseInt(args[1], 10, 64)
if err != nil {
return err
}

plan := upgradetypes.Plan{
Name: name,
Height: height,
}

// attempt to unmarshal client state argument
var clientState ibcexported.ClientState
clientContentOrFileName := args[2]
if err := cdc.UnmarshalInterfaceJSON([]byte(clientContentOrFileName), &clientState); err != nil {

// check for file path if JSON input is not provided
contents, err := os.ReadFile(clientContentOrFileName)
if err != nil {
return fmt.Errorf("neither JSON input nor path to .json file for client state were provided: %w", err)
}

if err := cdc.UnmarshalInterfaceJSON(contents, &clientState); err != nil {
return fmt.Errorf("error unmarshalling client state file: %w", err)
}
}

content, err := ibcclienttypes.NewUpgradeProposal(title, description, plan, clientState)
if err != nil {
return err
}

from := clientCtx.GetFromAddress()

depositStr, err := cmd.Flags().GetString(govcli.FlagDeposit)
if err != nil {
return err
}
deposit, err := sdk.ParseCoinsNormalized(depositStr)
if err != nil {
return err
}

msg, err := govv1beta1.NewMsgSubmitProposal(content, deposit, from)
if err != nil {
return err
}

if err = msg.ValidateBasic(); err != nil {
return err
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

cmd.Flags().String(govcli.FlagTitle, "", "title of proposal") //nolint:staticcheck // need this till full govv1 conversion.

Check failure on line 456 in app/gov_handlers.go

View workflow job for this annotation

GitHub Actions / golangci-lint

directive `//nolint:staticcheck // need this till full govv1 conversion.` is unused for linter "staticcheck" (nolintlint)

Check failure on line 456 in app/gov_handlers.go

View workflow job for this annotation

GitHub Actions / Analyze

directive `//nolint:staticcheck // need this till full govv1 conversion.` is unused for linter "staticcheck" (nolintlint)
cmd.Flags().String(govcli.FlagDescription, "", "description of proposal") //nolint:staticcheck // need this till full govv1 conversion.
cmd.Flags().String(govcli.FlagDeposit, "", "deposit of proposal")

return cmd
}
Loading

0 comments on commit 9fd24cd

Please sign in to comment.