Skip to content

Commit

Permalink
test: fix falky test
Browse files Browse the repository at this point in the history
  • Loading branch information
tbruyelle committed Nov 26, 2024
1 parent 2f2d958 commit 830ae39
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 38 deletions.
42 changes: 21 additions & 21 deletions tests/e2e/e2e_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
rpchttp "github.com/cometbft/cometbft/rpc/client/http"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
Expand Down Expand Up @@ -159,7 +160,7 @@ func (s *IntegrationTestSuite) execVestingTx( //nolint:unused
atomoneCommand = append(atomoneCommand, fmt.Sprintf("--%s=%v", flag, value))
}

s.executeAtomoneTxCommand(ctx, c, atomoneCommand, 0, s.defaultExecValidation(c, 0))
s.executeAtomoneTxCommand(ctx, c, atomoneCommand, 0, s.defaultExecValidation(c, 0, nil))
s.T().Logf("successfully %s with %v", method, args)
}

Expand Down Expand Up @@ -196,7 +197,7 @@ func (s *IntegrationTestSuite) execUnjail(
atomoneCommand = append(atomoneCommand, fmt.Sprintf("--%s=%v", flag, value))
}

s.executeAtomoneTxCommand(ctx, c, atomoneCommand, 0, s.defaultExecValidation(c, 0))
s.executeAtomoneTxCommand(ctx, c, atomoneCommand, 0, s.defaultExecValidation(c, 0, nil))
s.T().Logf("successfully unjail with options %v", opt)
}

Expand All @@ -223,7 +224,7 @@ func (s *IntegrationTestSuite) execFeeGrant(c *chain, valIdx int, granter, grant
atomoneCommand = append(atomoneCommand, fmt.Sprintf("--%s=%v", flag, value))
}

s.executeAtomoneTxCommand(ctx, c, atomoneCommand, valIdx, s.defaultExecValidation(c, valIdx))
s.executeAtomoneTxCommand(ctx, c, atomoneCommand, valIdx, s.defaultExecValidation(c, valIdx, nil))
}

func (s *IntegrationTestSuite) execFeeGrantRevoke(c *chain, valIdx int, granter, grantee string, opt ...flagOption) {
Expand All @@ -248,7 +249,7 @@ func (s *IntegrationTestSuite) execFeeGrantRevoke(c *chain, valIdx int, granter,
atomoneCommand = append(atomoneCommand, fmt.Sprintf("--%s=%v", flag, value))
}

s.executeAtomoneTxCommand(ctx, c, atomoneCommand, valIdx, s.defaultExecValidation(c, valIdx))
s.executeAtomoneTxCommand(ctx, c, atomoneCommand, valIdx, s.defaultExecValidation(c, valIdx, nil))
}

func (s *IntegrationTestSuite) execBankSend(
Expand Down Expand Up @@ -396,7 +397,7 @@ func (s *IntegrationTestSuite) execDistributionFundCommunityPool(c *chain, valId
"-y",
}

s.executeAtomoneTxCommand(ctx, c, atomoneCommand, valIdx, s.defaultExecValidation(c, valIdx))
s.executeAtomoneTxCommand(ctx, c, atomoneCommand, valIdx, s.defaultExecValidation(c, valIdx, nil))
s.T().Logf("Successfully funded community pool")
}

Expand All @@ -423,7 +424,7 @@ func (s *IntegrationTestSuite) runGovExec(c *chain, valIdx int, submitterAddr, g

atomoneCommand = concatFlags(atomoneCommand, proposalFlags, generalFlags)
s.T().Logf("Executing atomoned tx gov %s on chain %s", govCommand, c.id)
s.executeAtomoneTxCommand(ctx, c, atomoneCommand, valIdx, s.defaultExecValidation(c, valIdx))
s.executeAtomoneTxCommand(ctx, c, atomoneCommand, valIdx, s.defaultExecValidation(c, valIdx, nil))
s.T().Logf("Successfully executed %s", govCommand)
}

