forked from ignite/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cmd): introduce node command (ignite#2669)
* add node command with bank query and send Bounty address: cosmos1zra3wz596n0qc898myceka3q23x9rk8fuw65c7 * Code review cleanup * Code review fix * fix merge * refactor * Merge branch 'develop' into issue-2489-node-banks-cmds Fixed a bunch of conflicts with recent renaming * wip: fix integration tests due to recent refac * test: finish apply refac in tests * Fix integration test on node command * various improvments in node integration test * test: read test.v to add more trace on step execution That helps when some commands are not running properly. * fix: spinner hides os keyring prompt * Ensure bank send can take account name or address Also simplify the logic by removing the read of the --from flag, which is no longer used in this command. BroadcastTx now takes a cosmosaccount.Account instead of just an account name. Since ensure the account comes from the keyring, and avoid the BroadcastTx impl to re-check that (because the keyring is always checked before the call to BroadcastTx). * fix after rebase * fix: bank balance can take an address absent of the keyring * fix tx broadcast error message * add fees flag to node tx bank send cmd fees are required, at least in cosmos-hub to send funds to an other account. W/o fees the transaction returns this message: error code: '13' msg: 'insufficient fees; got: required: 8uatom: insufficient fee' Also increase the additional gas amount added to the simulated gas, because I got some insufficient gas error with the previous value. * Change tx BroadcastMode to BroadcastSync Previous mode BroadcastBlock alweus triggered a timeout error, even if the tx was finally accepted in a block. RPC error -32603 - Internal error: timed out waiting for tx to be included in a block * fix linter * fix other lint * improve lookupAddress error report * fix typo * wip * simplify * add assertBankBalanceOutput * wip find a way to wait for block height * test: replace time.Sleep with app.WaitNBlocks * gofumpt * fix integration test * BroadcastBlock is deprecated * simplify node query * feat: move WaitForBlock methods in cosmosclient * fix: revert usage of BroadcastSync mode * docs: adapt blog tutorial to cosmosclient changes * feat: add --broadcasd-mode flag to node tx command The default value is still block but the flag can change to sync in order to avoid timeout when the tx is broadcasted to busy nodes. Also add a `node query tx` so the user can check when his tx is included in a block. * comments * style: consolidate arg names * fix: add port to default node * merge const decls * fix merge error * fix after merge * fix after merge Co-authored-by: Gjermund Bjaanes <[email protected]> Co-authored-by: İlker G. Öztürk <[email protected]> Co-authored-by: Alex Johnson <[email protected]>
- Loading branch information
1 parent
d714782
commit 2d016ca
Showing
67 changed files
with
2,232 additions
and
790 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package ignitecmd | ||
|
||
import ( | ||
"github.com/ignite/cli/ignite/pkg/cosmosclient" | ||
"github.com/ignite/cli/ignite/pkg/xurl" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
const ( | ||
flagNode = "node" | ||
cosmosRPCAddress = "https://rpc.cosmos.network:443" | ||
) | ||
|
||
func NewNode() *cobra.Command { | ||
c := &cobra.Command{ | ||
Use: "node [command]", | ||
Short: "Make calls to a live blockchain node", | ||
Args: cobra.ExactArgs(1), | ||
} | ||
|
||
c.PersistentFlags().String(flagNode, cosmosRPCAddress, "<host>:<port> to tendermint rpc interface for this chain") | ||
|
||
c.AddCommand(NewNodeQuery()) | ||
c.AddCommand(NewNodeTx()) | ||
|
||
return c | ||
} | ||
|
||
func newNodeCosmosClient(cmd *cobra.Command) (cosmosclient.Client, error) { | ||
var ( | ||
home = getHome(cmd) | ||
prefix = getAddressPrefix(cmd) | ||
node = getRPC(cmd) | ||
keyringBackend = getKeyringBackend(cmd) | ||
keyringDir = getKeyringDir(cmd) | ||
gas = getGas(cmd) | ||
gasPrices = getGasPrices(cmd) | ||
fees = getFees(cmd) | ||
broadcastMode = getBroadcastMode(cmd) | ||
) | ||
|
||
options := []cosmosclient.Option{ | ||
cosmosclient.WithAddressPrefix(prefix), | ||
cosmosclient.WithHome(home), | ||
cosmosclient.WithKeyringBackend(keyringBackend), | ||
cosmosclient.WithKeyringDir(keyringDir), | ||
cosmosclient.WithNodeAddress(xurl.HTTPEnsurePort(node)), | ||
cosmosclient.WithBroadcastMode(broadcastMode), | ||
} | ||
|
||
if gas != "" { | ||
options = append(options, cosmosclient.WithGas(gas)) | ||
} | ||
if gasPrices != "" { | ||
options = append(options, cosmosclient.WithGasPrices(gasPrices)) | ||
} | ||
if fees != "" { | ||
options = append(options, cosmosclient.WithFees(fees)) | ||
} | ||
|
||
return cosmosclient.New(cmd.Context(), options...) | ||
} | ||
|
||
func getRPC(cmd *cobra.Command) (rpc string) { | ||
rpc, _ = cmd.Flags().GetString(flagNode) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package ignitecmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/cosmos/cosmos-sdk/types/query" | ||
"github.com/spf13/cobra" | ||
flag "github.com/spf13/pflag" | ||
) | ||
|
||
const ( | ||
flagPage = "page" | ||
flagLimit = "limit" | ||
flagPageKey = "page-key" | ||
flagOffset = "offset" | ||
flagCountTotal = "count-total" | ||
flagReverse = "reverse" | ||
) | ||
|
||
func NewNodeQuery() *cobra.Command { | ||
c := &cobra.Command{ | ||
Use: "query", | ||
Short: "Querying subcommands", | ||
Aliases: []string{"q"}, | ||
} | ||
|
||
c.AddCommand(NewNodeQueryBank()) | ||
c.AddCommand(NewNodeQueryTx()) | ||
|
||
return c | ||
} | ||
|
||
func flagSetPagination(query string) *flag.FlagSet { | ||
fs := flag.NewFlagSet("", flag.ContinueOnError) | ||
|
||
fs.Uint64(flagPage, 1, fmt.Sprintf("pagination page of %s to query for. This sets offset to a multiple of limit", query)) | ||
fs.String(flagPageKey, "", fmt.Sprintf("pagination page-key of %s to query for", query)) | ||
fs.Uint64(flagOffset, 0, fmt.Sprintf("pagination offset of %s to query for", query)) | ||
fs.Uint64(flagLimit, 100, fmt.Sprintf("pagination limit of %s to query for", query)) | ||
fs.Bool(flagCountTotal, false, fmt.Sprintf("count total number of records in %s to query for", query)) | ||
fs.Bool(flagReverse, false, "results are sorted in descending order") | ||
|
||
return fs | ||
} | ||
|
||
func getPagination(cmd *cobra.Command) (*query.PageRequest, error) { | ||
var ( | ||
pageKey, _ = cmd.Flags().GetString(flagPageKey) | ||
offset, _ = cmd.Flags().GetUint64(flagOffset) | ||
limit, _ = cmd.Flags().GetUint64(flagLimit) | ||
countTotal, _ = cmd.Flags().GetBool(flagCountTotal) | ||
page, _ = cmd.Flags().GetUint64(flagPage) | ||
reverse, _ = cmd.Flags().GetBool(flagReverse) | ||
) | ||
|
||
if page > 1 && offset > 0 { | ||
return nil, errors.New("page and offset cannot be used together") | ||
} | ||
|
||
if page > 1 { | ||
offset = (page - 1) * limit | ||
} | ||
|
||
return &query.PageRequest{ | ||
Key: []byte(pageKey), | ||
Offset: offset, | ||
Limit: limit, | ||
CountTotal: countTotal, | ||
Reverse: reverse, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package ignitecmd | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
func NewNodeQueryBank() *cobra.Command { | ||
c := &cobra.Command{ | ||
Use: "bank", | ||
Short: "Querying commands for the bank module", | ||
} | ||
|
||
c.AddCommand(NewNodeQueryBankBalances()) | ||
|
||
return c | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package ignitecmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/ignite/cli/ignite/pkg/cliui" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewNodeQueryBankBalances() *cobra.Command { | ||
c := &cobra.Command{ | ||
Use: "balances [from_account_or_address]", | ||
Short: "Query for account balances by account name or address", | ||
RunE: nodeQueryBankBalancesHandler, | ||
Args: cobra.ExactArgs(1), | ||
} | ||
|
||
c.Flags().AddFlagSet(flagSetHome()) | ||
c.Flags().AddFlagSet(flagSetAccountPrefixes()) | ||
c.Flags().AddFlagSet(flagSetKeyringBackend()) | ||
c.Flags().AddFlagSet(flagSetKeyringDir()) | ||
c.Flags().AddFlagSet(flagSetPagination("all balances")) | ||
|
||
return c | ||
} | ||
|
||
func nodeQueryBankBalancesHandler(cmd *cobra.Command, args []string) error { | ||
inputAccount := args[0] | ||
|
||
client, err := newNodeCosmosClient(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// inputAccount can be an account of the keyring or a raw address | ||
address, err := client.Address(inputAccount) | ||
if err != nil { | ||
address = inputAccount | ||
} | ||
|
||
pagination, err := getPagination(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
session := cliui.New() | ||
defer session.Cleanup() | ||
session.StartSpinner("Querying...") | ||
balances, err := client.BankBalances(cmd.Context(), address, pagination) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var rows [][]string | ||
for _, b := range balances { | ||
rows = append(rows, []string{fmt.Sprintf("%s", b.Amount), b.Denom}) | ||
} | ||
|
||
session.StopSpinner() | ||
return session.PrintTable([]string{"Amount", "Denom"}, rows...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package ignitecmd | ||
|
||
import ( | ||
"encoding/hex" | ||
"encoding/json" | ||
"fmt" | ||
|
||
sdkclient "github.com/cosmos/cosmos-sdk/client" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewNodeQueryTx() *cobra.Command { | ||
c := &cobra.Command{ | ||
Use: "tx [hash]", | ||
Short: "Query for transaction by hash", | ||
RunE: nodeQueryTxHandler, | ||
Args: cobra.ExactArgs(1), | ||
} | ||
return c | ||
} | ||
|
||
func nodeQueryTxHandler(cmd *cobra.Command, args []string) error { | ||
bz, err := hex.DecodeString(args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
rpc, err := sdkclient.NewClientFromNode(getRPC(cmd)) | ||
if err != nil { | ||
return err | ||
} | ||
resp, err := rpc.Tx(cmd.Context(), bz, false) | ||
if err != nil { | ||
return err | ||
} | ||
bz, err = json.MarshalIndent(resp, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println(string(bz)) | ||
return nil | ||
} |
Oops, something went wrong.