Skip to content

Commit

Permalink
feat: add pluskeeper
Browse files Browse the repository at this point in the history
Because of avoiding the import cycle problem
  • Loading branch information
da1suk8 committed Apr 21, 2023
1 parent 78f312f commit 6b4897f
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 13 deletions.
2 changes: 2 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ import (
"github.com/line/wasmd/x/wasm"
wasmclient "github.com/line/wasmd/x/wasm/client"
wasmkeeper "github.com/line/wasmd/x/wasm/keeper"
wasmpluskeeper "github.com/line/wasmd/x/wasmplus/keeper"

// unnamed import of statik for swagger UI support
_ "github.com/line/lbm-sdk/client/docs/statik"
Expand Down Expand Up @@ -520,6 +521,7 @@ func NewWasmApp(
wasmDir,
wasmConfig,
availableCapabilities,
&wasmpluskeeper.Keeper{},
wasmOpts...,
)

Expand Down
1 change: 1 addition & 0 deletions x/wasm/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@ func setupKeeper(t *testing.T) (*Keeper, sdk.Context, []sdk.StoreKey) {
tempDir,
wasmConfig,
AvailableCapabilities,
&mockpluskeeper{},
)
return &srcKeeper, ctx, []sdk.StoreKey{keyWasm, keyParams}
}
Expand Down
15 changes: 9 additions & 6 deletions x/wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ type Keeper struct {
maxQueryStackSize uint32
acceptedAccountTypes map[reflect.Type]struct{}
accountPruner AccountPruner
pluskeeper PlusKeeper
}

