diff --git a/docs/rpc.md b/docs/rpc.md index 68c68fdf9..51e070d0a 100644 --- a/docs/rpc.md +++ b/docs/rpc.md @@ -40,6 +40,7 @@ which would yield the response: | `getblockcount` | | `getblockhash` | | `getblockheader` | +| `getblockheadercount` | | `getblocksysfee` | | `getconnectioncount` | | `getcontractstate` | diff --git a/pkg/rpc/client/rpc.go b/pkg/rpc/client/rpc.go index 2f51150ab..dc3f6d73e 100644 --- a/pkg/rpc/client/rpc.go +++ b/pkg/rpc/client/rpc.go @@ -155,6 +155,15 @@ func (c *Client) GetBlockHeader(hash util.Uint256) (*block.Header, error) { return h, nil } +// GetBlockHeaderCount returns the number of headers in the main chain. +func (c *Client) GetBlockHeaderCount() (uint32, error) { + var resp uint32 + if err := c.performRequest("getblockheadercount", request.NewRawParams(), &resp); err != nil { + return resp, err + } + return resp, nil +} + // GetBlockHeaderVerbose returns the corresponding block header information from Json format string // according to the specified script hash. func (c *Client) GetBlockHeaderVerbose(hash util.Uint256) (*result.Header, error) { diff --git a/pkg/rpc/client/rpc_test.go b/pkg/rpc/client/rpc_test.go index a0d01a6a4..163e3f16a 100644 --- a/pkg/rpc/client/rpc_test.go +++ b/pkg/rpc/client/rpc_test.go @@ -280,6 +280,18 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{ }, }, }, + "getblockheadercount": { + { + name: "positive", + invoke: func(c *Client) (interface{}, error) { + return c.GetBlockHeaderCount() + }, + serverResponse: `{"jsonrpc":"2.0","id":1,"result":2021}`, + result: func(c *Client) interface{} { + return uint32(2021) + }, + }, + }, "getblocksysfee": { { name: "positive", diff --git a/pkg/rpc/server/server.go b/pkg/rpc/server/server.go index 6e1ca675e..54d9e6799 100644 --- a/pkg/rpc/server/server.go +++ b/pkg/rpc/server/server.go @@ -29,12 +29,12 @@ import ( "github.com/nspcc-dev/neo-go/pkg/encoding/address" "github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/network" - "github.com/nspcc-dev/neo-go/pkg/services/oracle" - "github.com/nspcc-dev/neo-go/pkg/services/oracle/broadcaster" "github.com/nspcc-dev/neo-go/pkg/rpc" "github.com/nspcc-dev/neo-go/pkg/rpc/request" "github.com/nspcc-dev/neo-go/pkg/rpc/response" "github.com/nspcc-dev/neo-go/pkg/rpc/response/result" + "github.com/nspcc-dev/neo-go/pkg/services/oracle" + "github.com/nspcc-dev/neo-go/pkg/services/oracle/broadcaster" "github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag" "github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest" "github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger" @@ -99,6 +99,7 @@ var rpcHandlers = map[string]func(*Server, request.Params) (interface{}, *respon "getblockcount": (*Server).getBlockCount, "getblockhash": (*Server).getBlockHash, "getblockheader": (*Server).getBlockHeader, + "getblockheadercount": (*Server).getBlockHeaderCount, "getblocksysfee": (*Server).getBlockSysFee, "getcommittee": (*Server).getCommittee, "getconnectioncount": (*Server).getConnectionCount, @@ -433,6 +434,10 @@ func (s *Server) getBlockCount(_ request.Params) (interface{}, *response.Error) return s.chain.BlockHeight() + 1, nil } +func (s *Server) getBlockHeaderCount(_ request.Params) (interface{}, *response.Error) { + return s.chain.HeaderHeight() + 1, nil +} + func (s *Server) getConnectionCount(_ request.Params) (interface{}, *response.Error) { return s.coreServer.PeerCount(), nil } diff --git a/pkg/rpc/server/server_test.go b/pkg/rpc/server/server_test.go index b99011ef4..5ea5ffac5 100644 --- a/pkg/rpc/server/server_test.go +++ b/pkg/rpc/server/server_test.go @@ -494,6 +494,15 @@ var rpcTestCases = map[string][]rpcTestCase{ fail: true, }, }, + "getblockheadercount": { + { + params: "[]", + result: func(e *executor) interface{} { + v := int(e.chain.HeaderHeight() + 1) + return &v + }, + }, + }, "getblocksysfee": { { name: "positive",