Skip to content

Commit

Permalink
wasm exec returns response & err (#842)
Browse files Browse the repository at this point in the history
* make `GetTransaction` public

* ExecuteContract returns TxResponse

(cherry picked from commit 10163ee)

# Conflicts:
#	examples/cosmos/chain_miscellaneous_test.go
#	go.work.sum
#	local-interchain/go.mod
#	local-interchain/go.sum
  • Loading branch information
Reecepbcups authored and mergify[bot] committed Oct 30, 2023
1 parent 35f277c commit ac7fb49
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 25 deletions.
22 changes: 18 additions & 4 deletions chain/cosmos/chain_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ func (tn *ChainNode) StoreContract(ctx context.Context, keyName string, fileName
return res.CodeInfos[0].CodeID, nil
}

func (tn *ChainNode) getTransaction(clientCtx client.Context, txHash string) (*types.TxResponse, error) {
func (tn *ChainNode) GetTransaction(clientCtx client.Context, txHash string) (*types.TxResponse, error) {
// Retry because sometimes the tx is not committed to state yet.
var txResp *types.TxResponse
err := retry.Do(func() error {
Expand Down Expand Up @@ -958,7 +958,7 @@ func (tn *ChainNode) InstantiateContract(ctx context.Context, keyName string, co
return "", err
}

txResp, err := tn.getTransaction(tn.CliContext(), txHash)
txResp, err := tn.GetTransaction(tn.CliContext(), txHash)
if err != nil {
return "", fmt.Errorf("failed to get transaction %s: %w", txHash, err)
}
Expand All @@ -981,11 +981,25 @@ func (tn *ChainNode) InstantiateContract(ctx context.Context, keyName string, co
}

// ExecuteContract executes a contract transaction with a message using it's address.
func (tn *ChainNode) ExecuteContract(ctx context.Context, keyName string, contractAddress string, message string, extraExecTxArgs ...string) (txHash string, err error) {
func (tn *ChainNode) ExecuteContract(ctx context.Context, keyName string, contractAddress string, message string, extraExecTxArgs ...string) (res *types.TxResponse, err error) {
cmd := []string{"wasm", "execute", contractAddress, message}
cmd = append(cmd, extraExecTxArgs...)

return tn.ExecTx(ctx, keyName, cmd...)
txHash, err := tn.ExecTx(ctx, keyName, cmd...)
if err != nil {
return &types.TxResponse{}, err
}

txResp, err := tn.GetTransaction(tn.CliContext(), txHash)
if err != nil {
return &types.TxResponse{}, fmt.Errorf("failed to get transaction %s: %w", txHash, err)
}

if txResp.Code != 0 {
return txResp, fmt.Errorf("error in transaction (code: %d): %s", txResp.Code, txResp.RawLog)
}

return txResp, nil
}

// QueryContract performs a smart query, taking in a query struct and returning a error with the response struct populated.
Expand Down
10 changes: 5 additions & 5 deletions chain/cosmos/cosmos_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ func (c *CosmosChain) SendIBCTransfer(
if err != nil {
return tx, fmt.Errorf("send ibc transfer: %w", err)
}
txResp, err := c.getTransaction(txHash)
txResp, err := c.GetTransaction(txHash)
if err != nil {
return tx, fmt.Errorf("failed to get transaction %s: %w", txHash, err)
}
Expand Down Expand Up @@ -491,7 +491,7 @@ func (c *CosmosChain) QueryBankMetadata(ctx context.Context, denom string) (*Ban
}

func (c *CosmosChain) txProposal(txHash string) (tx TxProposal, _ error) {
txResp, err := c.getTransaction(txHash)
txResp, err := c.GetTransaction(txHash)
if err != nil {
return tx, fmt.Errorf("failed to get transaction %s: %w", txHash, err)
}
Expand Down Expand Up @@ -521,7 +521,7 @@ func (c *CosmosChain) InstantiateContract(ctx context.Context, keyName string, c
}

// ExecuteContract executes a contract transaction with a message using it's address.
func (c *CosmosChain) ExecuteContract(ctx context.Context, keyName string, contractAddress string, message string, extraExecTxArgs ...string) (txHash string, err error) {
func (c *CosmosChain) ExecuteContract(ctx context.Context, keyName string, contractAddress string, message string, extraExecTxArgs ...string) (res *types.TxResponse, err error) {
return c.getFullNode().ExecuteContract(ctx, keyName, contractAddress, message, extraExecTxArgs...)
}

Expand Down Expand Up @@ -592,9 +592,9 @@ func (c *CosmosChain) AllBalances(ctx context.Context, address string) (types.Co
return res.GetBalances(), nil
}

func (c *CosmosChain) getTransaction(txhash string) (*types.TxResponse, error) {
func (c *CosmosChain) GetTransaction(txhash string) (*types.TxResponse, error) {
fn := c.getFullNode()
return fn.getTransaction(fn.CliContext(), txhash)
return fn.GetTransaction(fn.CliContext(), txhash)
}

func (c *CosmosChain) GetGasFeesInNativeDenom(gasPaid int64) int64 {
Expand Down
46 changes: 42 additions & 4 deletions examples/cosmos/chain_miscellaneous_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@ import (

"cosmossdk.io/math"

wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"

sdk "github.com/cosmos/cosmos-sdk/types"
<<<<<<< HEAD
"github.com/strangelove-ventures/interchaintest/v7"
"github.com/strangelove-ventures/interchaintest/v7/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v7/ibc"
=======
testutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
"github.com/strangelove-ventures/interchaintest/v8"
"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v8/ibc"
>>>>>>> 10163ee (wasm exec returns response & err (#842))
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)
Expand Down Expand Up @@ -44,10 +52,11 @@ func CosmosChainTestMiscellaneous(t *testing.T, name, version string) {
ChainName: name,
Version: version,
ChainConfig: ibc.ChainConfig{
Denom: "ujuno",
Bech32Prefix: "juno",
CoinType: "118",
ModifyGenesis: cosmos.ModifyGenesis(sdk47Genesis),
Denom: "ujuno",
Bech32Prefix: "juno",
CoinType: "118",
ModifyGenesis: cosmos.ModifyGenesis(sdk47Genesis),
EncodingConfig: wasmEncoding(),
},
NumValidators: &numVals,
NumFullNodes: &numFullNodes,
Expand Down Expand Up @@ -87,10 +96,17 @@ func CosmosChainTestMiscellaneous(t *testing.T, name, version string) {
testQueryCmd(ctx, t, chain)
testHasCommand(ctx, t, chain)
testTokenFactory(ctx, t, chain, users)
testFailedCWExecute(ctx, t, chain, users)
testAddingNode(ctx, t, chain)
testGetGovernanceAddress(ctx, t, chain)
}

func wasmEncoding() *testutil.TestEncodingConfig {
cfg := cosmos.DefaultEncoding()
wasmtypes.RegisterInterfaces(cfg.InterfaceRegistry)
return &cfg
}

func testBuildDependencies(ctx context.Context, t *testing.T, chain *cosmos.CosmosChain) {
deps := chain.Validators[0].GetBuildInformation(ctx)

Expand Down Expand Up @@ -129,6 +145,28 @@ func testBuildDependencies(ctx context.Context, t *testing.T, chain *cosmos.Cosm
}
}

func testFailedCWExecute(ctx context.Context, t *testing.T, chain *cosmos.CosmosChain, users []ibc.Wallet) {
user := users[0]
keyName := user.KeyName()

codeId, err := chain.StoreContract(ctx, keyName, "sample_contracts/cw_template.wasm")
if err != nil {
t.Fatal(err)
}

contractAddr, err := chain.InstantiateContract(ctx, keyName, codeId, `{"count":0}`, true)
if err != nil {
t.Fatal(err)
}

// execute on the contract with the wrong message (err)
txResp, err := chain.ExecuteContract(ctx, keyName, contractAddr, `{"not_a_func":{}}`)
require.Error(t, err)
fmt.Printf("txResp.RawLog: %+v\n", txResp.RawLog)
fmt.Printf("err: %+v\n", err)
require.Contains(t, err.Error(), "failed to execute message")
}

func testWalletKeys(ctx context.Context, t *testing.T, chain *cosmos.CosmosChain) {
// create a general key
randKey := "randkey123"
Expand Down
Binary file not shown.
14 changes: 2 additions & 12 deletions examples/ibc/wasm/wasm_icq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,19 +278,9 @@ func TestInterchainQueriesWASM(t *testing.T) {
logger.Info("Executing msg ->", zap.String("msg", msg))

//Query the contract on chain 1. The contract makes an interchain query to chain 2 to get the chain 2 user's balance.
hash, err := chain1CChain.ExecuteContract(ctx, chain1User.KeyName(), contractAddr, msg)

require.NoError(t, err)

// Check the results from the interchain query above.
cmd = []string{chain1.Config().Bin, "query", "tx", hash,
"--node", chain1.GetRPCAddress(),
"--home", chain1.HomeDir(),
"--chain-id", chain1.Config().ChainID,
"--output", "json",
}
_, _, err = chain1.Exec(ctx, cmd, nil)
resp, err := chain1CChain.ExecuteContract(ctx, chain1User.KeyName(), contractAddr, msg)
require.NoError(t, err)
require.NotNil(t, resp)

// Wait a few blocks for query to be sent to counterparty.
err = testutil.WaitForBlocks(ctx, 5, chain1, chain2)
Expand Down
30 changes: 30 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<<<<<<< HEAD
cloud.google.com/go v0.110.2/go.mod h1:k04UEeEtb6ZBRTv3dZz4CeJC3jKGxyhl0sAiVVquxiw=
cloud.google.com/go v0.110.7 h1:rJyC7nWRg2jWGZ4wSJ5nY65GTdYJkg0cd/uXb+ACI6o=
cloud.google.com/go v0.110.7/go.mod h1:+EYjdK8e5RME/VY/qLCAtuyALQ9q67dvuum8i+H5xsI=
Expand Down Expand Up @@ -177,3 +178,32 @@ google.golang.org/grpc v1.59.0 h1:Z5Iec2pjwb+LEOqzpB2MR12/eKFhDPhuqW91O+4bwUk=
google.golang.org/grpc v1.59.0/go.mod h1:aUPDwccQo6OTjy7Hct4AfBPD1GptF4fyUjIkQ9YtF98=
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
=======
github.com/CosmWasm/wasmd v0.42.1-0.20230928145107-894076a25cb2 h1:j8J9LnhC6IikohLEYMAFX0xPQmgPez9vsj0rNQISkiE=
github.com/CosmWasm/wasmd v0.42.1-0.20230928145107-894076a25cb2/go.mod h1:3sCglc35LoFUGmh4a/auoJALitaE4qw+jAqK53ak7+s=
github.com/CosmWasm/wasmvm v1.4.0 h1:84I3MlvvzcOo2z+ed0ztPi7eeDNk6/sYuK76uyXP1nI=
github.com/CosmWasm/wasmvm v1.4.0/go.mod h1:vW/E3h8j9xBQs9bCoijDuawKo9kCtxOaS8N8J7KFtkc=
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell/v2 v2.4.1-0.20210905002822-f057f0a857a1/go.mod h1:Az6Jt+M5idSED2YPGtwnfJV0kXohgdCBPmHGSYc1r04=
github.com/gdamore/tcell/v2 v2.6.0 h1:OKbluoP9VYmJwZwq/iLb4BxwKcwGthaa1YNBJIyCySg=
github.com/gdamore/tcell/v2 v2.6.0/go.mod h1:be9omFATkdr0D9qewWW3d+MEvl5dha+Etb5y65J2H8Y=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/rivo/tview v0.0.0-20220307222120-9994674d60a8 h1:xe+mmCnDN82KhC010l3NfYlA8ZbOuzbXAzSYBa6wbMc=
github.com/rivo/tview v0.0.0-20220307222120-9994674d60a8/go.mod h1:WIfMkQNY+oq/mWwtsjOYHIZBuwthioY2srOmljJkTnk=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw=
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
golang.org/x/sys v0.0.0-20210309074719-68d13333faf2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
>>>>>>> 10163ee (wasm exec returns response & err (#842))
4 changes: 4 additions & 0 deletions local-interchain/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ require (
github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
<<<<<<< HEAD
github.com/docker/docker v24.0.5+incompatible // indirect
=======
github.com/docker/docker v24.0.7+incompatible // indirect
>>>>>>> 10163ee (wasm exec returns response & err (#842))
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
Expand Down
5 changes: 5 additions & 0 deletions local-interchain/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,13 @@ github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 h1:fAjc9m62+UWV/WA
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
github.com/docker/distribution v2.8.2+incompatible h1:T3de5rq0dB1j30rp0sA2rER+m322EBzniBPB6ZIzuh8=
github.com/docker/distribution v2.8.2+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
<<<<<<< HEAD
github.com/docker/docker v24.0.5+incompatible h1:WmgcE4fxyI6EEXxBRxsHnZXrO1pQ3smi0k/jho4HLeY=
github.com/docker/docker v24.0.5+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
=======
github.com/docker/docker v24.0.7+incompatible h1:Wo6l37AuwP3JaMnZa226lzVXGA3F9Ig1seQen0cKYlM=
github.com/docker/docker v24.0.7+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
>>>>>>> 10163ee (wasm exec returns response & err (#842))
github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ=
github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec=
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
Expand Down

0 comments on commit ac7fb49

Please sign in to comment.