From 38707cb2b63b2ae1ad27eb27c646f16b5687c2fe Mon Sep 17 00:00:00 2001 From: Alex Peters Date: Thu, 7 Jan 2021 11:58:22 +0100 Subject: [PATCH 1/3] Upgrade wasmvm to v0.13.0 --- go.mod | 2 +- go.sum | 4 ++-- x/wasm/internal/keeper/keeper.go | 6 +++++- x/wasm/internal/keeper/keeper_test.go | 6 +++--- x/wasm/internal/keeper/recurse_test.go | 16 ++++++++-------- 5 files changed, 19 insertions(+), 15 deletions(-) diff --git a/go.mod b/go.mod index cd3ba3f61d..88c70fb7c6 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/CosmWasm/wasmd go 1.15 require ( - github.com/CosmWasm/wasmvm v0.12.0 + github.com/CosmWasm/wasmvm v0.13.0 github.com/cosmos/cosmos-sdk v0.40.0-rc6 github.com/cosmos/iavl v0.15.3 github.com/dvsekhvalnov/jose2go v0.0.0-20200901110807-248326c1351b diff --git a/go.sum b/go.sum index f14371c893..ef5115077a 100644 --- a/go.sum +++ b/go.sum @@ -18,8 +18,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03 github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/ChainSafe/go-schnorrkel v0.0.0-20200405005733-88cbf1b4c40d h1:nalkkPQcITbvhmL4+C4cKA87NW0tfm3Kl9VXRoPywFg= github.com/ChainSafe/go-schnorrkel v0.0.0-20200405005733-88cbf1b4c40d/go.mod h1:URdX5+vg25ts3aCh8H5IFZybJYKWhJHYMTnf+ULtoC4= -github.com/CosmWasm/wasmvm v0.12.0 h1:L9ez6fCg2Co1SgCm0YHjbnewZ8myemu2cm/QL0qR1OE= -github.com/CosmWasm/wasmvm v0.12.0/go.mod h1:tbXGE9Jz6sYpiJroGr71OQ5TFOufq/P5LWsruA2u6JE= +github.com/CosmWasm/wasmvm v0.13.0 h1:AP8LVcCxI3KLtHY24qLhzVe+ChOXwcR8MNm8Jxkuhwk= +github.com/CosmWasm/wasmvm v0.13.0/go.mod h1:Id107qllDJyJjVQQsKMOy2YYF98sqPJ2t+jX1QES40A= github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ= github.com/DataDog/zstd v1.4.1/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo= github.com/DataDog/zstd v1.4.5 h1:EndNeuB0l9syBZhut0wns3gV1hL8zX8LIu6ZiVHWLIQ= diff --git a/x/wasm/internal/keeper/keeper.go b/x/wasm/internal/keeper/keeper.go index aa7ce10758..40e9c19a22 100644 --- a/x/wasm/internal/keeper/keeper.go +++ b/x/wasm/internal/keeper/keeper.go @@ -42,6 +42,10 @@ const InstanceCost uint64 = 40_000 // CompileCost is how much SDK gas we charge *per byte* for compiling WASM code. const CompileCost uint64 = 2 +// contractMemoryLimit is the memory limit of each contract execution (in MiB) +// constant value so all nodes run with the same limit. +const contractMemoryLimit = 32 + // Keeper will have a reference to Wasmer with it's own data directory. type Keeper struct { storeKey sdk.StoreKey @@ -75,7 +79,7 @@ func NewKeeper( customEncoders *MessageEncoders, customPlugins *QueryPlugins, ) Keeper { - wasmer, err := wasmvm.NewVM(filepath.Join(homeDir, "wasm"), supportedFeatures, wasmConfig.ContractDebugMode, wasmConfig.MemoryCacheSize) + wasmer, err := wasmvm.NewVM(filepath.Join(homeDir, "wasm"), supportedFeatures, contractMemoryLimit, wasmConfig.ContractDebugMode, wasmConfig.MemoryCacheSize) if err != nil { panic(err) } diff --git a/x/wasm/internal/keeper/keeper_test.go b/x/wasm/internal/keeper/keeper_test.go index 51b83ebac4..f5a6a9f3f4 100644 --- a/x/wasm/internal/keeper/keeper_test.go +++ b/x/wasm/internal/keeper/keeper_test.go @@ -278,7 +278,7 @@ func TestInstantiate(t *testing.T) { require.Equal(t, "cosmos18vd8fpwxzck93qlwghaj6arh4p7c5n89uzcee5", contractAddr.String()) gasAfter := ctx.GasMeter().GasConsumed() - require.Equal(t, uint64(0x11974), gasAfter-gasBefore) + require.Equal(t, uint64(0x118c2), gasAfter-gasBefore) // ensure it is stored properly info := keeper.GetContractInfo(ctx, contractAddr) @@ -507,7 +507,7 @@ func TestExecute(t *testing.T) { // make sure gas is properly deducted from ctx gasAfter := ctx.GasMeter().GasConsumed() - require.Equal(t, uint64(0x11ec9), gasAfter-gasBefore) + require.Equal(t, uint64(0x11d8c), gasAfter-gasBefore) // ensure bob now exists and got both payments released bobAcct = accKeeper.GetAccount(ctx, bob) @@ -639,7 +639,7 @@ func TestExecuteWithPanic(t *testing.T) { _, err = keeper.Execute(ctx, addr, fred, []byte(`{"panic":{}}`), topUp) require.Error(t, err) require.True(t, errors.Is(err, types.ErrExecuteFailed)) - require.Equal(t, "Out of gas: execute wasm contract failed", err.Error()) + require.Equal(t, "Error calling the VM: Error executing Wasm: Wasmer runtime error: RuntimeError: unreachable: execute wasm contract failed", err.Error()) } func TestExecuteWithCpuLoop(t *testing.T) { diff --git a/x/wasm/internal/keeper/recurse_test.go b/x/wasm/internal/keeper/recurse_test.go index 4657938145..35f993591a 100644 --- a/x/wasm/internal/keeper/recurse_test.go +++ b/x/wasm/internal/keeper/recurse_test.go @@ -56,12 +56,12 @@ func initRecurseContract(t *testing.T) (contract sdk.AccAddress, creator sdk.Acc func TestGasCostOnQuery(t *testing.T) { const ( - GasNoWork uint64 = 43229 + GasNoWork uint64 = 43091 // Note: about 100 SDK gas (10k wasmer gas) for each round of sha256 - GasWork50 uint64 = 48937 // this is a little shy of 50k gas - to keep an eye on the limit + GasWork50 uint64 = 48752 // this is a little shy of 50k gas - to keep an eye on the limit - GasReturnUnhashed uint64 = 393 - GasReturnHashed uint64 = 342 + GasReturnUnhashed uint64 = 295 + GasReturnHashed uint64 = 264 ) cases := map[string]struct { @@ -222,9 +222,9 @@ func TestLimitRecursiveQueryGas(t *testing.T) { const ( // Note: about 100 SDK gas (10k wasmer gas) for each round of sha256 - GasWork2k uint64 = 273_851 // = InstanceCost + x // we have 6x gas used in cpu than in the instance + GasWork2k uint64 = 271_454 // = InstanceCost + x // we have 6x gas used in cpu than in the instance // This is overhead for calling into a sub-contract - GasReturnHashed uint64 = 349 + GasReturnHashed uint64 = 268 ) cases := map[string]struct { @@ -250,8 +250,8 @@ func TestLimitRecursiveQueryGas(t *testing.T) { Work: 2000, }, expectQueriesFromContract: 5, - // FIXME: why -2... confused a bit by calculations, seems like rounding issues - expectedGas: GasWork2k + 5*(GasWork2k+GasReturnHashed) - 2, + // FIXME: why -4... confused a bit by calculations, seems like rounding issues + expectedGas: GasWork2k + 5*(GasWork2k+GasReturnHashed) - 4, }, // this is where we expect an error... // it has enough gas to run 4 times and die on the 5th (4th time dispatching to sub-contract) From d790dc9dbd32858740c4581dddc9250cf4e1e5a3 Mon Sep 17 00:00:00 2001 From: Alex Peters Date: Thu, 7 Jan 2021 12:05:34 +0100 Subject: [PATCH 2/3] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d87beffd6c..43413678ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ **Features:** +- Upgrade to WasmVM v0.13.0 [\#358](https://github.com/CosmWasm/wasmd/pull/358) - Upgrade to cosmos-sdk v0.40.0-rc6 [\#354](https://github.com/CosmWasm/wasmd/pull/354) - Upgrade to cosmos-sdk v0.40.0-rc5 [\#344](https://github.com/CosmWasm/wasmd/issues/344) - Add Dependabot to keep dependencies secure and up-to-date [\#336](https://github.com/CosmWasm/wasmd/issues/336) From 70f35d89b97b875eb4060008feb89dc4fa819b7a Mon Sep 17 00:00:00 2001 From: Alex Peters Date: Thu, 7 Jan 2021 12:28:45 +0100 Subject: [PATCH 3/3] Fix test condition to work on Linux --- x/wasm/internal/keeper/keeper_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/x/wasm/internal/keeper/keeper_test.go b/x/wasm/internal/keeper/keeper_test.go index f5a6a9f3f4..5704199dba 100644 --- a/x/wasm/internal/keeper/keeper_test.go +++ b/x/wasm/internal/keeper/keeper_test.go @@ -639,7 +639,8 @@ func TestExecuteWithPanic(t *testing.T) { _, err = keeper.Execute(ctx, addr, fred, []byte(`{"panic":{}}`), topUp) require.Error(t, err) require.True(t, errors.Is(err, types.ErrExecuteFailed)) - require.Equal(t, "Error calling the VM: Error executing Wasm: Wasmer runtime error: RuntimeError: unreachable: execute wasm contract failed", err.Error()) + // test with contains as "Display" implementation of the Wasmer "RuntimeError" is different for Mac and Linux + assert.Contains(t, err.Error(), "Error calling the VM: Error executing Wasm: Wasmer runtime error: RuntimeError: unreachable") } func TestExecuteWithCpuLoop(t *testing.T) {