Skip to content

Commit

Permalink
Merge commit '8627f85275761e69cfb2a535c0e9f48a065485a4' into ease-of-…
Browse files Browse the repository at this point in the history
…merge-linter-autofixes
  • Loading branch information
faddat committed Aug 4, 2023
2 parents 4919ec7 + 8627f85 commit 75f9385
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 43 deletions.
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ func NewWasmApp(

// The gov proposal types can be individually enabled
if len(enabledProposals) != 0 {
govRouter.AddRoute(wasmtypes.RouterKey, wasmkeeper.NewWasmProposalHandler(app.WasmKeeper, enabledProposals)) //nolint:staticcheck
govRouter.AddRoute(wasmtypes.RouterKey, wasmkeeper.NewWasmProposalHandler(app.WasmKeeper, enabledProposals))
}
// Set legacy router for backwards compatibility with gov v1beta1
app.GovKeeper.SetLegacyRouter(govRouter)
Expand Down
2 changes: 1 addition & 1 deletion x/wasm/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ var (
// Deprecated: Do not use.
TestHandler = keeper.TestHandler
// Deprecated: Do not use.
NewWasmProposalHandler = keeper.NewWasmProposalHandler //nolint:staticcheck
NewWasmProposalHandler = keeper.NewWasmProposalHandler
// Deprecated: Do not use.
NewQuerier = keeper.Querier
// Deprecated: Do not use.
Expand Down
107 changes: 66 additions & 41 deletions x/wasm/keeper/test_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,11 @@ type TestFaucet struct {
minterModuleName string
}

func NewTestFaucet(t testing.TB, ctx sdk.Context, bankKeeper bankkeeper.Keeper, minterModuleName string, initialAmount ...sdk.Coin) *TestFaucet {
require.NotEmpty(t, initialAmount)
r := &TestFaucet{t: t, bankKeeper: bankKeeper, minterModuleName: minterModuleName}
func NewTestFaucet(tb testing.TB, ctx sdk.Context, bankKeeper bankkeeper.Keeper, minterModuleName string, initialAmount ...sdk.Coin) *TestFaucet {
tb.Helper()

require.NotEmpty(tb, initialAmount)
r := &TestFaucet{t: tb, bankKeeper: bankKeeper, minterModuleName: minterModuleName}
_, addr := keyPubAddr()
r.sender = addr
r.Mint(ctx, addr, initialAmount...)
Expand Down Expand Up @@ -203,26 +205,32 @@ type TestKeepers struct {
}

// CreateDefaultTestInput common settings for CreateTestInput
func CreateDefaultTestInput(t testing.TB) (sdk.Context, TestKeepers) {
return CreateTestInput(t, false, "staking")
func CreateDefaultTestInput(tb testing.TB) (sdk.Context, TestKeepers) {
tb.Helper()

return CreateTestInput(tb, false, "staking")
}

// CreateTestInput encoders can be nil to accept the defaults, or set it to override some of the message handlers (like default)
func CreateTestInput(t testing.TB, isCheckTx bool, availableCapabilities string, opts ...Option) (sdk.Context, TestKeepers) {
func CreateTestInput(tb testing.TB, isCheckTx bool, availableCapabilities string, opts ...Option) (sdk.Context, TestKeepers) {
tb.Helper()

// Load default wasm config
return createTestInput(t, isCheckTx, availableCapabilities, types.DefaultWasmConfig(), dbm.NewMemDB(), opts...)
return createTestInput(tb, isCheckTx, availableCapabilities, types.DefaultWasmConfig(), dbm.NewMemDB(), opts...)
}

// encoders can be nil to accept the defaults, or set it to override some of the message handlers (like default)
func createTestInput(
t testing.TB,
tb testing.TB,
isCheckTx bool,
availableCapabilities string,
wasmConfig types.WasmConfig,
db dbm.DB,
opts ...Option,
) (sdk.Context, TestKeepers) {
tempDir := t.TempDir()
tb.Helper()

tempDir := tb.TempDir()

keys := storetypes.NewKVStoreKeys(
authtypes.StoreKey, banktypes.StoreKey, stakingtypes.StoreKey,
Expand All @@ -232,7 +240,8 @@ func createTestInput(
capabilitytypes.StoreKey, feegrant.StoreKey, authzkeeper.StoreKey,
types.StoreKey,
)
logger := log.NewTestLogger(t)
logger := log.NewTestLogger(tb)

ms := store.NewCommitMultiStore(db, logger, storemetrics.NewNoOpMetrics())
for _, v := range keys {
ms.MountStoreWithDB(v, storetypes.StoreTypeIAVL, db)
Expand All @@ -247,15 +256,15 @@ func createTestInput(
ms.MountStoreWithDB(v, storetypes.StoreTypeMemory, db)
}

require.NoError(t, ms.LoadLatestVersion())
require.NoError(tb, ms.LoadLatestVersion())

ctx := sdk.NewContext(ms, tmproto.Header{
Height: 1234567,
Time: time.Date(2020, time.April, 22, 12, 0, 0, 0, time.UTC),
}, isCheckTx, log.NewNopLogger())
ctx = types.WithTXCounter(ctx, 0)

encodingConfig := MakeEncodingConfig(t)
encodingConfig := MakeEncodingConfig(tb)
appCodec, legacyAmino := encodingConfig.Marshaler, encodingConfig.Amino

paramsKeeper := paramskeeper.NewKeeper(
Expand All @@ -282,7 +291,7 @@ func createTestInput(
}
subspace := func(m string) paramstypes.Subspace {
r, ok := paramsKeeper.GetSubspace(m)
require.True(t, ok)
require.True(tb, ok)

var keyTable paramstypes.KeyTable
switch r.Name() {
Expand Down Expand Up @@ -345,7 +354,7 @@ func createTestInput(
for acc := range maccPerms {
blockedAddrs[authtypes.NewModuleAddress(acc).String()] = true
}
require.NoError(t, accountKeeper.Params.Set(ctx, authtypes.DefaultParams()))
require.NoError(tb, accountKeeper.Params.Set(ctx, authtypes.DefaultParams()))

bankKeeper := bankkeeper.NewBaseKeeper(
appCodec,
Expand All @@ -355,7 +364,7 @@ func createTestInput(
authtypes.NewModuleAddress(banktypes.ModuleName).String(),
logger,
)
require.NoError(t, bankKeeper.SetParams(ctx, banktypes.DefaultParams()))
require.NoError(tb, bankKeeper.SetParams(ctx, banktypes.DefaultParams()))

stakingKeeper := stakingkeeper.NewKeeper(
appCodec,
Expand All @@ -367,7 +376,7 @@ func createTestInput(
authcodec.NewBech32Codec(sdk.GetConfig().GetBech32ConsensusAddrPrefix()),
)
stakingtypes.DefaultParams()
require.NoError(t, stakingKeeper.SetParams(ctx, TestingStakeParams))
require.NoError(tb, stakingKeeper.SetParams(ctx, TestingStakeParams))

distKeeper := distributionkeeper.NewKeeper(
appCodec,
Expand All @@ -378,8 +387,8 @@ func createTestInput(
authtypes.FeeCollectorName,
authtypes.NewModuleAddress(distributiontypes.ModuleName).String(),
)
require.NoError(t, distKeeper.Params.Set(ctx, distributiontypes.DefaultParams()))
require.NoError(t, distKeeper.FeePool.Set(ctx, distributiontypes.InitialFeePool()))
require.NoError(tb, distKeeper.Params.Set(ctx, distributiontypes.DefaultParams()))
require.NoError(tb, distKeeper.FeePool.Set(ctx, distributiontypes.InitialFeePool()))
stakingKeeper.SetHooks(distKeeper.Hooks())

upgradeKeeper := upgradekeeper.NewKeeper(
Expand All @@ -391,7 +400,7 @@ func createTestInput(
authtypes.NewModuleAddress(upgradetypes.ModuleName).String(),
)

faucet := NewTestFaucet(t, ctx, bankKeeper, minttypes.ModuleName, sdk.NewCoin("stake", sdkmath.NewInt(100_000_000_000)))
faucet := NewTestFaucet(tb, ctx, bankKeeper, minttypes.ModuleName, sdk.NewCoin("stake", sdkmath.NewInt(100_000_000_000)))

// set some funds ot pay out validatores, based on code from:
// https://github.com/cosmos/cosmos-sdk/blob/fea231556aee4d549d7551a6190389c4328194eb/x/distribution/keeper/keeper_test.go#L50-L57
Expand Down Expand Up @@ -444,7 +453,7 @@ func createTestInput(
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
opts...,
)
require.NoError(t, keeper.SetParams(ctx, types.DefaultParams()))
require.NoError(tb, keeper.SetParams(ctx, types.DefaultParams()))

// add wasm handler so we can loop-back (contracts calling contracts)
contractKeeper := NewDefaultPermissionKeeper(&keeper)
Expand All @@ -465,7 +474,7 @@ func createTestInput(
govtypes.DefaultConfig(),
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
require.NoError(t, govKeeper.Params.Set(ctx, govv1.DefaultParams()))
require.NoError(tb, govKeeper.Params.Set(ctx, govv1.DefaultParams()))
govKeeper.SetLegacyRouter(govRouter)

am := module.NewManager( // minimal module set that we use for message/ query tests
Expand Down Expand Up @@ -592,8 +601,10 @@ func DeterministicAccountAddress(_ testing.TB, v byte) sdk.AccAddress {
return bytes.Repeat([]byte{v}, address.Len)
}

func RandomBech32AccountAddress(t testing.TB) string {
return RandomAccountAddress(t).String()
func RandomBech32AccountAddress(tb testing.TB) string {
tb.Helper()

return RandomAccountAddress(tb).String()
}

type ExampleContract struct {
Expand All @@ -604,35 +615,47 @@ type ExampleContract struct {
Checksum []byte
}

func StoreHackatomExampleContract(t testing.TB, ctx sdk.Context, keepers TestKeepers) ExampleContract {
return StoreExampleContractWasm(t, ctx, keepers, testdata.HackatomContractWasm())
func StoreHackatomExampleContract(tb testing.TB, ctx sdk.Context, keepers TestKeepers) ExampleContract {
tb.Helper()

return StoreExampleContractWasm(tb, ctx, keepers, testdata.HackatomContractWasm())
}

func StoreBurnerExampleContract(t testing.TB, ctx sdk.Context, keepers TestKeepers) ExampleContract {
return StoreExampleContractWasm(t, ctx, keepers, testdata.BurnerContractWasm())
func StoreBurnerExampleContract(tb testing.TB, ctx sdk.Context, keepers TestKeepers) ExampleContract {
tb.Helper()

return StoreExampleContractWasm(tb, ctx, keepers, testdata.BurnerContractWasm())
}

func StoreIBCReflectContract(t testing.TB, ctx sdk.Context, keepers TestKeepers) ExampleContract {
return StoreExampleContractWasm(t, ctx, keepers, testdata.IBCReflectContractWasm())
func StoreIBCReflectContract(tb testing.TB, ctx sdk.Context, keepers TestKeepers) ExampleContract {
tb.Helper()

return StoreExampleContractWasm(tb, ctx, keepers, testdata.IBCReflectContractWasm())
}

func StoreReflectContract(t testing.TB, ctx sdk.Context, keepers TestKeepers) ExampleContract {
return StoreExampleContractWasm(t, ctx, keepers, testdata.ReflectContractWasm())
func StoreReflectContract(tb testing.TB, ctx sdk.Context, keepers TestKeepers) ExampleContract {
tb.Helper()

return StoreExampleContractWasm(tb, ctx, keepers, testdata.ReflectContractWasm())
}

func StoreExampleContract(t testing.TB, ctx sdk.Context, keepers TestKeepers, wasmFile string) ExampleContract {
func StoreExampleContract(tb testing.TB, ctx sdk.Context, keepers TestKeepers, wasmFile string) ExampleContract {
tb.Helper()

wasmCode, err := os.ReadFile(wasmFile)
require.NoError(t, err)
return StoreExampleContractWasm(t, ctx, keepers, wasmCode)
require.NoError(tb, err)
return StoreExampleContractWasm(tb, ctx, keepers, wasmCode)
}

func StoreExampleContractWasm(t testing.TB, ctx sdk.Context, keepers TestKeepers, wasmCode []byte) ExampleContract {
func StoreExampleContractWasm(tb testing.TB, ctx sdk.Context, keepers TestKeepers, wasmCode []byte) ExampleContract {
tb.Helper()

anyAmount := sdk.NewCoins(sdk.NewInt64Coin("denom", 1000))
creator, creatorAddr := keyPubAddr()
fundAccounts(t, ctx, keepers.AccountKeeper, keepers.BankKeeper, creatorAddr, anyAmount)
fundAccounts(tb, ctx, keepers.AccountKeeper, keepers.BankKeeper, creatorAddr, anyAmount)

codeID, _, err := keepers.ContractKeeper.Create(ctx, creatorAddr, wasmCode, nil)
require.NoError(t, err)
require.NoError(tb, err)
hash := keepers.WasmKeeper.GetCodeInfo(ctx, codeID).CodeHash
return ExampleContract{anyAmount, creator, creatorAddr, codeID, hash}
}
Expand All @@ -645,11 +668,11 @@ type ExampleContractInstance struct {
}

// SeedNewContractInstance sets the mock wasmerEngine in keeper and calls store + instantiate to init the contract's metadata
func SeedNewContractInstance(t testing.TB, ctx sdk.Context, keepers TestKeepers, mock types.WasmerEngine) ExampleContractInstance {
t.Helper()
exampleContract := StoreRandomContract(t, ctx, keepers, mock)
func SeedNewContractInstance(tb testing.TB, ctx sdk.Context, keepers TestKeepers, mock types.WasmerEngine) ExampleContractInstance {
tb.Helper()
exampleContract := StoreRandomContract(tb, ctx, keepers, mock)
contractAddr, _, err := keepers.ContractKeeper.Instantiate(ctx, exampleContract.CodeID, exampleContract.CreatorAddr, exampleContract.CreatorAddr, []byte(`{}`), "", nil)
require.NoError(t, err)
require.NoError(tb, err)
return ExampleContractInstance{
ExampleContract: exampleContract,
Contract: contractAddr,
Expand All @@ -658,6 +681,7 @@ func SeedNewContractInstance(t testing.TB, ctx sdk.Context, keepers TestKeepers,

// StoreRandomContract sets the mock wasmerEngine in keeper and calls store
func StoreRandomContract(tb testing.TB, ctx sdk.Context, keepers TestKeepers, mock types.WasmerEngine) ExampleContract {
tb.Helper()
return StoreRandomContractWithAccessConfig(tb, ctx, keepers, mock, nil)
}

Expand Down Expand Up @@ -692,6 +716,7 @@ type HackatomExampleInstance struct {

// InstantiateHackatomExampleContract load and instantiate the "./testdata/hackatom.wasm" contract
func InstantiateHackatomExampleContract(tb testing.TB, ctx sdk.Context, keepers TestKeepers) HackatomExampleInstance {
tb.Helper()
contract := StoreHackatomExampleContract(tb, ctx, keepers)

verifier, verifierAddr := keyPubAddr()
Expand Down

0 comments on commit 75f9385

Please sign in to comment.