0g-chain/cli_test/test_helpers.go

800 lines
28 KiB
Go
Raw Normal View History

2019-06-25 12:01:02 +00:00
package clitest
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
2019-06-25 12:01:02 +00:00
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
2019-09-12 16:19:13 +00:00
tmtypes "github.com/tendermint/tendermint/types"
2019-06-25 12:01:02 +00:00
clientkeys "github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys"
"github.com/cosmos/cosmos-sdk/server"
2019-09-12 16:19:13 +00:00
"github.com/cosmos/cosmos-sdk/simapp"
2019-06-25 12:01:02 +00:00
"github.com/cosmos/cosmos-sdk/tests"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
2019-09-12 16:19:13 +00:00
"github.com/cosmos/cosmos-sdk/x/distribution"
2019-06-25 12:01:02 +00:00
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/staking"
2019-06-25 12:50:25 +00:00
2024-05-01 03:17:24 +00:00
"github.com/0glabs/0g-chain/app"
2019-06-25 12:01:02 +00:00
)
const (
denom = "stake"
keyFoo = "foo"
keyBar = "bar"
fooDenom = "footoken"
feeDenom = "feetoken"
fee2Denom = "fee2token"
keyBaz = "baz"
keyVesting = "vesting"
keyFooBarBaz = "foobarbaz"
)
var (
2019-09-12 16:19:13 +00:00
totalCoins = sdk.NewCoins(
sdk.NewCoin(fee2Denom, sdk.TokensFromConsensusPower(2000000)),
sdk.NewCoin(feeDenom, sdk.TokensFromConsensusPower(2000000)),
sdk.NewCoin(fooDenom, sdk.TokensFromConsensusPower(2000)),
sdk.NewCoin(denom, sdk.TokensFromConsensusPower(300).Add(sdkmath.NewInt(12))), // add coins from inflation
2019-09-12 16:19:13 +00:00
)
2019-06-25 12:01:02 +00:00
2019-09-12 16:19:13 +00:00
startCoins = sdk.NewCoins(
sdk.NewCoin(fee2Denom, sdk.TokensFromConsensusPower(1000000)),
sdk.NewCoin(feeDenom, sdk.TokensFromConsensusPower(1000000)),
sdk.NewCoin(fooDenom, sdk.TokensFromConsensusPower(1000)),
sdk.NewCoin(denom, sdk.TokensFromConsensusPower(150)),
)
vestingCoins = sdk.NewCoins(
sdk.NewCoin(feeDenom, sdk.TokensFromConsensusPower(500000)),
)
2019-06-25 12:01:02 +00:00
)
2019-06-25 13:30:06 +00:00
func init() {
// set the address prefixes
2024-05-01 08:07:32 +00:00
chaincfg.SetSDKConfig()
2020-04-27 17:28:42 +00:00
// config.Seal()
2019-06-25 13:30:06 +00:00
}
2019-06-25 12:01:02 +00:00
//___________________________________________________________________________________
// Fixtures
// Fixtures is used to setup the testing environment
type Fixtures struct {
2020-04-28 14:10:02 +00:00
BuildDir string
RootDir string
KvdBinary string
KvcliBinary string
ChainID string
RPCAddr string
Port string
KvdHome string
KvcliHome string
P2PAddr string
T *testing.T
2020-04-27 17:16:18 +00:00
cdc *codec.Codec
2019-06-25 12:01:02 +00:00
}
// NewFixtures creates a new instance of Fixtures with many vars set
func NewFixtures(t *testing.T) *Fixtures {
2024-05-01 05:53:58 +00:00
tmpDir, err := ioutil.TempDir("", "0gchain_integration_"+t.Name()+"_")
2019-06-25 12:01:02 +00:00
require.NoError(t, err)
servAddr, port, err := server.FreeTCPAddr()
require.NoError(t, err)
p2pAddr, _, err := server.FreeTCPAddr()
require.NoError(t, err)
2019-09-12 16:19:13 +00:00
buildDir := os.Getenv("BUILDDIR")
if buildDir == "" {
buildDir, err = filepath.Abs(filepath.Join("../build/", runtime.GOOS))
2019-09-12 16:19:13 +00:00
require.NoError(t, err)
}
2020-04-27 17:16:18 +00:00
cdc := app.MakeCodec()
2019-06-25 12:01:02 +00:00
return &Fixtures{
2020-04-28 14:10:02 +00:00
T: t,
BuildDir: buildDir,
RootDir: tmpDir,
KvdBinary: filepath.Join(buildDir, "kvd"),
KvcliBinary: filepath.Join(buildDir, "kvcli"),
KvdHome: filepath.Join(tmpDir, ".kvd"),
KvcliHome: filepath.Join(tmpDir, ".kvcli"),
RPCAddr: servAddr,
P2PAddr: p2pAddr,
Port: port,
cdc: cdc,
2019-06-25 12:01:02 +00:00
}
}
// GenesisFile returns the path of the genesis file
func (f Fixtures) GenesisFile() string {
2020-04-28 14:10:02 +00:00
return filepath.Join(f.KvdHome, "config", "genesis.json")
2019-06-25 12:01:02 +00:00
}
2020-04-27 17:16:18 +00:00
// GenesisState returns the application's genesis state
2019-09-12 16:19:13 +00:00
func (f Fixtures) GenesisState() simapp.GenesisState {
2019-06-25 12:01:02 +00:00
cdc := codec.New()
2019-09-12 16:19:13 +00:00
genDoc, err := tmtypes.GenesisDocFromFile(f.GenesisFile())
2019-06-25 12:01:02 +00:00
require.NoError(f.T, err)
2019-09-12 16:19:13 +00:00
var appState simapp.GenesisState
2019-06-25 12:01:02 +00:00
require.NoError(f.T, cdc.UnmarshalJSON(genDoc.AppState, &appState))
return appState
}
// InitFixtures is called at the beginning of a test and initializes a chain
// with 1 validator.
func InitFixtures(t *testing.T) (f *Fixtures) {
f = NewFixtures(t)
// reset test state
f.UnsafeResetAll()
2020-04-28 18:04:30 +00:00
f.CLIConfig("keyring-backend", "test")
2019-06-25 12:01:02 +00:00
// ensure keystore has foo and bar keys
f.KeysDelete(keyFoo)
f.KeysDelete(keyBar)
f.KeysDelete(keyBar)
f.KeysDelete(keyFooBarBaz)
f.KeysAdd(keyFoo)
f.KeysAdd(keyBar)
f.KeysAdd(keyBaz)
f.KeysAdd(keyVesting)
f.KeysAdd(keyFooBarBaz, "--multisig-threshold=2", fmt.Sprintf(
"--multisig=%s,%s,%s", keyFoo, keyBar, keyBaz))
// ensure that CLI output is in JSON format
f.CLIConfig("output", "json")
2020-04-28 14:27:12 +00:00
// NOTE: KvInit sets the ChainID
f.KvInit(keyFoo)
2019-06-25 12:01:02 +00:00
f.CLIConfig("chain-id", f.ChainID)
f.CLIConfig("broadcast-mode", "block")
2019-09-12 16:19:13 +00:00
f.CLIConfig("trust-node", "true")
2019-06-25 12:01:02 +00:00
// start an account with tokens
f.AddGenesisAccount(f.KeyAddress(keyFoo), startCoins)
f.AddGenesisAccount(
f.KeyAddress(keyVesting), startCoins,
fmt.Sprintf("--vesting-amount=%s", vestingCoins),
fmt.Sprintf("--vesting-start-time=%d", time.Now().UTC().UnixNano()),
fmt.Sprintf("--vesting-end-time=%d", time.Now().Add(60*time.Second).UTC().UnixNano()),
)
f.GenTx(keyFoo)
f.CollectGenTxs()
return
}
// Cleanup is meant to be run at the end of a test to clean up an remaining test state
func (f *Fixtures) Cleanup(dirs ...string) {
2019-09-12 16:19:13 +00:00
clean := append(dirs, f.RootDir)
2019-06-25 12:01:02 +00:00
for _, d := range clean {
2019-09-12 16:19:13 +00:00
require.NoError(f.T, os.RemoveAll(d))
2019-06-25 12:01:02 +00:00
}
}
// Flags returns the flags necessary for making most CLI calls
func (f *Fixtures) Flags() string {
2020-04-28 14:10:02 +00:00
return fmt.Sprintf("--home=%s --node=%s", f.KvcliHome, f.RPCAddr)
2019-06-25 12:01:02 +00:00
}
//___________________________________________________________________________________
2024-05-01 05:53:58 +00:00
// 0gchaind
2019-06-25 12:01:02 +00:00
2024-05-01 05:53:58 +00:00
// UnsafeResetAll is 0gchaind unsafe-reset-all
2019-06-25 12:01:02 +00:00
func (f *Fixtures) UnsafeResetAll(flags ...string) {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s --home=%s unsafe-reset-all", f.KvdBinary, f.KvdHome)
2019-06-25 12:01:02 +00:00
executeWrite(f.T, addFlags(cmd, flags))
2020-04-28 14:10:02 +00:00
err := os.RemoveAll(filepath.Join(f.KvdHome, "config", "gentx"))
2019-06-25 12:01:02 +00:00
require.NoError(f.T, err)
}
2024-05-01 05:53:58 +00:00
// KvInit is 0gchaind init
2020-04-28 14:27:12 +00:00
// NOTE: KvInit sets the ChainID for the Fixtures instance
func (f *Fixtures) KvInit(moniker string, flags ...string) {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s init -o --home=%s %s", f.KvdBinary, f.KvdHome, moniker)
_, stderr := tests.ExecuteT(f.T, addFlags(cmd, flags), clientkeys.DefaultKeyPass)
2019-06-25 12:01:02 +00:00
var chainID string
var initRes map[string]json.RawMessage
err := json.Unmarshal([]byte(stderr), &initRes)
require.NoError(f.T, err)
err = json.Unmarshal(initRes["chain_id"], &chainID)
require.NoError(f.T, err)
f.ChainID = chainID
}
2024-05-01 05:53:58 +00:00
// AddGenesisAccount is 0gchaind add-genesis-account
2019-06-25 12:01:02 +00:00
func (f *Fixtures) AddGenesisAccount(address sdk.AccAddress, coins sdk.Coins, flags ...string) {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s add-genesis-account %s %s --home=%s --keyring-backend=test", f.KvdBinary, address, coins, f.KvdHome)
2019-06-25 12:01:02 +00:00
executeWriteCheckErr(f.T, addFlags(cmd, flags))
}
2024-05-01 05:53:58 +00:00
// GenTx is 0gchaind gentx
2019-06-25 12:01:02 +00:00
func (f *Fixtures) GenTx(name string, flags ...string) {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s gentx --name=%s --home=%s --home-client=%s --keyring-backend=test", f.KvdBinary, name, f.KvdHome, f.KvcliHome)
2020-04-27 17:16:18 +00:00
executeWriteCheckErr(f.T, addFlags(cmd, flags))
2019-06-25 12:01:02 +00:00
}
2024-05-01 05:53:58 +00:00
// CollectGenTxs is 0gchaind collect-gentxs
2019-06-25 12:01:02 +00:00
func (f *Fixtures) CollectGenTxs(flags ...string) {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s collect-gentxs --home=%s", f.KvdBinary, f.KvdHome)
2020-04-27 17:16:18 +00:00
executeWriteCheckErr(f.T, addFlags(cmd, flags))
2019-06-25 12:01:02 +00:00
}
2024-05-01 05:53:58 +00:00
// GDStart runs 0gchaind start with the appropriate flags and returns a process
2019-06-25 12:01:02 +00:00
func (f *Fixtures) GDStart(flags ...string) *tests.Process {
cmd := fmt.Sprintf("%s start --home=%s --rpc.laddr=%v --p2p.laddr=%v --pruning=everything", f.KvdBinary, f.KvdHome, f.RPCAddr, f.P2PAddr)
2019-06-25 12:01:02 +00:00
proc := tests.GoExecuteTWithStdout(f.T, addFlags(cmd, flags))
tests.WaitForTMStart(f.Port)
tests.WaitForNextNBlocksTM(1, f.Port)
return proc
}
2024-05-01 05:53:58 +00:00
// GDTendermint returns the results of 0gchaind tendermint [query]
2019-06-25 12:01:02 +00:00
func (f *Fixtures) GDTendermint(query string) string {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s tendermint %s --home=%s", f.KvdBinary, query, f.KvdHome)
2019-06-25 12:01:02 +00:00
success, stdout, stderr := executeWriteRetStdStreams(f.T, cmd)
require.Empty(f.T, stderr)
require.True(f.T, success)
return strings.TrimSpace(stdout)
}
2024-05-01 05:53:58 +00:00
// ValidateGenesis runs 0gchaind validate-genesis
2019-06-25 12:01:02 +00:00
func (f *Fixtures) ValidateGenesis() {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s validate-genesis --home=%s", f.KvdBinary, f.KvdHome)
2019-06-25 12:01:02 +00:00
executeWriteCheckErr(f.T, cmd)
}
//___________________________________________________________________________________
2020-04-28 14:10:02 +00:00
// kvcli keys
2019-06-25 12:01:02 +00:00
2020-04-28 14:10:02 +00:00
// KeysDelete is kvcli keys delete
2019-06-25 12:01:02 +00:00
func (f *Fixtures) KeysDelete(name string, flags ...string) {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s keys delete --keyring-backend=test --home=%s %s", f.KvcliBinary,
f.KvcliHome, name)
2019-06-25 12:01:02 +00:00
executeWrite(f.T, addFlags(cmd, append(append(flags, "-y"), "-f")))
}
2020-04-28 14:10:02 +00:00
// KeysAdd is kvcli keys add
2019-06-25 12:01:02 +00:00
func (f *Fixtures) KeysAdd(name string, flags ...string) {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s keys add --keyring-backend=test --home=%s %s", f.KvcliBinary,
f.KvcliHome, name)
2020-04-27 17:16:18 +00:00
executeWriteCheckErr(f.T, addFlags(cmd, flags))
2019-06-25 12:01:02 +00:00
}
2020-04-28 14:10:02 +00:00
// KeysAddRecover prepares kvcli keys add --recover
2019-06-25 12:01:02 +00:00
func (f *Fixtures) KeysAddRecover(name, mnemonic string, flags ...string) (exitSuccess bool, stdout, stderr string) {
2020-04-27 17:16:18 +00:00
cmd := fmt.Sprintf("%s keys add --keyring-backend=test --home=%s --recover %s",
2020-04-28 14:10:02 +00:00
f.KvcliBinary, f.KvcliHome, name)
2020-04-27 17:16:18 +00:00
return executeWriteRetStdStreams(f.T, addFlags(cmd, flags), mnemonic)
2019-06-25 12:01:02 +00:00
}
2020-04-28 14:10:02 +00:00
// KeysAddRecoverHDPath prepares kvcli keys add --recover --account --index
2019-06-25 12:01:02 +00:00
func (f *Fixtures) KeysAddRecoverHDPath(name, mnemonic string, account uint32, index uint32, flags ...string) {
2020-04-27 17:16:18 +00:00
cmd := fmt.Sprintf("%s keys add --keyring-backend=test --home=%s --recover %s --account %d"+
2020-04-28 14:10:02 +00:00
" --index %d", f.KvcliBinary, f.KvcliHome, name, account, index)
2020-04-27 17:16:18 +00:00
executeWriteCheckErr(f.T, addFlags(cmd, flags), mnemonic)
2019-06-25 12:01:02 +00:00
}
2020-04-28 14:10:02 +00:00
// KeysShow is kvcli keys show
2019-06-25 12:01:02 +00:00
func (f *Fixtures) KeysShow(name string, flags ...string) keys.KeyOutput {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s keys show --keyring-backend=test --home=%s %s", f.KvcliBinary,
f.KvcliHome, name)
2019-06-25 12:01:02 +00:00
out, _ := tests.ExecuteT(f.T, addFlags(cmd, flags), "")
var ko keys.KeyOutput
err := clientkeys.UnmarshalJSON([]byte(out), &ko)
require.NoError(f.T, err)
return ko
}
// KeyAddress returns the SDK account address from the key
func (f *Fixtures) KeyAddress(name string) sdk.AccAddress {
ko := f.KeysShow(name)
accAddr, err := sdk.AccAddressFromBech32(ko.Address)
require.NoError(f.T, err)
return accAddr
}
//___________________________________________________________________________________
2020-04-28 14:10:02 +00:00
// kvcli config
2019-06-25 12:01:02 +00:00
2020-04-28 14:10:02 +00:00
// CLIConfig is kvcli config
2019-06-25 12:01:02 +00:00
func (f *Fixtures) CLIConfig(key, value string, flags ...string) {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s config --home=%s %s %s", f.KvcliBinary, f.KvcliHome, key, value)
2019-06-25 12:01:02 +00:00
executeWriteCheckErr(f.T, addFlags(cmd, flags))
}
//___________________________________________________________________________________
2020-04-28 14:10:02 +00:00
// kvcli tx send/sign/broadcast
2019-06-25 12:01:02 +00:00
2020-04-28 14:10:02 +00:00
// Status is kvcli status
2020-04-27 17:16:18 +00:00
func (f *Fixtures) Status(flags ...string) (bool, string, string) {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s status %s", f.KvcliBinary, f.Flags())
2020-04-27 17:16:18 +00:00
return executeWriteRetStdStreams(f.T, addFlags(cmd, flags), clientkeys.DefaultKeyPass)
}
2020-04-28 14:10:02 +00:00
// TxSend is kvcli tx send
2019-06-25 12:01:02 +00:00
func (f *Fixtures) TxSend(from string, to sdk.AccAddress, amount sdk.Coin, flags ...string) (bool, string, string) {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s tx send --keyring-backend=test %s %s %s %v", f.KvcliBinary, from,
2020-04-27 17:16:18 +00:00
to, amount, f.Flags())
return executeWriteRetStdStreams(f.T, addFlags(cmd, flags), clientkeys.DefaultKeyPass)
2019-06-25 12:01:02 +00:00
}
2020-04-28 14:10:02 +00:00
// TxSign is kvcli tx sign
2019-06-25 12:01:02 +00:00
func (f *Fixtures) TxSign(signer, fileName string, flags ...string) (bool, string, string) {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s tx sign %v --keyring-backend=test --from=%s %v", f.KvcliBinary,
2020-04-27 17:16:18 +00:00
f.Flags(), signer, fileName)
return executeWriteRetStdStreams(f.T, addFlags(cmd, flags), clientkeys.DefaultKeyPass)
2019-06-25 12:01:02 +00:00
}
2020-04-28 14:10:02 +00:00
// TxBroadcast is kvcli tx broadcast
2019-06-25 12:01:02 +00:00
func (f *Fixtures) TxBroadcast(fileName string, flags ...string) (bool, string, string) {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s tx broadcast %v %v", f.KvcliBinary, f.Flags(), fileName)
return executeWriteRetStdStreams(f.T, addFlags(cmd, flags), clientkeys.DefaultKeyPass)
2019-06-25 12:01:02 +00:00
}
2020-04-28 14:10:02 +00:00
// TxEncode is kvcli tx encode
2019-06-25 12:01:02 +00:00
func (f *Fixtures) TxEncode(fileName string, flags ...string) (bool, string, string) {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s tx encode %v %v", f.KvcliBinary, f.Flags(), fileName)
return executeWriteRetStdStreams(f.T, addFlags(cmd, flags), clientkeys.DefaultKeyPass)
2019-06-25 12:01:02 +00:00
}
2020-04-28 14:10:02 +00:00
// TxMultisign is kvcli tx multisign
2019-06-25 12:01:02 +00:00
func (f *Fixtures) TxMultisign(fileName, name string, signaturesFiles []string,
flags ...string,
) (bool, string, string) {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s tx multisign --keyring-backend=test %v %s %s %s", f.KvcliBinary, f.Flags(),
2019-06-25 12:01:02 +00:00
fileName, name, strings.Join(signaturesFiles, " "),
)
2020-04-27 17:16:18 +00:00
return executeWriteRetStdStreams(f.T, addFlags(cmd, flags))
2019-06-25 12:01:02 +00:00
}
//___________________________________________________________________________________
2020-04-28 14:10:02 +00:00
// kvcli tx staking
2019-06-25 12:01:02 +00:00
2020-04-28 14:10:02 +00:00
// TxStakingCreateValidator is kvcli tx staking create-validator
2019-06-25 12:01:02 +00:00
func (f *Fixtures) TxStakingCreateValidator(from, consPubKey string, amount sdk.Coin, flags ...string) (bool, string, string) {
2020-04-27 17:16:18 +00:00
cmd := fmt.Sprintf("%s tx staking create-validator %v --keyring-backend=test --from=%s"+
2020-04-28 14:10:02 +00:00
" --pubkey=%s", f.KvcliBinary, f.Flags(), from, consPubKey)
2019-06-25 12:01:02 +00:00
cmd += fmt.Sprintf(" --amount=%v --moniker=%v --commission-rate=%v", amount, from, "0.05")
cmd += fmt.Sprintf(" --commission-max-rate=%v --commission-max-change-rate=%v", "0.20", "0.10")
cmd += fmt.Sprintf(" --min-self-delegation=%v", "1")
return executeWriteRetStdStreams(f.T, addFlags(cmd, flags), clientkeys.DefaultKeyPass)
2019-06-25 12:01:02 +00:00
}
2020-04-28 14:10:02 +00:00
// TxStakingUnbond is kvcli tx staking unbond
2019-06-25 12:01:02 +00:00
func (f *Fixtures) TxStakingUnbond(from, shares string, validator sdk.ValAddress, flags ...string) bool {
2020-04-27 17:16:18 +00:00
cmd := fmt.Sprintf("%s tx staking unbond --keyring-backend=test %s %v --from=%s %v",
2020-04-28 14:10:02 +00:00
f.KvcliBinary, validator, shares, from, f.Flags())
return executeWrite(f.T, addFlags(cmd, flags), clientkeys.DefaultKeyPass)
2019-06-25 12:01:02 +00:00
}
//___________________________________________________________________________________
2020-04-28 14:10:02 +00:00
// kvcli tx gov
2019-06-25 12:01:02 +00:00
2020-04-28 14:10:02 +00:00
// TxGovSubmitProposal is kvcli tx gov submit-proposal
2019-06-25 12:01:02 +00:00
func (f *Fixtures) TxGovSubmitProposal(from, typ, title, description string, deposit sdk.Coin, flags ...string) (bool, string, string) {
2020-04-27 17:16:18 +00:00
cmd := fmt.Sprintf("%s tx gov submit-proposal %v --keyring-backend=test --from=%s --type=%s",
2020-04-28 14:10:02 +00:00
f.KvcliBinary, f.Flags(), from, typ)
2019-06-25 12:01:02 +00:00
cmd += fmt.Sprintf(" --title=%s --description=%s --deposit=%s", title, description, deposit)
return executeWriteRetStdStreams(f.T, addFlags(cmd, flags), clientkeys.DefaultKeyPass)
2019-06-25 12:01:02 +00:00
}
2020-04-28 14:10:02 +00:00
// TxGovDeposit is kvcli tx gov deposit
2019-06-25 12:01:02 +00:00
func (f *Fixtures) TxGovDeposit(proposalID int, from string, amount sdk.Coin, flags ...string) (bool, string, string) {
2020-04-27 17:16:18 +00:00
cmd := fmt.Sprintf("%s tx gov deposit %d %s --keyring-backend=test --from=%s %v",
2020-04-28 14:10:02 +00:00
f.KvcliBinary, proposalID, amount, from, f.Flags())
return executeWriteRetStdStreams(f.T, addFlags(cmd, flags), clientkeys.DefaultKeyPass)
2019-06-25 12:01:02 +00:00
}
2020-04-28 14:10:02 +00:00
// TxGovVote is kvcli tx gov vote
2019-06-25 12:01:02 +00:00
func (f *Fixtures) TxGovVote(proposalID int, option gov.VoteOption, from string, flags ...string) (bool, string, string) {
2020-04-27 17:16:18 +00:00
cmd := fmt.Sprintf("%s tx gov vote %d %s --keyring-backend=test --from=%s %v",
2020-04-28 14:10:02 +00:00
f.KvcliBinary, proposalID, option, from, f.Flags())
return executeWriteRetStdStreams(f.T, addFlags(cmd, flags), clientkeys.DefaultKeyPass)
2019-09-12 16:19:13 +00:00
}
// TxGovSubmitParamChangeProposal executes a CLI parameter change proposal
// submission.
func (f *Fixtures) TxGovSubmitParamChangeProposal(
from, proposalPath string, deposit sdk.Coin, flags ...string,
) (bool, string, string) {
cmd := fmt.Sprintf(
2020-04-27 17:16:18 +00:00
"%s tx gov submit-proposal param-change %s --keyring-backend=test --from=%s %v",
2020-04-28 14:10:02 +00:00
f.KvcliBinary, proposalPath, from, f.Flags(),
2019-09-12 16:19:13 +00:00
)
return executeWriteRetStdStreams(f.T, addFlags(cmd, flags), clientkeys.DefaultKeyPass)
2019-09-12 16:19:13 +00:00
}
// TxGovSubmitCommunityPoolSpendProposal executes a CLI community pool spend proposal
// submission.
func (f *Fixtures) TxGovSubmitCommunityPoolSpendProposal(
from, proposalPath string, deposit sdk.Coin, flags ...string,
) (bool, string, string) {
cmd := fmt.Sprintf(
2020-04-27 17:16:18 +00:00
"%s tx gov submit-proposal community-pool-spend %s --keyring-backend=test --from=%s %v",
2020-04-28 14:10:02 +00:00
f.KvcliBinary, proposalPath, from, f.Flags(),
2019-09-12 16:19:13 +00:00
)
return executeWriteRetStdStreams(f.T, addFlags(cmd, flags), clientkeys.DefaultKeyPass)
2019-06-25 12:01:02 +00:00
}
//___________________________________________________________________________________
2020-04-28 14:10:02 +00:00
// kvcli query account
2019-06-25 12:01:02 +00:00
2020-04-28 14:10:02 +00:00
// QueryAccount is kvcli query account
2019-06-25 12:01:02 +00:00
func (f *Fixtures) QueryAccount(address sdk.AccAddress, flags ...string) auth.BaseAccount {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s query account %s %v", f.KvcliBinary, address, f.Flags())
2019-06-25 12:01:02 +00:00
out, _ := tests.ExecuteT(f.T, addFlags(cmd, flags), "")
var initRes map[string]json.RawMessage
err := json.Unmarshal([]byte(out), &initRes)
require.NoError(f.T, err, "out %v, err %v", out, err)
value := initRes["value"]
var acc auth.BaseAccount
cdc := codec.New()
codec.RegisterCrypto(cdc)
err = cdc.UnmarshalJSON(value, &acc)
require.NoError(f.T, err, "value %v, err %v", string(value), err)
return acc
}
//___________________________________________________________________________________
2020-04-28 14:10:02 +00:00
// kvcli query txs
2019-06-25 12:01:02 +00:00
2020-04-28 14:10:02 +00:00
// QueryTxs is kvcli query txs
2020-04-27 17:16:18 +00:00
func (f *Fixtures) QueryTxs(page, limit int, events ...string) *sdk.SearchTxsResult {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s query txs --page=%d --limit=%d --events='%s' %v", f.KvcliBinary, page, limit, queryEvents(events), f.Flags())
2019-06-25 12:01:02 +00:00
out, _ := tests.ExecuteT(f.T, cmd, "")
2019-09-12 16:19:13 +00:00
var result sdk.SearchTxsResult
2020-04-27 17:16:18 +00:00
err := f.cdc.UnmarshalJSON([]byte(out), &result)
2019-06-25 12:01:02 +00:00
require.NoError(f.T, err, "out %v\n, err %v", out, err)
2019-09-12 16:19:13 +00:00
return &result
2019-06-25 12:01:02 +00:00
}
// QueryTxsInvalid query txs with wrong parameters and compare expected error
2020-04-27 17:16:18 +00:00
func (f *Fixtures) QueryTxsInvalid(expectedErr error, page, limit int, events ...string) {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s query txs --page=%d --limit=%d --events='%s' %v", f.KvcliBinary, page, limit, queryEvents(events), f.Flags())
2019-06-25 12:01:02 +00:00
_, err := tests.ExecuteT(f.T, cmd, "")
require.EqualError(f.T, expectedErr, err)
}
//___________________________________________________________________________________
2020-04-28 14:10:02 +00:00
// kvcli query staking
2019-06-25 12:01:02 +00:00
2020-04-28 14:10:02 +00:00
// QueryStakingValidator is kvcli query staking validator
2019-06-25 12:01:02 +00:00
func (f *Fixtures) QueryStakingValidator(valAddr sdk.ValAddress, flags ...string) staking.Validator {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s query staking validator %s %v", f.KvcliBinary, valAddr, f.Flags())
2019-06-25 12:01:02 +00:00
out, _ := tests.ExecuteT(f.T, addFlags(cmd, flags), "")
var validator staking.Validator
2020-04-27 17:16:18 +00:00
err := f.cdc.UnmarshalJSON([]byte(out), &validator)
2019-06-25 12:01:02 +00:00
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return validator
}
2020-04-28 14:10:02 +00:00
// QueryStakingUnbondingDelegationsFrom is kvcli query staking unbonding-delegations-from
2019-06-25 12:01:02 +00:00
func (f *Fixtures) QueryStakingUnbondingDelegationsFrom(valAddr sdk.ValAddress, flags ...string) []staking.UnbondingDelegation {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s query staking unbonding-delegations-from %s %v", f.KvcliBinary, valAddr, f.Flags())
2019-06-25 12:01:02 +00:00
out, _ := tests.ExecuteT(f.T, addFlags(cmd, flags), "")
var ubds []staking.UnbondingDelegation
2020-04-27 17:16:18 +00:00
err := f.cdc.UnmarshalJSON([]byte(out), &ubds)
2019-06-25 12:01:02 +00:00
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return ubds
}
2020-04-28 14:10:02 +00:00
// QueryStakingDelegationsTo is kvcli query staking delegations-to
2019-06-25 12:01:02 +00:00
func (f *Fixtures) QueryStakingDelegationsTo(valAddr sdk.ValAddress, flags ...string) []staking.Delegation {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s query staking delegations-to %s %v", f.KvcliBinary, valAddr, f.Flags())
2019-06-25 12:01:02 +00:00
out, _ := tests.ExecuteT(f.T, addFlags(cmd, flags), "")
var delegations []staking.Delegation
2020-04-27 17:16:18 +00:00
err := f.cdc.UnmarshalJSON([]byte(out), &delegations)
2019-06-25 12:01:02 +00:00
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return delegations
}
2020-04-28 14:10:02 +00:00
// QueryStakingPool is kvcli query staking pool
2019-06-25 12:01:02 +00:00
func (f *Fixtures) QueryStakingPool(flags ...string) staking.Pool {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s query staking pool %v", f.KvcliBinary, f.Flags())
2019-06-25 12:01:02 +00:00
out, _ := tests.ExecuteT(f.T, addFlags(cmd, flags), "")
var pool staking.Pool
2020-04-27 17:16:18 +00:00
err := f.cdc.UnmarshalJSON([]byte(out), &pool)
2019-06-25 12:01:02 +00:00
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return pool
}
2020-04-28 14:10:02 +00:00
// QueryStakingParameters is kvcli query staking parameters
2019-06-25 12:01:02 +00:00
func (f *Fixtures) QueryStakingParameters(flags ...string) staking.Params {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s query staking params %v", f.KvcliBinary, f.Flags())
2019-06-25 12:01:02 +00:00
out, _ := tests.ExecuteT(f.T, addFlags(cmd, flags), "")
var params staking.Params
2020-04-27 17:16:18 +00:00
err := f.cdc.UnmarshalJSON([]byte(out), &params)
2019-06-25 12:01:02 +00:00
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return params
}
//___________________________________________________________________________________
2020-04-28 14:10:02 +00:00
// kvcli query gov
2019-06-25 12:01:02 +00:00
2020-04-28 14:10:02 +00:00
// QueryGovParamDeposit is kvcli query gov param deposit
2019-06-25 12:01:02 +00:00
func (f *Fixtures) QueryGovParamDeposit() gov.DepositParams {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s query gov param deposit %s", f.KvcliBinary, f.Flags())
2019-06-25 12:01:02 +00:00
out, _ := tests.ExecuteT(f.T, cmd, "")
var depositParam gov.DepositParams
2020-04-27 17:16:18 +00:00
err := f.cdc.UnmarshalJSON([]byte(out), &depositParam)
2019-06-25 12:01:02 +00:00
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return depositParam
}
2020-04-28 14:10:02 +00:00
// QueryGovParamVoting is kvcli query gov param voting
2019-06-25 12:01:02 +00:00
func (f *Fixtures) QueryGovParamVoting() gov.VotingParams {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s query gov param voting %s", f.KvcliBinary, f.Flags())
2019-06-25 12:01:02 +00:00
out, _ := tests.ExecuteT(f.T, cmd, "")
var votingParam gov.VotingParams
2020-04-27 17:16:18 +00:00
err := f.cdc.UnmarshalJSON([]byte(out), &votingParam)
2019-06-25 12:01:02 +00:00
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return votingParam
}
2020-04-28 14:10:02 +00:00
// QueryGovParamTallying is kvcli query gov param tallying
2019-06-25 12:01:02 +00:00
func (f *Fixtures) QueryGovParamTallying() gov.TallyParams {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s query gov param tallying %s", f.KvcliBinary, f.Flags())
2019-06-25 12:01:02 +00:00
out, _ := tests.ExecuteT(f.T, cmd, "")
var tallyingParam gov.TallyParams
2020-04-27 17:16:18 +00:00
err := f.cdc.UnmarshalJSON([]byte(out), &tallyingParam)
2019-06-25 12:01:02 +00:00
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return tallyingParam
}
2020-04-28 14:10:02 +00:00
// QueryGovProposals is kvcli query gov proposals
2019-06-25 12:01:02 +00:00
func (f *Fixtures) QueryGovProposals(flags ...string) gov.Proposals {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s query gov proposals %v", f.KvcliBinary, f.Flags())
2019-06-25 12:01:02 +00:00
stdout, stderr := tests.ExecuteT(f.T, addFlags(cmd, flags), "")
2019-09-12 16:19:13 +00:00
if strings.Contains(stderr, "no matching proposals found") {
2019-06-25 12:01:02 +00:00
return gov.Proposals{}
}
require.Empty(f.T, stderr)
var out gov.Proposals
2020-04-27 17:16:18 +00:00
err := f.cdc.UnmarshalJSON([]byte(stdout), &out)
2019-06-25 12:01:02 +00:00
require.NoError(f.T, err)
return out
}
2020-04-28 14:10:02 +00:00
// QueryGovProposal is kvcli query gov proposal
2019-06-25 12:01:02 +00:00
func (f *Fixtures) QueryGovProposal(proposalID int, flags ...string) gov.Proposal {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s query gov proposal %d %v", f.KvcliBinary, proposalID, f.Flags())
2019-06-25 12:01:02 +00:00
out, _ := tests.ExecuteT(f.T, addFlags(cmd, flags), "")
var proposal gov.Proposal
2020-04-27 17:16:18 +00:00
err := f.cdc.UnmarshalJSON([]byte(out), &proposal)
2019-06-25 12:01:02 +00:00
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return proposal
}
2020-04-28 14:10:02 +00:00
// QueryGovVote is kvcli query gov vote
2019-06-25 12:01:02 +00:00
func (f *Fixtures) QueryGovVote(proposalID int, voter sdk.AccAddress, flags ...string) gov.Vote {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s query gov vote %d %s %v", f.KvcliBinary, proposalID, voter, f.Flags())
2019-06-25 12:01:02 +00:00
out, _ := tests.ExecuteT(f.T, addFlags(cmd, flags), "")
var vote gov.Vote
2020-04-27 17:16:18 +00:00
err := f.cdc.UnmarshalJSON([]byte(out), &vote)
2019-06-25 12:01:02 +00:00
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return vote
}
2020-04-28 14:10:02 +00:00
// QueryGovVotes is kvcli query gov votes
2019-06-25 12:01:02 +00:00
func (f *Fixtures) QueryGovVotes(proposalID int, flags ...string) []gov.Vote {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s query gov votes %d %v", f.KvcliBinary, proposalID, f.Flags())
2019-06-25 12:01:02 +00:00
out, _ := tests.ExecuteT(f.T, addFlags(cmd, flags), "")
var votes []gov.Vote
2020-04-27 17:16:18 +00:00
err := f.cdc.UnmarshalJSON([]byte(out), &votes)
2019-06-25 12:01:02 +00:00
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return votes
}
2020-04-28 14:10:02 +00:00
// QueryGovDeposit is kvcli query gov deposit
2019-06-25 12:01:02 +00:00
func (f *Fixtures) QueryGovDeposit(proposalID int, depositor sdk.AccAddress, flags ...string) gov.Deposit {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s query gov deposit %d %s %v", f.KvcliBinary, proposalID, depositor, f.Flags())
2019-06-25 12:01:02 +00:00
out, _ := tests.ExecuteT(f.T, addFlags(cmd, flags), "")
var deposit gov.Deposit
2020-04-27 17:16:18 +00:00
err := f.cdc.UnmarshalJSON([]byte(out), &deposit)
2019-06-25 12:01:02 +00:00
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return deposit
}
2020-04-28 14:10:02 +00:00
// QueryGovDeposits is kvcli query gov deposits
2019-06-25 12:01:02 +00:00
func (f *Fixtures) QueryGovDeposits(propsalID int, flags ...string) []gov.Deposit {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s query gov deposits %d %v", f.KvcliBinary, propsalID, f.Flags())
2019-06-25 12:01:02 +00:00
out, _ := tests.ExecuteT(f.T, addFlags(cmd, flags), "")
var deposits []gov.Deposit
2020-04-27 17:16:18 +00:00
err := f.cdc.UnmarshalJSON([]byte(out), &deposits)
2019-06-25 12:01:02 +00:00
require.NoError(f.T, err, "out %v\n, err %v", out, err)
return deposits
}
//___________________________________________________________________________________
// query slashing
// QuerySigningInfo returns the signing info for a validator
func (f *Fixtures) QuerySigningInfo(val string) slashing.ValidatorSigningInfo {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s query slashing signing-info %s %s", f.KvcliBinary, val, f.Flags())
2019-06-25 12:01:02 +00:00
res, errStr := tests.ExecuteT(f.T, cmd, "")
require.Empty(f.T, errStr)
2020-04-27 17:16:18 +00:00
2019-06-25 12:01:02 +00:00
var sinfo slashing.ValidatorSigningInfo
2020-04-27 17:16:18 +00:00
err := f.cdc.UnmarshalJSON([]byte(res), &sinfo)
2019-06-25 12:01:02 +00:00
require.NoError(f.T, err)
return sinfo
}
2020-04-28 14:10:02 +00:00
// QuerySlashingParams is kvcli query slashing params
2019-06-25 12:01:02 +00:00
func (f *Fixtures) QuerySlashingParams() slashing.Params {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s query slashing params %s", f.KvcliBinary, f.Flags())
2019-06-25 12:01:02 +00:00
res, errStr := tests.ExecuteT(f.T, cmd, "")
require.Empty(f.T, errStr)
2020-04-27 17:16:18 +00:00
2019-06-25 12:01:02 +00:00
var params slashing.Params
2020-04-27 17:16:18 +00:00
err := f.cdc.UnmarshalJSON([]byte(res), &params)
2019-06-25 12:01:02 +00:00
require.NoError(f.T, err)
return params
}
2019-09-12 16:19:13 +00:00
//___________________________________________________________________________________
// query distribution
// QueryRewards returns the rewards of a delegator
func (f *Fixtures) QueryRewards(delAddr sdk.AccAddress, flags ...string) distribution.QueryDelegatorTotalRewardsResponse {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s query distribution rewards %s %s", f.KvcliBinary, delAddr, f.Flags())
2019-09-12 16:19:13 +00:00
res, errStr := tests.ExecuteT(f.T, cmd, "")
require.Empty(f.T, errStr)
2020-04-27 17:16:18 +00:00
2019-09-12 16:19:13 +00:00
var rewards distribution.QueryDelegatorTotalRewardsResponse
2020-04-27 17:16:18 +00:00
err := f.cdc.UnmarshalJSON([]byte(res), &rewards)
2019-09-12 16:19:13 +00:00
require.NoError(f.T, err)
return rewards
}
//___________________________________________________________________________________
// query supply
// QueryTotalSupply returns the total supply of coins
func (f *Fixtures) QueryTotalSupply(flags ...string) (totalSupply sdk.Coins) {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s query supply total %s", f.KvcliBinary, f.Flags())
2019-09-12 16:19:13 +00:00
res, errStr := tests.ExecuteT(f.T, cmd, "")
require.Empty(f.T, errStr)
2020-04-27 17:16:18 +00:00
err := f.cdc.UnmarshalJSON([]byte(res), &totalSupply)
2019-09-12 16:19:13 +00:00
require.NoError(f.T, err)
return totalSupply
}
// QueryTotalSupplyOf returns the total supply of a given coin denom
func (f *Fixtures) QueryTotalSupplyOf(denom string, flags ...string) sdkmath.Int {
2020-04-28 14:10:02 +00:00
cmd := fmt.Sprintf("%s query supply total %s %s", f.KvcliBinary, denom, f.Flags())
2019-09-12 16:19:13 +00:00
res, errStr := tests.ExecuteT(f.T, cmd, "")
require.Empty(f.T, errStr)
2020-04-27 17:16:18 +00:00
var supplyOf sdkmath.Int
2020-04-27 17:16:18 +00:00
err := f.cdc.UnmarshalJSON([]byte(res), &supplyOf)
2019-09-12 16:19:13 +00:00
require.NoError(f.T, err)
return supplyOf
}
2019-06-25 12:01:02 +00:00
//___________________________________________________________________________________
// executors
func executeWriteCheckErr(t *testing.T, cmdStr string, writes ...string) {
require.True(t, executeWrite(t, cmdStr, writes...))
}
func executeWrite(t *testing.T, cmdStr string, writes ...string) (exitSuccess bool) {
exitSuccess, _, _ = executeWriteRetStdStreams(t, cmdStr, writes...)
return
}
func executeWriteRetStdStreams(t *testing.T, cmdStr string, writes ...string) (bool, string, string) {
proc := tests.GoExecuteT(t, cmdStr)
// Enables use of interactive commands
for _, write := range writes {
_, err := proc.StdinPipe.Write([]byte(write + "\n"))
require.NoError(t, err)
}
// Read both stdout and stderr from the process
stdout, stderr, err := proc.ReadAll()
if err != nil {
fmt.Println("Err on proc.ReadAll()", err, cmdStr)
}
// Log output.
if len(stdout) > 0 {
2019-09-12 16:19:13 +00:00
t.Log("Stdout:", string(stdout))
2019-06-25 12:01:02 +00:00
}
if len(stderr) > 0 {
2019-09-12 16:19:13 +00:00
t.Log("Stderr:", string(stderr))
2019-06-25 12:01:02 +00:00
}
// Wait for process to exit
proc.Wait()
// Return succes, stdout, stderr
return proc.ExitState.Success(), string(stdout), string(stderr)
}
//___________________________________________________________________________________
// utils
func addFlags(cmd string, flags []string) string {
for _, f := range flags {
cmd += " " + f
}
return strings.TrimSpace(cmd)
}
2020-04-27 17:16:18 +00:00
func queryEvents(events []string) (out string) {
for _, event := range events {
out += event + "&"
2019-06-25 12:01:02 +00:00
}
return strings.TrimSuffix(out, "&")
}
// Write the given string to a new temporary file
func WriteToNewTempFile(t *testing.T, s string) *os.File {
fp, err := ioutil.TempFile(os.TempDir(), "cosmos_cli_test_")
require.Nil(t, err)
_, err = fp.WriteString(s)
require.Nil(t, err)
return fp
}
2020-04-27 17:16:18 +00:00
//nolint:deadcode,unused
func (f *Fixtures) marshalStdTx(t *testing.T, stdTx auth.StdTx) []byte {
bz, err := f.cdc.MarshalBinaryBare(stdTx)
2019-06-25 12:01:02 +00:00
require.NoError(t, err)
return bz
}
2020-04-27 17:16:18 +00:00
//nolint:deadcode,unused
func (f *Fixtures) unmarshalStdTx(t *testing.T, s string) (stdTx auth.StdTx) {
require.Nil(t, f.cdc.UnmarshalJSON([]byte(s), &stdTx))
2019-06-25 12:01:02 +00:00
return
}