From 2ef9ec0756a5fe80458a5f253cdb240aaafd00c8 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 5 Jun 2020 16:02:55 +0300 Subject: [PATCH] rpc/server: allow specifying number for getblockheader As it should be allowed. --- pkg/rpc/server/server.go | 33 +++++++++++++++++++++------------ pkg/rpc/server/server_test.go | 4 ++++ 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/pkg/rpc/server/server.go b/pkg/rpc/server/server.go index 4615e4509..4145d7bf9 100644 --- a/pkg/rpc/server/server.go +++ b/pkg/rpc/server/server.go @@ -382,31 +382,39 @@ func (s *Server) getConnectionCount(_ request.Params) (interface{}, *response.Er return s.coreServer.PeerCount(), nil } -func (s *Server) getBlock(reqParams request.Params) (interface{}, *response.Error) { +func (s *Server) blockHashFromParam(param *request.Param) (util.Uint256, *response.Error) { var hash util.Uint256 - param, ok := reqParams.Value(0) - if !ok { - return nil, response.ErrInvalidParams - } - switch param.Type { case request.StringT: var err error hash, err = param.GetUint256() if err != nil { - return nil, response.ErrInvalidParams + return hash, response.ErrInvalidParams } case request.NumberT: num, err := s.blockHeightFromParam(param) if err != nil { - return nil, response.ErrInvalidParams + return hash, response.ErrInvalidParams } hash = s.chain.GetHeaderHash(num) default: + return hash, response.ErrInvalidParams + } + return hash, nil +} + +func (s *Server) getBlock(reqParams request.Params) (interface{}, *response.Error) { + param, ok := reqParams.Value(0) + if !ok { return nil, response.ErrInvalidParams } + hash, respErr := s.blockHashFromParam(param) + if respErr != nil { + return nil, respErr + } + block, err := s.chain.GetBlock(hash) if err != nil { return nil, response.NewInternalServerError(fmt.Sprintf("Problem locating block with hash: %s", hash), err) @@ -771,13 +779,14 @@ func (s *Server) getBlockSysFee(reqParams request.Params) (interface{}, *respons func (s *Server) getBlockHeader(reqParams request.Params) (interface{}, *response.Error) { var verbose bool - param, ok := reqParams.ValueWithType(0, request.StringT) + param, ok := reqParams.Value(0) if !ok { return nil, response.ErrInvalidParams } - hash, err := param.GetUint256() - if err != nil { - return nil, response.ErrInvalidParams + + hash, respErr := s.blockHashFromParam(param) + if respErr != nil { + return nil, respErr } param, ok = reqParams.ValueWithType(1, request.NumberT) diff --git a/pkg/rpc/server/server_test.go b/pkg/rpc/server/server_test.go index 3db6daa48..e66498e94 100644 --- a/pkg/rpc/server/server_test.go +++ b/pkg/rpc/server/server_test.go @@ -910,6 +910,10 @@ func testRPCProtocol(t *testing.T, doRPCCall func(string, string, *testing.T) [] t.Run("verbose=0", func(t *testing.T) { runCase(t, fmt.Sprintf(rpc, `["`+testHeaderHash+`", 0]`), &encoded, new(string)) }) + + t.Run("by number", func(t *testing.T) { + runCase(t, fmt.Sprintf(rpc, `[1]`), &encoded, new(string)) + }) }) t.Run("verbose != 0", func(t *testing.T) {