Skip to content

Commit

Permalink
test tx encoding & decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
joelsmith-2019 committed Aug 14, 2024
1 parent 5831f91 commit 0066156
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions examples/cosmos/chain_miscellaneous_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ func CosmosChainTestMiscellaneous(t *testing.T, name, version string) {
CoinType: "118",
ModifyGenesis: cosmos.ModifyGenesis(sdk47Genesis),
EncodingConfig: wasmEncoding(),
GasAdjustment: 1.5,
Gas: "auto",
},
NumValidators: &numVals,
NumFullNodes: &numFullNodes,
Expand Down Expand Up @@ -94,6 +96,7 @@ func CosmosChainTestMiscellaneous(t *testing.T, name, version string) {
testAddingNode(ctx, t, chain)
testGetGovernanceAddress(ctx, t, chain)
testTXFailsOnBlockInclusion(ctx, t, chain, users)
testTXEncodeDecode(ctx, t, chain, users)
}

func wasmEncoding() *testutil.TestEncodingConfig {
Expand Down Expand Up @@ -440,6 +443,40 @@ func testTXFailsOnBlockInclusion(ctx context.Context, t *testing.T, chain *cosmo
require.Error(t, err)
}

func testTXEncodeDecode(ctx context.Context, t *testing.T, chain *cosmos.CosmosChain, users []ibc.Wallet) {
generate := chain.GetNode().TxCommand(users[0].KeyName(), "bank", "send", users[0].FormattedAddress(), users[1].FormattedAddress(), "1"+chain.Config().Denom, "--generate-only")

// check if generate contains "--gas"
require.Contains(t, generate, "--gas")

txJson, _, err := chain.GetNode().Exec(ctx, generate, nil)
require.NoError(t, err)

// encode
err = chain.GetNode().WriteFile(ctx, txJson, "tx.json")
require.NoError(t, err)
encode := chain.GetNode().TxCommand(users[0].KeyName(), "encode", chain.GetNode().HomeDir()+"/tx.json")
encoded, _, err := chain.GetNode().Exec(ctx, encode, nil)
require.NoError(t, err)

// decode
decode := chain.GetNode().TxCommand(users[0].KeyName(), "decode", string(encoded))
decoded, _, err := chain.GetNode().Exec(ctx, decode, nil)
require.NoError(t, err)

err = chain.GetNode().WriteFile(ctx, decoded, "decoded.json")
require.NoError(t, err)

sign := chain.GetNode().TxCommand(users[0].KeyName(), "sign", chain.GetNode().HomeDir()+"/decoded.json")
signed, _, err := chain.GetNode().Exec(ctx, sign, nil)
require.NoError(t, err)

err = chain.GetNode().WriteFile(ctx, signed, "signed.json")
require.NoError(t, err)
_, err = chain.GetNode().ExecTx(ctx, users[0].KeyName(), "broadcast", chain.GetNode().HomeDir()+"/signed.json")
require.NoError(t, err)
}

// helpers
func sendTokens(ctx context.Context, chain *cosmos.CosmosChain, from, to ibc.Wallet, token string, amount int64) (ibc.WalletAmount, error) {
if token == "" {
Expand Down

0 comments on commit 0066156

Please sign in to comment.