Skip to content

Commit

Permalink
Updates and fixes to system tests (#1449)
Browse files Browse the repository at this point in the history
* Updates

* Minor cleanup

* Make tests pass
  • Loading branch information
alpe committed Jun 14, 2023
1 parent ce088d9 commit 2f5888c
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 36 deletions.
63 changes: 39 additions & 24 deletions tests/system/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type WasmdCli struct {
amino *codec.LegacyAmino
assertErrorFn RunErrorAssert
awaitNextBlock awaitNextBlock
expTXCommitted bool
}

// NewWasmdCLI constructor
Expand Down Expand Up @@ -67,6 +68,7 @@ func NewWasmdCLIx(
assertErrorFn: require.NoError,
awaitNextBlock: awaiter,
fees: fees,
expTXCommitted: true,
}
}

Expand All @@ -78,53 +80,66 @@ func (c WasmdCli) WithRunErrorsIgnored() WasmdCli {
// WithRunErrorMatcher assert function to ensure run command error value
func (c WasmdCli) WithRunErrorMatcher(f RunErrorAssert) WasmdCli {
return WasmdCli{
t: c.t,
nodeAddress: c.nodeAddress,
chainID: c.chainID,
homeDir: c.homeDir,
Debug: c.Debug,
amino: c.amino,
assertErrorFn: f,
t: c.t,
nodeAddress: c.nodeAddress,
chainID: c.chainID,
homeDir: c.homeDir,
Debug: c.Debug,
amino: c.amino,
assertErrorFn: f,
awaitNextBlock: c.awaitNextBlock,
fees: c.fees,
expTXCommitted: c.expTXCommitted,
}
}

func (c WasmdCli) WithNodeAddress(addr string) WasmdCli {
return WasmdCli{
t: c.t,
nodeAddress: addr,
chainID: c.chainID,
homeDir: c.homeDir,
Debug: c.Debug,
amino: c.amino,
assertErrorFn: c.assertErrorFn,
t: c.t,
nodeAddress: addr,
chainID: c.chainID,
homeDir: c.homeDir,
Debug: c.Debug,
amino: c.amino,
assertErrorFn: c.assertErrorFn,
awaitNextBlock: c.awaitNextBlock,
fees: c.fees,
expTXCommitted: c.expTXCommitted,
}
}

// CustomCommand main entry for excutiong wasmd cli commands. Method blocks until tx is committed.
// CustomCommand main entry for executing wasmd cli commands.
// When configured, method blocks until tx is committed.
func (c WasmdCli) CustomCommand(args ...string) string {
if c.fees != "" && !slices.ContainsFunc(args, func(s string) bool {
return strings.HasPrefix(s, "--fees")
}) {
args = append(args, "--fees="+c.fees) // add default fee
}
args = c.withTXFlags(args...)
return c.awaitTxCommitted(c.run(args))
rsp, committed := c.awaitTxCommitted(c.run(args), defaultWaitTime)
c.t.Logf("tx committed: %v", committed)
require.Equal(c.t, c.expTXCommitted, committed, "expected tx committed: %v", c.expTXCommitted)
return rsp
}

// wait for tx committed on chain
func (c WasmdCli) awaitTxCommitted(got string, timeout ...time.Duration) string {
RequireTxSuccess(c.t, got)
txHash := gjson.Get(got, "txhash")
func (c WasmdCli) awaitTxCommitted(submitResp string, timeout ...time.Duration) (string, bool) {
RequireTxSuccess(c.t, submitResp)
txHash := gjson.Get(submitResp, "txhash")
require.True(c.t, txHash.Exists())
var txResult string
for {
for i := 0; i < 3; i++ { // max blocks to wait for a commit
txResult = c.WithRunErrorsIgnored().CustomQuery("q", "tx", txHash.String())
if gjson.Get(txResult, "code").Exists() {
break
if code := gjson.Get(txResult, "code"); code.Exists() {
if code.Int() != 0 { // 0 = success code
c.t.Logf("+++ got error response code: %s\n", txResult)
}
return txResult, true
}
c.awaitNextBlock(c.t, timeout...)
}
return txResult
return "", false
}

// Keys wasmd keys CLI command
Expand Down Expand Up @@ -335,7 +350,7 @@ func parseResultCode(t *testing.T, got string) (int64, string) {
}

var (
// ErrOutOfGasMatcher requires error with out of gas message
// ErrOutOfGasMatcher requires error with "out of gas" message
ErrOutOfGasMatcher RunErrorAssert = func(t require.TestingT, err error, args ...interface{}) {
const oogMsg = "out of gas"
expErrWithMsg(t, err, args, oogMsg)
Expand Down
33 changes: 22 additions & 11 deletions tests/system/fraud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
package system

import (
sdkmath "cosmossdk.io/math"
"fmt"
"github.com/stretchr/testify/require"
"math"
"strconv"
"testing"

"github.com/stretchr/testify/require"
)

func TestRecursiveMsgsExternalTrigger(t *testing.T) {
t.Skip()
sut.ResetDirtyChain(t)
const maxBlockGas = 2_000_000
sut.ModifyGenesisJSON(t, SetConsensusMaxGas(t, maxBlockGas))
sut.StartChain(t)
cli := NewWasmdCLI(t, sut, verbose)

Expand All @@ -29,18 +28,20 @@ func TestRecursiveMsgsExternalTrigger(t *testing.T) {
gas: "auto",
expErrMatcher: ErrOutOfGasMatcher,
},
"tx": { // tx will be rejected by Tendermint in post abci checkTX operation
gas: strconv.Itoa(math.MaxInt64),
expErrMatcher: ErrTimeoutMatcher,
},
}
for name, spec := range specs {
t.Run(name, func(t *testing.T) {
cli := NewWasmdCLI(t, sut, verbose)
execMsg := `{"message_loop":{}}`
fees := "1stake"
gas := spec.gas
if gas != "auto" {
fees = calcMinFeeRequired(t, gas)
}
for _, n := range sut.AllNodes(t) {
cli.WithRunErrorMatcher(spec.expErrMatcher).WithNodeAddress(n.RPCAddr()).
WasmExecute(contractAddr, execMsg, defaultSrcAddr, "--gas="+spec.gas, "--broadcast-mode=sync", "--fees=1stake")
clix := cli.WithRunErrorMatcher(spec.expErrMatcher).WithNodeAddress(n.RPCAddr())
clix.expTXCommitted = false
clix.WasmExecute(contractAddr, execMsg, defaultSrcAddr, "--gas="+gas, "--broadcast-mode=sync", "--fees="+fees)
}
sut.AwaitNextBlock(t)
})
Expand All @@ -64,3 +65,13 @@ func TestRecursiveSmartQuery(t *testing.T) {
}
sut.AwaitNextBlock(t)
}

// with default gas factor and token
func calcMinFeeRequired(t *testing.T, gas string) string {
x, ok := sdkmath.NewIntFromString(gas)
require.True(t, ok)
const defaultTestnetFee = "0.000006"
minFee, err := sdkmath.LegacyNewDecFromStr(defaultTestnetFee)
require.NoError(t, err)
return fmt.Sprintf("%sstake", minFee.Mul(sdkmath.LegacyNewDecFromInt(x)).RoundInt().String())
}
19 changes: 19 additions & 0 deletions tests/system/genesis_mutators.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package system

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
"github.com/tidwall/sjson"
)

// SetConsensusMaxGas max gas that can be consumed in a block
func SetConsensusMaxGas(t *testing.T, max int) GenesisMutator {
return func(genesis []byte) []byte {
t.Helper()
state, err := sjson.SetRawBytes(genesis, "consensus_params.block.max_gas", []byte(fmt.Sprintf(`"%d"`, max)))
require.NoError(t, err)
return state
}
}
2 changes: 1 addition & 1 deletion tests/system/staking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestStakeUnstake(t *testing.T) {

cli := NewWasmdCLI(t, sut, verbose)

//add genesis account with some tokens
// add genesis account with some tokens
account1Addr := cli.AddKey("account1")
sut.ModifyGenesisCLI(t,
[]string{"genesis", "add-genesis-account", account1Addr, "100000000stake"},
Expand Down

0 comments on commit 2f5888c

Please sign in to comment.