Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

added switch case to detect blank pointers in keeper #2403

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added function to check for empty keeper
  • Loading branch information
alizahidraja committed Sep 29, 2022
commit 316a1e89706bedf36a38083637d558ac3af096e4
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (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 switch case in NewKeeper to cater for blank pointers.
* (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

Expand Down
41 changes: 20 additions & 21 deletions modules/core/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,11 @@ func NewKeeper(
}

// panic if any of the keepers passed in is empty

// switch case to detect blank pointers
switch reflect.TypeOf(stakingKeeper).Kind() {
case reflect.Ptr:
if reflect.ValueOf(stakingKeeper).Elem().IsZero() {
panic(fmt.Errorf("cannot initialize IBC keeper: empty staking keeper pointer"))
}
default:
if reflect.ValueOf(stakingKeeper).IsZero() {
panic(fmt.Errorf("cannot initialize IBC keeper: empty staking keeper value"))
}
if isEmpty(stakingKeeper) {
panic(fmt.Errorf("cannot initialize IBC keeper: empty staking keeper"))
}

switch reflect.TypeOf(upgradeKeeper).Kind() {
case reflect.Ptr:
if reflect.ValueOf(upgradeKeeper).Elem().IsZero() {
panic(fmt.Errorf("cannot initialize IBC keeper: empty upgrade keeper pointer"))
}
default:
if reflect.ValueOf(upgradeKeeper).IsZero() {
panic(fmt.Errorf("cannot initialize IBC keeper: empty upgrade keeper value"))
}
if isEmpty(upgradeKeeper) {
panic(fmt.Errorf("cannot initialize IBC keeper: empty upgrade keeper"))
}

if reflect.DeepEqual(capabilitykeeper.ScopedKeeper{}, scopedKeeper) {
Expand Down Expand Up @@ -108,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
}