2021-07-16 14:47:40 +00:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
2021-07-22 11:40:18 +00:00
|
|
|
"encoding/hex"
|
2021-07-16 14:47:40 +00:00
|
|
|
"fmt"
|
2021-07-22 11:40:18 +00:00
|
|
|
"sort"
|
2021-07-16 14:47:40 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"text/tabwriter"
|
|
|
|
|
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/native/nativenames"
|
|
|
|
"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"
|
2021-07-22 11:57:29 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2021-07-16 14:47:40 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
2021-07-22 16:52:17 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2022-07-08 14:28:29 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/vmstate"
|
2021-07-16 14:47:40 +00:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewCommands returns 'query' command.
|
|
|
|
func NewCommands() []cli.Command {
|
|
|
|
queryTxFlags := append([]cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "verbose, v",
|
|
|
|
Usage: "Output full tx info and execution logs",
|
|
|
|
},
|
|
|
|
}, options.RPC...)
|
|
|
|
return []cli.Command{{
|
|
|
|
Name: "query",
|
2021-07-21 15:26:58 +00:00
|
|
|
Usage: "Query data from RPC node",
|
2021-07-16 14:47:40 +00:00
|
|
|
Subcommands: []cli.Command{
|
2021-07-22 11:40:18 +00:00
|
|
|
{
|
|
|
|
Name: "candidates",
|
|
|
|
Usage: "Get candidates and votes",
|
|
|
|
Action: queryCandidates,
|
|
|
|
Flags: options.RPC,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "committee",
|
|
|
|
Usage: "Get committee list",
|
|
|
|
Action: queryCommittee,
|
|
|
|
Flags: options.RPC,
|
|
|
|
},
|
2021-07-22 18:10:32 +00:00
|
|
|
{
|
|
|
|
Name: "height",
|
|
|
|
Usage: "Get node height",
|
|
|
|
Action: queryHeight,
|
|
|
|
Flags: options.RPC,
|
|
|
|
},
|
2021-07-16 14:47:40 +00:00
|
|
|
{
|
2022-03-25 08:40:53 +00:00
|
|
|
Name: "tx",
|
|
|
|
Usage: "Query transaction status",
|
|
|
|
UsageText: "neo-go query tx <hash> -r endpoint [-v]",
|
|
|
|
Action: queryTx,
|
|
|
|
Flags: queryTxFlags,
|
2021-07-16 14:47:40 +00:00
|
|
|
},
|
2021-07-22 11:57:29 +00:00
|
|
|
{
|
|
|
|
Name: "voter",
|
|
|
|
Usage: "Print NEO holder account state",
|
|
|
|
Action: queryVoter,
|
|
|
|
Flags: options.RPC,
|
|
|
|
},
|
2021-07-16 14:47:40 +00:00
|
|
|
},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryTx(ctx *cli.Context) error {
|
|
|
|
args := ctx.Args()
|
|
|
|
if len(args) == 0 {
|
|
|
|
return cli.NewExitError("Transaction hash is missing", 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
txHash, err := util.Uint256DecodeStringLE(strings.TrimPrefix(args[0], "0x"))
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(fmt.Sprintf("Invalid tx hash: %s", args[0]), 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
gctx, cancel := options.GetTimeoutContext(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
c, err := options.GetRPCClient(gctx, ctx)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
txOut, err := c.GetRawTransactionVerbose(txHash)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
var res *result.ApplicationLog
|
|
|
|
if !txOut.Blockhash.Equals(util.Uint256{}) {
|
|
|
|
res, err = c.GetApplicationLog(txHash, nil)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:02:54 +00:00
|
|
|
DumpApplicationLog(ctx, res, &txOut.Transaction, &txOut.TransactionMetadata, ctx.Bool("verbose"))
|
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,
|
|
|
|
verbose bool) {
|
2021-07-16 14:47:40 +00:00
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
|
|
|
|
// Ignore the errors below because `Write` to buffer doesn't return error.
|
|
|
|
tw := tabwriter.NewWriter(buf, 0, 4, 4, '\t', 0)
|
|
|
|
_, _ = tw.Write([]byte("Hash:\t" + tx.Hash().StringLE() + "\n"))
|
|
|
|
_, _ = tw.Write([]byte(fmt.Sprintf("OnChain:\t%t\n", res != nil)))
|
|
|
|
if res == nil {
|
|
|
|
_, _ = tw.Write([]byte("ValidUntil:\t" + strconv.FormatUint(uint64(tx.ValidUntilBlock), 10) + "\n"))
|
|
|
|
} else {
|
2021-09-14 12:02:54 +00:00
|
|
|
if txMeta != nil {
|
|
|
|
_, _ = tw.Write([]byte("BlockHash:\t" + txMeta.Blockhash.StringLE() + "\n"))
|
|
|
|
}
|
2021-07-21 15:25:10 +00:00
|
|
|
if len(res.Executions) != 1 {
|
|
|
|
_, _ = tw.Write([]byte("Success:\tunknown (no execution data)\n"))
|
|
|
|
} else {
|
2022-07-08 14:28:29 +00:00
|
|
|
_, _ = tw.Write([]byte(fmt.Sprintf("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 {
|
|
|
|
_, _ = tw.Write([]byte(fmt.Sprintf("Signer:\t%s (%s)",
|
2021-07-21 15:27:09 +00:00
|
|
|
address.Uint160ToString(sig.Account),
|
2021-07-16 14:47:40 +00:00
|
|
|
sig.Scopes) + "\n"))
|
|
|
|
}
|
|
|
|
_, _ = tw.Write([]byte("SystemFee:\t" + fixedn.Fixed8(tx.SystemFee).String() + " GAS\n"))
|
|
|
|
_, _ = tw.Write([]byte("NetworkFee:\t" + fixedn.Fixed8(tx.NetworkFee).String() + " GAS\n"))
|
|
|
|
_, _ = tw.Write([]byte("Script:\t" + base64.StdEncoding.EncodeToString(tx.Script) + "\n"))
|
2021-09-14 12:02:54 +00:00
|
|
|
v := vm.New()
|
|
|
|
v.Load(tx.Script)
|
|
|
|
v.PrintOps(tw)
|
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 {
|
2021-07-16 14:47:40 +00:00
|
|
|
_, _ = tw.Write([]byte("Exception:\t" + e.FaultException + "\n"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ = tw.Flush()
|
|
|
|
fmt.Fprint(ctx.App.Writer, buf.String())
|
|
|
|
}
|
2021-07-22 11:40:18 +00:00
|
|
|
|
|
|
|
func queryCandidates(ctx *cli.Context) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
gctx, cancel := options.GetTimeoutContext(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
c, err := options.GetRPCClient(gctx, ctx)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
2022-07-01 13:02:03 +00:00
|
|
|
vals, err := c.GetCandidates()
|
2021-07-22 11:40:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
comm, err := c.GetCommittee()
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
})
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
tw := tabwriter.NewWriter(buf, 0, 2, 2, ' ', 0)
|
|
|
|
_, _ = tw.Write([]byte("Key\tVotes\tCommittee\tConsensus\n"))
|
|
|
|
for _, val := range vals {
|
2022-07-01 13:02:03 +00:00
|
|
|
_, _ = tw.Write([]byte(fmt.Sprintf("%s\t%d\t%t\t%t\n", hex.EncodeToString(val.PublicKey.Bytes()), val.Votes, comm.Contains(&val.PublicKey), val.Active)))
|
2021-07-22 11:40:18 +00:00
|
|
|
}
|
|
|
|
_ = tw.Flush()
|
|
|
|
fmt.Fprint(ctx.App.Writer, buf.String())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryCommittee(ctx *cli.Context) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
gctx, cancel := options.GetTimeoutContext(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
c, err := options.GetRPCClient(gctx, ctx)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
comm, err := c.GetCommittee()
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, k := range comm {
|
|
|
|
fmt.Fprintln(ctx.App.Writer, hex.EncodeToString(k.Bytes()))
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
|
gctx, cancel := options.GetTimeoutContext(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
c, err := options.GetRPCClient(gctx, ctx)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
blockCount, err := c.GetBlockCount()
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
args := ctx.Args()
|
|
|
|
if len(args) == 0 {
|
|
|
|
return cli.NewExitError("No address specified", 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
addr, err := flags.ParseAddress(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(fmt.Sprintf("wrong address: %s", args[0]), 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
gctx, cancel := options.GetTimeoutContext(ctx)
|
|
|
|
defer cancel()
|
|
|
|
c, exitErr := options.GetRPCClient(gctx, ctx)
|
|
|
|
if exitErr != nil {
|
|
|
|
return exitErr
|
|
|
|
}
|
|
|
|
|
|
|
|
neoHash, err := c.GetNativeContractHash(nativenames.Neo)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(fmt.Errorf("failed to get NEO contract hash: %w", err), 1)
|
|
|
|
}
|
|
|
|
res, err := c.InvokeFunction(neoHash, "getAccountState", []smartcontract.Parameter{
|
|
|
|
{
|
|
|
|
Type: smartcontract.Hash160Type,
|
|
|
|
Value: addr,
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
if res.State != "HALT" {
|
|
|
|
return cli.NewExitError(fmt.Errorf("invocation failed: %s", res.FaultException), 1)
|
|
|
|
}
|
|
|
|
if len(res.Stack) == 0 {
|
|
|
|
return cli.NewExitError("result stack is empty", 1)
|
|
|
|
}
|
|
|
|
st := new(state.NEOBalance)
|
2021-07-22 16:52:17 +00:00
|
|
|
if _, ok := res.Stack[0].(stackitem.Null); !ok {
|
|
|
|
err = st.FromStackItem(res.Stack[0])
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(fmt.Errorf("failed to convert account state from stackitem: %w", err), 1)
|
|
|
|
}
|
2021-07-22 11:57:29 +00:00
|
|
|
}
|
|
|
|
dec, err := c.NEP17Decimals(neoHash)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(fmt.Errorf("failed to get decimals: %w", err), 1)
|
|
|
|
}
|
|
|
|
voted := "null"
|
|
|
|
if st.VoteTo != nil {
|
2021-07-22 12:05:09 +00:00
|
|
|
voted = fmt.Sprintf("%s (%s)", hex.EncodeToString(st.VoteTo.Bytes()), 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
|
|
|
|
}
|