diff --git a/docs/rpc.md b/docs/rpc.md index 9efe72589..be814cd45 100644 --- a/docs/rpc.md +++ b/docs/rpc.md @@ -53,7 +53,7 @@ which would yield the response: | `getrawmempool` | Yes | | `getrawtransaction` | Yes | | `getstorage` | Yes | -| `gettransactionheight` | No (#713) | +| `gettransactionheight` | Yes | | `gettxout` | Yes | | `getunclaimed` | No (#712) | | `getunspents` | Yes | diff --git a/pkg/rpc/server/prometheus.go b/pkg/rpc/server/prometheus.go index f8fea1207..79d3d838f 100644 --- a/pkg/rpc/server/prometheus.go +++ b/pkg/rpc/server/prometheus.go @@ -147,6 +147,14 @@ var ( }, ) + gettransactionheightCalled = prometheus.NewCounter( + prometheus.CounterOpts{ + Help: "Number of calls to gettransactionheight rpc endpoint", + Name: "gettransactionheight_called", + Namespace: "neogo", + }, + ) + gettxoutCalled = prometheus.NewCounter( prometheus.CounterOpts{ Help: "Number of calls to gettxout rpc endpoint", @@ -206,6 +214,7 @@ func init() { getassetstateCalled, getaccountstateCalled, getunspentsCalled, + gettransactionheightCalled, gettxoutCalled, getrawtransactionCalled, sendrawtransactionCalled, diff --git a/pkg/rpc/server/server.go b/pkg/rpc/server/server.go index 93084fc39..c70d90225 100644 --- a/pkg/rpc/server/server.go +++ b/pkg/rpc/server/server.go @@ -285,6 +285,10 @@ Methods: getrawtransactionCalled.Inc() results, resultsErr = s.getrawtransaction(reqParams) + case "gettransactionheight": + gettransactionheightCalled.Inc() + results, resultsErr = s.getTransactionHeight(reqParams) + case "gettxout": gettxoutCalled.Inc() results, resultsErr = s.getTxOut(reqParams) @@ -589,6 +593,25 @@ func (s *Server) getrawtransaction(reqParams request.Params) (interface{}, error return results, resultsErr } +func (s *Server) getTransactionHeight(ps request.Params) (interface{}, error) { + p, ok := ps.Value(0) + if !ok { + return nil, response.ErrInvalidParams + } + + h, err := p.GetUint256() + if err != nil { + return nil, response.ErrInvalidParams + } + + _, height, err := s.chain.GetTransaction(h) + if err != nil { + return nil, response.NewRPCError("unknown transaction", "", nil) + } + + return height, nil +} + func (s *Server) getTxOut(ps request.Params) (interface{}, error) { p, ok := ps.Value(0) if !ok { diff --git a/pkg/rpc/server/server_test.go b/pkg/rpc/server/server_test.go index 902fdca75..9636a31a0 100644 --- a/pkg/rpc/server/server_test.go +++ b/pkg/rpc/server/server_test.go @@ -576,6 +576,31 @@ var rpcTestCases = map[string][]rpcTestCase{ fail: true, }, }, + "gettransactionheight": { + { + name: "poositive", + params: `["3fee783413c27849c8ee2656fd757a7483de64f4e78bd7897f30ecdf42ce788b"]`, + result: func(e *executor) interface{} { + h := 202 + return &h + }, + }, + { + name: "no params", + params: `[]`, + fail: true, + }, + { + name: "invalid hash", + params: `["notahex"]`, + fail: true, + }, + { + name: "missing hash", + params: `["` + util.Uint256{}.String() + `"]`, + fail: true, + }, + }, "getunspents": { { name: "positive",