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

New validation for keeper fields #740

Merged
merged 9 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 3 additions & 2 deletions testutil/keeper/unit_test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"
"time"

authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
abci "github.com/tendermint/tendermint/abci/types"

"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -122,7 +123,7 @@ func NewInMemProviderKeeper(params InMemKeeperParams, mocks MockedKeepers) provi
mocks.MockSlashingKeeper,
mocks.MockAccountKeeper,
mocks.MockEvidenceKeeper,
"",
authtypes.FeeCollectorName,
)
}

Expand All @@ -142,7 +143,7 @@ func NewInMemConsumerKeeper(params InMemKeeperParams, mocks MockedKeepers) consu
mocks.MockAccountKeeper,
mocks.MockIBCTransferKeeper,
mocks.MockIBCCoreKeeper,
"",
authtypes.FeeCollectorName,
)
}

Expand Down
2 changes: 1 addition & 1 deletion x/ccv/consumer/keeper/distribution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestGetEstimatedNextFeeDistribution(t *testing.T) {

// Setup mock calls
gomock.InOrder(
mockAccountKeeper.EXPECT().GetModuleAccount(ctx, "").
mockAccountKeeper.EXPECT().GetModuleAccount(ctx, authTypes.FeeCollectorName).
Return(mAcc).
Times(1),
mockBankKeeper.EXPECT().GetAllBalances(ctx, mAcc.GetAddress()).
Expand Down
61 changes: 60 additions & 1 deletion x/ccv/consumer/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"encoding/binary"
"fmt"
"reflect"
"time"

"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -58,7 +59,7 @@ func NewKeeper(
paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable())
}

return Keeper{
k := Keeper{
storeKey: key,
cdc: cdc,
paramStore: paramSpace,
Expand All @@ -74,6 +75,64 @@ func NewKeeper(
ibcCoreKeeper: ibcCoreKeeper,
feeCollectorName: feeCollectorName,
}

k.mustValidateFields()
return k
}

// Validates that the consumer keeper is initialized with non-zero and
// non-nil values for all its fields. Otherwise this method will panic.
func (k Keeper) mustValidateFields() {

// Ensures no fields are missed in this validation
if reflect.ValueOf(k).NumField() != 15 {
panic("number of fields in provider keeper is not 15")
}

// Note 14 fields will be validated, hooks are explicitly set after the constructor

if reflect.ValueOf(k.storeKey).IsZero() { // 1
panic("storeKey is zero-valued or nil")
}
if reflect.ValueOf(k.cdc).IsZero() { // 2
panic("cdc is zero-valued or nil")
}
if reflect.ValueOf(k.paramStore).IsZero() { // 3
panic("paramStore is zero-valued or nil")
}
if reflect.ValueOf(k.scopedKeeper).IsZero() { // 4
panic("scopedKeeper is zero-valued or nil")
}
if reflect.ValueOf(k.channelKeeper).IsZero() { // 5
panic("channelKeeper is zero-valued or nil")
}
if reflect.ValueOf(k.portKeeper).IsZero() { // 6
panic("portKeeper is zero-valued or nil")
}
if reflect.ValueOf(k.connectionKeeper).IsZero() { // 7
panic("connectionKeeper is zero-valued or nil")
}
if reflect.ValueOf(k.clientKeeper).IsZero() { // 8
panic("clientKeeper is zero-valued or nil")
}
if reflect.ValueOf(k.slashingKeeper).IsZero() { // 9
panic("slashingKeeper is zero-valued or nil")
}
if reflect.ValueOf(k.bankKeeper).IsZero() { // 10
panic("bankKeeper is zero-valued or nil")
}
if reflect.ValueOf(k.authKeeper).IsZero() { // 11
panic("authKeeper is zero-valued or nil")
}
if reflect.ValueOf(k.ibcTransferKeeper).IsZero() { // 12
panic("ibcTransferKeeper is zero-valued or nil")
}
if reflect.ValueOf(k.ibcCoreKeeper).IsZero() { // 13
panic("ibcCoreKeeper is zero-valued or nil")
}
if reflect.ValueOf(k.feeCollectorName).IsZero() { // 14
panic("feeCollectorName is zero-valued or nil")
}
}

// Logger returns a module-specific logger.
Expand Down
2 changes: 1 addition & 1 deletion x/ccv/provider/ibc_module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func TestOnChanOpenTry(t *testing.T) {
mocks.MockClientKeeper.EXPECT().GetClientState(ctx, "clientIDToConsumer").Return(
&ibctmtypes.ClientState{ChainId: "consumerChainID"}, true,
).AnyTimes(),
mocks.MockAccountKeeper.EXPECT().GetModuleAccount(ctx, "").Return(&moduleAcct).AnyTimes(),
mocks.MockAccountKeeper.EXPECT().GetModuleAccount(ctx, authtypes.FeeCollectorName).Return(&moduleAcct).AnyTimes(),
)

tc.mutateParams(&params, &providerKeeper)
Expand Down
56 changes: 55 additions & 1 deletion x/ccv/provider/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"encoding/binary"
"fmt"
"reflect"
"time"

"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -56,7 +57,7 @@ func NewKeeper(
paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable())
}