Expand Down Expand Up @@ -495,7 +496,7 @@ func (s *IntegrationTestSuite) execDelegate(c *chain, valIdx int, amount, valOpe
"-y",
}

s.executeAtomoneTxCommand(ctx, c, atomoneCommand, valIdx, s.defaultExecValidation(c, valIdx))
s.executeAtomoneTxCommand(ctx, c, atomoneCommand, valIdx, s.defaultExecValidation(c, valIdx, nil))
s.T().Logf("%s successfully delegated %s to %s", delegatorAddr, amount, valOperAddress)
}

Expand All @@ -521,7 +522,7 @@ func (s *IntegrationTestSuite) execUnbondDelegation(c *chain, valIdx int, amount
"-y",
}

s.executeAtomoneTxCommand(ctx, c, atomoneCommand, valIdx, s.defaultExecValidation(c, valIdx))
s.executeAtomoneTxCommand(ctx, c, atomoneCommand, valIdx, s.defaultExecValidation(c, valIdx, nil))
s.T().Logf("%s successfully undelegated %s to %s", delegatorAddr, amount, valOperAddress)
}

Expand All @@ -548,7 +549,7 @@ func (s *IntegrationTestSuite) execCancelUnbondingDelegation(c *chain, valIdx in
"-y",
}

s.executeAtomoneTxCommand(ctx, c, atomoneCommand, valIdx, s.defaultExecValidation(c, valIdx))
s.executeAtomoneTxCommand(ctx, c, atomoneCommand, valIdx, s.defaultExecValidation(c, valIdx, nil))
s.T().Logf("%s successfully canceled unbonding %s to %s", delegatorAddr, amount, valOperAddress)
}

Expand Down Expand Up @@ -578,7 +579,7 @@ func (s *IntegrationTestSuite) execRedelegate(c *chain, valIdx int, amount, orig
"-y",
}

s.executeAtomoneTxCommand(ctx, c, atomoneCommand, valIdx, s.defaultExecValidation(c, valIdx))
s.executeAtomoneTxCommand(ctx, c, atomoneCommand, valIdx, s.defaultExecValidation(c, valIdx, nil))
s.T().Logf("%s successfully redelegated %s from %s to %s", delegatorAddr, amount, originalValOperAddress, newValOperAddress)
}

Expand Down Expand Up @@ -640,7 +641,7 @@ func (s *IntegrationTestSuite) execSetWithdrawAddress(
"-y",
}

s.executeAtomoneTxCommand(ctx, c, atomoneCommand, valIdx, s.defaultExecValidation(c, valIdx))
s.executeAtomoneTxCommand(ctx, c, atomoneCommand, valIdx, s.defaultExecValidation(c, valIdx, nil))
s.T().Logf("Successfully set new distribution withdrawal address for %s to %s", delegatorAddress, newWithdrawalAddress)
}

Expand Down Expand Up @@ -672,13 +673,13 @@ func (s *IntegrationTestSuite) execWithdrawReward(
"-y",
}

s.executeAtomoneTxCommand(ctx, c, atomoneCommand, valIdx, s.defaultExecValidation(c, valIdx))
s.executeAtomoneTxCommand(ctx, c, atomoneCommand, valIdx, s.defaultExecValidation(c, valIdx, nil))
s.T().Logf("Successfully withdrew distribution rewards for delegator %s from validator %s", delegatorAddress, validatorAddress)
}

