Skip to content

Commit

Permalink
Refactor InitGenesis function
Browse files Browse the repository at this point in the history
  • Loading branch information
pinosu committed Dec 9, 2022
1 parent dfc4ab2 commit aeecb1c
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 52 deletions.
2 changes: 1 addition & 1 deletion x/wasm/client/cli/gov_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func ProposalInstantiateContract2Cmd() *cobra.Command {
return err
}

src, err := parseInstantiateArgs(args[0], args[1], clientCtx.FromAddress, cmd.Flags())
src, err := parseInstantiateArgs(args[0], args[1], clientCtx.Keyring, clientCtx.FromAddress, cmd.Flags())
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion x/wasm/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestInitGenesis(t *testing.T) {
q2 := newData.module.LegacyQuerierHandler(nil)

// initialize new app with genstate
InitGenesis(newData.ctx, &newData.keeper, *genState, newData.stakingKeeper, newData.module.Route().Handler())
InitGenesis(newData.ctx, &newData.keeper, *genState)

// run same checks again on newdata, to make sure it was reinitialized correctly
assertCodeList(t, q2, newData.ctx, 1)
Expand Down
2 changes: 1 addition & 1 deletion x/wasm/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type ValidatorSetSource interface {
// InitGenesis sets supply information for genesis.
//
// CONTRACT: all types of accounts must have been already initialized/created
func InitGenesis(ctx sdk.Context, keeper *Keeper, data types.GenesisState, stakingKeeper ValidatorSetSource, msgHandler sdk.Handler) ([]abci.ValidatorUpdate, error) {
func InitGenesis(ctx sdk.Context, keeper *Keeper, data types.GenesisState) ([]abci.ValidatorUpdate, error) {
contractKeeper := NewGovPermissionKeeper(keeper)
keeper.SetParams(ctx, data.Params)
var maxCodeID uint64
Expand Down
54 changes: 6 additions & 48 deletions x/wasm/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
fuzz "github.com/google/gofuzz"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
dbm "github.com/tendermint/tm-db"
Expand Down Expand Up @@ -118,7 +117,7 @@ func TestGenesisExportImport(t *testing.T) {
var importState wasmTypes.GenesisState
err = dstKeeper.cdc.UnmarshalJSON(exportedGenesis, &importState)
require.NoError(t, err)
InitGenesis(dstCtx, dstKeeper, importState, &StakingKeeperMock{}, TestHandler(contractKeeper))
InitGenesis(dstCtx, dstKeeper, importState)

// compare whole DB
for j := range srcStoreKeys {
Expand Down Expand Up @@ -146,10 +145,8 @@ func TestGenesisInit(t *testing.T) {

myCodeInfo := wasmTypes.CodeInfoFixture(wasmTypes.WithSHA256CodeHash(wasmCode))
specs := map[string]struct {
src types.GenesisState
stakingMock StakingKeeperMock
msgHandlerMock MockMsgHandler
expSuccess bool
src types.GenesisState
expSuccess bool
}{
"happy path: code info correct": {
src: types.GenesisState{
Expand Down Expand Up @@ -460,15 +457,13 @@ func TestGenesisInit(t *testing.T) {
keeper, ctx, _ := setupKeeper(t)

require.NoError(t, types.ValidateGenesis(spec.src))
gotValidatorSet, gotErr := InitGenesis(ctx, keeper, spec.src, &spec.stakingMock, spec.msgHandlerMock.Handle)
_, gotErr := InitGenesis(ctx, keeper, spec.src)
if !spec.expSuccess {
require.Error(t, gotErr)
return
}
require.NoError(t, gotErr)
spec.msgHandlerMock.verifyCalls(t)
spec.stakingMock.verifyCalls(t)
assert.Equal(t, spec.stakingMock.validatorUpdate, gotValidatorSet)

for _, c := range spec.src.Codes {
assert.Equal(t, c.Pinned, keeper.IsPinnedCode(ctx, c.CodeID))
}
Expand Down Expand Up @@ -540,7 +535,6 @@ func TestImportContractWithCodeHistoryPreserved(t *testing.T) {
]
}`
keeper, ctx, _ := setupKeeper(t)
contractKeeper := NewGovPermissionKeeper(keeper)

wasmCode, err := os.ReadFile("./testdata/hackatom.wasm")
require.NoError(t, err)
Expand All @@ -557,7 +551,7 @@ func TestImportContractWithCodeHistoryPreserved(t *testing.T) {
ctx = ctx.WithBlockHeight(0).WithGasMeter(sdk.NewInfiniteGasMeter())

// when
_, err = InitGenesis(ctx, keeper, importState, &StakingKeeperMock{}, TestHandler(contractKeeper))
_, err = InitGenesis(ctx, keeper, importState)
require.NoError(t, err)

// verify wasm code
Expand Down Expand Up @@ -675,39 +669,3 @@ func setupKeeper(t *testing.T) (*Keeper, sdk.Context, []sdk.StoreKey) {
)
return &srcKeeper, ctx, []sdk.StoreKey{keyWasm, keyParams}
}

type StakingKeeperMock struct {
err error
validatorUpdate []abci.ValidatorUpdate
expCalls int
gotCalls int
}

func (s *StakingKeeperMock) ApplyAndReturnValidatorSetUpdates(_ sdk.Context) ([]abci.ValidatorUpdate, error) {
s.gotCalls++
return s.validatorUpdate, s.err
}

func (s *StakingKeeperMock) verifyCalls(t *testing.T) {
assert.Equal(t, s.expCalls, s.gotCalls, "number calls")
}

type MockMsgHandler struct {
result *sdk.Result
err error
expCalls int
gotCalls int
expMsg sdk.Msg
gotMsg sdk.Msg
}

func (m *MockMsgHandler) Handle(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
m.gotCalls++
m.gotMsg = msg
return m.result, m.err
}

func (m *MockMsgHandler) verifyCalls(t *testing.T) {
assert.Equal(t, m.expMsg, m.gotMsg, "message param")
assert.Equal(t, m.expCalls, m.gotCalls, "number calls")
}
2 changes: 1 addition & 1 deletion x/wasm/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (AppModule) QuerierRoute() string {
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate {
var genesisState GenesisState
cdc.MustUnmarshalJSON(data, &genesisState)
validators, err := InitGenesis(ctx, am.keeper, genesisState, am.validatorSetSource, am.Route().Handler())
validators, err := InitGenesis(ctx, am.keeper, genesisState)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit aeecb1c

Please sign in to comment.