Skip to content

Commit

Permalink
Fix ICA json test
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed Jul 21, 2023
1 parent 86de3a7 commit ac225c6
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 49 deletions.
2 changes: 1 addition & 1 deletion tests/e2e/gov_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func TestGovVoteByContract(t *testing.T) {
e2e.MustExecViaReflectContract(t, chain, contractAddr, voteMsg)

// then proposal executed after voting period
proposal, err := govKeeper.Proposals.Get(sdk.WrapSDKContext(chain.GetContext()), propID)
proposal, err := govKeeper.Proposals.Get(chain.GetContext(), propID)
require.NoError(t, err)
coord.IncrementTimeBy(proposal.VotingEndTime.Sub(chain.GetContext().BlockTime()) + time.Minute)
coord.CommitBlock(chain)
Expand Down
122 changes: 75 additions & 47 deletions tests/e2e/ica_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package e2e

import (
"bytes"
"testing"
"time"

sdkmath "cosmossdk.io/math"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/libs/rand"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
Expand Down Expand Up @@ -41,57 +42,84 @@ func TestICA(t *testing.T) {
path := wasmibctesting.NewPath(controllerChain, hostChain)
coord.SetupConnections(path)

ownerAddr := sdk.AccAddress(controllerChain.SenderPrivKey.PubKey().Address())
msg := icacontrollertypes.NewMsgRegisterInterchainAccount(path.EndpointA.ConnectionID, ownerAddr.String(), "")
res, err := controllerChain.SendMsgs(msg)
require.NoError(t, err)
chanID, portID, version := parseIBCChannelEvents(t, res)

// next open channels on both sides
path.EndpointA.ChannelID = chanID
path.EndpointA.ChannelConfig = &ibctesting.ChannelConfig{
PortID: portID,
Version: version,
Order: channeltypes.ORDERED,
}
path.EndpointB.ChannelConfig = &ibctesting.ChannelConfig{
PortID: icatypes.HostPortID,
Version: icatypes.Version,
Order: channeltypes.ORDERED,
specs := map[string]struct {
icaVersion string
encoding string
}{
"proto": {
icaVersion: "", // empty string defaults to the proto3 encoding type
encoding: icatypes.EncodingProtobuf,
},
"json": {
icaVersion: string(icatypes.ModuleCdc.MustMarshalJSON(&icatypes.Metadata{
Version: icatypes.Version,
ControllerConnectionId: path.EndpointA.ConnectionID,
HostConnectionId: path.EndpointB.ConnectionID,
Encoding: icatypes.EncodingProto3JSON, // use proto3json
TxType: icatypes.TxTypeSDKMultiMsg,
})),
encoding: icatypes.EncodingProto3JSON,
},
}
coord.CreateChannels(path)
for name, spec := range specs {
t.Run(name, func(t *testing.T) {
icaControllerKey := secp256k1.GenPrivKey()
icaControllerAddr := sdk.AccAddress(icaControllerKey.PubKey().Address().Bytes())
controllerChain.Fund(icaControllerAddr, sdkmath.NewInt(1_000))

// assert ICA exists on controller
contApp := controllerChain.App.(*app.WasmApp)
icaRsp, err := contApp.ICAControllerKeeper.InterchainAccount(sdk.WrapSDKContext(controllerChain.GetContext()), &icacontrollertypes.QueryInterchainAccountRequest{
Owner: ownerAddr.String(),
ConnectionId: path.EndpointA.ConnectionID,
})
require.NoError(t, err)
icaAddr := sdk.MustAccAddressFromBech32(icaRsp.GetAddress())
hostChain.Fund(icaAddr, sdkmath.NewInt(1_000))
msg := icacontrollertypes.NewMsgRegisterInterchainAccount(path.EndpointA.ConnectionID, icaControllerAddr.String(), spec.icaVersion)
res, err := controllerChain.SendNonDefaultSenderMsgs(icaControllerKey, msg)
require.NoError(t, err)
chanID, portID, version := parseIBCChannelEvents(t, res)

// submit a tx
targetAddr := sdk.AccAddress(bytes.Repeat([]byte{1}, address.Len))
sendCoin := sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(100))
payloadMsg := banktypes.NewMsgSend(icaAddr, targetAddr, sdk.NewCoins(sendCoin))
rawPayloadData, err := icatypes.SerializeCosmosTx(controllerChain.Codec, []proto.Message{payloadMsg}, icatypes.EncodingProtobuf)
require.NoError(t, err)
payloadPacket := icatypes.InterchainAccountPacketData{
Type: icatypes.EXECUTE_TX,
Data: rawPayloadData,
Memo: "testing",
}
relativeTimeout := uint64(time.Minute.Nanoseconds()) // note this is in nanoseconds
msgSendTx := icacontrollertypes.NewMsgSendTx(ownerAddr.String(), path.EndpointA.ConnectionID, relativeTimeout, payloadPacket)
_, err = controllerChain.SendMsgs(msgSendTx)
require.NoError(t, err)
// next open channels on both sides
path.EndpointA.ChannelID = chanID
path.EndpointA.ChannelConfig = &ibctesting.ChannelConfig{
PortID: portID,
Version: version,
Order: channeltypes.ORDERED,
}
path.EndpointB.ChannelID = ""
path.EndpointB.ChannelConfig = &ibctesting.ChannelConfig{
PortID: icatypes.HostPortID,
Version: icatypes.Version,
Order: channeltypes.ORDERED,
}
coord.CreateChannels(path)

// assert ICA exists on controller
contApp := controllerChain.App.(*app.WasmApp)
icaRsp, err := contApp.ICAControllerKeeper.InterchainAccount(controllerChain.GetContext(), &icacontrollertypes.QueryInterchainAccountRequest{
Owner: icaControllerAddr.String(),
ConnectionId: path.EndpointA.ConnectionID,
})
require.NoError(t, err)
icaAddr := sdk.MustAccAddressFromBech32(icaRsp.GetAddress())
hostChain.Fund(icaAddr, sdkmath.NewInt(1_000))

assert.Equal(t, 1, len(controllerChain.PendingSendPackets))
require.NoError(t, coord.RelayAndAckPendingPackets(path))
// submit a tx
targetAddr := sdk.AccAddress(rand.Bytes(address.Len))
sendCoin := sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(100))
payloadMsg := banktypes.NewMsgSend(icaAddr, targetAddr, sdk.NewCoins(sendCoin))
rawPayloadData, err := icatypes.SerializeCosmosTx(controllerChain.Codec, []proto.Message{payloadMsg}, spec.encoding)
require.NoError(t, err)
payloadPacket := icatypes.InterchainAccountPacketData{
Type: icatypes.EXECUTE_TX,
Data: rawPayloadData,
Memo: "testing",
}
relativeTimeout := uint64(time.Minute.Nanoseconds()) // note this is in nanoseconds
msgSendTx := icacontrollertypes.NewMsgSendTx(icaControllerAddr.String(), path.EndpointA.ConnectionID, relativeTimeout, payloadPacket)
_, err = controllerChain.SendNonDefaultSenderMsgs(icaControllerKey, msgSendTx)
require.NoError(t, err)

gotBalance := hostChain.Balance(targetAddr, sdk.DefaultBondDenom)
assert.Equal(t, sendCoin.String(), gotBalance.String())
assert.Equal(t, 1, len(controllerChain.PendingSendPackets))
require.NoError(t, coord.RelayAndAckPendingPackets(path))

gotBalance := hostChain.Balance(targetAddr, sdk.DefaultBondDenom)
assert.Equal(t, sendCoin.String(), gotBalance.String())
})
}
}

func parseIBCChannelEvents(t *testing.T, res *abci.ExecTxResult) (string, string, string) {
Expand Down
2 changes: 1 addition & 1 deletion x/wasm/keeper/proposal_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func TestInstantiateProposal_NoAdmin(t *testing.T) {
require.NoError(t, gotErr)
// and when
em := sdk.NewEventManager()
_, err = msgServer.ExecLegacyContent(sdk.WrapSDKContext(ctx.WithEventManager(em)), v1.NewMsgExecLegacyContent(contentMsg.Content, govAuthority))
_, err = msgServer.ExecLegacyContent(ctx.WithEventManager(em), v1.NewMsgExecLegacyContent(contentMsg.Content, govAuthority))
// then
require.NoError(t, err)
contractAddr, err := sdk.AccAddressFromBech32("cosmos14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9s4hmalr")
Expand Down

0 comments on commit ac225c6

Please sign in to comment.