return Keeper{
k := Keeper{
cdc: cdc,
storeKey: key,
paramSpace: paramSpace,
Expand All @@ -71,6 +72,59 @@ func NewKeeper(
evidenceKeeper: evidenceKeeper,
feeCollectorName: feeCollectorName,
}

k.mustValidateFields()
return k
}

// Validates that the provider keeper is initialized with non-zero and
// non-nil values for all its fields. Otherwise this method will panic.
func (k Keeper) mustValidateFields() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering in the future if it's worth refactoring to have mustValidateFields be generic for keepers, then pass in a set of validations instead per keeper....

validations := map[string]func(Keeper) bool{
	"number of fields in provider keeper is not 13": func(keeper Keeper) bool { return reflect.ValueOf(k).NumField() != 13 },
	"cdc is zero-valued or nil": func(k Keeper) bool { return reflect.ValueOf(k.cdc).IsZero() },
	"storeKey is zero-valued or nil": func(keeper Keeper) bool { return reflect.ValueOf(k.storeKey).IsZero()},
}

...

func (k Keeper) mustValidateFields(validations map[string]func(Keeper) bool) {
	for errMessage, failedValidation := range validations {
		if failedValidation(k) {
			panic(errMessage)
		}
	}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a solid idea! Although in our repo there will only ever be 2 keepers defined, so maybe not worth adding the additional code and complexity? That sort of functionality living within the sdk repo would make the most sense to me.

Lmk if you strongly disagree and I can refactor in a future PR


// Ensures no fields are missed in this validation
if reflect.ValueOf(k).NumField() != 13 {
panic("number of fields in provider keeper is not 13")
}

if reflect.ValueOf(k.cdc).IsZero() { // 1
panic("cdc is zero-valued or nil")
}
if reflect.ValueOf(k.storeKey).IsZero() { // 2
panic("storeKey is zero-valued or nil")
}
if reflect.ValueOf(k.paramSpace).IsZero() { // 3
panic("paramSpace is zero-valued or nil")
}
if reflect.ValueOf(k.scopedKeeper).IsZero() { // 4
panic("scopedKeeper is zero-valued or nil")
}
if reflect.ValueOf(k.channelKeeper).IsZero() { // 5
panic("channelKeeper is zero-valued or nil")
}
if reflect.ValueOf(k.portKeeper).IsZero() { // 6
panic("portKeeper is zero-valued or nil")
}
if reflect.ValueOf(k.connectionKeeper).IsZero() { // 7
panic("connectionKeeper is zero-valued or nil")
}
if reflect.ValueOf(k.accountKeeper).IsZero() { // 8
panic("accountKeeper is zero-valued or nil")
}
if reflect.ValueOf(k.clientKeeper).IsZero() { // 9
panic("clientKeeper is zero-valued or nil")
}
if reflect.ValueOf(k.stakingKeeper).IsZero() { // 10
panic("stakingKeeper is zero-valued or nil")
}
if reflect.ValueOf(k.slashingKeeper).IsZero() { // 11
panic("slashingKeeper is zero-valued or nil")
}
if reflect.ValueOf(k.evidenceKeeper).IsZero() { // 12
panic("evidenceKeeper is zero-valued or nil")
}
if reflect.ValueOf(k.feeCollectorName).IsZero() { // 13
panic("feeCollectorName is zero-valued or nil")
}
}

// Logger returns a module-specific logger.
Expand Down