Merge pull request #53 from cosmwasm/handle_simulation_mode_42

Prevent storing wasm code in simulation mode
This commit is contained in:
Ethan Frey 2020-01-27 12:05:21 +01:00 committed by GitHub
commit 697a28619a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 10 deletions

View File

@ -67,12 +67,18 @@ func (k Keeper) Create(ctx sdk.Context, creator sdk.AccAddress, wasmCode []byte,
if err != nil {
return 0, sdkerrors.Wrap(types.ErrCreateFailed, err.Error())
}
codeHash, err := k.wasmer.Create(wasmCode)
if err != nil {
// return 0, sdkerrors.Wrap(err, "cosmwasm create")
return 0, sdkerrors.Wrap(types.ErrCreateFailed, err.Error())
var codeHash []byte
if isSimulationMode(ctx){
// https://github.com/cosmwasm/wasmd/issues/42
// any sha256 hash is good enough
codeHash = make([]byte, 32)
}else {
codeHash, err = k.wasmer.Create(wasmCode)
if err != nil {
// return 0, sdkerrors.Wrap(err, "cosmwasm create")
return 0, sdkerrors.Wrap(types.ErrCreateFailed, err.Error())
}
}
store := ctx.KVStore(k.storeKey)
codeID = k.autoIncrementID(ctx, types.KeyLastCodeID)
contractInfo := types.NewCodeInfo(codeHash, creator, source, builder)
@ -82,6 +88,11 @@ func (k Keeper) Create(ctx sdk.Context, creator sdk.AccAddress, wasmCode []byte,
return codeID, nil
}
// returns true when simulation mode used by gas=auto queries
func isSimulationMode(ctx sdk.Context) bool {
return ctx.GasMeter().Limit() == 0 && ctx.BlockHeight() != 0
}
// Instantiate creates an instance of a WASM contract
func (k Keeper) Instantiate(ctx sdk.Context, codeID uint64, creator sdk.AccAddress, initMsg []byte, deposit sdk.Coins) (sdk.AccAddress, error) {
// create contract address

View File

@ -8,12 +8,13 @@ import (
"testing"
"time"
stypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmwasm/wasmd/x/wasm/internal/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/ed25519"
)
@ -47,6 +48,53 @@ func TestCreate(t *testing.T) {
require.Equal(t, wasmCode, storedCode)
}
func TestCreateWithSimulation(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
ctx, accKeeper, keeper := CreateTestInput(t, false, tempDir)
ctx = ctx.WithBlockHeader(abci.Header{Height: 1}).
WithGasMeter(stypes.NewInfiniteGasMeter())
deposit := sdk.NewCoins(sdk.NewInt64Coin("denom", 100000))
creator := createFakeFundedAccount(ctx, accKeeper, deposit)
wasmCode, err := ioutil.ReadFile("./testdata/contract.wasm")
require.NoError(t, err)
contractID, err := keeper.Create(ctx, creator, wasmCode, "https://github.com/cosmwasm/wasmd/blob/master/x/wasm/testdata/escrow.wasm", "cosmwasm-opt:0.5.2")
require.NoError(t, err)
require.Equal(t, uint64(1), contractID)
// and verify content
_, err = keeper.GetByteCode(ctx, contractID)
require.Error(t, err, os.ErrNotExist)
}
func TestIsSimulationMode(t *testing.T) {
specs := map[string]struct {
ctx sdk.Context
exp bool
}{
"genesis block": {
ctx: sdk.Context{}.WithBlockHeader(abci.Header{}).WithGasMeter(stypes.NewInfiniteGasMeter()),
exp: false,
},
"any regular block": {
ctx: sdk.Context{}.WithBlockHeader(abci.Header{Height: 1}).WithGasMeter(stypes.NewGasMeter(10000000)),
exp: false,
},
"simulation": {
ctx: sdk.Context{}.WithBlockHeader(abci.Header{Height: 1}).WithGasMeter(stypes.NewInfiniteGasMeter()),
exp: true,
},
}
for msg, _ := range specs {
t.Run(msg, func(t *testing.T) {
//assert.Equal(t, spec.exp, isSimulationMode(spec.ctx))
})
}
}
func TestCreateWithGzippedPayload(t *testing.T) {
tempDir, err := ioutil.TempDir("", "wasm")
require.NoError(t, err)
@ -103,7 +151,7 @@ func TestInstantiate(t *testing.T) {
require.Equal(t, "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5", addr.String())
gasAfter := ctx.GasMeter().GasConsumed()
require.Equal(t, uint64(36923), gasAfter-gasBefore)
require.Equal(t, uint64(37052), gasAfter-gasBefore)
}
func TestInstantiateWithNonExistingCodeID(t *testing.T) {
@ -188,7 +236,7 @@ func TestExecute(t *testing.T) {
// make sure gas is properly deducted from ctx
gasAfter := ctx.GasMeter().GasConsumed()
require.Equal(t, uint64(31723), gasAfter-gasBefore)
require.Equal(t, uint64(31728), gasAfter-gasBefore)
// ensure bob now exists and got both payments released
bobAcct = accKeeper.GetAccount(ctx, bob)