Skip to content

Commit

Permalink
restore libwasmvm to ease reference
Browse files Browse the repository at this point in the history
  • Loading branch information
faddat committed Dec 26, 2024
1 parent 4309b4e commit 9af9725
Show file tree
Hide file tree
Showing 33 changed files with 7,557 additions and 9 deletions.
14 changes: 7 additions & 7 deletions ibc_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build cgo && !nolink_libwasmvm

package cosmwasm

import (
Expand Down Expand Up @@ -74,7 +76,6 @@ type AcknowledgeDispatch struct {
}

func toBytes(t *testing.T, v interface{}) []byte {

Check failure on line 78 in ibc_test.go

View workflow job for this annotation

GitHub Actions / lint

test helper function should start from t.Helper() (thelper)
t.Helper()
bz, err := json.Marshal(v)
require.NoError(t, err)
return bz
Expand Down Expand Up @@ -106,10 +107,9 @@ func TestIBCHandshake(t *testing.T) {
}
i, _, err := vm.Instantiate(checksum, env, info, toBytes(t, init_msg), store, *goapi, querier, gasMeter1, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
t.Logf("Instantiation response: %+v", i)
assert.NotNil(t, i.Ok)
iResponse := i.Ok
require.Empty(t, iResponse.Messages)
require.Equal(t, 0, len(iResponse.Messages))

Check failure on line 112 in ibc_test.go

View workflow job for this annotation

GitHub Actions / lint

empty: use require.Empty (testifylint)

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

Check failure on line 135 in ibc_test.go

View workflow job for this annotation

GitHub Actions / lint

len: use require.Len (testifylint)

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

Check failure on line 203 in ibc_test.go

View workflow job for this annotation

GitHub Actions / lint

len: use require.Len (testifylint)
id := connResponse.Messages[0].ID

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

Check failure on line 240 in ibc_test.go

View workflow job for this annotation

GitHub Actions / lint

len: use require.Len (testifylint)
require.Equal(t, CHANNEL_ID, accounts.Accounts[0].ChannelID)
require.Equal(t, REFLECT_ADDR, accounts.Accounts[0].Account)

Expand Down Expand Up @@ -332,7 +332,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, CHANNEL_ID, msg1.GetChannel().Endpoint.ChannelID)
require.Equal(t, msg1.GetChannel().Endpoint.ChannelID, CHANNEL_ID)

Check failure on line 335 in ibc_test.go

View workflow job for this annotation

GitHub Actions / lint

expected-actual: need to reverse actual and expected values (testifylint)
}

func TestIBCMsgGetCounterVersion(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion internal/api/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ import (

const MOCK_CONTRACT_ADDR = "contract"

// MockEnv returns a mock environment for testing
// this is the original, and should not be changed.
func MockEnv() types.Env {
return types.Env{
Block: types.BlockInfo{
Height: 123,
Time: types.Uint64(1578939743_987654321),
Time: 1578939743_987654321,
ChainID: "foobar",
},
Transaction: &types.TransactionInfo{
Expand Down
6 changes: 6 additions & 0 deletions internal/api/testdb/memdb_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,18 @@ func (i *memDBIterator) Error() error {
// Key implements Iterator.
func (i *memDBIterator) Key() []byte {
i.assertIsValid()
if i.item.key == nil || len(i.item.key) == 0 {

Check failure on line 144 in internal/api/testdb/memdb_iterator.go

View workflow job for this annotation

GitHub Actions / lint

S1009: should omit nil check; len() for []byte is defined as zero (gosimple)
return nil
}
return i.item.key
}

// Value implements Iterator.
func (i *memDBIterator) Value() []byte {
i.assertIsValid()
if i.item.value == nil || len(i.item.value) == 0 {

Check failure on line 153 in internal/api/testdb/memdb_iterator.go

View workflow job for this annotation

GitHub Actions / lint

S1009: should omit nil check; len() for []byte is defined as zero (gosimple)
return nil
}
return i.item.value
}

Expand Down
35 changes: 34 additions & 1 deletion internal/runtime/hostfunctions.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,40 @@ func hostCloseIterator(ctx context.Context, mod api.Module, callID, iterID uint6

// hostAbort implements the abort function required by Wasm modules
func hostAbort(ctx context.Context, mod api.Module, code uint32) {
panic(fmt.Sprintf("Wasm contract aborted with code: %d", code))
// Print debug information about the abort
fmt.Printf("Debug: Wasm contract abort triggered\n")
fmt.Printf("Debug: Abort code: %d (0x%x)\n", code, code)

// Try to get any memory exports to check for error messages
if mem := mod.Memory(); mem != nil {
// Try to read memory around the abort code location
// We'll read a few different ranges to try to catch any error message
ranges := []struct{ start, size uint32 }{
{code - 100, 200}, // Around the code point
{0, 256}, // Start of memory
{code & 0xFFFF, 256}, // Lower 16 bits as offset
}

for _, r := range ranges {
if data, ok := mem.Read(r.start, r.size); ok {
if len(data) > 0 {
// Try to interpret the memory as both string and raw bytes
fmt.Printf("Debug: Memory at offset %d:\n", r.start)
fmt.Printf(" As string: %s\n", string(data))
fmt.Printf(" As bytes: %v\n", data)
}
}
}
}

env := ctx.Value(envKey).(*RuntimeEnvironment)
if env != nil {
fmt.Printf("Debug: Runtime environment state:\n")
fmt.Printf(" Gas used: %d\n", env.GasUsed)
fmt.Printf(" Gas limit: %d\n", env.Gas.GasConsumed())
}

panic(fmt.Sprintf("Wasm contract aborted with code: %d (0x%x)", code, code))
}

// hostDbRead implements db_read
Expand Down
3 changes: 3 additions & 0 deletions libwasmvm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Build results
target/
artifacts/
Loading

0 comments on commit 9af9725

Please sign in to comment.