Skip to content

Commit

Permalink
e2e: sdk.TxResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
Reecepbcups committed Jul 15, 2024
1 parent da5cc79 commit 792cc70
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 90 deletions.
2 changes: 1 addition & 1 deletion e2e/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ require (
github.com/cockroachdb/pebble v1.1.0 // indirect
github.com/cockroachdb/redact v1.1.5 // indirect
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect
github.com/cometbft/cometbft v0.38.6 // indirect
github.com/cometbft/cometbft v0.38.7 // indirect
github.com/cometbft/cometbft-db v0.9.1 // indirect
github.com/cosmos/btcutil v1.0.5 // indirect
github.com/cosmos/cosmos-db v1.0.2 // indirect
Expand Down
4 changes: 2 additions & 2 deletions e2e/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -958,8 +958,8 @@ github.com/cockroachdb/redact v1.1.5/go.mod h1:BVNblN9mBWFyMyqK1k3AAiSxhvhfK2oOZ
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 h1:zuQyyAKVxetITBuuhv3BI9cMrmStnpT18zmgmTxunpo=
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06/go.mod h1:7nc4anLGjupUW/PeY5qiNYsdNXj7zopG+eqsS7To5IQ=
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=
github.com/cometbft/cometbft v0.38.6 h1:QSgpCzrGWJ2KUq1qpw+FCfASRpE27T6LQbfEHscdyOk=
github.com/cometbft/cometbft v0.38.6/go.mod h1:8rSPxzUJYquCN8uuBgbUHOMg2KAwvr7CyUw+6ukO4nw=
github.com/cometbft/cometbft v0.38.7 h1:ULhIOJ9+LgSy6nLekhq9ae3juX3NnQUMMPyVdhZV6Hk=
github.com/cometbft/cometbft v0.38.7/go.mod h1:HIyf811dFMI73IE0F7RrnY/Fr+d1+HuJAgtkEpQjCMY=
github.com/cometbft/cometbft-db v0.9.1 h1:MIhVX5ja5bXNHF8EYrThkG9F7r9kSfv8BX4LWaxWJ4M=
github.com/cometbft/cometbft-db v0.9.1/go.mod h1:iliyWaoV0mRwBJoizElCwwRA9Tf7jZJOURcRZF9m60U=
github.com/containerd/continuity v0.3.0 h1:nisirsYROK15TAMVukJOUyGJjz4BNQJBVsNvAXZJ/eg=
Expand Down
29 changes: 20 additions & 9 deletions e2e/helpers/action_builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"context"
"encoding/json"
"fmt"
"strings"

"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v8/testutil"
)
Expand Down Expand Up @@ -33,34 +35,43 @@ func ExecuteExec(ctx context.Context, chain *cosmos.CosmosChain, cmd []string, i
}

fmt.Println(string(stdout))
if err := json.Unmarshal(stdout, &i); err != nil {
fmt.Println(err)
if err := chain.GetCodec().UnmarshalInterface(stdout, &i); err != nil {
// If the codec can not properly unmarshal, then it may be a standard json Unmarshal request.
// This is required since we are ExecuteExec'ing an interface{} instead of some concrete type.
if strings.Contains(err.Error(), "illegal wireType") {
if err2 := json.Unmarshal(stdout, &i); err2 != nil {
fmt.Println(err2)
}
} else {
fmt.Println(err)
}

}
}

