rpc: implement getblocksysfee RPC

Closes #341
This commit is contained in:
Anna Shaleva 2020-02-19 12:44:31 +03:00
parent 56f87cd44e
commit 76a0a6e7e8
4 changed files with 70 additions and 1 deletions

View file

@ -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) |

View file

@ -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,

View file

@ -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)

View file

@ -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: "[]",