Skip to content

Commit

Permalink
Merge pull request #345 from CosmWasm/test-env
Browse files Browse the repository at this point in the history
Fix JSON serialization of Transaction in  Env
  • Loading branch information
webmaster128 authored Aug 31, 2022
2 parents d014052 + 86ea1d9 commit 025da40
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 11 deletions.
18 changes: 9 additions & 9 deletions internal/api/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func TestInstantiate(t *testing.T) {
res, cost, err := Instantiate(cache, checksum, env, info, msg, &igasMeter, store, api, &querier, TESTING_GAS_LIMIT, TESTING_PRINT_DEBUG)
require.NoError(t, err)
requireOkResponse(t, res, 0)
assert.Equal(t, uint64(0x12e722d7c), cost)
assert.Equal(t, uint64(0x1432036ec), cost)

var result types.ContractResult
err = json.Unmarshal(res, &result)
Expand Down Expand Up @@ -343,7 +343,7 @@ func TestExecute(t *testing.T) {
diff := time.Now().Sub(start)
require.NoError(t, err)
requireOkResponse(t, res, 0)
assert.Equal(t, uint64(0x12e722d7c), cost)
assert.Equal(t, uint64(0x1432036ec), cost)
t.Logf("Time (%d gas): %s\n", cost, diff)

// execute with the same store
Expand All @@ -356,7 +356,7 @@ func TestExecute(t *testing.T) {
res, cost, err = Execute(cache, checksum, env, info, []byte(`{"release":{}}`), &igasMeter2, store, api, &querier, TESTING_GAS_LIMIT, TESTING_PRINT_DEBUG)
diff = time.Now().Sub(start)
require.NoError(t, err)
assert.Equal(t, uint64(0x21f7a66d0), cost)
assert.Equal(t, uint64(0x2335827f0), cost)
t.Logf("Time (%d gas): %s\n", cost, diff)

// make sure it read the balance properly and we got 250 atoms
Expand Down Expand Up @@ -407,7 +407,7 @@ func TestExecuteCpuLoop(t *testing.T) {
diff := time.Now().Sub(start)
require.NoError(t, err)
requireOkResponse(t, res, 0)
assert.Equal(t, uint64(0x12e722d7c), cost)
assert.Equal(t, uint64(0x1432036ec), cost)
t.Logf("Time (%d gas): %s\n", cost, diff)

// execute a cpu loop
Expand Down Expand Up @@ -558,7 +558,7 @@ func TestMultipleInstances(t *testing.T) {
require.NoError(t, err)
requireOkResponse(t, res, 0)
// we now count wasm gas charges and db writes
assert.Equal(t, uint64(0x12c4f266c), cost)
assert.Equal(t, uint64(0x140fd2fdc), cost)

// instance2 controlled by mary
gasMeter2 := NewMockGasMeter(TESTING_GAS_LIMIT)
Expand All @@ -569,14 +569,14 @@ func TestMultipleInstances(t *testing.T) {
res, cost, err = Instantiate(cache, checksum, env, info, msg, &igasMeter2, store2, api, &querier, TESTING_GAS_LIMIT, TESTING_PRINT_DEBUG)
require.NoError(t, err)
requireOkResponse(t, res, 0)
assert.Equal(t, uint64(0x12d8b01cc), cost)
assert.Equal(t, uint64(0x142390b3c), cost)

// fail to execute store1 with mary
resp := exec(t, cache, checksum, "mary", store1, api, querier, 0x115070970)
resp := exec(t, cache, checksum, "mary", store1, api, querier, 0x129b512e0)
require.Equal(t, "Unauthorized", resp.Err)

// succeed to execute store1 with fred
resp = exec(t, cache, checksum, "fred", store1, api, querier, 0x21e7a0dd0)
resp = exec(t, cache, checksum, "fred", store1, api, querier, 0x23257cef0)
require.Equal(t, "", resp.Err)
require.Equal(t, 1, len(resp.Ok.Messages))
attributes := resp.Ok.Attributes
Expand All @@ -585,7 +585,7 @@ func TestMultipleInstances(t *testing.T) {
require.Equal(t, "bob", attributes[1].Value)

// succeed to execute store2 with mary
resp = exec(t, cache, checksum, "mary", store2, api, querier, 0x21efa3a50)
resp = exec(t, cache, checksum, "mary", store2, api, querier, 0x232d7fb70)
require.Equal(t, "", resp.Err)
require.Equal(t, 1, len(resp.Ok.Messages))
attributes = resp.Ok.Attributes
Expand Down
3 changes: 3 additions & 0 deletions internal/api/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ func MockEnv() types.Env {
Time: 1578939743_987654321,
ChainID: "foobar",
},
Transaction: &types.TransactionInfo{
Index: 4,
},
Contract: types.ContractInfo{
Address: MOCK_CONTRACT_ADDR,
},
Expand Down
62 changes: 62 additions & 0 deletions lib_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cosmwasm

import (
"encoding/json"
"io/ioutil"
"os"
"testing"
Expand All @@ -19,6 +20,7 @@ const (
TESTING_CACHE_SIZE = 100 // MiB
)

const CYBERPUNK_TEST_CONTRACT = "./testdata/cyberpunk.wasm"
const HACKATOM_TEST_CONTRACT = "./testdata/hackatom.wasm"

func withVM(t *testing.T) *VM {
Expand Down Expand Up @@ -97,6 +99,66 @@ func TestHappyPath(t *testing.T) {
assert.Equal(t, expectedData, hres.Data)
}

func TestEnv(t *testing.T) {
vm := withVM(t)
checksum := createTestContract(t, vm, CYBERPUNK_TEST_CONTRACT)

deserCost := types.UFraction{1, 1}
gasMeter1 := api.NewMockGasMeter(TESTING_GAS_LIMIT)
// instantiate it with this store
store := api.NewLookup(gasMeter1)
goapi := api.NewMockAPI()
balance := types.Coins{types.NewCoin(250, "ATOM")}
querier := api.DefaultQuerier(api.MOCK_CONTRACT_ADDR, balance)

// instantiate
env := api.MockEnv()
info := api.MockInfo("creator", nil)
ires, _, err := vm.Instantiate(checksum, env, info, []byte(`{}`), store, *goapi, querier, gasMeter1, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
require.Equal(t, 0, len(ires.Messages))

// Execute mirror env without Transaction
env = types.Env{
Block: types.BlockInfo{
Height: 444,
Time: 1955939743_123456789,
ChainID: "nice-chain",
},
Contract: types.ContractInfo{
Address: "wasm10dyr9899g6t0pelew4nvf4j5c3jcgv0r5d3a5l",
},
Transaction: nil,
}
info = api.MockInfo("creator", nil)
msg := []byte(`{"mirror_env": {}}`)
ires, _, err = vm.Execute(checksum, env, info, msg, store, *goapi, querier, gasMeter1, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
expected, _ := json.Marshal(env)
require.Equal(t, expected, ires.Data)

// Execute mirror env with Transaction
env = types.Env{
Block: types.BlockInfo{
Height: 444,
Time: 1955939743_123456789,
ChainID: "nice-chain",
},
Contract: types.ContractInfo{
Address: "wasm10dyr9899g6t0pelew4nvf4j5c3jcgv0r5d3a5l",
},
Transaction: &types.TransactionInfo{
Index: 18,
},
}
info = api.MockInfo("creator", nil)
msg = []byte(`{"mirror_env": {}}`)
ires, _, err = vm.Execute(checksum, env, info, msg, store, *goapi, querier, gasMeter1, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
expected, _ = json.Marshal(env)
require.Equal(t, expected, ires.Data)
}

func TestGetMetrics(t *testing.T) {
vm := withVM(t)

Expand Down
Binary file added testdata/cyberpunk.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion testdata/download_releases.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fi

tag="$1"

for contract in hackatom queue reflect ibc_reflect; do
for contract in cyberpunk hackatom queue reflect ibc_reflect; do
url="https://github.com/CosmWasm/cosmwasm/releases/download/$tag/$contract.wasm"
echo "Downloading $url ..."
wget -O "$contract.wasm" "$url"
Expand Down
2 changes: 1 addition & 1 deletion types/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ package types
// Env are json encoded to a byte slice before passing to the wasm contract.
type Env struct {
Block BlockInfo `json:"block"`
Transaction *TransactionInfo `json:"transaction"`
Contract ContractInfo `json:"contract"`
Transaction *TransactionInfo `json:"transaction_info,omitempty"`
}

type BlockInfo struct {
Expand Down

0 comments on commit 025da40

Please sign in to comment.