Skip to content

Commit

Permalink
BCI-978: Update core integration tests helper method (#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
augustbleeds authored Jul 28, 2023
1 parent 9007d9f commit bd75c14
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
17 changes: 9 additions & 8 deletions pkg/cosmos/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestErrMatch(t *testing.T) {
}

func TestBatchSim(t *testing.T) {
accounts, testdir, tendermintURL := SetupLocalCosmosNode(t, "42")
accounts, testdir, tendermintURL := SetupLocalCosmosNode(t, "42", "ucosm")

lggr, logs := logger.TestObserved(t, zap.WarnLevel)
tc, err := NewClient(
Expand All @@ -61,7 +61,7 @@ func TestBatchSim(t *testing.T) {
return func() { assert.Len(t, logs.TakeAll(), l) }
}

contract := DeployTestContract(t, tendermintURL, "42", accounts[0], accounts[0], tc, testdir, "../testdata/my_first_contract.wasm")
contract := DeployTestContract(t, tendermintURL, "42", "ucosm", accounts[0], accounts[0], tc, testdir, "../testdata/my_first_contract.wasm")
var succeed sdk.Msg = &wasmtypes.MsgExecuteContract{Sender: accounts[0].Address.String(), Contract: contract.String(), Msg: []byte(`{"reset":{"count":5}}`)}
var fail sdk.Msg = &wasmtypes.MsgExecuteContract{Sender: accounts[0].Address.String(), Contract: contract.String(), Msg: []byte(`{"blah":{"count":5}}`)}

Expand Down Expand Up @@ -132,8 +132,9 @@ func TestBatchSim(t *testing.T) {
}

func TestCosmosClient(t *testing.T) {
minGasPrice := sdk.NewDecCoinFromDec("ucosm", defaultCoin)
// Local only for now, could maybe run on CI if we install terrad there?
accounts, testdir, tendermintURL := SetupLocalCosmosNode(t, "42")
accounts, testdir, tendermintURL := SetupLocalCosmosNode(t, "42", "ucosm")
lggr := logger.Test(t)
tc, err := NewClient(
"42",
Expand All @@ -144,7 +145,7 @@ func TestCosmosClient(t *testing.T) {
gpe := NewFixedGasPriceEstimator(map[string]sdk.DecCoin{
"ucosm": sdk.NewDecCoinFromDec("ucosm", sdk.MustNewDecFromStr("0.01")),
})
contract := DeployTestContract(t, tendermintURL, "42", accounts[0], accounts[0], tc, testdir, "../testdata/my_first_contract.wasm")
contract := DeployTestContract(t, tendermintURL, "42", "ucosm", accounts[0], accounts[0], tc, testdir, "../testdata/my_first_contract.wasm")

t.Run("send tx between accounts", func(t *testing.T) {
// Assert balance before
Expand All @@ -166,7 +167,7 @@ func TestCosmosClient(t *testing.T) {
require.NoError(t, err)
resp, err := tc.Broadcast(txBytes, txtypes.BroadcastMode_BROADCAST_MODE_SYNC)
require.NoError(t, err)
tx, success := awaitTxCommitted(t, tc, resp.TxResponse.TxHash)
tx, success := AwaitTxCommitted(t, tc, resp.TxResponse.TxHash)
require.True(t, success)
require.Equal(t, types.CodeTypeOK, tx.TxResponse.Code)

Expand Down Expand Up @@ -226,7 +227,7 @@ func TestCosmosClient(t *testing.T) {
require.NoError(t, err)
resp1, err := tc.SignAndBroadcast([]sdk.Msg{rawMsg}, an, sn, gasPrices["ucosm"], accounts[0].PrivateKey, txtypes.BroadcastMode_BROADCAST_MODE_SYNC)
require.NoError(t, err)
tx1, success := awaitTxCommitted(t, tc, resp1.TxResponse.TxHash)
tx1, success := AwaitTxCommitted(t, tc, resp1.TxResponse.TxHash)
require.True(t, success)
require.Equal(t, types.CodeTypeOK, tx1.TxResponse.Code)

Expand All @@ -241,7 +242,7 @@ func TestCosmosClient(t *testing.T) {
require.NoError(t, err)
resp2, err := tc.SignAndBroadcast([]sdk.Msg{rawMsg}, an, sn, gasPrices["ucosm"], accounts[0].PrivateKey, txtypes.BroadcastMode_BROADCAST_MODE_SYNC)
require.NoError(t, err)
tx2, success := awaitTxCommitted(t, tc, resp2.TxResponse.TxHash)
tx2, success := AwaitTxCommitted(t, tc, resp2.TxResponse.TxHash)
require.True(t, success)
require.Equal(t, types.CodeTypeOK, tx2.TxResponse.Code)

Expand Down Expand Up @@ -344,7 +345,7 @@ func TestCosmosClient(t *testing.T) {

if tt.expCode == 0 {
require.NoError(t, err)
tx, success := awaitTxCommitted(t, tc, resp.TxResponse.TxHash)
tx, success := AwaitTxCommitted(t, tc, resp.TxResponse.TxHash)
require.True(t, success)
require.Equal(t, types.CodeTypeOK, tx.TxResponse.Code)
require.Equal(t, "", tx.TxResponse.Codespace)
Expand Down
32 changes: 18 additions & 14 deletions pkg/cosmos/client/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ type Account struct {
}

// 0.001
var minGasPrice = sdk.NewDecCoinFromDec("ucosm", sdk.NewDecWithPrec(1, 3))
var defaultCoin = sdk.NewDecWithPrec(1, 3)

// SetupLocalCosmosNode sets up a local terra node via wasmd, and returns pre-funded accounts, the test directory, and the url.
func SetupLocalCosmosNode(t *testing.T, chainID string) ([]Account, string, string) {
// Token name is for both staking and fee coin
func SetupLocalCosmosNode(t *testing.T, chainID string, token string) ([]Account, string, string) {
minGasPrice := sdk.NewDecCoinFromDec(token, defaultCoin)
testdir, err := os.MkdirTemp("", "integration-test")
require.NoError(t, err)
t.Cleanup(func() {
Expand All @@ -62,11 +64,12 @@ func SetupLocalCosmosNode(t *testing.T, chainID string) ([]Account, string, stri
require.NoError(t, err)

genesisData := string(f)
// fix hardcoded token, see
// fix hardcoded staking/governance token, see
// https://github.com/CosmWasm/wasmd/blob/develop/docker/setup_wasmd.sh
// https://github.com/CosmWasm/wasmd/blob/develop/contrib/local/setup_wasmd.sh
genesisData = strings.ReplaceAll(genesisData, "\"ustake\"", "\"ucosm\"")
genesisData = strings.ReplaceAll(genesisData, "\"stake\"", "\"ucosm\"")
newStakingToken := fmt.Sprintf(`"%s"`, token)
genesisData = strings.ReplaceAll(genesisData, "\"ustake\"", newStakingToken)
genesisData = strings.ReplaceAll(genesisData, "\"stake\"", newStakingToken)
require.NoError(t, os.WriteFile(p, []byte(genesisData), 0600))

// Create 2 test accounts
Expand All @@ -86,16 +89,16 @@ func SetupLocalCosmosNode(t *testing.T, chainID string) ([]Account, string, stri
require.NoError(t, err4)
require.Equal(t, expAcctAddr, address)
// Give it 100000000ucosm
out2, err2 := exec.Command("wasmd", "genesis", "add-genesis-account", k.Address, "100000000ucosm", "--home", testdir).Output() //nolint:gosec
out2, err2 := exec.Command("wasmd", "genesis", "add-genesis-account", k.Address, "100000000"+token, "--home", testdir).Output() //nolint:gosec
require.NoError(t, err2, string(out2))
accounts = append(accounts, Account{
Name: account,
Address: address,
PrivateKey: privateKey,
})
}
// Stake 10 luna in first acct
out, err = exec.Command("wasmd", "genesis", "gentx", accounts[0].Name, "10000000ucosm", "--chain-id", chainID, "--keyring-backend", "test", "--home", testdir).CombinedOutput() //nolint:gosec
// Stake 10 tokens in first acct
out, err = exec.Command("wasmd", "genesis", "gentx", accounts[0].Name, "10000000"+token, "--chain-id", chainID, "--keyring-backend", "test", "--home", testdir).CombinedOutput() //nolint:gosec
require.NoError(t, err, string(out))
out, err = exec.Command("wasmd", "genesis", "collect-gentxs", "--home", testdir).CombinedOutput()
require.NoError(t, err, string(out))
Expand Down Expand Up @@ -152,16 +155,17 @@ func SetupLocalCosmosNode(t *testing.T, chainID string) ([]Account, string, stri
}

// DeployTestContract deploys a test contract.
func DeployTestContract(t *testing.T, tendermintURL, chainID string, deployAccount, ownerAccount Account, tc *Client, testdir, wasmTestContractPath string) sdk.AccAddress {
func DeployTestContract(t *testing.T, tendermintURL, chainID string, token string, deployAccount, ownerAccount Account, tc *Client, testdir, wasmTestContractPath string) sdk.AccAddress {
minGasPrice := sdk.NewDecCoinFromDec(token, defaultCoin)
//nolint:gosec
submitResp, err2 := exec.Command("wasmd", "tx", "wasm", "store", wasmTestContractPath, "--node", tendermintURL,
"--from", deployAccount.Name, "--gas", "auto", "--fees", "100000ucosm", "--gas-adjustment", "1.3", "--chain-id", chainID, "--home", testdir, "--keyring-backend", "test", "--keyring-dir", testdir, "--yes", "--output", "json").Output()
"--from", deployAccount.Name, "--gas", "auto", "--fees", "100000"+token, "--gas-adjustment", "1.3", "--chain-id", chainID, "--home", testdir, "--keyring-backend", "test", "--keyring-dir", testdir, "--yes", "--output", "json").Output()
require.NoError(t, err2, string(submitResp))

// wait for tx to be committed
txHash := gjson.Get(string(submitResp), "txhash")
require.True(t, txHash.Exists())
storeTx, success := awaitTxCommitted(t, tc, txHash.String())
storeTx, success := AwaitTxCommitted(t, tc, txHash.String())
require.True(t, success)

// get code id from tx receipt
Expand All @@ -184,7 +188,7 @@ func DeployTestContract(t *testing.T, tendermintURL, chainID string, deployAccou
require.NoError(t, err3)

// wait for tx to be committed
deployTxReceipt, success := awaitTxCommitted(t, tc, deployTx.TxResponse.TxHash)
deployTxReceipt, success := AwaitTxCommitted(t, tc, deployTx.TxResponse.TxHash)
require.True(t, success)

return GetContractAddr(t, deployTxReceipt.GetTxResponse())
Expand Down Expand Up @@ -215,8 +219,8 @@ func mustRandomPort() int {
return int(r.Int64() + 1024)
}

// awaitTxCommitted waits for a transaction to be committed on chain and returns the tx receipt
func awaitTxCommitted(t *testing.T, tc *Client, txHash string) (response *txtypes.GetTxResponse, success bool) {
// AwaitTxCommitted waits for a transaction to be committed on chain and returns the tx receipt
func AwaitTxCommitted(t *testing.T, tc *Client, txHash string) (response *txtypes.GetTxResponse, success bool) {
for i := 0; i < 10; i++ { // max poll attempts to wait for tx commitment
txReceipt, err := tc.Tx(txHash)
if err == nil {
Expand Down

0 comments on commit bd75c14

Please sign in to comment.