// NewKeeper creates a new contract Keeper instance
Expand All @@ -123,6 +124,7 @@ func NewKeeper(
homeDir string,
wasmConfig types.WasmConfig,
availableCapabilities string,
pluskeeper PlusKeeper,
opts ...Option,
) Keeper {
wasmer, err := wasmvm.NewVM(filepath.Join(homeDir, "wasm"), availableCapabilities, contractMemoryLimit, wasmConfig.ContractDebugMode, wasmConfig.MemoryCacheSize)
Expand Down Expand Up @@ -150,6 +152,7 @@ func NewKeeper(
gasRegister: NewDefaultWasmGasRegister(),
maxQueryStackSize: types.DefaultMaxQueryStackSize,
acceptedAccountTypes: defaultAcceptedAccountTypes,
pluskeeper: pluskeeper,
}
keeper.wasmVMQueryHandler = DefaultQueryPlugins(bankKeeper, stakingKeeper, distKeeper, channelKeeper, queryRouter, keeper)
for _, o := range opts {
Expand Down Expand Up @@ -352,7 +355,7 @@ func (k Keeper) instantiate(

// instantiate wasm contract
gas := k.runtimeGasForContract(ctx)
res, gasUsed, err := k.wasmVM.Instantiate(codeInfo.CodeHash, env, info, initMsg, prefixStore, cosmwasmAPI, querier, k.gasMeter(ctx), gas, costJSONDeserialization)
res, gasUsed, err := k.wasmVM.Instantiate(codeInfo.CodeHash, env, info, initMsg, prefixStore, k.pluskeeper.CosmwasmAPI(ctx), querier, k.gasMeter(ctx), gas, costJSONDeserialization)
k.consumeRuntimeGas(ctx, gasUsed)
if err != nil {
return nil, nil, sdkerrors.Wrap(types.ErrInstantiateFailed, err.Error())
Expand Down Expand Up @@ -420,7 +423,7 @@ func (k Keeper) execute(ctx sdk.Context, contractAddress sdk.AccAddress, caller
// prepare querier
querier := k.newQueryHandler(ctx, contractAddress)
gas := k.runtimeGasForContract(ctx)
res, gasUsed, execErr := k.wasmVM.Execute(codeInfo.CodeHash, env, info, msg, prefixStore, cosmwasmAPI, querier, k.gasMeter(ctx), gas, costJSONDeserialization)
res, gasUsed, execErr := k.wasmVM.Execute(codeInfo.CodeHash, env, info, msg, prefixStore, k.pluskeeper.CosmwasmAPI(ctx), querier, k.gasMeter(ctx), gas, costJSONDeserialization)
k.consumeRuntimeGas(ctx, gasUsed)
if execErr != nil {
return nil, sdkerrors.Wrap(types.ErrExecuteFailed, execErr.Error())
Expand Down Expand Up @@ -485,7 +488,7 @@ func (k Keeper) migrate(ctx sdk.Context, contractAddress sdk.AccAddress, caller
prefixStoreKey := types.GetContractStorePrefix(contractAddress)
prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), prefixStoreKey)
gas := k.runtimeGasForContract(ctx)
res, gasUsed, err := k.wasmVM.Migrate(newCodeInfo.CodeHash, env, msg, &prefixStore, cosmwasmAPI, &querier, k.gasMeter(ctx), gas, costJSONDeserialization)
res, gasUsed, err := k.wasmVM.Migrate(newCodeInfo.CodeHash, env, msg, &prefixStore, k.pluskeeper.CosmwasmAPI(ctx), &querier, k.gasMeter(ctx), gas, costJSONDeserialization)
k.consumeRuntimeGas(ctx, gasUsed)
if err != nil {
return nil, sdkerrors.Wrap(types.ErrMigrationFailed, err.Error())
Expand Down Expand Up @@ -531,7 +534,7 @@ func (k Keeper) Sudo(ctx sdk.Context, contractAddress sdk.AccAddress, msg []byte
// prepare querier
querier := k.newQueryHandler(ctx, contractAddress)
gas := k.runtimeGasForContract(ctx)
res, gasUsed, execErr := k.wasmVM.Sudo(codeInfo.CodeHash, env, msg, prefixStore, cosmwasmAPI, querier, k.gasMeter(ctx), gas, costJSONDeserialization)
res, gasUsed, execErr := k.wasmVM.Sudo(codeInfo.CodeHash, env, msg, prefixStore, k.pluskeeper.CosmwasmAPI(ctx), querier, k.gasMeter(ctx), gas, costJSONDeserialization)
k.consumeRuntimeGas(ctx, gasUsed)
if execErr != nil {
return nil, sdkerrors.Wrap(types.ErrExecuteFailed, execErr.Error())
Expand Down Expand Up @@ -567,7 +570,7 @@ func (k Keeper) reply(ctx sdk.Context, contractAddress sdk.AccAddress, reply was
querier := k.newQueryHandler(ctx, contractAddress)
gas := k.runtimeGasForContract(ctx)

res, gasUsed, execErr := k.wasmVM.Reply(codeInfo.CodeHash, env, reply, prefixStore, cosmwasmAPI, querier, k.gasMeter(ctx), gas, costJSONDeserialization)
res, gasUsed, execErr := k.wasmVM.Reply(codeInfo.CodeHash, env, reply, prefixStore, k.pluskeeper.CosmwasmAPI(ctx), querier, k.gasMeter(ctx), gas, costJSONDeserialization)
k.consumeRuntimeGas(ctx, gasUsed)
if execErr != nil {
return nil, sdkerrors.Wrap(types.ErrExecuteFailed, execErr.Error())
Expand Down Expand Up @@ -694,7 +697,7 @@ func (k Keeper) QuerySmart(ctx sdk.Context, contractAddr sdk.AccAddress, req []b
querier := k.newQueryHandler(ctx, contractAddr)

env := types.NewEnv(ctx, contractAddr)
queryResult, gasUsed, qErr := k.wasmVM.Query(codeInfo.CodeHash, env, req, prefixStore, cosmwasmAPI, querier, k.gasMeter(ctx), k.runtimeGasForContract(ctx), costJSONDeserialization)
queryResult, gasUsed, qErr := k.wasmVM.Query(codeInfo.CodeHash, env, req, prefixStore, k.pluskeeper.CosmwasmAPI(ctx), querier, k.gasMeter(ctx), k.runtimeGasForContract(ctx), costJSONDeserialization)
k.consumeRuntimeGas(ctx, gasUsed)
if qErr != nil {
return nil, sdkerrors.Wrap(types.ErrQueryFailed, qErr.Error())
Expand Down
2 changes: 1 addition & 1 deletion x/wasm/keeper/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestConstructorOptions(t *testing.T) {
}
for name, spec := range specs {
t.Run(name, func(t *testing.T) {
k := NewKeeper(nil, nil, paramtypes.NewSubspace(nil, nil, nil, nil, ""), authkeeper.AccountKeeper{}, bankpluskeeper.BaseKeeper{}, stakingkeeper.Keeper{}, distributionkeeper.Keeper{}, nil, nil, nil, nil, nil, nil, "tempDir", types.DefaultWasmConfig(), AvailableCapabilities, spec.srcOpt)
k := NewKeeper(nil, nil, paramtypes.NewSubspace(nil, nil, nil, nil, ""), authkeeper.AccountKeeper{}, bankpluskeeper.BaseKeeper{}, stakingkeeper.Keeper{}, distributionkeeper.Keeper{}, nil, nil, nil, nil, nil, nil, "tempDir", types.DefaultWasmConfig(), AvailableCapabilities, &mockpluskeeper{}, spec.srcOpt)
spec.verify(t, k)
})
}
Expand Down
12 changes: 6 additions & 6 deletions x/wasm/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (k Keeper) OnOpenChannel(
querier := k.newQueryHandler(ctx, contractAddr)

gas := k.runtimeGasForContract(ctx)
res, gasUsed, execErr := k.wasmVM.IBCChannelOpen(codeInfo.CodeHash, env, msg, prefixStore, cosmwasmAPI, querier, ctx.GasMeter(), gas, costJSONDeserialization)
res, gasUsed, execErr := k.wasmVM.IBCChannelOpen(codeInfo.CodeHash, env, msg, prefixStore, k.pluskeeper.CosmwasmAPI(ctx), querier, ctx.GasMeter(), gas, costJSONDeserialization)
k.consumeRuntimeGas(ctx, gasUsed)
if execErr != nil {
return "", sdkerrors.Wrap(types.ErrExecuteFailed, execErr.Error())
Expand Down Expand Up @@ -70,7 +70,7 @@ func (k Keeper) OnConnectChannel(
querier := k.newQueryHandler(ctx, contractAddr)

gas := k.runtimeGasForContract(ctx)
res, gasUsed, execErr := k.wasmVM.IBCChannelConnect(codeInfo.CodeHash, env, msg, prefixStore, cosmwasmAPI, querier, ctx.GasMeter(), gas, costJSONDeserialization)
res, gasUsed, execErr := k.wasmVM.IBCChannelConnect(codeInfo.CodeHash, env, msg, prefixStore, k.pluskeeper.CosmwasmAPI(ctx), querier, ctx.GasMeter(), gas, costJSONDeserialization)
k.consumeRuntimeGas(ctx, gasUsed)

if execErr != nil {
Expand Down Expand Up @@ -102,7 +102,7 @@ func (k Keeper) OnCloseChannel(
querier := k.newQueryHandler(ctx, contractAddr)

gas := k.runtimeGasForContract(ctx)
res, gasUsed, execErr := k.wasmVM.IBCChannelClose(codeInfo.CodeHash, params, msg, prefixStore, cosmwasmAPI, querier, ctx.GasMeter(), gas, costJSONDeserialization)
res, gasUsed, execErr := k.wasmVM.IBCChannelClose(codeInfo.CodeHash, params, msg, prefixStore, k.pluskeeper.CosmwasmAPI(ctx), querier, ctx.GasMeter(), gas, costJSONDeserialization)
k.consumeRuntimeGas(ctx, gasUsed)

if execErr != nil {
Expand Down Expand Up @@ -133,7 +133,7 @@ func (k Keeper) OnRecvPacket(
querier := k.newQueryHandler(ctx, contractAddr)

gas := k.runtimeGasForContract(ctx)
res, gasUsed, execErr := k.wasmVM.IBCPacketReceive(codeInfo.CodeHash, env, msg, prefixStore, cosmwasmAPI, querier, ctx.GasMeter(), gas, costJSONDeserialization)
res, gasUsed, execErr := k.wasmVM.IBCPacketReceive(codeInfo.CodeHash, env, msg, prefixStore, k.pluskeeper.CosmwasmAPI(ctx), querier, ctx.GasMeter(), gas, costJSONDeserialization)
k.consumeRuntimeGas(ctx, gasUsed)

if execErr != nil {
Expand Down Expand Up @@ -168,7 +168,7 @@ func (k Keeper) OnAckPacket(
querier := k.newQueryHandler(ctx, contractAddr)

gas := k.runtimeGasForContract(ctx)
res, gasUsed, execErr := k.wasmVM.IBCPacketAck(codeInfo.CodeHash, env, msg, prefixStore, cosmwasmAPI, querier, ctx.GasMeter(), gas, costJSONDeserialization)
res, gasUsed, execErr := k.wasmVM.IBCPacketAck(codeInfo.CodeHash, env, msg, prefixStore, k.pluskeeper.CosmwasmAPI(ctx), querier, ctx.GasMeter(), gas, costJSONDeserialization)
k.consumeRuntimeGas(ctx, gasUsed)

if execErr != nil {
Expand Down Expand Up @@ -196,7 +196,7 @@ func (k Keeper) OnTimeoutPacket(
querier := k.newQueryHandler(ctx, contractAddr)

gas := k.runtimeGasForContract(ctx)
res, gasUsed, execErr := k.wasmVM.IBCPacketTimeout(codeInfo.CodeHash, env, msg, prefixStore, cosmwasmAPI, querier, ctx.GasMeter(), gas, costJSONDeserialization)
res, gasUsed, execErr := k.wasmVM.IBCPacketTimeout(codeInfo.CodeHash, env, msg, prefixStore, k.pluskeeper.CosmwasmAPI(ctx), querier, ctx.GasMeter(), gas, costJSONDeserialization)
k.consumeRuntimeGas(ctx, gasUsed)

if execErr != nil {
Expand Down
17 changes: 17 additions & 0 deletions x/wasm/keeper/test_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import (
"github.com/line/ostracon/crypto/ed25519"
"github.com/line/ostracon/libs/log"
"github.com/line/ostracon/libs/rand"
wasmvm "github.com/line/wasmvm"

wasmappparams "github.com/line/wasmd/app/params"
"github.com/line/wasmd/x/wasm/keeper/wasmtesting"
Expand Down Expand Up @@ -198,6 +199,21 @@ func CreateTestInput(t testing.TB, isCheckTx bool, availableCapabilities string,
return createTestInput(t, isCheckTx, availableCapabilities, types.DefaultWasmConfig(), dbm.NewMemDB(), opts...)
}

type mockpluskeeper struct{}

func (m *mockpluskeeper) CosmwasmAPI(ctx sdk.Context) wasmvm.GoAPI {
return wasmvm.GoAPI{
HumanAddress: humanAddress,
CanonicalAddress: canonicalAddress,
CallCallablePoint: func(s string, b1, b2 []byte, b3 bool, b4 []byte, u uint64) ([]byte, uint64, error) {
return nil, 0, fmt.Errorf("mock does not implement CallCallablePoint")
},
ValidateInterface: func(s string, b []byte) ([]byte, uint64, error) {
return nil, 0, fmt.Errorf("mock does not implement ValidateInterface")
},
}
}

// 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,
Expand Down Expand Up @@ -392,6 +408,7 @@ func createTestInput(
tempDir,
wasmConfig,
availableCapabilities,
&mockpluskeeper{},
opts...,
)
keeper.SetParams(ctx, types.DefaultParams())
Expand Down
1 change: 1 addition & 0 deletions x/wasmplus/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func NewKeeper(
homeDir,
wasmConfig,
availableCapabilities,
&result,
opts...,
)
return result
Expand Down

0 comments on commit 6b4897f

Please sign in to comment.