diff --git a/tests/system/cli.go b/tests/system/cli.go index ef7f8fb104..403cbfd4d9 100644 --- a/tests/system/cli.go +++ b/tests/system/cli.go @@ -40,6 +40,7 @@ type WasmdCli struct { amino *codec.LegacyAmino assertErrorFn RunErrorAssert awaitNextBlock awaitNextBlock + expTXCommitted bool } // NewWasmdCLI constructor @@ -67,6 +68,7 @@ func NewWasmdCLIx( assertErrorFn: require.NoError, awaitNextBlock: awaiter, fees: fees, + expTXCommitted: true, } } @@ -78,29 +80,36 @@ 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") @@ -108,23 +117,29 @@ func (c WasmdCli) CustomCommand(args ...string) string { 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 @@ -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) diff --git a/tests/system/fraud_test.go b/tests/system/fraud_test.go index 503a826944..7d42d4b2e1 100644 --- a/tests/system/fraud_test.go +++ b/tests/system/fraud_test.go @@ -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) @@ -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) }) @@ -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()) +} diff --git a/tests/system/genesis_mutators.go b/tests/system/genesis_mutators.go new file mode 100644 index 0000000000..5393415c87 --- /dev/null +++ b/tests/system/genesis_mutators.go @@ -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 + } +} diff --git a/tests/system/staking_test.go b/tests/system/staking_test.go index 1e83db8593..2cb25d9778 100644 --- a/tests/system/staking_test.go +++ b/tests/system/staking_test.go @@ -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"},