From fe8038e8b70899dab5be94db1b31c564ccd40954 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 4 Jun 2020 11:09:07 +0300 Subject: [PATCH] rpc/server: implement getstateheight RPC --- pkg/rpc/response/result/mpt.go | 7 +++++++ pkg/rpc/server/server.go | 9 +++++++++ pkg/rpc/server/server_test.go | 15 +++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 pkg/rpc/response/result/mpt.go diff --git a/pkg/rpc/response/result/mpt.go b/pkg/rpc/response/result/mpt.go new file mode 100644 index 000000000..65224e726 --- /dev/null +++ b/pkg/rpc/response/result/mpt.go @@ -0,0 +1,7 @@ +package result + +// StateHeight is a result of getstateheight RPC. +type StateHeight struct { + BlockHeight uint32 `json:"blockHeight"` + StateHeight uint32 `json:"stateHeight"` +} diff --git a/pkg/rpc/server/server.go b/pkg/rpc/server/server.go index b38129bb1..dcb8de045 100644 --- a/pkg/rpc/server/server.go +++ b/pkg/rpc/server/server.go @@ -95,6 +95,7 @@ var rpcHandlers = map[string]func(*Server, request.Params) (interface{}, *respon "getpeers": (*Server).getPeers, "getrawmempool": (*Server).getRawMempool, "getrawtransaction": (*Server).getrawtransaction, + "getstateheight": (*Server).getStateHeight, "getstateroot": (*Server).getStateRoot, "getstorage": (*Server).getStorage, "gettransactionheight": (*Server).getTransactionHeight, @@ -688,6 +689,14 @@ func (s *Server) getDecimals(h util.Uint160, cache map[util.Uint160]int64) (int6 return d, nil } +func (s *Server) getStateHeight(_ request.Params) (interface{}, *response.Error) { + height := s.chain.BlockHeight() + return &result.StateHeight{ + BlockHeight: height, + StateHeight: height, + }, nil +} + func (s *Server) getStateRoot(ps request.Params) (interface{}, *response.Error) { p := ps.Value(0) if p == nil { diff --git a/pkg/rpc/server/server_test.go b/pkg/rpc/server/server_test.go index c6d384f80..f4853c8a8 100644 --- a/pkg/rpc/server/server_test.go +++ b/pkg/rpc/server/server_test.go @@ -214,6 +214,21 @@ var rpcTestCases = map[string][]rpcTestCase{ }, }, }, + "getstateheight": { + { + name: "positive", + params: `[]`, + result: func(_ *executor) interface{} { return new(result.StateHeight) }, + check: func(t *testing.T, e *executor, res interface{}) { + sh, ok := res.(*result.StateHeight) + require.True(t, ok) + + h := e.chain.BlockHeight() + require.Equal(t, h, sh.BlockHeight) + require.Equal(t, h, sh.StateHeight) + }, + }, + }, "getstateroot": { { name: "no params",