diff --git a/CHANGELOG.md b/CHANGELOG.md index 81de10a0fa..2379f6e21d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,8 @@ [Full Changelog](https://github.com/CosmWasm/wasmd/compare/v0.15.0...HEAD) -- Implement IBC contract support [\#394](https://github.com/CosmWasm/wasmd/pull/394) +- Support custom MessageHandler [\#327](https://github.com/CosmWasm/wasmd/issues/327) +- 🎉 Implement IBC contract support [\#394](https://github.com/CosmWasm/wasmd/pull/394) ## [v0.15.0](https://github.com/CosmWasm/wasmd/tree/v0.15.0) (2021-01-27) diff --git a/x/wasm/internal/keeper/options.go b/x/wasm/internal/keeper/options.go index d7fb6b3acc..5e5d6eb607 100644 --- a/x/wasm/internal/keeper/options.go +++ b/x/wasm/internal/keeper/options.go @@ -8,8 +8,17 @@ func (f optsFn) apply(keeper *Keeper) { f(keeper) } +// WithMessageHandler is an optional constructor parameter to replace the default wasm vm engine with the +// given one. func WithWasmEngine(x types.WasmerEngine) Option { return optsFn(func(k *Keeper) { k.wasmer = x }) } + +// WithMessageHandler is an optional constructor parameter to set a custom message handler. +func WithMessageHandler(n messenger) Option { + return optsFn(func(k *Keeper) { + k.messenger = n + }) +} diff --git a/x/wasm/internal/keeper/options_test.go b/x/wasm/internal/keeper/options_test.go new file mode 100644 index 0000000000..2de206c75e --- /dev/null +++ b/x/wasm/internal/keeper/options_test.go @@ -0,0 +1,57 @@ +package keeper + +import ( + "github.com/CosmWasm/wasmd/x/wasm/internal/keeper/wasmtesting" + "github.com/CosmWasm/wasmd/x/wasm/internal/types" + authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" + distributionkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper" + paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" + stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestConstructorOptions(t *testing.T) { + specs := map[string]struct { + srcOpt Option + verify func(Keeper) + }{ + "wasm engine": { + srcOpt: WithWasmEngine(&wasmtesting.MockWasmer{}), + verify: func(k Keeper) { + assert.IsType(t, k.wasmer, &wasmtesting.MockWasmer{}) + }, + }, + "message handler": { + srcOpt: WithMessageHandler(&wasmtesting.MockMessageHandler{}), + verify: func(k Keeper) { + assert.IsType(t, k.messenger, &wasmtesting.MockMessageHandler{}) + }, + }, + } + for name, spec := range specs { + t.Run(name, func(t *testing.T) { + k := NewKeeper( + nil, + nil, + paramtypes.NewSubspace(nil, nil, nil, nil, ""), + authkeeper.AccountKeeper{}, + nil, + stakingkeeper.Keeper{}, + distributionkeeper.Keeper{}, + nil, + nil, + nil, + nil, + "tempDir", + types.DefaultWasmConfig(), + "", + nil, + nil, + spec.srcOpt, + ) + spec.verify(k) + }) + } + +}