From 8b0dfe135fd4097ad7f5ad963aed9cfd11aadf7e Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Thu, 22 Jul 2021 21:10:32 +0300 Subject: [PATCH] cli/query: add height command --- cli/query/query.go | 33 +++++++++++++++++++++++++++++++++ cli/query_test.go | 9 +++++++++ docs/cli.md | 8 ++++++++ 3 files changed, 50 insertions(+) diff --git a/cli/query/query.go b/cli/query/query.go index 732719897..8d15e121e 100644 --- a/cli/query/query.go +++ b/cli/query/query.go @@ -48,6 +48,12 @@ func NewCommands() []cli.Command { Action: queryCommittee, Flags: options.RPC, }, + { + Name: "height", + Usage: "Get node height", + Action: queryHeight, + Flags: options.RPC, + }, { Name: "tx", Usage: "Query transaction status", @@ -201,6 +207,33 @@ func queryCommittee(ctx *cli.Context) error { return nil } +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 +} + func queryVoter(ctx *cli.Context) error { args := ctx.Args() if len(args) == 0 { diff --git a/cli/query_test.go b/cli/query_test.go index 0e6f8e9d2..5e4c9a516 100644 --- a/cli/query_test.go +++ b/cli/query_test.go @@ -130,3 +130,12 @@ func (e *executor) compareQueryTxVerbose(t *testing.T, tx *transaction.Transacti } e.checkEOF(t) } + +func TestQueryHeight(t *testing.T) { + e := newExecutor(t, true) + + e.Run(t, "neo-go", "query", "height", "--rpc-endpoint", "http://"+e.RPC.Addr) + e.checkNextLine(t, `^Latest block: [0-9]+$`) + e.checkNextLine(t, `^Validated state: [0-9]+$`) + e.checkEOF(t) +} diff --git a/docs/cli.md b/docs/cli.md index dbc2fd359..7dc4a9e02 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -466,6 +466,14 @@ You can also vote for candidates if you own NEO: ### Getting data from chain +#### Node height/validated height +`query height` returns the latest block and validated state height: +``` +$ ./bin/neo-go query height -r http://localhost:20332 +Latest block: 11926 +Validated state: 11926 +``` + #### Transaction status `query tx` provides convenient wrapper over RPC calls to query transaction status. ```