func (s *IntegrationTestSuite) executeAtomoneTxCommand(ctx context.Context, c *chain, atomoneCommand []string, valIdx int, validation func([]byte, []byte) bool) {
if validation == nil {
validation = s.defaultExecValidation(s.chainA, 0)
validation = s.defaultExecValidation(s.chainA, 0, nil)
}
var (
outBuf bytes.Buffer
Expand Down Expand Up @@ -766,7 +767,7 @@ func (s *IntegrationTestSuite) expectErrExecValidation(chain *chain, valIdx int,
// wait for the tx to be committed on chain
s.Require().Eventuallyf(
func() bool {
gotErr := queryAtomOneTx(endpoint, txResp.TxHash) != nil
gotErr := queryAtomOneTx(endpoint, txResp.TxHash, nil) != nil
return gotErr == expectErr
},
time.Minute,
Expand All @@ -778,7 +779,7 @@ func (s *IntegrationTestSuite) expectErrExecValidation(chain *chain, valIdx int,
}
}

func (s *IntegrationTestSuite) defaultExecValidation(chain *chain, valIdx int) func([]byte, []byte) bool {
func (s *IntegrationTestSuite) defaultExecValidation(chain *chain, valIdx int, msgResp codec.ProtoMarshaler) func([]byte, []byte) bool {
return func(stdOut []byte, stdErr []byte) bool {
var txResp sdk.TxResponse
if err := cdc.UnmarshalJSON(stdOut, &txResp); err != nil {
Expand All @@ -788,7 +789,7 @@ func (s *IntegrationTestSuite) defaultExecValidation(chain *chain, valIdx int) f
endpoint := fmt.Sprintf("http://%s", s.valResources[chain.id][valIdx].GetHostPort("1317/tcp"))
s.Require().Eventually(
func() bool {
err := queryAtomOneTx(endpoint, txResp.TxHash)
err := queryAtomOneTx(endpoint, txResp.TxHash, msgResp)
if isErrNotFound(err) {
// tx not processed yet, continue
return false
Expand Down Expand Up @@ -828,7 +829,7 @@ func (s *IntegrationTestSuite) executeValidatorBond(c *chain, valIdx int, valOpe
"-y",
}

s.executeAtomoneTxCommand(ctx, c, atomoneCommand, valIdx, s.defaultExecValidation(c, valIdx))
s.executeAtomoneTxCommand(ctx, c, atomoneCommand, valIdx, s.defaultExecValidation(c, valIdx, nil))
s.T().Logf("%s successfully executed validator bond tx to %s", delegatorAddr, valOperAddress)
}

Expand All @@ -838,9 +839,8 @@ func (s *IntegrationTestSuite) execPhotonMint(
from,
amt,
fees string,
expectErr bool,
opt ...flagOption,
) {
) (resp photontypes.MsgMintPhotonResponse) {
opt = append(opt, withKeyValue(flagFees, fees))
opt = append(opt, withKeyValue(flagFrom, from))
opts := applyOptions(c.id, opt)
Expand All @@ -862,8 +862,8 @@ func (s *IntegrationTestSuite) execPhotonMint(
atomoneCommand = append(atomoneCommand, fmt.Sprintf("--%s=%v", flag, value))
}

// TODO retrieve tx response
s.executeAtomoneTxCommand(ctx, c, atomoneCommand, valIdx, s.expectErrExecValidation(c, valIdx, expectErr))
s.executeAtomoneTxCommand(ctx, c, atomoneCommand, valIdx, s.defaultExecValidation(c, valIdx, &resp))
return
}

// signTxFileOnline signs a transaction file using the atomoned tx sign command
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/e2e_ibc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (s *IntegrationTestSuite) sendIBC(c *chain, valIdx int, sender, recipient,
"-y",
}
s.T().Logf("sending %s from %s (%s) to %s (%s) with memo %s", token, s.chainA.id, sender, s.chainB.id, recipient, note)
s.executeAtomoneTxCommand(ctx, c, ibcCmd, valIdx, s.defaultExecValidation(c, valIdx))
s.executeAtomoneTxCommand(ctx, c, ibcCmd, valIdx, s.defaultExecValidation(c, valIdx, nil))
s.T().Log("successfully sent IBC tokens")
}

Expand Down
35 changes: 20 additions & 15 deletions tests/e2e/e2e_photon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,32 @@ func (s *IntegrationTestSuite) testMintPhoton() {
conversionRate := s.queryPhotonConversionRate(chainEndpoint)
s.Require().Positive(conversionRate.MustFloat64())

atoneAmt := sdk.NewInt64Coin(uatoneDenom, 1_000_000)
s.execPhotonMint(s.chainA, valIdx, alice.String(), atoneAmt.String(), standardFees.String(), false)
burnedAtoneAmt := sdk.NewInt64Coin(uatoneDenom, 1_000_000)
resp := s.execPhotonMint(s.chainA, valIdx, alice.String(), burnedAtoneAmt.String(), standardFees.String())

var (
expectedMintedPhoton = sdk.NewCoin(uphotonDenom, atoneAmt.Amount.ToLegacyDec().Mul(conversionRate).TruncateInt())
expectedBalance = beforeBalance.
Sub(standardFees).Sub(atoneAmt). // remve burned atones and fees
Add(expectedMintedPhoton) // add minted photons
)
expectedBalance := beforeBalance.
Sub(burnedAtoneAmt). // remove burned atones
Add(resp.Minted). // add minted photons
Sub(standardFees) // remove fees
afterBalance, err := queryAtomOneAllBalances(chainEndpoint, alice.String())
s.Require().NoError(err)
s.Require().Equal(expectedBalance.String(), afterBalance.String())
afterSupply := s.queryBankSupply(chainEndpoint)
_, afterUphotonSupply := afterSupply.Find(uphotonDenom)
s.Require().Equal(expectedMintedPhoton.String(), afterUphotonSupply.String())
var (
expectedUphotonSupply = resp.Minted
afterSupply = s.queryBankSupply(chainEndpoint)
_, afterUphotonSupply = afterSupply.Find(uphotonDenom)
)
s.Require().Equal(expectedUphotonSupply.String(), afterUphotonSupply.String())
// For atone supply assertion we must take into account inflation and so
// we except the final supply to be greater or equal than the initial
// supply + the burned atones.
_, beforeUatoneSupply := beforeSupply.Find(uatoneDenom)
_, afterUatoneSupply := afterSupply.Find(uatoneDenom)
s.Require().True(afterUatoneSupply.IsGTE(beforeUatoneSupply.Add(atoneAmt)),
// supply - the burned atones.
var (
_, beforeUatoneSupply = beforeSupply.Find(uatoneDenom)
_, afterUatoneSupply = afterSupply.Find(uatoneDenom)
)
fmt.Println("BEFORE", beforeUatoneSupply.Sub(burnedAtoneAmt))
fmt.Println("AFTER ", afterUatoneSupply)
s.Require().True(afterUatoneSupply.IsGTE(beforeUatoneSupply.Sub(burnedAtoneAmt)),
"after supply should be >= than initial %s supply", uatoneDenom)
})
}
18 changes: 17 additions & 1 deletion tests/e2e/query_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package e2e

import (
"encoding/hex"
"fmt"
"io"
"net/http"
"strings"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Expand All @@ -21,7 +23,7 @@ import (
)

// queryAtomOneTx returns an error if the tx is not found or is failed.
func queryAtomOneTx(endpoint, txHash string) error {
func queryAtomOneTx(endpoint, txHash string, msgResp codec.ProtoMarshaler) error {
body, err := httpGet(fmt.Sprintf("%s/cosmos/tx/v1beta1/txs/%s", endpoint, txHash))
if err != nil {
return err
Expand All @@ -34,6 +36,20 @@ func queryAtomOneTx(endpoint, txHash string) error {
if resp.TxResponse.Code != 0 {
return fmt.Errorf("tx %s is failed with code=%d log='%s'", txHash, resp.TxResponse.Code, resp.TxResponse.RawLog)
}
if msgResp != nil {
// msgResp is provided, try to decode the tx response
data, err := hex.DecodeString(resp.TxResponse.Data)
if err != nil {
return err
}
var txMsgData sdk.TxMsgData
if err := cdc.Unmarshal(data, &txMsgData); err != nil {
return err
}
if err := cdc.Unmarshal(txMsgData.MsgResponses[0].Value, msgResp); err != nil {
return err
}
}
return nil
}

Expand Down

0 comments on commit 830ae39

Please sign in to comment.