// Executes a command from CommandBuilder
func ExecuteTransaction(ctx context.Context, chain *cosmos.CosmosChain, cmd []string) (TxResponse, error) {
func ExecuteTransaction(ctx context.Context, chain *cosmos.CosmosChain, cmd []string) (sdk.TxResponse, error) {
var err error
var stdout []byte

stdout, _, err = chain.Exec(ctx, cmd, nil)
if err != nil {
return TxResponse{}, err
return sdk.TxResponse{}, err
}

if err := testutil.WaitForBlocks(ctx, waitForBlocks, chain); err != nil {
return TxResponse{}, err
return sdk.TxResponse{}, err
}

var res TxResponse
if err := json.Unmarshal(stdout, &res); err != nil {
return TxResponse{}, err
var res sdk.TxResponse
if err := chain.GetCodec().UnmarshalJSON(stdout, &res); err != nil {
return sdk.TxResponse{}, err
}

return res, err
}

func ExecuteTransactionNoError(ctx context.Context, chain *cosmos.CosmosChain, cmd []string) TxResponse {
func ExecuteTransactionNoError(ctx context.Context, chain *cosmos.CosmosChain, cmd []string) sdk.TxResponse {
res, _ := ExecuteTransaction(ctx, chain, cmd)
return res
}
Expand Down
5 changes: 3 additions & 2 deletions e2e/helpers/authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"strings"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v8/ibc"
)

func ExecuteAuthzGrantMsg(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, granter ibc.Wallet, grantee ibc.Wallet, msgType string) (TxResponse, error) {
func ExecuteAuthzGrantMsg(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, granter ibc.Wallet, grantee ibc.Wallet, msgType string) (sdk.TxResponse, error) {
if !strings.HasPrefix(msgType, "/") {
msgType = "/" + msgType
}
Expand All @@ -19,7 +20,7 @@ func ExecuteAuthzGrantMsg(t *testing.T, ctx context.Context, chain *cosmos.Cosmo
return ExecuteTransaction(ctx, chain, cmd)
}

func ExecuteAuthzExecMsg(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, grantee ibc.Wallet, nestedMsgCmd []string) (TxResponse, error) {
func ExecuteAuthzExecMsg(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, grantee ibc.Wallet, nestedMsgCmd []string) (sdk.TxResponse, error) {
// generate the message to JSON, then exec the message
fileName := "authz.json"
node := chain.GetNode()
Expand Down
13 changes: 7 additions & 6 deletions e2e/helpers/poa.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,33 @@ import (
"fmt"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v8/ibc"
"github.com/strangelove-ventures/poa"
"github.com/stretchr/testify/require"
)

func POASetPower(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, user ibc.Wallet, valoper string, power int64, flags ...string) (TxResponse, error) {
func POASetPower(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, user ibc.Wallet, valoper string, power int64, flags ...string) (sdk.TxResponse, error) {
cmd := TxCommandBuilder(ctx, chain, []string{"tx", "poa", "set-power", valoper, fmt.Sprintf("%d", power)}, user.KeyName(), flags...)
return ExecuteTransaction(ctx, chain, cmd)
}

func POARemove(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, user ibc.Wallet, valoper string) (TxResponse, error) {
func POARemove(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, user ibc.Wallet, valoper string) (sdk.TxResponse, error) {
cmd := TxCommandBuilder(ctx, chain, []string{"tx", "poa", "remove", valoper}, user.KeyName())
return ExecuteTransaction(ctx, chain, cmd)
}

func POARemovePending(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, user ibc.Wallet, valoper string) (TxResponse, error) {
func POARemovePending(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, user ibc.Wallet, valoper string) (sdk.TxResponse, error) {
cmd := TxCommandBuilder(ctx, chain, []string{"tx", "poa", "remove-pending", valoper}, user.KeyName())
return ExecuteTransaction(ctx, chain, cmd)
}

func POACreatePendingValidator(
t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, user ibc.Wallet,
ed25519PubKey string, moniker string, commissionRate string, commissionMaxRate string, commissionMaxChangeRate string,
) (TxResponse, error) {
) (sdk.TxResponse, error) {
file := "validator_file.json"

content := fmt.Sprintf(`{
Expand Down Expand Up @@ -106,7 +107,7 @@ func GetPOAPending(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain)
return res
}

func POAUpdateParams(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, user ibc.Wallet, admins []string, gracefulExit bool) (TxResponse, error) {
func POAUpdateParams(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, user ibc.Wallet, admins []string, gracefulExit bool) (sdk.TxResponse, error) {
// admin1,admin2,admin3
adminList := ""
for _, admin := range admins {
Expand All @@ -123,7 +124,7 @@ func POAUpdateParams(t *testing.T, ctx context.Context, chain *cosmos.CosmosChai
return ExecuteTransaction(ctx, chain, cmd)
}

func POAUpdateStakingParams(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, user ibc.Wallet, sp stakingtypes.Params) (TxResponse, error) {
func POAUpdateStakingParams(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, user ibc.Wallet, sp stakingtypes.Params) (sdk.TxResponse, error) {
command := []string{"tx", "poa", "update-staking-params",
sp.UnbondingTime.String(),
fmt.Sprintf("%d", sp.MaxValidators),
Expand Down
5 changes: 3 additions & 2 deletions e2e/helpers/staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import (
"context"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v8/ibc"
)

func StakeTokens(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, user ibc.Wallet, valoper, coinAmt string) (TxResponse, error) {
func StakeTokens(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, user ibc.Wallet, valoper, coinAmt string) (sdk.TxResponse, error) {
cmd := TxCommandBuilder(ctx, chain, []string{"tx", "staking", "delegate", valoper, coinAmt}, user.KeyName())
return ExecuteTransaction(ctx, chain, cmd)
}

func ClaimStakingRewards(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, user ibc.Wallet, valoper string) (TxResponse, error) {
func ClaimStakingRewards(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, user ibc.Wallet, valoper string) (sdk.TxResponse, error) {
cmd := TxCommandBuilder(ctx, chain, []string{"tx", "distribution", "withdraw-rewards", valoper}, user.KeyName())
return ExecuteTransaction(ctx, chain, cmd)
}
Expand Down
60 changes: 0 additions & 60 deletions e2e/helpers/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,66 +90,6 @@ type BlockData struct {
} `json:"last_commit"`
}

// sdk v50
type TxResponse struct {
Height string `json:"height"`
Txhash string `json:"txhash"`
Codespace string `json:"codespace"`
Code int `json:"code"`
Data string `json:"data"`
RawLog string `json:"raw_log"`
Logs []any `json:"logs"`
Info string `json:"info"`
GasWanted string `json:"gas_wanted"`
GasUsed string `json:"gas_used"`
Tx struct {
Type string `json:"@type"`
Body struct {
Messages []struct {
Type string `json:"@type"`
FromAddress string `json:"from_address"`
ValidatorAddress string `json:"validator_address"`
Power string `json:"power"`
} `json:"messages"`
Memo string `json:"memo"`
TimeoutHeight string `json:"timeout_height"`
ExtensionOptions []any `json:"extension_options"`
NonCriticalExtensionOptions []any `json:"non_critical_extension_options"`
} `json:"body"`
AuthInfo struct {
SignerInfos []struct {
PublicKey struct {
Type string `json:"@type"`
Key string `json:"key"`
} `json:"public_key"`
ModeInfo struct {
Single struct {
Mode string `json:"mode"`
} `json:"single"`
} `json:"mode_info"`
Sequence string `json:"sequence"`
} `json:"signer_infos"`
Fee struct {
Amount []any `json:"amount"`
GasLimit string `json:"gas_limit"`
Payer string `json:"payer"`
Granter string `json:"granter"`
} `json:"fee"`
Tip any `json:"tip"`
} `json:"auth_info"`
Signatures []string `json:"signatures"`
} `json:"tx"`
Timestamp string `json:"timestamp"`
Events []struct {
Type string `json:"type"`
Attributes []struct {
Key string `json:"key"`
Value string `json:"value"`
Index bool `json:"index"`
} `json:"attributes"`
} `json:"events"`
}

type POAConsensusPower struct {
Power string `json:"consensus_power"`
}
Expand Down
11 changes: 6 additions & 5 deletions e2e/poa_gov_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/strangelove-ventures/interchaintest/v8"
"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v8/ibc"
Expand Down Expand Up @@ -85,7 +86,7 @@ func testGovernance(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain
}

func testUpdatePOAParams(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, acc0, incorrectUser ibc.Wallet) {
var tx helpers.TxResponse
var tx sdk.TxResponse
var err error

t.Log("\n===== TEST UPDATE POA PARAMS =====")
Expand All @@ -95,7 +96,7 @@ func testUpdatePOAParams(t *testing.T, ctx context.Context, chain *cosmos.Cosmos
if err != nil {
t.Fatal(err)
}
txRes, err := chain.GetTransaction(tx.Txhash)
txRes, err := chain.GetTransaction(tx.TxHash)
require.NoError(t, err)
fmt.Printf("%+v", txRes)
require.Contains(t, txRes.RawLog, poa.ErrNotAnAuthority.Error())
Expand All @@ -106,7 +107,7 @@ func testUpdatePOAParams(t *testing.T, ctx context.Context, chain *cosmos.Cosmos
if err != nil {
t.Fatal(err)
}
txRes, err := chain.GetTransaction(tx.Txhash)
txRes, err := chain.GetTransaction(tx.TxHash)
require.NoError(t, err)
fmt.Printf("%+v", txRes)
require.EqualValues(t, txRes.Code, 3)
Expand All @@ -129,7 +130,7 @@ func testUpdatePOAParams(t *testing.T, ctx context.Context, chain *cosmos.Cosmos
t.Fatal(err)
}

txRes, err := chain.GetTransaction(tx.Txhash)
txRes, err := chain.GetTransaction(tx.TxHash)
require.NoError(t, err)
fmt.Printf("%+v", txRes)
require.EqualValues(t, txRes.Code, 0)
Expand All @@ -145,7 +146,7 @@ func testUpdatePOAParams(t *testing.T, ctx context.Context, chain *cosmos.Cosmos
if err != nil {
t.Fatal(err)
}
txRes, err := chain.GetTransaction(tx.Txhash)
txRes, err := chain.GetTransaction(tx.TxHash)
require.NoError(t, err)
fmt.Printf("%+v", txRes)
require.EqualValues(t, txRes.Code, 0)
Expand Down
7 changes: 4 additions & 3 deletions e2e/poa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/strangelove-ventures/interchaintest/v8"
"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v8/ibc"
Expand Down Expand Up @@ -91,13 +92,13 @@ func testRemovePending(t *testing.T, ctx context.Context, chain *cosmos.CosmosCh

func testPowerErrors(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, validators []string, incorrectUser ibc.Wallet, admin ibc.Wallet) {
t.Log("\n===== TEST POWER ERRORS =====")
var res helpers.TxResponse
var res sdk.TxResponse
var err error

t.Run("fail: set-power message from a non authorized user", func(t *testing.T) {
// runtime error: index out of range [1] with length 1 [recovered]
res, _ = helpers.POASetPower(t, ctx, chain, incorrectUser, validators[0], 1_000_000)
res, err := chain.GetTransaction(res.Txhash)
res, err := chain.GetTransaction(res.TxHash)
require.NoError(t, err)
require.Contains(t, res.RawLog, poa.ErrNotAnAuthority.Error())
})
Expand All @@ -110,7 +111,7 @@ func testPowerErrors(t *testing.T, ctx context.Context, chain *cosmos.CosmosChai

t.Run("fail: set-power message above 30%% without unsafe flag", func(t *testing.T) {
res, _ = helpers.POASetPower(t, ctx, chain, admin, validators[0], 9_000_000_000_000_000)
res, err := chain.GetTransaction(res.Txhash)
res, err := chain.GetTransaction(res.TxHash)
require.NoError(t, err)
require.Contains(t, res.RawLog, poa.ErrUnsafePower.Error())
})
Expand Down
Loading

0 comments on commit 792cc70

Please sign in to comment.