Skip to content

Commit

Permalink
chore(test): migrate wasm ibc to cw_ibc_example from reflect_send (#1131
Browse files Browse the repository at this point in the history
)

* chore: switched wasm ibc test from reflect_send to zeke's cw_ibc_example

* chore: added readme and removed extraneous coercion

* chore: remove unnecessary type cast
  • Loading branch information
kakucodes authored May 19, 2024
1 parent 2fe399e commit 1340e70
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 60 deletions.
5 changes: 5 additions & 0 deletions chain/cosmos/cosmos_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,11 @@ func (c *CosmosChain) ExportState(ctx context.Context, height int64) (string, er
return c.getFullNode().ExportState(ctx, height)
}

// QueryContractInfo queries the chain for the contract metadata.
func (c *CosmosChain) QueryContractInfo(ctx context.Context, contractAddress string) (*ContractInfoResponse, error) {
return c.getFullNode().QueryContractInfo(ctx, contractAddress)
}

func (c *CosmosChain) GetTransaction(txhash string) (*types.TxResponse, error) {
fn := c.getFullNode()
return fn.GetTransaction(fn.CliContext(), txhash)
Expand Down
16 changes: 16 additions & 0 deletions chain/cosmos/module_cosmwasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,19 @@ func (tn *ChainNode) DumpContractState(ctx context.Context, contractAddress stri
}
return res, nil
}

// QueryContractInfo queries the information about a contract like the admin and code_id.
func (tn *ChainNode) QueryContractInfo(ctx context.Context, contractAddress string) (*ContractInfoResponse, error) {
stdout, _, err := tn.ExecQuery(ctx,
"wasm", "contract", contractAddress,
)
if err != nil {
return nil, err
}

res := new(ContractInfoResponse)
if err := json.Unmarshal(stdout, res); err != nil {
return nil, err
}
return res, nil
}
16 changes: 16 additions & 0 deletions chain/cosmos/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,19 @@ type DenomAuthorityMetadata struct {
// Can be empty for no admin, or a valid address
Admin string `protobuf:"bytes,1,opt,name=admin,proto3" json:"admin,omitempty" yaml:"admin"`
}

type ContractInfoResponse struct {
Address string `json:"address"`
ContractInfo struct {
CodeID string `json:"code_id"`
Creator string `json:"creator"`
Admin string `json:"admin"`
Label string `json:"label"`
Created struct {
BlockHeight string `json:"block_height"`
TxIndex string `json:"tx_index"`
} `json:"created"`
IbcPortID string `json:"ibc_port_id"`
Extension any `json:"extension"`
} `json:"contract_info"`
}
3 changes: 3 additions & 0 deletions examples/ibc/wasm/sample_contracts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Sample Contracts

