Merge pull request #684 from nspcc-dev/feature/getblocksysfee

rpc: implement getblocksysfee RPC
This commit is contained in:
Roman Khimov 2020-02-21 18:48:56 +03:00 committed by GitHub
commit 5d4dfbfa1a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 1 deletions

View file

@ -42,7 +42,7 @@ which would yield the response:
| `getblock` | Yes | | `getblock` | Yes |
| `getblockcount` | Yes | | `getblockcount` | Yes |
| `getblockhash` | Yes | | `getblockhash` | Yes |
| `getblocksysfee` | No (#341) | | `getblocksysfee` | Yes |
| `getconnectioncount` | Yes | | `getconnectioncount` | Yes |
| `getcontractstate` | Yes | | `getcontractstate` | Yes |
| `getnep5balances` | No (#498) | | `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( getconnectioncountCalled = prometheus.NewCounter(
prometheus.CounterOpts{ prometheus.CounterOpts{
Help: "Number of calls to getconnectioncount rpc endpoint", Help: "Number of calls to getconnectioncount rpc endpoint",
@ -139,6 +147,7 @@ func init() {
getbestblockCalled, getbestblockCalled,
getblockcountCalled, getblockcountCalled,
getblockHashCalled, getblockHashCalled,
getblocksysfeeCalled,
getconnectioncountCalled, getconnectioncountCalled,
getcontractstateCalled, getcontractstateCalled,
getversionCalled, getversionCalled,

View file

@ -181,6 +181,10 @@ Methods:
results = s.chain.GetHeaderHash(num) results = s.chain.GetHeaderHash(num)
case "getblocksysfee":
getblocksysfeeCalled.Inc()
results, resultsErr = s.getBlockSysFee(reqParams)
case "getconnectioncount": case "getconnectioncount":
getconnectioncountCalled.Inc() getconnectioncountCalled.Inc()
results = s.coreServer.PeerCount() results = s.coreServer.PeerCount()
@ -430,6 +434,32 @@ func (s *Server) getAccountState(reqParams request.Params, unspents bool) (inter
return results, resultsErr 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. // invoke implements the `invoke` RPC call.
func (s *Server) invoke(reqParams request.Params) (interface{}, error) { func (s *Server) invoke(reqParams request.Params) (interface{}, error) {
scriptHashHex, ok := reqParams.ValueWithType(0, request.StringT) scriptHashHex, ok := reqParams.ValueWithType(0, request.StringT)

View file

@ -295,6 +295,36 @@ var rpcTestCases = map[string][]rpcTestCase{
fail: true, 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": { "getconnectioncount": {
{ {
params: "[]", params: "[]",