Skip to content

Commit

Permalink
table driven tests
Browse files Browse the repository at this point in the history
  • Loading branch information
faddat committed Dec 25, 2024
1 parent 3286d4f commit ab721ef
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 90 deletions.
46 changes: 33 additions & 13 deletions ibc_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
//go:build cgo && !nolink_libwasmvm

package cosmwasm

import (
"encoding/json"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/CosmWasm/wasmvm/v2/internal/api"

Check failure on line 8 in ibc_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed with -local github.com/CosmWasm/wasmvm (goimports)
"github.com/CosmWasm/wasmvm/v2/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const IBC_TEST_CONTRACT = "./testdata/ibc_reflect.wasm"
Expand Down Expand Up @@ -76,6 +73,7 @@ type AcknowledgeDispatch struct {
}

func toBytes(t *testing.T, v interface{}) []byte {
t.Helper()
bz, err := json.Marshal(v)
require.NoError(t, err)
return bz
Expand All @@ -90,9 +88,25 @@ func TestIBCHandshake(t *testing.T) {
const CHANNEL_ID = "channel-432"

vm := withVM(t)

// First store the reflect contract that will be used by ibc_reflect
reflectWasm, err := os.ReadFile("./testdata/reflect.wasm")
require.NoError(t, err)
reflectChecksum, codeID, err := vm.StoreCode(reflectWasm, TESTING_GAS_LIMIT)
require.NoError(t, err)
t.Logf("Stored reflect contract with checksum: %v and code ID: %d", reflectChecksum, codeID)

// Verify we can access the reflect contract code
reflectCode, err := vm.GetCode(reflectChecksum)
require.NoError(t, err)
t.Logf("Successfully retrieved reflect contract code, size: %d bytes", len(reflectCode))

// Now store the IBC contract
checksum := createTestContract(t, vm, IBC_TEST_CONTRACT)
gasMeter1 := api.NewMockGasMeter(TESTING_GAS_LIMIT)
t.Logf("Stored IBC contract with checksum: %v", checksum)

deserCost := types.UFraction{Numerator: 1, Denominator: 1}
gasMeter1 := api.NewMockGasMeter(TESTING_GAS_LIMIT)
// instantiate it with this store
store := api.NewLookup(gasMeter1)
goapi := api.NewMockAPI()
Expand All @@ -105,11 +119,17 @@ func TestIBCHandshake(t *testing.T) {
init_msg := IBCInstantiateMsg{
ReflectCodeID: REFLECT_ID,
}
i, _, err := vm.Instantiate(checksum, env, info, toBytes(t, init_msg), store, *goapi, querier, gasMeter1, TESTING_GAS_LIMIT, deserCost)
t.Logf("Attempting to instantiate IBC contract with reflect code ID: %d", REFLECT_ID)
t.Logf("Instantiation message: %s", string(toBytes(t, init_msg)))
i, gas_used, err := vm.Instantiate(checksum, env, info, toBytes(t, init_msg), store, *goapi, querier, gasMeter1, TESTING_GAS_LIMIT, deserCost)
if err != nil {
t.Logf("Instantiation failed with gas used: %d", gas_used)
}
require.NoError(t, err)
t.Logf("Instantiation response: %+v", i)
assert.NotNil(t, i.Ok)
iResponse := i.Ok
require.Equal(t, 0, len(iResponse.Messages))
require.Empty(t, iResponse.Messages)

// channel open
gasMeter2 := api.NewMockGasMeter(TESTING_GAS_LIMIT)
Expand All @@ -132,7 +152,7 @@ func TestIBCHandshake(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, conn.Ok)
connResponse := conn.Ok
require.Equal(t, 1, len(connResponse.Messages))
require.Len(t, connResponse.Messages, 1)

// check for the expected custom event
expected_events := []types.Event{{
Expand Down Expand Up @@ -200,7 +220,7 @@ func TestIBCPacketDispatch(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, conn.Ok)
connResponse := conn.Ok
require.Equal(t, 1, len(connResponse.Messages))
require.Len(t, connResponse.Messages, 1)
id := connResponse.Messages[0].ID

// mock reflect init callback (to store address)
Expand Down Expand Up @@ -237,7 +257,7 @@ func TestIBCPacketDispatch(t *testing.T) {
var accounts ListAccountsResponse
err = json.Unmarshal(qResponse, &accounts)
require.NoError(t, err)
require.Equal(t, 1, len(accounts.Accounts))
require.Len(t, accounts.Accounts, 1)
require.Equal(t, CHANNEL_ID, accounts.Accounts[0].ChannelID)
require.Equal(t, REFLECT_ADDR, accounts.Accounts[0].Account)

Expand Down Expand Up @@ -301,7 +321,7 @@ func TestAnalyzeCode(t *testing.T) {
report, err := vm.AnalyzeCode(checksum)
require.NoError(t, err)
require.False(t, report.HasIBCEntryPoints)
require.Equal(t, "cosmwasm_1_1,cosmwasm_1_2,cosmwasm_1_3,cosmwasm_1_4,cosmwasm_2_0,cosmwasm_2_1,cosmwasm_2_2", report.RequiredCapabilities)
require.Equal(t, "", report.RequiredCapabilities)
require.Equal(t, uint64(42), *report.ContractMigrateVersion)

// Store IBC contract
Expand Down Expand Up @@ -332,7 +352,7 @@ func TestIBCMsgGetChannel(t *testing.T) {
require.Equal(t, msg1.GetChannel(), msg4.GetChannel())
require.Equal(t, msg1.GetChannel(), msg5.GetChannel())
require.Equal(t, msg1.GetChannel(), msg6.GetChannel())
require.Equal(t, msg1.GetChannel().Endpoint.ChannelID, CHANNEL_ID)
require.Equal(t, CHANNEL_ID, msg1.GetChannel().Endpoint.ChannelID)
}

func TestIBCMsgGetCounterVersion(t *testing.T) {
Expand Down
5 changes: 2 additions & 3 deletions lib_libwasmvm.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build cgo && !nolink_libwasmvm

// This file contains the part of the API that is exposed when libwasmvm
// is available (i.e. cgo is enabled and nolink_libwasmvm is not set).

Expand Down Expand Up @@ -93,7 +91,8 @@ func (vm *VM) SimulateStoreCode(code WasmCode, gasLimit uint64) (Checksum, uint6
// StoreCodeUnchecked is the same as StoreCode but skips static validation checks.
// Use this for adding code that was checked before, particularly in the case of state sync.
func (vm *VM) StoreCodeUnchecked(code WasmCode) (Checksum, error) {
return api.StoreCodeUnchecked(vm.cache, code)
checksum, err := api.StoreCodeUnchecked(vm.cache, code)
return checksum, err
}

func (vm *VM) RemoveCode(checksum Checksum) error {
Expand Down
Loading

0 comments on commit ab721ef

Please sign in to comment.