Skip to content

Commit

Permalink
added switch case to detect blank pointers in keeper (backport #2403) (
Browse files Browse the repository at this point in the history
…#2447)

* added switch case to detect blank pointers in keeper (#2403)

* 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 <carlos@interchain.io>
Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com>
(cherry picked from commit 462ccb6)

# Conflicts:
#	CHANGELOG.md

* fix conflict

* Update CHANGELOG.md

Co-authored-by: Ali Zahid Raja <alizahidrajaa@gmail.com>
Co-authored-by: Carlos Rodriguez <carlos@interchain.io>
  • Loading branch information
3 people authored Sep 30, 2022
1 parent 9bcb54a commit e920d7a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,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) [\#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

Expand Down
21 changes: 18 additions & 3 deletions modules/core/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}

Expand Down Expand Up @@ -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
}
22 changes: 16 additions & 6 deletions modules/core/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,31 @@ 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
emptyMockStakingKeeper := MockStakingKeeper{}

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{}
Expand Down

0 comments on commit e920d7a

Please sign in to comment.