Skip to content

feat: Add checksum of tx to TransactionInfo #670

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions internal/api/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,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)
require.Equal(t, uint64(0xd35950), cost.UsedInternally)
require.Equal(t, uint64(0xdf0237), cost.UsedInternally)

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

// execute with the same store
Expand All @@ -624,7 +624,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.Since(start)
require.NoError(t, err)
require.Equal(t, uint64(0x16057d3), cost.UsedInternally)
require.Equal(t, uint64(0x16d0d5c), cost.UsedInternally)
t.Logf("Time (%d gas): %s\n", cost.UsedInternally, diff)

// make sure it read the balance properly and we got 250 atoms
Expand Down Expand Up @@ -678,7 +678,7 @@ func TestExecutePanic(t *testing.T) {
store.SetGasMeter(gasMeter2)
info = MockInfoBin(t, "fred")
_, _, err = Execute(cache, checksum, env, info, []byte(`{"panic":{}}`), &igasMeter2, store, api, &querier, maxGas, TESTING_PRINT_DEBUG)
require.ErrorContains(t, err, "RuntimeError: Aborted: panicked at 'This page intentionally faulted'")
require.ErrorContains(t, err, "This page intentionally faulted")
}

func TestExecuteUnreachable(t *testing.T) {
Expand Down Expand Up @@ -731,7 +731,7 @@ func TestExecuteCpuLoop(t *testing.T) {
diff := time.Since(start)
require.NoError(t, err)
requireOkResponse(t, res, 0)
require.Equal(t, uint64(0x895c33), cost.UsedInternally)
require.Equal(t, uint64(0x8bbb63), cost.UsedInternally)
t.Logf("Time (%d gas): %s\n", cost.UsedInternally, diff)

// execute a cpu loop
Expand Down Expand Up @@ -973,7 +973,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(0xd2189c), cost.UsedInternally)
assert.Equal(t, uint64(0xddc183), cost.UsedInternally)

// instance2 controlled by mary
gasMeter2 := NewMockGasMeter(TESTING_GAS_LIMIT)
Expand All @@ -984,14 +984,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(0xd2ce86), cost.UsedInternally)
assert.Equal(t, uint64(0xde776d), cost.UsedInternally)

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

// succeed to execute store1 with fred
resp = exec(t, cache, checksum, "fred", store1, api, querier, 0x15fce67)
resp = exec(t, cache, checksum, "fred", store1, api, querier, 0x16c83f0)
require.Empty(t, resp.Err)
require.Len(t, resp.Ok.Messages, 1)
attributes := resp.Ok.Attributes
Expand All @@ -1000,7 +1000,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, 0x160131d)
resp = exec(t, cache, checksum, "mary", store2, api, querier, 0x16cc8a6)
require.Empty(t, resp.Err)
require.Len(t, resp.Ok.Messages, 1)
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
@@ -1,6 +1,7 @@
package api

import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
Expand All @@ -20,6 +21,7 @@ import (
const MOCK_CONTRACT_ADDR = "contract"

func MockEnv() types.Env {
tx_hash, _ := hex.DecodeString("AABBCCDDEEFF0011AABBCCDDEEFF0011AABBCCDDEEFF0011AABBCCDDEEFF0011")
return types.Env{
Block: types.BlockInfo{
Height: 123,
Expand All @@ -28,6 +30,7 @@ func MockEnv() types.Env {
},
Transaction: &types.TransactionInfo{
Index: 4,
Hash: tx_hash,
},
Contract: types.ContractInfo{
Address: MOCK_CONTRACT_ADDR,
Expand Down
4 changes: 4 additions & 0 deletions lib_libwasmvm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package cosmwasm

import (
"encoding/hex"
"encoding/json"
"fmt"
"math"
Expand Down Expand Up @@ -253,6 +254,8 @@ func TestEnv(t *testing.T) {
expected, _ := json.Marshal(env)
require.Equal(t, expected, ires.Data)

tx_hash, _ := hex.DecodeString("AABBCCDDEEFF0011AABBCCDDEEFF0011AABBCCDDEEFF0011AABBCCDDEEFF0011")

// Execute mirror env with Transaction
env = types.Env{
Block: types.BlockInfo{
Expand All @@ -265,6 +268,7 @@ func TestEnv(t *testing.T) {
},
Transaction: &types.TransactionInfo{
Index: 18,
Hash: tx_hash,
},
}
info = api.MockInfo("creator", nil)
Expand Down
Binary file modified testdata/cyberpunk.wasm
Binary file not shown.
3 changes: 3 additions & 0 deletions types/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ type TransactionInfo struct {
// Along with BlockInfo.Height, this allows you to get a unique
// transaction identifier for the chain for future queries
Index uint32 `json:"index"`

// Hash of the transaction.
Hash []byte `json:"hash,omitempty"`
}

type MessageInfo struct {
Expand Down