rpc: implement gettransactionheight

closes #713
This commit is contained in:
Anna Shaleva 2020-03-05 17:20:50 +03:00
parent f72c321426
commit ccd88c3af8
4 changed files with 58 additions and 1 deletions

View file

@ -53,7 +53,7 @@ which would yield the response:
| `getrawmempool` | Yes | | `getrawmempool` | Yes |
| `getrawtransaction` | Yes | | `getrawtransaction` | Yes |
| `getstorage` | Yes | | `getstorage` | Yes |
| `gettransactionheight` | No (#713) | | `gettransactionheight` | Yes |
| `gettxout` | Yes | | `gettxout` | Yes |
| `getunclaimed` | No (#712) | | `getunclaimed` | No (#712) |
| `getunspents` | Yes | | `getunspents` | Yes |

View file

@ -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( gettxoutCalled = prometheus.NewCounter(
prometheus.CounterOpts{ prometheus.CounterOpts{
Help: "Number of calls to gettxout rpc endpoint", Help: "Number of calls to gettxout rpc endpoint",
@ -206,6 +214,7 @@ func init() {
getassetstateCalled, getassetstateCalled,
getaccountstateCalled, getaccountstateCalled,
getunspentsCalled, getunspentsCalled,
gettransactionheightCalled,
gettxoutCalled, gettxoutCalled,
getrawtransactionCalled, getrawtransactionCalled,
sendrawtransactionCalled, sendrawtransactionCalled,

View file

@ -285,6 +285,10 @@ Methods:
getrawtransactionCalled.Inc() getrawtransactionCalled.Inc()
results, resultsErr = s.getrawtransaction(reqParams) results, resultsErr = s.getrawtransaction(reqParams)
case "gettransactionheight":
gettransactionheightCalled.Inc()
results, resultsErr = s.getTransactionHeight(reqParams)
case "gettxout": case "gettxout":
gettxoutCalled.Inc() gettxoutCalled.Inc()
results, resultsErr = s.getTxOut(reqParams) results, resultsErr = s.getTxOut(reqParams)
@ -589,6 +593,25 @@ func (s *Server) getrawtransaction(reqParams request.Params) (interface{}, error
return results, resultsErr 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) { func (s *Server) getTxOut(ps request.Params) (interface{}, error) {
p, ok := ps.Value(0) p, ok := ps.Value(0)
if !ok { if !ok {

View file

@ -576,6 +576,31 @@ var rpcTestCases = map[string][]rpcTestCase{
fail: true, 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": { "getunspents": {
{ {
name: "positive", name: "positive",