2021-07-16 14:47:40 +00:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
2021-07-22 11:40:18 +00:00
|
|
|
"sort"
|
2021-07-16 14:47:40 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"text/tabwriter"
|
|
|
|
|
2022-08-05 10:32:37 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/cli/cmdargs"
|
2021-07-22 11:57:29 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/cli/flags"
|
2021-07-16 14:47:40 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/cli/options"
|
2021-07-22 11:57:29 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2021-09-14 12:02:54 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2021-07-21 15:27:09 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
2021-07-16 14:47:40 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
|
2022-07-22 16:09:29 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neorpc/result"
|
2022-08-12 12:30:55 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker"
|
2022-08-17 18:55:30 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/neo"
|
2021-07-16 14:47:40 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
2022-07-08 14:28:29 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/vmstate"
|
2024-07-09 18:24:39 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2021-07-16 14:47:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewCommands returns 'query' command.
|
2024-07-09 18:24:39 +00:00
|
|
|
func NewCommands() []*cli.Command {
|
2021-07-16 14:47:40 +00:00
|
|
|
queryTxFlags := append([]cli.Flag{
|
2024-07-09 18:24:39 +00:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "verbose",
|
|
|
|
Aliases: []string{"v"},
|
|
|
|
Usage: "Output full tx info and execution logs",
|
2021-07-16 14:47:40 +00:00
|
|
|
},
|
|
|
|
}, options.RPC...)
|
2024-07-09 18:24:39 +00:00
|
|
|
return []*cli.Command{{
|
2021-07-16 14:47:40 +00:00
|
|
|
Name: "query",
|
2021-07-21 15:26:58 +00:00
|
|
|
Usage: "Query data from RPC node",
|
2024-07-09 18:24:39 +00:00
|
|
|
Subcommands: []*cli.Command{
|
2021-07-22 11:40:18 +00:00
|
|
|
{
|
2022-08-05 13:23:50 +00:00
|
|
|
Name: "candidates",
|
|
|
|
Usage: "Get candidates and votes",
|
|
|
|
UsageText: "neo-go query candidates -r endpoint [-s timeout]",
|
|
|
|
Action: queryCandidates,
|
|
|
|
Flags: options.RPC,
|
2021-07-22 11:40:18 +00:00
|
|
|
},
|
|
|
|
{
|
2022-08-05 13:23:50 +00:00
|
|
|
Name: "committee",
|
|
|
|
Usage: "Get committee list",
|
|
|
|
UsageText: "neo-go query committee -r endpoint [-s timeout]",
|
|
|
|
Action: queryCommittee,
|
|
|
|
Flags: options.RPC,
|
2021-07-22 11:40:18 +00:00
|
|
|
},
|
2021-07-22 18:10:32 +00:00
|
|
|
{
|
2022-08-05 13:23:50 +00:00
|
|
|
Name: "height",
|
|
|
|
Usage: "Get node height",
|
|
|
|
UsageText: "neo-go query height -r endpoint [-s timeout]",
|
|
|
|
Action: queryHeight,
|
|
|
|
Flags: options.RPC,
|
2021-07-22 18:10:32 +00:00
|
|
|
},
|
2021-07-16 14:47:40 +00:00
|
|
|
{
|
2022-03-25 08:40:53 +00:00
|
|
|
Name: "tx",
|
|
|
|
Usage: "Query transaction status",
|
2024-07-09 18:24:39 +00:00
|
|
|
UsageText: "neo-go query tx -r endpoint [-s timeout] [-v] <hash>",
|
2022-03-25 08:40:53 +00:00
|
|
|
Action: queryTx,
|
|
|
|
Flags: queryTxFlags,
|
2021-07-16 14:47:40 +00:00
|
|
|
},
|
2021-07-22 11:57:29 +00:00
|
|
|
{
|
2022-08-05 13:23:50 +00:00
|
|
|
Name: "voter",
|
|
|
|
Usage: "Print NEO holder account state",
|
2024-07-09 18:24:39 +00:00
|
|
|
UsageText: "neo-go query voter -r endpoint [-s timeout] <address>",
|
2022-08-05 13:23:50 +00:00
|
|
|
Action: queryVoter,
|
|
|
|
Flags: options.RPC,
|
2021-07-22 11:57:29 +00:00
|
|
|
},
|
2021-07-16 14:47:40 +00:00
|
|
|
},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryTx(ctx *cli.Context) error {
|
2024-07-09 18:24:39 +00:00
|
|
|
args := ctx.Args().Slice()
|
2021-07-16 14:47:40 +00:00
|
|
|
if len(args) == 0 {
|
2024-07-12 09:35:10 +00:00
|
|
|
return cli.Exit("transaction hash is missing", 1)
|
2022-08-05 10:32:37 +00:00
|
|
|
} else if len(args) > 1 {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit("only one transaction hash is accepted", 1)
|
2021-07-16 14:47:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
txHash, err := util.Uint256DecodeStringLE(strings.TrimPrefix(args[0], "0x"))
|
|
|
|
if err != nil {
|
2024-07-12 09:35:10 +00:00
|
|
|
return cli.Exit(fmt.Sprintf("invalid tx hash: %s", args[0]), 1)
|
2021-07-16 14:47:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gctx, cancel := options.GetTimeoutContext(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
c, err := options.GetRPCClient(gctx, ctx)
|
|
|
|
if err != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(err, 1)
|
2021-07-16 14:47:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
txOut, err := c.GetRawTransactionVerbose(txHash)
|
|
|
|
if err != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(err, 1)
|
2021-07-16 14:47:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var res *result.ApplicationLog
|
|
|
|
if !txOut.Blockhash.Equals(util.Uint256{}) {
|
|
|
|
res, err = c.GetApplicationLog(txHash, nil)
|
|
|
|
if err != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(err, 1)
|
2021-07-16 14:47:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-12 15:07:54 +00:00
|
|
|
err = DumpApplicationLog(ctx, res, &txOut.Transaction, &txOut.TransactionMetadata, ctx.Bool("verbose"))
|
|
|
|
if err != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(err, 1)
|
2023-10-12 15:07:54 +00:00
|
|
|
}
|
2021-07-16 14:47:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:02:54 +00:00
|
|
|
func DumpApplicationLog(
|
|
|
|
ctx *cli.Context,
|
|
|
|
res *result.ApplicationLog,
|
|
|
|
tx *transaction.Transaction,
|
|
|
|
txMeta *result.TransactionMetadata,
|
2023-10-12 15:07:54 +00:00
|
|
|
verbose bool) error {
|
|
|
|
var buf []byte
|
2021-07-16 14:47:40 +00:00
|
|
|
|
2023-10-12 15:07:54 +00:00
|
|
|
buf = fmt.Appendf(buf, "Hash:\t%s\n", tx.Hash().StringLE())
|
|
|
|
buf = fmt.Appendf(buf, "OnChain:\t%t\n", res != nil)
|
2021-07-16 14:47:40 +00:00
|
|
|
if res == nil {
|
2023-10-12 15:07:54 +00:00
|
|
|
buf = fmt.Appendf(buf, "ValidUntil:\t%s\n", strconv.FormatUint(uint64(tx.ValidUntilBlock), 10))
|
2021-07-16 14:47:40 +00:00
|
|
|
} else {
|
2021-09-14 12:02:54 +00:00
|
|
|
if txMeta != nil {
|
2023-10-12 15:07:54 +00:00
|
|
|
buf = fmt.Appendf(buf, "BlockHash:\t%s\n", txMeta.Blockhash.StringLE())
|
2021-09-14 12:02:54 +00:00
|
|
|
}
|
2021-07-21 15:25:10 +00:00
|
|
|
if len(res.Executions) != 1 {
|
2023-10-12 15:07:54 +00:00
|
|
|
buf = fmt.Appendf(buf, "Success:\tunknown (no execution data)\n")
|
2021-07-21 15:25:10 +00:00
|
|
|
} else {
|
2023-10-12 15:07:54 +00:00
|
|
|
buf = fmt.Appendf(buf, "Success:\t%t\n", res.Executions[0].VMState == vmstate.Halt)
|
2021-07-21 15:25:10 +00:00
|
|
|
}
|
2021-07-16 14:47:40 +00:00
|
|
|
}
|
|
|
|
if verbose {
|
|
|
|
for _, sig := range tx.Signers {
|
2023-10-12 15:07:54 +00:00
|
|
|
buf = fmt.Appendf(buf, "Signer:\t%s (%s)\n", address.Uint160ToString(sig.Account), sig.Scopes)
|
2021-07-16 14:47:40 +00:00
|
|
|
}
|
2023-10-12 15:07:54 +00:00
|
|
|
buf = fmt.Appendf(buf, "SystemFee:\t%s GAS\n", fixedn.Fixed8(tx.SystemFee).String())
|
|
|
|
buf = fmt.Appendf(buf, "NetworkFee:\t%s GAS\n", fixedn.Fixed8(tx.NetworkFee).String())
|
|
|
|
buf = fmt.Appendf(buf, "Script:\t%s\n", base64.StdEncoding.EncodeToString(tx.Script))
|
2021-09-14 12:02:54 +00:00
|
|
|
v := vm.New()
|
|
|
|
v.Load(tx.Script)
|
2023-10-12 15:07:54 +00:00
|
|
|
opts := bytes.NewBuffer(nil)
|
|
|
|
v.PrintOps(opts)
|
|
|
|
buf = append(buf, opts.Bytes()...)
|
2021-07-16 14:47:40 +00:00
|
|
|
if res != nil {
|
|
|
|
for _, e := range res.Executions {
|
2022-07-08 14:28:29 +00:00
|
|
|
if e.VMState != vmstate.Halt {
|
2023-10-12 15:07:54 +00:00
|
|
|
buf = fmt.Appendf(buf, "Exception:\t%s\n", e.FaultException)
|
2021-07-16 14:47:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-12 15:07:54 +00:00
|
|
|
tw := tabwriter.NewWriter(ctx.App.Writer, 0, 4, 4, '\t', 0)
|
|
|
|
_, err := tw.Write(buf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return tw.Flush()
|
2021-07-16 14:47:40 +00:00
|
|
|
}
|
2021-07-22 11:40:18 +00:00
|
|
|
|
|
|
|
func queryCandidates(ctx *cli.Context) error {
|
|
|
|
var err error
|
|
|
|
|
2022-08-05 10:32:37 +00:00
|
|
|
if err := cmdargs.EnsureNone(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-22 11:40:18 +00:00
|
|
|
gctx, cancel := options.GetTimeoutContext(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
c, err := options.GetRPCClient(gctx, ctx)
|
|
|
|
if err != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(err, 1)
|
2021-07-22 11:40:18 +00:00
|
|
|
}
|
|
|
|
|
2022-07-01 13:02:03 +00:00
|
|
|
vals, err := c.GetCandidates()
|
2021-07-22 11:40:18 +00:00
|
|
|
if err != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(err, 1)
|
2021-07-22 11:40:18 +00:00
|
|
|
}
|
|
|
|
comm, err := c.GetCommittee()
|
|
|
|
if err != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(err, 1)
|
2021-07-22 11:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(vals, func(i, j int) bool {
|
2022-07-01 13:02:03 +00:00
|
|
|
if vals[i].Active != vals[j].Active {
|
|
|
|
return vals[i].Active
|
|
|
|
}
|
|
|
|
if vals[i].Votes != vals[j].Votes {
|
|
|
|
return vals[i].Votes > vals[j].Votes
|
|
|
|
}
|
2021-07-22 11:40:18 +00:00
|
|
|
return vals[i].PublicKey.Cmp(&vals[j].PublicKey) == -1
|
|
|
|
})
|
2023-10-12 15:07:54 +00:00
|
|
|
var res []byte
|
|
|
|
res = fmt.Appendf(res, "Key\tVotes\tCommittee\tConsensus\n")
|
2021-07-22 11:40:18 +00:00
|
|
|
for _, val := range vals {
|
2024-04-09 09:13:43 +00:00
|
|
|
res = fmt.Appendf(res, "%s\t%d\t%t\t%t\n", val.PublicKey.StringCompressed(), val.Votes, comm.Contains(&val.PublicKey), val.Active)
|
2021-07-22 11:40:18 +00:00
|
|
|
}
|
2023-10-12 15:07:54 +00:00
|
|
|
tw := tabwriter.NewWriter(ctx.App.Writer, 0, 2, 2, ' ', 0)
|
|
|
|
_, err = tw.Write(res)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return tw.Flush()
|
2021-07-22 11:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func queryCommittee(ctx *cli.Context) error {
|
|
|
|
var err error
|
|
|
|
|
2022-08-05 10:32:37 +00:00
|
|
|
if err := cmdargs.EnsureNone(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-22 11:40:18 +00:00
|
|
|
gctx, cancel := options.GetTimeoutContext(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
c, err := options.GetRPCClient(gctx, ctx)
|
|
|
|
if err != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(err, 1)
|
2021-07-22 11:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
comm, err := c.GetCommittee()
|
|
|
|
if err != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(err, 1)
|
2021-07-22 11:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, k := range comm {
|
2024-04-09 09:13:43 +00:00
|
|
|
fmt.Fprintln(ctx.App.Writer, k.StringCompressed())
|
2021-07-22 11:40:18 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-07-22 11:57:29 +00:00
|
|
|
|
2021-07-22 18:10:32 +00:00
|
|
|
func queryHeight(ctx *cli.Context) error {
|
|
|
|
var err error
|
|
|
|
|
2022-08-05 10:32:37 +00:00
|
|
|
if err := cmdargs.EnsureNone(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-22 18:10:32 +00:00
|
|
|
gctx, cancel := options.GetTimeoutContext(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
c, err := options.GetRPCClient(gctx, ctx)
|
|
|
|
if err != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(err, 1)
|
2021-07-22 18:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
blockCount, err := c.GetBlockCount()
|
|
|
|
if err != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(err, 1)
|
2021-07-22 18:10:32 +00:00
|
|
|
}
|
|
|
|
blockHeight := blockCount - 1 // GetBlockCount returns block count (including 0), not the highest block index.
|
|
|
|
|
|
|
|
fmt.Fprintf(ctx.App.Writer, "Latest block: %d\n", blockHeight)
|
|
|
|
|
|
|
|
stateHeight, err := c.GetStateHeight()
|
|
|
|
if err == nil { // We can be talking to a node without getstateheight request support.
|
|
|
|
fmt.Fprintf(ctx.App.Writer, "Validated state: %d\n", stateHeight.Validated)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-22 11:57:29 +00:00
|
|
|
func queryVoter(ctx *cli.Context) error {
|
2024-07-09 18:24:39 +00:00
|
|
|
args := ctx.Args().Slice()
|
2021-07-22 11:57:29 +00:00
|
|
|
if len(args) == 0 {
|
2024-07-12 09:35:10 +00:00
|
|
|
return cli.Exit("no address specified", 1)
|
2022-08-05 10:32:37 +00:00
|
|
|
} else if len(args) > 1 {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit("this command only accepts one address", 1)
|
2021-07-22 11:57:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addr, err := flags.ParseAddress(args[0])
|
|
|
|
if err != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(fmt.Sprintf("wrong address: %s", args[0]), 1)
|
2021-07-22 11:57:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gctx, cancel := options.GetTimeoutContext(ctx)
|
|
|
|
defer cancel()
|
|
|
|
c, exitErr := options.GetRPCClient(gctx, ctx)
|
|
|
|
if exitErr != nil {
|
|
|
|
return exitErr
|
|
|
|
}
|
|
|
|
|
2022-08-17 18:55:30 +00:00
|
|
|
neoToken := neo.NewReader(invoker.New(c, nil))
|
2022-08-12 12:30:55 +00:00
|
|
|
|
2022-08-17 18:55:30 +00:00
|
|
|
st, err := neoToken.GetAccountState(addr)
|
2021-07-22 11:57:29 +00:00
|
|
|
if err != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(err, 1)
|
2021-07-22 11:57:29 +00:00
|
|
|
}
|
2022-08-17 18:55:30 +00:00
|
|
|
if st == nil {
|
|
|
|
st = new(state.NEOBalance)
|
2021-07-22 11:57:29 +00:00
|
|
|
}
|
2022-08-12 12:30:55 +00:00
|
|
|
dec, err := neoToken.Decimals()
|
2021-07-22 11:57:29 +00:00
|
|
|
if err != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(fmt.Errorf("failed to get decimals: %w", err), 1)
|
2021-07-22 11:57:29 +00:00
|
|
|
}
|
|
|
|
voted := "null"
|
|
|
|
if st.VoteTo != nil {
|
2024-04-09 09:13:43 +00:00
|
|
|
voted = fmt.Sprintf("%s (%s)", st.VoteTo.StringCompressed(), address.Uint160ToString(st.VoteTo.GetScriptHash()))
|
2021-07-22 11:57:29 +00:00
|
|
|
}
|
|
|
|
fmt.Fprintf(ctx.App.Writer, "\tVoted: %s\n", voted)
|
|
|
|
fmt.Fprintf(ctx.App.Writer, "\tAmount : %s\n", fixedn.ToString(&st.Balance, int(dec)))
|
|
|
|
fmt.Fprintf(ctx.App.Writer, "\tBlock: %d\n", st.BalanceHeight)
|
|
|
|
return nil
|
|
|
|
}
|