From 76a0a6e7e84385f42c67375d56ba049264c12867 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Wed, 19 Feb 2020 12:44:31 +0300 Subject: [PATCH] rpc: implement getblocksysfee RPC Closes #341 --- docs/rpc.md | 2 +- pkg/rpc/server/prometheus.go | 9 +++++++++ pkg/rpc/server/server.go | 30 ++++++++++++++++++++++++++++++ pkg/rpc/server/server_test.go | 30 ++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 1 deletion(-) diff --git a/docs/rpc.md b/docs/rpc.md index c83d03be0..101f671ed 100644 --- a/docs/rpc.md +++ b/docs/rpc.md @@ -42,7 +42,7 @@ which would yield the response: | `getblock` | Yes | | `getblockcount` | Yes | | `getblockhash` | Yes | -| `getblocksysfee` | No (#341) | +| `getblocksysfee` | Yes | | `getconnectioncount` | Yes | | `getcontractstate` | Yes | | `getnep5balances` | No (#498) | diff --git a/pkg/rpc/server/prometheus.go b/pkg/rpc/server/prometheus.go index 63312433d..ff165d3b6 100644 --- a/pkg/rpc/server/prometheus.go +++ b/pkg/rpc/server/prometheus.go @@ -36,6 +36,14 @@ var ( }, ) + getblocksysfeeCalled = prometheus.NewCounter( + prometheus.CounterOpts{ + Help: "Number of calls to getblocksysfee rpc endpoint", + Name: "getblocksysfee_called", + Namespace: "neogo", + }, + ) + getconnectioncountCalled = prometheus.NewCounter( prometheus.CounterOpts{ Help: "Number of calls to getconnectioncount rpc endpoint", @@ -139,6 +147,7 @@ func init() { getbestblockCalled, getblockcountCalled, getblockHashCalled, + getblocksysfeeCalled, getconnectioncountCalled, getcontractstateCalled, getversionCalled, diff --git a/pkg/rpc/server/server.go b/pkg/rpc/server/server.go index 805c41168..e5b84b85a 100644 --- a/pkg/rpc/server/server.go +++ b/pkg/rpc/server/server.go @@ -181,6 +181,10 @@ Methods: results = s.chain.GetHeaderHash(num) + case "getblocksysfee": + getblocksysfeeCalled.Inc() + results, resultsErr = s.getBlockSysFee(reqParams) + case "getconnectioncount": getconnectioncountCalled.Inc() results = s.coreServer.PeerCount() @@ -430,6 +434,32 @@ func (s *Server) getAccountState(reqParams request.Params, unspents bool) (inter return results, resultsErr } +// getBlockSysFee returns the system fees of the block, based on the specified index. +func (s *Server) getBlockSysFee(reqParams request.Params) (util.Fixed8, error) { + param, ok := reqParams.ValueWithType(0, request.NumberT) + if !ok { + return 0, response.ErrInvalidParams + } + + num, err := s.blockHeightFromParam(param) + if err != nil { + return 0, response.NewRPCError("Invalid height", "", nil) + } + + headerHash := s.chain.GetHeaderHash(num) + block, err := s.chain.GetBlock(headerHash) + if err != nil { + return 0, response.NewRPCError(err.Error(), "", nil) + } + + var blockSysFee util.Fixed8 + for _, tx := range block.Transactions { + blockSysFee += s.chain.SystemFee(tx) + } + + return blockSysFee, nil +} + // invoke implements the `invoke` RPC call. func (s *Server) invoke(reqParams request.Params) (interface{}, error) { scriptHashHex, ok := reqParams.ValueWithType(0, request.StringT) diff --git a/pkg/rpc/server/server_test.go b/pkg/rpc/server/server_test.go index cccddc12e..b94e213e8 100644 --- a/pkg/rpc/server/server_test.go +++ b/pkg/rpc/server/server_test.go @@ -295,6 +295,36 @@ var rpcTestCases = map[string][]rpcTestCase{ fail: true, }, }, + "getblocksysfee": { + { + name: "positive", + params: "[1]", + result: func(e *executor) interface{} { + block, _ := e.chain.GetBlock(e.chain.GetHeaderHash(1)) + + var expectedBlockSysFee util.Fixed8 + for _, tx := range block.Transactions { + expectedBlockSysFee += e.chain.SystemFee(tx) + } + return &expectedBlockSysFee + }, + }, + { + name: "no params", + params: `[]`, + fail: true, + }, + { + name: "string height", + params: `["first"]`, + fail: true, + }, + { + name: "invalid number height", + params: `[-2]`, + fail: true, + }, + }, "getconnectioncount": { { params: "[]",