From b89e03b9553a4f07278b9ea0ee418539cb4e9f3b Mon Sep 17 00:00:00 2001 From: Sean King Date: Tue, 7 Dec 2021 18:10:32 +0100 Subject: [PATCH] Feature: Interchain accounts v1 (#380) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * migrate ibc-account module to ibc-go * Add @seantking to CODEOWNERS for interchain accounts module * rename ibc-account -> interchain-accounts (#280) * rename ibc-account -> interchain-accounts * fix codeowner file * Integrate ICA into testing package, add simple keeper tests (#282) * add ica to simapp * add simple keeper tests * Add interchain account OnChanOpenInit and InitInterchainAccount tests (#287) * add OnChanOpenInit test * add InitInterchainAccount test * Update modules/apps/27-interchain-accounts/keeper/relay.go * feat: ica proto (#305) * feat: adding proto files for interchain accounts & updating references of IBCAccount -> InterchainAccount * doc: updating comments * docs: update comment * fix: updating proto yaml, query name, query params, comments * ICA OnChanOpenTry update + tests (#299) * update OnChanOpenTry to match ICS specs * write OnChanOpenTry and OnChanOpenAck tests * update comment * test: adding test for OnChanOpenConfirm (#312) * fix: updating port-id & fixing OnChanOpenInit bug (#321) * fix: updating port-id & fixing OnChanOpenInit bug * Update modules/apps/27-interchain-accounts/keeper/handshake.go Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> * Add counterparty port ID to controller portID (#319) * refactor! move GeneratePortID to types, add counterpartyConnection sequence change all PortId -> PortID move GeneratePortID to types package add counterparty connection sequence argument utilize connectiontypes connectionID parsing function * refactor! use counterparty portID in interchain account address gRPC Remove existing args from gRPC request for interchain account address Use counterparty portID * tests add generate port id tests * apply self-review fixes * test: check active channel is correct (#324) * test: check active channel is correct * test: adding version string check * Removed memkey from ICA keeper (#342) * Removed memkey from ICA keeper * Removed memkey arg in call to NewKeeper from simapp * moving stateless GenerateAddress func to types (#352) * update ica branch codeowners (#353) * move Get/SetInterchainAccount to keeper.go, add tests (#355) * moving setter/getter funcs to keeper.go, adding tests * removing redundant var * fixing test assertion string typo * updating Get/SetInterchainAccount func signatures, tests and adding godocs * grouping imports according, updating suite.FailNow() -> suite.Require().NoError() Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> * chore: interchain accounts cleanup, cli alias (#362) * adding ica alias for interchain-accounts queries * refactoring bind port and claim capability functionality, code cleanup and godocs * updating interchain accounts pkg naming (#364) * adding ica grpc query tests (#368) * updating grpc query tests, removing queryClient on KeeperTestSuite (#379) * ICA: Adding tests for relay.go (#337) * test: adding test for TrySendTx * test: adding tests for data check * test: adding check for SendTx with []sdk.Message * chore: seperate imports * test: add helper function for creating ICA path * test: adding cases for incorrect outgoing data & channel does not exist * Update ICA on main + add app version negotiation stub (#403) * Bump github.com/cosmos/cosmos-sdk from 0.43.0-rc1 to 0.43.0-rc2 (#269) Bumps [github.com/cosmos/cosmos-sdk](https://github.com/cosmos/cosmos-sdk) from 0.43.0-rc1 to 0.43.0-rc2. - [Release notes](https://github.com/cosmos/cosmos-sdk/releases) - [Changelog](https://github.com/cosmos/cosmos-sdk/blob/v0.43.0-rc2/CHANGELOG.md) - [Commits](https://github.com/cosmos/cosmos-sdk/compare/v0.43.0-rc1...v0.43.0-rc2) --- updated-dependencies: - dependency-name: github.com/cosmos/cosmos-sdk dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * generate swagger files (#267) * Reject Redundant Tx Antedecorator (#235) * writeup simple antedecorator * only do antehandler on checkTx * Update modules/core/04-channel/ante.go Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> * enable 2 antehandler strategies, and write tests * remove strict decorator and pass on non-packet/update type * move ante logic into its own package * changelog Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> * perform a no-op on redundant relay messages (#268) * create initial changes for delivertx handling * handle closed channel no-ops, fix tests * self review nits * add changelog * add events for no op messages * add back comment * Bump codecov/codecov-action from 1.5.2 to 2.0.1 (#273) Bumps [codecov/codecov-action](https://github.com/codecov/codecov-action) from 1.5.2 to 2.0.1. - [Release notes](https://github.com/codecov/codecov-action/releases) - [Changelog](https://github.com/codecov/codecov-action/blob/master/CHANGELOG.md) - [Commits](https://github.com/codecov/codecov-action/compare/v1.5.2...v2.0.1) --- updated-dependencies: - dependency-name: codecov/codecov-action dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> * remove ChanCloseInit function from transfer keeper (#275) * remove CloseChanInit from transfer * add changelog Co-authored-by: Aditya * Bump codecov/codecov-action from 2.0.1 to 2.0.2 (#296) Bumps [codecov/codecov-action](https://github.com/codecov/codecov-action) from 2.0.1 to 2.0.2. - [Release notes](https://github.com/codecov/codecov-action/releases) - [Changelog](https://github.com/codecov/codecov-action/blob/master/CHANGELOG.md) - [Commits](https://github.com/codecov/codecov-action/compare/v2.0.1...v2.0.2) --- updated-dependencies: - dependency-name: codecov/codecov-action dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Bump github.com/spf13/cast from 1.3.1 to 1.4.0 (#301) Bumps [github.com/spf13/cast](https://github.com/spf13/cast) from 1.3.1 to 1.4.0. - [Release notes](https://github.com/spf13/cast/releases) - [Commits](https://github.com/spf13/cast/compare/v1.3.1...v1.4.0) --- updated-dependencies: - dependency-name: github.com/spf13/cast dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Bump technote-space/get-diff-action from 4.2 to 5 (#306) Bumps [technote-space/get-diff-action](https://github.com/technote-space/get-diff-action) from 4.2 to 5. - [Release notes](https://github.com/technote-space/get-diff-action/releases) - [Changelog](https://github.com/technote-space/get-diff-action/blob/main/.releasegarc) - [Commits](https://github.com/technote-space/get-diff-action/compare/v4.2...v5) --- updated-dependencies: - dependency-name: technote-space/get-diff-action dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * bump to SDK v0.43.0-rc3 (#308) * SDK v0.43.0-rc3 * add capability fixes * add @seantking as codeowner to interchain accounts (#309) * Bump google.golang.org/grpc from 1.39.0 to 1.39.1 (#320) Bumps [google.golang.org/grpc](https://github.com/grpc/grpc-go) from 1.39.0 to 1.39.1. - [Release notes](https://github.com/grpc/grpc-go/releases) - [Commits](https://github.com/grpc/grpc-go/compare/v1.39.0...v1.39.1) --- updated-dependencies: - dependency-name: google.golang.org/grpc dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Bump github.com/cosmos/cosmos-sdk from 0.43.0-rc3 to 0.43.0 (#325) Bumps [github.com/cosmos/cosmos-sdk](https://github.com/cosmos/cosmos-sdk) from 0.43.0-rc3 to 0.43.0. - [Release notes](https://github.com/cosmos/cosmos-sdk/releases) - [Changelog](https://github.com/cosmos/cosmos-sdk/blob/master/CHANGELOG.md) - [Commits](https://github.com/cosmos/cosmos-sdk/compare/v0.43.0-rc3...v0.43.0) --- updated-dependencies: - dependency-name: github.com/cosmos/cosmos-sdk dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * module: improve 04-channel logging (#323) * module: improve 04-channel logging * update log Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> * update changelog (#326) * Bump google.golang.org/grpc from 1.39.1 to 1.40.0 (#332) Bumps [google.golang.org/grpc](https://github.com/grpc/grpc-go) from 1.39.1 to 1.40.0. - [Release notes](https://github.com/grpc/grpc-go/releases) - [Commits](https://github.com/grpc/grpc-go/compare/v1.39.1...v1.40.0) --- updated-dependencies: - dependency-name: google.golang.org/grpc dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Bump github.com/spf13/cast from 1.4.0 to 1.4.1 (#338) Bumps [github.com/spf13/cast](https://github.com/spf13/cast) from 1.4.0 to 1.4.1. - [Release notes](https://github.com/spf13/cast/releases) - [Commits](https://github.com/spf13/cast/compare/v1.4.0...v1.4.1) --- updated-dependencies: - dependency-name: github.com/spf13/cast dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Bump github.com/tendermint/tendermint from 0.34.11 to 0.34.12 (#341) Bumps [github.com/tendermint/tendermint](https://github.com/tendermint/tendermint) from 0.34.11 to 0.34.12. - [Release notes](https://github.com/tendermint/tendermint/releases) - [Changelog](https://github.com/tendermint/tendermint/blob/master/CHANGELOG.md) - [Commits](https://github.com/tendermint/tendermint/compare/v0.34.11...v0.34.12) --- updated-dependencies: - dependency-name: github.com/tendermint/tendermint dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> * fix broken links in integration.md (#340) * fix broken link in integration.md * fix: broken link to simulator.md file in cosmos-sdk docs Co-authored-by: Carlos Rodriguez * Created helper functions for emitting packet events (#343) * Created helper functions for emitting packet events * Fixed comments and re-ordered helper functions Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> * Merge pull request from GHSA-qrhq-96mh-q8jv * Bump codecov/codecov-action from 2.0.2 to 2.0.3 (#346) Bumps [codecov/codecov-action](https://github.com/codecov/codecov-action) from 2.0.2 to 2.0.3. - [Release notes](https://github.com/codecov/codecov-action/releases) - [Changelog](https://github.com/codecov/codecov-action/blob/master/CHANGELOG.md) - [Commits](https://github.com/codecov/codecov-action/compare/v2.0.2...v2.0.3) --- updated-dependencies: - dependency-name: codecov/codecov-action dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> * Bump actions/setup-go from 2.1.3 to 2.1.4 (#349) Bumps [actions/setup-go](https://github.com/actions/setup-go) from 2.1.3 to 2.1.4. - [Release notes](https://github.com/actions/setup-go/releases) - [Commits](https://github.com/actions/setup-go/compare/v2.1.3...v2.1.4) --- updated-dependencies: - dependency-name: actions/setup-go dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * increase port identifier limit to 128 characters (#344) * increase port identifier limit to 128 characters increase port limit and add tests for port validation * add changelog * fix tests * update codeowners to include new team members and granular ownership (#354) * update codeowners * add proto files to ownership * bump SDK dependency (#367) * update SDK dependency and fix changes Removes tests from sdk_test.go which are no longer needed to upstream changes in the SDK Fixes client_test.go due to inclusion of the fee in tx events * bump SDK version to v0.44.0 * adding client status cli query (#372) * adding client status cli query * adding query client status cli to changelog * updating long CLI help usage * adding markdown link checker to ci workflows (#377) * packet acknowledgment filtering (#375) * adding packet commitments to QueryPacketAcknowledgementsRequest for filtering * adding testcase for filtered packet ack query * adding changelog entry for packet ack filtering * updating packet sequences type to repeated uint64 * updating to query specific packet acks outside bounds of paginated req * updating changelog field naming, removing redundant pagination in query test * continue in favour of returning an error on query PacketAcknowledgements * updating to return empty array of acks if none of the provided commitments are found Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> * Bump github.com/tendermint/tendermint from 0.34.12 to 0.34.13 (#386) Bumps [github.com/tendermint/tendermint](https://github.com/tendermint/tendermint) from 0.34.12 to 0.34.13. - [Release notes](https://github.com/tendermint/tendermint/releases) - [Changelog](https://github.com/tendermint/tendermint/blob/v0.34.13/CHANGELOG.md) - [Commits](https://github.com/tendermint/tendermint/compare/v0.34.12...v0.34.13) --- updated-dependencies: - dependency-name: github.com/tendermint/tendermint dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Change ICS 20 packet data amount to be string (#350) * modify proto defintions * fix non string usage in code and various tests * fix mbt tests * fix bug in data validation * fix various build issues fix unaddressed issues from changing amount from uint64 to string * add changelog entry * apply review suggestions Add check that amount is strictly positive Construct granular error messages to indicate invalid amount value or failure to parse amount * verify and fix telemetry bug Verify msg panics on amounts > int64 by adding tests Add checks to telemetry emission of transfer amounts to handle when the amount cannot be casted to float32 * fix: ibc build docs (#361) * initial fix commit * add release v1.0.1 to versions * improvements for docs website * add new version * address review comments Co-authored-by: Carlos Rodriguez Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> * feat: scalable version queries (#384) * adding protos for port query interface * adding NegotiateAppVersion method to IBCModule interface * adding grpc port query implementation and module surrounds * adding NegotiateAppVersion implementation to apps/transfer and mocks * updating ErrInvalidVersion error code * adding grpc query tests for 05-port * updating grpc naming, adding godocs, removing redundant query cli * updating grpc query tests * adding changelog entry for #384 app version negotiation * fixing error formatting in transfer version negotiation Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> * removing client/cli query * updating grpc query naming, adding new fields and associated surrounds Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> * Bump codecov/codecov-action from 2.0.3 to 2.1.0 (#399) Bumps [codecov/codecov-action](https://github.com/codecov/codecov-action) from 2.0.3 to 2.1.0. - [Release notes](https://github.com/codecov/codecov-action/releases) - [Changelog](https://github.com/codecov/codecov-action/blob/master/CHANGELOG.md) - [Commits](https://github.com/codecov/codecov-action/compare/v2.0.3...v2.1.0) --- updated-dependencies: - dependency-name: codecov/codecov-action dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * feat: adding fn stub for version negotiation Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> Co-authored-by: Aditya Co-authored-by: Aleksandr Bezobchuk Co-authored-by: Carlos Rodriguez Co-authored-by: Carlos Rodriguez Co-authored-by: Luke Rhoads <51463884+lukerhoads@users.noreply.github.com> Co-authored-by: Damian Nolan Co-authored-by: Charly * fix err message (#419) * feat: ica app version negotiation (#410) * adding NegotiateAppVersion implementation and tests * updating GenerateAddress to return sdk.AccAddress, fixing tests * updating ica handshake flow to parse address from version string, fixing associated tests * updating module_tests * derive ica addresses from module account addr * removing unused keys * adding improved version validation, updating tests * removing redundant local var - owner * updating Delimiter godoc * updating validation logic * adding test cases for ValidateVersion * adding additional validation testcase, updating godocs * updating Version -> VersionPrefix, error msgs, validation tests * updating NewAppVersion func sig and usage * updating NewAppVersion args and returning more appropriate errors for handshake * Update modules/apps/27-interchain-accounts/keeper/handshake.go Co-authored-by: Sean King * updating ValidateVersion godoc Co-authored-by: Sean King * test: adding tests for OnRecvPacket (#412) * test: adding tests for OnRecvPacket * test: adding further test cases for onRecvPacket * chore: merge latest main to interchain-accounts (#435) * chore: update ica prefix for port identifiers (#434) * removing ICAPrefix const in favour of VersionPrefix * updating tests * test: account/keeper tests for ICA (#420) * test: adding tests for account type * tests: adding test for keeper & account * fix: updating channel closing capabilities * fix: updating to use test library instead of hardcoded values * fix: updating error handling for account * test: adding test for account string comparison * fix: updating marshal yaml * feat: module account address derivation (#428) * adding module account to interchain-accounts with associated changes * configuring ica module account in simapp * Update modules/apps/27-interchain-accounts/keeper/keeper.go Co-authored-by: Sean King * updating godoc and import alias Co-authored-by: Sean King * ICA: tests for module.go (#424) * test * test: adding module.go test with handshake callback tests * tests: adding key tests * additional code cov and clean up (#440) * split ica module.go into ibc_module.go (#453) Splits IBCModule logic into separate ibc_module.go file and updates app.go registration * Rename IBCAccountPacketData, Remove TxRaw (#456) * rename IBCAccountPacketData, remove txRaw type * Update proto/ibc/applications/interchain_accounts/v1/types.proto * Rename DeserializeTx, enforce []sdk.Msg usage in SerializeCosmosTx (#457) * rename DeserializeTx to DeserializeCosmosTx, simply serialization logic * improve godoc wording * remove computeVirtualTxHash (#473) * chore: ctrl port connection id validation (#454) * adding pipe char | to identifier regex * updating GeneratePortID to use pipe delimiter in favour of dash, ParseAddressFromVersion to use Split in favour of TrimPrefix * adding CounterpartyHops method to expected channel keeper interface * updating tests to satisy delimiter updates * adding connection seq validation of ctrl port id and updating tests * cleanup * adding defensive check for ParseAddressFromVersion * adding conn sequence parsing funcs to pkg types * moving conn sequence validation to reusable func * updating error msgs, adding tests for conn seq parsers * adding expected sequence to error msgs * updating ParseCtrlConnSequence to ParseControllerConnSequence * fixing counterparty port error * Update modules/apps/27-interchain-accounts/keeper/handshake.go Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> * Update modules/apps/27-interchain-accounts/keeper/handshake.go Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> * Update modules/apps/27-interchain-accounts/types/account.go Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> * removing pipe from valid identifier regex * adding error returns to parsing funcs, updating tests, error messages * separting imports in keys.go * updating handshake tests * Update modules/apps/27-interchain-accounts/types/keys.go Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> * renaming validation func, removing parenthesis in error msgs * renaming func validateControllerPort -> validateControllerPortParams Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> * chore: correctly set/delete active channels (#463) * correctly set active channels, implement delete OnChanCloseConfirm callback * removing active channel on packet timeout * remove ica hooks (#480) * chore: minor nits - renaming and error msgs (#464) * update ErrPortAlreadyBound error string - remove for address * rename RegisterInterchainAccount api portID -> counterpartyPortID * wrap claim capability errors in handshake * Update modules/apps/27-interchain-accounts/keeper/account.go Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> * adding channel and port id in error msg * correcting error wording Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> * chore: update portkey to include port ID (#467) * update port key to use prefix, separate key prefixes to vars * updating godoc * Update modules/apps/27-interchain-accounts/keeper/keeper.go Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> * adding todo with ica genesis issue ref * fixing failing test from browser commit * removing GetPort in favour of GetAllPorts Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> * ICA: Rename TxBody, Remove serialization logic from controller, introduce CosmosTx type (#474) * remove ICA TxBody type, use repeated Any in packet data * adjust SerializeCosmosTx, fix tests * apply self nits * add memo length validation * chore(spec): remove old specification * ica: unspecified type enum for interchain account packet data (#487) * adding unspecified type enum, adding defensive check to ValidateBasic * Update modules/apps/27-interchain-accounts/types/packet_test.go Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> * chore: ica audit nitpicks (#483) * updating version validation with corrections * removing unused methods from expected keeper interfaces * updating validate version error msg * update to use ErrInvalidVersion in favour of ErrInvalidAccountAddress * fixed typo (#507) * ica: move Serialize/DeserializeCosmosTx to package types (#493) * moving SerializeCosmosTx and DeserializeCosmosTx to types pkg * removing dead code * adding mockSdkMsg message type for failing codec test scenarios * Update modules/apps/27-interchain-accounts/types/codec_test.go * ica: TrySendTx error handling nits (#491) * updating error handling and msgs for TrySendTx flow * renaming active channel ID getter/setters, adding comment re indeterminate errs * renaming DeleteActiveChannel -> DeleteActiveChannelID * chore: adding damo to codeowners (#520) * ica: AuthenticateTx/executeTx clean up (#490) * cleaning up AuthenticateTx and executeTx to reduce unnecessary complexity * adding error wrapping to AuthenticateTx * updating err msg to include expected signer * ICA Controller Side Middleware (#417) * initial draft for ica middleware * add capability handling, fix build and tests * split module.go into ibc_module.go * add middleware handshake tests * fix app.go wiring and various tests * godoc self nits * remove unnecessary error * update comment * fix testing build * split channel keeper interface splits ChannelKeeper interface into ics4Wrapper and core ChannelKeeper * fix tests * remove comments * add callback for timeouts * Apply suggestions from code review Co-authored-by: Sean King Co-authored-by: Damian Nolan * fix test and update testing README apply test fix pointed out by Sean. Update testing README to reflect how to test with the mock module for middleware * add OnRecvPacket test Add test cases for OnRecvPacket, reused structure in relay_test.go * add failing test case for NegotiateAppVersion Co-authored-by: Sean King Co-authored-by: Damian Nolan * ica: genesis state implementation (#481) * initial genesis state draft * updating protos * including yaml tags, sorting proto file structure * updating to use range queries for active channels/interchain accounts * updating GetAllPorts test * moving test strings to expected vars * test: relay tests for TrySendTx/OnRecvPacket (#531) * adding various sdk.Msg type tests and cleaning up relay_test.go * cleaning up tests to make consistent * adding missing godoc for OnRecvPacket * adding ica test for transfertypes.MsgTranfer * updating hardcoded strings to use sdk.DefaultBondDenom * Update modules/apps/27-interchain-accounts/keeper/relay_test.go Co-authored-by: Sean King * removing staking test for insufficient funds Co-authored-by: Sean King * ICA controller/host submodules (#541) * go mod tidy * creating new genesis types for controller and host submodules * removing dead root module code * updating genesis helpers and adding newly generated types * adding interchain-accounts controller submodule * adding interchain-accounts host submodule * updating simapp to include controller and host ica submodules * adding errors returns for disallowed handshake directions, removing embedded app from host module, updating simapp to conform * updating simapp to remove nil arg to ica host ibc module * removing ics4Wrapper arg from ica host submodule * cleaning up module.go for controller and host submodules * removing commented out tests * commit with broken tests to rebase * disabling app version negotation on controller submodule * fixing tests - now passing * various cleanup, godocs and moving code * updating error msgs to conform to pkg split * removing commented out code * adding combined ica genesis, consolidating to single ica AppModule, updating app.go * adding missing godocs * clean up, godocs, rename validate.go -> version.go, move version related funcs * updating godocs and code organization * removing controller module acc, using icatypes module name for module acc in host submodule * correcting panic error msg * Update modules/apps/27-interchain-accounts/controller/ibc_module.go * Update modules/apps/27-interchain-accounts/types/genesis.go Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> * updating logger kvs, and simplifying OnRecvPacket * address nits on error strings and godocs Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> * chore: ica submodules minor improvements and cleanup * test: adding test for RegisterInterchainAccount & adding check to rel… (#552) * test: adding test for RegisterInterchainAccount & adding check to relay_test * Update modules/apps/27-interchain-accounts/host/keeper/relay_test.go Co-authored-by: Damian Nolan * Update modules/apps/27-interchain-accounts/host/keeper/relay_test.go Co-authored-by: Damian Nolan * Update modules/apps/27-interchain-accounts/host/keeper/account_test.go * Update modules/apps/27-interchain-accounts/host/keeper/account_test.go Co-authored-by: Damian Nolan * ICA Code Hygiene (#553) * chore: re-creating account.go for controller side for consitency * chore: remove comment * Update modules/apps/27-interchain-accounts/controller/keeper/account.go * ica: wrong handshake flow tests (#538) * add tests for testing wrong handshake flow Adds tests for each handshake test attempting to initialize the handshake using the wrong flow. Adds an additional portID check to OnChanOpenAck. * remove unnecessary comment * readjust tests based on new layout * Add tests provided by Damian * add tests for OnChanCloseInit and OnChanCloseConfirm on host side * add OnChanCloseInit/Confirm and NegotiateAppVersion tests to controller side * fix failing test * ica: genesis state validation (#554) * adding genesis state validation * adding genesis state validation tests * Update modules/apps/27-interchain-accounts/types/genesis_test.go Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> * Update modules/apps/27-interchain-accounts/types/genesis_test.go Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> * adding ValidateAccountAddress helper to reduce code duplication Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> * Rename imports within host/controller (#571) * alias ica types import to for host submodule * alias ica types import to for controller submodule * Add Enable/Disable controller/host on-chain params (#566) * add ica params Add new Params type to ICA. A single test is added to check defaults and validation. Usage within the ICA keepers is still needed * regenerate params proto into host and controller submodules * split params implementation into host/controller * add keeper params logic * Apply suggestions from code review Co-authored-by: Damian Nolan * add host genesis init/export params test case * add genesis validation for controller and host params Co-authored-by: Damian Nolan * Disable usage of controller and host submodules based on on-chain params (#575) * add usage of enabling/disabling controller and host submodules Adds if statement checks in controller/host ibc_module.go. Adds tests for each added if statements. Tests not added for controller ack/timeout since tests do not exist for those functions yet. * Update modules/apps/27-interchain-accounts/controller/ibc_module_test.go * add grpc query for controller and host params (#574) Adds gRPC routes for controller params and host params. Add tests and registers the gRPC gateways on the ica module * modify ica portid to be interchain-account instead of ibcaccount (#577) * feat: allowlist host param using msg typeURLs (#576) * add ica params Add new Params type to ICA. A single test is added to check defaults and validation. Usage within the ICA keepers is still needed * regenerate params proto into host and controller submodules * split params implementation into host/controller * add keeper params logic * Apply suggestions from code review Co-authored-by: Damian Nolan * add host genesis init/export params test case * updating host proto params to include msg allowlist * adding surrounds for new allowlist host param * enforcing msg is present in allowlist in AuthenticateTx, updating tests * regenerating protos post merge conflict * applying suggestinons from review * adding strings.Trimspace as suggested Co-authored-by: Colin Axnér <25233464+colin-axner@users.noreply.github.com> * Add cli query for host and controller params (#578) * add cli query commands for host/controller params * Update modules/apps/27-interchain-accounts/client/cli/cli.go * Update modules/apps/27-interchain-accounts/controller/client/cli/query.go Co-authored-by: Damian Nolan * Update modules/apps/27-interchain-accounts/host/client/cli/query.go Co-authored-by: Damian Nolan Co-authored-by: Damian Nolan * renmaing ModuleName to SubModuleName for ica controller/host (#579) Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> * add tests/code coverage for OnRecv, OnTimeout and OnAck for controller submodule (#585) Co-authored-by: Colin Axnér <25233464+colin-axner@users.noreply.github.com> Co-authored-by: Luke Rhoads <51463884+lukerhoads@users.noreply.github.com> Co-authored-by: Damian Nolan Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Aditya Co-authored-by: Aleksandr Bezobchuk Co-authored-by: Carlos Rodriguez Co-authored-by: Carlos Rodriguez Co-authored-by: Charly --- .github/CODEOWNERS | 4 +- docs/ibc/proto-docs.md | 209 +++ go.mod | 4 +- .../27-interchain-accounts/client/cli/cli.go | 26 + .../controller/client/cli/cli.go | 21 + .../controller/client/cli/query.go | 37 + .../controller/ibc_module.go | 177 ++ .../controller/ibc_module_test.go | 688 ++++++++ .../controller/keeper/account.go | 40 + .../controller/keeper/account_test.go | 72 + .../controller/keeper/genesis.go | 42 + .../controller/keeper/genesis_test.go | 66 + .../controller/keeper/grpc_query.go | 19 + .../controller/keeper/grpc_query_test.go | 14 + .../controller/keeper/handshake.go | 137 ++ .../controller/keeper/handshake_test.go | 264 +++ .../controller/keeper/keeper.go | 193 ++ .../controller/keeper/keeper_test.go | 247 +++ .../controller/keeper/params.go | 25 + .../controller/keeper/params_test.go | 15 + .../controller/keeper/relay.go | 80 + .../controller/keeper/relay_test.go | 201 +++ .../controller/types/controller.pb.go | 318 ++++ .../controller/types/errors.go | 10 + .../controller/types/keys.go | 9 + .../controller/types/params.go | 59 + .../controller/types/params_test.go | 14 + .../controller/types/query.pb.go | 547 ++++++ .../controller/types/query.pb.gw.go | 148 ++ .../host/client/cli/cli.go | 21 + .../host/client/cli/query.go | 37 + .../27-interchain-accounts/host/ibc_module.go | 151 ++ .../host/ibc_module_test.go | 644 +++++++ .../host/keeper/account.go | 26 + .../host/keeper/account_test.go | 33 + .../host/keeper/genesis.go | 40 + .../host/keeper/genesis_test.go | 65 + .../host/keeper/grpc_query.go | 19 + .../host/keeper/grpc_query_test.go | 14 + .../host/keeper/handshake.go | 137 ++ .../host/keeper/handshake_test.go | 267 +++ .../host/keeper/keeper.go | 201 +++ .../host/keeper/keeper_test.go | 229 +++ .../host/keeper/params.go | 32 + .../host/keeper/params_test.go | 16 + .../host/keeper/relay.go | 93 + .../host/keeper/relay_test.go | 449 +++++ .../host/types/errors.go | 10 + .../host/types/host.pb.go | 377 ++++ .../27-interchain-accounts/host/types/keys.go | 24 + .../host/types/params.go | 83 + .../host/types/params_test.go | 14 + .../host/types/query.pb.go | 547 ++++++ .../host/types/query.pb.gw.go | 148 ++ modules/apps/27-interchain-accounts/module.go | 182 ++ .../27-interchain-accounts/types/account.go | 128 ++ .../types/account.pb.go | 377 ++++ .../types/account_test.go | 128 ++ .../27-interchain-accounts/types/codec.go | 79 + .../types/codec_test.go | 130 ++ .../27-interchain-accounts/types/errors.go | 20 + .../types/expected_keepers.go | 37 + .../27-interchain-accounts/types/genesis.go | 139 ++ .../types/genesis.pb.go | 1548 +++++++++++++++++ .../types/genesis_test.go | 313 ++++ .../apps/27-interchain-accounts/types/keys.go | 54 + .../27-interchain-accounts/types/keys_test.go | 15 + .../27-interchain-accounts/types/packet.go | 50 + .../types/packet_test.go | 75 + .../apps/27-interchain-accounts/types/port.go | 78 + .../27-interchain-accounts/types/port_test.go | 169 ++ .../27-interchain-accounts/types/types.pb.go | 634 +++++++ .../27-interchain-accounts/types/version.go | 67 + .../types/version_test.go | 106 ++ modules/core/04-channel/types/errors.go | 2 + .../controller/v1/controller.proto | 15 + .../controller/v1/query.proto | 27 + .../interchain_accounts/host/v1/host.proto | 17 + .../interchain_accounts/host/v1/query.proto | 27 + .../interchain_accounts/v1/account.proto | 20 + .../interchain_accounts/v1/genesis.proto | 49 + .../interchain_accounts/v1/types.proto | 31 + testing/README.md | 16 +- testing/app.go | 2 +- testing/simapp/app.go | 112 +- .../proto/cosmos/auth/v1beta1/auth.proto | 51 + .../proto/cosmos/auth/v1beta1/genesis.proto | 18 + .../proto/cosmos/auth/v1beta1/query.proto | 48 + third_party/proto/cosmos_proto/cosmos.proto | 16 + 89 files changed, 12106 insertions(+), 37 deletions(-) create mode 100644 modules/apps/27-interchain-accounts/client/cli/cli.go create mode 100644 modules/apps/27-interchain-accounts/controller/client/cli/cli.go create mode 100644 modules/apps/27-interchain-accounts/controller/client/cli/query.go create mode 100644 modules/apps/27-interchain-accounts/controller/ibc_module.go create mode 100644 modules/apps/27-interchain-accounts/controller/ibc_module_test.go create mode 100644 modules/apps/27-interchain-accounts/controller/keeper/account.go create mode 100644 modules/apps/27-interchain-accounts/controller/keeper/account_test.go create mode 100644 modules/apps/27-interchain-accounts/controller/keeper/genesis.go create mode 100644 modules/apps/27-interchain-accounts/controller/keeper/genesis_test.go create mode 100644 modules/apps/27-interchain-accounts/controller/keeper/grpc_query.go create mode 100644 modules/apps/27-interchain-accounts/controller/keeper/grpc_query_test.go create mode 100644 modules/apps/27-interchain-accounts/controller/keeper/handshake.go create mode 100644 modules/apps/27-interchain-accounts/controller/keeper/handshake_test.go create mode 100644 modules/apps/27-interchain-accounts/controller/keeper/keeper.go create mode 100644 modules/apps/27-interchain-accounts/controller/keeper/keeper_test.go create mode 100644 modules/apps/27-interchain-accounts/controller/keeper/params.go create mode 100644 modules/apps/27-interchain-accounts/controller/keeper/params_test.go create mode 100644 modules/apps/27-interchain-accounts/controller/keeper/relay.go create mode 100644 modules/apps/27-interchain-accounts/controller/keeper/relay_test.go create mode 100644 modules/apps/27-interchain-accounts/controller/types/controller.pb.go create mode 100644 modules/apps/27-interchain-accounts/controller/types/errors.go create mode 100644 modules/apps/27-interchain-accounts/controller/types/keys.go create mode 100644 modules/apps/27-interchain-accounts/controller/types/params.go create mode 100644 modules/apps/27-interchain-accounts/controller/types/params_test.go create mode 100644 modules/apps/27-interchain-accounts/controller/types/query.pb.go create mode 100644 modules/apps/27-interchain-accounts/controller/types/query.pb.gw.go create mode 100644 modules/apps/27-interchain-accounts/host/client/cli/cli.go create mode 100644 modules/apps/27-interchain-accounts/host/client/cli/query.go create mode 100644 modules/apps/27-interchain-accounts/host/ibc_module.go create mode 100644 modules/apps/27-interchain-accounts/host/ibc_module_test.go create mode 100644 modules/apps/27-interchain-accounts/host/keeper/account.go create mode 100644 modules/apps/27-interchain-accounts/host/keeper/account_test.go create mode 100644 modules/apps/27-interchain-accounts/host/keeper/genesis.go create mode 100644 modules/apps/27-interchain-accounts/host/keeper/genesis_test.go create mode 100644 modules/apps/27-interchain-accounts/host/keeper/grpc_query.go create mode 100644 modules/apps/27-interchain-accounts/host/keeper/grpc_query_test.go create mode 100644 modules/apps/27-interchain-accounts/host/keeper/handshake.go create mode 100644 modules/apps/27-interchain-accounts/host/keeper/handshake_test.go create mode 100644 modules/apps/27-interchain-accounts/host/keeper/keeper.go create mode 100644 modules/apps/27-interchain-accounts/host/keeper/keeper_test.go create mode 100644 modules/apps/27-interchain-accounts/host/keeper/params.go create mode 100644 modules/apps/27-interchain-accounts/host/keeper/params_test.go create mode 100644 modules/apps/27-interchain-accounts/host/keeper/relay.go create mode 100644 modules/apps/27-interchain-accounts/host/keeper/relay_test.go create mode 100644 modules/apps/27-interchain-accounts/host/types/errors.go create mode 100644 modules/apps/27-interchain-accounts/host/types/host.pb.go create mode 100644 modules/apps/27-interchain-accounts/host/types/keys.go create mode 100644 modules/apps/27-interchain-accounts/host/types/params.go create mode 100644 modules/apps/27-interchain-accounts/host/types/params_test.go create mode 100644 modules/apps/27-interchain-accounts/host/types/query.pb.go create mode 100644 modules/apps/27-interchain-accounts/host/types/query.pb.gw.go create mode 100644 modules/apps/27-interchain-accounts/module.go create mode 100644 modules/apps/27-interchain-accounts/types/account.go create mode 100644 modules/apps/27-interchain-accounts/types/account.pb.go create mode 100644 modules/apps/27-interchain-accounts/types/account_test.go create mode 100644 modules/apps/27-interchain-accounts/types/codec.go create mode 100644 modules/apps/27-interchain-accounts/types/codec_test.go create mode 100644 modules/apps/27-interchain-accounts/types/errors.go create mode 100644 modules/apps/27-interchain-accounts/types/expected_keepers.go create mode 100644 modules/apps/27-interchain-accounts/types/genesis.go create mode 100644 modules/apps/27-interchain-accounts/types/genesis.pb.go create mode 100644 modules/apps/27-interchain-accounts/types/genesis_test.go create mode 100644 modules/apps/27-interchain-accounts/types/keys.go create mode 100644 modules/apps/27-interchain-accounts/types/keys_test.go create mode 100644 modules/apps/27-interchain-accounts/types/packet.go create mode 100644 modules/apps/27-interchain-accounts/types/packet_test.go create mode 100644 modules/apps/27-interchain-accounts/types/port.go create mode 100644 modules/apps/27-interchain-accounts/types/port_test.go create mode 100644 modules/apps/27-interchain-accounts/types/types.pb.go create mode 100644 modules/apps/27-interchain-accounts/types/version.go create mode 100644 modules/apps/27-interchain-accounts/types/version_test.go create mode 100644 proto/ibc/applications/interchain_accounts/controller/v1/controller.proto create mode 100644 proto/ibc/applications/interchain_accounts/controller/v1/query.proto create mode 100644 proto/ibc/applications/interchain_accounts/host/v1/host.proto create mode 100644 proto/ibc/applications/interchain_accounts/host/v1/query.proto create mode 100644 proto/ibc/applications/interchain_accounts/v1/account.proto create mode 100644 proto/ibc/applications/interchain_accounts/v1/genesis.proto create mode 100644 proto/ibc/applications/interchain_accounts/v1/types.proto create mode 100644 third_party/proto/cosmos/auth/v1beta1/auth.proto create mode 100644 third_party/proto/cosmos/auth/v1beta1/genesis.proto create mode 100644 third_party/proto/cosmos/auth/v1beta1/query.proto create mode 100644 third_party/proto/cosmos_proto/cosmos.proto diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 743e1c582b..2c6a4204e1 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -28,5 +28,5 @@ # CODEOWNERS for interchain-accounts module -/modules/apps/27-interchain-accounts/ @seantking @colin-axner @AdityaSripal -/proto/applications/interchain_accounts/ @seantking @colin-axner @AdityaSripal +/modules/apps/27-interchain-accounts/ @seantking @colin-axner @AdityaSripal @damiannolan +/proto/applications/interchain_accounts/ @seantking @colin-axner @AdityaSripal @damiannolan diff --git a/docs/ibc/proto-docs.md b/docs/ibc/proto-docs.md index 1eb896d016..c1eaa08166 100644 --- a/docs/ibc/proto-docs.md +++ b/docs/ibc/proto-docs.md @@ -4,6 +4,22 @@ ## Table of Contents +- [ibc/applications/interchain_accounts/v1/account.proto](#ibc/applications/interchain_accounts/v1/account.proto) + - [InterchainAccount](#ibc.applications.interchain_accounts.v1.InterchainAccount) + +- [ibc/applications/interchain_accounts/v1/genesis.proto](#ibc/applications/interchain_accounts/v1/genesis.proto) + - [ActiveChannel](#ibc.applications.interchain_accounts.v1.ActiveChannel) + - [ControllerGenesisState](#ibc.applications.interchain_accounts.v1.ControllerGenesisState) + - [GenesisState](#ibc.applications.interchain_accounts.v1.GenesisState) + - [HostGenesisState](#ibc.applications.interchain_accounts.v1.HostGenesisState) + - [RegisteredInterchainAccount](#ibc.applications.interchain_accounts.v1.RegisteredInterchainAccount) + +- [ibc/applications/interchain_accounts/v1/types.proto](#ibc/applications/interchain_accounts/v1/types.proto) + - [CosmosTx](#ibc.applications.interchain_accounts.v1.CosmosTx) + - [InterchainAccountPacketData](#ibc.applications.interchain_accounts.v1.InterchainAccountPacketData) + + - [Type](#ibc.applications.interchain_accounts.v1.Type) + - [ibc/applications/transfer/v1/transfer.proto](#ibc/applications/transfer/v1/transfer.proto) - [DenomTrace](#ibc.applications.transfer.v1.DenomTrace) - [Params](#ibc.applications.transfer.v1.Params) @@ -254,6 +270,199 @@ + +

Top

+ +## ibc/applications/interchain_accounts/v1/account.proto + + + + + +### InterchainAccount +An InterchainAccount is defined as a BaseAccount & the address of the account owner on the controller chain + + +| Field | Type | Label | Description | +| ----- | ---- | ----- | ----------- | +| `base_account` | [cosmos.auth.v1beta1.BaseAccount](#cosmos.auth.v1beta1.BaseAccount) | | | +| `account_owner` | [string](#string) | | | + + + + + + + + + + + + + + + + +

Top

+ +## ibc/applications/interchain_accounts/v1/genesis.proto + + + + + +### ActiveChannel +ActiveChannel contains a pairing of port ID and channel ID for an active interchain accounts channel + + +| Field | Type | Label | Description | +| ----- | ---- | ----- | ----------- | +| `port_id` | [string](#string) | | | +| `channel_id` | [string](#string) | | | + + + + + + + + +### ControllerGenesisState +ControllerGenesisState defines the interchain accounts controller genesis state + + +| Field | Type | Label | Description | +| ----- | ---- | ----- | ----------- | +| `active_channels` | [ActiveChannel](#ibc.applications.interchain_accounts.v1.ActiveChannel) | repeated | | +| `interchain_accounts` | [RegisteredInterchainAccount](#ibc.applications.interchain_accounts.v1.RegisteredInterchainAccount) | repeated | | +| `ports` | [string](#string) | repeated | | +| `params` | [ibc.applications.interchain_accounts.controller.v1.Params](#ibc.applications.interchain_accounts.controller.v1.Params) | | | + + + + + + + + +### GenesisState +GenesisState defines the interchain accounts genesis state + + +| Field | Type | Label | Description | +| ----- | ---- | ----- | ----------- | +| `controller_genesis_state` | [ControllerGenesisState](#ibc.applications.interchain_accounts.v1.ControllerGenesisState) | | | +| `host_genesis_state` | [HostGenesisState](#ibc.applications.interchain_accounts.v1.HostGenesisState) | | | + + + + + + + + +### HostGenesisState +HostGenesisState defines the interchain accounts host genesis state + + +| Field | Type | Label | Description | +| ----- | ---- | ----- | ----------- | +| `active_channels` | [ActiveChannel](#ibc.applications.interchain_accounts.v1.ActiveChannel) | repeated | | +| `interchain_accounts` | [RegisteredInterchainAccount](#ibc.applications.interchain_accounts.v1.RegisteredInterchainAccount) | repeated | | +| `port` | [string](#string) | | | +| `params` | [ibc.applications.interchain_accounts.host.v1.Params](#ibc.applications.interchain_accounts.host.v1.Params) | | | + + + + + + + + +### RegisteredInterchainAccount +RegisteredInterchainAccount contains a pairing of controller port ID and associated interchain account address + + +| Field | Type | Label | Description | +| ----- | ---- | ----- | ----------- | +| `port_id` | [string](#string) | | | +| `account_address` | [string](#string) | | | + + + + + + + + + + + + + + + + +

Top

+ +## ibc/applications/interchain_accounts/v1/types.proto + + + + + +### CosmosTx +CosmosTx contains a list of sdk.Msg's. It should be used when sending transactions to an SDK host chain. + + +| Field | Type | Label | Description | +| ----- | ---- | ----- | ----------- | +| `messages` | [google.protobuf.Any](#google.protobuf.Any) | repeated | | + + + + + + + + +### InterchainAccountPacketData +InterchainAccountPacketData is comprised of a raw transaction, type of transaction and optional memo field. + + +| Field | Type | Label | Description | +| ----- | ---- | ----- | ----------- | +| `type` | [Type](#ibc.applications.interchain_accounts.v1.Type) | | | +| `data` | [bytes](#bytes) | | | +| `memo` | [string](#string) | | | + + + + + + + + + + +### Type +Type defines a classification of message issued from a controller chain to its associated interchain accounts +host + +| Name | Number | Description | +| ---- | ------ | ----------- | +| TYPE_UNSPECIFIED | 0 | Default zero value enumeration | +| TYPE_EXECUTE_TX | 1 | Execute a transaction on an interchain accounts host chain | + + + + + + + + + +

Top

diff --git a/go.mod b/go.mod index 81ae1d022b..2f60220f62 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/grpc-ecosystem/grpc-gateway v1.16.0 github.com/pkg/errors v0.9.1 github.com/rakyll/statik v0.1.7 + github.com/regen-network/cosmos-proto v0.3.1 github.com/spf13/cast v1.4.1 github.com/spf13/cobra v1.2.1 github.com/spf13/viper v1.9.0 @@ -23,6 +24,7 @@ require ( google.golang.org/genproto v0.0.0-20210828152312-66f60bf46e71 google.golang.org/grpc v1.42.0 google.golang.org/protobuf v1.27.1 + gopkg.in/yaml.v2 v2.4.0 ) require ( @@ -92,7 +94,6 @@ require ( github.com/prometheus/common v0.29.0 // indirect github.com/prometheus/procfs v0.6.0 // indirect github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0 // indirect - github.com/regen-network/cosmos-proto v0.3.1 // indirect github.com/rs/cors v1.7.0 // indirect github.com/rs/zerolog v1.23.0 // indirect github.com/sasha-s/go-deadlock v0.2.1-0.20190427202633-1595213edefa // indirect @@ -113,7 +114,6 @@ require ( golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 // indirect golang.org/x/text v0.3.6 // indirect gopkg.in/ini.v1 v1.63.2 // indirect - gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect nhooyr.io/websocket v1.8.6 // indirect ) diff --git a/modules/apps/27-interchain-accounts/client/cli/cli.go b/modules/apps/27-interchain-accounts/client/cli/cli.go new file mode 100644 index 0000000000..25a89600be --- /dev/null +++ b/modules/apps/27-interchain-accounts/client/cli/cli.go @@ -0,0 +1,26 @@ +package cli + +import ( + "github.com/spf13/cobra" + + controllercli "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/controller/client/cli" + hostcli "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/host/client/cli" +) + +// GetQueryCmd returns the query commands for the interchain-accounts submodule +func GetQueryCmd() *cobra.Command { + icaQueryCmd := &cobra.Command{ + Use: "interchain-accounts", + Aliases: []string{"ica"}, + Short: "interchain-accounts subcommands", + DisableFlagParsing: true, + SuggestionsMinimumDistance: 2, + } + + icaQueryCmd.AddCommand( + controllercli.GetQueryCmd(), + hostcli.GetQueryCmd(), + ) + + return icaQueryCmd +} diff --git a/modules/apps/27-interchain-accounts/controller/client/cli/cli.go b/modules/apps/27-interchain-accounts/controller/client/cli/cli.go new file mode 100644 index 0000000000..0d2f54bd59 --- /dev/null +++ b/modules/apps/27-interchain-accounts/controller/client/cli/cli.go @@ -0,0 +1,21 @@ +package cli + +import ( + "github.com/spf13/cobra" +) + +// GetQueryCmd returns the query commands for the ICA controller submodule +func GetQueryCmd() *cobra.Command { + queryCmd := &cobra.Command{ + Use: "controller", + Short: "interchain-accounts controller subcommands", + DisableFlagParsing: true, + SuggestionsMinimumDistance: 2, + } + + queryCmd.AddCommand( + GetCmdParams(), + ) + + return queryCmd +} diff --git a/modules/apps/27-interchain-accounts/controller/client/cli/query.go b/modules/apps/27-interchain-accounts/controller/client/cli/query.go new file mode 100644 index 0000000000..18c007ea2e --- /dev/null +++ b/modules/apps/27-interchain-accounts/controller/client/cli/query.go @@ -0,0 +1,37 @@ +package cli + +import ( + "fmt" + + "github.com/spf13/cobra" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/version" + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/controller/types" +) + +// GetCmdParams returns the command handler for the controller submodule parameter querying. +func GetCmdParams() *cobra.Command { + cmd := &cobra.Command{ + Use: "params", + Short: "Query the current interchain-accounts controller submodule parameters", + Long: "Query the current interchain-accounts controller submodule parameters", + Args: cobra.NoArgs, + Example: fmt.Sprintf("%s query interchain-accounts controller params", version.AppName), + RunE: func(cmd *cobra.Command, _ []string) error { + clientCtx, err := client.GetClientQueryContext(cmd) + if err != nil { + return err + } + queryClient := types.NewQueryClient(clientCtx) + + res, _ := queryClient.Params(cmd.Context(), &types.QueryParamsRequest{}) + return clientCtx.PrintProto(res.Params) + }, + } + + flags.AddQueryFlagsToCmd(cmd) + + return cmd +} diff --git a/modules/apps/27-interchain-accounts/controller/ibc_module.go b/modules/apps/27-interchain-accounts/controller/ibc_module.go new file mode 100644 index 0000000000..897191eca0 --- /dev/null +++ b/modules/apps/27-interchain-accounts/controller/ibc_module.go @@ -0,0 +1,177 @@ +package controller + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" + + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/controller/keeper" + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/controller/types" + icatypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" + channeltypes "github.com/cosmos/ibc-go/v2/modules/core/04-channel/types" + porttypes "github.com/cosmos/ibc-go/v2/modules/core/05-port/types" + ibcexported "github.com/cosmos/ibc-go/v2/modules/core/exported" +) + +// IBCModule implements the ICS26 interface for interchain accounts controller chains +type IBCModule struct { + keeper keeper.Keeper + app porttypes.IBCModule +} + +// NewIBCModule creates a new IBCModule given the associated keeper and underlying application +func NewIBCModule(k keeper.Keeper, app porttypes.IBCModule) IBCModule { + return IBCModule{ + keeper: k, + app: app, + } +} + +// OnChanOpenInit implements the IBCModule interface +// +// Interchain Accounts is implemented to act as middleware for connected authentication modules on +// the controller side. The connected modules may not change the controller side portID or +// version. They will be allowed to perform custom logic without changing +// the parameters stored within a channel struct. +func (im IBCModule) OnChanOpenInit( + ctx sdk.Context, + order channeltypes.Order, + connectionHops []string, + portID string, + channelID string, + chanCap *capabilitytypes.Capability, + counterparty channeltypes.Counterparty, + version string, +) error { + if !im.keeper.IsControllerEnabled(ctx) { + return types.ErrControllerSubModuleDisabled + } + + if err := im.keeper.OnChanOpenInit(ctx, order, connectionHops, portID, channelID, chanCap, counterparty, version); err != nil { + return err + } + + // call underlying app's OnChanOpenInit callback with the appVersion + return im.app.OnChanOpenInit(ctx, order, connectionHops, portID, channelID, + chanCap, counterparty, version) +} + +// OnChanOpenTry implements the IBCModule interface +func (im IBCModule) OnChanOpenTry( + ctx sdk.Context, + order channeltypes.Order, + connectionHops []string, + portID, + channelID string, + chanCap *capabilitytypes.Capability, + counterparty channeltypes.Counterparty, + version, + counterpartyVersion string, +) error { + return sdkerrors.Wrap(icatypes.ErrInvalidChannelFlow, "channel handshake must be initiated by controller chain") +} + +// OnChanOpenAck implements the IBCModule interface +// +// Interchain Accounts is implemented to act as middleware for connected authentication modules on +// the controller side. The connected modules may not change the portID or +// version. They will be allowed to perform custom logic without changing +// the parameters stored within a channel struct. +func (im IBCModule) OnChanOpenAck( + ctx sdk.Context, + portID, + channelID string, + counterpartyVersion string, +) error { + if !im.keeper.IsControllerEnabled(ctx) { + return types.ErrControllerSubModuleDisabled + } + + if err := im.keeper.OnChanOpenAck(ctx, portID, channelID, counterpartyVersion); err != nil { + return err + } + + // call underlying app's OnChanOpenAck callback with the counterparty app version. + return im.app.OnChanOpenAck(ctx, portID, channelID, counterpartyVersion) +} + +// OnChanOpenAck implements the IBCModule interface +func (im IBCModule) OnChanOpenConfirm( + ctx sdk.Context, + portID, + channelID string, +) error { + return sdkerrors.Wrap(icatypes.ErrInvalidChannelFlow, "channel handshake must be initiated by controller chain") +} + +// OnChanCloseInit implements the IBCModule interface +func (im IBCModule) OnChanCloseInit( + ctx sdk.Context, + portID, + channelID string, +) error { + // Disallow user-initiated channel closing for interchain account channels + return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "user cannot close channel") +} + +// OnChanCloseConfirm implements the IBCModule interface +func (im IBCModule) OnChanCloseConfirm( + ctx sdk.Context, + portID, + channelID string, +) error { + return im.keeper.OnChanCloseConfirm(ctx, portID, channelID) +} + +// OnRecvPacket implements the IBCModule interface +func (im IBCModule) OnRecvPacket( + ctx sdk.Context, + packet channeltypes.Packet, + _ sdk.AccAddress, +) ibcexported.Acknowledgement { + return channeltypes.NewErrorAcknowledgement("cannot receive packet on controller chain") +} + +// OnAcknowledgementPacket implements the IBCModule interface +func (im IBCModule) OnAcknowledgementPacket( + ctx sdk.Context, + packet channeltypes.Packet, + acknowledgement []byte, + relayer sdk.AccAddress, +) error { + if !im.keeper.IsControllerEnabled(ctx) { + return types.ErrControllerSubModuleDisabled + } + + // call underlying app's OnAcknowledgementPacket callback. + return im.app.OnAcknowledgementPacket(ctx, packet, acknowledgement, relayer) +} + +// OnTimeoutPacket implements the IBCModule interface +func (im IBCModule) OnTimeoutPacket( + ctx sdk.Context, + packet channeltypes.Packet, + relayer sdk.AccAddress, +) error { + if !im.keeper.IsControllerEnabled(ctx) { + return types.ErrControllerSubModuleDisabled + } + + if err := im.keeper.OnTimeoutPacket(ctx, packet); err != nil { + return err + } + + return im.app.OnTimeoutPacket(ctx, packet, relayer) +} + +// NegotiateAppVersion implements the IBCModule interface +func (im IBCModule) NegotiateAppVersion( + ctx sdk.Context, + order channeltypes.Order, + connectionID string, + portID string, + counterparty channeltypes.Counterparty, + proposedVersion string, +) (string, error) { + return "", sdkerrors.Wrap(icatypes.ErrInvalidChannelFlow, "ICS-27 app version negotiation is unsupported on controller chains") +} diff --git a/modules/apps/27-interchain-accounts/controller/ibc_module_test.go b/modules/apps/27-interchain-accounts/controller/ibc_module_test.go new file mode 100644 index 0000000000..0cba3a54ea --- /dev/null +++ b/modules/apps/27-interchain-accounts/controller/ibc_module_test.go @@ -0,0 +1,688 @@ +package controller_test + +import ( + "fmt" + "testing" + + sdk "github.com/cosmos/cosmos-sdk/types" + capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" + "github.com/stretchr/testify/suite" + "github.com/tendermint/tendermint/crypto" + + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/controller/types" + icatypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" + clienttypes "github.com/cosmos/ibc-go/v2/modules/core/02-client/types" + channeltypes "github.com/cosmos/ibc-go/v2/modules/core/04-channel/types" + host "github.com/cosmos/ibc-go/v2/modules/core/24-host" + ibctesting "github.com/cosmos/ibc-go/v2/testing" +) + +var ( + // TestAccAddress defines a resuable bech32 address for testing purposes + // TODO: update crypto.AddressHash() when sdk uses address.Module() + TestAccAddress = icatypes.GenerateAddress(sdk.AccAddress(crypto.AddressHash([]byte(icatypes.ModuleName))), TestPortID) + // TestOwnerAddress defines a reusable bech32 address for testing purposes + TestOwnerAddress = "cosmos17dtl0mjt3t77kpuhg2edqzjpszulwhgzuj9ljs" + // TestPortID defines a resuable port identifier for testing purposes + TestPortID, _ = icatypes.GeneratePortID(TestOwnerAddress, ibctesting.FirstConnectionID, ibctesting.FirstConnectionID) + // TestVersion defines a resuable interchainaccounts version string for testing purposes + TestVersion = icatypes.NewAppVersion(icatypes.VersionPrefix, TestAccAddress.String()) +) + +type InterchainAccountsTestSuite struct { + suite.Suite + + coordinator *ibctesting.Coordinator + + // testing chains used for convenience and readability + chainA *ibctesting.TestChain + chainB *ibctesting.TestChain +} + +func TestICATestSuite(t *testing.T) { + suite.Run(t, new(InterchainAccountsTestSuite)) +} + +func (suite *InterchainAccountsTestSuite) SetupTest() { + suite.coordinator = ibctesting.NewCoordinator(suite.T(), 2) + suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(0)) + suite.chainB = suite.coordinator.GetChain(ibctesting.GetChainID(1)) +} + +func NewICAPath(chainA, chainB *ibctesting.TestChain) *ibctesting.Path { + path := ibctesting.NewPath(chainA, chainB) + path.EndpointA.ChannelConfig.PortID = icatypes.PortID + path.EndpointB.ChannelConfig.PortID = icatypes.PortID + path.EndpointA.ChannelConfig.Order = channeltypes.ORDERED + path.EndpointB.ChannelConfig.Order = channeltypes.ORDERED + path.EndpointA.ChannelConfig.Version = icatypes.VersionPrefix + path.EndpointB.ChannelConfig.Version = TestVersion + + return path +} + +func InitInterchainAccount(endpoint *ibctesting.Endpoint, owner string) error { + portID, err := icatypes.GeneratePortID(owner, endpoint.ConnectionID, endpoint.Counterparty.ConnectionID) + if err != nil { + return err + } + + channelSequence := endpoint.Chain.App.GetIBCKeeper().ChannelKeeper.GetNextChannelSequence(endpoint.Chain.GetContext()) + + if err := endpoint.Chain.GetSimApp().ICAControllerKeeper.InitInterchainAccount(endpoint.Chain.GetContext(), endpoint.ConnectionID, endpoint.Counterparty.ConnectionID, owner); err != nil { + return err + } + + // commit state changes for proof verification + endpoint.Chain.App.Commit() + endpoint.Chain.NextBlock() + + // update port/channel ids + endpoint.ChannelID = channeltypes.FormatChannelIdentifier(channelSequence) + endpoint.ChannelConfig.PortID = portID + + return nil +} + +// SetupICAPath invokes the InterchainAccounts entrypoint and subsequent channel handshake handlers +func SetupICAPath(path *ibctesting.Path, owner string) error { + if err := InitInterchainAccount(path.EndpointA, owner); err != nil { + return err + } + + if err := path.EndpointB.ChanOpenTry(); err != nil { + return err + } + + if err := path.EndpointA.ChanOpenAck(); err != nil { + return err + } + + if err := path.EndpointB.ChanOpenConfirm(); err != nil { + return err + } + + return nil +} + +func (suite *InterchainAccountsTestSuite) TestOnChanOpenInit() { + var ( + channel *channeltypes.Channel + ) + + testCases := []struct { + name string + malleate func() + expPass bool + }{ + { + "success", func() {}, true, + }, + { + "controller submodule disabled", func() { + suite.chainA.GetSimApp().ICAControllerKeeper.SetParams(suite.chainA.GetContext(), types.NewParams(false)) + }, false, + }, + { + "ICA OnChanOpenInit fails - UNORDERED channel", func() { + channel.Ordering = channeltypes.UNORDERED + }, false, + }, + { + "ICA auth module callback fails", func() { + suite.chainA.GetSimApp().ICAAuthModule.IBCApp.OnChanOpenInit = func(ctx sdk.Context, order channeltypes.Order, connectionHops []string, + portID, channelID string, chanCap *capabilitytypes.Capability, + counterparty channeltypes.Counterparty, version string, + ) error { + return fmt.Errorf("mock ica auth fails") + } + }, false, + }, + } + + for _, tc := range testCases { + tc := tc + + suite.Run(tc.name, func() { + suite.SetupTest() // reset + + path := NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + // mock init interchain account + portID, err := icatypes.GeneratePortID(TestOwnerAddress, path.EndpointA.ConnectionID, path.EndpointB.ConnectionID) + suite.Require().NoError(err) + + portCap := suite.chainA.GetSimApp().IBCKeeper.PortKeeper.BindPort(suite.chainA.GetContext(), portID) + suite.chainA.GetSimApp().ICAControllerKeeper.ClaimCapability(suite.chainA.GetContext(), portCap, host.PortPath(portID)) + + path.EndpointA.ChannelConfig.PortID = portID + path.EndpointA.ChannelID = ibctesting.FirstChannelID + + // default values + counterparty := channeltypes.NewCounterparty(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) + channel = &channeltypes.Channel{ + State: channeltypes.INIT, + Ordering: channeltypes.ORDERED, + Counterparty: counterparty, + ConnectionHops: []string{path.EndpointA.ConnectionID}, + Version: icatypes.VersionPrefix, + } + + tc.malleate() // malleate mutates test data + + // ensure channel on chainA is set in state + suite.chainA.GetSimApp().IBCKeeper.ChannelKeeper.SetChannel(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, *channel) + + module, _, err := suite.chainA.App.GetIBCKeeper().PortKeeper.LookupModuleByPort(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID) + suite.Require().NoError(err) + + chanCap, err := suite.chainA.App.GetScopedIBCKeeper().NewCapability(suite.chainA.GetContext(), host.ChannelCapabilityPath(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)) + suite.Require().NoError(err) + + cbs, ok := suite.chainA.App.GetIBCKeeper().Router.GetRoute(module) + suite.Require().True(ok) + + err = cbs.OnChanOpenInit(suite.chainA.GetContext(), channel.Ordering, channel.GetConnectionHops(), + path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, chanCap, channel.Counterparty, channel.GetVersion(), + ) + + if tc.expPass { + suite.Require().NoError(err) + } else { + suite.Require().Error(err) + } + }) + } +} + +// Test initiating a ChanOpenTry using the controller chain instead of the host chain +// ChainA is the controller chain. ChainB creates a controller port as well, +// attempting to trick chainA. +// Sending a MsgChanOpenTry will never reach the application callback due to +// core IBC checks not passing, so a call to the application callback is also +// done directly. +func (suite *InterchainAccountsTestSuite) TestChanOpenTry() { + suite.SetupTest() // reset + path := NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := InitInterchainAccount(path.EndpointA, TestOwnerAddress) + suite.Require().NoError(err) + + // chainB also creates a controller port + err = InitInterchainAccount(path.EndpointB, TestOwnerAddress) + suite.Require().NoError(err) + + path.EndpointA.UpdateClient() + channelKey := host.ChannelKey(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) + proofInit, proofHeight := path.EndpointB.Chain.QueryProof(channelKey) + + // use chainA (controller) for ChanOpenTry + msg := channeltypes.NewMsgChannelOpenTry(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, TestVersion, channeltypes.ORDERED, []string{path.EndpointA.ConnectionID}, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, icatypes.VersionPrefix, proofInit, proofHeight, icatypes.ModuleName) + handler := suite.chainA.GetSimApp().MsgServiceRouter().Handler(msg) + _, err = handler(suite.chainA.GetContext(), msg) + + suite.Require().Error(err) + + // call application callback directly + module, _, err := suite.chainA.App.GetIBCKeeper().PortKeeper.LookupModuleByPort(suite.chainA.GetContext(), path.EndpointB.ChannelConfig.PortID) + suite.Require().NoError(err) + + cbs, ok := suite.chainA.App.GetIBCKeeper().Router.GetRoute(module) + suite.Require().True(ok) + + counterparty := channeltypes.NewCounterparty(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) + chanCap, found := suite.chainA.App.GetScopedIBCKeeper().GetCapability(suite.chainA.GetContext(), host.ChannelCapabilityPath(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)) + suite.Require().True(found) + + err = cbs.OnChanOpenTry( + suite.chainA.GetContext(), path.EndpointA.ChannelConfig.Order, []string{path.EndpointA.ConnectionID}, + path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, chanCap, + counterparty, path.EndpointA.ChannelConfig.Version, path.EndpointB.ChannelConfig.Version, + ) + suite.Require().Error(err) +} + +func (suite *InterchainAccountsTestSuite) TestOnChanOpenAck() { + var ( + path *ibctesting.Path + ) + + testCases := []struct { + name string + malleate func() + expPass bool + }{ + { + "success", func() {}, true, + }, + { + "controller submodule disabled", func() { + suite.chainA.GetSimApp().ICAControllerKeeper.SetParams(suite.chainA.GetContext(), types.NewParams(false)) + }, false, + }, + { + "ICA OnChanOpenACK fails - invalid version", func() { + path.EndpointB.ChannelConfig.Version = "invalid|version" + }, false, + }, + { + "ICA auth module callback fails", func() { + suite.chainA.GetSimApp().ICAAuthModule.IBCApp.OnChanOpenAck = func( + ctx sdk.Context, portID, channelID string, counterpartyVersion string, + ) error { + return fmt.Errorf("mock ica auth fails") + } + }, false, + }, + } + + for _, tc := range testCases { + tc := tc + + suite.Run(tc.name, func() { + suite.SetupTest() // reset + + path = NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := InitInterchainAccount(path.EndpointA, TestOwnerAddress) + suite.Require().NoError(err) + + err = path.EndpointB.ChanOpenTry() + suite.Require().NoError(err) + + tc.malleate() // malleate mutates test data + + module, _, err := suite.chainA.App.GetIBCKeeper().PortKeeper.LookupModuleByPort(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID) + suite.Require().NoError(err) + + cbs, ok := suite.chainA.App.GetIBCKeeper().Router.GetRoute(module) + suite.Require().True(ok) + + err = cbs.OnChanOpenAck(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.Version) + + if tc.expPass { + suite.Require().NoError(err) + } else { + suite.Require().Error(err) + } + + }) + } + +} + +// Test initiating a ChanOpenConfirm using the controller chain instead of the host chain +// ChainA is the controller chain. ChainB is the host chain +// Sending a MsgChanOpenConfirm will never reach the application callback due to +// core IBC checks not passing, so a call to the application callback is also +// done directly. +func (suite *InterchainAccountsTestSuite) TestChanOpenConfirm() { + suite.SetupTest() // reset + path := NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := InitInterchainAccount(path.EndpointA, TestOwnerAddress) + suite.Require().NoError(err) + + err = path.EndpointB.ChanOpenTry() + suite.Require().NoError(err) + + // chainB maliciously sets channel to OPEN + channel := channeltypes.NewChannel(channeltypes.OPEN, channeltypes.ORDERED, channeltypes.NewCounterparty(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID), []string{path.EndpointB.ConnectionID}, TestVersion) + suite.chainB.GetSimApp().GetIBCKeeper().ChannelKeeper.SetChannel(suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, channel) + + // commit state changes so proof can be created + suite.chainB.App.Commit() + suite.chainB.NextBlock() + + path.EndpointA.UpdateClient() + + // query proof from ChainB + channelKey := host.ChannelKey(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) + proofAck, proofHeight := path.EndpointB.Chain.QueryProof(channelKey) + + // use chainA (controller) for ChanOpenConfirm + msg := channeltypes.NewMsgChannelOpenConfirm(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, proofAck, proofHeight, icatypes.ModuleName) + handler := suite.chainA.GetSimApp().MsgServiceRouter().Handler(msg) + _, err = handler(suite.chainA.GetContext(), msg) + + suite.Require().Error(err) + + // call application callback directly + module, _, err := suite.chainA.App.GetIBCKeeper().PortKeeper.LookupModuleByPort(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID) + suite.Require().NoError(err) + + cbs, ok := suite.chainA.App.GetIBCKeeper().Router.GetRoute(module) + suite.Require().True(ok) + + err = cbs.OnChanOpenConfirm( + suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, + ) + suite.Require().Error(err) + +} + +// OnChanCloseInit on controller (chainA) +func (suite *InterchainAccountsTestSuite) TestOnChanCloseInit() { + path := NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := SetupICAPath(path, TestOwnerAddress) + suite.Require().NoError(err) + + module, _, err := suite.chainA.App.GetIBCKeeper().PortKeeper.LookupModuleByPort(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID) + suite.Require().NoError(err) + + cbs, ok := suite.chainA.App.GetIBCKeeper().Router.GetRoute(module) + suite.Require().True(ok) + + err = cbs.OnChanCloseInit( + suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, + ) + + suite.Require().Error(err) +} + +func (suite *InterchainAccountsTestSuite) TestOnChanCloseConfirm() { + var ( + path *ibctesting.Path + ) + + testCases := []struct { + name string + malleate func() + expPass bool + }{ + + { + "success", func() {}, true, + }, + } + + for _, tc := range testCases { + suite.Run(tc.name, func() { + suite.SetupTest() // reset + + path = NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := SetupICAPath(path, TestOwnerAddress) + suite.Require().NoError(err) + + tc.malleate() // malleate mutates test data + module, _, err := suite.chainA.App.GetIBCKeeper().PortKeeper.LookupModuleByPort(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID) + suite.Require().NoError(err) + + cbs, ok := suite.chainA.App.GetIBCKeeper().Router.GetRoute(module) + suite.Require().True(ok) + + err = cbs.OnChanCloseConfirm( + suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) + + activeChannelID, found := suite.chainA.GetSimApp().ICAHostKeeper.GetActiveChannelID(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID) + + if tc.expPass { + suite.Require().NoError(err) + suite.Require().False(found) + suite.Require().Empty(activeChannelID) + } else { + suite.Require().Error(err) + } + + }) + } +} + +func (suite *InterchainAccountsTestSuite) TestOnRecvPacket() { + + testCases := []struct { + name string + malleate func() + expPass bool + }{ + { + "ICA OnRecvPacket fails with ErrInvalidChannelFlow", func() {}, false, + }, + } + + for _, tc := range testCases { + tc := tc + + suite.Run(tc.name, func() { + suite.SetupTest() // reset + + path := NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := SetupICAPath(path, TestOwnerAddress) + suite.Require().NoError(err) + + tc.malleate() // malleate mutates test data + + module, _, err := suite.chainA.App.GetIBCKeeper().PortKeeper.LookupModuleByPort(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID) + suite.Require().NoError(err) + + cbs, ok := suite.chainA.App.GetIBCKeeper().Router.GetRoute(module) + suite.Require().True(ok) + + packet := channeltypes.NewPacket( + []byte("empty packet data"), + suite.chainB.SenderAccount.GetSequence(), + path.EndpointB.ChannelConfig.PortID, + path.EndpointB.ChannelID, + path.EndpointA.ChannelConfig.PortID, + path.EndpointA.ChannelID, + clienttypes.NewHeight(0, 100), + 0, + ) + + ack := cbs.OnRecvPacket(suite.chainA.GetContext(), packet, TestAccAddress) + suite.Require().Equal(tc.expPass, ack.Success()) + }) + } +} + +func (suite *InterchainAccountsTestSuite) TestOnAcknowledgementPacket() { + var ( + path *ibctesting.Path + ) + + testCases := []struct { + msg string + malleate func() + expPass bool + }{ + { + "success", + func() {}, + true, + }, + { + "controller submodule disabled", func() { + suite.chainA.GetSimApp().ICAControllerKeeper.SetParams(suite.chainA.GetContext(), types.NewParams(false)) + }, false, + }, + { + "ICA auth module callback fails", func() { + suite.chainA.GetSimApp().ICAAuthModule.IBCApp.OnAcknowledgementPacket = func( + ctx sdk.Context, packet channeltypes.Packet, acknowledgement []byte, relayer sdk.AccAddress, + ) error { + return fmt.Errorf("mock ica auth fails") + } + }, false, + }, + } + + for _, tc := range testCases { + suite.Run(tc.msg, func() { + suite.SetupTest() // reset + + path = NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := SetupICAPath(path, TestOwnerAddress) + suite.Require().NoError(err) + + packet := channeltypes.NewPacket( + []byte("empty packet data"), + suite.chainA.SenderAccount.GetSequence(), + path.EndpointA.ChannelConfig.PortID, + path.EndpointA.ChannelID, + path.EndpointB.ChannelConfig.PortID, + path.EndpointB.ChannelID, + clienttypes.NewHeight(0, 100), + 0, + ) + + tc.malleate() // malleate mutates test data + + module, _, err := suite.chainA.App.GetIBCKeeper().PortKeeper.LookupModuleByPort(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID) + suite.Require().NoError(err) + + cbs, ok := suite.chainA.App.GetIBCKeeper().Router.GetRoute(module) + suite.Require().True(ok) + + err = cbs.OnAcknowledgementPacket(suite.chainA.GetContext(), packet, []byte("ack"), nil) + + if tc.expPass { + suite.Require().NoError(err) + } else { + suite.Require().Error(err) + } + }) + } +} + +func (suite *InterchainAccountsTestSuite) TestOnTimeoutPacket() { + var ( + path *ibctesting.Path + ) + + testCases := []struct { + msg string + malleate func() + expPass bool + }{ + { + "success", + func() {}, + true, + }, + { + "controller submodule disabled", func() { + suite.chainA.GetSimApp().ICAControllerKeeper.SetParams(suite.chainA.GetContext(), types.NewParams(false)) + }, false, + }, + { + "ICA auth module callback fails", func() { + suite.chainA.GetSimApp().ICAAuthModule.IBCApp.OnTimeoutPacket = func( + ctx sdk.Context, packet channeltypes.Packet, relayer sdk.AccAddress, + ) error { + return fmt.Errorf("mock ica auth fails") + } + }, false, + }, + } + + for _, tc := range testCases { + suite.Run(tc.msg, func() { + suite.SetupTest() // reset + + path = NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := SetupICAPath(path, TestOwnerAddress) + suite.Require().NoError(err) + + packet := channeltypes.NewPacket( + []byte("empty packet data"), + suite.chainA.SenderAccount.GetSequence(), + path.EndpointA.ChannelConfig.PortID, + path.EndpointA.ChannelID, + path.EndpointB.ChannelConfig.PortID, + path.EndpointB.ChannelID, + clienttypes.NewHeight(0, 100), + 0, + ) + + tc.malleate() // malleate mutates test data + + module, _, err := suite.chainA.App.GetIBCKeeper().PortKeeper.LookupModuleByPort(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID) + suite.Require().NoError(err) + + cbs, ok := suite.chainA.App.GetIBCKeeper().Router.GetRoute(module) + suite.Require().True(ok) + + err = cbs.OnTimeoutPacket(suite.chainA.GetContext(), packet, nil) + + activeChannelID, found := suite.chainA.GetSimApp().ICAControllerKeeper.GetActiveChannelID(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID) + + if tc.expPass { + suite.Require().NoError(err) + suite.Require().Empty(activeChannelID) + suite.Require().False(found) + } else { + suite.Require().Error(err) + } + }) + } +} + +func (suite *InterchainAccountsTestSuite) TestNegotiateAppVersion() { + var ( + proposedVersion string + ) + testCases := []struct { + name string + malleate func() + expPass bool + }{ + { + "ICA OnRecvPacket fails with ErrInvalidChannelFlow", func() {}, false, + }, + } + + for _, tc := range testCases { + tc := tc + + suite.Run(tc.name, func() { + suite.SetupTest() + path := NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := InitInterchainAccount(path.EndpointA, TestOwnerAddress) + suite.Require().NoError(err) + + module, _, err := suite.chainA.GetSimApp().GetIBCKeeper().PortKeeper.LookupModuleByPort(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID) + suite.Require().NoError(err) + + cbs, ok := suite.chainA.GetSimApp().GetIBCKeeper().Router.GetRoute(module) + suite.Require().True(ok) + + counterpartyPortID, err := icatypes.GeneratePortID(TestOwnerAddress, path.EndpointA.ConnectionID, path.EndpointB.ConnectionID) + suite.Require().NoError(err) + + counterparty := channeltypes.Counterparty{ + PortId: counterpartyPortID, + ChannelId: path.EndpointB.ChannelID, + } + + proposedVersion = icatypes.VersionPrefix + + tc.malleate() + + version, err := cbs.NegotiateAppVersion(suite.chainA.GetContext(), channeltypes.ORDERED, path.EndpointA.ConnectionID, path.EndpointA.ChannelConfig.PortID, counterparty, proposedVersion) + if tc.expPass { + suite.Require().NoError(err) + suite.Require().NoError(icatypes.ValidateVersion(version)) + suite.Require().Equal(TestVersion, version) + } else { + suite.Require().Error(err) + suite.Require().Empty(version) + } + }) + } +} diff --git a/modules/apps/27-interchain-accounts/controller/keeper/account.go b/modules/apps/27-interchain-accounts/controller/keeper/account.go new file mode 100644 index 0000000000..8876b3467b --- /dev/null +++ b/modules/apps/27-interchain-accounts/controller/keeper/account.go @@ -0,0 +1,40 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + + icatypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" + channeltypes "github.com/cosmos/ibc-go/v2/modules/core/04-channel/types" + host "github.com/cosmos/ibc-go/v2/modules/core/24-host" +) + +// InitInterchainAccount is the entry point to registering an interchain account. +// It generates a new port identifier using the owner address, connection identifier, +// and counterparty connection identifier. It will bind to the port identifier and +// call 04-channel 'ChanOpenInit'. An error is returned if the port identifier is +// already in use. Gaining access to interchain accounts whose channels have closed +// cannot be done with this function. A regular MsgChanOpenInit must be used. +func (k Keeper) InitInterchainAccount(ctx sdk.Context, connectionID, counterpartyConnectionID, owner string) error { + portID, err := icatypes.GeneratePortID(owner, connectionID, counterpartyConnectionID) + if err != nil { + return err + } + + if k.portKeeper.IsBound(ctx, portID) { + return sdkerrors.Wrap(icatypes.ErrPortAlreadyBound, portID) + } + + cap := k.BindPort(ctx, portID) + if err := k.ClaimCapability(ctx, cap, host.PortPath(portID)); err != nil { + return sdkerrors.Wrap(err, "unable to bind to newly generated portID") + } + + msg := channeltypes.NewMsgChannelOpenInit(portID, icatypes.VersionPrefix, channeltypes.ORDERED, []string{connectionID}, icatypes.PortID, icatypes.ModuleName) + handler := k.msgRouter.Handler(msg) + if _, err := handler(ctx, msg); err != nil { + return err + } + + return nil +} diff --git a/modules/apps/27-interchain-accounts/controller/keeper/account_test.go b/modules/apps/27-interchain-accounts/controller/keeper/account_test.go new file mode 100644 index 0000000000..3a019deac9 --- /dev/null +++ b/modules/apps/27-interchain-accounts/controller/keeper/account_test.go @@ -0,0 +1,72 @@ +package keeper_test + +import ( + icatypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" + ibctesting "github.com/cosmos/ibc-go/v2/testing" +) + +func (suite *KeeperTestSuite) TestInitInterchainAccount() { + var ( + owner string + path *ibctesting.Path + err error + ) + + testCases := []struct { + name string + malleate func() + expPass bool + }{ + { + "success", func() {}, true, + }, + { + "port is already bound", + func() { + suite.chainA.GetSimApp().IBCKeeper.PortKeeper.BindPort(suite.chainA.GetContext(), TestPortID) + }, + false, + }, + { + "fails to generate port-id", + func() { + owner = "" + }, + false, + }, + { + "MsgChanOpenInit fails - channel is already active", + func() { + portID, err := icatypes.GeneratePortID(owner, path.EndpointA.ConnectionID, path.EndpointB.ConnectionID) + suite.Require().NoError(err) + + suite.chainA.GetSimApp().ICAControllerKeeper.SetActiveChannelID(suite.chainA.GetContext(), portID, path.EndpointA.ChannelID) + }, + false, + }, + } + + for _, tc := range testCases { + tc := tc + + suite.Run(tc.name, func() { + suite.SetupTest() + + owner = TestOwnerAddress // must be explicitly changed + + path = NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + tc.malleate() // malleate mutates test data + + err = suite.chainA.GetSimApp().ICAControllerKeeper.InitInterchainAccount(suite.chainA.GetContext(), path.EndpointA.ConnectionID, path.EndpointB.ConnectionID, owner) + + if tc.expPass { + suite.Require().NoError(err) + } else { + suite.Require().Error(err) + } + + }) + } +} diff --git a/modules/apps/27-interchain-accounts/controller/keeper/genesis.go b/modules/apps/27-interchain-accounts/controller/keeper/genesis.go new file mode 100644 index 0000000000..19bcb1de6c --- /dev/null +++ b/modules/apps/27-interchain-accounts/controller/keeper/genesis.go @@ -0,0 +1,42 @@ +package keeper + +import ( + "fmt" + + sdk "github.com/cosmos/cosmos-sdk/types" + + icatypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" + host "github.com/cosmos/ibc-go/v2/modules/core/24-host" +) + +// InitGenesis initializes the interchain accounts controller application state from a provided genesis state +func InitGenesis(ctx sdk.Context, keeper Keeper, state icatypes.ControllerGenesisState) { + for _, portID := range state.Ports { + if !keeper.IsBound(ctx, portID) { + cap := keeper.BindPort(ctx, portID) + if err := keeper.ClaimCapability(ctx, cap, host.PortPath(portID)); err != nil { + panic(fmt.Sprintf("could not claim port capability: %v", err)) + } + } + } + + for _, ch := range state.ActiveChannels { + keeper.SetActiveChannelID(ctx, ch.PortId, ch.ChannelId) + } + + for _, acc := range state.InterchainAccounts { + keeper.SetInterchainAccountAddress(ctx, acc.PortId, acc.AccountAddress) + } + + keeper.SetParams(ctx, state.Params) +} + +// ExportGenesis returns the interchain accounts controller exported genesis +func ExportGenesis(ctx sdk.Context, keeper Keeper) icatypes.ControllerGenesisState { + return icatypes.NewControllerGenesisState( + keeper.GetAllActiveChannels(ctx), + keeper.GetAllInterchainAccounts(ctx), + keeper.GetAllPorts(ctx), + keeper.GetParams(ctx), + ) +} diff --git a/modules/apps/27-interchain-accounts/controller/keeper/genesis_test.go b/modules/apps/27-interchain-accounts/controller/keeper/genesis_test.go new file mode 100644 index 0000000000..488d39d982 --- /dev/null +++ b/modules/apps/27-interchain-accounts/controller/keeper/genesis_test.go @@ -0,0 +1,66 @@ +package keeper_test + +import ( + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/controller/keeper" + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/controller/types" + icatypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" + ibctesting "github.com/cosmos/ibc-go/v2/testing" +) + +func (suite *KeeperTestSuite) TestInitGenesis() { + suite.SetupTest() + + genesisState := icatypes.ControllerGenesisState{ + ActiveChannels: []icatypes.ActiveChannel{ + { + PortId: TestPortID, + ChannelId: ibctesting.FirstChannelID, + }, + }, + InterchainAccounts: []icatypes.RegisteredInterchainAccount{ + { + PortId: TestPortID, + AccountAddress: TestAccAddress.String(), + }, + }, + Ports: []string{TestPortID}, + } + + keeper.InitGenesis(suite.chainA.GetContext(), suite.chainA.GetSimApp().ICAControllerKeeper, genesisState) + + channelID, found := suite.chainA.GetSimApp().ICAControllerKeeper.GetActiveChannelID(suite.chainA.GetContext(), TestPortID) + suite.Require().True(found) + suite.Require().Equal(ibctesting.FirstChannelID, channelID) + + accountAdrr, found := suite.chainA.GetSimApp().ICAControllerKeeper.GetInterchainAccountAddress(suite.chainA.GetContext(), TestPortID) + suite.Require().True(found) + suite.Require().Equal(TestAccAddress.String(), accountAdrr) + + expParams := types.NewParams(false) + params := suite.chainA.GetSimApp().ICAControllerKeeper.GetParams(suite.chainA.GetContext()) + suite.Require().Equal(expParams, params) + +} + +func (suite *KeeperTestSuite) TestExportGenesis() { + suite.SetupTest() + + path := NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := SetupICAPath(path, TestOwnerAddress) + suite.Require().NoError(err) + + genesisState := keeper.ExportGenesis(suite.chainA.GetContext(), suite.chainA.GetSimApp().ICAControllerKeeper) + + suite.Require().Equal(path.EndpointA.ChannelID, genesisState.ActiveChannels[0].ChannelId) + suite.Require().Equal(path.EndpointA.ChannelConfig.PortID, genesisState.ActiveChannels[0].PortId) + + suite.Require().Equal(TestAccAddress.String(), genesisState.InterchainAccounts[0].AccountAddress) + suite.Require().Equal(path.EndpointA.ChannelConfig.PortID, genesisState.InterchainAccounts[0].PortId) + + suite.Require().Equal([]string{TestPortID}, genesisState.GetPorts()) + + expParams := types.DefaultParams() + suite.Require().Equal(expParams, genesisState.GetParams()) +} diff --git a/modules/apps/27-interchain-accounts/controller/keeper/grpc_query.go b/modules/apps/27-interchain-accounts/controller/keeper/grpc_query.go new file mode 100644 index 0000000000..1e74921269 --- /dev/null +++ b/modules/apps/27-interchain-accounts/controller/keeper/grpc_query.go @@ -0,0 +1,19 @@ +package keeper + +import ( + "context" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/controller/types" +) + +var _ types.QueryServer = Keeper{} + +// Params implements the Query/Params gRPC method +func (q Keeper) Params(c context.Context, _ *types.QueryParamsRequest) (*types.QueryParamsResponse, error) { + ctx := sdk.UnwrapSDKContext(c) + params := q.GetParams(ctx) + + return &types.QueryParamsResponse{ + Params: ¶ms, + }, nil +} diff --git a/modules/apps/27-interchain-accounts/controller/keeper/grpc_query_test.go b/modules/apps/27-interchain-accounts/controller/keeper/grpc_query_test.go new file mode 100644 index 0000000000..03aa63474a --- /dev/null +++ b/modules/apps/27-interchain-accounts/controller/keeper/grpc_query_test.go @@ -0,0 +1,14 @@ +package keeper_test + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/controller/types" +) + +func (suite *KeeperTestSuite) TestQueryParams() { + ctx := sdk.WrapSDKContext(suite.chainA.GetContext()) + expParams := types.DefaultParams() + res, _ := suite.chainA.GetSimApp().ICAControllerKeeper.Params(ctx, &types.QueryParamsRequest{}) + suite.Require().Equal(&expParams, res.Params) +} diff --git a/modules/apps/27-interchain-accounts/controller/keeper/handshake.go b/modules/apps/27-interchain-accounts/controller/keeper/handshake.go new file mode 100644 index 0000000000..e48dee00f0 --- /dev/null +++ b/modules/apps/27-interchain-accounts/controller/keeper/handshake.go @@ -0,0 +1,137 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" + + icatypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" + connectiontypes "github.com/cosmos/ibc-go/v2/modules/core/03-connection/types" + channeltypes "github.com/cosmos/ibc-go/v2/modules/core/04-channel/types" + porttypes "github.com/cosmos/ibc-go/v2/modules/core/05-port/types" +) + +// OnChanOpenInit performs basic validation of channel initialization. +// The channel order must be ORDERED, the counterparty port identifier +// must be the host chain representation as defined in the types package, +// the channel version must be equal to the version in the types package, +// there must not be an active channel for the specfied port identifier, +// and the interchain accounts module must be able to claim the channel +// capability. +func (k Keeper) OnChanOpenInit( + ctx sdk.Context, + order channeltypes.Order, + connectionHops []string, + portID string, + channelID string, + chanCap *capabilitytypes.Capability, + counterparty channeltypes.Counterparty, + version string, +) error { + if order != channeltypes.ORDERED { + return sdkerrors.Wrapf(channeltypes.ErrInvalidChannelOrdering, "expected %s channel, got %s", channeltypes.ORDERED, order) + } + + connSequence, err := icatypes.ParseControllerConnSequence(portID) + if err != nil { + return sdkerrors.Wrapf(err, "expected format %s, got %s", icatypes.ControllerPortFormat, portID) + } + + counterpartyConnSequence, err := icatypes.ParseHostConnSequence(portID) + if err != nil { + return sdkerrors.Wrapf(err, "expected format %s, got %s", icatypes.ControllerPortFormat, portID) + } + + if err := k.validateControllerPortParams(ctx, channelID, portID, connSequence, counterpartyConnSequence); err != nil { + return sdkerrors.Wrapf(err, "failed to validate controller port %s", portID) + } + + if counterparty.PortId != icatypes.PortID { + return sdkerrors.Wrapf(porttypes.ErrInvalidPort, "expected %s, got %s", icatypes.PortID, counterparty.PortId) + } + + if version != icatypes.VersionPrefix { + return sdkerrors.Wrapf(icatypes.ErrInvalidVersion, "expected %s, got %s", icatypes.VersionPrefix, version) + } + + activeChannelID, found := k.GetActiveChannelID(ctx, portID) + if found { + return sdkerrors.Wrapf(porttypes.ErrInvalidPort, "existing active channel %s for portID %s", activeChannelID, portID) + } + + return nil +} + +// OnChanOpenAck sets the active channel for the interchain account/owner pair +// and stores the associated interchain account address in state keyed by it's corresponding port identifier +func (k Keeper) OnChanOpenAck( + ctx sdk.Context, + portID, + channelID string, + counterpartyVersion string, +) error { + if portID == icatypes.PortID { + return sdkerrors.Wrapf(porttypes.ErrInvalidPort, "portID cannot be host chain port ID: %s", icatypes.PortID) + } + + if err := icatypes.ValidateVersion(counterpartyVersion); err != nil { + return sdkerrors.Wrap(err, "counterparty version validation failed") + } + + k.SetActiveChannelID(ctx, portID, channelID) + + accAddr, err := icatypes.ParseAddressFromVersion(counterpartyVersion) + if err != nil { + return sdkerrors.Wrapf(err, "expected format , got %s", icatypes.Delimiter, counterpartyVersion) + } + + k.SetInterchainAccountAddress(ctx, portID, accAddr) + + return nil +} + +// OnChanCloseConfirm removes the active channel stored in state +func (k Keeper) OnChanCloseConfirm( + ctx sdk.Context, + portID, + channelID string, +) error { + + k.DeleteActiveChannelID(ctx, portID) + + return nil +} + +// validateControllerPortParams asserts the provided connection sequence and counterparty connection sequence +// match that of the associated connection stored in state +func (k Keeper) validateControllerPortParams(ctx sdk.Context, channelID, portID string, connectionSeq, counterpartyConnectionSeq uint64) error { + channel, found := k.channelKeeper.GetChannel(ctx, portID, channelID) + if !found { + return sdkerrors.Wrapf(channeltypes.ErrChannelNotFound, "port ID %s channel ID %s", portID, channelID) + } + + counterpartyHops, found := k.channelKeeper.CounterpartyHops(ctx, channel) + if !found { + return sdkerrors.Wrap(connectiontypes.ErrConnectionNotFound, channel.ConnectionHops[0]) + } + + connSeq, err := connectiontypes.ParseConnectionSequence(channel.ConnectionHops[0]) + if err != nil { + return sdkerrors.Wrapf(err, "failed to parse connection sequence %s", channel.ConnectionHops[0]) + } + + counterpartyConnSeq, err := connectiontypes.ParseConnectionSequence(counterpartyHops[0]) + if err != nil { + return sdkerrors.Wrapf(err, "failed to parse counterparty connection sequence %s", counterpartyHops[0]) + } + + if connSeq != connectionSeq { + return sdkerrors.Wrapf(connectiontypes.ErrInvalidConnection, "sequence mismatch, expected %d, got %d", connSeq, connectionSeq) + } + + if counterpartyConnSeq != counterpartyConnectionSeq { + return sdkerrors.Wrapf(connectiontypes.ErrInvalidConnection, "counterparty sequence mismatch, expected %d, got %d", counterpartyConnSeq, counterpartyConnectionSeq) + } + + return nil +} diff --git a/modules/apps/27-interchain-accounts/controller/keeper/handshake_test.go b/modules/apps/27-interchain-accounts/controller/keeper/handshake_test.go new file mode 100644 index 0000000000..31aaf9d028 --- /dev/null +++ b/modules/apps/27-interchain-accounts/controller/keeper/handshake_test.go @@ -0,0 +1,264 @@ +package keeper_test + +import ( + capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" + + icatypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" + channeltypes "github.com/cosmos/ibc-go/v2/modules/core/04-channel/types" + host "github.com/cosmos/ibc-go/v2/modules/core/24-host" + ibctesting "github.com/cosmos/ibc-go/v2/testing" +) + +func (suite *KeeperTestSuite) TestOnChanOpenInit() { + var ( + channel *channeltypes.Channel + path *ibctesting.Path + chanCap *capabilitytypes.Capability + ) + + testCases := []struct { + name string + malleate func() + expPass bool + }{ + + { + "success", + func() { + path.EndpointA.SetChannel(*channel) + }, + true, + }, + { + "invalid order - UNORDERED", + func() { + channel.Ordering = channeltypes.UNORDERED + }, + false, + }, + { + "invalid port ID", + func() { + path.EndpointA.ChannelConfig.PortID = "invalid-port-id" + }, + false, + }, + { + "invalid counterparty port ID", + func() { + path.EndpointA.SetChannel(*channel) + channel.Counterparty.PortId = "invalid-port-id" + }, + false, + }, + { + "invalid version", + func() { + path.EndpointA.SetChannel(*channel) + channel.Version = "version" + }, + false, + }, + { + "channel not found", + func() { + path.EndpointA.ChannelID = "invalid-channel-id" + }, + false, + }, + { + "connection not found", + func() { + channel.ConnectionHops = []string{"invalid-connnection-id"} + path.EndpointA.SetChannel(*channel) + }, + false, + }, + { + "invalid connection sequence", + func() { + portID, err := icatypes.GeneratePortID(TestOwnerAddress, "connection-1", "connection-0") + suite.Require().NoError(err) + + path.EndpointA.ChannelConfig.PortID = portID + path.EndpointA.SetChannel(*channel) + }, + false, + }, + { + "invalid counterparty connection sequence", + func() { + portID, err := icatypes.GeneratePortID(TestOwnerAddress, "connection-0", "connection-1") + suite.Require().NoError(err) + + path.EndpointA.ChannelConfig.PortID = portID + path.EndpointA.SetChannel(*channel) + }, + false, + }, + { + "channel is already active", + func() { + suite.chainA.GetSimApp().ICAControllerKeeper.SetActiveChannelID(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) + }, + false, + }, + } + + for _, tc := range testCases { + tc := tc + + suite.Run(tc.name, func() { + suite.SetupTest() // reset + + path = NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + // mock init interchain account + portID, err := icatypes.GeneratePortID(TestOwnerAddress, path.EndpointA.ConnectionID, path.EndpointB.ConnectionID) + suite.Require().NoError(err) + + portCap := suite.chainA.GetSimApp().IBCKeeper.PortKeeper.BindPort(suite.chainA.GetContext(), portID) + suite.chainA.GetSimApp().ICAControllerKeeper.ClaimCapability(suite.chainA.GetContext(), portCap, host.PortPath(portID)) + path.EndpointA.ChannelConfig.PortID = portID + + // default values + counterparty := channeltypes.NewCounterparty(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) + channel = &channeltypes.Channel{ + State: channeltypes.INIT, + Ordering: channeltypes.ORDERED, + Counterparty: counterparty, + ConnectionHops: []string{path.EndpointA.ConnectionID}, + Version: icatypes.VersionPrefix, + } + + chanCap, err = suite.chainA.App.GetScopedIBCKeeper().NewCapability(suite.chainA.GetContext(), host.ChannelCapabilityPath(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)) + suite.Require().NoError(err) + + tc.malleate() // malleate mutates test data + + err = suite.chainA.GetSimApp().ICAControllerKeeper.OnChanOpenInit(suite.chainA.GetContext(), channel.Ordering, channel.GetConnectionHops(), + path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, chanCap, channel.Counterparty, channel.GetVersion(), + ) + + if tc.expPass { + suite.Require().NoError(err) + } else { + suite.Require().Error(err) + } + + }) + } +} + +func (suite *KeeperTestSuite) TestOnChanOpenAck() { + var ( + path *ibctesting.Path + expectedChannelID string + counterpartyVersion string + ) + + testCases := []struct { + name string + malleate func() + expPass bool + }{ + { + "success", func() {}, true, + }, + { + "invalid counterparty version", + func() { + expectedChannelID = "" + counterpartyVersion = "version" + }, + false, + }, + { + "invalid portID", func() { + path.EndpointA.ChannelConfig.PortID = icatypes.PortID + expectedChannelID = "" + }, false, + }, + } + + for _, tc := range testCases { + tc := tc + + suite.Run(tc.name, func() { + suite.SetupTest() // reset + + path = NewICAPath(suite.chainA, suite.chainB) + counterpartyVersion = TestVersion + suite.coordinator.SetupConnections(path) + + err := InitInterchainAccount(path.EndpointA, TestOwnerAddress) + suite.Require().NoError(err) + + err = path.EndpointB.ChanOpenTry() + suite.Require().NoError(err) + expectedChannelID = path.EndpointA.ChannelID + + tc.malleate() // malleate mutates test data + + err = suite.chainA.GetSimApp().ICAControllerKeeper.OnChanOpenAck(suite.chainA.GetContext(), + path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, counterpartyVersion, + ) + + activeChannelID, _ := suite.chainA.GetSimApp().ICAControllerKeeper.GetActiveChannelID(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID) + + suite.Require().Equal(activeChannelID, expectedChannelID) + + if tc.expPass { + suite.Require().NoError(err) + } else { + suite.Require().Error(err) + } + }) + } +} + +func (suite *KeeperTestSuite) TestOnChanCloseConfirm() { + var ( + path *ibctesting.Path + ) + + testCases := []struct { + name string + malleate func() + expPass bool + }{ + + { + "success", func() {}, true, + }, + } + + for _, tc := range testCases { + suite.Run(tc.name, func() { + suite.SetupTest() // reset + + path = NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := SetupICAPath(path, TestOwnerAddress) + suite.Require().NoError(err) + + tc.malleate() // malleate mutates test data + + err = suite.chainB.GetSimApp().ICAControllerKeeper.OnChanCloseConfirm(suite.chainB.GetContext(), + path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) + + activeChannelID, found := suite.chainB.GetSimApp().ICAControllerKeeper.GetActiveChannelID(suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID) + + if tc.expPass { + suite.Require().NoError(err) + suite.Require().False(found) + suite.Require().Empty(activeChannelID) + } else { + suite.Require().Error(err) + } + + }) + } +} diff --git a/modules/apps/27-interchain-accounts/controller/keeper/keeper.go b/modules/apps/27-interchain-accounts/controller/keeper/keeper.go new file mode 100644 index 0000000000..91705ae89e --- /dev/null +++ b/modules/apps/27-interchain-accounts/controller/keeper/keeper.go @@ -0,0 +1,193 @@ +package keeper + +import ( + "fmt" + "strings" + + baseapp "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper" + capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" + paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" + "github.com/tendermint/tendermint/libs/log" + + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/controller/types" + icatypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" + host "github.com/cosmos/ibc-go/v2/modules/core/24-host" +) + +// Keeper defines the IBC interchain accounts controller keeper +type Keeper struct { + storeKey sdk.StoreKey + cdc codec.BinaryCodec + paramSpace paramtypes.Subspace + + ics4Wrapper icatypes.ICS4Wrapper + channelKeeper icatypes.ChannelKeeper + portKeeper icatypes.PortKeeper + accountKeeper icatypes.AccountKeeper + + scopedKeeper capabilitykeeper.ScopedKeeper + + msgRouter *baseapp.MsgServiceRouter +} + +// NewKeeper creates a new interchain accounts controller Keeper instance +func NewKeeper( + cdc codec.BinaryCodec, key sdk.StoreKey, paramSpace paramtypes.Subspace, + ics4Wrapper icatypes.ICS4Wrapper, channelKeeper icatypes.ChannelKeeper, portKeeper icatypes.PortKeeper, + accountKeeper icatypes.AccountKeeper, scopedKeeper capabilitykeeper.ScopedKeeper, msgRouter *baseapp.MsgServiceRouter, +) Keeper { + + // set KeyTable if it has not already been set + if !paramSpace.HasKeyTable() { + paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable()) + } + + return Keeper{ + storeKey: key, + cdc: cdc, + paramSpace: paramSpace, + ics4Wrapper: ics4Wrapper, + channelKeeper: channelKeeper, + portKeeper: portKeeper, + accountKeeper: accountKeeper, + scopedKeeper: scopedKeeper, + msgRouter: msgRouter, + } +} + +// Logger returns the application logger, scoped to the associated module +func (k Keeper) Logger(ctx sdk.Context) log.Logger { + return ctx.Logger().With("module", fmt.Sprintf("x/%s-%s", host.ModuleName, icatypes.ModuleName)) +} + +// GetAllPorts returns all ports to which the interchain accounts controller module is bound. Used in ExportGenesis +func (k Keeper) GetAllPorts(ctx sdk.Context) []string { + store := ctx.KVStore(k.storeKey) + iterator := sdk.KVStorePrefixIterator(store, []byte(icatypes.PortKeyPrefix)) + defer iterator.Close() + + var ports []string + for ; iterator.Valid(); iterator.Next() { + keySplit := strings.Split(string(iterator.Key()), "/") + + ports = append(ports, keySplit[1]) + } + + return ports +} + +// BindPort stores the provided portID and binds to it, returning the associated capability +func (k Keeper) BindPort(ctx sdk.Context, portID string) *capabilitytypes.Capability { + store := ctx.KVStore(k.storeKey) + store.Set(icatypes.KeyPort(portID), []byte{0x01}) + + return k.portKeeper.BindPort(ctx, portID) +} + +// IsBound checks if the interchain account controller module is already bound to the desired port +func (k Keeper) IsBound(ctx sdk.Context, portID string) bool { + _, ok := k.scopedKeeper.GetCapability(ctx, host.PortPath(portID)) + return ok +} + +// AuthenticateCapability wraps the scopedKeeper's AuthenticateCapability function +func (k Keeper) AuthenticateCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) bool { + return k.scopedKeeper.AuthenticateCapability(ctx, cap, name) +} + +// ClaimCapability wraps the scopedKeeper's ClaimCapability function +func (k Keeper) ClaimCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) error { + return k.scopedKeeper.ClaimCapability(ctx, cap, name) +} + +// GetActiveChannelID retrieves the active channelID from the store keyed by the provided portID +func (k Keeper) GetActiveChannelID(ctx sdk.Context, portID string) (string, bool) { + store := ctx.KVStore(k.storeKey) + key := icatypes.KeyActiveChannel(portID) + + if !store.Has(key) { + return "", false + } + + return string(store.Get(key)), true +} + +// GetAllActiveChannels returns a list of all active interchain accounts controller channels and their associated port identifiers +func (k Keeper) GetAllActiveChannels(ctx sdk.Context) []icatypes.ActiveChannel { + store := ctx.KVStore(k.storeKey) + iterator := sdk.KVStorePrefixIterator(store, []byte(icatypes.ActiveChannelKeyPrefix)) + defer iterator.Close() + + var activeChannels []icatypes.ActiveChannel + for ; iterator.Valid(); iterator.Next() { + keySplit := strings.Split(string(iterator.Key()), "/") + + ch := icatypes.ActiveChannel{ + PortId: keySplit[1], + ChannelId: string(iterator.Value()), + } + + activeChannels = append(activeChannels, ch) + } + + return activeChannels +} + +// SetActiveChannelID stores the active channelID, keyed by the provided portID +func (k Keeper) SetActiveChannelID(ctx sdk.Context, portID, channelID string) { + store := ctx.KVStore(k.storeKey) + store.Set(icatypes.KeyActiveChannel(portID), []byte(channelID)) +} + +// DeleteActiveChannelID removes the active channel keyed by the provided portID stored in state +func (k Keeper) DeleteActiveChannelID(ctx sdk.Context, portID string) { + store := ctx.KVStore(k.storeKey) + store.Delete(icatypes.KeyActiveChannel(portID)) +} + +// IsActiveChannel returns true if there exists an active channel for the provided portID, otherwise false +func (k Keeper) IsActiveChannel(ctx sdk.Context, portID string) bool { + _, ok := k.GetActiveChannelID(ctx, portID) + return ok +} + +// GetInterchainAccountAddress retrieves the InterchainAccount address from the store keyed by the provided portID +func (k Keeper) GetInterchainAccountAddress(ctx sdk.Context, portID string) (string, bool) { + store := ctx.KVStore(k.storeKey) + key := icatypes.KeyOwnerAccount(portID) + + if !store.Has(key) { + return "", false + } + + return string(store.Get(key)), true +} + +// GetAllInterchainAccounts returns a list of all registered interchain account addresses and their associated controller port identifiers +func (k Keeper) GetAllInterchainAccounts(ctx sdk.Context) []icatypes.RegisteredInterchainAccount { + store := ctx.KVStore(k.storeKey) + iterator := sdk.KVStorePrefixIterator(store, []byte(icatypes.OwnerKeyPrefix)) + + var interchainAccounts []icatypes.RegisteredInterchainAccount + for ; iterator.Valid(); iterator.Next() { + keySplit := strings.Split(string(iterator.Key()), "/") + + acc := icatypes.RegisteredInterchainAccount{ + PortId: keySplit[1], + AccountAddress: string(iterator.Value()), + } + + interchainAccounts = append(interchainAccounts, acc) + } + + return interchainAccounts +} + +// SetInterchainAccountAddress stores the InterchainAccount address, keyed by the associated portID +func (k Keeper) SetInterchainAccountAddress(ctx sdk.Context, portID string, address string) { + store := ctx.KVStore(k.storeKey) + store.Set(icatypes.KeyOwnerAccount(portID), []byte(address)) +} diff --git a/modules/apps/27-interchain-accounts/controller/keeper/keeper_test.go b/modules/apps/27-interchain-accounts/controller/keeper/keeper_test.go new file mode 100644 index 0000000000..f522a954fa --- /dev/null +++ b/modules/apps/27-interchain-accounts/controller/keeper/keeper_test.go @@ -0,0 +1,247 @@ +package keeper_test + +import ( + "testing" + + sdk "github.com/cosmos/cosmos-sdk/types" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + "github.com/stretchr/testify/suite" + "github.com/tendermint/tendermint/crypto" + + icatypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" + channeltypes "github.com/cosmos/ibc-go/v2/modules/core/04-channel/types" + ibctesting "github.com/cosmos/ibc-go/v2/testing" +) + +var ( + // TestAccAddress defines a resuable bech32 address for testing purposes + // TODO: update crypto.AddressHash() when sdk uses address.Module() + TestAccAddress = icatypes.GenerateAddress(sdk.AccAddress(crypto.AddressHash([]byte(icatypes.ModuleName))), TestPortID) + // TestOwnerAddress defines a reusable bech32 address for testing purposes + TestOwnerAddress = "cosmos17dtl0mjt3t77kpuhg2edqzjpszulwhgzuj9ljs" + // TestPortID defines a resuable port identifier for testing purposes + TestPortID, _ = icatypes.GeneratePortID(TestOwnerAddress, ibctesting.FirstConnectionID, ibctesting.FirstConnectionID) + // TestVersion defines a resuable interchainaccounts version string for testing purposes + TestVersion = icatypes.NewAppVersion(icatypes.VersionPrefix, TestAccAddress.String()) +) + +type KeeperTestSuite struct { + suite.Suite + + coordinator *ibctesting.Coordinator + + // testing chains used for convenience and readability + chainA *ibctesting.TestChain + chainB *ibctesting.TestChain + chainC *ibctesting.TestChain +} + +func (suite *KeeperTestSuite) SetupTest() { + suite.coordinator = ibctesting.NewCoordinator(suite.T(), 3) + suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(0)) + suite.chainB = suite.coordinator.GetChain(ibctesting.GetChainID(1)) + suite.chainC = suite.coordinator.GetChain(ibctesting.GetChainID(2)) +} + +func NewICAPath(chainA, chainB *ibctesting.TestChain) *ibctesting.Path { + path := ibctesting.NewPath(chainA, chainB) + path.EndpointA.ChannelConfig.PortID = icatypes.PortID + path.EndpointB.ChannelConfig.PortID = icatypes.PortID + path.EndpointA.ChannelConfig.Order = channeltypes.ORDERED + path.EndpointB.ChannelConfig.Order = channeltypes.ORDERED + path.EndpointA.ChannelConfig.Version = icatypes.VersionPrefix + path.EndpointB.ChannelConfig.Version = TestVersion + + return path +} + +// SetupICAPath invokes the InterchainAccounts entrypoint and subsequent channel handshake handlers +func SetupICAPath(path *ibctesting.Path, owner string) error { + if err := InitInterchainAccount(path.EndpointA, owner); err != nil { + return err + } + + if err := path.EndpointB.ChanOpenTry(); err != nil { + return err + } + + if err := path.EndpointA.ChanOpenAck(); err != nil { + return err + } + + if err := path.EndpointB.ChanOpenConfirm(); err != nil { + return err + } + + return nil +} + +// InitInterchainAccount is a helper function for starting the channel handshake +func InitInterchainAccount(endpoint *ibctesting.Endpoint, owner string) error { + portID, err := icatypes.GeneratePortID(owner, endpoint.ConnectionID, endpoint.Counterparty.ConnectionID) + if err != nil { + return err + } + + channelSequence := endpoint.Chain.App.GetIBCKeeper().ChannelKeeper.GetNextChannelSequence(endpoint.Chain.GetContext()) + + if err := endpoint.Chain.GetSimApp().ICAControllerKeeper.InitInterchainAccount(endpoint.Chain.GetContext(), endpoint.ConnectionID, endpoint.Counterparty.ConnectionID, owner); err != nil { + return err + } + + // commit state changes for proof verification + endpoint.Chain.App.Commit() + endpoint.Chain.NextBlock() + + // update port/channel ids + endpoint.ChannelID = channeltypes.FormatChannelIdentifier(channelSequence) + endpoint.ChannelConfig.PortID = portID + + return nil +} + +func TestKeeperTestSuite(t *testing.T) { + suite.Run(t, new(KeeperTestSuite)) +} + +func (suite *KeeperTestSuite) TestIsBound() { + suite.SetupTest() + + path := NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := SetupICAPath(path, TestOwnerAddress) + suite.Require().NoError(err) + + isBound := suite.chainA.GetSimApp().ICAControllerKeeper.IsBound(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID) + suite.Require().True(isBound) +} + +func (suite *KeeperTestSuite) TestGetAllPorts() { + suite.SetupTest() + + path := NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := SetupICAPath(path, TestOwnerAddress) + suite.Require().NoError(err) + + expectedPorts := []string{TestPortID} + + ports := suite.chainA.GetSimApp().ICAControllerKeeper.GetAllPorts(suite.chainA.GetContext()) + suite.Require().Len(ports, len(expectedPorts)) + suite.Require().Equal(expectedPorts, ports) +} + +func (suite *KeeperTestSuite) TestGetInterchainAccountAddress() { + suite.SetupTest() + + path := NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := SetupICAPath(path, TestOwnerAddress) + suite.Require().NoError(err) + + counterpartyPortID := path.EndpointA.ChannelConfig.PortID + expectedAddr := authtypes.NewBaseAccountWithAddress(icatypes.GenerateAddress(suite.chainA.GetSimApp().AccountKeeper.GetModuleAddress(icatypes.ModuleName), counterpartyPortID)).GetAddress() + + retrievedAddr, found := suite.chainA.GetSimApp().ICAControllerKeeper.GetInterchainAccountAddress(suite.chainA.GetContext(), counterpartyPortID) + suite.Require().True(found) + suite.Require().Equal(expectedAddr.String(), retrievedAddr) + + retrievedAddr, found = suite.chainA.GetSimApp().ICAControllerKeeper.GetInterchainAccountAddress(suite.chainA.GetContext(), "invalid port") + suite.Require().False(found) + suite.Require().Empty(retrievedAddr) +} + +func (suite *KeeperTestSuite) TestGetAllActiveChannels() { + var ( + expectedChannelID string = "test-channel" + expectedPortID string = "test-port" + ) + + suite.SetupTest() + + path := NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := SetupICAPath(path, TestOwnerAddress) + suite.Require().NoError(err) + + suite.chainA.GetSimApp().ICAControllerKeeper.SetActiveChannelID(suite.chainA.GetContext(), expectedPortID, expectedChannelID) + + expectedChannels := []icatypes.ActiveChannel{ + { + PortId: TestPortID, + ChannelId: path.EndpointA.ChannelID, + }, + { + PortId: expectedPortID, + ChannelId: expectedChannelID, + }, + } + + activeChannels := suite.chainA.GetSimApp().ICAControllerKeeper.GetAllActiveChannels(suite.chainA.GetContext()) + suite.Require().Len(activeChannels, len(expectedChannels)) + suite.Require().Equal(expectedChannels, activeChannels) +} + +func (suite *KeeperTestSuite) TestGetAllInterchainAccounts() { + var ( + expectedAccAddr string = "test-acc-addr" + expectedPortID string = "test-port" + ) + + suite.SetupTest() + + path := NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := SetupICAPath(path, TestOwnerAddress) + suite.Require().NoError(err) + + suite.chainA.GetSimApp().ICAControllerKeeper.SetInterchainAccountAddress(suite.chainA.GetContext(), expectedPortID, expectedAccAddr) + + expectedAccounts := []icatypes.RegisteredInterchainAccount{ + { + PortId: TestPortID, + AccountAddress: TestAccAddress.String(), + }, + { + PortId: expectedPortID, + AccountAddress: expectedAccAddr, + }, + } + + interchainAccounts := suite.chainA.GetSimApp().ICAControllerKeeper.GetAllInterchainAccounts(suite.chainA.GetContext()) + suite.Require().Len(interchainAccounts, len(expectedAccounts)) + suite.Require().Equal(expectedAccounts, interchainAccounts) +} + +func (suite *KeeperTestSuite) TestIsActiveChannel() { + suite.SetupTest() + + path := NewICAPath(suite.chainA, suite.chainB) + owner := TestOwnerAddress + suite.coordinator.SetupConnections(path) + + err := SetupICAPath(path, owner) + suite.Require().NoError(err) + portID := path.EndpointA.ChannelConfig.PortID + + isActive := suite.chainA.GetSimApp().ICAControllerKeeper.IsActiveChannel(suite.chainA.GetContext(), portID) + suite.Require().Equal(isActive, true) +} + +func (suite *KeeperTestSuite) TestSetInterchainAccountAddress() { + var ( + expectedAccAddr string = "test-acc-addr" + expectedPortID string = "test-port" + ) + + suite.chainA.GetSimApp().ICAControllerKeeper.SetInterchainAccountAddress(suite.chainA.GetContext(), expectedPortID, expectedAccAddr) + + retrievedAddr, found := suite.chainA.GetSimApp().ICAControllerKeeper.GetInterchainAccountAddress(suite.chainA.GetContext(), expectedPortID) + suite.Require().True(found) + suite.Require().Equal(expectedAccAddr, retrievedAddr) +} diff --git a/modules/apps/27-interchain-accounts/controller/keeper/params.go b/modules/apps/27-interchain-accounts/controller/keeper/params.go new file mode 100644 index 0000000000..55e15e26d9 --- /dev/null +++ b/modules/apps/27-interchain-accounts/controller/keeper/params.go @@ -0,0 +1,25 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/controller/types" +) + +// IsControllerEnabled retrieves the host enabled boolean from the paramstore. +// True is returned if the controller submodule is enabled. +func (k Keeper) IsControllerEnabled(ctx sdk.Context) bool { + var res bool + k.paramSpace.Get(ctx, types.KeyControllerEnabled, &res) + return res +} + +// GetParams returns the total set of the host submodule parameters. +func (k Keeper) GetParams(ctx sdk.Context) types.Params { + return types.NewParams(k.IsControllerEnabled(ctx)) +} + +// SetParams sets the total set of the host submodule parameters. +func (k Keeper) SetParams(ctx sdk.Context, params types.Params) { + k.paramSpace.SetParamSet(ctx, ¶ms) +} diff --git a/modules/apps/27-interchain-accounts/controller/keeper/params_test.go b/modules/apps/27-interchain-accounts/controller/keeper/params_test.go new file mode 100644 index 0000000000..27138d42de --- /dev/null +++ b/modules/apps/27-interchain-accounts/controller/keeper/params_test.go @@ -0,0 +1,15 @@ +package keeper_test + +import "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/controller/types" + +func (suite *KeeperTestSuite) TestParams() { + expParams := types.DefaultParams() + + params := suite.chainA.GetSimApp().ICAControllerKeeper.GetParams(suite.chainA.GetContext()) + suite.Require().Equal(expParams, params) + + expParams.ControllerEnabled = false + suite.chainA.GetSimApp().ICAControllerKeeper.SetParams(suite.chainA.GetContext(), expParams) + params = suite.chainA.GetSimApp().ICAControllerKeeper.GetParams(suite.chainA.GetContext()) + suite.Require().Equal(expParams, params) +} diff --git a/modules/apps/27-interchain-accounts/controller/keeper/relay.go b/modules/apps/27-interchain-accounts/controller/keeper/relay.go new file mode 100644 index 0000000000..a7bbda175a --- /dev/null +++ b/modules/apps/27-interchain-accounts/controller/keeper/relay.go @@ -0,0 +1,80 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" + + icatypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" + clienttypes "github.com/cosmos/ibc-go/v2/modules/core/02-client/types" + channeltypes "github.com/cosmos/ibc-go/v2/modules/core/04-channel/types" +) + +// TrySendTx takes in a transaction from an authentication module and attempts to send the packet +// if the base application has the capability to send on the provided portID +func (k Keeper) TrySendTx(ctx sdk.Context, chanCap *capabilitytypes.Capability, portID string, icaPacketData icatypes.InterchainAccountPacketData) (uint64, error) { + // Check for the active channel + activeChannelID, found := k.GetActiveChannelID(ctx, portID) + if !found { + return 0, sdkerrors.Wrapf(icatypes.ErrActiveChannelNotFound, "failed to retrieve active channel for port %s", portID) + } + + sourceChannelEnd, found := k.channelKeeper.GetChannel(ctx, portID, activeChannelID) + if !found { + return 0, sdkerrors.Wrap(channeltypes.ErrChannelNotFound, activeChannelID) + } + + destinationPort := sourceChannelEnd.GetCounterparty().GetPortID() + destinationChannel := sourceChannelEnd.GetCounterparty().GetChannelID() + + return k.createOutgoingPacket(ctx, portID, activeChannelID, destinationPort, destinationChannel, chanCap, icaPacketData) +} + +func (k Keeper) createOutgoingPacket( + ctx sdk.Context, + sourcePort, + sourceChannel, + destinationPort, + destinationChannel string, + chanCap *capabilitytypes.Capability, + icaPacketData icatypes.InterchainAccountPacketData, +) (uint64, error) { + if err := icaPacketData.ValidateBasic(); err != nil { + return 0, sdkerrors.Wrap(err, "invalid interchain account packet data") + } + + // get the next sequence + sequence, found := k.channelKeeper.GetNextSequenceSend(ctx, sourcePort, sourceChannel) + if !found { + return 0, sdkerrors.Wrapf(channeltypes.ErrSequenceSendNotFound, "failed to retrieve next sequence send for channel %s on port %s", sourceChannel, sourcePort) + } + + // timeoutTimestamp is set to be a max number here so that we never recieve a timeout + // ics-27-1 uses ordered channels which can close upon recieving a timeout, which is an undesired effect + const timeoutTimestamp = ^uint64(0) >> 1 // Shift the unsigned bit to satisfy hermes relayer timestamp conversion + + packet := channeltypes.NewPacket( + icaPacketData.GetBytes(), + sequence, + sourcePort, + sourceChannel, + destinationPort, + destinationChannel, + clienttypes.ZeroHeight(), + timeoutTimestamp, + ) + + if err := k.ics4Wrapper.SendPacket(ctx, chanCap, packet); err != nil { + return 0, err + } + + return packet.Sequence, nil +} + +// OnTimeoutPacket removes the active channel associated with the provided packet, the underlying channel end is closed +// due to the semantics of ORDERED channels +func (k Keeper) OnTimeoutPacket(ctx sdk.Context, packet channeltypes.Packet) error { + k.DeleteActiveChannelID(ctx, packet.SourcePort) + + return nil +} diff --git a/modules/apps/27-interchain-accounts/controller/keeper/relay_test.go b/modules/apps/27-interchain-accounts/controller/keeper/relay_test.go new file mode 100644 index 0000000000..d00ed6a936 --- /dev/null +++ b/modules/apps/27-interchain-accounts/controller/keeper/relay_test.go @@ -0,0 +1,201 @@ +package keeper_test + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" + + icatypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" + clienttypes "github.com/cosmos/ibc-go/v2/modules/core/02-client/types" + channeltypes "github.com/cosmos/ibc-go/v2/modules/core/04-channel/types" + host "github.com/cosmos/ibc-go/v2/modules/core/24-host" + ibctesting "github.com/cosmos/ibc-go/v2/testing" +) + +func (suite *KeeperTestSuite) TestTrySendTx() { + var ( + path *ibctesting.Path + packetData icatypes.InterchainAccountPacketData + chanCap *capabilitytypes.Capability + ) + + testCases := []struct { + msg string + malleate func() + expPass bool + }{ + { + "success", + func() { + interchainAccountAddr, found := suite.chainA.GetSimApp().ICAControllerKeeper.GetInterchainAccountAddress(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID) + suite.Require().True(found) + + msg := &banktypes.MsgSend{ + FromAddress: interchainAccountAddr, + ToAddress: suite.chainB.SenderAccount.GetAddress().String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100))), + } + + data, err := icatypes.SerializeCosmosTx(suite.chainB.GetSimApp().AppCodec(), []sdk.Msg{msg}) + suite.Require().NoError(err) + + packetData = icatypes.InterchainAccountPacketData{ + Type: icatypes.EXECUTE_TX, + Data: data, + } + }, + true, + }, + { + "success with multiple sdk.Msg", + func() { + interchainAccountAddr, found := suite.chainA.GetSimApp().ICAControllerKeeper.GetInterchainAccountAddress(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID) + suite.Require().True(found) + + msgsBankSend := []sdk.Msg{ + &banktypes.MsgSend{ + FromAddress: interchainAccountAddr, + ToAddress: suite.chainB.SenderAccount.GetAddress().String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100))), + }, + &banktypes.MsgSend{ + FromAddress: interchainAccountAddr, + ToAddress: suite.chainB.SenderAccount.GetAddress().String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100))), + }, + } + + data, err := icatypes.SerializeCosmosTx(suite.chainB.GetSimApp().AppCodec(), msgsBankSend) + suite.Require().NoError(err) + + packetData = icatypes.InterchainAccountPacketData{ + Type: icatypes.EXECUTE_TX, + Data: data, + } + }, + true, + }, + { + "data is nil", + func() { + packetData = icatypes.InterchainAccountPacketData{ + Type: icatypes.EXECUTE_TX, + Data: nil, + } + }, + false, + }, + { + "active channel not found", + func() { + path.EndpointA.ChannelConfig.PortID = "invalid-port-id" + }, + false, + }, + { + "channel does not exist", + func() { + suite.chainA.GetSimApp().ICAControllerKeeper.SetActiveChannelID(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, "channel-100") + }, + false, + }, + { + "sendPacket fails - channel closed", + func() { + err := path.EndpointA.SetChannelClosed() + suite.Require().NoError(err) + }, + false, + }, + { + "invalid channel capability provided", + func() { + chanCap = nil + }, + false, + }, + } + + for _, tc := range testCases { + tc := tc + + suite.Run(tc.msg, func() { + suite.SetupTest() // reset + + path = NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := SetupICAPath(path, TestOwnerAddress) + suite.Require().NoError(err) + + var ok bool + chanCap, ok = suite.chainA.GetSimApp().ScopedICAMockKeeper.GetCapability(path.EndpointA.Chain.GetContext(), host.ChannelCapabilityPath(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)) + suite.Require().True(ok) + + tc.malleate() // malleate mutates test data + + _, err = suite.chainA.GetSimApp().ICAControllerKeeper.TrySendTx(suite.chainA.GetContext(), chanCap, path.EndpointA.ChannelConfig.PortID, packetData) + + if tc.expPass { + suite.Require().NoError(err) + } else { + suite.Require().Error(err) + } + }) + } +} + +func (suite *KeeperTestSuite) TestOnTimeoutPacket() { + var ( + path *ibctesting.Path + ) + + testCases := []struct { + msg string + malleate func() + expPass bool + }{ + { + "success", + func() {}, + true, + }, + } + + for _, tc := range testCases { + suite.Run(tc.msg, func() { + suite.SetupTest() // reset + + path = NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := SetupICAPath(path, TestOwnerAddress) + suite.Require().NoError(err) + + tc.malleate() // malleate mutates test data + + packet := channeltypes.NewPacket( + []byte{}, + 1, + path.EndpointA.ChannelConfig.PortID, + path.EndpointA.ChannelID, + path.EndpointB.ChannelConfig.PortID, + path.EndpointB.ChannelID, + clienttypes.NewHeight(0, 100), + 0, + ) + + err = suite.chainA.GetSimApp().ICAControllerKeeper.OnTimeoutPacket(suite.chainA.GetContext(), packet) + + activeChannelID, found := suite.chainA.GetSimApp().ICAControllerKeeper.GetActiveChannelID(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID) + + if tc.expPass { + suite.Require().NoError(err) + suite.Require().Empty(activeChannelID) + suite.Require().False(found) + } else { + suite.Require().Error(err) + } + }) + } +} diff --git a/modules/apps/27-interchain-accounts/controller/types/controller.pb.go b/modules/apps/27-interchain-accounts/controller/types/controller.pb.go new file mode 100644 index 0000000000..3a292e9a77 --- /dev/null +++ b/modules/apps/27-interchain-accounts/controller/types/controller.pb.go @@ -0,0 +1,318 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: ibc/applications/interchain_accounts/controller/v1/controller.proto + +package types + +import ( + fmt "fmt" + _ "github.com/cosmos/cosmos-sdk/codec/types" + _ "github.com/gogo/protobuf/gogoproto" + proto "github.com/gogo/protobuf/proto" + io "io" + math "math" + math_bits "math/bits" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package + +// Params defines the set of on-chain interchain accounts parameters. +// The following parameters may be used to disable the controller submodule. +type Params struct { + // controller_enabled enables or disables the controller submodule. + ControllerEnabled bool `protobuf:"varint,1,opt,name=controller_enabled,json=controllerEnabled,proto3" json:"controller_enabled,omitempty" yaml:"controller_enabled"` +} + +func (m *Params) Reset() { *m = Params{} } +func (m *Params) String() string { return proto.CompactTextString(m) } +func (*Params) ProtoMessage() {} +func (*Params) Descriptor() ([]byte, []int) { + return fileDescriptor_177fd0fec5eb3400, []int{0} +} +func (m *Params) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *Params) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_Params.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *Params) XXX_Merge(src proto.Message) { + xxx_messageInfo_Params.Merge(m, src) +} +func (m *Params) XXX_Size() int { + return m.Size() +} +func (m *Params) XXX_DiscardUnknown() { + xxx_messageInfo_Params.DiscardUnknown(m) +} + +var xxx_messageInfo_Params proto.InternalMessageInfo + +func (m *Params) GetControllerEnabled() bool { + if m != nil { + return m.ControllerEnabled + } + return false +} + +func init() { + proto.RegisterType((*Params)(nil), "ibc.applications.interchain_accounts.controller.v1.Params") +} + +func init() { + proto.RegisterFile("ibc/applications/interchain_accounts/controller/v1/controller.proto", fileDescriptor_177fd0fec5eb3400) +} + +var fileDescriptor_177fd0fec5eb3400 = []byte{ + // 268 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x90, 0xb1, 0x4e, 0xc3, 0x30, + 0x14, 0x45, 0x93, 0xa5, 0x42, 0xd9, 0x88, 0x18, 0x68, 0x25, 0x0c, 0xca, 0xc4, 0x92, 0x3c, 0x35, + 0x0c, 0x48, 0x8c, 0x45, 0x6c, 0x0c, 0x15, 0x03, 0x03, 0x4b, 0x65, 0xbb, 0xc6, 0x35, 0x72, 0xfc, + 0x22, 0xdb, 0x89, 0x94, 0xbf, 0xe0, 0xb3, 0x18, 0x3b, 0x32, 0x21, 0x94, 0xfc, 0x01, 0x5f, 0x80, + 0x9a, 0x0c, 0x89, 0xd4, 0x6e, 0xd7, 0x47, 0x7e, 0x47, 0xba, 0x37, 0x7a, 0x54, 0x8c, 0x03, 0x2d, + 0x4b, 0xad, 0x38, 0xf5, 0x0a, 0x8d, 0x03, 0x65, 0xbc, 0xb0, 0x7c, 0x47, 0x95, 0xd9, 0x50, 0xce, + 0xb1, 0x32, 0xde, 0x01, 0x47, 0xe3, 0x2d, 0x6a, 0x2d, 0x2c, 0xd4, 0xcb, 0xc9, 0x2b, 0x2b, 0x2d, + 0x7a, 0x8c, 0x73, 0xc5, 0x78, 0x36, 0x95, 0x64, 0x27, 0x24, 0xd9, 0xe4, 0xac, 0x5e, 0x2e, 0xe6, + 0x12, 0x51, 0x6a, 0x01, 0xbd, 0x81, 0x55, 0xef, 0x40, 0x4d, 0x33, 0xe8, 0x16, 0x17, 0x12, 0x25, + 0xf6, 0x11, 0x0e, 0x69, 0xa0, 0xc9, 0x6b, 0x34, 0x5b, 0x53, 0x4b, 0x0b, 0x17, 0x3f, 0x47, 0xf1, + 0xe8, 0xda, 0x08, 0x43, 0x99, 0x16, 0xdb, 0xcb, 0xf0, 0x26, 0xbc, 0x3d, 0x5b, 0x5d, 0xfd, 0xfd, + 0x5c, 0xcf, 0x1b, 0x5a, 0xe8, 0x87, 0xe4, 0xf8, 0x4f, 0xf2, 0x72, 0x3e, 0xc2, 0xa7, 0x81, 0xad, + 0x3e, 0xbe, 0x5a, 0x12, 0xee, 0x5b, 0x12, 0xfe, 0xb6, 0x24, 0xfc, 0xec, 0x48, 0xb0, 0xef, 0x48, + 0xf0, 0xdd, 0x91, 0xe0, 0x6d, 0x2d, 0x95, 0xdf, 0x55, 0x2c, 0xe3, 0x58, 0x00, 0x47, 0x57, 0xa0, + 0x03, 0xc5, 0x78, 0x2a, 0x11, 0xea, 0x1c, 0x0a, 0xdc, 0x56, 0x5a, 0xb8, 0xc3, 0x76, 0x0e, 0xf2, + 0xfb, 0x74, 0x6c, 0x9c, 0x9e, 0x9a, 0xcd, 0x37, 0xa5, 0x70, 0x6c, 0xd6, 0x57, 0xb9, 0xfb, 0x0f, + 0x00, 0x00, 0xff, 0xff, 0x0f, 0xf9, 0x46, 0xeb, 0x76, 0x01, 0x00, 0x00, +} + +func (m *Params) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *Params) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *Params) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.ControllerEnabled { + i-- + if m.ControllerEnabled { + dAtA[i] = 1 + } else { + dAtA[i] = 0 + } + i-- + dAtA[i] = 0x8 + } + return len(dAtA) - i, nil +} + +func encodeVarintController(dAtA []byte, offset int, v uint64) int { + offset -= sovController(v) + base := offset + for v >= 1<<7 { + dAtA[offset] = uint8(v&0x7f | 0x80) + v >>= 7 + offset++ + } + dAtA[offset] = uint8(v) + return base +} +func (m *Params) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.ControllerEnabled { + n += 2 + } + return n +} + +func sovController(x uint64) (n int) { + return (math_bits.Len64(x|1) + 6) / 7 +} +func sozController(x uint64) (n int) { + return sovController(uint64((x << 1) ^ uint64((int64(x) >> 63)))) +} +func (m *Params) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowController + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: Params: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: Params: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field ControllerEnabled", wireType) + } + var v int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowController + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + v |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + m.ControllerEnabled = bool(v != 0) + default: + iNdEx = preIndex + skippy, err := skipController(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthController + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func skipController(dAtA []byte) (n int, err error) { + l := len(dAtA) + iNdEx := 0 + depth := 0 + for iNdEx < l { + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowController + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + wireType := int(wire & 0x7) + switch wireType { + case 0: + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowController + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + iNdEx++ + if dAtA[iNdEx-1] < 0x80 { + break + } + } + case 1: + iNdEx += 8 + case 2: + var length int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowController + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + length |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if length < 0 { + return 0, ErrInvalidLengthController + } + iNdEx += length + case 3: + depth++ + case 4: + if depth == 0 { + return 0, ErrUnexpectedEndOfGroupController + } + depth-- + case 5: + iNdEx += 4 + default: + return 0, fmt.Errorf("proto: illegal wireType %d", wireType) + } + if iNdEx < 0 { + return 0, ErrInvalidLengthController + } + if depth == 0 { + return iNdEx, nil + } + } + return 0, io.ErrUnexpectedEOF +} + +var ( + ErrInvalidLengthController = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowController = fmt.Errorf("proto: integer overflow") + ErrUnexpectedEndOfGroupController = fmt.Errorf("proto: unexpected end of group") +) diff --git a/modules/apps/27-interchain-accounts/controller/types/errors.go b/modules/apps/27-interchain-accounts/controller/types/errors.go new file mode 100644 index 0000000000..3a0ade00fe --- /dev/null +++ b/modules/apps/27-interchain-accounts/controller/types/errors.go @@ -0,0 +1,10 @@ +package types + +import ( + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" +) + +// ICA Controller sentinel errors +var ( + ErrControllerSubModuleDisabled = sdkerrors.Register(SubModuleName, 2, "controller submodule is disabled") +) diff --git a/modules/apps/27-interchain-accounts/controller/types/keys.go b/modules/apps/27-interchain-accounts/controller/types/keys.go new file mode 100644 index 0000000000..238c8f603f --- /dev/null +++ b/modules/apps/27-interchain-accounts/controller/types/keys.go @@ -0,0 +1,9 @@ +package types + +const ( + // SubModuleName defines the interchain accounts controller module name + SubModuleName = "icacontroller" + + // StoreKey is the store key string for the interchain accounts controller module + StoreKey = SubModuleName +) diff --git a/modules/apps/27-interchain-accounts/controller/types/params.go b/modules/apps/27-interchain-accounts/controller/types/params.go new file mode 100644 index 0000000000..eb9c413bec --- /dev/null +++ b/modules/apps/27-interchain-accounts/controller/types/params.go @@ -0,0 +1,59 @@ +package types + +import ( + "fmt" + + paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" +) + +const ( + // DefaultControllerEnabled is the default value for the controller param (set to true) + DefaultControllerEnabled = true +) + +var ( + // KeyControllerEnabled is the store key for ControllerEnabled Params + KeyControllerEnabled = []byte("ControllerEnabled") +) + +// ParamKeyTable type declaration for parameters +func ParamKeyTable() paramtypes.KeyTable { + return paramtypes.NewKeyTable().RegisterParamSet(&Params{}) +} + +// NewParams creates a new parameter configuration for the controller submodule +func NewParams(enableController bool) Params { + return Params{ + ControllerEnabled: enableController, + } +} + +// DefaultParams is the default parameter configuration for the controller submodule +func DefaultParams() Params { + return NewParams(DefaultControllerEnabled) +} + +// Validate validates all controller submodule parameters +func (p Params) Validate() error { + if err := validateEnabled(p.ControllerEnabled); err != nil { + return err + } + + return nil +} + +// ParamSetPairs implements params.ParamSet +func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs { + return paramtypes.ParamSetPairs{ + paramtypes.NewParamSetPair(KeyControllerEnabled, p.ControllerEnabled, validateEnabled), + } +} + +func validateEnabled(i interface{}) error { + _, ok := i.(bool) + if !ok { + return fmt.Errorf("invalid parameter type: %T", i) + } + + return nil +} diff --git a/modules/apps/27-interchain-accounts/controller/types/params_test.go b/modules/apps/27-interchain-accounts/controller/types/params_test.go new file mode 100644 index 0000000000..0a25fd213a --- /dev/null +++ b/modules/apps/27-interchain-accounts/controller/types/params_test.go @@ -0,0 +1,14 @@ +package types_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/controller/types" +) + +func TestValidateParams(t *testing.T) { + require.NoError(t, types.DefaultParams().Validate()) + require.NoError(t, types.NewParams(false).Validate()) +} diff --git a/modules/apps/27-interchain-accounts/controller/types/query.pb.go b/modules/apps/27-interchain-accounts/controller/types/query.pb.go new file mode 100644 index 0000000000..659605862e --- /dev/null +++ b/modules/apps/27-interchain-accounts/controller/types/query.pb.go @@ -0,0 +1,547 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: ibc/applications/interchain_accounts/controller/v1/query.proto + +package types + +import ( + context "context" + fmt "fmt" + _ "github.com/gogo/protobuf/gogoproto" + grpc1 "github.com/gogo/protobuf/grpc" + proto "github.com/gogo/protobuf/proto" + _ "google.golang.org/genproto/googleapis/api/annotations" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" + io "io" + math "math" + math_bits "math/bits" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package + +// QueryParamsRequest is the request type for the Query/Params RPC method. +type QueryParamsRequest struct { +} + +func (m *QueryParamsRequest) Reset() { *m = QueryParamsRequest{} } +func (m *QueryParamsRequest) String() string { return proto.CompactTextString(m) } +func (*QueryParamsRequest) ProtoMessage() {} +func (*QueryParamsRequest) Descriptor() ([]byte, []int) { + return fileDescriptor_df0d8b259d72854e, []int{0} +} +func (m *QueryParamsRequest) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *QueryParamsRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_QueryParamsRequest.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *QueryParamsRequest) XXX_Merge(src proto.Message) { + xxx_messageInfo_QueryParamsRequest.Merge(m, src) +} +func (m *QueryParamsRequest) XXX_Size() int { + return m.Size() +} +func (m *QueryParamsRequest) XXX_DiscardUnknown() { + xxx_messageInfo_QueryParamsRequest.DiscardUnknown(m) +} + +var xxx_messageInfo_QueryParamsRequest proto.InternalMessageInfo + +// QueryParamsResponse is the response type for the Query/Params RPC method. +type QueryParamsResponse struct { + // params defines the parameters of the module. + Params *Params `protobuf:"bytes,1,opt,name=params,proto3" json:"params,omitempty"` +} + +func (m *QueryParamsResponse) Reset() { *m = QueryParamsResponse{} } +func (m *QueryParamsResponse) String() string { return proto.CompactTextString(m) } +func (*QueryParamsResponse) ProtoMessage() {} +func (*QueryParamsResponse) Descriptor() ([]byte, []int) { + return fileDescriptor_df0d8b259d72854e, []int{1} +} +func (m *QueryParamsResponse) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *QueryParamsResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_QueryParamsResponse.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *QueryParamsResponse) XXX_Merge(src proto.Message) { + xxx_messageInfo_QueryParamsResponse.Merge(m, src) +} +func (m *QueryParamsResponse) XXX_Size() int { + return m.Size() +} +func (m *QueryParamsResponse) XXX_DiscardUnknown() { + xxx_messageInfo_QueryParamsResponse.DiscardUnknown(m) +} + +var xxx_messageInfo_QueryParamsResponse proto.InternalMessageInfo + +func (m *QueryParamsResponse) GetParams() *Params { + if m != nil { + return m.Params + } + return nil +} + +func init() { + proto.RegisterType((*QueryParamsRequest)(nil), "ibc.applications.interchain_accounts.controller.v1.QueryParamsRequest") + proto.RegisterType((*QueryParamsResponse)(nil), "ibc.applications.interchain_accounts.controller.v1.QueryParamsResponse") +} + +func init() { + proto.RegisterFile("ibc/applications/interchain_accounts/controller/v1/query.proto", fileDescriptor_df0d8b259d72854e) +} + +var fileDescriptor_df0d8b259d72854e = []byte{ + // 326 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xa4, 0x91, 0x31, 0x4b, 0x3b, 0x31, + 0x18, 0xc6, 0x9b, 0x3f, 0xfc, 0x3b, 0x9c, 0xdb, 0xd9, 0x41, 0x8a, 0x1c, 0xd2, 0xc9, 0xa5, 0x79, + 0xe9, 0x29, 0x08, 0x1d, 0x1c, 0x14, 0x74, 0xad, 0x1d, 0x5d, 0x24, 0x17, 0x43, 0x1a, 0xb9, 0xcb, + 0x9b, 0x26, 0xb9, 0x42, 0x57, 0x3f, 0x81, 0xe0, 0x97, 0x72, 0x2c, 0x88, 0xe0, 0xa6, 0xb4, 0x7e, + 0x10, 0xe9, 0xe5, 0xa0, 0x15, 0x3b, 0x68, 0xdd, 0x42, 0x5e, 0x9e, 0xdf, 0xf3, 0x3c, 0x3c, 0xd1, + 0xa9, 0xca, 0x38, 0x30, 0x63, 0x72, 0xc5, 0x99, 0x57, 0xa8, 0x1d, 0x28, 0xed, 0x85, 0xe5, 0x23, + 0xa6, 0xf4, 0x0d, 0xe3, 0x1c, 0x4b, 0xed, 0x1d, 0x70, 0xd4, 0xde, 0x62, 0x9e, 0x0b, 0x0b, 0x93, + 0x1e, 0x8c, 0x4b, 0x61, 0xa7, 0xd4, 0x58, 0xf4, 0x18, 0xa7, 0x2a, 0xe3, 0x74, 0x5d, 0x4f, 0x37, + 0xe8, 0xe9, 0x4a, 0x4f, 0x27, 0xbd, 0x76, 0x4b, 0xa2, 0xc4, 0x4a, 0x0e, 0xcb, 0x57, 0x20, 0xb5, + 0xcf, 0xb7, 0x48, 0xb2, 0xc6, 0x0d, 0x90, 0x7d, 0x89, 0x28, 0x73, 0x01, 0xcc, 0x28, 0x60, 0x5a, + 0xa3, 0xaf, 0x43, 0x55, 0xd7, 0x4e, 0x2b, 0x8a, 0xaf, 0x96, 0xd9, 0x07, 0xcc, 0xb2, 0xc2, 0x0d, + 0xc5, 0xb8, 0x14, 0xce, 0x77, 0x54, 0xb4, 0xfb, 0xe5, 0xd7, 0x19, 0xd4, 0x4e, 0xc4, 0xc3, 0xa8, + 0x69, 0xaa, 0x9f, 0x3d, 0x72, 0x40, 0x0e, 0x77, 0xd2, 0x3e, 0xfd, 0x7d, 0x55, 0x5a, 0x33, 0x6b, + 0x52, 0xfa, 0x46, 0xa2, 0xff, 0x95, 0x57, 0xfc, 0x42, 0xa2, 0x66, 0x38, 0xc6, 0x17, 0xdb, 0x80, + 0xbf, 0xf7, 0x68, 0x5f, 0xfe, 0x99, 0x13, 0x9a, 0x77, 0xfa, 0xf7, 0xcf, 0x1f, 0x8f, 0xff, 0x8e, + 0xe3, 0x14, 0xea, 0x49, 0x7e, 0x32, 0x45, 0x68, 0x78, 0x76, 0xf7, 0x34, 0x4f, 0xc8, 0x6c, 0x9e, + 0x90, 0xf7, 0x79, 0x42, 0x1e, 0x16, 0x49, 0x63, 0xb6, 0x48, 0x1a, 0xaf, 0x8b, 0xa4, 0x71, 0x3d, + 0x90, 0xca, 0x8f, 0xca, 0x8c, 0x72, 0x2c, 0x80, 0xa3, 0x2b, 0xd0, 0x2d, 0xf1, 0x5d, 0x89, 0x30, + 0x49, 0xa1, 0xc0, 0xdb, 0x32, 0x17, 0x2e, 0x98, 0xa5, 0x27, 0xdd, 0x95, 0x5f, 0x77, 0x93, 0x9f, + 0x9f, 0x1a, 0xe1, 0xb2, 0x66, 0xb5, 0xea, 0xd1, 0x67, 0x00, 0x00, 0x00, 0xff, 0xff, 0xef, 0xec, + 0xb6, 0x2f, 0xc4, 0x02, 0x00, 0x00, +} + +// Reference imports to suppress errors if they are not otherwise used. +var _ context.Context +var _ grpc.ClientConn + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +const _ = grpc.SupportPackageIsVersion4 + +// QueryClient is the client API for Query service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. +type QueryClient interface { + // Params queries all parameters of the ICA controller submodule. + Params(ctx context.Context, in *QueryParamsRequest, opts ...grpc.CallOption) (*QueryParamsResponse, error) +} + +type queryClient struct { + cc grpc1.ClientConn +} + +func NewQueryClient(cc grpc1.ClientConn) QueryClient { + return &queryClient{cc} +} + +func (c *queryClient) Params(ctx context.Context, in *QueryParamsRequest, opts ...grpc.CallOption) (*QueryParamsResponse, error) { + out := new(QueryParamsResponse) + err := c.cc.Invoke(ctx, "/ibc.applications.interchain_accounts.controller.v1.Query/Params", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// QueryServer is the server API for Query service. +type QueryServer interface { + // Params queries all parameters of the ICA controller submodule. + Params(context.Context, *QueryParamsRequest) (*QueryParamsResponse, error) +} + +// UnimplementedQueryServer can be embedded to have forward compatible implementations. +type UnimplementedQueryServer struct { +} + +func (*UnimplementedQueryServer) Params(ctx context.Context, req *QueryParamsRequest) (*QueryParamsResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method Params not implemented") +} + +func RegisterQueryServer(s grpc1.Server, srv QueryServer) { + s.RegisterService(&_Query_serviceDesc, srv) +} + +func _Query_Params_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(QueryParamsRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(QueryServer).Params(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/ibc.applications.interchain_accounts.controller.v1.Query/Params", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(QueryServer).Params(ctx, req.(*QueryParamsRequest)) + } + return interceptor(ctx, in, info, handler) +} + +var _Query_serviceDesc = grpc.ServiceDesc{ + ServiceName: "ibc.applications.interchain_accounts.controller.v1.Query", + HandlerType: (*QueryServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "Params", + Handler: _Query_Params_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "ibc/applications/interchain_accounts/controller/v1/query.proto", +} + +func (m *QueryParamsRequest) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *QueryParamsRequest) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *QueryParamsRequest) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + return len(dAtA) - i, nil +} + +func (m *QueryParamsResponse) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *QueryParamsResponse) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *QueryParamsResponse) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.Params != nil { + { + size, err := m.Params.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintQuery(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func encodeVarintQuery(dAtA []byte, offset int, v uint64) int { + offset -= sovQuery(v) + base := offset + for v >= 1<<7 { + dAtA[offset] = uint8(v&0x7f | 0x80) + v >>= 7 + offset++ + } + dAtA[offset] = uint8(v) + return base +} +func (m *QueryParamsRequest) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + return n +} + +func (m *QueryParamsResponse) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.Params != nil { + l = m.Params.Size() + n += 1 + l + sovQuery(uint64(l)) + } + return n +} + +func sovQuery(x uint64) (n int) { + return (math_bits.Len64(x|1) + 6) / 7 +} +func sozQuery(x uint64) (n int) { + return sovQuery(uint64((x << 1) ^ uint64((int64(x) >> 63)))) +} +func (m *QueryParamsRequest) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQuery + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: QueryParamsRequest: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: QueryParamsRequest: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + default: + iNdEx = preIndex + skippy, err := skipQuery(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthQuery + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *QueryParamsResponse) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQuery + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: QueryParamsResponse: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: QueryParamsResponse: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Params", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQuery + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthQuery + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthQuery + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if m.Params == nil { + m.Params = &Params{} + } + if err := m.Params.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipQuery(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthQuery + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func skipQuery(dAtA []byte) (n int, err error) { + l := len(dAtA) + iNdEx := 0 + depth := 0 + for iNdEx < l { + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowQuery + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + wireType := int(wire & 0x7) + switch wireType { + case 0: + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowQuery + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + iNdEx++ + if dAtA[iNdEx-1] < 0x80 { + break + } + } + case 1: + iNdEx += 8 + case 2: + var length int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowQuery + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + length |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if length < 0 { + return 0, ErrInvalidLengthQuery + } + iNdEx += length + case 3: + depth++ + case 4: + if depth == 0 { + return 0, ErrUnexpectedEndOfGroupQuery + } + depth-- + case 5: + iNdEx += 4 + default: + return 0, fmt.Errorf("proto: illegal wireType %d", wireType) + } + if iNdEx < 0 { + return 0, ErrInvalidLengthQuery + } + if depth == 0 { + return iNdEx, nil + } + } + return 0, io.ErrUnexpectedEOF +} + +var ( + ErrInvalidLengthQuery = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowQuery = fmt.Errorf("proto: integer overflow") + ErrUnexpectedEndOfGroupQuery = fmt.Errorf("proto: unexpected end of group") +) diff --git a/modules/apps/27-interchain-accounts/controller/types/query.pb.gw.go b/modules/apps/27-interchain-accounts/controller/types/query.pb.gw.go new file mode 100644 index 0000000000..a9ac1bc03b --- /dev/null +++ b/modules/apps/27-interchain-accounts/controller/types/query.pb.gw.go @@ -0,0 +1,148 @@ +// Code generated by protoc-gen-grpc-gateway. DO NOT EDIT. +// source: ibc/applications/interchain_accounts/controller/v1/query.proto + +/* +Package types is a reverse proxy. + +It translates gRPC into RESTful JSON APIs. +*/ +package types + +import ( + "context" + "io" + "net/http" + + "github.com/golang/protobuf/descriptor" + "github.com/golang/protobuf/proto" + "github.com/grpc-ecosystem/grpc-gateway/runtime" + "github.com/grpc-ecosystem/grpc-gateway/utilities" + "google.golang.org/grpc" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/grpclog" + "google.golang.org/grpc/status" +) + +// Suppress "imported and not used" errors +var _ codes.Code +var _ io.Reader +var _ status.Status +var _ = runtime.String +var _ = utilities.NewDoubleArray +var _ = descriptor.ForMessage + +func request_Query_Params_0(ctx context.Context, marshaler runtime.Marshaler, client QueryClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq QueryParamsRequest + var metadata runtime.ServerMetadata + + msg, err := client.Params(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + +} + +func local_request_Query_Params_0(ctx context.Context, marshaler runtime.Marshaler, server QueryServer, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq QueryParamsRequest + var metadata runtime.ServerMetadata + + msg, err := server.Params(ctx, &protoReq) + return msg, metadata, err + +} + +// RegisterQueryHandlerServer registers the http handlers for service Query to "mux". +// UnaryRPC :call QueryServer directly. +// StreamingRPC :currently unsupported pending https://github.com/grpc/grpc-go/issues/906. +// Note that using this registration option will cause many gRPC library features (such as grpc.SendHeader, etc) to stop working. Consider using RegisterQueryHandlerFromEndpoint instead. +func RegisterQueryHandlerServer(ctx context.Context, mux *runtime.ServeMux, server QueryServer) error { + + mux.Handle("GET", pattern_Query_Params_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateIncomingContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := local_request_Query_Params_0(rctx, inboundMarshaler, server, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_Query_Params_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + + return nil +} + +// RegisterQueryHandlerFromEndpoint is same as RegisterQueryHandler but +// automatically dials to "endpoint" and closes the connection when "ctx" gets done. +func RegisterQueryHandlerFromEndpoint(ctx context.Context, mux *runtime.ServeMux, endpoint string, opts []grpc.DialOption) (err error) { + conn, err := grpc.Dial(endpoint, opts...) + if err != nil { + return err + } + defer func() { + if err != nil { + if cerr := conn.Close(); cerr != nil { + grpclog.Infof("Failed to close conn to %s: %v", endpoint, cerr) + } + return + } + go func() { + <-ctx.Done() + if cerr := conn.Close(); cerr != nil { + grpclog.Infof("Failed to close conn to %s: %v", endpoint, cerr) + } + }() + }() + + return RegisterQueryHandler(ctx, mux, conn) +} + +// RegisterQueryHandler registers the http handlers for service Query to "mux". +// The handlers forward requests to the grpc endpoint over "conn". +func RegisterQueryHandler(ctx context.Context, mux *runtime.ServeMux, conn *grpc.ClientConn) error { + return RegisterQueryHandlerClient(ctx, mux, NewQueryClient(conn)) +} + +// RegisterQueryHandlerClient registers the http handlers for service Query +// to "mux". The handlers forward requests to the grpc endpoint over the given implementation of "QueryClient". +// Note: the gRPC framework executes interceptors within the gRPC handler. If the passed in "QueryClient" +// doesn't go through the normal gRPC flow (creating a gRPC client etc.) then it will be up to the passed in +// "QueryClient" to call the correct interceptors. +func RegisterQueryHandlerClient(ctx context.Context, mux *runtime.ServeMux, client QueryClient) error { + + mux.Handle("GET", pattern_Query_Params_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := request_Query_Params_0(rctx, inboundMarshaler, client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_Query_Params_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + + return nil +} + +var ( + pattern_Query_Params_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2, 2, 3, 2, 4, 2, 5}, []string{"ibc", "apps", "interchain_accounts", "controller", "v1", "params"}, "", runtime.AssumeColonVerbOpt(true))) +) + +var ( + forward_Query_Params_0 = runtime.ForwardResponseMessage +) diff --git a/modules/apps/27-interchain-accounts/host/client/cli/cli.go b/modules/apps/27-interchain-accounts/host/client/cli/cli.go new file mode 100644 index 0000000000..9d88f4cba2 --- /dev/null +++ b/modules/apps/27-interchain-accounts/host/client/cli/cli.go @@ -0,0 +1,21 @@ +package cli + +import ( + "github.com/spf13/cobra" +) + +// GetQueryCmd returns the query commands for the ICA host submodule +func GetQueryCmd() *cobra.Command { + queryCmd := &cobra.Command{ + Use: "host", + Short: "interchain-accounts host subcommands", + DisableFlagParsing: true, + SuggestionsMinimumDistance: 2, + } + + queryCmd.AddCommand( + GetCmdParams(), + ) + + return queryCmd +} diff --git a/modules/apps/27-interchain-accounts/host/client/cli/query.go b/modules/apps/27-interchain-accounts/host/client/cli/query.go new file mode 100644 index 0000000000..822e4ec257 --- /dev/null +++ b/modules/apps/27-interchain-accounts/host/client/cli/query.go @@ -0,0 +1,37 @@ +package cli + +import ( + "fmt" + + "github.com/spf13/cobra" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/version" + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/host/types" +) + +// GetCmdParams returns the command handler for the host submodule parameter querying. +func GetCmdParams() *cobra.Command { + cmd := &cobra.Command{ + Use: "params", + Short: "Query the current interchain-accounts host submodule parameters", + Long: "Query the current interchain-accounts host submodule parameters", + Args: cobra.NoArgs, + Example: fmt.Sprintf("%s query interchain-accounts host params", version.AppName), + RunE: func(cmd *cobra.Command, _ []string) error { + clientCtx, err := client.GetClientQueryContext(cmd) + if err != nil { + return err + } + queryClient := types.NewQueryClient(clientCtx) + + res, _ := queryClient.Params(cmd.Context(), &types.QueryParamsRequest{}) + return clientCtx.PrintProto(res.Params) + }, + } + + flags.AddQueryFlagsToCmd(cmd) + + return cmd +} diff --git a/modules/apps/27-interchain-accounts/host/ibc_module.go b/modules/apps/27-interchain-accounts/host/ibc_module.go new file mode 100644 index 0000000000..b8dbc15d42 --- /dev/null +++ b/modules/apps/27-interchain-accounts/host/ibc_module.go @@ -0,0 +1,151 @@ +package host + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" + + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/host/keeper" + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/host/types" + icatypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" + channeltypes "github.com/cosmos/ibc-go/v2/modules/core/04-channel/types" + ibcexported "github.com/cosmos/ibc-go/v2/modules/core/exported" +) + +// IBCModule implements the ICS26 interface for interchain accounts host chains +type IBCModule struct { + keeper keeper.Keeper +} + +// NewIBCModule creates a new IBCModule given the associated keeper +func NewIBCModule(k keeper.Keeper) IBCModule { + return IBCModule{ + keeper: k, + } +} + +// OnChanOpenInit implements the IBCModule interface +func (im IBCModule) OnChanOpenInit( + ctx sdk.Context, + order channeltypes.Order, + connectionHops []string, + portID string, + channelID string, + chanCap *capabilitytypes.Capability, + counterparty channeltypes.Counterparty, + version string, +) error { + return sdkerrors.Wrap(icatypes.ErrInvalidChannelFlow, "channel handshake must be initiated by controller chain") +} + +// OnChanOpenTry implements the IBCModule interface +func (im IBCModule) OnChanOpenTry( + ctx sdk.Context, + order channeltypes.Order, + connectionHops []string, + portID, + channelID string, + chanCap *capabilitytypes.Capability, + counterparty channeltypes.Counterparty, + version, + counterpartyVersion string, +) error { + if !im.keeper.IsHostEnabled(ctx) { + return types.ErrHostSubModuleDisabled + } + + return im.keeper.OnChanOpenTry(ctx, order, connectionHops, portID, channelID, chanCap, counterparty, version, counterpartyVersion) +} + +// OnChanOpenAck implements the IBCModule interface +func (im IBCModule) OnChanOpenAck( + ctx sdk.Context, + portID, + channelID string, + counterpartyVersion string, +) error { + return sdkerrors.Wrap(icatypes.ErrInvalidChannelFlow, "channel handshake must be initiated by controller chain") +} + +// OnChanOpenAck implements the IBCModule interface +func (im IBCModule) OnChanOpenConfirm( + ctx sdk.Context, + portID, + channelID string, +) error { + if !im.keeper.IsHostEnabled(ctx) { + return types.ErrHostSubModuleDisabled + } + + return im.keeper.OnChanOpenConfirm(ctx, portID, channelID) +} + +// OnChanCloseInit implements the IBCModule interface +func (im IBCModule) OnChanCloseInit( + ctx sdk.Context, + portID, + channelID string, +) error { + // Disallow user-initiated channel closing for interchain account channels + return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "user cannot close channel") +} + +// OnChanCloseConfirm implements the IBCModule interface +func (im IBCModule) OnChanCloseConfirm( + ctx sdk.Context, + portID, + channelID string, +) error { + return im.keeper.OnChanCloseConfirm(ctx, portID, channelID) +} + +// OnRecvPacket implements the IBCModule interface +func (im IBCModule) OnRecvPacket( + ctx sdk.Context, + packet channeltypes.Packet, + _ sdk.AccAddress, +) ibcexported.Acknowledgement { + if !im.keeper.IsHostEnabled(ctx) { + return channeltypes.NewErrorAcknowledgement(types.ErrHostSubModuleDisabled.Error()) + } + + ack := channeltypes.NewResultAcknowledgement([]byte{byte(1)}) + + if err := im.keeper.OnRecvPacket(ctx, packet); err != nil { + ack = channeltypes.NewErrorAcknowledgement(err.Error()) + } + + // NOTE: acknowledgement will be written synchronously during IBC handler execution. + return ack +} + +// OnAcknowledgementPacket implements the IBCModule interface +func (im IBCModule) OnAcknowledgementPacket( + ctx sdk.Context, + packet channeltypes.Packet, + acknowledgement []byte, + relayer sdk.AccAddress, +) error { + return sdkerrors.Wrap(icatypes.ErrInvalidChannelFlow, "cannot receive acknowledgement on a host channel end, a host chain does not send a packet over the channel") +} + +// OnTimeoutPacket implements the IBCModule interface +func (im IBCModule) OnTimeoutPacket( + ctx sdk.Context, + packet channeltypes.Packet, + relayer sdk.AccAddress, +) error { + return sdkerrors.Wrap(icatypes.ErrInvalidChannelFlow, "cannot cause a packet timeout on a host channel end, a host chain does not send a packet over the channel") +} + +// NegotiateAppVersion implements the IBCModule interface +func (im IBCModule) NegotiateAppVersion( + ctx sdk.Context, + order channeltypes.Order, + connectionID string, + portID string, + counterparty channeltypes.Counterparty, + proposedVersion string, +) (string, error) { + return im.keeper.NegotiateAppVersion(ctx, order, connectionID, portID, counterparty, proposedVersion) +} diff --git a/modules/apps/27-interchain-accounts/host/ibc_module_test.go b/modules/apps/27-interchain-accounts/host/ibc_module_test.go new file mode 100644 index 0000000000..823ae74b8a --- /dev/null +++ b/modules/apps/27-interchain-accounts/host/ibc_module_test.go @@ -0,0 +1,644 @@ +package host_test + +import ( + "fmt" + "testing" + + sdk "github.com/cosmos/cosmos-sdk/types" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" + "github.com/stretchr/testify/suite" + "github.com/tendermint/tendermint/crypto" + + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/host/types" + icatypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" + clienttypes "github.com/cosmos/ibc-go/v2/modules/core/02-client/types" + channeltypes "github.com/cosmos/ibc-go/v2/modules/core/04-channel/types" + host "github.com/cosmos/ibc-go/v2/modules/core/24-host" + "github.com/cosmos/ibc-go/v2/modules/core/exported" + ibctesting "github.com/cosmos/ibc-go/v2/testing" +) + +var ( + // TestAccAddress defines a resuable bech32 address for testing purposes + // TODO: update crypto.AddressHash() when sdk uses address.Module() + TestAccAddress = icatypes.GenerateAddress(sdk.AccAddress(crypto.AddressHash([]byte(icatypes.ModuleName))), TestPortID) + // TestOwnerAddress defines a reusable bech32 address for testing purposes + TestOwnerAddress = "cosmos17dtl0mjt3t77kpuhg2edqzjpszulwhgzuj9ljs" + // TestPortID defines a resuable port identifier for testing purposes + TestPortID, _ = icatypes.GeneratePortID(TestOwnerAddress, ibctesting.FirstConnectionID, ibctesting.FirstConnectionID) + // TestVersion defines a resuable interchainaccounts version string for testing purposes + TestVersion = icatypes.NewAppVersion(icatypes.VersionPrefix, TestAccAddress.String()) +) + +type InterchainAccountsTestSuite struct { + suite.Suite + + coordinator *ibctesting.Coordinator + + // testing chains used for convenience and readability + chainA *ibctesting.TestChain + chainB *ibctesting.TestChain +} + +func TestICATestSuite(t *testing.T) { + suite.Run(t, new(InterchainAccountsTestSuite)) +} + +func (suite *InterchainAccountsTestSuite) SetupTest() { + suite.coordinator = ibctesting.NewCoordinator(suite.T(), 2) + suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(0)) + suite.chainB = suite.coordinator.GetChain(ibctesting.GetChainID(1)) +} + +func NewICAPath(chainA, chainB *ibctesting.TestChain) *ibctesting.Path { + path := ibctesting.NewPath(chainA, chainB) + path.EndpointA.ChannelConfig.PortID = icatypes.PortID + path.EndpointB.ChannelConfig.PortID = icatypes.PortID + path.EndpointA.ChannelConfig.Order = channeltypes.ORDERED + path.EndpointB.ChannelConfig.Order = channeltypes.ORDERED + path.EndpointA.ChannelConfig.Version = icatypes.VersionPrefix + path.EndpointB.ChannelConfig.Version = TestVersion + + return path +} + +func InitInterchainAccount(endpoint *ibctesting.Endpoint, owner string) error { + portID, err := icatypes.GeneratePortID(owner, endpoint.ConnectionID, endpoint.Counterparty.ConnectionID) + if err != nil { + return err + } + + channelSequence := endpoint.Chain.App.GetIBCKeeper().ChannelKeeper.GetNextChannelSequence(endpoint.Chain.GetContext()) + + if err := endpoint.Chain.GetSimApp().ICAControllerKeeper.InitInterchainAccount(endpoint.Chain.GetContext(), endpoint.ConnectionID, endpoint.Counterparty.ConnectionID, owner); err != nil { + return err + } + + // commit state changes for proof verification + endpoint.Chain.App.Commit() + endpoint.Chain.NextBlock() + + // update port/channel ids + endpoint.ChannelID = channeltypes.FormatChannelIdentifier(channelSequence) + endpoint.ChannelConfig.PortID = portID + + return nil +} + +// SetupICAPath invokes the InterchainAccounts entrypoint and subsequent channel handshake handlers +func SetupICAPath(path *ibctesting.Path, owner string) error { + if err := InitInterchainAccount(path.EndpointA, owner); err != nil { + return err + } + + if err := path.EndpointB.ChanOpenTry(); err != nil { + return err + } + + if err := path.EndpointA.ChanOpenAck(); err != nil { + return err + } + + if err := path.EndpointB.ChanOpenConfirm(); err != nil { + return err + } + + return nil +} + +// Test initiating a ChanOpenInit using the host chain instead of the controller chain +// ChainA is the controller chain. ChainB is the host chain +func (suite *InterchainAccountsTestSuite) TestChanOpenInit() { + suite.SetupTest() // reset + path := NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + // use chainB (host) for ChanOpenInit + msg := channeltypes.NewMsgChannelOpenInit(path.EndpointB.ChannelConfig.PortID, icatypes.VersionPrefix, channeltypes.ORDERED, []string{path.EndpointB.ConnectionID}, path.EndpointA.ChannelConfig.PortID, icatypes.ModuleName) + handler := suite.chainB.GetSimApp().MsgServiceRouter().Handler(msg) + _, err := handler(suite.chainB.GetContext(), msg) + + suite.Require().Error(err) +} + +func (suite *InterchainAccountsTestSuite) TestOnChanOpenTry() { + var ( + path *ibctesting.Path + channel *channeltypes.Channel + ) + + testCases := []struct { + name string + malleate func() + expPass bool + }{ + + { + "success", func() {}, true, + }, + { + "host submodule disabled", func() { + suite.chainB.GetSimApp().ICAHostKeeper.SetParams(suite.chainB.GetContext(), types.NewParams(false, []string{})) + }, false, + }, + { + "success: ICA auth module callback returns error", func() { + // mock module callback should not be called on host side + suite.chainB.GetSimApp().ICAAuthModule.IBCApp.OnChanOpenTry = func(ctx sdk.Context, order channeltypes.Order, connectionHops []string, + portID, channelID string, chanCap *capabilitytypes.Capability, + counterparty channeltypes.Counterparty, version, counterpartyVersion string, + ) error { + return fmt.Errorf("mock ica auth fails") + } + + }, true, + }, + { + "ICA callback fails - invalid version", func() { + channel.Version = icatypes.VersionPrefix + }, false, + }, + } + + for _, tc := range testCases { + tc := tc + + suite.Run(tc.name, func() { + suite.SetupTest() // reset + + path = NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := InitInterchainAccount(path.EndpointA, TestOwnerAddress) + suite.Require().NoError(err) + path.EndpointB.ChannelID = ibctesting.FirstChannelID + + // default values + counterparty := channeltypes.NewCounterparty(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) + channel = &channeltypes.Channel{ + State: channeltypes.TRYOPEN, + Ordering: channeltypes.ORDERED, + Counterparty: counterparty, + ConnectionHops: []string{path.EndpointB.ConnectionID}, + Version: TestVersion, + } + + tc.malleate() + + // ensure channel on chainB is set in state + suite.chainB.GetSimApp().IBCKeeper.ChannelKeeper.SetChannel(suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, *channel) + + module, _, err := suite.chainB.App.GetIBCKeeper().PortKeeper.LookupModuleByPort(suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID) + suite.Require().NoError(err) + + chanCap, err := suite.chainB.App.GetScopedIBCKeeper().NewCapability(suite.chainB.GetContext(), host.ChannelCapabilityPath(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID)) + suite.Require().NoError(err) + + cbs, ok := suite.chainB.App.GetIBCKeeper().Router.GetRoute(module) + suite.Require().True(ok) + + err = cbs.OnChanOpenTry(suite.chainB.GetContext(), channel.Ordering, channel.GetConnectionHops(), + path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, chanCap, channel.Counterparty, channel.GetVersion(), path.EndpointA.ChannelConfig.Version, + ) + + if tc.expPass { + suite.Require().NoError(err) + } else { + suite.Require().Error(err) + } + + }) + } + +} + +// Test initiating a ChanOpenAck using the host chain instead of the controller chain +// ChainA is the controller chain. ChainB is the host chain +func (suite *InterchainAccountsTestSuite) TestChanOpenAck() { + suite.SetupTest() // reset + path := NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := InitInterchainAccount(path.EndpointA, TestOwnerAddress) + suite.Require().NoError(err) + + err = path.EndpointB.ChanOpenTry() + suite.Require().NoError(err) + + // chainA maliciously sets channel to TRYOPEN + channel := channeltypes.NewChannel(channeltypes.TRYOPEN, channeltypes.ORDERED, channeltypes.NewCounterparty(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID), []string{path.EndpointA.ConnectionID}, TestVersion) + suite.chainA.GetSimApp().GetIBCKeeper().ChannelKeeper.SetChannel(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, channel) + + // commit state changes so proof can be created + suite.chainA.App.Commit() + suite.chainA.NextBlock() + + path.EndpointB.UpdateClient() + + // query proof from ChainA + channelKey := host.ChannelKey(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) + proofTry, proofHeight := path.EndpointA.Chain.QueryProof(channelKey) + + // use chainB (host) for ChanOpenAck + msg := channeltypes.NewMsgChannelOpenAck(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, path.EndpointA.ChannelID, TestVersion, proofTry, proofHeight, icatypes.ModuleName) + handler := suite.chainB.GetSimApp().MsgServiceRouter().Handler(msg) + _, err = handler(suite.chainB.GetContext(), msg) + + suite.Require().Error(err) +} + +func (suite *InterchainAccountsTestSuite) TestOnChanOpenConfirm() { + testCases := []struct { + name string + malleate func() + expPass bool + }{ + + { + "success", func() {}, true, + }, + { + "host submodule disabled", func() { + suite.chainB.GetSimApp().ICAHostKeeper.SetParams(suite.chainB.GetContext(), types.NewParams(false, []string{})) + }, false, + }, + { + "success: ICA auth module callback returns error", func() { + // mock module callback should not be called on host side + suite.chainB.GetSimApp().ICAAuthModule.IBCApp.OnChanOpenConfirm = func( + ctx sdk.Context, portID, channelID string, + ) error { + return fmt.Errorf("mock ica auth fails") + } + + }, true, + }, + } + + for _, tc := range testCases { + tc := tc + + suite.Run(tc.name, func() { + suite.SetupTest() + path := NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := InitInterchainAccount(path.EndpointA, TestOwnerAddress) + suite.Require().NoError(err) + + err = path.EndpointB.ChanOpenTry() + suite.Require().NoError(err) + + err = path.EndpointA.ChanOpenAck() + suite.Require().NoError(err) + + tc.malleate() + + module, _, err := suite.chainB.App.GetIBCKeeper().PortKeeper.LookupModuleByPort(suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID) + suite.Require().NoError(err) + + cbs, ok := suite.chainB.App.GetIBCKeeper().Router.GetRoute(module) + suite.Require().True(ok) + + err = cbs.OnChanOpenConfirm(suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) + + if tc.expPass { + suite.Require().NoError(err) + } else { + suite.Require().Error(err) + } + + }) + } + +} + +// OnChanCloseInit on host (chainB) +func (suite *InterchainAccountsTestSuite) TestOnChanCloseInit() { + path := NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := SetupICAPath(path, TestOwnerAddress) + suite.Require().NoError(err) + + module, _, err := suite.chainB.App.GetIBCKeeper().PortKeeper.LookupModuleByPort(suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID) + suite.Require().NoError(err) + + cbs, ok := suite.chainB.App.GetIBCKeeper().Router.GetRoute(module) + suite.Require().True(ok) + + err = cbs.OnChanCloseInit( + suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, + ) + + suite.Require().Error(err) +} + +func (suite *InterchainAccountsTestSuite) TestOnChanCloseConfirm() { + var ( + path *ibctesting.Path + ) + + testCases := []struct { + name string + malleate func() + expPass bool + }{ + + { + "success", func() {}, true, + }, + } + + for _, tc := range testCases { + suite.Run(tc.name, func() { + suite.SetupTest() // reset + + path = NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := SetupICAPath(path, TestOwnerAddress) + suite.Require().NoError(err) + + tc.malleate() // malleate mutates test data + module, _, err := suite.chainB.App.GetIBCKeeper().PortKeeper.LookupModuleByPort(suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID) + suite.Require().NoError(err) + + cbs, ok := suite.chainB.App.GetIBCKeeper().Router.GetRoute(module) + suite.Require().True(ok) + + err = cbs.OnChanCloseConfirm( + suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) + + activeChannelID, found := suite.chainB.GetSimApp().ICAHostKeeper.GetActiveChannelID(suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID) + + if tc.expPass { + suite.Require().NoError(err) + suite.Require().False(found) + suite.Require().Empty(activeChannelID) + } else { + suite.Require().Error(err) + } + + }) + } +} + +func (suite *InterchainAccountsTestSuite) TestOnRecvPacket() { + var ( + packetData []byte + ) + testCases := []struct { + name string + malleate func() + expAckSuccess bool + }{ + { + "success", func() {}, true, + }, + { + "host submodule disabled", func() { + suite.chainB.GetSimApp().ICAHostKeeper.SetParams(suite.chainB.GetContext(), types.NewParams(false, []string{})) + }, false, + }, + { + "success with ICA auth module callback failure", func() { + suite.chainB.GetSimApp().ICAAuthModule.IBCApp.OnRecvPacket = func( + ctx sdk.Context, packet channeltypes.Packet, relayer sdk.AccAddress, + ) exported.Acknowledgement { + return channeltypes.NewErrorAcknowledgement("failed OnRecvPacket mock callback") + } + }, true, + }, + { + "ICA OnRecvPacket fails - cannot unmarshal packet data", func() { + packetData = []byte("invalid data") + }, false, + }, + } + + for _, tc := range testCases { + tc := tc + + suite.Run(tc.name, func() { + suite.SetupTest() // reset + + path := NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + err := SetupICAPath(path, TestOwnerAddress) + suite.Require().NoError(err) + + // send 100stake to interchain account wallet + amount, _ := sdk.ParseCoinsNormalized("100stake") + interchainAccountAddr, _ := suite.chainB.GetSimApp().ICAHostKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), path.EndpointA.ChannelConfig.PortID) + bankMsg := &banktypes.MsgSend{FromAddress: suite.chainB.SenderAccount.GetAddress().String(), ToAddress: interchainAccountAddr, Amount: amount} + + _, err = suite.chainB.SendMsgs(bankMsg) + suite.Require().NoError(err) + + // build packet data + msg := &banktypes.MsgSend{ + FromAddress: interchainAccountAddr, + ToAddress: suite.chainB.SenderAccount.GetAddress().String(), + Amount: amount, + } + data, err := icatypes.SerializeCosmosTx(suite.chainA.Codec, []sdk.Msg{msg}) + suite.Require().NoError(err) + + icaPacketData := icatypes.InterchainAccountPacketData{ + Type: icatypes.EXECUTE_TX, + Data: data, + } + packetData = icaPacketData.GetBytes() + + params := types.NewParams(true, []string{sdk.MsgTypeURL(msg)}) + suite.chainB.GetSimApp().ICAHostKeeper.SetParams(suite.chainB.GetContext(), params) + + // malleate packetData for test cases + tc.malleate() + + seq := uint64(1) + packet := channeltypes.NewPacket(packetData, seq, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.NewHeight(0, 100), 0) + + tc.malleate() + + module, _, err := suite.chainB.App.GetIBCKeeper().PortKeeper.LookupModuleByPort(suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID) + suite.Require().NoError(err) + + cbs, ok := suite.chainB.App.GetIBCKeeper().Router.GetRoute(module) + suite.Require().True(ok) + + ack := cbs.OnRecvPacket(suite.chainB.GetContext(), packet, nil) + suite.Require().Equal(tc.expAckSuccess, ack.Success()) + + }) + } + +} + +func (suite *InterchainAccountsTestSuite) TestOnAcknowledgementPacket() { + + testCases := []struct { + name string + malleate func() + expPass bool + }{ + { + "ICA OnAcknowledgementPacket fails with ErrInvalidChannelFlow", func() {}, false, + }, + } + + for _, tc := range testCases { + tc := tc + + suite.Run(tc.name, func() { + suite.SetupTest() // reset + + path := NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := SetupICAPath(path, TestOwnerAddress) + suite.Require().NoError(err) + + tc.malleate() // malleate mutates test data + + module, _, err := suite.chainB.App.GetIBCKeeper().PortKeeper.LookupModuleByPort(suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID) + suite.Require().NoError(err) + + cbs, ok := suite.chainB.App.GetIBCKeeper().Router.GetRoute(module) + suite.Require().True(ok) + + packet := channeltypes.NewPacket( + []byte("empty packet data"), + suite.chainA.SenderAccount.GetSequence(), + path.EndpointB.ChannelConfig.PortID, + path.EndpointB.ChannelID, + path.EndpointA.ChannelConfig.PortID, + path.EndpointA.ChannelID, + clienttypes.NewHeight(0, 100), + 0, + ) + + err = cbs.OnAcknowledgementPacket(suite.chainB.GetContext(), packet, []byte("ackBytes"), TestAccAddress) + + if tc.expPass { + suite.Require().NoError(err) + } else { + suite.Require().Error(err) + } + }) + } +} + +func (suite *InterchainAccountsTestSuite) TestOnTimeoutPacket() { + + testCases := []struct { + name string + malleate func() + expPass bool + }{ + { + "ICA OnTimeoutPacket fails with ErrInvalidChannelFlow", func() {}, false, + }, + } + + for _, tc := range testCases { + tc := tc + + suite.Run(tc.name, func() { + suite.SetupTest() // reset + + path := NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := SetupICAPath(path, TestOwnerAddress) + suite.Require().NoError(err) + + tc.malleate() // malleate mutates test data + + module, _, err := suite.chainA.App.GetIBCKeeper().PortKeeper.LookupModuleByPort(suite.chainA.GetContext(), path.EndpointB.ChannelConfig.PortID) + suite.Require().NoError(err) + + cbs, ok := suite.chainA.App.GetIBCKeeper().Router.GetRoute(module) + suite.Require().True(ok) + + packet := channeltypes.NewPacket( + []byte("empty packet data"), + suite.chainA.SenderAccount.GetSequence(), + path.EndpointB.ChannelConfig.PortID, + path.EndpointB.ChannelID, + path.EndpointA.ChannelConfig.PortID, + path.EndpointA.ChannelID, + clienttypes.NewHeight(0, 100), + 0, + ) + + err = cbs.OnTimeoutPacket(suite.chainA.GetContext(), packet, TestAccAddress) + + if tc.expPass { + suite.Require().NoError(err) + } else { + suite.Require().Error(err) + } + }) + } +} + +func (suite *InterchainAccountsTestSuite) TestNegotiateAppVersion() { + var ( + proposedVersion string + ) + testCases := []struct { + name string + malleate func() + expPass bool + }{ + { + "success", func() {}, true, + }, + { + "invalid proposed version", func() { + proposedVersion = "invalid version" + }, false, + }, + } + + for _, tc := range testCases { + tc := tc + + suite.Run(tc.name, func() { + suite.SetupTest() + path := NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + module, _, err := suite.chainA.GetSimApp().GetIBCKeeper().PortKeeper.LookupModuleByPort(suite.chainA.GetContext(), icatypes.PortID) + suite.Require().NoError(err) + + cbs, ok := suite.chainA.GetSimApp().GetIBCKeeper().Router.GetRoute(module) + suite.Require().True(ok) + + counterpartyPortID, err := icatypes.GeneratePortID(TestOwnerAddress, path.EndpointA.ConnectionID, path.EndpointB.ConnectionID) + suite.Require().NoError(err) + + counterparty := &channeltypes.Counterparty{ + PortId: counterpartyPortID, + ChannelId: path.EndpointB.ChannelID, + } + + proposedVersion = icatypes.VersionPrefix + + tc.malleate() + + version, err := cbs.NegotiateAppVersion(suite.chainA.GetContext(), channeltypes.ORDERED, path.EndpointA.ConnectionID, icatypes.PortID, *counterparty, proposedVersion) + if tc.expPass { + suite.Require().NoError(err) + suite.Require().NoError(icatypes.ValidateVersion(version)) + suite.Require().Equal(TestVersion, version) + } else { + suite.Require().Error(err) + suite.Require().Empty(version) + } + }) + } +} diff --git a/modules/apps/27-interchain-accounts/host/keeper/account.go b/modules/apps/27-interchain-accounts/host/keeper/account.go new file mode 100644 index 0000000000..4ed947eb28 --- /dev/null +++ b/modules/apps/27-interchain-accounts/host/keeper/account.go @@ -0,0 +1,26 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + + icatypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" +) + +// RegisterInterchainAccount attempts to create a new account using the provided address and stores it in state keyed by the provided port identifier +// If an account for the provided address already exists this function returns early (no-op) +func (k Keeper) RegisterInterchainAccount(ctx sdk.Context, accAddr sdk.AccAddress, controllerPortID string) { + if acc := k.accountKeeper.GetAccount(ctx, accAddr); acc != nil { + return + } + + interchainAccount := icatypes.NewInterchainAccount( + authtypes.NewBaseAccountWithAddress(accAddr), + controllerPortID, + ) + + k.accountKeeper.NewAccount(ctx, interchainAccount) + k.accountKeeper.SetAccount(ctx, interchainAccount) + + k.SetInterchainAccountAddress(ctx, controllerPortID, interchainAccount.Address) +} diff --git a/modules/apps/27-interchain-accounts/host/keeper/account_test.go b/modules/apps/27-interchain-accounts/host/keeper/account_test.go new file mode 100644 index 0000000000..90be30ff2c --- /dev/null +++ b/modules/apps/27-interchain-accounts/host/keeper/account_test.go @@ -0,0 +1,33 @@ +package keeper_test + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + + icatypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" + ibctesting "github.com/cosmos/ibc-go/v2/testing" +) + +func (suite *KeeperTestSuite) TestRegisterInterchainAccount() { + suite.SetupTest() + + path := NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + // InitInterchainAccount + err := SetupICAPath(path, TestOwnerAddress) + suite.Require().NoError(err) + + portID, err := icatypes.GeneratePortID(TestOwnerAddress, ibctesting.FirstConnectionID, ibctesting.FirstConnectionID) + suite.Require().NoError(err) + + // Get the address of the interchain account stored in state during handshake step + storedAddr, found := suite.chainB.GetSimApp().ICAHostKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), portID) + suite.Require().True(found) + + icaAddr, err := sdk.AccAddressFromBech32(storedAddr) + suite.Require().NoError(err) + + // Check if account is created + interchainAccount := suite.chainB.GetSimApp().AccountKeeper.GetAccount(suite.chainB.GetContext(), icaAddr) + suite.Require().Equal(interchainAccount.GetAddress().String(), storedAddr) +} diff --git a/modules/apps/27-interchain-accounts/host/keeper/genesis.go b/modules/apps/27-interchain-accounts/host/keeper/genesis.go new file mode 100644 index 0000000000..12174ca3f2 --- /dev/null +++ b/modules/apps/27-interchain-accounts/host/keeper/genesis.go @@ -0,0 +1,40 @@ +package keeper + +import ( + "fmt" + + sdk "github.com/cosmos/cosmos-sdk/types" + + icatypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" + host "github.com/cosmos/ibc-go/v2/modules/core/24-host" +) + +// InitGenesis initializes the interchain accounts host application state from a provided genesis state +func InitGenesis(ctx sdk.Context, keeper Keeper, state icatypes.HostGenesisState) { + if !keeper.IsBound(ctx, state.Port) { + cap := keeper.BindPort(ctx, state.Port) + if err := keeper.ClaimCapability(ctx, cap, host.PortPath(state.Port)); err != nil { + panic(fmt.Sprintf("could not claim port capability: %v", err)) + } + } + + for _, ch := range state.ActiveChannels { + keeper.SetActiveChannelID(ctx, ch.PortId, ch.ChannelId) + } + + for _, acc := range state.InterchainAccounts { + keeper.SetInterchainAccountAddress(ctx, acc.PortId, acc.AccountAddress) + } + + keeper.SetParams(ctx, state.Params) +} + +// ExportGenesis returns the interchain accounts host exported genesis +func ExportGenesis(ctx sdk.Context, keeper Keeper) icatypes.HostGenesisState { + return icatypes.NewHostGenesisState( + keeper.GetAllActiveChannels(ctx), + keeper.GetAllInterchainAccounts(ctx), + icatypes.PortID, + keeper.GetParams(ctx), + ) +} diff --git a/modules/apps/27-interchain-accounts/host/keeper/genesis_test.go b/modules/apps/27-interchain-accounts/host/keeper/genesis_test.go new file mode 100644 index 0000000000..ba1e7e252c --- /dev/null +++ b/modules/apps/27-interchain-accounts/host/keeper/genesis_test.go @@ -0,0 +1,65 @@ +package keeper_test + +import ( + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/host/keeper" + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/host/types" + icatypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" + ibctesting "github.com/cosmos/ibc-go/v2/testing" +) + +func (suite *KeeperTestSuite) TestInitGenesis() { + suite.SetupTest() + + genesisState := icatypes.HostGenesisState{ + ActiveChannels: []icatypes.ActiveChannel{ + { + PortId: TestPortID, + ChannelId: ibctesting.FirstChannelID, + }, + }, + InterchainAccounts: []icatypes.RegisteredInterchainAccount{ + { + PortId: TestPortID, + AccountAddress: TestAccAddress.String(), + }, + }, + Port: icatypes.PortID, + } + + keeper.InitGenesis(suite.chainA.GetContext(), suite.chainA.GetSimApp().ICAHostKeeper, genesisState) + + channelID, found := suite.chainA.GetSimApp().ICAHostKeeper.GetActiveChannelID(suite.chainA.GetContext(), TestPortID) + suite.Require().True(found) + suite.Require().Equal(ibctesting.FirstChannelID, channelID) + + accountAdrr, found := suite.chainA.GetSimApp().ICAHostKeeper.GetInterchainAccountAddress(suite.chainA.GetContext(), TestPortID) + suite.Require().True(found) + suite.Require().Equal(TestAccAddress.String(), accountAdrr) + + expParams := types.NewParams(false, nil) + params := suite.chainA.GetSimApp().ICAHostKeeper.GetParams(suite.chainA.GetContext()) + suite.Require().Equal(expParams, params) +} + +func (suite *KeeperTestSuite) TestExportGenesis() { + suite.SetupTest() + + path := NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := SetupICAPath(path, TestOwnerAddress) + suite.Require().NoError(err) + + genesisState := keeper.ExportGenesis(suite.chainB.GetContext(), suite.chainB.GetSimApp().ICAHostKeeper) + + suite.Require().Equal(path.EndpointB.ChannelID, genesisState.ActiveChannels[0].ChannelId) + suite.Require().Equal(path.EndpointB.ChannelConfig.PortID, genesisState.ActiveChannels[0].PortId) + + suite.Require().Equal(TestAccAddress.String(), genesisState.InterchainAccounts[0].AccountAddress) + suite.Require().Equal(path.EndpointA.ChannelConfig.PortID, genesisState.InterchainAccounts[0].PortId) + + suite.Require().Equal(icatypes.PortID, genesisState.GetPort()) + + expParams := types.DefaultParams() + suite.Require().Equal(expParams, genesisState.GetParams()) +} diff --git a/modules/apps/27-interchain-accounts/host/keeper/grpc_query.go b/modules/apps/27-interchain-accounts/host/keeper/grpc_query.go new file mode 100644 index 0000000000..5cb4b569b0 --- /dev/null +++ b/modules/apps/27-interchain-accounts/host/keeper/grpc_query.go @@ -0,0 +1,19 @@ +package keeper + +import ( + "context" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/host/types" +) + +var _ types.QueryServer = Keeper{} + +// Params implements the Query/Params gRPC method +func (q Keeper) Params(c context.Context, _ *types.QueryParamsRequest) (*types.QueryParamsResponse, error) { + ctx := sdk.UnwrapSDKContext(c) + params := q.GetParams(ctx) + + return &types.QueryParamsResponse{ + Params: ¶ms, + }, nil +} diff --git a/modules/apps/27-interchain-accounts/host/keeper/grpc_query_test.go b/modules/apps/27-interchain-accounts/host/keeper/grpc_query_test.go new file mode 100644 index 0000000000..123679f7b8 --- /dev/null +++ b/modules/apps/27-interchain-accounts/host/keeper/grpc_query_test.go @@ -0,0 +1,14 @@ +package keeper_test + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/host/types" +) + +func (suite *KeeperTestSuite) TestQueryParams() { + ctx := sdk.WrapSDKContext(suite.chainA.GetContext()) + expParams := types.DefaultParams() + res, _ := suite.chainA.GetSimApp().ICAHostKeeper.Params(ctx, &types.QueryParamsRequest{}) + suite.Require().Equal(&expParams, res.Params) +} diff --git a/modules/apps/27-interchain-accounts/host/keeper/handshake.go b/modules/apps/27-interchain-accounts/host/keeper/handshake.go new file mode 100644 index 0000000000..c4c40e68d8 --- /dev/null +++ b/modules/apps/27-interchain-accounts/host/keeper/handshake.go @@ -0,0 +1,137 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" + + icatypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" + connectiontypes "github.com/cosmos/ibc-go/v2/modules/core/03-connection/types" + channeltypes "github.com/cosmos/ibc-go/v2/modules/core/04-channel/types" + porttypes "github.com/cosmos/ibc-go/v2/modules/core/05-port/types" + host "github.com/cosmos/ibc-go/v2/modules/core/24-host" +) + +// OnChanOpenTry performs basic validation of the ICA channel +// and registers a new interchain account (if it doesn't exist). +func (k Keeper) OnChanOpenTry( + ctx sdk.Context, + order channeltypes.Order, + connectionHops []string, + portID, + channelID string, + chanCap *capabilitytypes.Capability, + counterparty channeltypes.Counterparty, + version, + counterpartyVersion string, +) error { + if order != channeltypes.ORDERED { + return sdkerrors.Wrapf(channeltypes.ErrInvalidChannelOrdering, "expected %s channel, got %s", channeltypes.ORDERED, order) + } + + if portID != icatypes.PortID { + return sdkerrors.Wrapf(porttypes.ErrInvalidPort, "expected %s, got %s", icatypes.PortID, portID) + } + + connSequence, err := icatypes.ParseHostConnSequence(counterparty.PortId) + if err != nil { + return sdkerrors.Wrapf(err, "expected format %s, got %s", icatypes.ControllerPortFormat, counterparty.PortId) + } + + counterpartyConnSequence, err := icatypes.ParseControllerConnSequence(counterparty.PortId) + if err != nil { + return sdkerrors.Wrapf(err, "expected format %s, got %s", icatypes.ControllerPortFormat, counterparty.PortId) + } + + if err := k.validateControllerPortParams(ctx, channelID, portID, connSequence, counterpartyConnSequence); err != nil { + return sdkerrors.Wrapf(err, "failed to validate controller port %s", counterparty.PortId) + } + + if err := icatypes.ValidateVersion(version); err != nil { + return sdkerrors.Wrap(err, "version validation failed") + } + + if counterpartyVersion != icatypes.VersionPrefix { + return sdkerrors.Wrapf(icatypes.ErrInvalidVersion, "expected %s, got %s", icatypes.VersionPrefix, version) + } + + // On the host chain the capability may only be claimed during the OnChanOpenTry + // The capability being claimed in OpenInit is for a controller chain (the port is different) + if err := k.ClaimCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)); err != nil { + return sdkerrors.Wrapf(err, "failed to claim capability for channel %s on port %s", channelID, portID) + } + + // Check to ensure that the version string contains the expected address generated from the Counterparty portID + accAddr := icatypes.GenerateAddress(k.accountKeeper.GetModuleAddress(icatypes.ModuleName), counterparty.PortId) + parsedAddr, err := icatypes.ParseAddressFromVersion(version) + if err != nil { + return sdkerrors.Wrapf(err, "expected format , got %s", icatypes.Delimiter, version) + } + + if parsedAddr != accAddr.String() { + return sdkerrors.Wrapf(icatypes.ErrInvalidVersion, "version contains invalid account address: expected %s, got %s", parsedAddr, accAddr) + } + + // Register interchain account if it does not already exist + k.RegisterInterchainAccount(ctx, accAddr, counterparty.PortId) + + return nil +} + +// OnChanOpenConfirm completes the handshake process by setting the active channel in state on the host chain +func (k Keeper) OnChanOpenConfirm( + ctx sdk.Context, + portID, + channelID string, +) error { + + k.SetActiveChannelID(ctx, portID, channelID) + + return nil +} + +// OnChanCloseConfirm removes the active channel stored in state +func (k Keeper) OnChanCloseConfirm( + ctx sdk.Context, + portID, + channelID string, +) error { + + k.DeleteActiveChannelID(ctx, portID) + + return nil +} + +// validateControllerPortParams asserts the provided connection sequence and counterparty connection sequence +// match that of the associated connection stored in state +func (k Keeper) validateControllerPortParams(ctx sdk.Context, channelID, portID string, connectionSeq, counterpartyConnectionSeq uint64) error { + channel, found := k.channelKeeper.GetChannel(ctx, portID, channelID) + if !found { + return sdkerrors.Wrapf(channeltypes.ErrChannelNotFound, "port ID %s channel ID %s", portID, channelID) + } + + counterpartyHops, found := k.channelKeeper.CounterpartyHops(ctx, channel) + if !found { + return sdkerrors.Wrap(connectiontypes.ErrConnectionNotFound, channel.ConnectionHops[0]) + } + + connSeq, err := connectiontypes.ParseConnectionSequence(channel.ConnectionHops[0]) + if err != nil { + return sdkerrors.Wrapf(err, "failed to parse connection sequence %s", channel.ConnectionHops[0]) + } + + counterpartyConnSeq, err := connectiontypes.ParseConnectionSequence(counterpartyHops[0]) + if err != nil { + return sdkerrors.Wrapf(err, "failed to parse counterparty connection sequence %s", counterpartyHops[0]) + } + + if connSeq != connectionSeq { + return sdkerrors.Wrapf(connectiontypes.ErrInvalidConnection, "sequence mismatch, expected %d, got %d", connSeq, connectionSeq) + } + + if counterpartyConnSeq != counterpartyConnectionSeq { + return sdkerrors.Wrapf(connectiontypes.ErrInvalidConnection, "counterparty sequence mismatch, expected %d, got %d", counterpartyConnSeq, counterpartyConnectionSeq) + } + + return nil +} diff --git a/modules/apps/27-interchain-accounts/host/keeper/handshake_test.go b/modules/apps/27-interchain-accounts/host/keeper/handshake_test.go new file mode 100644 index 0000000000..c07b46834e --- /dev/null +++ b/modules/apps/27-interchain-accounts/host/keeper/handshake_test.go @@ -0,0 +1,267 @@ +package keeper_test + +import ( + capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" + + icatypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" + channeltypes "github.com/cosmos/ibc-go/v2/modules/core/04-channel/types" + host "github.com/cosmos/ibc-go/v2/modules/core/24-host" + ibctesting "github.com/cosmos/ibc-go/v2/testing" +) + +func (suite *KeeperTestSuite) TestOnChanOpenTry() { + var ( + channel *channeltypes.Channel + path *ibctesting.Path + chanCap *capabilitytypes.Capability + counterpartyVersion string + ) + + testCases := []struct { + name string + malleate func() + expPass bool + }{ + + { + "success", + func() { + path.EndpointB.SetChannel(*channel) + }, + true, + }, + { + "invalid order - UNORDERED", + func() { + channel.Ordering = channeltypes.UNORDERED + }, + false, + }, + { + "invalid port", + func() { + path.EndpointB.ChannelConfig.PortID = "invalid-port-id" + }, + false, + }, + { + "invalid counterparty port", + func() { + channel.Counterparty.PortId = "invalid-port-id" + }, + false, + }, + { + "channel not found", + func() { + path.EndpointB.ChannelID = "invalid-channel-id" + }, + false, + }, + { + "connection not found", + func() { + channel.ConnectionHops = []string{"invalid-connnection-id"} + path.EndpointB.SetChannel(*channel) + }, + false, + }, + { + "invalid connection sequence", + func() { + portID, err := icatypes.GeneratePortID(TestOwnerAddress, "connection-0", "connection-1") + suite.Require().NoError(err) + + channel.Counterparty.PortId = portID + path.EndpointB.SetChannel(*channel) + }, + false, + }, + { + "invalid counterparty connection sequence", + func() { + portID, err := icatypes.GeneratePortID(TestOwnerAddress, "connection-1", "connection-0") + suite.Require().NoError(err) + + channel.Counterparty.PortId = portID + path.EndpointB.SetChannel(*channel) + }, + false, + }, + { + "invalid version", + func() { + channel.Version = "version" + path.EndpointB.SetChannel(*channel) + }, + false, + }, + { + "invalid counterparty version", + func() { + counterpartyVersion = "version" + path.EndpointB.SetChannel(*channel) + }, + false, + }, + { + "capability already claimed", + func() { + path.EndpointB.SetChannel(*channel) + err := suite.chainB.GetSimApp().ScopedICAHostKeeper.ClaimCapability(suite.chainB.GetContext(), chanCap, host.ChannelCapabilityPath(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID)) + suite.Require().NoError(err) + }, + false, + }, + { + "invalid account address", + func() { + portID, err := icatypes.GeneratePortID("invalid-owner-addr", "connection-0", "connection-0") + suite.Require().NoError(err) + + channel.Counterparty.PortId = portID + path.EndpointB.SetChannel(*channel) + }, + false, + }, + } + + for _, tc := range testCases { + tc := tc + + suite.Run(tc.name, func() { + suite.SetupTest() // reset + + path = NewICAPath(suite.chainA, suite.chainB) + counterpartyVersion = icatypes.VersionPrefix + suite.coordinator.SetupConnections(path) + + err := InitInterchainAccount(path.EndpointA, TestOwnerAddress) + suite.Require().NoError(err) + + // set the channel id on host + channelSequence := path.EndpointB.Chain.App.GetIBCKeeper().ChannelKeeper.GetNextChannelSequence(path.EndpointB.Chain.GetContext()) + path.EndpointB.ChannelID = channeltypes.FormatChannelIdentifier(channelSequence) + + // default values + counterparty := channeltypes.NewCounterparty(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) + channel = &channeltypes.Channel{ + State: channeltypes.TRYOPEN, + Ordering: channeltypes.ORDERED, + Counterparty: counterparty, + ConnectionHops: []string{path.EndpointB.ConnectionID}, + Version: TestVersion, + } + + chanCap, err = suite.chainB.App.GetScopedIBCKeeper().NewCapability(suite.chainB.GetContext(), host.ChannelCapabilityPath(path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID)) + suite.Require().NoError(err) + + tc.malleate() // malleate mutates test data + + err = suite.chainB.GetSimApp().ICAHostKeeper.OnChanOpenTry(suite.chainB.GetContext(), channel.Ordering, channel.GetConnectionHops(), + path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, chanCap, channel.Counterparty, channel.GetVersion(), + counterpartyVersion, + ) + + if tc.expPass { + suite.Require().NoError(err) + } else { + suite.Require().Error(err) + } + }) + } +} + +func (suite *KeeperTestSuite) TestOnChanOpenConfirm() { + var ( + path *ibctesting.Path + ) + + testCases := []struct { + name string + malleate func() + expPass bool + }{ + + { + "success", func() {}, true, + }, + } + + for _, tc := range testCases { + tc := tc + + suite.Run(tc.name, func() { + suite.SetupTest() // reset + + path = NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := InitInterchainAccount(path.EndpointA, TestOwnerAddress) + suite.Require().NoError(err) + + err = path.EndpointB.ChanOpenTry() + suite.Require().NoError(err) + + err = path.EndpointA.ChanOpenAck() + suite.Require().NoError(err) + + tc.malleate() // malleate mutates test data + + err = suite.chainB.GetSimApp().ICAHostKeeper.OnChanOpenConfirm(suite.chainB.GetContext(), + path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID) + + if tc.expPass { + suite.Require().NoError(err) + } else { + suite.Require().Error(err) + } + + }) + } +} + +func (suite *KeeperTestSuite) TestOnChanCloseConfirm() { + var ( + path *ibctesting.Path + ) + + testCases := []struct { + name string + malleate func() + expPass bool + }{ + + { + "success", func() {}, true, + }, + } + + for _, tc := range testCases { + suite.Run(tc.name, func() { + suite.SetupTest() // reset + + path = NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := SetupICAPath(path, TestOwnerAddress) + suite.Require().NoError(err) + + tc.malleate() // malleate mutates test data + + err = suite.chainB.GetSimApp().ICAHostKeeper.OnChanCloseConfirm(suite.chainB.GetContext(), + path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID) + + activeChannelID, found := suite.chainB.GetSimApp().ICAHostKeeper.GetActiveChannelID(suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID) + + if tc.expPass { + suite.Require().NoError(err) + suite.Require().False(found) + suite.Require().Empty(activeChannelID) + } else { + suite.Require().Error(err) + } + + }) + } +} diff --git a/modules/apps/27-interchain-accounts/host/keeper/keeper.go b/modules/apps/27-interchain-accounts/host/keeper/keeper.go new file mode 100644 index 0000000000..c4d9261651 --- /dev/null +++ b/modules/apps/27-interchain-accounts/host/keeper/keeper.go @@ -0,0 +1,201 @@ +package keeper + +import ( + "fmt" + "strings" + + baseapp "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper" + capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" + paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" + "github.com/tendermint/tendermint/libs/log" + + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/host/types" + icatypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" + channeltypes "github.com/cosmos/ibc-go/v2/modules/core/04-channel/types" + host "github.com/cosmos/ibc-go/v2/modules/core/24-host" +) + +// Keeper defines the IBC interchain accounts host keeper +type Keeper struct { + storeKey sdk.StoreKey + cdc codec.BinaryCodec + paramSpace paramtypes.Subspace + + channelKeeper icatypes.ChannelKeeper + portKeeper icatypes.PortKeeper + accountKeeper icatypes.AccountKeeper + + scopedKeeper capabilitykeeper.ScopedKeeper + + msgRouter *baseapp.MsgServiceRouter +} + +// NewKeeper creates a new interchain accounts host Keeper instance +func NewKeeper( + cdc codec.BinaryCodec, key sdk.StoreKey, paramSpace paramtypes.Subspace, + channelKeeper icatypes.ChannelKeeper, portKeeper icatypes.PortKeeper, + accountKeeper icatypes.AccountKeeper, scopedKeeper capabilitykeeper.ScopedKeeper, msgRouter *baseapp.MsgServiceRouter, +) Keeper { + + // ensure ibc interchain accounts module account is set + if addr := accountKeeper.GetModuleAddress(icatypes.ModuleName); addr == nil { + panic("the Interchain Accounts module account has not been set") + } + + // set KeyTable if it has not already been set + if !paramSpace.HasKeyTable() { + paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable()) + } + + return Keeper{ + storeKey: key, + cdc: cdc, + paramSpace: paramSpace, + channelKeeper: channelKeeper, + portKeeper: portKeeper, + accountKeeper: accountKeeper, + scopedKeeper: scopedKeeper, + msgRouter: msgRouter, + } +} + +// Logger returns the application logger, scoped to the associated module +func (k Keeper) Logger(ctx sdk.Context) log.Logger { + return ctx.Logger().With("module", fmt.Sprintf("x/%s-%s", host.ModuleName, icatypes.ModuleName)) +} + +// BindPort stores the provided portID and binds to it, returning the associated capability +func (k Keeper) BindPort(ctx sdk.Context, portID string) *capabilitytypes.Capability { + store := ctx.KVStore(k.storeKey) + store.Set(icatypes.KeyPort(portID), []byte{0x01}) + + return k.portKeeper.BindPort(ctx, portID) +} + +// IsBound checks if the interchain account host module is already bound to the desired port +func (k Keeper) IsBound(ctx sdk.Context, portID string) bool { + _, ok := k.scopedKeeper.GetCapability(ctx, host.PortPath(portID)) + return ok +} + +// AuthenticateCapability wraps the scopedKeeper's AuthenticateCapability function +func (k Keeper) AuthenticateCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) bool { + return k.scopedKeeper.AuthenticateCapability(ctx, cap, name) +} + +// ClaimCapability wraps the scopedKeeper's ClaimCapability function +func (k Keeper) ClaimCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) error { + return k.scopedKeeper.ClaimCapability(ctx, cap, name) +} + +// GetActiveChannelID retrieves the active channelID from the store keyed by the provided portID +func (k Keeper) GetActiveChannelID(ctx sdk.Context, portID string) (string, bool) { + store := ctx.KVStore(k.storeKey) + key := icatypes.KeyActiveChannel(portID) + + if !store.Has(key) { + return "", false + } + + return string(store.Get(key)), true +} + +// GetAllActiveChannels returns a list of all active interchain accounts host channels and their associated port identifiers +func (k Keeper) GetAllActiveChannels(ctx sdk.Context) []icatypes.ActiveChannel { + store := ctx.KVStore(k.storeKey) + iterator := sdk.KVStorePrefixIterator(store, []byte(icatypes.ActiveChannelKeyPrefix)) + defer iterator.Close() + + var activeChannels []icatypes.ActiveChannel + for ; iterator.Valid(); iterator.Next() { + keySplit := strings.Split(string(iterator.Key()), "/") + + ch := icatypes.ActiveChannel{ + PortId: keySplit[1], + ChannelId: string(iterator.Value()), + } + + activeChannels = append(activeChannels, ch) + } + + return activeChannels +} + +// SetActiveChannelID stores the active channelID, keyed by the provided portID +func (k Keeper) SetActiveChannelID(ctx sdk.Context, portID, channelID string) { + store := ctx.KVStore(k.storeKey) + store.Set(icatypes.KeyActiveChannel(portID), []byte(channelID)) +} + +// DeleteActiveChannelID removes the active channel keyed by the provided portID stored in state +func (k Keeper) DeleteActiveChannelID(ctx sdk.Context, portID string) { + store := ctx.KVStore(k.storeKey) + store.Delete(icatypes.KeyActiveChannel(portID)) +} + +// IsActiveChannel returns true if there exists an active channel for the provided portID, otherwise false +func (k Keeper) IsActiveChannel(ctx sdk.Context, portID string) bool { + _, ok := k.GetActiveChannelID(ctx, portID) + return ok +} + +// GetInterchainAccountAddress retrieves the InterchainAccount address from the store keyed by the provided portID +func (k Keeper) GetInterchainAccountAddress(ctx sdk.Context, portID string) (string, bool) { + store := ctx.KVStore(k.storeKey) + key := icatypes.KeyOwnerAccount(portID) + + if !store.Has(key) { + return "", false + } + + return string(store.Get(key)), true +} + +// GetAllInterchainAccounts returns a list of all registered interchain account addresses and their associated controller port identifiers +func (k Keeper) GetAllInterchainAccounts(ctx sdk.Context) []icatypes.RegisteredInterchainAccount { + store := ctx.KVStore(k.storeKey) + iterator := sdk.KVStorePrefixIterator(store, []byte(icatypes.OwnerKeyPrefix)) + + var interchainAccounts []icatypes.RegisteredInterchainAccount + for ; iterator.Valid(); iterator.Next() { + keySplit := strings.Split(string(iterator.Key()), "/") + + acc := icatypes.RegisteredInterchainAccount{ + PortId: keySplit[1], + AccountAddress: string(iterator.Value()), + } + + interchainAccounts = append(interchainAccounts, acc) + } + + return interchainAccounts +} + +// SetInterchainAccountAddress stores the InterchainAccount address, keyed by the associated portID +func (k Keeper) SetInterchainAccountAddress(ctx sdk.Context, portID string, address string) { + store := ctx.KVStore(k.storeKey) + store.Set(icatypes.KeyOwnerAccount(portID), []byte(address)) +} + +// NegotiateAppVersion handles application version negotation for the IBC interchain accounts module +func (k Keeper) NegotiateAppVersion( + ctx sdk.Context, + order channeltypes.Order, + connectionID string, + portID string, + counterparty channeltypes.Counterparty, + proposedVersion string, +) (string, error) { + if proposedVersion != icatypes.VersionPrefix { + return "", sdkerrors.Wrapf(icatypes.ErrInvalidVersion, "failed to negotiate app version: expected %s, got %s", icatypes.VersionPrefix, proposedVersion) + } + + moduleAccAddr := k.accountKeeper.GetModuleAddress(icatypes.ModuleName) + accAddr := icatypes.GenerateAddress(moduleAccAddr, counterparty.PortId) + + return icatypes.NewAppVersion(icatypes.VersionPrefix, accAddr.String()), nil +} diff --git a/modules/apps/27-interchain-accounts/host/keeper/keeper_test.go b/modules/apps/27-interchain-accounts/host/keeper/keeper_test.go new file mode 100644 index 0000000000..5f12ab2dfd --- /dev/null +++ b/modules/apps/27-interchain-accounts/host/keeper/keeper_test.go @@ -0,0 +1,229 @@ +package keeper_test + +import ( + "testing" + + sdk "github.com/cosmos/cosmos-sdk/types" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + "github.com/stretchr/testify/suite" + "github.com/tendermint/tendermint/crypto" + + icatypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" + channeltypes "github.com/cosmos/ibc-go/v2/modules/core/04-channel/types" + ibctesting "github.com/cosmos/ibc-go/v2/testing" +) + +var ( + // TestAccAddress defines a resuable bech32 address for testing purposes + // TODO: update crypto.AddressHash() when sdk uses address.Module() + TestAccAddress = icatypes.GenerateAddress(sdk.AccAddress(crypto.AddressHash([]byte(icatypes.ModuleName))), TestPortID) + // TestOwnerAddress defines a reusable bech32 address for testing purposes + TestOwnerAddress = "cosmos17dtl0mjt3t77kpuhg2edqzjpszulwhgzuj9ljs" + // TestPortID defines a resuable port identifier for testing purposes + TestPortID, _ = icatypes.GeneratePortID(TestOwnerAddress, ibctesting.FirstConnectionID, ibctesting.FirstConnectionID) + // TestVersion defines a resuable interchainaccounts version string for testing purposes + TestVersion = icatypes.NewAppVersion(icatypes.VersionPrefix, TestAccAddress.String()) +) + +type KeeperTestSuite struct { + suite.Suite + + coordinator *ibctesting.Coordinator + + // testing chains used for convenience and readability + chainA *ibctesting.TestChain + chainB *ibctesting.TestChain + chainC *ibctesting.TestChain +} + +func (suite *KeeperTestSuite) SetupTest() { + suite.coordinator = ibctesting.NewCoordinator(suite.T(), 3) + suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(0)) + suite.chainB = suite.coordinator.GetChain(ibctesting.GetChainID(1)) + suite.chainC = suite.coordinator.GetChain(ibctesting.GetChainID(2)) +} + +func NewICAPath(chainA, chainB *ibctesting.TestChain) *ibctesting.Path { + path := ibctesting.NewPath(chainA, chainB) + path.EndpointA.ChannelConfig.PortID = icatypes.PortID + path.EndpointB.ChannelConfig.PortID = icatypes.PortID + path.EndpointA.ChannelConfig.Order = channeltypes.ORDERED + path.EndpointB.ChannelConfig.Order = channeltypes.ORDERED + path.EndpointA.ChannelConfig.Version = icatypes.VersionPrefix + path.EndpointB.ChannelConfig.Version = TestVersion + + return path +} + +// SetupICAPath invokes the InterchainAccounts entrypoint and subsequent channel handshake handlers +func SetupICAPath(path *ibctesting.Path, owner string) error { + if err := InitInterchainAccount(path.EndpointA, owner); err != nil { + return err + } + + if err := path.EndpointB.ChanOpenTry(); err != nil { + return err + } + + if err := path.EndpointA.ChanOpenAck(); err != nil { + return err + } + + if err := path.EndpointB.ChanOpenConfirm(); err != nil { + return err + } + + return nil +} + +// InitInterchainAccount is a helper function for starting the channel handshake +func InitInterchainAccount(endpoint *ibctesting.Endpoint, owner string) error { + portID, err := icatypes.GeneratePortID(owner, endpoint.ConnectionID, endpoint.Counterparty.ConnectionID) + if err != nil { + return err + } + + channelSequence := endpoint.Chain.App.GetIBCKeeper().ChannelKeeper.GetNextChannelSequence(endpoint.Chain.GetContext()) + + if err := endpoint.Chain.GetSimApp().ICAControllerKeeper.InitInterchainAccount(endpoint.Chain.GetContext(), endpoint.ConnectionID, endpoint.Counterparty.ConnectionID, owner); err != nil { + return err + } + + // commit state changes for proof verification + endpoint.Chain.App.Commit() + endpoint.Chain.NextBlock() + + // update port/channel ids + endpoint.ChannelID = channeltypes.FormatChannelIdentifier(channelSequence) + endpoint.ChannelConfig.PortID = portID + + return nil +} + +func TestKeeperTestSuite(t *testing.T) { + suite.Run(t, new(KeeperTestSuite)) +} + +func (suite *KeeperTestSuite) TestIsBound() { + suite.SetupTest() + + path := NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := SetupICAPath(path, TestOwnerAddress) + suite.Require().NoError(err) + + isBound := suite.chainB.GetSimApp().ICAHostKeeper.IsBound(suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID) + suite.Require().True(isBound) +} + +func (suite *KeeperTestSuite) TestGetInterchainAccountAddress() { + suite.SetupTest() + + path := NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := SetupICAPath(path, TestOwnerAddress) + suite.Require().NoError(err) + + counterpartyPortID := path.EndpointA.ChannelConfig.PortID + expectedAddr := authtypes.NewBaseAccountWithAddress(icatypes.GenerateAddress(suite.chainA.GetSimApp().AccountKeeper.GetModuleAddress(icatypes.ModuleName), counterpartyPortID)).GetAddress() + + retrievedAddr, found := suite.chainB.GetSimApp().ICAHostKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), counterpartyPortID) + suite.Require().True(found) + suite.Require().Equal(expectedAddr.String(), retrievedAddr) + + retrievedAddr, found = suite.chainB.GetSimApp().ICAHostKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), "invalid port") + suite.Require().False(found) + suite.Require().Empty(retrievedAddr) +} + +func (suite *KeeperTestSuite) TestGetAllActiveChannels() { + var ( + expectedChannelID string = "test-channel" + expectedPortID string = "test-port" + ) + + suite.SetupTest() + + path := NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := SetupICAPath(path, TestOwnerAddress) + suite.Require().NoError(err) + + suite.chainB.GetSimApp().ICAHostKeeper.SetActiveChannelID(suite.chainB.GetContext(), expectedPortID, expectedChannelID) + + expectedChannels := []icatypes.ActiveChannel{ + { + PortId: path.EndpointB.ChannelConfig.PortID, + ChannelId: path.EndpointB.ChannelID, + }, + { + PortId: expectedPortID, + ChannelId: expectedChannelID, + }, + } + + activeChannels := suite.chainB.GetSimApp().ICAHostKeeper.GetAllActiveChannels(suite.chainB.GetContext()) + suite.Require().Len(activeChannels, len(expectedChannels)) + suite.Require().Equal(expectedChannels, activeChannels) +} + +func (suite *KeeperTestSuite) TestGetAllInterchainAccounts() { + var ( + expectedAccAddr string = "test-acc-addr" + expectedPortID string = "test-port" + ) + + suite.SetupTest() + + path := NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := SetupICAPath(path, TestOwnerAddress) + suite.Require().NoError(err) + + suite.chainB.GetSimApp().ICAHostKeeper.SetInterchainAccountAddress(suite.chainB.GetContext(), expectedPortID, expectedAccAddr) + + expectedAccounts := []icatypes.RegisteredInterchainAccount{ + { + PortId: TestPortID, + AccountAddress: TestAccAddress.String(), + }, + { + PortId: expectedPortID, + AccountAddress: expectedAccAddr, + }, + } + + interchainAccounts := suite.chainB.GetSimApp().ICAHostKeeper.GetAllInterchainAccounts(suite.chainB.GetContext()) + suite.Require().Len(interchainAccounts, len(expectedAccounts)) + suite.Require().Equal(expectedAccounts, interchainAccounts) +} + +func (suite *KeeperTestSuite) TestIsActiveChannel() { + suite.SetupTest() + + path := NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := SetupICAPath(path, TestOwnerAddress) + suite.Require().NoError(err) + + isActive := suite.chainB.GetSimApp().ICAHostKeeper.IsActiveChannel(suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID) + suite.Require().True(isActive) +} + +func (suite *KeeperTestSuite) TestSetInterchainAccountAddress() { + var ( + expectedAccAddr string = "test-acc-addr" + expectedPortID string = "test-port" + ) + + suite.chainB.GetSimApp().ICAHostKeeper.SetInterchainAccountAddress(suite.chainB.GetContext(), expectedPortID, expectedAccAddr) + + retrievedAddr, found := suite.chainB.GetSimApp().ICAHostKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), expectedPortID) + suite.Require().True(found) + suite.Require().Equal(expectedAccAddr, retrievedAddr) +} diff --git a/modules/apps/27-interchain-accounts/host/keeper/params.go b/modules/apps/27-interchain-accounts/host/keeper/params.go new file mode 100644 index 0000000000..b126dd6bd8 --- /dev/null +++ b/modules/apps/27-interchain-accounts/host/keeper/params.go @@ -0,0 +1,32 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/host/types" +) + +// IsHostEnabled retrieves the host enabled boolean from the paramstore. +// True is returned if the host submodule is enabled. +func (k Keeper) IsHostEnabled(ctx sdk.Context) bool { + var res bool + k.paramSpace.Get(ctx, types.KeyHostEnabled, &res) + return res +} + +// GetAllowMessages retrieves the host enabled msg types from the paramstore +func (k Keeper) GetAllowMessages(ctx sdk.Context) []string { + var res []string + k.paramSpace.Get(ctx, types.KeyAllowMessages, &res) + return res +} + +// GetParams returns the total set of the host submodule parameters. +func (k Keeper) GetParams(ctx sdk.Context) types.Params { + return types.NewParams(k.IsHostEnabled(ctx), k.GetAllowMessages(ctx)) +} + +// SetParams sets the total set of the host submodule parameters. +func (k Keeper) SetParams(ctx sdk.Context, params types.Params) { + k.paramSpace.SetParamSet(ctx, ¶ms) +} diff --git a/modules/apps/27-interchain-accounts/host/keeper/params_test.go b/modules/apps/27-interchain-accounts/host/keeper/params_test.go new file mode 100644 index 0000000000..ff4d6d5586 --- /dev/null +++ b/modules/apps/27-interchain-accounts/host/keeper/params_test.go @@ -0,0 +1,16 @@ +package keeper_test + +import "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/host/types" + +func (suite *KeeperTestSuite) TestParams() { + expParams := types.DefaultParams() + + params := suite.chainA.GetSimApp().ICAHostKeeper.GetParams(suite.chainA.GetContext()) + suite.Require().Equal(expParams, params) + + expParams.HostEnabled = false + expParams.AllowMessages = []string{"/cosmos.staking.v1beta1.MsgDelegate"} + suite.chainA.GetSimApp().ICAHostKeeper.SetParams(suite.chainA.GetContext(), expParams) + params = suite.chainA.GetSimApp().ICAHostKeeper.GetParams(suite.chainA.GetContext()) + suite.Require().Equal(expParams, params) +} diff --git a/modules/apps/27-interchain-accounts/host/keeper/relay.go b/modules/apps/27-interchain-accounts/host/keeper/relay.go new file mode 100644 index 0000000000..244e0d06e0 --- /dev/null +++ b/modules/apps/27-interchain-accounts/host/keeper/relay.go @@ -0,0 +1,93 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/host/types" + icatypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" + channeltypes "github.com/cosmos/ibc-go/v2/modules/core/04-channel/types" +) + +// AuthenticateTx ensures the provided msgs contain the correct interchain account signer address retrieved +// from state using the provided controller port identifier +func (k Keeper) AuthenticateTx(ctx sdk.Context, msgs []sdk.Msg, portID string) error { + interchainAccountAddr, found := k.GetInterchainAccountAddress(ctx, portID) + if !found { + return sdkerrors.Wrapf(icatypes.ErrInterchainAccountNotFound, "failed to retrieve interchain account on port %s", portID) + } + + allowMsgs := k.GetAllowMessages(ctx) + for _, msg := range msgs { + if !types.ContainsMsgType(allowMsgs, msg) { + return sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "message type not allowed: %s", sdk.MsgTypeURL(msg)) + } + + for _, signer := range msg.GetSigners() { + if interchainAccountAddr != signer.String() { + return sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "unexpected signer address: expected %s, got %s", interchainAccountAddr, signer.String()) + } + } + } + + return nil +} + +func (k Keeper) executeTx(ctx sdk.Context, sourcePort, destPort, destChannel string, msgs []sdk.Msg) error { + if err := k.AuthenticateTx(ctx, msgs, sourcePort); err != nil { + return err + } + + // CacheContext returns a new context with the multi-store branched into a cached storage object + // writeCache is called only if all msgs succeed, performing state transitions atomically + cacheCtx, writeCache := ctx.CacheContext() + for _, msg := range msgs { + if err := msg.ValidateBasic(); err != nil { + return err + } + + if _, err := k.executeMsg(cacheCtx, msg); err != nil { + return err + } + } + + writeCache() + + return nil +} + +// Attempts to get the message handler from the router and if found will then execute the message +func (k Keeper) executeMsg(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { + handler := k.msgRouter.Handler(msg) + if handler == nil { + return nil, icatypes.ErrInvalidRoute + } + + return handler(ctx, msg) +} + +// OnRecvPacket handles a given interchain accounts packet on a destination host chain +func (k Keeper) OnRecvPacket(ctx sdk.Context, packet channeltypes.Packet) error { + var data icatypes.InterchainAccountPacketData + + if err := icatypes.ModuleCdc.UnmarshalJSON(packet.GetData(), &data); err != nil { + // UnmarshalJSON errors are indeterminate and therefore are not wrapped and included in failed acks + return sdkerrors.Wrapf(icatypes.ErrUnknownDataType, "cannot unmarshal ICS-27 interchain account packet data") + } + + switch data.Type { + case icatypes.EXECUTE_TX: + msgs, err := icatypes.DeserializeCosmosTx(k.cdc, data.Data) + if err != nil { + return err + } + + if err = k.executeTx(ctx, packet.SourcePort, packet.DestinationPort, packet.DestinationChannel, msgs); err != nil { + return err + } + + return nil + default: + return icatypes.ErrUnknownDataType + } +} diff --git a/modules/apps/27-interchain-accounts/host/keeper/relay_test.go b/modules/apps/27-interchain-accounts/host/keeper/relay_test.go new file mode 100644 index 0000000000..d77e0735bb --- /dev/null +++ b/modules/apps/27-interchain-accounts/host/keeper/relay_test.go @@ -0,0 +1,449 @@ +package keeper_test + +import ( + "time" + + codectypes "github.com/cosmos/cosmos-sdk/codec/types" + sdk "github.com/cosmos/cosmos-sdk/types" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + disttypes "github.com/cosmos/cosmos-sdk/x/distribution/types" + govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/host/types" + icatypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" + transfertypes "github.com/cosmos/ibc-go/v2/modules/apps/transfer/types" + clienttypes "github.com/cosmos/ibc-go/v2/modules/core/02-client/types" + channeltypes "github.com/cosmos/ibc-go/v2/modules/core/04-channel/types" + ibctesting "github.com/cosmos/ibc-go/v2/testing" +) + +func (suite *KeeperTestSuite) TestOnRecvPacket() { + var ( + path *ibctesting.Path + packetData []byte + ) + + testCases := []struct { + msg string + malleate func() + expPass bool + }{ + { + "interchain account successfully executes banktypes.MsgSend", + func() { + interchainAccountAddr, found := suite.chainB.GetSimApp().ICAHostKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), path.EndpointA.ChannelConfig.PortID) + suite.Require().True(found) + + msg := &banktypes.MsgSend{ + FromAddress: interchainAccountAddr, + ToAddress: suite.chainB.SenderAccount.GetAddress().String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100))), + } + + data, err := icatypes.SerializeCosmosTx(suite.chainA.GetSimApp().AppCodec(), []sdk.Msg{msg}) + suite.Require().NoError(err) + + icaPacketData := icatypes.InterchainAccountPacketData{ + Type: icatypes.EXECUTE_TX, + Data: data, + } + + packetData = icaPacketData.GetBytes() + + params := types.NewParams(true, []string{sdk.MsgTypeURL(msg)}) + suite.chainB.GetSimApp().ICAHostKeeper.SetParams(suite.chainB.GetContext(), params) + }, + true, + }, + { + "interchain account successfully executes stakingtypes.MsgDelegate", + func() { + interchainAccountAddr, found := suite.chainB.GetSimApp().ICAHostKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), path.EndpointA.ChannelConfig.PortID) + suite.Require().True(found) + + validatorAddr := (sdk.ValAddress)(suite.chainB.Vals.Validators[0].Address) + msg := &stakingtypes.MsgDelegate{ + DelegatorAddress: interchainAccountAddr, + ValidatorAddress: validatorAddr.String(), + Amount: sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(5000)), + } + + data, err := icatypes.SerializeCosmosTx(suite.chainA.GetSimApp().AppCodec(), []sdk.Msg{msg}) + suite.Require().NoError(err) + + icaPacketData := icatypes.InterchainAccountPacketData{ + Type: icatypes.EXECUTE_TX, + Data: data, + } + + packetData = icaPacketData.GetBytes() + + params := types.NewParams(true, []string{sdk.MsgTypeURL(msg)}) + suite.chainB.GetSimApp().ICAHostKeeper.SetParams(suite.chainB.GetContext(), params) + }, + true, + }, + { + "interchain account successfully executes stakingtypes.MsgDelegate and stakingtypes.MsgUndelegate sequentially", + func() { + interchainAccountAddr, found := suite.chainB.GetSimApp().ICAHostKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), path.EndpointA.ChannelConfig.PortID) + suite.Require().True(found) + + validatorAddr := (sdk.ValAddress)(suite.chainB.Vals.Validators[0].Address) + msgDelegate := &stakingtypes.MsgDelegate{ + DelegatorAddress: interchainAccountAddr, + ValidatorAddress: validatorAddr.String(), + Amount: sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(5000)), + } + + msgUndelegate := &stakingtypes.MsgUndelegate{ + DelegatorAddress: interchainAccountAddr, + ValidatorAddress: validatorAddr.String(), + Amount: sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(5000)), + } + + data, err := icatypes.SerializeCosmosTx(suite.chainA.GetSimApp().AppCodec(), []sdk.Msg{msgDelegate, msgUndelegate}) + suite.Require().NoError(err) + + icaPacketData := icatypes.InterchainAccountPacketData{ + Type: icatypes.EXECUTE_TX, + Data: data, + } + + packetData = icaPacketData.GetBytes() + + params := types.NewParams(true, []string{sdk.MsgTypeURL(msgDelegate), sdk.MsgTypeURL(msgUndelegate)}) + suite.chainB.GetSimApp().ICAHostKeeper.SetParams(suite.chainB.GetContext(), params) + }, + true, + }, + { + "interchain account successfully executes govtypes.MsgSubmitProposal", + func() { + interchainAccountAddr, found := suite.chainB.GetSimApp().ICAHostKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), path.EndpointA.ChannelConfig.PortID) + suite.Require().True(found) + + testProposal := &govtypes.TextProposal{ + Title: "IBC Gov Proposal", + Description: "tokens for all!", + } + + any, err := codectypes.NewAnyWithValue(testProposal) + suite.Require().NoError(err) + + msg := &govtypes.MsgSubmitProposal{ + Content: any, + InitialDeposit: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(5000))), + Proposer: interchainAccountAddr, + } + + data, err := icatypes.SerializeCosmosTx(suite.chainA.GetSimApp().AppCodec(), []sdk.Msg{msg}) + suite.Require().NoError(err) + + icaPacketData := icatypes.InterchainAccountPacketData{ + Type: icatypes.EXECUTE_TX, + Data: data, + } + + packetData = icaPacketData.GetBytes() + + params := types.NewParams(true, []string{sdk.MsgTypeURL(msg)}) + suite.chainB.GetSimApp().ICAHostKeeper.SetParams(suite.chainB.GetContext(), params) + }, + true, + }, + { + "interchain account successfully executes govtypes.MsgVote", + func() { + interchainAccountAddr, found := suite.chainB.GetSimApp().ICAHostKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), path.EndpointA.ChannelConfig.PortID) + suite.Require().True(found) + + // Populate the gov keeper in advance with an active proposal + testProposal := &govtypes.TextProposal{ + Title: "IBC Gov Proposal", + Description: "tokens for all!", + } + + proposal, err := govtypes.NewProposal(testProposal, govtypes.DefaultStartingProposalID, time.Now(), time.Now().Add(time.Hour)) + suite.Require().NoError(err) + + suite.chainB.GetSimApp().GovKeeper.SetProposal(suite.chainB.GetContext(), proposal) + suite.chainB.GetSimApp().GovKeeper.ActivateVotingPeriod(suite.chainB.GetContext(), proposal) + + msg := &govtypes.MsgVote{ + ProposalId: govtypes.DefaultStartingProposalID, + Voter: interchainAccountAddr, + Option: govtypes.OptionYes, + } + + data, err := icatypes.SerializeCosmosTx(suite.chainA.GetSimApp().AppCodec(), []sdk.Msg{msg}) + suite.Require().NoError(err) + + icaPacketData := icatypes.InterchainAccountPacketData{ + Type: icatypes.EXECUTE_TX, + Data: data, + } + + packetData = icaPacketData.GetBytes() + + params := types.NewParams(true, []string{sdk.MsgTypeURL(msg)}) + suite.chainB.GetSimApp().ICAHostKeeper.SetParams(suite.chainB.GetContext(), params) + }, + true, + }, + { + "interchain account successfully executes disttypes.MsgFundCommunityPool", + func() { + interchainAccountAddr, found := suite.chainB.GetSimApp().ICAHostKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), path.EndpointA.ChannelConfig.PortID) + suite.Require().True(found) + + msg := &disttypes.MsgFundCommunityPool{ + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(5000))), + Depositor: interchainAccountAddr, + } + + data, err := icatypes.SerializeCosmosTx(suite.chainA.GetSimApp().AppCodec(), []sdk.Msg{msg}) + suite.Require().NoError(err) + + icaPacketData := icatypes.InterchainAccountPacketData{ + Type: icatypes.EXECUTE_TX, + Data: data, + } + + packetData = icaPacketData.GetBytes() + + params := types.NewParams(true, []string{sdk.MsgTypeURL(msg)}) + suite.chainB.GetSimApp().ICAHostKeeper.SetParams(suite.chainB.GetContext(), params) + }, + true, + }, + { + "interchain account successfully executes disttypes.MsgSetWithdrawAddress", + func() { + interchainAccountAddr, found := suite.chainB.GetSimApp().ICAHostKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), path.EndpointA.ChannelConfig.PortID) + suite.Require().True(found) + + msg := &disttypes.MsgSetWithdrawAddress{ + DelegatorAddress: interchainAccountAddr, + WithdrawAddress: suite.chainB.SenderAccount.GetAddress().String(), + } + + data, err := icatypes.SerializeCosmosTx(suite.chainA.GetSimApp().AppCodec(), []sdk.Msg{msg}) + suite.Require().NoError(err) + + icaPacketData := icatypes.InterchainAccountPacketData{ + Type: icatypes.EXECUTE_TX, + Data: data, + } + + packetData = icaPacketData.GetBytes() + + params := types.NewParams(true, []string{sdk.MsgTypeURL(msg)}) + suite.chainB.GetSimApp().ICAHostKeeper.SetParams(suite.chainB.GetContext(), params) + }, + true, + }, + { + "interchain account successfully executes transfertypes.MsgTransfer", + func() { + transferPath := ibctesting.NewPath(suite.chainB, suite.chainC) + transferPath.EndpointA.ChannelConfig.PortID = ibctesting.TransferPort + transferPath.EndpointB.ChannelConfig.PortID = ibctesting.TransferPort + + suite.coordinator.Setup(transferPath) + + interchainAccountAddr, found := suite.chainB.GetSimApp().ICAHostKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), path.EndpointA.ChannelConfig.PortID) + suite.Require().True(found) + + msg := &transfertypes.MsgTransfer{ + SourcePort: transferPath.EndpointA.ChannelConfig.PortID, + SourceChannel: transferPath.EndpointA.ChannelID, + Token: sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100)), + Sender: interchainAccountAddr, + Receiver: suite.chainA.SenderAccount.GetAddress().String(), + TimeoutHeight: clienttypes.NewHeight(0, 100), + TimeoutTimestamp: uint64(0), + } + + data, err := icatypes.SerializeCosmosTx(suite.chainA.GetSimApp().AppCodec(), []sdk.Msg{msg}) + suite.Require().NoError(err) + + icaPacketData := icatypes.InterchainAccountPacketData{ + Type: icatypes.EXECUTE_TX, + Data: data, + } + + packetData = icaPacketData.GetBytes() + + params := types.NewParams(true, []string{sdk.MsgTypeURL(msg)}) + suite.chainB.GetSimApp().ICAHostKeeper.SetParams(suite.chainB.GetContext(), params) + }, + true, + }, + { + "cannot unmarshal interchain account packet data", + func() { + packetData = []byte{} + }, + false, + }, + { + "cannot deserialize interchain account packet data messages", + func() { + data := []byte("invalid packet data") + + icaPacketData := icatypes.InterchainAccountPacketData{ + Type: icatypes.EXECUTE_TX, + Data: data, + } + + packetData = icaPacketData.GetBytes() + }, + false, + }, + { + "invalid packet type - UNSPECIFIED", + func() { + data, err := icatypes.SerializeCosmosTx(suite.chainA.GetSimApp().AppCodec(), []sdk.Msg{&banktypes.MsgSend{}}) + suite.Require().NoError(err) + + icaPacketData := icatypes.InterchainAccountPacketData{ + Type: icatypes.UNSPECIFIED, + Data: data, + } + + packetData = icaPacketData.GetBytes() + }, + false, + }, + { + "unauthorised: interchain account not found for controller port ID", + func() { + path.EndpointA.ChannelConfig.PortID = "invalid-port-id" + + data, err := icatypes.SerializeCosmosTx(suite.chainA.GetSimApp().AppCodec(), []sdk.Msg{&banktypes.MsgSend{}}) + suite.Require().NoError(err) + + icaPacketData := icatypes.InterchainAccountPacketData{ + Type: icatypes.EXECUTE_TX, + Data: data, + } + + packetData = icaPacketData.GetBytes() + }, + false, + }, + { + "unauthorised: message type not allowed", // NOTE: do not update params to explicitly force the error + func() { + msg := &banktypes.MsgSend{ + FromAddress: suite.chainB.SenderAccount.GetAddress().String(), + ToAddress: suite.chainB.SenderAccount.GetAddress().String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100))), + } + + data, err := icatypes.SerializeCosmosTx(suite.chainA.GetSimApp().AppCodec(), []sdk.Msg{msg}) + suite.Require().NoError(err) + + icaPacketData := icatypes.InterchainAccountPacketData{ + Type: icatypes.EXECUTE_TX, + Data: data, + } + + packetData = icaPacketData.GetBytes() + }, + false, + }, + { + "unauthorised: signer address is not the interchain account associated with the controller portID", + func() { + msg := &banktypes.MsgSend{ + FromAddress: suite.chainB.SenderAccount.GetAddress().String(), // unexpected signer + ToAddress: suite.chainB.SenderAccount.GetAddress().String(), + Amount: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100))), + } + + data, err := icatypes.SerializeCosmosTx(suite.chainA.GetSimApp().AppCodec(), []sdk.Msg{msg}) + suite.Require().NoError(err) + + icaPacketData := icatypes.InterchainAccountPacketData{ + Type: icatypes.EXECUTE_TX, + Data: data, + } + + packetData = icaPacketData.GetBytes() + + params := types.NewParams(true, []string{sdk.MsgTypeURL(msg)}) + suite.chainB.GetSimApp().ICAHostKeeper.SetParams(suite.chainB.GetContext(), params) + }, + false, + }, + } + + for _, tc := range testCases { + tc := tc + + suite.Run(tc.msg, func() { + suite.SetupTest() // reset + + path = NewICAPath(suite.chainA, suite.chainB) + suite.coordinator.SetupConnections(path) + + err := SetupICAPath(path, TestOwnerAddress) + suite.Require().NoError(err) + + portID, err := icatypes.GeneratePortID(TestOwnerAddress, ibctesting.FirstConnectionID, ibctesting.FirstConnectionID) + suite.Require().NoError(err) + + // Get the address of the interchain account stored in state during handshake step + storedAddr, found := suite.chainB.GetSimApp().ICAHostKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), portID) + suite.Require().True(found) + + icaAddr, err := sdk.AccAddressFromBech32(storedAddr) + suite.Require().NoError(err) + + // Check if account is created + interchainAccount := suite.chainB.GetSimApp().AccountKeeper.GetAccount(suite.chainB.GetContext(), icaAddr) + suite.Require().Equal(interchainAccount.GetAddress().String(), storedAddr) + + suite.fundICAWallet(suite.chainB.GetContext(), path.EndpointA.ChannelConfig.PortID, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10000)))) + + tc.malleate() // malleate mutates test data + + packet := channeltypes.NewPacket( + packetData, + suite.chainA.SenderAccount.GetSequence(), + path.EndpointA.ChannelConfig.PortID, + path.EndpointA.ChannelID, + path.EndpointB.ChannelConfig.PortID, + path.EndpointB.ChannelID, + clienttypes.NewHeight(0, 100), + 0, + ) + + err = suite.chainB.GetSimApp().ICAHostKeeper.OnRecvPacket(suite.chainB.GetContext(), packet) + + if tc.expPass { + suite.Require().NoError(err) + } else { + suite.Require().Error(err) + } + }) + } +} + +func (suite *KeeperTestSuite) fundICAWallet(ctx sdk.Context, portID string, amount sdk.Coins) { + interchainAccountAddr, found := suite.chainB.GetSimApp().ICAHostKeeper.GetInterchainAccountAddress(ctx, portID) + suite.Require().True(found) + + msgBankSend := &banktypes.MsgSend{ + FromAddress: suite.chainB.SenderAccount.GetAddress().String(), + ToAddress: interchainAccountAddr, + Amount: amount, + } + + res, err := suite.chainB.SendMsgs(msgBankSend) + suite.Require().NotEmpty(res) + suite.Require().NoError(err) +} diff --git a/modules/apps/27-interchain-accounts/host/types/errors.go b/modules/apps/27-interchain-accounts/host/types/errors.go new file mode 100644 index 0000000000..b16b4093e5 --- /dev/null +++ b/modules/apps/27-interchain-accounts/host/types/errors.go @@ -0,0 +1,10 @@ +package types + +import ( + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" +) + +// ICA Host sentinel errors +var ( + ErrHostSubModuleDisabled = sdkerrors.Register(SubModuleName, 2, "host submodule is disabled") +) diff --git a/modules/apps/27-interchain-accounts/host/types/host.pb.go b/modules/apps/27-interchain-accounts/host/types/host.pb.go new file mode 100644 index 0000000000..8ad1c15c24 --- /dev/null +++ b/modules/apps/27-interchain-accounts/host/types/host.pb.go @@ -0,0 +1,377 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: ibc/applications/interchain_accounts/host/v1/host.proto + +package types + +import ( + fmt "fmt" + _ "github.com/cosmos/cosmos-sdk/codec/types" + _ "github.com/gogo/protobuf/gogoproto" + proto "github.com/gogo/protobuf/proto" + io "io" + math "math" + math_bits "math/bits" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package + +// Params defines the set of on-chain interchain accounts parameters. +// The following parameters may be used to disable the host submodule. +type Params struct { + // host_enabled enables or disables the host submodule. + HostEnabled bool `protobuf:"varint,1,opt,name=host_enabled,json=hostEnabled,proto3" json:"host_enabled,omitempty" yaml:"host_enabled"` + // allow_messages defines a list of sdk message typeURLs allowed to be executed on a host chain. + AllowMessages []string `protobuf:"bytes,2,rep,name=allow_messages,json=allowMessages,proto3" json:"allow_messages,omitempty" yaml:"allow_messages"` +} + +func (m *Params) Reset() { *m = Params{} } +func (m *Params) String() string { return proto.CompactTextString(m) } +func (*Params) ProtoMessage() {} +func (*Params) Descriptor() ([]byte, []int) { + return fileDescriptor_48e202774f13d08e, []int{0} +} +func (m *Params) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *Params) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_Params.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *Params) XXX_Merge(src proto.Message) { + xxx_messageInfo_Params.Merge(m, src) +} +func (m *Params) XXX_Size() int { + return m.Size() +} +func (m *Params) XXX_DiscardUnknown() { + xxx_messageInfo_Params.DiscardUnknown(m) +} + +var xxx_messageInfo_Params proto.InternalMessageInfo + +func (m *Params) GetHostEnabled() bool { + if m != nil { + return m.HostEnabled + } + return false +} + +func (m *Params) GetAllowMessages() []string { + if m != nil { + return m.AllowMessages + } + return nil +} + +func init() { + proto.RegisterType((*Params)(nil), "ibc.applications.interchain_accounts.host.v1.Params") +} + +func init() { + proto.RegisterFile("ibc/applications/interchain_accounts/host/v1/host.proto", fileDescriptor_48e202774f13d08e) +} + +var fileDescriptor_48e202774f13d08e = []byte{ + // 306 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x90, 0xbf, 0x6a, 0x32, 0x41, + 0x14, 0xc5, 0xdd, 0xef, 0x03, 0x49, 0x36, 0x7f, 0x0a, 0x93, 0x10, 0xb5, 0x58, 0x65, 0x2b, 0x8b, + 0xb8, 0x17, 0x4d, 0x21, 0x58, 0x05, 0x21, 0x4d, 0x20, 0x10, 0x2c, 0xd3, 0xc8, 0xcc, 0x38, 0x19, + 0x07, 0x66, 0xe7, 0x2e, 0xde, 0x59, 0x83, 0x2f, 0x90, 0x3a, 0x8f, 0x95, 0xd2, 0x32, 0x95, 0x04, + 0x7d, 0x03, 0x9f, 0x20, 0xec, 0x6c, 0x20, 0x0a, 0xa9, 0xe6, 0x9e, 0x73, 0xf8, 0x1d, 0x98, 0x13, + 0x0e, 0x34, 0x17, 0xc0, 0xb2, 0xcc, 0x68, 0xc1, 0x9c, 0x46, 0x4b, 0xa0, 0xad, 0x93, 0x73, 0x31, + 0x63, 0xda, 0x4e, 0x98, 0x10, 0x98, 0x5b, 0x47, 0x30, 0x43, 0x72, 0xb0, 0xe8, 0xf9, 0x37, 0xc9, + 0xe6, 0xe8, 0xb0, 0x76, 0xa3, 0xb9, 0x48, 0xf6, 0xc1, 0xe4, 0x0f, 0x30, 0xf1, 0xc0, 0xa2, 0xd7, + 0x6c, 0x28, 0x44, 0x65, 0x24, 0x78, 0x96, 0xe7, 0x2f, 0xc0, 0xec, 0xb2, 0x2c, 0x6a, 0x5e, 0x2a, + 0x54, 0xe8, 0x4f, 0x28, 0xae, 0xd2, 0x8d, 0xdf, 0x82, 0xb0, 0xfa, 0xc4, 0xe6, 0x2c, 0xa5, 0xda, + 0x30, 0x3c, 0x2d, 0x6a, 0x26, 0xd2, 0x32, 0x6e, 0xe4, 0xb4, 0x1e, 0xb4, 0x83, 0xce, 0xd1, 0xe8, + 0x7a, 0xb7, 0x6e, 0x5d, 0x2c, 0x59, 0x6a, 0x86, 0xf1, 0x7e, 0x1a, 0x8f, 0x4f, 0x0a, 0x79, 0x5f, + 0xaa, 0xda, 0x5d, 0x78, 0xce, 0x8c, 0xc1, 0xd7, 0x49, 0x2a, 0x89, 0x98, 0x92, 0x54, 0xff, 0xd7, + 0xfe, 0xdf, 0x39, 0x1e, 0x35, 0x76, 0xeb, 0xd6, 0x55, 0x49, 0x1f, 0xe6, 0xf1, 0xf8, 0xcc, 0x1b, + 0x8f, 0x3f, 0x7a, 0x34, 0xfd, 0xd8, 0x44, 0xc1, 0x6a, 0x13, 0x05, 0x5f, 0x9b, 0x28, 0x78, 0xdf, + 0x46, 0x95, 0xd5, 0x36, 0xaa, 0x7c, 0x6e, 0xa3, 0xca, 0xf3, 0x83, 0xd2, 0x6e, 0x96, 0xf3, 0x44, + 0x60, 0x0a, 0x02, 0x29, 0x45, 0x02, 0xcd, 0x45, 0x57, 0x21, 0x2c, 0xfa, 0x90, 0xe2, 0x34, 0x37, + 0x92, 0x8a, 0x69, 0x09, 0xfa, 0x83, 0xee, 0xef, 0x38, 0xdd, 0xc3, 0x55, 0xdd, 0x32, 0x93, 0xc4, + 0xab, 0xfe, 0xd7, 0xb7, 0xdf, 0x01, 0x00, 0x00, 0xff, 0xff, 0x6b, 0x0d, 0x8d, 0xb8, 0x8f, 0x01, + 0x00, 0x00, +} + +func (m *Params) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *Params) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *Params) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if len(m.AllowMessages) > 0 { + for iNdEx := len(m.AllowMessages) - 1; iNdEx >= 0; iNdEx-- { + i -= len(m.AllowMessages[iNdEx]) + copy(dAtA[i:], m.AllowMessages[iNdEx]) + i = encodeVarintHost(dAtA, i, uint64(len(m.AllowMessages[iNdEx]))) + i-- + dAtA[i] = 0x12 + } + } + if m.HostEnabled { + i-- + if m.HostEnabled { + dAtA[i] = 1 + } else { + dAtA[i] = 0 + } + i-- + dAtA[i] = 0x8 + } + return len(dAtA) - i, nil +} + +func encodeVarintHost(dAtA []byte, offset int, v uint64) int { + offset -= sovHost(v) + base := offset + for v >= 1<<7 { + dAtA[offset] = uint8(v&0x7f | 0x80) + v >>= 7 + offset++ + } + dAtA[offset] = uint8(v) + return base +} +func (m *Params) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.HostEnabled { + n += 2 + } + if len(m.AllowMessages) > 0 { + for _, s := range m.AllowMessages { + l = len(s) + n += 1 + l + sovHost(uint64(l)) + } + } + return n +} + +func sovHost(x uint64) (n int) { + return (math_bits.Len64(x|1) + 6) / 7 +} +func sozHost(x uint64) (n int) { + return sovHost(uint64((x << 1) ^ uint64((int64(x) >> 63)))) +} +func (m *Params) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowHost + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: Params: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: Params: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field HostEnabled", wireType) + } + var v int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowHost + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + v |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + m.HostEnabled = bool(v != 0) + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field AllowMessages", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowHost + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthHost + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthHost + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.AllowMessages = append(m.AllowMessages, string(dAtA[iNdEx:postIndex])) + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipHost(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthHost + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func skipHost(dAtA []byte) (n int, err error) { + l := len(dAtA) + iNdEx := 0 + depth := 0 + for iNdEx < l { + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowHost + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + wireType := int(wire & 0x7) + switch wireType { + case 0: + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowHost + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + iNdEx++ + if dAtA[iNdEx-1] < 0x80 { + break + } + } + case 1: + iNdEx += 8 + case 2: + var length int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowHost + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + length |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if length < 0 { + return 0, ErrInvalidLengthHost + } + iNdEx += length + case 3: + depth++ + case 4: + if depth == 0 { + return 0, ErrUnexpectedEndOfGroupHost + } + depth-- + case 5: + iNdEx += 4 + default: + return 0, fmt.Errorf("proto: illegal wireType %d", wireType) + } + if iNdEx < 0 { + return 0, ErrInvalidLengthHost + } + if depth == 0 { + return iNdEx, nil + } + } + return 0, io.ErrUnexpectedEOF +} + +var ( + ErrInvalidLengthHost = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowHost = fmt.Errorf("proto: integer overflow") + ErrUnexpectedEndOfGroupHost = fmt.Errorf("proto: unexpected end of group") +) diff --git a/modules/apps/27-interchain-accounts/host/types/keys.go b/modules/apps/27-interchain-accounts/host/types/keys.go new file mode 100644 index 0000000000..74c93cdb60 --- /dev/null +++ b/modules/apps/27-interchain-accounts/host/types/keys.go @@ -0,0 +1,24 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + +const ( + // SubModuleName defines the interchain accounts host module name + SubModuleName = "icahost" + + // StoreKey is the store key string for the interchain accounts host module + StoreKey = SubModuleName +) + +// ContainsMsgType returns true if the sdk.Msg TypeURL is present in allowMsgs, otherwise false +func ContainsMsgType(allowMsgs []string, msg sdk.Msg) bool { + for _, v := range allowMsgs { + if v == sdk.MsgTypeURL(msg) { + return true + } + } + + return false +} diff --git a/modules/apps/27-interchain-accounts/host/types/params.go b/modules/apps/27-interchain-accounts/host/types/params.go new file mode 100644 index 0000000000..480de05c28 --- /dev/null +++ b/modules/apps/27-interchain-accounts/host/types/params.go @@ -0,0 +1,83 @@ +package types + +import ( + "fmt" + "strings" + + paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" +) + +const ( + // DefaultHostEnabled is the default value for the host param (set to true) + DefaultHostEnabled = true +) + +var ( + // KeyHostEnabled is the store key for HostEnabled Params + KeyHostEnabled = []byte("HostEnabled") + // KeyAllowMessages is the store key for the AllowMessages Params + KeyAllowMessages = []byte("AllowMessages") +) + +// ParamKeyTable type declaration for parameters +func ParamKeyTable() paramtypes.KeyTable { + return paramtypes.NewKeyTable().RegisterParamSet(&Params{}) +} + +// NewParams creates a new parameter configuration for the host submodule +func NewParams(enableHost bool, allowMsgs []string) Params { + return Params{ + HostEnabled: enableHost, + AllowMessages: allowMsgs, + } +} + +// DefaultParams is the default parameter configuration for the host submodule +func DefaultParams() Params { + return NewParams(DefaultHostEnabled, nil) +} + +// Validate validates all host submodule parameters +func (p Params) Validate() error { + if err := validateEnabled(p.HostEnabled); err != nil { + return err + } + + if err := validateAllowlist(p.AllowMessages); err != nil { + return err + } + + return nil +} + +// ParamSetPairs implements params.ParamSet +func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs { + return paramtypes.ParamSetPairs{ + paramtypes.NewParamSetPair(KeyHostEnabled, p.HostEnabled, validateEnabled), + paramtypes.NewParamSetPair(KeyAllowMessages, p.AllowMessages, validateAllowlist), + } +} + +func validateEnabled(i interface{}) error { + _, ok := i.(bool) + if !ok { + return fmt.Errorf("invalid parameter type: %T", i) + } + + return nil +} + +func validateAllowlist(i interface{}) error { + allowMsgs, ok := i.([]string) + if !ok { + return fmt.Errorf("invalid parameter type: %T", i) + } + + for _, typeURL := range allowMsgs { + if strings.TrimSpace(typeURL) == "" { + return fmt.Errorf("parameter must not contain empty strings: %s", allowMsgs) + } + } + + return nil +} diff --git a/modules/apps/27-interchain-accounts/host/types/params_test.go b/modules/apps/27-interchain-accounts/host/types/params_test.go new file mode 100644 index 0000000000..75dc5ff342 --- /dev/null +++ b/modules/apps/27-interchain-accounts/host/types/params_test.go @@ -0,0 +1,14 @@ +package types_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/host/types" +) + +func TestValidateParams(t *testing.T) { + require.NoError(t, types.DefaultParams().Validate()) + require.NoError(t, types.NewParams(false, []string{}).Validate()) +} diff --git a/modules/apps/27-interchain-accounts/host/types/query.pb.go b/modules/apps/27-interchain-accounts/host/types/query.pb.go new file mode 100644 index 0000000000..5f8f55bf81 --- /dev/null +++ b/modules/apps/27-interchain-accounts/host/types/query.pb.go @@ -0,0 +1,547 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: ibc/applications/interchain_accounts/host/v1/query.proto + +package types + +import ( + context "context" + fmt "fmt" + _ "github.com/gogo/protobuf/gogoproto" + grpc1 "github.com/gogo/protobuf/grpc" + proto "github.com/gogo/protobuf/proto" + _ "google.golang.org/genproto/googleapis/api/annotations" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" + io "io" + math "math" + math_bits "math/bits" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package + +// QueryParamsRequest is the request type for the Query/Params RPC method. +type QueryParamsRequest struct { +} + +func (m *QueryParamsRequest) Reset() { *m = QueryParamsRequest{} } +func (m *QueryParamsRequest) String() string { return proto.CompactTextString(m) } +func (*QueryParamsRequest) ProtoMessage() {} +func (*QueryParamsRequest) Descriptor() ([]byte, []int) { + return fileDescriptor_e6b7e23fc90c353a, []int{0} +} +func (m *QueryParamsRequest) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *QueryParamsRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_QueryParamsRequest.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *QueryParamsRequest) XXX_Merge(src proto.Message) { + xxx_messageInfo_QueryParamsRequest.Merge(m, src) +} +func (m *QueryParamsRequest) XXX_Size() int { + return m.Size() +} +func (m *QueryParamsRequest) XXX_DiscardUnknown() { + xxx_messageInfo_QueryParamsRequest.DiscardUnknown(m) +} + +var xxx_messageInfo_QueryParamsRequest proto.InternalMessageInfo + +// QueryParamsResponse is the response type for the Query/Params RPC method. +type QueryParamsResponse struct { + // params defines the parameters of the module. + Params *Params `protobuf:"bytes,1,opt,name=params,proto3" json:"params,omitempty"` +} + +func (m *QueryParamsResponse) Reset() { *m = QueryParamsResponse{} } +func (m *QueryParamsResponse) String() string { return proto.CompactTextString(m) } +func (*QueryParamsResponse) ProtoMessage() {} +func (*QueryParamsResponse) Descriptor() ([]byte, []int) { + return fileDescriptor_e6b7e23fc90c353a, []int{1} +} +func (m *QueryParamsResponse) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *QueryParamsResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_QueryParamsResponse.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *QueryParamsResponse) XXX_Merge(src proto.Message) { + xxx_messageInfo_QueryParamsResponse.Merge(m, src) +} +func (m *QueryParamsResponse) XXX_Size() int { + return m.Size() +} +func (m *QueryParamsResponse) XXX_DiscardUnknown() { + xxx_messageInfo_QueryParamsResponse.DiscardUnknown(m) +} + +var xxx_messageInfo_QueryParamsResponse proto.InternalMessageInfo + +func (m *QueryParamsResponse) GetParams() *Params { + if m != nil { + return m.Params + } + return nil +} + +func init() { + proto.RegisterType((*QueryParamsRequest)(nil), "ibc.applications.interchain_accounts.host.v1.QueryParamsRequest") + proto.RegisterType((*QueryParamsResponse)(nil), "ibc.applications.interchain_accounts.host.v1.QueryParamsResponse") +} + +func init() { + proto.RegisterFile("ibc/applications/interchain_accounts/host/v1/query.proto", fileDescriptor_e6b7e23fc90c353a) +} + +var fileDescriptor_e6b7e23fc90c353a = []byte{ + // 322 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xa4, 0x91, 0x31, 0x4b, 0x3b, 0x31, + 0x18, 0xc6, 0x9b, 0x3f, 0xfc, 0x3b, 0x9c, 0xdb, 0xd9, 0x41, 0x8a, 0x04, 0xe9, 0xe4, 0xd0, 0xe6, + 0xa5, 0x67, 0xa1, 0x8e, 0xea, 0x28, 0x0e, 0xea, 0xe8, 0x22, 0xb9, 0x34, 0xa4, 0x81, 0x5e, 0xde, + 0xf4, 0x92, 0x2b, 0x74, 0xf5, 0x13, 0x08, 0x7e, 0x24, 0x17, 0x17, 0xa1, 0xe0, 0xe2, 0x28, 0xad, + 0x1f, 0x44, 0x2e, 0x77, 0xa0, 0x45, 0x11, 0x0e, 0xb7, 0x97, 0x37, 0x3c, 0xbf, 0xe7, 0x79, 0xde, + 0x44, 0xc7, 0x3a, 0x15, 0xc0, 0xad, 0x9d, 0x69, 0xc1, 0xbd, 0x46, 0xe3, 0x40, 0x1b, 0x2f, 0x73, + 0x31, 0xe5, 0xda, 0xdc, 0x72, 0x21, 0xb0, 0x30, 0xde, 0xc1, 0x14, 0x9d, 0x87, 0xc5, 0x10, 0xe6, + 0x85, 0xcc, 0x97, 0xcc, 0xe6, 0xe8, 0x31, 0xee, 0xeb, 0x54, 0xb0, 0xaf, 0x4a, 0xf6, 0x83, 0x92, + 0x95, 0x4a, 0xb6, 0x18, 0x76, 0xf7, 0x15, 0xa2, 0x9a, 0x49, 0xe0, 0x56, 0x03, 0x37, 0x06, 0x7d, + 0xad, 0x09, 0xac, 0x6e, 0x47, 0xa1, 0xc2, 0x30, 0x42, 0x39, 0xd5, 0xdb, 0x71, 0xa3, 0x6c, 0xc1, + 0x29, 0x08, 0x7b, 0x9d, 0x28, 0xbe, 0x2a, 0x93, 0x5e, 0xf2, 0x9c, 0x67, 0xee, 0x5a, 0xce, 0x0b, + 0xe9, 0x7c, 0x4f, 0x44, 0xbb, 0x5b, 0x5b, 0x67, 0xd1, 0x38, 0x19, 0x5f, 0x44, 0x6d, 0x1b, 0x36, + 0x7b, 0xe4, 0x80, 0x1c, 0xee, 0x24, 0x23, 0xd6, 0xa4, 0x18, 0xab, 0x69, 0x35, 0x23, 0x79, 0x26, + 0xd1, 0xff, 0xe0, 0x12, 0x3f, 0x92, 0xa8, 0x5d, 0x3d, 0xc6, 0x27, 0xcd, 0x90, 0xdf, 0xb3, 0x77, + 0x4f, 0xff, 0x40, 0xa8, 0x7a, 0xf6, 0x46, 0x77, 0x2f, 0xef, 0x0f, 0xff, 0x58, 0xdc, 0x87, 0xfa, + 0xac, 0xbf, 0x9f, 0xb3, 0xea, 0x73, 0x36, 0x79, 0x5a, 0x53, 0xb2, 0x5a, 0x53, 0xf2, 0xb6, 0xa6, + 0xe4, 0x7e, 0x43, 0x5b, 0xab, 0x0d, 0x6d, 0xbd, 0x6e, 0x68, 0xeb, 0xe6, 0x5c, 0x69, 0x3f, 0x2d, + 0x52, 0x26, 0x30, 0x03, 0x81, 0x2e, 0x43, 0x57, 0x82, 0x07, 0x0a, 0x61, 0x91, 0x40, 0x86, 0x93, + 0x62, 0x26, 0x5d, 0x65, 0x93, 0x8c, 0x07, 0x9f, 0x4e, 0x83, 0x6d, 0x27, 0xbf, 0xb4, 0xd2, 0xa5, + 0xed, 0xf0, 0x6f, 0x47, 0x1f, 0x01, 0x00, 0x00, 0xff, 0xff, 0x60, 0xf9, 0x1a, 0x0c, 0x8e, 0x02, + 0x00, 0x00, +} + +// Reference imports to suppress errors if they are not otherwise used. +var _ context.Context +var _ grpc.ClientConn + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +const _ = grpc.SupportPackageIsVersion4 + +// QueryClient is the client API for Query service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. +type QueryClient interface { + // Params queries all parameters of the ICA host submodule. + Params(ctx context.Context, in *QueryParamsRequest, opts ...grpc.CallOption) (*QueryParamsResponse, error) +} + +type queryClient struct { + cc grpc1.ClientConn +} + +func NewQueryClient(cc grpc1.ClientConn) QueryClient { + return &queryClient{cc} +} + +func (c *queryClient) Params(ctx context.Context, in *QueryParamsRequest, opts ...grpc.CallOption) (*QueryParamsResponse, error) { + out := new(QueryParamsResponse) + err := c.cc.Invoke(ctx, "/ibc.applications.interchain_accounts.host.v1.Query/Params", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// QueryServer is the server API for Query service. +type QueryServer interface { + // Params queries all parameters of the ICA host submodule. + Params(context.Context, *QueryParamsRequest) (*QueryParamsResponse, error) +} + +// UnimplementedQueryServer can be embedded to have forward compatible implementations. +type UnimplementedQueryServer struct { +} + +func (*UnimplementedQueryServer) Params(ctx context.Context, req *QueryParamsRequest) (*QueryParamsResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method Params not implemented") +} + +func RegisterQueryServer(s grpc1.Server, srv QueryServer) { + s.RegisterService(&_Query_serviceDesc, srv) +} + +func _Query_Params_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(QueryParamsRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(QueryServer).Params(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/ibc.applications.interchain_accounts.host.v1.Query/Params", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(QueryServer).Params(ctx, req.(*QueryParamsRequest)) + } + return interceptor(ctx, in, info, handler) +} + +var _Query_serviceDesc = grpc.ServiceDesc{ + ServiceName: "ibc.applications.interchain_accounts.host.v1.Query", + HandlerType: (*QueryServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "Params", + Handler: _Query_Params_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "ibc/applications/interchain_accounts/host/v1/query.proto", +} + +func (m *QueryParamsRequest) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *QueryParamsRequest) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *QueryParamsRequest) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + return len(dAtA) - i, nil +} + +func (m *QueryParamsResponse) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *QueryParamsResponse) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *QueryParamsResponse) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.Params != nil { + { + size, err := m.Params.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintQuery(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func encodeVarintQuery(dAtA []byte, offset int, v uint64) int { + offset -= sovQuery(v) + base := offset + for v >= 1<<7 { + dAtA[offset] = uint8(v&0x7f | 0x80) + v >>= 7 + offset++ + } + dAtA[offset] = uint8(v) + return base +} +func (m *QueryParamsRequest) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + return n +} + +func (m *QueryParamsResponse) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.Params != nil { + l = m.Params.Size() + n += 1 + l + sovQuery(uint64(l)) + } + return n +} + +func sovQuery(x uint64) (n int) { + return (math_bits.Len64(x|1) + 6) / 7 +} +func sozQuery(x uint64) (n int) { + return sovQuery(uint64((x << 1) ^ uint64((int64(x) >> 63)))) +} +func (m *QueryParamsRequest) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQuery + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: QueryParamsRequest: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: QueryParamsRequest: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + default: + iNdEx = preIndex + skippy, err := skipQuery(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthQuery + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *QueryParamsResponse) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQuery + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: QueryParamsResponse: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: QueryParamsResponse: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Params", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQuery + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthQuery + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthQuery + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if m.Params == nil { + m.Params = &Params{} + } + if err := m.Params.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipQuery(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthQuery + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func skipQuery(dAtA []byte) (n int, err error) { + l := len(dAtA) + iNdEx := 0 + depth := 0 + for iNdEx < l { + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowQuery + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + wireType := int(wire & 0x7) + switch wireType { + case 0: + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowQuery + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + iNdEx++ + if dAtA[iNdEx-1] < 0x80 { + break + } + } + case 1: + iNdEx += 8 + case 2: + var length int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowQuery + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + length |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if length < 0 { + return 0, ErrInvalidLengthQuery + } + iNdEx += length + case 3: + depth++ + case 4: + if depth == 0 { + return 0, ErrUnexpectedEndOfGroupQuery + } + depth-- + case 5: + iNdEx += 4 + default: + return 0, fmt.Errorf("proto: illegal wireType %d", wireType) + } + if iNdEx < 0 { + return 0, ErrInvalidLengthQuery + } + if depth == 0 { + return iNdEx, nil + } + } + return 0, io.ErrUnexpectedEOF +} + +var ( + ErrInvalidLengthQuery = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowQuery = fmt.Errorf("proto: integer overflow") + ErrUnexpectedEndOfGroupQuery = fmt.Errorf("proto: unexpected end of group") +) diff --git a/modules/apps/27-interchain-accounts/host/types/query.pb.gw.go b/modules/apps/27-interchain-accounts/host/types/query.pb.gw.go new file mode 100644 index 0000000000..fbf8503339 --- /dev/null +++ b/modules/apps/27-interchain-accounts/host/types/query.pb.gw.go @@ -0,0 +1,148 @@ +// Code generated by protoc-gen-grpc-gateway. DO NOT EDIT. +// source: ibc/applications/interchain_accounts/host/v1/query.proto + +/* +Package types is a reverse proxy. + +It translates gRPC into RESTful JSON APIs. +*/ +package types + +import ( + "context" + "io" + "net/http" + + "github.com/golang/protobuf/descriptor" + "github.com/golang/protobuf/proto" + "github.com/grpc-ecosystem/grpc-gateway/runtime" + "github.com/grpc-ecosystem/grpc-gateway/utilities" + "google.golang.org/grpc" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/grpclog" + "google.golang.org/grpc/status" +) + +// Suppress "imported and not used" errors +var _ codes.Code +var _ io.Reader +var _ status.Status +var _ = runtime.String +var _ = utilities.NewDoubleArray +var _ = descriptor.ForMessage + +func request_Query_Params_0(ctx context.Context, marshaler runtime.Marshaler, client QueryClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq QueryParamsRequest + var metadata runtime.ServerMetadata + + msg, err := client.Params(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + +} + +func local_request_Query_Params_0(ctx context.Context, marshaler runtime.Marshaler, server QueryServer, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq QueryParamsRequest + var metadata runtime.ServerMetadata + + msg, err := server.Params(ctx, &protoReq) + return msg, metadata, err + +} + +// RegisterQueryHandlerServer registers the http handlers for service Query to "mux". +// UnaryRPC :call QueryServer directly. +// StreamingRPC :currently unsupported pending https://github.com/grpc/grpc-go/issues/906. +// Note that using this registration option will cause many gRPC library features (such as grpc.SendHeader, etc) to stop working. Consider using RegisterQueryHandlerFromEndpoint instead. +func RegisterQueryHandlerServer(ctx context.Context, mux *runtime.ServeMux, server QueryServer) error { + + mux.Handle("GET", pattern_Query_Params_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateIncomingContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := local_request_Query_Params_0(rctx, inboundMarshaler, server, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_Query_Params_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + + return nil +} + +// RegisterQueryHandlerFromEndpoint is same as RegisterQueryHandler but +// automatically dials to "endpoint" and closes the connection when "ctx" gets done. +func RegisterQueryHandlerFromEndpoint(ctx context.Context, mux *runtime.ServeMux, endpoint string, opts []grpc.DialOption) (err error) { + conn, err := grpc.Dial(endpoint, opts...) + if err != nil { + return err + } + defer func() { + if err != nil { + if cerr := conn.Close(); cerr != nil { + grpclog.Infof("Failed to close conn to %s: %v", endpoint, cerr) + } + return + } + go func() { + <-ctx.Done() + if cerr := conn.Close(); cerr != nil { + grpclog.Infof("Failed to close conn to %s: %v", endpoint, cerr) + } + }() + }() + + return RegisterQueryHandler(ctx, mux, conn) +} + +// RegisterQueryHandler registers the http handlers for service Query to "mux". +// The handlers forward requests to the grpc endpoint over "conn". +func RegisterQueryHandler(ctx context.Context, mux *runtime.ServeMux, conn *grpc.ClientConn) error { + return RegisterQueryHandlerClient(ctx, mux, NewQueryClient(conn)) +} + +// RegisterQueryHandlerClient registers the http handlers for service Query +// to "mux". The handlers forward requests to the grpc endpoint over the given implementation of "QueryClient". +// Note: the gRPC framework executes interceptors within the gRPC handler. If the passed in "QueryClient" +// doesn't go through the normal gRPC flow (creating a gRPC client etc.) then it will be up to the passed in +// "QueryClient" to call the correct interceptors. +func RegisterQueryHandlerClient(ctx context.Context, mux *runtime.ServeMux, client QueryClient) error { + + mux.Handle("GET", pattern_Query_Params_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := request_Query_Params_0(rctx, inboundMarshaler, client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_Query_Params_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + + return nil +} + +var ( + pattern_Query_Params_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2, 2, 3, 2, 4, 2, 5}, []string{"ibc", "apps", "interchain_accounts", "host", "v1", "params"}, "", runtime.AssumeColonVerbOpt(true))) +) + +var ( + forward_Query_Params_0 = runtime.ForwardResponseMessage +) diff --git a/modules/apps/27-interchain-accounts/module.go b/modules/apps/27-interchain-accounts/module.go new file mode 100644 index 0000000000..8691111bbb --- /dev/null +++ b/modules/apps/27-interchain-accounts/module.go @@ -0,0 +1,182 @@ +package ica + +import ( + "context" + "encoding/json" + "fmt" + + "github.com/gorilla/mux" + "github.com/spf13/cobra" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/codec" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" + "github.com/grpc-ecosystem/grpc-gateway/runtime" + abci "github.com/tendermint/tendermint/abci/types" + + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/client/cli" + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/controller" + controllerkeeper "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/controller/keeper" + controllertypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/controller/types" + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/host" + hostkeeper "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/host/keeper" + hosttypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/host/types" + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" + porttypes "github.com/cosmos/ibc-go/v2/modules/core/05-port/types" +) + +var ( + _ module.AppModule = AppModule{} + _ module.AppModuleBasic = AppModuleBasic{} + + _ porttypes.IBCModule = controller.IBCModule{} + _ porttypes.IBCModule = host.IBCModule{} +) + +// AppModuleBasic is the IBC interchain accounts AppModuleBasic +type AppModuleBasic struct{} + +// Name implements AppModuleBasic interface +func (AppModuleBasic) Name() string { + return types.ModuleName +} + +// RegisterLegacyAminoCodec implements AppModuleBasic interface +func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { + types.RegisterLegacyAminoCodec(cdc) +} + +// RegisterInterfaces registers module concrete types into protobuf Any +func (AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) { + types.RegisterInterfaces(registry) +} + +// DefaultGenesis returns default genesis state as raw bytes for the IBC +// interchain accounts module +func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage { + return cdc.MustMarshalJSON(types.DefaultGenesis()) +} + +// ValidateGenesis performs genesis state validation for the IBC interchain acounts module +func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncodingConfig, bz json.RawMessage) error { + var gs types.GenesisState + if err := cdc.UnmarshalJSON(bz, &gs); err != nil { + return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err) + } + + return gs.Validate() +} + +// RegisterRESTRoutes implements AppModuleBasic interface +func (AppModuleBasic) RegisterRESTRoutes(ctx client.Context, rtr *mux.Router) { +} + +// RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the interchain accounts module. +func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) { + controllertypes.RegisterQueryHandlerClient(context.Background(), mux, controllertypes.NewQueryClient(clientCtx)) + hosttypes.RegisterQueryHandlerClient(context.Background(), mux, hosttypes.NewQueryClient(clientCtx)) +} + +// GetTxCmd implements AppModuleBasic interface +func (AppModuleBasic) GetTxCmd() *cobra.Command { + return nil +} + +// GetQueryCmd implements AppModuleBasic interface +func (AppModuleBasic) GetQueryCmd() *cobra.Command { + return cli.GetQueryCmd() +} + +// AppModule is the application module for the IBC interchain accounts module +type AppModule struct { + AppModuleBasic + controllerKeeper *controllerkeeper.Keeper + hostKeeper *hostkeeper.Keeper +} + +// NewAppModule creates a new IBC interchain accounts module +func NewAppModule(controllerKeeper *controllerkeeper.Keeper, hostKeeper *hostkeeper.Keeper) AppModule { + return AppModule{ + controllerKeeper: controllerKeeper, + hostKeeper: hostKeeper, + } +} + +// RegisterInvariants implements the AppModule interface +func (AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { +} + +// Route implements the AppModule interface +func (AppModule) Route() sdk.Route { + return sdk.NewRoute(types.RouterKey, nil) +} + +// NewHandler implements the AppModule interface +func (AppModule) NewHandler() sdk.Handler { + return nil +} + +// QuerierRoute implements the AppModule interface +func (AppModule) QuerierRoute() string { + return types.QuerierRoute +} + +// LegacyQuerierHandler implements the AppModule interface +func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sdk.Querier { + return nil +} + +// RegisterServices registers module services +func (am AppModule) RegisterServices(cfg module.Configurator) { +} + +// InitGenesis performs genesis initialization for the interchain accounts module. +// It returns no validator updates. +func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate { + var genesisState types.GenesisState + cdc.MustUnmarshalJSON(data, &genesisState) + + if am.controllerKeeper != nil { + controllerkeeper.InitGenesis(ctx, *am.controllerKeeper, genesisState.ControllerGenesisState) + } + + if am.hostKeeper != nil { + hostkeeper.InitGenesis(ctx, *am.hostKeeper, genesisState.HostGenesisState) + } + + return []abci.ValidatorUpdate{} +} + +// ExportGenesis returns the exported genesis state as raw bytes for the interchain accounts module +func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage { + var ( + controllerGenesisState = types.DefaultControllerGenesis() + hostGenesisState = types.DefaultHostGenesis() + ) + + if am.controllerKeeper != nil { + controllerGenesisState = controllerkeeper.ExportGenesis(ctx, *am.controllerKeeper) + } + + if am.hostKeeper != nil { + hostGenesisState = hostkeeper.ExportGenesis(ctx, *am.hostKeeper) + } + + gs := types.NewGenesisState(controllerGenesisState, hostGenesisState) + + return cdc.MustMarshalJSON(gs) +} + +// ConsensusVersion implements AppModule/ConsensusVersion. +func (AppModule) ConsensusVersion() uint64 { return 1 } + +// BeginBlock implements the AppModule interface +func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) { +} + +// EndBlock implements the AppModule interface +func (am AppModule) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) []abci.ValidatorUpdate { + return []abci.ValidatorUpdate{} +} diff --git a/modules/apps/27-interchain-accounts/types/account.go b/modules/apps/27-interchain-accounts/types/account.go new file mode 100644 index 0000000000..e55c57f3b3 --- /dev/null +++ b/modules/apps/27-interchain-accounts/types/account.go @@ -0,0 +1,128 @@ +package types + +import ( + "encoding/json" + "strings" + + crypto "github.com/cosmos/cosmos-sdk/crypto/types" + sdk "github.com/cosmos/cosmos-sdk/types" + sdkaddress "github.com/cosmos/cosmos-sdk/types/address" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + yaml "gopkg.in/yaml.v2" +) + +var ( + _ authtypes.GenesisAccount = (*InterchainAccount)(nil) + _ InterchainAccountI = (*InterchainAccount)(nil) +) + +// InterchainAccountI wraps the authtypes.AccountI interface +type InterchainAccountI interface { + authtypes.AccountI +} + +// interchainAccountPretty defines an unexported struct used for encoding the InterchainAccount details +type interchainAccountPretty struct { + Address sdk.AccAddress `json:"address" yaml:"address"` + PubKey string `json:"public_key" yaml:"public_key"` + AccountNumber uint64 `json:"account_number" yaml:"account_number"` + Sequence uint64 `json:"sequence" yaml:"sequence"` + AccountOwner string `json:"account_owner" yaml:"account_owner"` +} + +// GenerateAddress returns an sdk.AccAddress derived using the provided module account address and port identifier. +// The sdk.AccAddress returned is a sub-address of the module account, using the controller chain's port identifier as the derivation key +func GenerateAddress(moduleAccAddr sdk.AccAddress, portID string) sdk.AccAddress { + return sdk.AccAddress(sdkaddress.Derive(moduleAccAddr, []byte(portID))) +} + +// NewInterchainAccount creates and returns a new InterchainAccount type +func NewInterchainAccount(ba *authtypes.BaseAccount, accountOwner string) *InterchainAccount { + return &InterchainAccount{ + BaseAccount: ba, + AccountOwner: accountOwner, + } +} + +// SetPubKey implements the authtypes.AccountI interface +func (ia InterchainAccount) SetPubKey(pubKey crypto.PubKey) error { + return sdkerrors.Wrap(ErrUnsupported, "cannot set public key for interchain account") +} + +// SetSequence implements the authtypes.AccountI interface +func (ia InterchainAccount) SetSequence(seq uint64) error { + return sdkerrors.Wrap(ErrUnsupported, "cannot set sequence number for interchain account") +} + +// Validate implements basic validation of the InterchainAccount +func (ia InterchainAccount) Validate() error { + if strings.TrimSpace(ia.AccountOwner) == "" { + return sdkerrors.Wrap(ErrInvalidAccountAddress, "AccountOwner cannot be empty") + } + + return ia.BaseAccount.Validate() +} + +// String returns a string representation of the InterchainAccount +func (ia InterchainAccount) String() string { + out, _ := ia.MarshalYAML() + return string(out) +} + +// MarshalYAML returns the YAML representation of the InterchainAccount +func (ia InterchainAccount) MarshalYAML() ([]byte, error) { + accAddr, err := sdk.AccAddressFromBech32(ia.Address) + if err != nil { + return nil, err + } + + bz, err := yaml.Marshal(interchainAccountPretty{ + Address: accAddr, + PubKey: "", + AccountNumber: ia.AccountNumber, + Sequence: ia.Sequence, + AccountOwner: ia.AccountOwner, + }) + + if err != nil { + return nil, err + } + + return bz, nil +} + +// MarshalJSON returns the JSON representation of the InterchainAccount +func (ia InterchainAccount) MarshalJSON() ([]byte, error) { + accAddr, err := sdk.AccAddressFromBech32(ia.Address) + if err != nil { + return nil, err + } + + bz, err := json.Marshal(interchainAccountPretty{ + Address: accAddr, + PubKey: "", + AccountNumber: ia.AccountNumber, + Sequence: ia.Sequence, + AccountOwner: ia.AccountOwner, + }) + + if err != nil { + return nil, err + } + + return bz, nil +} + +// UnmarshalJSON unmarshals raw JSON bytes into the InterchainAccount +func (ia *InterchainAccount) UnmarshalJSON(bz []byte) error { + var alias interchainAccountPretty + if err := json.Unmarshal(bz, &alias); err != nil { + return err + } + + ia.BaseAccount = authtypes.NewBaseAccount(alias.Address, nil, alias.AccountNumber, alias.Sequence) + ia.AccountOwner = alias.AccountOwner + + return nil +} diff --git a/modules/apps/27-interchain-accounts/types/account.pb.go b/modules/apps/27-interchain-accounts/types/account.pb.go new file mode 100644 index 0000000000..d645d28b72 --- /dev/null +++ b/modules/apps/27-interchain-accounts/types/account.pb.go @@ -0,0 +1,377 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: ibc/applications/interchain_accounts/v1/account.proto + +package types + +import ( + fmt "fmt" + types "github.com/cosmos/cosmos-sdk/x/auth/types" + _ "github.com/gogo/protobuf/gogoproto" + proto "github.com/gogo/protobuf/proto" + _ "github.com/regen-network/cosmos-proto" + io "io" + math "math" + math_bits "math/bits" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package + +// An InterchainAccount is defined as a BaseAccount & the address of the account owner on the controller chain +type InterchainAccount struct { + *types.BaseAccount `protobuf:"bytes,1,opt,name=base_account,json=baseAccount,proto3,embedded=base_account" json:"base_account,omitempty" yaml:"base_account"` + AccountOwner string `protobuf:"bytes,2,opt,name=account_owner,json=accountOwner,proto3" json:"account_owner,omitempty" yaml:"account_owner"` +} + +func (m *InterchainAccount) Reset() { *m = InterchainAccount{} } +func (*InterchainAccount) ProtoMessage() {} +func (*InterchainAccount) Descriptor() ([]byte, []int) { + return fileDescriptor_5561bd92625bf7da, []int{0} +} +func (m *InterchainAccount) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *InterchainAccount) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_InterchainAccount.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *InterchainAccount) XXX_Merge(src proto.Message) { + xxx_messageInfo_InterchainAccount.Merge(m, src) +} +func (m *InterchainAccount) XXX_Size() int { + return m.Size() +} +func (m *InterchainAccount) XXX_DiscardUnknown() { + xxx_messageInfo_InterchainAccount.DiscardUnknown(m) +} + +var xxx_messageInfo_InterchainAccount proto.InternalMessageInfo + +func init() { + proto.RegisterType((*InterchainAccount)(nil), "ibc.applications.interchain_accounts.v1.InterchainAccount") +} + +func init() { + proto.RegisterFile("ibc/applications/interchain_accounts/v1/account.proto", fileDescriptor_5561bd92625bf7da) +} + +var fileDescriptor_5561bd92625bf7da = []byte{ + // 341 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x32, 0xcd, 0x4c, 0x4a, 0xd6, + 0x4f, 0x2c, 0x28, 0xc8, 0xc9, 0x4c, 0x4e, 0x2c, 0xc9, 0xcc, 0xcf, 0x2b, 0xd6, 0xcf, 0xcc, 0x2b, + 0x49, 0x2d, 0x4a, 0xce, 0x48, 0xcc, 0xcc, 0x8b, 0x4f, 0x4c, 0x4e, 0xce, 0x2f, 0xcd, 0x2b, 0x29, + 0xd6, 0x2f, 0x33, 0xd4, 0x87, 0xb2, 0xf5, 0x0a, 0x8a, 0xf2, 0x4b, 0xf2, 0x85, 0xd4, 0x33, 0x93, + 0x92, 0xf5, 0x90, 0xb5, 0xe9, 0x61, 0xd1, 0xa6, 0x57, 0x66, 0x28, 0x25, 0x99, 0x9c, 0x5f, 0x9c, + 0x9b, 0x5f, 0x1c, 0x0f, 0xd6, 0xa6, 0x0f, 0xe1, 0x40, 0xcc, 0x90, 0x12, 0x49, 0xcf, 0x4f, 0xcf, + 0x87, 0x88, 0x83, 0x58, 0x50, 0x51, 0x39, 0x88, 0x1a, 0xfd, 0xc4, 0xd2, 0x92, 0x0c, 0xfd, 0x32, + 0xc3, 0xa4, 0xd4, 0x92, 0x44, 0x43, 0x30, 0x07, 0x22, 0xaf, 0x74, 0x85, 0x91, 0x4b, 0xd0, 0x13, + 0x6e, 0x97, 0x23, 0xc4, 0x2a, 0xa1, 0x04, 0x2e, 0x9e, 0xa4, 0xc4, 0xe2, 0x54, 0x98, 0xd5, 0x12, + 0x8c, 0x0a, 0x8c, 0x1a, 0xdc, 0x46, 0x0a, 0x7a, 0x50, 0x0b, 0xc1, 0xfa, 0xa1, 0x86, 0xe9, 0x39, + 0x25, 0x16, 0xa7, 0x42, 0xf5, 0x39, 0x49, 0x5f, 0xb8, 0x27, 0xcf, 0xf8, 0xe9, 0x9e, 0xbc, 0x70, + 0x65, 0x62, 0x6e, 0x8e, 0x95, 0x12, 0xb2, 0x19, 0x4a, 0x41, 0xdc, 0x49, 0x08, 0x95, 0x42, 0xb6, + 0x5c, 0xbc, 0x50, 0x89, 0xf8, 0xfc, 0xf2, 0xbc, 0xd4, 0x22, 0x09, 0x26, 0x05, 0x46, 0x0d, 0x4e, + 0x27, 0x89, 0x4f, 0xf7, 0xe4, 0x45, 0x20, 0x9a, 0x51, 0xa4, 0x95, 0x82, 0x78, 0xa0, 0x7c, 0x7f, + 0x10, 0xd7, 0x4a, 0xae, 0x63, 0x81, 0x3c, 0xc3, 0x8c, 0x05, 0xf2, 0x0c, 0x97, 0xb6, 0xe8, 0x0a, + 0x61, 0xb8, 0xdf, 0xd3, 0x29, 0xfe, 0xc4, 0x23, 0x39, 0xc6, 0x0b, 0x8f, 0xe4, 0x18, 0x1f, 0x3c, + 0x92, 0x63, 0x9c, 0xf0, 0x58, 0x8e, 0xe1, 0xc2, 0x63, 0x39, 0x86, 0x1b, 0x8f, 0xe5, 0x18, 0xa2, + 0x5c, 0xd3, 0x33, 0x4b, 0x32, 0x4a, 0x93, 0xf4, 0x92, 0xf3, 0x73, 0xa1, 0xe1, 0xa7, 0x9f, 0x99, + 0x94, 0xac, 0x9b, 0x9e, 0xaf, 0x5f, 0x66, 0xa4, 0x9f, 0x9b, 0x9f, 0x52, 0x9a, 0x93, 0x5a, 0x0c, + 0x8a, 0xc1, 0x62, 0x7d, 0x23, 0x73, 0x5d, 0x44, 0x2c, 0xe8, 0xc2, 0x23, 0xaf, 0xa4, 0xb2, 0x20, + 0xb5, 0x38, 0x89, 0x0d, 0x1c, 0x7c, 0xc6, 0x80, 0x00, 0x00, 0x00, 0xff, 0xff, 0xe7, 0xf4, 0x36, + 0xc4, 0xf1, 0x01, 0x00, 0x00, +} + +func (m *InterchainAccount) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *InterchainAccount) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *InterchainAccount) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if len(m.AccountOwner) > 0 { + i -= len(m.AccountOwner) + copy(dAtA[i:], m.AccountOwner) + i = encodeVarintAccount(dAtA, i, uint64(len(m.AccountOwner))) + i-- + dAtA[i] = 0x12 + } + if m.BaseAccount != nil { + { + size, err := m.BaseAccount.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintAccount(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func encodeVarintAccount(dAtA []byte, offset int, v uint64) int { + offset -= sovAccount(v) + base := offset + for v >= 1<<7 { + dAtA[offset] = uint8(v&0x7f | 0x80) + v >>= 7 + offset++ + } + dAtA[offset] = uint8(v) + return base +} +func (m *InterchainAccount) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.BaseAccount != nil { + l = m.BaseAccount.Size() + n += 1 + l + sovAccount(uint64(l)) + } + l = len(m.AccountOwner) + if l > 0 { + n += 1 + l + sovAccount(uint64(l)) + } + return n +} + +func sovAccount(x uint64) (n int) { + return (math_bits.Len64(x|1) + 6) / 7 +} +func sozAccount(x uint64) (n int) { + return sovAccount(uint64((x << 1) ^ uint64((int64(x) >> 63)))) +} +func (m *InterchainAccount) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowAccount + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: InterchainAccount: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: InterchainAccount: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field BaseAccount", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowAccount + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthAccount + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthAccount + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if m.BaseAccount == nil { + m.BaseAccount = &types.BaseAccount{} + } + if err := m.BaseAccount.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field AccountOwner", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowAccount + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthAccount + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthAccount + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.AccountOwner = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipAccount(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthAccount + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func skipAccount(dAtA []byte) (n int, err error) { + l := len(dAtA) + iNdEx := 0 + depth := 0 + for iNdEx < l { + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowAccount + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + wireType := int(wire & 0x7) + switch wireType { + case 0: + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowAccount + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + iNdEx++ + if dAtA[iNdEx-1] < 0x80 { + break + } + } + case 1: + iNdEx += 8 + case 2: + var length int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowAccount + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + length |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if length < 0 { + return 0, ErrInvalidLengthAccount + } + iNdEx += length + case 3: + depth++ + case 4: + if depth == 0 { + return 0, ErrUnexpectedEndOfGroupAccount + } + depth-- + case 5: + iNdEx += 4 + default: + return 0, fmt.Errorf("proto: illegal wireType %d", wireType) + } + if iNdEx < 0 { + return 0, ErrInvalidLengthAccount + } + if depth == 0 { + return iNdEx, nil + } + } + return 0, io.ErrUnexpectedEOF +} + +var ( + ErrInvalidLengthAccount = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowAccount = fmt.Errorf("proto: integer overflow") + ErrUnexpectedEndOfGroupAccount = fmt.Errorf("proto: unexpected end of group") +) diff --git a/modules/apps/27-interchain-accounts/types/account_test.go b/modules/apps/27-interchain-accounts/types/account_test.go new file mode 100644 index 0000000000..b3ad5a37b1 --- /dev/null +++ b/modules/apps/27-interchain-accounts/types/account_test.go @@ -0,0 +1,128 @@ +package types_test + +import ( + "encoding/json" + "fmt" + "testing" + + "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" + sdk "github.com/cosmos/cosmos-sdk/types" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + "github.com/stretchr/testify/suite" + + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" + ibctesting "github.com/cosmos/ibc-go/v2/testing" +) + +var ( + // TestOwnerAddress defines a reusable bech32 address for testing purposes + TestOwnerAddress = "cosmos17dtl0mjt3t77kpuhg2edqzjpszulwhgzuj9ljs" + // TestPortID defines a resuable port identifier for testing purposes + TestPortID, _ = types.GeneratePortID(TestOwnerAddress, "connection-0", "connection-0") +) + +type TypesTestSuite struct { + suite.Suite + + coordinator *ibctesting.Coordinator + + chainA *ibctesting.TestChain + chainB *ibctesting.TestChain +} + +func (suite *TypesTestSuite) SetupTest() { + suite.coordinator = ibctesting.NewCoordinator(suite.T(), 2) + + suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(0)) + suite.chainB = suite.coordinator.GetChain(ibctesting.GetChainID(1)) +} + +func TestTypesTestSuite(t *testing.T) { + suite.Run(t, new(TypesTestSuite)) +} + +func (suite *TypesTestSuite) TestGenerateAddress() { + addr := types.GenerateAddress([]byte{}, "test-port-id") + accAddr, err := sdk.AccAddressFromBech32(addr.String()) + + suite.Require().NoError(err, "TestGenerateAddress failed") + suite.Require().NotEmpty(accAddr) +} + +func (suite *TypesTestSuite) TestInterchainAccount() { + pubkey := secp256k1.GenPrivKey().PubKey() + addr := sdk.AccAddress(pubkey.Address()) + baseAcc := authtypes.NewBaseAccountWithAddress(addr) + interchainAcc := types.NewInterchainAccount(baseAcc, TestOwnerAddress) + + // should fail when trying to set the public key or sequence of an interchain account + err := interchainAcc.SetPubKey(pubkey) + suite.Require().Error(err) + err = interchainAcc.SetSequence(1) + suite.Require().Error(err) +} + +func (suite *TypesTestSuite) TestGenesisAccountValidate() { + pubkey := secp256k1.GenPrivKey().PubKey() + addr := sdk.AccAddress(pubkey.Address()) + baseAcc := authtypes.NewBaseAccountWithAddress(addr) + pubkey = secp256k1.GenPrivKey().PubKey() + ownerAddr := sdk.AccAddress(pubkey.Address()) + + testCases := []struct { + name string + acc authtypes.GenesisAccount + expPass bool + }{ + { + "success", + types.NewInterchainAccount(baseAcc, ownerAddr.String()), + true, + }, + { + "interchain account with empty AccountOwner field", + types.NewInterchainAccount(baseAcc, ""), + false, + }, + } + + for _, tc := range testCases { + err := tc.acc.Validate() + + if tc.expPass { + suite.Require().NoError(err) + } else { + suite.Require().Error(err) + } + } +} + +func (suite *TypesTestSuite) TestInterchainAccountMarshalYAML() { + addr := suite.chainA.SenderAccount.GetAddress() + baseAcc := authtypes.NewBaseAccountWithAddress(addr) + + interchainAcc := types.NewInterchainAccount(baseAcc, suite.chainB.SenderAccount.GetAddress().String()) + bz, err := interchainAcc.MarshalYAML() + suite.Require().NoError(err) + + expected := fmt.Sprintf("address: %s\npublic_key: \"\"\naccount_number: 0\nsequence: 0\naccount_owner: %s\n", suite.chainA.SenderAccount.GetAddress(), suite.chainB.SenderAccount.GetAddress()) + suite.Require().Equal(expected, string(bz)) +} + +func (suite *TypesTestSuite) TestInterchainAccountJSON() { + addr := suite.chainA.SenderAccount.GetAddress() + ba := authtypes.NewBaseAccountWithAddress(addr) + + interchainAcc := types.NewInterchainAccount(ba, suite.chainB.SenderAccount.GetAddress().String()) + + bz, err := json.Marshal(interchainAcc) + suite.Require().NoError(err) + + bz1, err := interchainAcc.MarshalJSON() + suite.Require().NoError(err) + suite.Require().Equal(string(bz), string(bz1)) + + var a types.InterchainAccount + suite.Require().NoError(json.Unmarshal(bz, &a)) + suite.Require().Equal(a.String(), interchainAcc.String()) +} diff --git a/modules/apps/27-interchain-accounts/types/codec.go b/modules/apps/27-interchain-accounts/types/codec.go new file mode 100644 index 0000000000..971554a83e --- /dev/null +++ b/modules/apps/27-interchain-accounts/types/codec.go @@ -0,0 +1,79 @@ +package types + +import ( + "github.com/cosmos/cosmos-sdk/codec" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" + sdk "github.com/cosmos/cosmos-sdk/types" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" +) + +var ( + // ModuleCdc references the global interchain accounts module codec. Note, the codec + // should ONLY be used in certain instances of tests and for JSON encoding. + // + // The actual codec used for serialization should be provided to interchain accounts and + // defined at the application level. + ModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry()) +) + +// RegisterLegacyAminoCodec registers the account interfaces and concrete types on the +// provided LegacyAmino codec. These types are used for Amino JSON serialization +func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { + cdc.RegisterInterface((*InterchainAccountI)(nil), nil) + cdc.RegisterConcrete(&InterchainAccount{}, "27-interchain-accounts/InterchainAccount", nil) +} + +// RegisterInterface associates protoName with AccountI interface +// and creates a registry of it's concrete implementations +func RegisterInterfaces(registry codectypes.InterfaceRegistry) { + registry.RegisterImplementations((*authtypes.AccountI)(nil), &InterchainAccount{}) +} + +// SerializeCosmosTx serializes a slice of sdk.Msg's using the CosmosTx type. The sdk.Msg's are +// packed into Any's and inserted into the Messages field of a CosmosTx. The proto marshaled CosmosTx +// bytes are returned. +func SerializeCosmosTx(cdc codec.BinaryCodec, msgs []sdk.Msg) (bz []byte, err error) { + msgAnys := make([]*codectypes.Any, len(msgs)) + + for i, msg := range msgs { + msgAnys[i], err = codectypes.NewAnyWithValue(msg) + if err != nil { + return nil, err + } + } + + cosmosTx := &CosmosTx{ + Messages: msgAnys, + } + + bz, err = cdc.Marshal(cosmosTx) + if err != nil { + return nil, err + } + + return bz, nil +} + +// DeserializeCosmosTx unmarshals and unpacks a slice of transaction bytes +// into a slice of sdk.Msg's. +func DeserializeCosmosTx(cdc codec.BinaryCodec, data []byte) ([]sdk.Msg, error) { + var cosmosTx CosmosTx + if err := cdc.Unmarshal(data, &cosmosTx); err != nil { + return nil, err + } + + msgs := make([]sdk.Msg, len(cosmosTx.Messages)) + + for i, any := range cosmosTx.Messages { + var msg sdk.Msg + + err := cdc.UnpackAny(any, &msg) + if err != nil { + return nil, err + } + + msgs[i] = msg + } + + return msgs, nil +} diff --git a/modules/apps/27-interchain-accounts/types/codec_test.go b/modules/apps/27-interchain-accounts/types/codec_test.go new file mode 100644 index 0000000000..49b2042c16 --- /dev/null +++ b/modules/apps/27-interchain-accounts/types/codec_test.go @@ -0,0 +1,130 @@ +package types_test + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" + + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" + "github.com/cosmos/ibc-go/v2/testing/simapp" +) + +// caseRawBytes defines a helper struct, used for testing codec operations +type caseRawBytes struct { + name string + bz []byte + expPass bool +} + +// mockSdkMsg defines a mock struct, used for testing codec error scenarios +type mockSdkMsg struct{} + +// Reset implements sdk.Msg +func (mockSdkMsg) Reset() { +} + +// String implements sdk.Msg +func (mockSdkMsg) String() string { + return "" +} + +// ProtoMessage implements sdk.Msg +func (mockSdkMsg) ProtoMessage() { +} + +// ValidateBasic implements sdk.Msg +func (mockSdkMsg) ValidateBasic() error { + return nil +} + +// GetSigners implements sdk.Msg +func (mockSdkMsg) GetSigners() []sdk.AccAddress { + return []sdk.AccAddress{} +} + +func (suite *TypesTestSuite) TestSerializeCosmosTx() { + + testCases := []struct { + name string + msgs []sdk.Msg + expPass bool + }{ + { + "single msg", + []sdk.Msg{ + &banktypes.MsgSend{ + FromAddress: TestOwnerAddress, + ToAddress: TestOwnerAddress, + Amount: sdk.NewCoins(sdk.NewCoin("bananas", sdk.NewInt(100))), + }, + }, + true, + }, + { + "multiple msgs, same types", + []sdk.Msg{ + &banktypes.MsgSend{ + FromAddress: TestOwnerAddress, + ToAddress: TestOwnerAddress, + Amount: sdk.NewCoins(sdk.NewCoin("bananas", sdk.NewInt(100))), + }, + &banktypes.MsgSend{ + FromAddress: TestOwnerAddress, + ToAddress: TestOwnerAddress, + Amount: sdk.NewCoins(sdk.NewCoin("bananas", sdk.NewInt(200))), + }, + }, + true, + }, + { + "multiple msgs, different types", + []sdk.Msg{ + &banktypes.MsgSend{ + FromAddress: TestOwnerAddress, + ToAddress: TestOwnerAddress, + Amount: sdk.NewCoins(sdk.NewCoin("bananas", sdk.NewInt(100))), + }, + &govtypes.MsgSubmitProposal{ + InitialDeposit: sdk.NewCoins(sdk.NewCoin("bananas", sdk.NewInt(100))), + Proposer: TestOwnerAddress, + }, + }, + true, + }, + { + "unregistered msg type", + []sdk.Msg{ + &mockSdkMsg{}, + }, + false, + }, + { + "multiple unregistered msg types", + []sdk.Msg{ + &mockSdkMsg{}, + &mockSdkMsg{}, + &mockSdkMsg{}, + }, + false, + }, + } + + testCasesAny := []caseRawBytes{} + + for _, tc := range testCases { + bz, err := types.SerializeCosmosTx(simapp.MakeTestEncodingConfig().Marshaler, tc.msgs) + suite.Require().NoError(err, tc.name) + + testCasesAny = append(testCasesAny, caseRawBytes{tc.name, bz, tc.expPass}) + } + + for i, tc := range testCasesAny { + msgs, err := types.DeserializeCosmosTx(simapp.MakeTestEncodingConfig().Marshaler, tc.bz) + if tc.expPass { + suite.Require().NoError(err, tc.name) + suite.Require().Equal(testCases[i].msgs, msgs, tc.name) + } else { + suite.Require().Error(err, tc.name) + } + } +} diff --git a/modules/apps/27-interchain-accounts/types/errors.go b/modules/apps/27-interchain-accounts/types/errors.go new file mode 100644 index 0000000000..6061c6ae24 --- /dev/null +++ b/modules/apps/27-interchain-accounts/types/errors.go @@ -0,0 +1,20 @@ +package types + +import ( + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" +) + +var ( + ErrUnknownDataType = sdkerrors.Register(ModuleName, 2, "unknown data type") + ErrAccountAlreadyExist = sdkerrors.Register(ModuleName, 3, "account already exist") + ErrPortAlreadyBound = sdkerrors.Register(ModuleName, 4, "port is already bound") + ErrInvalidChannelFlow = sdkerrors.Register(ModuleName, 5, "invalid message sent to channel end") + ErrInvalidOutgoingData = sdkerrors.Register(ModuleName, 6, "invalid outgoing data") + ErrInvalidRoute = sdkerrors.Register(ModuleName, 7, "invalid route") + ErrInterchainAccountNotFound = sdkerrors.Register(ModuleName, 8, "interchain account not found") + ErrInterchainAccountAlreadySet = sdkerrors.Register(ModuleName, 9, "interchain account is already set") + ErrActiveChannelNotFound = sdkerrors.Register(ModuleName, 10, "no active channel for this owner") + ErrInvalidVersion = sdkerrors.Register(ModuleName, 11, "invalid interchain accounts version") + ErrInvalidAccountAddress = sdkerrors.Register(ModuleName, 12, "invalid account address") + ErrUnsupported = sdkerrors.Register(ModuleName, 13, "interchain account does not support this action") +) diff --git a/modules/apps/27-interchain-accounts/types/expected_keepers.go b/modules/apps/27-interchain-accounts/types/expected_keepers.go new file mode 100644 index 0000000000..558a38c403 --- /dev/null +++ b/modules/apps/27-interchain-accounts/types/expected_keepers.go @@ -0,0 +1,37 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" + + channeltypes "github.com/cosmos/ibc-go/v2/modules/core/04-channel/types" + ibcexported "github.com/cosmos/ibc-go/v2/modules/core/exported" +) + +// AccountKeeper defines the expected account keeper +type AccountKeeper interface { + NewAccount(ctx sdk.Context, acc authtypes.AccountI) authtypes.AccountI + GetAccount(ctx sdk.Context, addr sdk.AccAddress) authtypes.AccountI + SetAccount(ctx sdk.Context, acc authtypes.AccountI) + GetModuleAccount(ctx sdk.Context, name string) authtypes.ModuleAccountI + GetModuleAddress(name string) sdk.AccAddress +} + +// ICS4Wrapper defines the expected ICS4Wrapper for middleware +type ICS4Wrapper interface { + SendPacket(ctx sdk.Context, channelCap *capabilitytypes.Capability, packet ibcexported.PacketI) error +} + +// ChannelKeeper defines the expected IBC channel keeper +type ChannelKeeper interface { + GetChannel(ctx sdk.Context, srcPort, srcChan string) (channel channeltypes.Channel, found bool) + GetNextSequenceSend(ctx sdk.Context, portID, channelID string) (uint64, bool) + CounterpartyHops(ctx sdk.Context, channel channeltypes.Channel) ([]string, bool) +} + +// PortKeeper defines the expected IBC port keeper +type PortKeeper interface { + BindPort(ctx sdk.Context, portID string) *capabilitytypes.Capability + IsBound(ctx sdk.Context, portID string) bool +} diff --git a/modules/apps/27-interchain-accounts/types/genesis.go b/modules/apps/27-interchain-accounts/types/genesis.go new file mode 100644 index 0000000000..97a906cf5f --- /dev/null +++ b/modules/apps/27-interchain-accounts/types/genesis.go @@ -0,0 +1,139 @@ +package types + +import ( + controllertypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/controller/types" + hosttypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/host/types" + host "github.com/cosmos/ibc-go/v2/modules/core/24-host" +) + +// DefaultGenesis creates and returns the interchain accounts GenesisState +func DefaultGenesis() *GenesisState { + return &GenesisState{ + ControllerGenesisState: DefaultControllerGenesis(), + HostGenesisState: DefaultHostGenesis(), + } +} + +// NewGenesisState creates and returns a new GenesisState instance from the provided controller and host genesis state types +func NewGenesisState(controllerGenesisState ControllerGenesisState, hostGenesisState HostGenesisState) *GenesisState { + return &GenesisState{ + ControllerGenesisState: controllerGenesisState, + HostGenesisState: hostGenesisState, + } +} + +// Validate performs basic validation of the interchain accounts GenesisState +func (gs GenesisState) Validate() error { + if err := gs.ControllerGenesisState.Validate(); err != nil { + return err + } + + if err := gs.HostGenesisState.Validate(); err != nil { + return err + } + + return nil +} + +// DefaultControllerGenesis creates and returns the default interchain accounts ControllerGenesisState +func DefaultControllerGenesis() ControllerGenesisState { + return ControllerGenesisState{ + Params: controllertypes.DefaultParams(), + } +} + +// NewControllerGenesisState creates a returns a new ControllerGenesisState instance +func NewControllerGenesisState(channels []ActiveChannel, accounts []RegisteredInterchainAccount, ports []string, controllerParams controllertypes.Params) ControllerGenesisState { + return ControllerGenesisState{ + ActiveChannels: channels, + InterchainAccounts: accounts, + Ports: ports, + Params: controllerParams, + } +} + +// Validate performs basic validation of the ControllerGenesisState +func (gs ControllerGenesisState) Validate() error { + for _, ch := range gs.ActiveChannels { + if err := host.ChannelIdentifierValidator(ch.ChannelId); err != nil { + return err + } + + if err := host.PortIdentifierValidator(ch.PortId); err != nil { + return err + } + } + + for _, acc := range gs.InterchainAccounts { + if err := host.PortIdentifierValidator(acc.PortId); err != nil { + return err + } + + if err := ValidateAccountAddress(acc.AccountAddress); err != nil { + return err + } + } + + for _, port := range gs.Ports { + if err := host.PortIdentifierValidator(port); err != nil { + return err + } + } + + if err := gs.Params.Validate(); err != nil { + return err + } + + return nil +} + +// DefaultHostGenesis creates and returns the default interchain accounts HostGenesisState +func DefaultHostGenesis() HostGenesisState { + return HostGenesisState{ + Port: PortID, + Params: hosttypes.DefaultParams(), + } +} + +// NewHostGenesisState creates a returns a new HostGenesisState instance +func NewHostGenesisState(channels []ActiveChannel, accounts []RegisteredInterchainAccount, port string, hostParams hosttypes.Params) HostGenesisState { + return HostGenesisState{ + ActiveChannels: channels, + InterchainAccounts: accounts, + Port: port, + Params: hostParams, + } +} + +// Validate performs basic validation of the HostGenesisState +func (gs HostGenesisState) Validate() error { + for _, ch := range gs.ActiveChannels { + if err := host.ChannelIdentifierValidator(ch.ChannelId); err != nil { + return err + } + + if err := host.PortIdentifierValidator(ch.PortId); err != nil { + return err + } + } + + for _, acc := range gs.InterchainAccounts { + if err := host.PortIdentifierValidator(acc.PortId); err != nil { + return err + } + + if err := ValidateAccountAddress(acc.AccountAddress); err != nil { + return err + } + } + + if err := host.PortIdentifierValidator(gs.Port); err != nil { + return err + } + + if err := gs.Params.Validate(); err != nil { + return err + } + + return nil +} diff --git a/modules/apps/27-interchain-accounts/types/genesis.pb.go b/modules/apps/27-interchain-accounts/types/genesis.pb.go new file mode 100644 index 0000000000..5a226263fb --- /dev/null +++ b/modules/apps/27-interchain-accounts/types/genesis.pb.go @@ -0,0 +1,1548 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: ibc/applications/interchain_accounts/v1/genesis.proto + +package types + +import ( + fmt "fmt" + types "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/controller/types" + types1 "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/host/types" + _ "github.com/gogo/protobuf/gogoproto" + proto "github.com/gogo/protobuf/proto" + io "io" + math "math" + math_bits "math/bits" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package + +// GenesisState defines the interchain accounts genesis state +type GenesisState struct { + ControllerGenesisState ControllerGenesisState `protobuf:"bytes,1,opt,name=controller_genesis_state,json=controllerGenesisState,proto3" json:"controller_genesis_state" yaml:"controller_genesis_state"` + HostGenesisState HostGenesisState `protobuf:"bytes,2,opt,name=host_genesis_state,json=hostGenesisState,proto3" json:"host_genesis_state" yaml:"host_genesis_state"` +} + +func (m *GenesisState) Reset() { *m = GenesisState{} } +func (m *GenesisState) String() string { return proto.CompactTextString(m) } +func (*GenesisState) ProtoMessage() {} +func (*GenesisState) Descriptor() ([]byte, []int) { + return fileDescriptor_629b3ced0911516b, []int{0} +} +func (m *GenesisState) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *GenesisState) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_GenesisState.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *GenesisState) XXX_Merge(src proto.Message) { + xxx_messageInfo_GenesisState.Merge(m, src) +} +func (m *GenesisState) XXX_Size() int { + return m.Size() +} +func (m *GenesisState) XXX_DiscardUnknown() { + xxx_messageInfo_GenesisState.DiscardUnknown(m) +} + +var xxx_messageInfo_GenesisState proto.InternalMessageInfo + +func (m *GenesisState) GetControllerGenesisState() ControllerGenesisState { + if m != nil { + return m.ControllerGenesisState + } + return ControllerGenesisState{} +} + +func (m *GenesisState) GetHostGenesisState() HostGenesisState { + if m != nil { + return m.HostGenesisState + } + return HostGenesisState{} +} + +// ControllerGenesisState defines the interchain accounts controller genesis state +type ControllerGenesisState struct { + ActiveChannels []ActiveChannel `protobuf:"bytes,1,rep,name=active_channels,json=activeChannels,proto3" json:"active_channels" yaml:"active_channels"` + InterchainAccounts []RegisteredInterchainAccount `protobuf:"bytes,2,rep,name=interchain_accounts,json=interchainAccounts,proto3" json:"interchain_accounts" yaml:"interchain_accounts"` + Ports []string `protobuf:"bytes,3,rep,name=ports,proto3" json:"ports,omitempty"` + Params types.Params `protobuf:"bytes,4,opt,name=params,proto3" json:"params"` +} + +func (m *ControllerGenesisState) Reset() { *m = ControllerGenesisState{} } +func (m *ControllerGenesisState) String() string { return proto.CompactTextString(m) } +func (*ControllerGenesisState) ProtoMessage() {} +func (*ControllerGenesisState) Descriptor() ([]byte, []int) { + return fileDescriptor_629b3ced0911516b, []int{1} +} +func (m *ControllerGenesisState) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *ControllerGenesisState) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_ControllerGenesisState.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *ControllerGenesisState) XXX_Merge(src proto.Message) { + xxx_messageInfo_ControllerGenesisState.Merge(m, src) +} +func (m *ControllerGenesisState) XXX_Size() int { + return m.Size() +} +func (m *ControllerGenesisState) XXX_DiscardUnknown() { + xxx_messageInfo_ControllerGenesisState.DiscardUnknown(m) +} + +var xxx_messageInfo_ControllerGenesisState proto.InternalMessageInfo + +func (m *ControllerGenesisState) GetActiveChannels() []ActiveChannel { + if m != nil { + return m.ActiveChannels + } + return nil +} + +func (m *ControllerGenesisState) GetInterchainAccounts() []RegisteredInterchainAccount { + if m != nil { + return m.InterchainAccounts + } + return nil +} + +func (m *ControllerGenesisState) GetPorts() []string { + if m != nil { + return m.Ports + } + return nil +} + +func (m *ControllerGenesisState) GetParams() types.Params { + if m != nil { + return m.Params + } + return types.Params{} +} + +// HostGenesisState defines the interchain accounts host genesis state +type HostGenesisState struct { + ActiveChannels []ActiveChannel `protobuf:"bytes,1,rep,name=active_channels,json=activeChannels,proto3" json:"active_channels" yaml:"active_channels"` + InterchainAccounts []RegisteredInterchainAccount `protobuf:"bytes,2,rep,name=interchain_accounts,json=interchainAccounts,proto3" json:"interchain_accounts" yaml:"interchain_accounts"` + Port string `protobuf:"bytes,3,opt,name=port,proto3" json:"port,omitempty"` + Params types1.Params `protobuf:"bytes,4,opt,name=params,proto3" json:"params"` +} + +func (m *HostGenesisState) Reset() { *m = HostGenesisState{} } +func (m *HostGenesisState) String() string { return proto.CompactTextString(m) } +func (*HostGenesisState) ProtoMessage() {} +func (*HostGenesisState) Descriptor() ([]byte, []int) { + return fileDescriptor_629b3ced0911516b, []int{2} +} +func (m *HostGenesisState) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *HostGenesisState) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_HostGenesisState.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *HostGenesisState) XXX_Merge(src proto.Message) { + xxx_messageInfo_HostGenesisState.Merge(m, src) +} +func (m *HostGenesisState) XXX_Size() int { + return m.Size() +} +func (m *HostGenesisState) XXX_DiscardUnknown() { + xxx_messageInfo_HostGenesisState.DiscardUnknown(m) +} + +var xxx_messageInfo_HostGenesisState proto.InternalMessageInfo + +func (m *HostGenesisState) GetActiveChannels() []ActiveChannel { + if m != nil { + return m.ActiveChannels + } + return nil +} + +func (m *HostGenesisState) GetInterchainAccounts() []RegisteredInterchainAccount { + if m != nil { + return m.InterchainAccounts + } + return nil +} + +func (m *HostGenesisState) GetPort() string { + if m != nil { + return m.Port + } + return "" +} + +func (m *HostGenesisState) GetParams() types1.Params { + if m != nil { + return m.Params + } + return types1.Params{} +} + +// ActiveChannel contains a pairing of port ID and channel ID for an active interchain accounts channel +type ActiveChannel struct { + PortId string `protobuf:"bytes,1,opt,name=port_id,json=portId,proto3" json:"port_id,omitempty" yaml:"port_id"` + ChannelId string `protobuf:"bytes,2,opt,name=channel_id,json=channelId,proto3" json:"channel_id,omitempty" yaml:"channel_id"` +} + +func (m *ActiveChannel) Reset() { *m = ActiveChannel{} } +func (m *ActiveChannel) String() string { return proto.CompactTextString(m) } +func (*ActiveChannel) ProtoMessage() {} +func (*ActiveChannel) Descriptor() ([]byte, []int) { + return fileDescriptor_629b3ced0911516b, []int{3} +} +func (m *ActiveChannel) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *ActiveChannel) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_ActiveChannel.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *ActiveChannel) XXX_Merge(src proto.Message) { + xxx_messageInfo_ActiveChannel.Merge(m, src) +} +func (m *ActiveChannel) XXX_Size() int { + return m.Size() +} +func (m *ActiveChannel) XXX_DiscardUnknown() { + xxx_messageInfo_ActiveChannel.DiscardUnknown(m) +} + +var xxx_messageInfo_ActiveChannel proto.InternalMessageInfo + +func (m *ActiveChannel) GetPortId() string { + if m != nil { + return m.PortId + } + return "" +} + +func (m *ActiveChannel) GetChannelId() string { + if m != nil { + return m.ChannelId + } + return "" +} + +// RegisteredInterchainAccount contains a pairing of controller port ID and associated interchain account address +type RegisteredInterchainAccount struct { + PortId string `protobuf:"bytes,1,opt,name=port_id,json=portId,proto3" json:"port_id,omitempty" yaml:"port_id"` + AccountAddress string `protobuf:"bytes,2,opt,name=account_address,json=accountAddress,proto3" json:"account_address,omitempty" yaml:"account_address"` +} + +func (m *RegisteredInterchainAccount) Reset() { *m = RegisteredInterchainAccount{} } +func (m *RegisteredInterchainAccount) String() string { return proto.CompactTextString(m) } +func (*RegisteredInterchainAccount) ProtoMessage() {} +func (*RegisteredInterchainAccount) Descriptor() ([]byte, []int) { + return fileDescriptor_629b3ced0911516b, []int{4} +} +func (m *RegisteredInterchainAccount) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *RegisteredInterchainAccount) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_RegisteredInterchainAccount.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *RegisteredInterchainAccount) XXX_Merge(src proto.Message) { + xxx_messageInfo_RegisteredInterchainAccount.Merge(m, src) +} +func (m *RegisteredInterchainAccount) XXX_Size() int { + return m.Size() +} +func (m *RegisteredInterchainAccount) XXX_DiscardUnknown() { + xxx_messageInfo_RegisteredInterchainAccount.DiscardUnknown(m) +} + +var xxx_messageInfo_RegisteredInterchainAccount proto.InternalMessageInfo + +func (m *RegisteredInterchainAccount) GetPortId() string { + if m != nil { + return m.PortId + } + return "" +} + +func (m *RegisteredInterchainAccount) GetAccountAddress() string { + if m != nil { + return m.AccountAddress + } + return "" +} + +func init() { + proto.RegisterType((*GenesisState)(nil), "ibc.applications.interchain_accounts.v1.GenesisState") + proto.RegisterType((*ControllerGenesisState)(nil), "ibc.applications.interchain_accounts.v1.ControllerGenesisState") + proto.RegisterType((*HostGenesisState)(nil), "ibc.applications.interchain_accounts.v1.HostGenesisState") + proto.RegisterType((*ActiveChannel)(nil), "ibc.applications.interchain_accounts.v1.ActiveChannel") + proto.RegisterType((*RegisteredInterchainAccount)(nil), "ibc.applications.interchain_accounts.v1.RegisteredInterchainAccount") +} + +func init() { + proto.RegisterFile("ibc/applications/interchain_accounts/v1/genesis.proto", fileDescriptor_629b3ced0911516b) +} + +var fileDescriptor_629b3ced0911516b = []byte{ + // 609 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xec, 0x94, 0xcf, 0x6f, 0xd3, 0x30, + 0x14, 0xc7, 0x9b, 0x66, 0x0c, 0xd5, 0x83, 0x31, 0xcc, 0x98, 0x42, 0x91, 0xd2, 0xe2, 0xcb, 0x2a, + 0xa1, 0x25, 0x5a, 0x19, 0x4c, 0xec, 0x82, 0x96, 0x82, 0x60, 0x37, 0x14, 0x2e, 0x88, 0x4b, 0xe4, + 0x3a, 0x56, 0x6a, 0x29, 0x8d, 0xa3, 0xd8, 0xab, 0xb4, 0x13, 0x77, 0x2e, 0x70, 0x43, 0x5c, 0x91, + 0xf8, 0x3f, 0x38, 0xee, 0xb8, 0x23, 0xa7, 0x0a, 0xad, 0xff, 0x41, 0xff, 0x02, 0x64, 0x27, 0xea, + 0x8f, 0xd0, 0x4d, 0xe1, 0xce, 0xa9, 0x76, 0xfc, 0xbe, 0xdf, 0xf7, 0x79, 0x7e, 0xee, 0x03, 0x4f, + 0x59, 0x9f, 0xb8, 0x38, 0x4d, 0x63, 0x46, 0xb0, 0x64, 0x3c, 0x11, 0x2e, 0x4b, 0x24, 0xcd, 0xc8, + 0x00, 0xb3, 0x24, 0xc0, 0x84, 0xf0, 0xd3, 0x44, 0x0a, 0x77, 0xb4, 0xef, 0x46, 0x34, 0xa1, 0x82, + 0x09, 0x27, 0xcd, 0xb8, 0xe4, 0x70, 0x97, 0xf5, 0x89, 0xb3, 0x28, 0x73, 0x56, 0xc8, 0x9c, 0xd1, + 0x7e, 0x73, 0x3b, 0xe2, 0x11, 0xd7, 0x1a, 0x57, 0xad, 0x72, 0x79, 0xb3, 0x57, 0x29, 0x2b, 0xe1, + 0x89, 0xcc, 0x78, 0x1c, 0xd3, 0x4c, 0x01, 0xcc, 0x77, 0x85, 0xc9, 0x61, 0x25, 0x93, 0x01, 0x17, + 0x52, 0xc9, 0xd5, 0x6f, 0x2e, 0x44, 0x3f, 0xeb, 0xe0, 0xd6, 0xeb, 0xbc, 0x9c, 0x77, 0x12, 0x4b, + 0x0a, 0xbf, 0x1b, 0xc0, 0x9a, 0xdb, 0x07, 0x45, 0xa9, 0x81, 0x50, 0x87, 0x96, 0xd1, 0x36, 0x3a, + 0x1b, 0xdd, 0x17, 0x4e, 0xc5, 0x8a, 0x9d, 0xde, 0xcc, 0x68, 0x31, 0x87, 0xb7, 0x7b, 0x3e, 0x6e, + 0xd5, 0xa6, 0xe3, 0x56, 0xeb, 0x0c, 0x0f, 0xe3, 0x23, 0x74, 0x55, 0x3a, 0xe4, 0xef, 0x90, 0x95, + 0x06, 0xf0, 0x93, 0x01, 0xa0, 0x2a, 0xa2, 0x84, 0x57, 0xd7, 0x78, 0xcf, 0x2b, 0xe3, 0xbd, 0xe1, + 0x42, 0x2e, 0x81, 0x3d, 0x2a, 0xc0, 0x1e, 0xe4, 0x60, 0x7f, 0xa7, 0x40, 0xfe, 0xd6, 0xa0, 0x24, + 0x42, 0x3f, 0x4c, 0xb0, 0xb3, 0xba, 0x50, 0xf8, 0x11, 0xdc, 0xc1, 0x44, 0xb2, 0x11, 0x0d, 0xc8, + 0x00, 0x27, 0x09, 0x8d, 0x85, 0x65, 0xb4, 0xcd, 0xce, 0x46, 0xf7, 0x59, 0x65, 0xc6, 0x63, 0xad, + 0xef, 0xe5, 0x72, 0xcf, 0x2e, 0x00, 0x77, 0x72, 0xc0, 0x92, 0x39, 0xf2, 0x37, 0xf1, 0x62, 0xb8, + 0x80, 0xdf, 0x0c, 0x70, 0x6f, 0x85, 0xb1, 0x55, 0xd7, 0x14, 0x2f, 0x2b, 0x53, 0xf8, 0x34, 0x62, + 0x42, 0xd2, 0x8c, 0x86, 0x27, 0xb3, 0x80, 0xe3, 0xfc, 0xdc, 0x43, 0x05, 0x53, 0x33, 0x67, 0x5a, + 0xe1, 0x80, 0x7c, 0xc8, 0xca, 0x32, 0x01, 0xb7, 0xc1, 0x8d, 0x94, 0x67, 0x52, 0x58, 0x66, 0xdb, + 0xec, 0x34, 0xfc, 0x7c, 0x03, 0xdf, 0x83, 0xf5, 0x14, 0x67, 0x78, 0x28, 0xac, 0x35, 0xdd, 0xcd, + 0xa3, 0x6a, 0x8c, 0x0b, 0xff, 0x88, 0xd1, 0xbe, 0xf3, 0x56, 0x3b, 0x78, 0x6b, 0x8a, 0xcc, 0x2f, + 0xfc, 0xd0, 0x57, 0x13, 0x6c, 0x95, 0x3b, 0xfe, 0xbf, 0x43, 0xd7, 0x75, 0x08, 0x82, 0x35, 0xd5, + 0x14, 0xcb, 0x6c, 0x1b, 0x9d, 0x86, 0xaf, 0xd7, 0xd0, 0x2f, 0xf5, 0xe7, 0xa0, 0x1a, 0xa1, 0x1e, + 0x39, 0x57, 0x75, 0x26, 0x03, 0xb7, 0x97, 0x2e, 0x11, 0x3e, 0x06, 0x37, 0x55, 0xb2, 0x80, 0x85, + 0x7a, 0xe4, 0x34, 0x3c, 0x38, 0x1d, 0xb7, 0x36, 0x73, 0xfa, 0xe2, 0x00, 0xf9, 0xeb, 0x6a, 0x75, + 0x12, 0xc2, 0x03, 0x00, 0x8a, 0xeb, 0x55, 0xf1, 0x75, 0x1d, 0x7f, 0x7f, 0x3a, 0x6e, 0xdd, 0x2d, + 0xa6, 0xcb, 0xec, 0x0c, 0xf9, 0x8d, 0x62, 0x73, 0x12, 0xa2, 0xcf, 0x06, 0x78, 0x78, 0xcd, 0x9d, + 0xfd, 0x1b, 0x42, 0x4f, 0xbd, 0x22, 0xad, 0x0b, 0x70, 0x18, 0x66, 0x54, 0x88, 0x82, 0xa3, 0xb9, + 0xf8, 0x12, 0x96, 0x02, 0xf4, 0x4b, 0xd0, 0x5f, 0x8e, 0xf3, 0x0f, 0x5e, 0x70, 0x7e, 0x69, 0x1b, + 0x17, 0x97, 0xb6, 0xf1, 0xfb, 0xd2, 0x36, 0xbe, 0x4c, 0xec, 0xda, 0xc5, 0xc4, 0xae, 0xfd, 0x9a, + 0xd8, 0xb5, 0x0f, 0xaf, 0x22, 0x26, 0x07, 0xa7, 0x7d, 0x87, 0xf0, 0xa1, 0x4b, 0xb8, 0x18, 0x72, + 0xe1, 0xb2, 0x3e, 0xd9, 0x8b, 0xb8, 0x3b, 0xea, 0xba, 0x43, 0x1e, 0x9e, 0xc6, 0x54, 0xa8, 0xe9, + 0x2f, 0xdc, 0xee, 0xe1, 0xde, 0xfc, 0xf6, 0xf7, 0x66, 0x83, 0x5f, 0x9e, 0xa5, 0x54, 0xf4, 0xd7, + 0xf5, 0xc8, 0x7f, 0xf2, 0x27, 0x00, 0x00, 0xff, 0xff, 0x61, 0x4a, 0xb3, 0xe7, 0xe8, 0x06, 0x00, + 0x00, +} + +func (m *GenesisState) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *GenesisState) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + { + size, err := m.HostGenesisState.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintGenesis(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x12 + { + size, err := m.ControllerGenesisState.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintGenesis(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0xa + return len(dAtA) - i, nil +} + +func (m *ControllerGenesisState) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *ControllerGenesisState) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *ControllerGenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + { + size, err := m.Params.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintGenesis(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x22 + if len(m.Ports) > 0 { + for iNdEx := len(m.Ports) - 1; iNdEx >= 0; iNdEx-- { + i -= len(m.Ports[iNdEx]) + copy(dAtA[i:], m.Ports[iNdEx]) + i = encodeVarintGenesis(dAtA, i, uint64(len(m.Ports[iNdEx]))) + i-- + dAtA[i] = 0x1a + } + } + if len(m.InterchainAccounts) > 0 { + for iNdEx := len(m.InterchainAccounts) - 1; iNdEx >= 0; iNdEx-- { + { + size, err := m.InterchainAccounts[iNdEx].MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintGenesis(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x12 + } + } + if len(m.ActiveChannels) > 0 { + for iNdEx := len(m.ActiveChannels) - 1; iNdEx >= 0; iNdEx-- { + { + size, err := m.ActiveChannels[iNdEx].MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintGenesis(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0xa + } + } + return len(dAtA) - i, nil +} + +func (m *HostGenesisState) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *HostGenesisState) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *HostGenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + { + size, err := m.Params.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintGenesis(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x22 + if len(m.Port) > 0 { + i -= len(m.Port) + copy(dAtA[i:], m.Port) + i = encodeVarintGenesis(dAtA, i, uint64(len(m.Port))) + i-- + dAtA[i] = 0x1a + } + if len(m.InterchainAccounts) > 0 { + for iNdEx := len(m.InterchainAccounts) - 1; iNdEx >= 0; iNdEx-- { + { + size, err := m.InterchainAccounts[iNdEx].MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintGenesis(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x12 + } + } + if len(m.ActiveChannels) > 0 { + for iNdEx := len(m.ActiveChannels) - 1; iNdEx >= 0; iNdEx-- { + { + size, err := m.ActiveChannels[iNdEx].MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintGenesis(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0xa + } + } + return len(dAtA) - i, nil +} + +func (m *ActiveChannel) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *ActiveChannel) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *ActiveChannel) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if len(m.ChannelId) > 0 { + i -= len(m.ChannelId) + copy(dAtA[i:], m.ChannelId) + i = encodeVarintGenesis(dAtA, i, uint64(len(m.ChannelId))) + i-- + dAtA[i] = 0x12 + } + if len(m.PortId) > 0 { + i -= len(m.PortId) + copy(dAtA[i:], m.PortId) + i = encodeVarintGenesis(dAtA, i, uint64(len(m.PortId))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func (m *RegisteredInterchainAccount) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *RegisteredInterchainAccount) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *RegisteredInterchainAccount) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if len(m.AccountAddress) > 0 { + i -= len(m.AccountAddress) + copy(dAtA[i:], m.AccountAddress) + i = encodeVarintGenesis(dAtA, i, uint64(len(m.AccountAddress))) + i-- + dAtA[i] = 0x12 + } + if len(m.PortId) > 0 { + i -= len(m.PortId) + copy(dAtA[i:], m.PortId) + i = encodeVarintGenesis(dAtA, i, uint64(len(m.PortId))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func encodeVarintGenesis(dAtA []byte, offset int, v uint64) int { + offset -= sovGenesis(v) + base := offset + for v >= 1<<7 { + dAtA[offset] = uint8(v&0x7f | 0x80) + v >>= 7 + offset++ + } + dAtA[offset] = uint8(v) + return base +} +func (m *GenesisState) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = m.ControllerGenesisState.Size() + n += 1 + l + sovGenesis(uint64(l)) + l = m.HostGenesisState.Size() + n += 1 + l + sovGenesis(uint64(l)) + return n +} + +func (m *ControllerGenesisState) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if len(m.ActiveChannels) > 0 { + for _, e := range m.ActiveChannels { + l = e.Size() + n += 1 + l + sovGenesis(uint64(l)) + } + } + if len(m.InterchainAccounts) > 0 { + for _, e := range m.InterchainAccounts { + l = e.Size() + n += 1 + l + sovGenesis(uint64(l)) + } + } + if len(m.Ports) > 0 { + for _, s := range m.Ports { + l = len(s) + n += 1 + l + sovGenesis(uint64(l)) + } + } + l = m.Params.Size() + n += 1 + l + sovGenesis(uint64(l)) + return n +} + +func (m *HostGenesisState) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if len(m.ActiveChannels) > 0 { + for _, e := range m.ActiveChannels { + l = e.Size() + n += 1 + l + sovGenesis(uint64(l)) + } + } + if len(m.InterchainAccounts) > 0 { + for _, e := range m.InterchainAccounts { + l = e.Size() + n += 1 + l + sovGenesis(uint64(l)) + } + } + l = len(m.Port) + if l > 0 { + n += 1 + l + sovGenesis(uint64(l)) + } + l = m.Params.Size() + n += 1 + l + sovGenesis(uint64(l)) + return n +} + +func (m *ActiveChannel) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.PortId) + if l > 0 { + n += 1 + l + sovGenesis(uint64(l)) + } + l = len(m.ChannelId) + if l > 0 { + n += 1 + l + sovGenesis(uint64(l)) + } + return n +} + +func (m *RegisteredInterchainAccount) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.PortId) + if l > 0 { + n += 1 + l + sovGenesis(uint64(l)) + } + l = len(m.AccountAddress) + if l > 0 { + n += 1 + l + sovGenesis(uint64(l)) + } + return n +} + +func sovGenesis(x uint64) (n int) { + return (math_bits.Len64(x|1) + 6) / 7 +} +func sozGenesis(x uint64) (n int) { + return sovGenesis(uint64((x << 1) ^ uint64((int64(x) >> 63)))) +} +func (m *GenesisState) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: GenesisState: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: GenesisState: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field ControllerGenesisState", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthGenesis + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthGenesis + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.ControllerGenesisState.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field HostGenesisState", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthGenesis + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthGenesis + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.HostGenesisState.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipGenesis(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthGenesis + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *ControllerGenesisState) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: ControllerGenesisState: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: ControllerGenesisState: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field ActiveChannels", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthGenesis + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthGenesis + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.ActiveChannels = append(m.ActiveChannels, ActiveChannel{}) + if err := m.ActiveChannels[len(m.ActiveChannels)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field InterchainAccounts", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthGenesis + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthGenesis + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.InterchainAccounts = append(m.InterchainAccounts, RegisteredInterchainAccount{}) + if err := m.InterchainAccounts[len(m.InterchainAccounts)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Ports", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthGenesis + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthGenesis + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Ports = append(m.Ports, string(dAtA[iNdEx:postIndex])) + iNdEx = postIndex + case 4: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Params", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthGenesis + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthGenesis + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.Params.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipGenesis(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthGenesis + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *HostGenesisState) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: HostGenesisState: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: HostGenesisState: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field ActiveChannels", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthGenesis + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthGenesis + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.ActiveChannels = append(m.ActiveChannels, ActiveChannel{}) + if err := m.ActiveChannels[len(m.ActiveChannels)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field InterchainAccounts", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthGenesis + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthGenesis + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.InterchainAccounts = append(m.InterchainAccounts, RegisteredInterchainAccount{}) + if err := m.InterchainAccounts[len(m.InterchainAccounts)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Port", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthGenesis + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthGenesis + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Port = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 4: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Params", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthGenesis + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthGenesis + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.Params.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipGenesis(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthGenesis + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *ActiveChannel) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: ActiveChannel: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: ActiveChannel: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field PortId", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthGenesis + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthGenesis + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.PortId = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field ChannelId", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthGenesis + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthGenesis + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.ChannelId = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipGenesis(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthGenesis + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *RegisteredInterchainAccount) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: RegisteredInterchainAccount: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: RegisteredInterchainAccount: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field PortId", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthGenesis + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthGenesis + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.PortId = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field AccountAddress", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthGenesis + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthGenesis + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.AccountAddress = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipGenesis(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthGenesis + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func skipGenesis(dAtA []byte) (n int, err error) { + l := len(dAtA) + iNdEx := 0 + depth := 0 + for iNdEx < l { + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowGenesis + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + wireType := int(wire & 0x7) + switch wireType { + case 0: + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowGenesis + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + iNdEx++ + if dAtA[iNdEx-1] < 0x80 { + break + } + } + case 1: + iNdEx += 8 + case 2: + var length int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowGenesis + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + length |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if length < 0 { + return 0, ErrInvalidLengthGenesis + } + iNdEx += length + case 3: + depth++ + case 4: + if depth == 0 { + return 0, ErrUnexpectedEndOfGroupGenesis + } + depth-- + case 5: + iNdEx += 4 + default: + return 0, fmt.Errorf("proto: illegal wireType %d", wireType) + } + if iNdEx < 0 { + return 0, ErrInvalidLengthGenesis + } + if depth == 0 { + return iNdEx, nil + } + } + return 0, io.ErrUnexpectedEOF +} + +var ( + ErrInvalidLengthGenesis = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowGenesis = fmt.Errorf("proto: integer overflow") + ErrUnexpectedEndOfGroupGenesis = fmt.Errorf("proto: unexpected end of group") +) diff --git a/modules/apps/27-interchain-accounts/types/genesis_test.go b/modules/apps/27-interchain-accounts/types/genesis_test.go new file mode 100644 index 0000000000..2ac017c63a --- /dev/null +++ b/modules/apps/27-interchain-accounts/types/genesis_test.go @@ -0,0 +1,313 @@ +package types_test + +import ( + controllertypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/controller/types" + hosttypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/host/types" + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" + ibctesting "github.com/cosmos/ibc-go/v2/testing" +) + +func (suite *TypesTestSuite) TestValidateGenesisState() { + var ( + genesisState types.GenesisState + ) + + testCases := []struct { + name string + malleate func() + expPass bool + }{ + { + "success", + func() {}, + true, + }, + { + "failed to validate - empty value", + func() { + genesisState = types.GenesisState{} + }, + false, + }, + { + "failed to validate - invalid controller genesis", + func() { + genesisState = *types.NewGenesisState(types.ControllerGenesisState{Ports: []string{"invalid|port"}}, types.DefaultHostGenesis()) + }, + false, + }, + { + "failed to validate - invalid host genesis", + func() { + genesisState = *types.NewGenesisState(types.DefaultControllerGenesis(), types.HostGenesisState{}) + }, + false, + }, + } + + for _, tc := range testCases { + suite.Run(tc.name, func() { + genesisState = *types.DefaultGenesis() + + tc.malleate() // malleate mutates test data + + err := genesisState.Validate() + + if tc.expPass { + suite.Require().NoError(err, tc.name) + } else { + suite.Require().Error(err, tc.name) + } + }) + } +} + +func (suite *TypesTestSuite) TestValidateControllerGenesisState() { + var ( + genesisState types.ControllerGenesisState + ) + + testCases := []struct { + name string + malleate func() + expPass bool + }{ + { + "success", + func() {}, + true, + }, + { + "failed to validate active channel - invalid port identifier", + func() { + activeChannels := []types.ActiveChannel{ + { + PortId: "invalid|port", + ChannelId: ibctesting.FirstChannelID, + }, + } + + genesisState = types.NewControllerGenesisState(activeChannels, []types.RegisteredInterchainAccount{}, []string{}, controllertypes.DefaultParams()) + }, + false, + }, + { + "failed to validate active channel - invalid channel identifier", + func() { + activeChannels := []types.ActiveChannel{ + { + PortId: TestPortID, + ChannelId: "invalid|channel", + }, + } + + genesisState = types.NewControllerGenesisState(activeChannels, []types.RegisteredInterchainAccount{}, []string{}, controllertypes.DefaultParams()) + }, + false, + }, + { + "failed to validate registered account - invalid port identifier", + func() { + activeChannels := []types.ActiveChannel{ + { + PortId: TestPortID, + ChannelId: ibctesting.FirstChannelID, + }, + } + + registeredAccounts := []types.RegisteredInterchainAccount{ + { + PortId: "invalid|port", + AccountAddress: TestOwnerAddress, + }, + } + + genesisState = types.NewControllerGenesisState(activeChannels, registeredAccounts, []string{}, controllertypes.DefaultParams()) + }, + false, + }, + { + "failed to validate registered account - invalid owner address", + func() { + activeChannels := []types.ActiveChannel{ + { + PortId: TestPortID, + ChannelId: ibctesting.FirstChannelID, + }, + } + + registeredAccounts := []types.RegisteredInterchainAccount{ + { + PortId: TestPortID, + AccountAddress: "", + }, + } + + genesisState = types.NewControllerGenesisState(activeChannels, registeredAccounts, []string{}, controllertypes.DefaultParams()) + }, + false, + }, + { + "failed to validate controller ports - invalid port identifier", + func() { + activeChannels := []types.ActiveChannel{ + { + PortId: TestPortID, + ChannelId: ibctesting.FirstChannelID, + }, + } + + registeredAccounts := []types.RegisteredInterchainAccount{ + { + PortId: TestPortID, + AccountAddress: TestOwnerAddress, + }, + } + + genesisState = types.NewControllerGenesisState(activeChannels, registeredAccounts, []string{"invalid|port"}, controllertypes.DefaultParams()) + }, + false, + }, + } + + for _, tc := range testCases { + suite.Run(tc.name, func() { + genesisState = types.DefaultControllerGenesis() + + tc.malleate() // malleate mutates test data + + err := genesisState.Validate() + + if tc.expPass { + suite.Require().NoError(err, tc.name) + } else { + suite.Require().Error(err, tc.name) + } + }) + } +} + +func (suite *TypesTestSuite) TestValidateHostGenesisState() { + var ( + genesisState types.HostGenesisState + ) + + testCases := []struct { + name string + malleate func() + expPass bool + }{ + { + "success", + func() {}, + true, + }, + { + "failed to validate active channel - invalid port identifier", + func() { + activeChannels := []types.ActiveChannel{ + { + PortId: "invalid|port", + ChannelId: ibctesting.FirstChannelID, + }, + } + + genesisState = types.NewHostGenesisState(activeChannels, []types.RegisteredInterchainAccount{}, types.PortID, hosttypes.DefaultParams()) + }, + false, + }, + { + "failed to validate active channel - invalid channel identifier", + func() { + activeChannels := []types.ActiveChannel{ + { + PortId: TestPortID, + ChannelId: "invalid|channel", + }, + } + + genesisState = types.NewHostGenesisState(activeChannels, []types.RegisteredInterchainAccount{}, types.PortID, hosttypes.DefaultParams()) + }, + false, + }, + { + "failed to validate registered account - invalid port identifier", + func() { + activeChannels := []types.ActiveChannel{ + { + PortId: TestPortID, + ChannelId: ibctesting.FirstChannelID, + }, + } + + registeredAccounts := []types.RegisteredInterchainAccount{ + { + PortId: "invalid|port", + AccountAddress: TestOwnerAddress, + }, + } + + genesisState = types.NewHostGenesisState(activeChannels, registeredAccounts, types.PortID, hosttypes.DefaultParams()) + }, + false, + }, + { + "failed to validate registered account - invalid owner address", + func() { + activeChannels := []types.ActiveChannel{ + { + PortId: TestPortID, + ChannelId: ibctesting.FirstChannelID, + }, + } + + registeredAccounts := []types.RegisteredInterchainAccount{ + { + PortId: TestPortID, + AccountAddress: "", + }, + } + + genesisState = types.NewHostGenesisState(activeChannels, registeredAccounts, types.PortID, hosttypes.DefaultParams()) + }, + false, + }, + { + "failed to validate controller ports - invalid port identifier", + func() { + activeChannels := []types.ActiveChannel{ + { + PortId: TestPortID, + ChannelId: ibctesting.FirstChannelID, + }, + } + + registeredAccounts := []types.RegisteredInterchainAccount{ + { + PortId: TestPortID, + AccountAddress: TestOwnerAddress, + }, + } + + genesisState = types.NewHostGenesisState(activeChannels, registeredAccounts, "invalid|port", hosttypes.DefaultParams()) + }, + false, + }, + } + + for _, tc := range testCases { + suite.Run(tc.name, func() { + genesisState = types.DefaultHostGenesis() + + tc.malleate() // malleate mutates test data + + err := genesisState.Validate() + + if tc.expPass { + suite.Require().NoError(err, tc.name) + } else { + suite.Require().Error(err, tc.name) + } + }) + } +} diff --git a/modules/apps/27-interchain-accounts/types/keys.go b/modules/apps/27-interchain-accounts/types/keys.go new file mode 100644 index 0000000000..3f535a0148 --- /dev/null +++ b/modules/apps/27-interchain-accounts/types/keys.go @@ -0,0 +1,54 @@ +package types + +import ( + "fmt" +) + +const ( + // ModuleName defines the interchain accounts module name + ModuleName = "interchainaccounts" + + // VersionPrefix defines the current version for interchain accounts + VersionPrefix = "ics27-1" + + // PortID is the default port id that the interchain accounts module binds to + PortID = "interchain-account" + + // StoreKey is the store key string for interchain accounts + StoreKey = ModuleName + + // RouterKey is the message route for interchain accounts + RouterKey = ModuleName + + // QuerierRoute is the querier route for interchain accounts + QuerierRoute = ModuleName + + // Delimiter is the delimiter used for the interchain accounts version string + Delimiter = "." +) + +var ( + // ActiveChannelKeyPrefix defines the key prefix used to store active channels + ActiveChannelKeyPrefix = "activeChannel" + + // OwnerKeyPrefix defines the key prefix used to store interchain accounts + OwnerKeyPrefix = "owner" + + // PortKeyPrefix defines the key prefix used to store ports + PortKeyPrefix = "port" +) + +// KeyActiveChannel creates and returns a new key used for active channels store operations +func KeyActiveChannel(portID string) []byte { + return []byte(fmt.Sprintf("%s/%s", ActiveChannelKeyPrefix, portID)) +} + +// KeyOwnerAccount creates and returns a new key used for interchain account store operations +func KeyOwnerAccount(portID string) []byte { + return []byte(fmt.Sprintf("%s/%s", OwnerKeyPrefix, portID)) +} + +// KeyPort creates and returns a new key used for port store operations +func KeyPort(portID string) []byte { + return []byte(fmt.Sprintf("%s/%s", PortKeyPrefix, portID)) +} diff --git a/modules/apps/27-interchain-accounts/types/keys_test.go b/modules/apps/27-interchain-accounts/types/keys_test.go new file mode 100644 index 0000000000..037061a3d3 --- /dev/null +++ b/modules/apps/27-interchain-accounts/types/keys_test.go @@ -0,0 +1,15 @@ +package types_test + +import ( + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" +) + +func (suite *TypesTestSuite) TestKeyActiveChannel() { + key := types.KeyActiveChannel("port-id") + suite.Require().Equal("activeChannel/port-id", string(key)) +} + +func (suite *TypesTestSuite) TestKeyOwnerAccount() { + key := types.KeyOwnerAccount("port-id") + suite.Require().Equal("owner/port-id", string(key)) +} diff --git a/modules/apps/27-interchain-accounts/types/packet.go b/modules/apps/27-interchain-accounts/types/packet.go new file mode 100644 index 0000000000..e7669a77fc --- /dev/null +++ b/modules/apps/27-interchain-accounts/types/packet.go @@ -0,0 +1,50 @@ +package types + +import ( + codectypes "github.com/cosmos/cosmos-sdk/codec/types" + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" +) + +// MaxMemoCharLength defines the maximum length for the InterchainAccountPacketData memo field +const MaxMemoCharLength = 256 + +// ValidateBasic performs basic validation of the interchain account packet data. +// The memo may be empty. +func (iapd InterchainAccountPacketData) ValidateBasic() error { + if iapd.Type == UNSPECIFIED { + return sdkerrors.Wrap(ErrInvalidOutgoingData, "packet data type cannot be unspecified") + } + + if iapd.Data == nil { + return sdkerrors.Wrap(ErrInvalidOutgoingData, "packet data cannot be empty") + } + + if len(iapd.Memo) > MaxMemoCharLength { + return sdkerrors.Wrapf(ErrInvalidOutgoingData, "packet data memo cannot be greater than %d characters", MaxMemoCharLength) + } + + return nil +} + +// GetBytes returns the JSON marshalled interchain account packet data. +func (iapd InterchainAccountPacketData) GetBytes() []byte { + return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&iapd)) +} + +// GetBytes returns the JSON marshalled interchain account CosmosTx. +func (ct CosmosTx) GetBytes() []byte { + return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&ct)) +} + +// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces +func (ct CosmosTx) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error { + for _, any := range ct.Messages { + err := unpacker.UnpackAny(any, new(sdk.Msg)) + if err != nil { + return err + } + } + + return nil +} diff --git a/modules/apps/27-interchain-accounts/types/packet_test.go b/modules/apps/27-interchain-accounts/types/packet_test.go new file mode 100644 index 0000000000..1c8d5c5fc4 --- /dev/null +++ b/modules/apps/27-interchain-accounts/types/packet_test.go @@ -0,0 +1,75 @@ +package types_test + +import ( + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" +) + +var largeMemo = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum" + +func (suite *TypesTestSuite) TestValidateBasic() { + testCases := []struct { + name string + packetData types.InterchainAccountPacketData + expPass bool + }{ + { + "success", + types.InterchainAccountPacketData{ + Type: types.EXECUTE_TX, + Data: []byte("data"), + Memo: "memo", + }, + true, + }, + { + "success, empty memo", + types.InterchainAccountPacketData{ + Type: types.EXECUTE_TX, + Data: []byte("data"), + }, + true, + }, + { + "type unspecified", + types.InterchainAccountPacketData{ + Type: types.UNSPECIFIED, + Data: []byte("data"), + Memo: "memo", + }, + false, + }, + { + "empty data", + types.InterchainAccountPacketData{ + Type: types.EXECUTE_TX, + Data: nil, + Memo: "memo", + }, + false, + }, + { + "memo too large", + types.InterchainAccountPacketData{ + Type: types.EXECUTE_TX, + Data: []byte("data"), + Memo: largeMemo, + }, + false, + }, + } + + for _, tc := range testCases { + tc := tc + suite.Run(tc.name, func() { + suite.SetupTest() // reset + + err := tc.packetData.ValidateBasic() + + if tc.expPass { + suite.Require().NoError(err) + } else { + suite.Require().Error(err) + } + }) + } +} diff --git a/modules/apps/27-interchain-accounts/types/port.go b/modules/apps/27-interchain-accounts/types/port.go new file mode 100644 index 0000000000..c44c5eda9b --- /dev/null +++ b/modules/apps/27-interchain-accounts/types/port.go @@ -0,0 +1,78 @@ +package types + +import ( + "fmt" + "strconv" + "strings" + + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + connectiontypes "github.com/cosmos/ibc-go/v2/modules/core/03-connection/types" + porttypes "github.com/cosmos/ibc-go/v2/modules/core/05-port/types" +) + +const ( + // ControllerPortFormat is the expected port identifier format to which controller chains must conform + // See (TODO: Link to spec when updated) + ControllerPortFormat = "..." +) + +// GeneratePortID generates an interchain accounts controller port identifier for the provided owner +// in the following format: +// +// 'ics-27---' +// https://github.com/seantking/ibc/tree/sean/ics-27-updates/spec/app/ics-027-interchain-accounts#registering--controlling-flows +// TODO: update link to spec +func GeneratePortID(owner, connectionID, counterpartyConnectionID string) (string, error) { + if strings.TrimSpace(owner) == "" { + return "", sdkerrors.Wrap(ErrInvalidAccountAddress, "owner address cannot be empty") + } + + connectionSeq, err := connectiontypes.ParseConnectionSequence(connectionID) + if err != nil { + return "", sdkerrors.Wrap(err, "invalid connection identifier") + } + + counterpartyConnectionSeq, err := connectiontypes.ParseConnectionSequence(counterpartyConnectionID) + if err != nil { + return "", sdkerrors.Wrap(err, "invalid counterparty connection identifier") + } + + return fmt.Sprint( + VersionPrefix, Delimiter, + connectionSeq, Delimiter, + counterpartyConnectionSeq, Delimiter, + owner, + ), nil +} + +// ParseControllerConnSequence attempts to parse the controller connection sequence from the provided port identifier +// The port identifier must match the controller chain format outlined in (TODO: link spec), otherwise an empty string is returned +func ParseControllerConnSequence(portID string) (uint64, error) { + s := strings.Split(portID, Delimiter) + if len(s) != 4 { + return 0, sdkerrors.Wrap(porttypes.ErrInvalidPort, "failed to parse port identifier") + } + + seq, err := strconv.ParseUint(s[1], 10, 64) + if err != nil { + return 0, sdkerrors.Wrapf(err, "failed to parse connection sequence (%s)", s[1]) + } + + return seq, nil +} + +// ParseHostConnSequence attempts to parse the host connection sequence from the provided port identifier +// The port identifier must match the controller chain format outlined in (TODO: link spec), otherwise an empty string is returned +func ParseHostConnSequence(portID string) (uint64, error) { + s := strings.Split(portID, Delimiter) + if len(s) != 4 { + return 0, sdkerrors.Wrap(porttypes.ErrInvalidPort, "failed to parse port identifier") + } + + seq, err := strconv.ParseUint(s[2], 10, 64) + if err != nil { + return 0, sdkerrors.Wrapf(err, "failed to parse connection sequence (%s)", s[2]) + } + + return seq, nil +} diff --git a/modules/apps/27-interchain-accounts/types/port_test.go b/modules/apps/27-interchain-accounts/types/port_test.go new file mode 100644 index 0000000000..cd12548c8d --- /dev/null +++ b/modules/apps/27-interchain-accounts/types/port_test.go @@ -0,0 +1,169 @@ +package types_test + +import ( + "fmt" + + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" + ibctesting "github.com/cosmos/ibc-go/v2/testing" +) + +func (suite *TypesTestSuite) TestGeneratePortID() { + var ( + path *ibctesting.Path + owner = TestOwnerAddress + ) + + testCases := []struct { + name string + malleate func() + expValue string + expPass bool + }{ + { + "success", + func() {}, + fmt.Sprint(types.VersionPrefix, types.Delimiter, "0", types.Delimiter, "0", types.Delimiter, TestOwnerAddress), + true, + }, + { + "success with non matching connection sequences", + func() { + path.EndpointA.ConnectionID = "connection-1" + }, + fmt.Sprint(types.VersionPrefix, types.Delimiter, "1", types.Delimiter, "0", types.Delimiter, TestOwnerAddress), + true, + }, + { + "invalid connectionID", + func() { + path.EndpointA.ConnectionID = "connection" + }, + "", + false, + }, + { + "invalid counterparty connectionID", + func() { + path.EndpointB.ConnectionID = "connection" + }, + "", + false, + }, + { + "invalid owner address", + func() { + owner = " " + }, + "", + false, + }, + } + + for _, tc := range testCases { + tc := tc + suite.Run(tc.name, func() { + suite.SetupTest() // reset + + path = ibctesting.NewPath(suite.chainA, suite.chainB) + suite.coordinator.Setup(path) + + tc.malleate() // malleate mutates test data + + portID, err := types.GeneratePortID(owner, path.EndpointA.ConnectionID, path.EndpointB.ConnectionID) + + if tc.expPass { + suite.Require().NoError(err, tc.name) + suite.Require().Equal(tc.expValue, portID) + } else { + suite.Require().Error(err, tc.name) + suite.Require().Empty(portID) + } + }) + } +} + +func (suite *TypesTestSuite) TestParseControllerConnSequence() { + + testCases := []struct { + name string + portID string + expValue uint64 + expPass bool + }{ + { + "success", + TestPortID, + 0, + true, + }, + { + "failed to parse port identifier", + "invalid-port-id", + 0, + false, + }, + { + "failed to parse connection sequence", + "ics27-1.x.y.cosmos1", + 0, + false, + }, + } + + for _, tc := range testCases { + suite.Run(tc.name, func() { + connSeq, err := types.ParseControllerConnSequence(tc.portID) + + if tc.expPass { + suite.Require().Equal(tc.expValue, connSeq) + suite.Require().NoError(err, tc.name) + } else { + suite.Require().Zero(connSeq) + suite.Require().Error(err, tc.name) + } + }) + } +} + +func (suite *TypesTestSuite) TestParseHostConnSequence() { + + testCases := []struct { + name string + portID string + expValue uint64 + expPass bool + }{ + { + "success", + TestPortID, + 0, + true, + }, + { + "failed to parse port identifier", + "invalid-port-id", + 0, + false, + }, + { + "failed to parse connection sequence", + "ics27-1.x.y.cosmos1", + 0, + false, + }, + } + + for _, tc := range testCases { + suite.Run(tc.name, func() { + connSeq, err := types.ParseHostConnSequence(tc.portID) + + if tc.expPass { + suite.Require().Equal(tc.expValue, connSeq) + suite.Require().NoError(err, tc.name) + } else { + suite.Require().Zero(connSeq) + suite.Require().Error(err, tc.name) + } + }) + } +} diff --git a/modules/apps/27-interchain-accounts/types/types.pb.go b/modules/apps/27-interchain-accounts/types/types.pb.go new file mode 100644 index 0000000000..7ed9c1b9e7 --- /dev/null +++ b/modules/apps/27-interchain-accounts/types/types.pb.go @@ -0,0 +1,634 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: ibc/applications/interchain_accounts/v1/types.proto + +package types + +import ( + fmt "fmt" + types "github.com/cosmos/cosmos-sdk/codec/types" + _ "github.com/gogo/protobuf/gogoproto" + proto "github.com/gogo/protobuf/proto" + io "io" + math "math" + math_bits "math/bits" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package + +// Type defines a classification of message issued from a controller chain to its associated interchain accounts +// host +type Type int32 + +const ( + // Default zero value enumeration + UNSPECIFIED Type = 0 + // Execute a transaction on an interchain accounts host chain + EXECUTE_TX Type = 1 +) + +var Type_name = map[int32]string{ + 0: "TYPE_UNSPECIFIED", + 1: "TYPE_EXECUTE_TX", +} + +var Type_value = map[string]int32{ + "TYPE_UNSPECIFIED": 0, + "TYPE_EXECUTE_TX": 1, +} + +func (x Type) String() string { + return proto.EnumName(Type_name, int32(x)) +} + +func (Type) EnumDescriptor() ([]byte, []int) { + return fileDescriptor_39bab93e18d89799, []int{0} +} + +// InterchainAccountPacketData is comprised of a raw transaction, type of transaction and optional memo field. +type InterchainAccountPacketData struct { + Type Type `protobuf:"varint,1,opt,name=type,proto3,enum=ibc.applications.interchain_accounts.v1.Type" json:"type,omitempty"` + Data []byte `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"` + Memo string `protobuf:"bytes,3,opt,name=memo,proto3" json:"memo,omitempty"` +} + +func (m *InterchainAccountPacketData) Reset() { *m = InterchainAccountPacketData{} } +func (m *InterchainAccountPacketData) String() string { return proto.CompactTextString(m) } +func (*InterchainAccountPacketData) ProtoMessage() {} +func (*InterchainAccountPacketData) Descriptor() ([]byte, []int) { + return fileDescriptor_39bab93e18d89799, []int{0} +} +func (m *InterchainAccountPacketData) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *InterchainAccountPacketData) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_InterchainAccountPacketData.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *InterchainAccountPacketData) XXX_Merge(src proto.Message) { + xxx_messageInfo_InterchainAccountPacketData.Merge(m, src) +} +func (m *InterchainAccountPacketData) XXX_Size() int { + return m.Size() +} +func (m *InterchainAccountPacketData) XXX_DiscardUnknown() { + xxx_messageInfo_InterchainAccountPacketData.DiscardUnknown(m) +} + +var xxx_messageInfo_InterchainAccountPacketData proto.InternalMessageInfo + +func (m *InterchainAccountPacketData) GetType() Type { + if m != nil { + return m.Type + } + return UNSPECIFIED +} + +func (m *InterchainAccountPacketData) GetData() []byte { + if m != nil { + return m.Data + } + return nil +} + +func (m *InterchainAccountPacketData) GetMemo() string { + if m != nil { + return m.Memo + } + return "" +} + +// CosmosTx contains a list of sdk.Msg's. It should be used when sending transactions to an SDK host chain. +type CosmosTx struct { + Messages []*types.Any `protobuf:"bytes,1,rep,name=messages,proto3" json:"messages,omitempty"` +} + +func (m *CosmosTx) Reset() { *m = CosmosTx{} } +func (m *CosmosTx) String() string { return proto.CompactTextString(m) } +func (*CosmosTx) ProtoMessage() {} +func (*CosmosTx) Descriptor() ([]byte, []int) { + return fileDescriptor_39bab93e18d89799, []int{1} +} +func (m *CosmosTx) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *CosmosTx) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_CosmosTx.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *CosmosTx) XXX_Merge(src proto.Message) { + xxx_messageInfo_CosmosTx.Merge(m, src) +} +func (m *CosmosTx) XXX_Size() int { + return m.Size() +} +func (m *CosmosTx) XXX_DiscardUnknown() { + xxx_messageInfo_CosmosTx.DiscardUnknown(m) +} + +var xxx_messageInfo_CosmosTx proto.InternalMessageInfo + +func (m *CosmosTx) GetMessages() []*types.Any { + if m != nil { + return m.Messages + } + return nil +} + +func init() { + proto.RegisterEnum("ibc.applications.interchain_accounts.v1.Type", Type_name, Type_value) + proto.RegisterType((*InterchainAccountPacketData)(nil), "ibc.applications.interchain_accounts.v1.InterchainAccountPacketData") + proto.RegisterType((*CosmosTx)(nil), "ibc.applications.interchain_accounts.v1.CosmosTx") +} + +func init() { + proto.RegisterFile("ibc/applications/interchain_accounts/v1/types.proto", fileDescriptor_39bab93e18d89799) +} + +var fileDescriptor_39bab93e18d89799 = []byte{ + // 391 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x51, 0x41, 0x8b, 0xd3, 0x40, + 0x18, 0xcd, 0xb8, 0x41, 0xd6, 0x59, 0xd9, 0x2d, 0x61, 0x0f, 0x31, 0x42, 0x08, 0x2b, 0x62, 0x10, + 0x32, 0xe3, 0x66, 0x0f, 0x5e, 0xbc, 0xd4, 0x6e, 0x84, 0x5e, 0xa4, 0xc4, 0x14, 0xaa, 0x97, 0x30, + 0x99, 0x8e, 0xe9, 0x60, 0x93, 0x09, 0x9d, 0x49, 0x31, 0xff, 0xa0, 0xf4, 0xe4, 0x1f, 0xe8, 0xc9, + 0x3f, 0xe3, 0xb1, 0x47, 0x8f, 0xd2, 0xfe, 0x11, 0xc9, 0x04, 0xdb, 0x1e, 0x3c, 0xec, 0xed, 0xf1, + 0xf8, 0xde, 0x9b, 0xf7, 0xe6, 0xc1, 0x3b, 0x9e, 0x51, 0x4c, 0xaa, 0x6a, 0xce, 0x29, 0x51, 0x5c, + 0x94, 0x12, 0xf3, 0x52, 0xb1, 0x05, 0x9d, 0x11, 0x5e, 0xa6, 0x84, 0x52, 0x51, 0x97, 0x4a, 0xe2, + 0xe5, 0x2d, 0x56, 0x4d, 0xc5, 0x24, 0xaa, 0x16, 0x42, 0x09, 0xeb, 0x15, 0xcf, 0x28, 0x3a, 0x15, + 0xa1, 0xff, 0x88, 0xd0, 0xf2, 0xd6, 0x79, 0x96, 0x0b, 0x91, 0xcf, 0x19, 0xd6, 0xb2, 0xac, 0xfe, + 0x8a, 0x49, 0xd9, 0x74, 0x1e, 0xce, 0x75, 0x2e, 0x72, 0xa1, 0x21, 0x6e, 0x51, 0xc7, 0xde, 0xac, + 0x00, 0x7c, 0x3e, 0x3c, 0x78, 0xf5, 0x3b, 0xab, 0x11, 0xa1, 0xdf, 0x98, 0xba, 0x27, 0x8a, 0x58, + 0x7d, 0x68, 0xb6, 0x41, 0x6c, 0xe0, 0x01, 0xff, 0x32, 0x0c, 0xd0, 0x03, 0x83, 0xa0, 0xa4, 0xa9, + 0x58, 0xac, 0xa5, 0x96, 0x05, 0xcd, 0x29, 0x51, 0xc4, 0x7e, 0xe4, 0x01, 0xff, 0x69, 0xac, 0x71, + 0xcb, 0x15, 0xac, 0x10, 0xf6, 0x99, 0x07, 0xfc, 0x27, 0xb1, 0xc6, 0x37, 0xef, 0xe0, 0xf9, 0x40, + 0xc8, 0x42, 0xc8, 0xe4, 0xbb, 0xf5, 0x06, 0x9e, 0x17, 0x4c, 0x4a, 0x92, 0x33, 0x69, 0x03, 0xef, + 0xcc, 0xbf, 0x08, 0xaf, 0x51, 0x57, 0x0d, 0xfd, 0xab, 0x86, 0xfa, 0x65, 0x13, 0x1f, 0xae, 0x5e, + 0x4f, 0xa0, 0xd9, 0xbe, 0x69, 0xbd, 0x84, 0xbd, 0xe4, 0xf3, 0x28, 0x4a, 0xc7, 0x1f, 0x3f, 0x8d, + 0xa2, 0xc1, 0xf0, 0xc3, 0x30, 0xba, 0xef, 0x19, 0xce, 0xd5, 0x7a, 0xe3, 0x5d, 0x9c, 0x50, 0xd6, + 0x0b, 0x78, 0xa5, 0xcf, 0xa2, 0x49, 0x34, 0x18, 0x27, 0x51, 0x9a, 0x4c, 0x7a, 0xc0, 0xb9, 0x5c, + 0x6f, 0x3c, 0x78, 0x64, 0x1c, 0x73, 0xf5, 0xd3, 0x35, 0xde, 0xa7, 0xbf, 0x76, 0x2e, 0xd8, 0xee, + 0x5c, 0xf0, 0x67, 0xe7, 0x82, 0x1f, 0x7b, 0xd7, 0xd8, 0xee, 0x5d, 0xe3, 0xf7, 0xde, 0x35, 0xbe, + 0x44, 0x39, 0x57, 0xb3, 0x3a, 0x43, 0x54, 0x14, 0x98, 0xea, 0xe8, 0x98, 0x67, 0x34, 0xc8, 0x05, + 0x5e, 0x86, 0xb8, 0x10, 0xd3, 0x7a, 0xce, 0x64, 0xbb, 0xb5, 0xc4, 0xe1, 0xdb, 0xe0, 0xf8, 0x51, + 0xc1, 0x61, 0x66, 0xbd, 0x71, 0xf6, 0x58, 0x57, 0xba, 0xfb, 0x1b, 0x00, 0x00, 0xff, 0xff, 0xfa, + 0xbe, 0xe0, 0xb6, 0x1b, 0x02, 0x00, 0x00, +} + +func (m *InterchainAccountPacketData) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *InterchainAccountPacketData) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *InterchainAccountPacketData) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if len(m.Memo) > 0 { + i -= len(m.Memo) + copy(dAtA[i:], m.Memo) + i = encodeVarintTypes(dAtA, i, uint64(len(m.Memo))) + i-- + dAtA[i] = 0x1a + } + if len(m.Data) > 0 { + i -= len(m.Data) + copy(dAtA[i:], m.Data) + i = encodeVarintTypes(dAtA, i, uint64(len(m.Data))) + i-- + dAtA[i] = 0x12 + } + if m.Type != 0 { + i = encodeVarintTypes(dAtA, i, uint64(m.Type)) + i-- + dAtA[i] = 0x8 + } + return len(dAtA) - i, nil +} + +func (m *CosmosTx) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *CosmosTx) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *CosmosTx) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if len(m.Messages) > 0 { + for iNdEx := len(m.Messages) - 1; iNdEx >= 0; iNdEx-- { + { + size, err := m.Messages[iNdEx].MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintTypes(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0xa + } + } + return len(dAtA) - i, nil +} + +func encodeVarintTypes(dAtA []byte, offset int, v uint64) int { + offset -= sovTypes(v) + base := offset + for v >= 1<<7 { + dAtA[offset] = uint8(v&0x7f | 0x80) + v >>= 7 + offset++ + } + dAtA[offset] = uint8(v) + return base +} +func (m *InterchainAccountPacketData) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if m.Type != 0 { + n += 1 + sovTypes(uint64(m.Type)) + } + l = len(m.Data) + if l > 0 { + n += 1 + l + sovTypes(uint64(l)) + } + l = len(m.Memo) + if l > 0 { + n += 1 + l + sovTypes(uint64(l)) + } + return n +} + +func (m *CosmosTx) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if len(m.Messages) > 0 { + for _, e := range m.Messages { + l = e.Size() + n += 1 + l + sovTypes(uint64(l)) + } + } + return n +} + +func sovTypes(x uint64) (n int) { + return (math_bits.Len64(x|1) + 6) / 7 +} +func sozTypes(x uint64) (n int) { + return sovTypes(uint64((x << 1) ^ uint64((int64(x) >> 63)))) +} +func (m *InterchainAccountPacketData) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: InterchainAccountPacketData: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: InterchainAccountPacketData: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Type", wireType) + } + m.Type = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.Type |= Type(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Data", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthTypes + } + postIndex := iNdEx + byteLen + if postIndex < 0 { + return ErrInvalidLengthTypes + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Data = append(m.Data[:0], dAtA[iNdEx:postIndex]...) + if m.Data == nil { + m.Data = []byte{} + } + iNdEx = postIndex + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Memo", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthTypes + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthTypes + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Memo = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipTypes(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthTypes + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *CosmosTx) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: CosmosTx: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: CosmosTx: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Messages", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthTypes + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthTypes + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Messages = append(m.Messages, &types.Any{}) + if err := m.Messages[len(m.Messages)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipTypes(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthTypes + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func skipTypes(dAtA []byte) (n int, err error) { + l := len(dAtA) + iNdEx := 0 + depth := 0 + for iNdEx < l { + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowTypes + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + wireType := int(wire & 0x7) + switch wireType { + case 0: + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowTypes + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + iNdEx++ + if dAtA[iNdEx-1] < 0x80 { + break + } + } + case 1: + iNdEx += 8 + case 2: + var length int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowTypes + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + length |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if length < 0 { + return 0, ErrInvalidLengthTypes + } + iNdEx += length + case 3: + depth++ + case 4: + if depth == 0 { + return 0, ErrUnexpectedEndOfGroupTypes + } + depth-- + case 5: + iNdEx += 4 + default: + return 0, fmt.Errorf("proto: illegal wireType %d", wireType) + } + if iNdEx < 0 { + return 0, ErrInvalidLengthTypes + } + if depth == 0 { + return iNdEx, nil + } + } + return 0, io.ErrUnexpectedEOF +} + +var ( + ErrInvalidLengthTypes = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowTypes = fmt.Errorf("proto: integer overflow") + ErrUnexpectedEndOfGroupTypes = fmt.Errorf("proto: unexpected end of group") +) diff --git a/modules/apps/27-interchain-accounts/types/version.go b/modules/apps/27-interchain-accounts/types/version.go new file mode 100644 index 0000000000..ffa33aa001 --- /dev/null +++ b/modules/apps/27-interchain-accounts/types/version.go @@ -0,0 +1,67 @@ +package types + +import ( + "fmt" + "regexp" + "strings" + + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" +) + +// DefaultMaxAddrLength defines the default maximum character length used in validation of addresses +var DefaultMaxAddrLength = 128 + +// IsValidAddr defines a regular expression to check if the provided string consists of +// strictly alphanumeric characters +var IsValidAddr = regexp.MustCompile("^[a-zA-Z0-9]*$").MatchString + +// NewVersion returns a complete version string in the format: VersionPrefix + Delimter + AccAddress +func NewAppVersion(versionPrefix, accAddr string) string { + return fmt.Sprint(versionPrefix, Delimiter, accAddr) +} + +// ParseAddressFromVersion attempts to extract the associated account address from the provided version string +func ParseAddressFromVersion(version string) (string, error) { + s := strings.Split(version, Delimiter) + if len(s) != 2 { + return "", sdkerrors.Wrap(ErrInvalidVersion, "failed to parse version") + } + + return s[1], nil +} + +// ValidateVersion performs basic validation of the provided ics27 version string. +// An ics27 version string may include an optional account address as per [TODO: Add spec when available] +// ValidateVersion first attempts to split the version string using the standard delimiter, then asserts a supported +// version prefix is included, followed by additional checks which enforce constraints on the account address. +func ValidateVersion(version string) error { + s := strings.Split(version, Delimiter) + + if len(s) != 2 { + return sdkerrors.Wrapf(ErrInvalidVersion, "expected format , got %s", Delimiter, version) + } + + if s[0] != VersionPrefix { + return sdkerrors.Wrapf(ErrInvalidVersion, "expected %s, got %s", VersionPrefix, s[0]) + } + + if err := ValidateAccountAddress(s[1]); err != nil { + return err + } + + return nil +} + +// ValidateAccountAddress performs basic validation of interchain account addresses, enforcing constraints +// on address length and character set +func ValidateAccountAddress(addr string) error { + if !IsValidAddr(addr) || len(addr) == 0 || len(addr) > DefaultMaxAddrLength { + return sdkerrors.Wrapf( + ErrInvalidAccountAddress, + "address must contain strictly alphanumeric characters, not exceeding %d characters in length", + DefaultMaxAddrLength, + ) + } + + return nil +} diff --git a/modules/apps/27-interchain-accounts/types/version_test.go b/modules/apps/27-interchain-accounts/types/version_test.go new file mode 100644 index 0000000000..0ad4a235eb --- /dev/null +++ b/modules/apps/27-interchain-accounts/types/version_test.go @@ -0,0 +1,106 @@ +package types_test + +import ( + "fmt" + + "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" +) + +func (suite *TypesTestSuite) TestParseAddressFromVersion() { + + testCases := []struct { + name string + version string + expValue string + expPass bool + }{ + { + "success", + types.NewAppVersion(types.VersionPrefix, TestOwnerAddress), + TestOwnerAddress, + true, + }, + { + "failed to parse address from version", + "invalid-version-string", + "", + false, + }, + { + "failure with multiple delimiters", + fmt.Sprint(types.NewAppVersion(types.VersionPrefix, TestOwnerAddress), types.Delimiter, types.NewAppVersion(types.VersionPrefix, TestOwnerAddress)), + "", + false, + }, + } + + for _, tc := range testCases { + suite.Run(tc.name, func() { + addr, err := types.ParseAddressFromVersion(tc.version) + + if tc.expPass { + suite.Require().Equal(tc.expValue, addr) + suite.Require().NoError(err, tc.name) + } else { + suite.Require().Empty(addr) + suite.Require().Error(err, tc.name) + } + }) + } +} + +func (suite *TypesTestSuite) TestValidateVersion() { + testCases := []struct { + name string + version string + expPass bool + }{ + { + "success", + types.NewAppVersion(types.VersionPrefix, TestOwnerAddress), + true, + }, + { + "unexpected version string format", + "invalid-version-string-format", + false, + }, + { + "unexpected version string format, additional delimiter", + types.NewAppVersion(types.VersionPrefix, "cosmos17dtl0mjt3t77kpu.hg2edqzjpszulwhgzuj9ljs"), + false, + }, + { + "invalid version", + types.NewAppVersion("ics27-5", TestOwnerAddress), + false, + }, + { + "invalid account address - empty", + types.NewAppVersion(types.VersionPrefix, ""), + false, + }, + { + "invalid account address - exceeded character length", + types.NewAppVersion(types.VersionPrefix, "ofwafxhdmqcdbpzvrccxkidbunrwyyoboyctignpvthxbwxtmnzyfwhhywobaatltfwafxhdmqcdbpzvrccxkidbunrwyyoboyctignpvthxbwxtmnzyfwhhywobaatlt"), + false, + }, + { + "invalid account address - non alphanumeric characters", + types.NewAppVersion(types.VersionPrefix, "-_-"), + false, + }, + } + + for _, tc := range testCases { + suite.Run(tc.name, func() { + err := types.ValidateVersion(tc.version) + + if tc.expPass { + suite.Require().NoError(err, tc.name) + } else { + suite.Require().Error(err, tc.name) + } + }) + } +} diff --git a/modules/core/04-channel/types/errors.go b/modules/core/04-channel/types/errors.go index 8149136ed5..8c31cfbae1 100644 --- a/modules/core/04-channel/types/errors.go +++ b/modules/core/04-channel/types/errors.go @@ -36,4 +36,6 @@ var ( // Perform a no-op on the current Msg ErrNoOpMsg = sdkerrors.Register(SubModuleName, 23, "message is redundant, no-op will be performed") + + ErrInvalidChannelVersion = sdkerrors.Register(SubModuleName, 24, "invalid channel version") ) diff --git a/proto/ibc/applications/interchain_accounts/controller/v1/controller.proto b/proto/ibc/applications/interchain_accounts/controller/v1/controller.proto new file mode 100644 index 0000000000..4689b72124 --- /dev/null +++ b/proto/ibc/applications/interchain_accounts/controller/v1/controller.proto @@ -0,0 +1,15 @@ +syntax = "proto3"; + +package ibc.applications.interchain_accounts.controller.v1; + +option go_package = "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/controller/types"; + +import "google/protobuf/any.proto"; +import "gogoproto/gogo.proto"; + +// Params defines the set of on-chain interchain accounts parameters. +// The following parameters may be used to disable the controller submodule. +message Params { + // controller_enabled enables or disables the controller submodule. + bool controller_enabled = 1 [(gogoproto.moretags) = "yaml:\"controller_enabled\""]; +} diff --git a/proto/ibc/applications/interchain_accounts/controller/v1/query.proto b/proto/ibc/applications/interchain_accounts/controller/v1/query.proto new file mode 100644 index 0000000000..d6e25f8a25 --- /dev/null +++ b/proto/ibc/applications/interchain_accounts/controller/v1/query.proto @@ -0,0 +1,27 @@ +syntax = "proto3"; + +package ibc.applications.interchain_accounts.controller.v1; + +option go_package = "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/controller/types"; + +import "gogoproto/gogo.proto"; +import "ibc/applications/interchain_accounts/controller/v1/controller.proto"; +import "google/api/annotations.proto"; + +// Query provides defines the gRPC querier service. +service Query { + // Params queries all parameters of the ICA controller submodule. + rpc Params(QueryParamsRequest) returns (QueryParamsResponse) { + option (google.api.http).get = "/ibc/apps/interchain_accounts/controller/v1/params"; + } +} + +// QueryParamsRequest is the request type for the Query/Params RPC method. +message QueryParamsRequest {} + +// QueryParamsResponse is the response type for the Query/Params RPC method. +message QueryParamsResponse { + // params defines the parameters of the module. + Params params = 1; +} + diff --git a/proto/ibc/applications/interchain_accounts/host/v1/host.proto b/proto/ibc/applications/interchain_accounts/host/v1/host.proto new file mode 100644 index 0000000000..5798c2f95c --- /dev/null +++ b/proto/ibc/applications/interchain_accounts/host/v1/host.proto @@ -0,0 +1,17 @@ +syntax = "proto3"; + +package ibc.applications.interchain_accounts.host.v1; + +option go_package = "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/host/types"; + +import "google/protobuf/any.proto"; +import "gogoproto/gogo.proto"; + +// Params defines the set of on-chain interchain accounts parameters. +// The following parameters may be used to disable the host submodule. +message Params { + // host_enabled enables or disables the host submodule. + bool host_enabled = 1 [(gogoproto.moretags) = "yaml:\"host_enabled\""]; + // allow_messages defines a list of sdk message typeURLs allowed to be executed on a host chain. + repeated string allow_messages = 2 [(gogoproto.moretags) = "yaml:\"allow_messages\""]; +} diff --git a/proto/ibc/applications/interchain_accounts/host/v1/query.proto b/proto/ibc/applications/interchain_accounts/host/v1/query.proto new file mode 100644 index 0000000000..4802b724af --- /dev/null +++ b/proto/ibc/applications/interchain_accounts/host/v1/query.proto @@ -0,0 +1,27 @@ +syntax = "proto3"; + +package ibc.applications.interchain_accounts.host.v1; + +option go_package = "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/host/types"; + +import "google/api/annotations.proto"; +import "gogoproto/gogo.proto"; +import "ibc/applications/interchain_accounts/host/v1/host.proto"; + +// Query provides defines the gRPC querier service. +service Query { + // Params queries all parameters of the ICA host submodule. + rpc Params(QueryParamsRequest) returns (QueryParamsResponse) { + option (google.api.http).get = "/ibc/apps/interchain_accounts/host/v1/params"; + } +} + +// QueryParamsRequest is the request type for the Query/Params RPC method. +message QueryParamsRequest {} + +// QueryParamsResponse is the response type for the Query/Params RPC method. +message QueryParamsResponse { + // params defines the parameters of the module. + Params params = 1; +} + diff --git a/proto/ibc/applications/interchain_accounts/v1/account.proto b/proto/ibc/applications/interchain_accounts/v1/account.proto new file mode 100644 index 0000000000..b7af89b502 --- /dev/null +++ b/proto/ibc/applications/interchain_accounts/v1/account.proto @@ -0,0 +1,20 @@ +syntax = "proto3"; + +package ibc.applications.interchain_accounts.v1; + +option go_package = "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types"; + +import "cosmos_proto/cosmos.proto"; +import "gogoproto/gogo.proto"; +import "cosmos/auth/v1beta1/auth.proto"; + +// An InterchainAccount is defined as a BaseAccount & the address of the account owner on the controller chain +message InterchainAccount { + option (gogoproto.goproto_getters) = false; + option (gogoproto.goproto_stringer) = false; + option (cosmos_proto.implements_interface) = "InterchainAccountI"; + + cosmos.auth.v1beta1.BaseAccount base_account = 1 + [(gogoproto.embed) = true, (gogoproto.moretags) = "yaml:\"base_account\""]; + string account_owner = 2 [(gogoproto.moretags) = "yaml:\"account_owner\""]; +} diff --git a/proto/ibc/applications/interchain_accounts/v1/genesis.proto b/proto/ibc/applications/interchain_accounts/v1/genesis.proto new file mode 100644 index 0000000000..670d05de53 --- /dev/null +++ b/proto/ibc/applications/interchain_accounts/v1/genesis.proto @@ -0,0 +1,49 @@ +syntax = "proto3"; + +package ibc.applications.interchain_accounts.v1; + +option go_package = "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types"; + +import "gogoproto/gogo.proto"; +import "ibc/applications/interchain_accounts/controller/v1/controller.proto"; +import "ibc/applications/interchain_accounts/host/v1/host.proto"; + +// GenesisState defines the interchain accounts genesis state +message GenesisState { + ControllerGenesisState controller_genesis_state = 1 + [(gogoproto.nullable) = false, (gogoproto.moretags) = "yaml:\"controller_genesis_state\""]; + HostGenesisState host_genesis_state = 2 + [(gogoproto.nullable) = false, (gogoproto.moretags) = "yaml:\"host_genesis_state\""]; +} + +// ControllerGenesisState defines the interchain accounts controller genesis state +message ControllerGenesisState { + repeated ActiveChannel active_channels = 1 + [(gogoproto.nullable) = false, (gogoproto.moretags) = "yaml:\"active_channels\""]; + repeated RegisteredInterchainAccount interchain_accounts = 2 + [(gogoproto.nullable) = false, (gogoproto.moretags) = "yaml:\"interchain_accounts\""]; + repeated string ports = 3; + ibc.applications.interchain_accounts.controller.v1.Params params = 4 [(gogoproto.nullable) = false]; +} + +// HostGenesisState defines the interchain accounts host genesis state +message HostGenesisState { + repeated ActiveChannel active_channels = 1 + [(gogoproto.nullable) = false, (gogoproto.moretags) = "yaml:\"active_channels\""]; + repeated RegisteredInterchainAccount interchain_accounts = 2 + [(gogoproto.nullable) = false, (gogoproto.moretags) = "yaml:\"interchain_accounts\""]; + string port = 3; + ibc.applications.interchain_accounts.host.v1.Params params = 4 [(gogoproto.nullable) = false]; +} + +// ActiveChannel contains a pairing of port ID and channel ID for an active interchain accounts channel +message ActiveChannel { + string port_id = 1 [(gogoproto.moretags) = "yaml:\"port_id\""]; + string channel_id = 2 [(gogoproto.moretags) = "yaml:\"channel_id\""]; +} + +// RegisteredInterchainAccount contains a pairing of controller port ID and associated interchain account address +message RegisteredInterchainAccount { + string port_id = 1 [(gogoproto.moretags) = "yaml:\"port_id\""]; + string account_address = 2 [(gogoproto.moretags) = "yaml:\"account_address\""]; +} diff --git a/proto/ibc/applications/interchain_accounts/v1/types.proto b/proto/ibc/applications/interchain_accounts/v1/types.proto new file mode 100644 index 0000000000..c6d9d0a3e9 --- /dev/null +++ b/proto/ibc/applications/interchain_accounts/v1/types.proto @@ -0,0 +1,31 @@ +syntax = "proto3"; + +package ibc.applications.interchain_accounts.v1; + +option go_package = "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types"; + +import "google/protobuf/any.proto"; +import "gogoproto/gogo.proto"; + +// Type defines a classification of message issued from a controller chain to its associated interchain accounts +// host +enum Type { + option (gogoproto.goproto_enum_prefix) = false; + + // Default zero value enumeration + TYPE_UNSPECIFIED = 0 [(gogoproto.enumvalue_customname) = "UNSPECIFIED"]; + // Execute a transaction on an interchain accounts host chain + TYPE_EXECUTE_TX = 1 [(gogoproto.enumvalue_customname) = "EXECUTE_TX"]; +} + +// InterchainAccountPacketData is comprised of a raw transaction, type of transaction and optional memo field. +message InterchainAccountPacketData { + Type type = 1; + bytes data = 2; + string memo = 3; +} + +// CosmosTx contains a list of sdk.Msg's. It should be used when sending transactions to an SDK host chain. +message CosmosTx { + repeated google.protobuf.Any messages = 1; +} diff --git a/testing/README.md b/testing/README.md index f7a71bb134..189f8e8c6c 100644 --- a/testing/README.md +++ b/testing/README.md @@ -294,7 +294,7 @@ When writing IBC applications acting as middleware, it might be desirable to tes This can be done by wiring a middleware stack in the app.go file using existing applications as middleware and IBC base applications. The mock module may also be leveraged to act as a base application in the instance that such an application is not available for testing or causes dependency concerns. -The mock module contains a `MockIBCApp`. This struct contains a function field for every IBC App Module callback. +The mock IBC module contains a `MockIBCApp`. This struct contains a function field for every IBC App Module callback. Each of these functions can be individually set to mock expected behaviour of a base application. For example, if one wanted to test that the base application cannot affect the outcome of the `OnChanOpenTry` callback, the mock module base application callback could be updated as such: @@ -303,3 +303,17 @@ For example, if one wanted to test that the base application cannot affect the o return fmt.Errorf("mock base app must not be called for OnChanOpenTry") } ``` + +Using a mock module as a base application in a middleware stack may require adding the module to your `SimApp`. +This is because IBC will route to the top level IBC module of a middleware stack, so a module which never +sits at the top of middleware stack will need to be accessed via a public field in `SimApp` + +This might look like: +```go + suite.chainA.GetSimApp().ICAAuthModule.IBCApp.OnChanOpenInit = func(ctx sdk.Context, order channeltypes.Order, connectionHops []string, + portID, channelID string, chanCap *capabilitytypes.Capability, + counterparty channeltypes.Counterparty, version string, + ) error { + return fmt.Errorf("mock ica auth fails") + } +``` diff --git a/testing/app.go b/testing/app.go index 6a2bb551e2..5fd5904043 100644 --- a/testing/app.go +++ b/testing/app.go @@ -89,8 +89,8 @@ func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs } validators = append(validators, validator) delegations = append(delegations, stakingtypes.NewDelegation(genAccs[0].GetAddress(), val.Address.Bytes(), sdk.OneDec())) - } + // set validators and delegations stakingGenesis := stakingtypes.NewGenesisState(stakingtypes.DefaultParams(), validators, delegations) genesisState[stakingtypes.ModuleName] = app.AppCodec().MustMarshalJSON(stakingGenesis) diff --git a/testing/simapp/app.go b/testing/simapp/app.go index 5c9eedf676..d146388977 100644 --- a/testing/simapp/app.go +++ b/testing/simapp/app.go @@ -80,6 +80,14 @@ import ( upgradeclient "github.com/cosmos/cosmos-sdk/x/upgrade/client" upgradekeeper "github.com/cosmos/cosmos-sdk/x/upgrade/keeper" upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" + ica "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts" + icacontroller "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/controller" + icacontrollerkeeper "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/controller/keeper" + icacontrollertypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/controller/types" + icahost "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/host" + icahostkeeper "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/host/keeper" + icahosttypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/host/types" + icatypes "github.com/cosmos/ibc-go/v2/modules/apps/27-interchain-accounts/types" transfer "github.com/cosmos/ibc-go/v2/modules/apps/transfer" ibctransferkeeper "github.com/cosmos/ibc-go/v2/modules/apps/transfer/keeper" ibctransfertypes "github.com/cosmos/ibc-go/v2/modules/apps/transfer/types" @@ -130,6 +138,7 @@ var ( evidence.AppModuleBasic{}, transfer.AppModuleBasic{}, ibcmock.AppModuleBasic{}, + ica.AppModuleBasic{}, authzmodule.AppModuleBasic{}, vesting.AppModuleBasic{}, ) @@ -143,6 +152,7 @@ var ( stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking}, govtypes.ModuleName: {authtypes.Burner}, ibctransfertypes.ModuleName: {authtypes.Minter, authtypes.Burner}, + icatypes.ModuleName: nil, } ) @@ -168,27 +178,36 @@ type SimApp struct { memKeys map[string]*sdk.MemoryStoreKey // keepers - AccountKeeper authkeeper.AccountKeeper - BankKeeper bankkeeper.Keeper - CapabilityKeeper *capabilitykeeper.Keeper - StakingKeeper stakingkeeper.Keeper - SlashingKeeper slashingkeeper.Keeper - MintKeeper mintkeeper.Keeper - DistrKeeper distrkeeper.Keeper - GovKeeper govkeeper.Keeper - CrisisKeeper crisiskeeper.Keeper - UpgradeKeeper upgradekeeper.Keeper - ParamsKeeper paramskeeper.Keeper - AuthzKeeper authzkeeper.Keeper - IBCKeeper *ibckeeper.Keeper // IBC Keeper must be a pointer in the app, so we can SetRouter on it correctly - EvidenceKeeper evidencekeeper.Keeper - TransferKeeper ibctransferkeeper.Keeper - FeeGrantKeeper feegrantkeeper.Keeper + AccountKeeper authkeeper.AccountKeeper + BankKeeper bankkeeper.Keeper + CapabilityKeeper *capabilitykeeper.Keeper + StakingKeeper stakingkeeper.Keeper + SlashingKeeper slashingkeeper.Keeper + MintKeeper mintkeeper.Keeper + DistrKeeper distrkeeper.Keeper + GovKeeper govkeeper.Keeper + CrisisKeeper crisiskeeper.Keeper + UpgradeKeeper upgradekeeper.Keeper + ParamsKeeper paramskeeper.Keeper + AuthzKeeper authzkeeper.Keeper + IBCKeeper *ibckeeper.Keeper // IBC Keeper must be a pointer in the app, so we can SetRouter on it correctly + ICAControllerKeeper icacontrollerkeeper.Keeper + ICAHostKeeper icahostkeeper.Keeper + EvidenceKeeper evidencekeeper.Keeper + TransferKeeper ibctransferkeeper.Keeper + FeeGrantKeeper feegrantkeeper.Keeper // make scoped keepers public for test purposes - ScopedIBCKeeper capabilitykeeper.ScopedKeeper - ScopedTransferKeeper capabilitykeeper.ScopedKeeper - ScopedIBCMockKeeper capabilitykeeper.ScopedKeeper + ScopedIBCKeeper capabilitykeeper.ScopedKeeper + ScopedTransferKeeper capabilitykeeper.ScopedKeeper + ScopedICAControllerKeeper capabilitykeeper.ScopedKeeper + ScopedICAHostKeeper capabilitykeeper.ScopedKeeper + ScopedIBCMockKeeper capabilitykeeper.ScopedKeeper + ScopedICAMockKeeper capabilitykeeper.ScopedKeeper + + // make IBC modules public for test purposes + // these modules are never directly routed to by the IBC Router + ICAAuthModule ibcmock.IBCModule // the module manager mm *module.Manager @@ -229,7 +248,7 @@ func NewSimApp( authtypes.StoreKey, banktypes.StoreKey, stakingtypes.StoreKey, minttypes.StoreKey, distrtypes.StoreKey, slashingtypes.StoreKey, govtypes.StoreKey, paramstypes.StoreKey, ibchost.StoreKey, upgradetypes.StoreKey, feegrant.StoreKey, - evidencetypes.StoreKey, ibctransfertypes.StoreKey, capabilitytypes.StoreKey, + evidencetypes.StoreKey, ibctransfertypes.StoreKey, icacontrollertypes.StoreKey, icahosttypes.StoreKey, capabilitytypes.StoreKey, authzkeeper.StoreKey, ) tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey) @@ -255,9 +274,13 @@ func NewSimApp( app.CapabilityKeeper = capabilitykeeper.NewKeeper(appCodec, keys[capabilitytypes.StoreKey], memKeys[capabilitytypes.MemStoreKey]) scopedIBCKeeper := app.CapabilityKeeper.ScopeToModule(ibchost.ModuleName) scopedTransferKeeper := app.CapabilityKeeper.ScopeToModule(ibctransfertypes.ModuleName) + scopedICAControllerKeeper := app.CapabilityKeeper.ScopeToModule(icacontrollertypes.SubModuleName) + scopedICAHostKeeper := app.CapabilityKeeper.ScopeToModule(icahosttypes.SubModuleName) + // NOTE: the IBC mock keeper and application module is used only for testing core IBC. Do - // note replicate if you do not need to test core IBC or light clients. + // not replicate if you do not need to test core IBC or light clients. scopedIBCMockKeeper := app.CapabilityKeeper.ScopeToModule(ibcmock.ModuleName) + scopedICAMockKeeper := app.CapabilityKeeper.ScopeToModule(ibcmock.ModuleName + icacontrollertypes.SubModuleName) // seal capability keeper after scoping modules app.CapabilityKeeper.Seal() @@ -323,15 +346,41 @@ func NewSimApp( ) transferModule := transfer.NewAppModule(app.TransferKeeper) transferIBCModule := transfer.NewIBCModule(app.TransferKeeper) + // NOTE: the IBC mock keeper and application module is used only for testing core IBC. Do - // note replicate if you do not need to test core IBC or light clients. + // not replicate if you do not need to test core IBC or light clients. mockModule := ibcmock.NewAppModule(scopedIBCMockKeeper, &app.IBCKeeper.PortKeeper) mockIBCModule := ibcmock.NewIBCModule(&ibcmock.MockIBCApp{}, scopedIBCMockKeeper) - // Create static IBC router, add transfer route, then set and seal it + app.ICAControllerKeeper = icacontrollerkeeper.NewKeeper( + appCodec, keys[icacontrollertypes.StoreKey], app.GetSubspace(icacontrollertypes.SubModuleName), + app.IBCKeeper.ChannelKeeper, // may be replaced with middleware such as ics29 fee + app.IBCKeeper.ChannelKeeper, &app.IBCKeeper.PortKeeper, + app.AccountKeeper, scopedICAControllerKeeper, app.MsgServiceRouter(), + ) + + app.ICAHostKeeper = icahostkeeper.NewKeeper( + appCodec, keys[icahosttypes.StoreKey], app.GetSubspace(icahosttypes.SubModuleName), + app.IBCKeeper.ChannelKeeper, &app.IBCKeeper.PortKeeper, + app.AccountKeeper, scopedICAHostKeeper, app.MsgServiceRouter(), + ) + + icaModule := ica.NewAppModule(&app.ICAControllerKeeper, &app.ICAHostKeeper) + + // initialize ICA module with mock module as the authentication module on the controller side + icaAuthModule := ibcmock.NewIBCModule(&ibcmock.MockIBCApp{}, scopedICAMockKeeper) + app.ICAAuthModule = icaAuthModule + + icaControllerIBCModule := icacontroller.NewIBCModule(app.ICAControllerKeeper, icaAuthModule) + icaHostIBCModule := icahost.NewIBCModule(app.ICAHostKeeper) + + // Create static IBC router, add app routes, then set and seal it ibcRouter := porttypes.NewRouter() - ibcRouter.AddRoute(ibctransfertypes.ModuleName, transferIBCModule) - ibcRouter.AddRoute(ibcmock.ModuleName, mockIBCModule) + ibcRouter.AddRoute(icacontrollertypes.SubModuleName, icaControllerIBCModule). + AddRoute(icahosttypes.SubModuleName, icaHostIBCModule). + AddRoute(ibcmock.ModuleName+icacontrollertypes.SubModuleName, icaControllerIBCModule). // ica with mock auth module stack route to ica (top level of middleware stack) + AddRoute(ibctransfertypes.ModuleName, transferIBCModule). + AddRoute(ibcmock.ModuleName, mockIBCModule) app.IBCKeeper.SetRouter(ibcRouter) // create evidence keeper with router @@ -371,6 +420,7 @@ func NewSimApp( params.NewAppModule(app.ParamsKeeper), authzmodule.NewAppModule(appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry), transferModule, + icaModule, mockModule, ) @@ -394,7 +444,7 @@ func NewSimApp( capabilitytypes.ModuleName, authtypes.ModuleName, banktypes.ModuleName, distrtypes.ModuleName, stakingtypes.ModuleName, slashingtypes.ModuleName, govtypes.ModuleName, minttypes.ModuleName, crisistypes.ModuleName, ibchost.ModuleName, genutiltypes.ModuleName, evidencetypes.ModuleName, authz.ModuleName, ibctransfertypes.ModuleName, - ibcmock.ModuleName, feegrant.ModuleName, + icatypes.ModuleName, ibcmock.ModuleName, feegrant.ModuleName, ) app.mm.RegisterInvariants(&app.CrisisKeeper) @@ -461,10 +511,13 @@ func NewSimApp( app.ScopedIBCKeeper = scopedIBCKeeper app.ScopedTransferKeeper = scopedTransferKeeper + app.ScopedICAControllerKeeper = scopedICAControllerKeeper + app.ScopedICAHostKeeper = scopedICAHostKeeper // NOTE: the IBC mock keeper and application module is used only for testing core IBC. Do // note replicate if you do not need to test core IBC or light clients. app.ScopedIBCMockKeeper = scopedIBCMockKeeper + app.ScopedICAMockKeeper = scopedICAMockKeeper return app } @@ -579,11 +632,6 @@ func (app *SimApp) GetScopedIBCKeeper() capabilitykeeper.ScopedKeeper { return app.ScopedIBCKeeper } -// GetMockModule returns the mock module in the testing application -func (app *SimApp) GetMockModule() ibcmock.AppModule { - return app.mm.Modules[ibcmock.ModuleName].(ibcmock.AppModule) -} - // GetTxConfig implements the TestingApp interface. func (app *SimApp) GetTxConfig() client.TxConfig { return MakeTestEncodingConfig().TxConfig @@ -660,6 +708,8 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino paramsKeeper.Subspace(crisistypes.ModuleName) paramsKeeper.Subspace(ibctransfertypes.ModuleName) paramsKeeper.Subspace(ibchost.ModuleName) + paramsKeeper.Subspace(icacontrollertypes.SubModuleName) + paramsKeeper.Subspace(icahosttypes.SubModuleName) return paramsKeeper } diff --git a/third_party/proto/cosmos/auth/v1beta1/auth.proto b/third_party/proto/cosmos/auth/v1beta1/auth.proto new file mode 100644 index 0000000000..79071456f6 --- /dev/null +++ b/third_party/proto/cosmos/auth/v1beta1/auth.proto @@ -0,0 +1,51 @@ +syntax = "proto3"; +package cosmos.auth.v1beta1; + +import "cosmos_proto/cosmos.proto"; +import "gogoproto/gogo.proto"; +import "google/protobuf/any.proto"; + +option go_package = "github.com/cosmos/cosmos-sdk/x/auth/types"; + +// BaseAccount defines a base account type. It contains all the necessary fields +// for basic account functionality. Any custom account type should extend this +// type for additional functionality (e.g. vesting). +message BaseAccount { + option (gogoproto.goproto_getters) = false; + option (gogoproto.goproto_stringer) = false; + option (gogoproto.equal) = false; + + option (cosmos_proto.implements_interface) = "AccountI"; + + string address = 1; + google.protobuf.Any pub_key = 2 + [(gogoproto.jsontag) = "public_key,omitempty", (gogoproto.moretags) = "yaml:\"public_key\""]; + uint64 account_number = 3 [(gogoproto.moretags) = "yaml:\"account_number\""]; + uint64 sequence = 4; +} + +// ModuleAccount defines an account for modules that holds coins on a pool. +message ModuleAccount { + option (gogoproto.goproto_getters) = false; + option (gogoproto.goproto_stringer) = false; + option (cosmos_proto.implements_interface) = "ModuleAccountI"; + + BaseAccount base_account = 1 [(gogoproto.embed) = true, (gogoproto.moretags) = "yaml:\"base_account\""]; + string name = 2; + repeated string permissions = 3; +} + +// Params defines the parameters for the auth module. +message Params { + option (gogoproto.equal) = true; + option (gogoproto.goproto_stringer) = false; + + uint64 max_memo_characters = 1 [(gogoproto.moretags) = "yaml:\"max_memo_characters\""]; + uint64 tx_sig_limit = 2 [(gogoproto.moretags) = "yaml:\"tx_sig_limit\""]; + uint64 tx_size_cost_per_byte = 3 [(gogoproto.moretags) = "yaml:\"tx_size_cost_per_byte\""]; + uint64 sig_verify_cost_ed25519 = 4 + [(gogoproto.customname) = "SigVerifyCostED25519", (gogoproto.moretags) = "yaml:\"sig_verify_cost_ed25519\""]; + uint64 sig_verify_cost_secp256k1 = 5 + [(gogoproto.customname) = "SigVerifyCostSecp256k1", (gogoproto.moretags) = "yaml:\"sig_verify_cost_secp256k1\""]; +} + diff --git a/third_party/proto/cosmos/auth/v1beta1/genesis.proto b/third_party/proto/cosmos/auth/v1beta1/genesis.proto new file mode 100644 index 0000000000..5d6823e6b3 --- /dev/null +++ b/third_party/proto/cosmos/auth/v1beta1/genesis.proto @@ -0,0 +1,18 @@ +syntax = "proto3"; +package cosmos.auth.v1beta1; + +import "google/protobuf/any.proto"; +import "gogoproto/gogo.proto"; +import "cosmos/auth/v1beta1/auth.proto"; + +option go_package = "github.com/cosmos/cosmos-sdk/x/auth/types"; + +// GenesisState defines the auth module's genesis state. +message GenesisState { + // params defines all the paramaters of the module. + Params params = 1 [(gogoproto.nullable) = false]; + + // accounts are the accounts present at genesis. + repeated google.protobuf.Any accounts = 2; +} + diff --git a/third_party/proto/cosmos/auth/v1beta1/query.proto b/third_party/proto/cosmos/auth/v1beta1/query.proto new file mode 100644 index 0000000000..2c438c7b5e --- /dev/null +++ b/third_party/proto/cosmos/auth/v1beta1/query.proto @@ -0,0 +1,48 @@ +syntax = "proto3"; +package cosmos.auth.v1beta1; + +import "gogoproto/gogo.proto"; +import "google/protobuf/any.proto"; +import "google/api/annotations.proto"; +import "cosmos/auth/v1beta1/auth.proto"; +import "cosmos_proto/cosmos.proto"; + +option go_package = "github.com/cosmos/cosmos-sdk/x/auth/types"; + +// Query defines the gRPC querier service. +service Query { + // Account returns account details based on address. + rpc Account(QueryAccountRequest) returns (QueryAccountResponse) { + option (google.api.http).get = "/cosmos/auth/v1beta1/accounts/{address}"; + } + + // Params queries all parameters. + rpc Params(QueryParamsRequest) returns (QueryParamsResponse) { + option (google.api.http).get = "/cosmos/auth/v1beta1/params"; + } +} + +// QueryAccountRequest is the request type for the Query/Account RPC method. +message QueryAccountRequest { + option (gogoproto.equal) = false; + option (gogoproto.goproto_getters) = false; + + // address defines the address to query for. + string address = 1; +} + +// QueryAccountResponse is the response type for the Query/Account RPC method. +message QueryAccountResponse { + // account defines the account of the corresponding address. + google.protobuf.Any account = 1 [(cosmos_proto.accepts_interface) = "AccountI"]; +} + +// QueryParamsRequest is the request type for the Query/Params RPC method. +message QueryParamsRequest {} + +// QueryParamsResponse is the response type for the Query/Params RPC method. +message QueryParamsResponse { + // params defines the parameters of the module. + Params params = 1 [(gogoproto.nullable) = false]; +} + diff --git a/third_party/proto/cosmos_proto/cosmos.proto b/third_party/proto/cosmos_proto/cosmos.proto new file mode 100644 index 0000000000..167b170757 --- /dev/null +++ b/third_party/proto/cosmos_proto/cosmos.proto @@ -0,0 +1,16 @@ +syntax = "proto3"; +package cosmos_proto; + +import "google/protobuf/descriptor.proto"; + +option go_package = "github.com/regen-network/cosmos-proto"; + +extend google.protobuf.MessageOptions { + string interface_type = 93001; + + string implements_interface = 93002; +} + +extend google.protobuf.FieldOptions { + string accepts_interface = 93001; +}