From 0ec5315f7349df4111055a6f09542c949d2359ff Mon Sep 17 00:00:00 2001 From: Ali Zahid Raja Date: Fri, 30 Sep 2022 00:06:11 +0500 Subject: [PATCH 1/3] added switch case to detect blank pointers in keeper (#2403) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * added switch case to detect blank pointers in keeper * updated changelog.md * updated error message for keeper * fixed switch-case; added test cases * added function to check for empty keeper Co-authored-by: Carlos Rodriguez Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> (cherry picked from commit 462ccb6c84467fa9baba7e8e5684c81e3933e008) # Conflicts: # CHANGELOG.md --- CHANGELOG.md | 20 ++++++++++++++++++++ modules/core/keeper/keeper.go | 21 ++++++++++++++++++--- modules/core/keeper/keeper_test.go | 22 ++++++++++++++++------ 3 files changed, 54 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c3fb917e26..ac914fb1c6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -67,6 +67,26 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (apps/27-interchain-accounts) [\#2147](https://github.com/cosmos/ibc-go/pull/2147) Adding a `SubmitTx` gRPC endpoint for the ICS27 Controller module which allows owners of interchain accounts to submit transactions. This replaces the previously existing need for authentication modules to implement this standard functionality. * (testing/simapp) [\#2190](https://github.com/cosmos/ibc-go/pull/2190) Adding the new `x/group` cosmos-sdk module to simapp. +<<<<<<< HEAD +======= + +### Bug Fixes + +* (makefile) [\#1785](https://github.com/cosmos/ibc-go/pull/1785) Fetch the correct versions of protocol buffers dependencies from tendermint, cosmos-sdk, and ics23. +* (light-clients/solomachine) [#1839](https://github.com/cosmos/ibc-go/issues/1839) Fixed usage of the new diversifier in validation of changing diversifiers for the solo machine. The current diversifier must sign over the new diversifier. +* (light-clients/07-tendermint) [\#1674](https://github.com/cosmos/ibc-go/pull/1674) Submitted ClientState is zeroed out before checking the proof in order to prevent the proposal from containing information governance is not actually voting on. +* (modules/core/02-client)[\#1676](https://github.com/cosmos/ibc-go/pull/1676) ClientState must be zeroed out for `UpgradeProposals` to pass validation. This prevents a proposal containing information governance is not actually voting on. +* (modules/core/keeper) [\#2268](https://github.com/cosmos/ibc-go/pull/2403) Added a function in keeper to cater for blank pointers. + +## [v4.1.0](https://github.com/cosmos/ibc-go/releases/tag/v4.1.0) - 2022-09-20 + +### Dependencies + +* [\#2288](https://github.com/cosmos/ibc-go/pull/2288) Bump SDK version to v0.45.8 and Tendermint to v0.34.21. + +### Features + +>>>>>>> 462ccb6 (added switch case to detect blank pointers in keeper (#2403)) * (apps/27-interchain-accounts) [\#2193](https://github.com/cosmos/ibc-go/pull/2193) Adding `InterchainAccount` gRPC query endpont to ICS27 `controller` submodule to allow users to retrieve registered interchain account addresses. ### Bug Fixes diff --git a/modules/core/keeper/keeper.go b/modules/core/keeper/keeper.go index 759936be3e9..680db22507e 100644 --- a/modules/core/keeper/keeper.go +++ b/modules/core/keeper/keeper.go @@ -50,11 +50,10 @@ func NewKeeper( } // panic if any of the keepers passed in is empty - if reflect.ValueOf(stakingKeeper).IsZero() { + if isEmpty(stakingKeeper) { panic(fmt.Errorf("cannot initialize IBC keeper: empty staking keeper")) } - - if reflect.ValueOf(upgradeKeeper).IsZero() { + if isEmpty(upgradeKeeper) { panic(fmt.Errorf("cannot initialize IBC keeper: empty upgrade keeper")) } @@ -92,3 +91,19 @@ func (k *Keeper) SetRouter(rtr *porttypes.Router) { k.Router = rtr k.Router.Seal() } + +// isEmpty checks if the interface is an empty struct or a pointer pointing +// to an empty struct +func isEmpty(keeper interface{}) bool { + switch reflect.TypeOf(keeper).Kind() { + case reflect.Ptr: + if reflect.ValueOf(keeper).Elem().IsZero() { + return true + } + default: + if reflect.ValueOf(keeper).IsZero() { + return true + } + } + return false +} diff --git a/modules/core/keeper/keeper_test.go b/modules/core/keeper/keeper_test.go index dd163c232b4..c8114875b47 100644 --- a/modules/core/keeper/keeper_test.go +++ b/modules/core/keeper/keeper_test.go @@ -79,10 +79,15 @@ func (suite *KeeperTestSuite) TestNewKeeper() { malleate func() expPass bool }{ - {"failure: empty staking keeper", func() { - emptyStakingKeeper := stakingkeeper.Keeper{} + {"failure: empty staking keeper value", func() { + emptyStakingKeeperValue := stakingkeeper.Keeper{} - stakingKeeper = emptyStakingKeeper + stakingKeeper = emptyStakingKeeperValue + }, false}, + {"failure: empty staking keeper pointer", func() { + emptyStakingKeeperPointer := &stakingkeeper.Keeper{} + + stakingKeeper = emptyStakingKeeperPointer }, false}, {"failure: empty mock staking keeper", func() { // use a different implementation of clienttypes.StakingKeeper @@ -90,10 +95,15 @@ func (suite *KeeperTestSuite) TestNewKeeper() { stakingKeeper = emptyMockStakingKeeper }, false}, - {"failure: empty upgrade keeper", func() { - emptyUpgradeKeeper := upgradekeeper.Keeper{} + {"failure: empty upgrade keeper value", func() { + emptyUpgradeKeeperValue := upgradekeeper.Keeper{} + + upgradeKeeper = emptyUpgradeKeeperValue + }, false}, + {"failure: empty upgrade keeper pointer", func() { + emptyUpgradeKeeperPointer := &upgradekeeper.Keeper{} - upgradeKeeper = emptyUpgradeKeeper + upgradeKeeper = emptyUpgradeKeeperPointer }, false}, {"failure: empty scoped keeper", func() { emptyScopedKeeper := capabilitykeeper.ScopedKeeper{} From d90adb992ff2c96da6288f742f8f67ac79ce032e Mon Sep 17 00:00:00 2001 From: Carlos Rodriguez Date: Fri, 30 Sep 2022 09:26:05 +0200 Subject: [PATCH 2/3] fix conflict --- CHANGELOG.md | 21 +-------------------- 1 file changed, 1 insertion(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ac914fb1c6a..3ef97362da9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -67,32 +67,13 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (apps/27-interchain-accounts) [\#2147](https://github.com/cosmos/ibc-go/pull/2147) Adding a `SubmitTx` gRPC endpoint for the ICS27 Controller module which allows owners of interchain accounts to submit transactions. This replaces the previously existing need for authentication modules to implement this standard functionality. * (testing/simapp) [\#2190](https://github.com/cosmos/ibc-go/pull/2190) Adding the new `x/group` cosmos-sdk module to simapp. -<<<<<<< HEAD -======= - -### Bug Fixes - -* (makefile) [\#1785](https://github.com/cosmos/ibc-go/pull/1785) Fetch the correct versions of protocol buffers dependencies from tendermint, cosmos-sdk, and ics23. -* (light-clients/solomachine) [#1839](https://github.com/cosmos/ibc-go/issues/1839) Fixed usage of the new diversifier in validation of changing diversifiers for the solo machine. The current diversifier must sign over the new diversifier. -* (light-clients/07-tendermint) [\#1674](https://github.com/cosmos/ibc-go/pull/1674) Submitted ClientState is zeroed out before checking the proof in order to prevent the proposal from containing information governance is not actually voting on. -* (modules/core/02-client)[\#1676](https://github.com/cosmos/ibc-go/pull/1676) ClientState must be zeroed out for `UpgradeProposals` to pass validation. This prevents a proposal containing information governance is not actually voting on. -* (modules/core/keeper) [\#2268](https://github.com/cosmos/ibc-go/pull/2403) Added a function in keeper to cater for blank pointers. - -## [v4.1.0](https://github.com/cosmos/ibc-go/releases/tag/v4.1.0) - 2022-09-20 - -### Dependencies - -* [\#2288](https://github.com/cosmos/ibc-go/pull/2288) Bump SDK version to v0.45.8 and Tendermint to v0.34.21. - -### Features - ->>>>>>> 462ccb6 (added switch case to detect blank pointers in keeper (#2403)) * (apps/27-interchain-accounts) [\#2193](https://github.com/cosmos/ibc-go/pull/2193) Adding `InterchainAccount` gRPC query endpont to ICS27 `controller` submodule to allow users to retrieve registered interchain account addresses. ### Bug Fixes * (27-interchain-accounts) [\#2308](https://github.com/cosmos/ibc-go/pull/2308) Nil checks have been added to ensure services are not registered for nil host or controller keepers. * (makefile) [\#1785](https://github.com/cosmos/ibc-go/pull/1785) Fetch the correct versions of protocol buffers dependencies from tendermint, cosmos-sdk, and ics23. +* (modules/core/keeper) [\#2268](https://github.com/cosmos/ibc-go/pull/2403) Added a function in keeper to cater for blank pointers. ## [v4.0.0](https://github.com/cosmos/ibc-go/releases/tag/v4.0.0) - 2022-08-12 From fbf8824422b9967282a0d8ab49fb96f8a2cb737b Mon Sep 17 00:00:00 2001 From: Carlos Rodriguez Date: Fri, 30 Sep 2022 09:26:22 +0200 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ef97362da9..7183288f5a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -73,7 +73,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (27-interchain-accounts) [\#2308](https://github.com/cosmos/ibc-go/pull/2308) Nil checks have been added to ensure services are not registered for nil host or controller keepers. * (makefile) [\#1785](https://github.com/cosmos/ibc-go/pull/1785) Fetch the correct versions of protocol buffers dependencies from tendermint, cosmos-sdk, and ics23. -* (modules/core/keeper) [\#2268](https://github.com/cosmos/ibc-go/pull/2403) Added a function in keeper to cater for blank pointers. +* (modules/core/keeper) [\#2403](https://github.com/cosmos/ibc-go/pull/2403) Added a function in keeper to cater for blank pointers. ## [v4.0.0](https://github.com/cosmos/ibc-go/releases/tag/v4.0.0) - 2022-08-12