* [cw_ibc_example](https://github.com/0xekez/cw-ibc-example/commit/95546a01548c98aa7b66927dcd603bc224284f75)
Binary file not shown.
Binary file removed examples/ibc/wasm/sample_contracts/ibc_reflect.wasm
Binary file not shown.
Binary file not shown.
Binary file removed examples/ibc/wasm/sample_contracts/reflect.wasm
Binary file not shown.
148 changes: 88 additions & 60 deletions examples/ibc/wasm/wasm_ibc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,48 +110,45 @@ func TestWasmIbc(t *testing.T) {
juno1Chain := juno1.(*cosmos.CosmosChain)
juno2Chain := juno2.(*cosmos.CosmosChain)

// Store ibc_reflect_send.wasm contract
ibcReflectSendCodeId, err := juno1Chain.StoreContract(
ctx, juno1User.KeyName(), "sample_contracts/ibc_reflect_send.wasm")
// Store ibc_reflect_send.wasm contract on juno1
juno1ContractCodeId, err := juno1Chain.StoreContract(
ctx, juno1User.KeyName(), "sample_contracts/cw_ibc_example.wasm")
require.NoError(t, err)

// Instantiate ibc_reflect_send.wasm contract
ibcReflectSendContractAddr, err := juno1Chain.InstantiateContract(
ctx, juno1User.KeyName(), ibcReflectSendCodeId, "{}", true)
// Instantiate the contract on juno1
juno1ContractAddr, err := juno1Chain.InstantiateContract(
ctx, juno1User.KeyName(), juno1ContractCodeId, "{}", true)
require.NoError(t, err)

// Store reflect.wasm contract
reflectCodeId, err := juno2Chain.StoreContract(
ctx, juno2User.KeyName(), "sample_contracts/reflect.wasm")
// Store ibc_reflect_send.wasm on juno2
juno2ContractCodeId, err := juno2Chain.StoreContract(
ctx, juno2User.KeyName(), "sample_contracts/cw_ibc_example.wasm")
require.NoError(t, err)

// Instantiate reflect.wasm contract
_, err = juno2Chain.InstantiateContract(
ctx, juno2User.KeyName(), reflectCodeId, "{}", true)
// Instantiate contract on juno2
juno2ContractAddr, err := juno2Chain.InstantiateContract(
ctx, juno2User.KeyName(), juno2ContractCodeId, "{}", true)
require.NoError(t, err)

// Store ibc_reflect.wasm contract
ibcReflectCodeId, err := juno2Chain.StoreContract(
ctx, juno2User.KeyName(), "sample_contracts/ibc_reflect.wasm")
err = testutil.WaitForBlocks(ctx, 1, juno1, juno2)
require.NoError(t, err)

// Instantiate ibc_reflect_send.wasm contract
initMsg := "{\"reflect_code_id\":" + reflectCodeId + "}"
ibcReflectContractAddr, err := juno2Chain.InstantiateContract(
ctx, juno2User.KeyName(), ibcReflectCodeId, initMsg, true)
// Query the reflect sender contract on Juno1 for it's port id
juno1ContractInfo, err := juno1Chain.QueryContractInfo(ctx, juno1ContractAddr)
require.NoError(t, err)
juno1ContractPortId := juno1ContractInfo.ContractInfo.IbcPortID

err = testutil.WaitForBlocks(ctx, 2, juno1, juno2)
// Query the reflect contract on Juno2 for it's port id
juno2ContractInfo, err := juno2Chain.QueryContractInfo(ctx, juno2ContractAddr)
require.NoError(t, err)
juno2ContractPortId := juno2ContractInfo.ContractInfo.IbcPortID

// Set up channel
ibcReflectSendPortId := "wasm." + ibcReflectSendContractAddr
ibcReflectPortId := "wasm." + ibcReflectContractAddr
// Create channel between Juno1 and Juno2
err = r.CreateChannel(ctx, eRep, ibcPath, ibc.CreateChannelOptions{
SourcePortName: ibcReflectSendPortId,
DestPortName: ibcReflectPortId,
Order: ibc.Ordered,
Version: "ibc-reflect-v1",
SourcePortName: juno1ContractPortId,
DestPortName: juno2ContractPortId,
Order: ibc.Unordered,
Version: "counter-1",
})
require.NoError(t, err)

Expand All @@ -164,49 +161,80 @@ func TestWasmIbc(t *testing.T) {
require.NoError(t, err)
juno1ChannelID := juno1ChannelInfo[len(juno1ChannelInfo)-1].ChannelID

queryMsg := fmt.Sprintf(`{"account":{"channel_id":"%s"}}`, juno1ChannelID)
// Get contract channel
juno2ChannelInfo, err := r.GetChannels(ctx, eRep, juno1.Config().ChainID)
require.NoError(t, err)
juno2ChannelID := juno2ChannelInfo[len(juno2ChannelInfo)-1].ChannelID

// Prepare the query and execute messages to interact with the contracts
queryJuno1CountMsg := fmt.Sprintf(`{"get_count":{"channel":"%s"}}`, juno1ChannelID)
queryJuno2CountMsg := fmt.Sprintf(`{"get_count":{"channel":"%s"}}`, juno2ChannelID)
juno1IncrementMsg := fmt.Sprintf(`{"increment": {"channel":"%s"}}`, juno1ChannelID)
juno2IncrementMsg := fmt.Sprintf(`{"increment": {"channel":"%s"}}`, juno2ChannelID)

// Query ibc_reflect_send contract on Juno1 for remote address (populated via ibc)
var ibcReflectSendResponse IbcReflectSendResponseData
err = juno1Chain.QueryContract(ctx, ibcReflectSendContractAddr, queryMsg, &ibcReflectSendResponse)
_, err = juno1.Height(ctx)
require.NoError(t, err)
require.NotEmpty(t, ibcReflectSendResponse.Data.RemoteAddr)

// Query ibc_reflect contract on Juno2 for local account address
var ibcReflectResponse IbcReflectResponseData
err = juno2Chain.QueryContract(ctx, ibcReflectContractAddr, queryMsg, &ibcReflectResponse)
// Query the count of the contract on juno1- should be 0 as no packets have been sent through
var juno1InitialCountResponse CwIbcCountResponse
err = juno1Chain.QueryContract(ctx, juno1ContractAddr, queryJuno1CountMsg, &juno1InitialCountResponse)
require.NoError(t, err)
require.NotEmpty(t, ibcReflectResponse.Data.Account)
require.Equal(t, 0, juno1InitialCountResponse.Data.Count)

// Verify that these addresses match, a match is a successful test run
// - ibc_reflect_send contract (Juno1) remote address (retrieved via ibc)
// - ibc_reflect contract (Juno2) account address populated locally
require.Equal(t, ibcReflectSendResponse.Data.RemoteAddr, ibcReflectResponse.Data.Account)
}
// Query the count of the contract on juno1- should be 0 as no packets have been sent through
var juno2InitialCountResponse CwIbcCountResponse
err = juno2Chain.QueryContract(ctx, juno2ContractAddr, queryJuno2CountMsg, &juno2InitialCountResponse)
require.NoError(t, err)
require.Equal(t, 0, juno2InitialCountResponse.Data.Count)

type Coin struct {
Denom string `json:"denom"` // type, eg. "ATOM"
Amount string `json:"amount"` // string encoing of decimal value, eg. "12.3456"
}
// Send packet from juno1 to juno2 and increment the juno2 contract count
juno1Increment, err := juno1Chain.ExecuteContract(ctx, juno1User.KeyName(), juno1ContractAddr, juno1IncrementMsg)
require.NoError(t, err)
// Check if the transaction was successful
require.Equal(t, uint32(0), juno1Increment.Code)

type Coins []Coin
// Wait for the ibc packet to be delivered
err = testutil.WaitForBlocks(ctx, 2, juno1, juno2)
require.NoError(t, err)

type IbcReflectSendAccountResponse struct {
LastUpdateTime uint64 `json:"last_update_time,string"`
RemoteAddr string `json:"remote_addr"`
RemoteBalance Coins `json:"remote_balance"`
}
// Query the count of the contract on juno2- should be 1 as a single packet has been sent through
var juno2IncrementedCountResponse CwIbcCountResponse
err = juno2Chain.QueryContract(ctx, juno2ContractAddr, queryJuno2CountMsg, &juno2IncrementedCountResponse)
require.NoError(t, err)
require.Equal(t, 1, juno2IncrementedCountResponse.Data.Count)

// ibc_reflect_send response data
type IbcReflectSendResponseData struct {
Data IbcReflectSendAccountResponse `json:"data"`
}
// Query the count of the contract on juno1- should still be 0 as no packets have been sent through
var juno1PreIncrementedCountResponse CwIbcCountResponse
err = juno1Chain.QueryContract(ctx, juno1ContractAddr, queryJuno1CountMsg, &juno1PreIncrementedCountResponse)
require.NoError(t, err)
require.Equal(t, 0, juno1PreIncrementedCountResponse.Data.Count)

// send packet from juno2 to juno1 and increment the juno1 contract count
juno2Increment, err := juno2Chain.ExecuteContract(ctx, juno2User.KeyName(), juno2ContractAddr, juno2IncrementMsg)
require.NoError(t, err)
require.Equal(t, uint32(0), juno2Increment.Code)

// Wait for the ibc packet to be delivered
err = testutil.WaitForBlocks(ctx, 2, juno1, juno2)
require.NoError(t, err)

// Query the count of the contract on juno1- should still be 1 as a single packet has been sent through
var juno1IncrementedCountResponse CwIbcCountResponse
err = juno1Chain.QueryContract(ctx, juno1ContractAddr, queryJuno1CountMsg, &juno1IncrementedCountResponse)
require.NoError(t, err)
require.Equal(t, 1, juno1IncrementedCountResponse.Data.Count)

// Query the count of the contract on juno2- should be 1 as a single packet has now been sent through from juno1 to juno2
var juno2PreIncrementedCountResponse CwIbcCountResponse
err = juno2Chain.QueryContract(ctx, juno2ContractAddr, queryJuno2CountMsg, &juno2PreIncrementedCountResponse)
require.NoError(t, err)
require.Equal(t, 1, juno2PreIncrementedCountResponse.Data.Count)

type IbcReflectAccountResponse struct {
Account string `json:"account"`
}

// ibc_reflect response data
type IbcReflectResponseData struct {
Data IbcReflectAccountResponse `json:"data"`
// cw_ibc_example response data
type CwIbcCountResponse struct {
Data struct {
Count int `json:"count"`
} `json:"data"`
}

0 comments on commit 1340e70

Please sign in to comment.