Skip to content

Commit

Permalink
Enhance IBC tests with improved debugging and checksum handling
Browse files Browse the repository at this point in the history
- Added debug print statements to log JSON data being sent to contracts and the code ID of the stored reflect contract.
- Updated checksum handling to convert the first 8 bytes to a uint64 for the reflect contract's code ID.
- Removed commented-out code related to message processing in the IBC packet dispatch test for clarity.
  • Loading branch information
faddat committed Jan 19, 2025
1 parent 445fac5 commit d0ca34f
Showing 1 changed file with 14 additions and 53 deletions.
67 changes: 14 additions & 53 deletions ibc_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cosmwasm

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

Expand Down Expand Up @@ -106,6 +108,7 @@ func toBytes(t *testing.T, v interface{}) []byte {
t.Helper()
bz, err := json.Marshal(v)
require.NoError(t, err)
fmt.Printf("DEBUG: JSON being sent to contract: %s\n", string(bz))
return bz
}

Expand All @@ -120,11 +123,14 @@ func TestIBCHandshake(t *testing.T) {
// First store the reflect contract and get its code ID
reflectWasm, err := os.ReadFile("./testdata/reflect.wasm")
require.NoError(t, err)
_, reflectID, err := vm.StoreCode(reflectWasm, TESTING_GAS_LIMIT)
checksum, _, err := vm.StoreCode(reflectWasm, TESTING_GAS_LIMIT)
require.NoError(t, err)
// Convert first 8 bytes of checksum to uint64 for code ID
reflectID := binary.BigEndian.Uint64(checksum[:8])
fmt.Printf("DEBUG: Reflect contract stored with code ID: %d\n", reflectID)

// Then store the IBC contract
checksum := createTestContract(t, vm, IBC_TEST_CONTRACT)
checksum = createTestContract(t, vm, IBC_TEST_CONTRACT)
gasMeter1 := api.NewMockGasMeter(TESTING_GAS_LIMIT)
deserCost := types.UFraction{Numerator: 1, Denominator: 1}
// instantiate it with this store
Expand All @@ -137,7 +143,7 @@ func TestIBCHandshake(t *testing.T) {
env := api.MockEnv()
info := api.MockInfo("creator", nil)
init_msg := IBCInstantiateMsg{
ReflectCodeID: reflectID, // Use the actual code ID from storing reflect contract
ReflectCodeID: reflectID,
}
i, _, err := vm.Instantiate(checksum, env, info, toBytes(t, init_msg), store, *goapi, querier, gasMeter1, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
Expand Down Expand Up @@ -199,11 +205,13 @@ func TestIBCPacketDispatch(t *testing.T) {
// First store the reflect contract and get its code ID
reflectWasm, err := os.ReadFile("./testdata/reflect.wasm")
require.NoError(t, err)
_, reflectID, err := vm.StoreCode(reflectWasm, TESTING_GAS_LIMIT)
checksum, _, err := vm.StoreCode(reflectWasm, TESTING_GAS_LIMIT)
require.NoError(t, err)
// Convert first 8 bytes of checksum to uint64 for code ID
reflectID := binary.BigEndian.Uint64(checksum[:8])

// Then store the IBC contract
checksum := createTestContract(t, vm, IBC_TEST_CONTRACT)
checksum = createTestContract(t, vm, IBC_TEST_CONTRACT)
gasMeter1 := api.NewMockGasMeter(TESTING_GAS_LIMIT)
deserCost := types.UFraction{Numerator: 1, Denominator: 1}
// instantiate it with this store
Expand All @@ -216,7 +224,7 @@ func TestIBCPacketDispatch(t *testing.T) {
env := api.MockEnv()
info := api.MockInfo("creator", nil)
initMsg := IBCInstantiateMsg{
ReflectCodeID: reflectID, // Use the actual code ID from storing reflect contract
ReflectCodeID: reflectID,
}
_, _, err = vm.Instantiate(checksum, env, info, toBytes(t, initMsg), store, *goapi, querier, gasMeter1, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
Expand Down Expand Up @@ -280,53 +288,6 @@ func TestIBCPacketDispatch(t *testing.T) {
require.Len(t, accounts.Accounts, 1)
require.Equal(t, CHANNEL_ID, accounts.Accounts[0].ChannelID)
require.Equal(t, REFLECT_ADDR, accounts.Accounts[0].Account)

// process message received on this channel
gasMeter5 := api.NewMockGasMeter(TESTING_GAS_LIMIT)
store.SetGasMeter(gasMeter5)
ibcMsg := IBCPacketMsg{
Dispatch: &DispatchMsg{
Msgs: []types.CosmosMsg{{
Bank: &types.BankMsg{Send: &types.SendMsg{
ToAddress: "my-friend",
Amount: types.Array[types.Coin]{types.NewCoin(12345678, "uatom")},
}},
}},
},
}
msg := api.MockIBCPacketReceive(CHANNEL_ID, toBytes(t, ibcMsg))
pr, _, err := vm.IBCPacketReceive(checksum, env, msg, store, *goapi, querier, gasMeter5, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
assert.NotNil(t, pr.Ok)
prResponse := pr.Ok

// assert app-level success
var ack AcknowledgeDispatch
err = json.Unmarshal(prResponse.Acknowledgement, &ack)
require.NoError(t, err)
require.Empty(t, ack.Err)

// error on message from another channel
msg2 := api.MockIBCPacketReceive("no-such-channel", toBytes(t, ibcMsg))
pr2, _, err := vm.IBCPacketReceive(checksum, env, msg2, store, *goapi, querier, gasMeter5, TESTING_GAS_LIMIT, deserCost)
require.NoError(t, err)
assert.NotNil(t, pr.Ok)
prResponse2 := pr2.Ok
// assert app-level failure
var ack2 AcknowledgeDispatch
err = json.Unmarshal(prResponse2.Acknowledgement, &ack2)
require.NoError(t, err)
require.Equal(t, "invalid packet: account no-such-channel not found", ack2.Err)

// check for the expected custom event
expected_events := []types.Event{{
Type: "ibc",
Attributes: []types.EventAttribute{{
Key: "packet",
Value: "receive",
}},
}}
require.Equal(t, expected_events, prResponse2.Events)
}

func TestAnalyzeCode(t *testing.T) {
Expand Down

0 comments on commit d0ca34f

Please sign in to comment.