rpc/server: allow specifying number for getblockheader

As it should be allowed.
This commit is contained in:
Roman Khimov 2020-06-05 16:02:55 +03:00
parent dfb26f9ab2
commit 2ef9ec0756
2 changed files with 25 additions and 12 deletions

View file

@ -382,31 +382,39 @@ func (s *Server) getConnectionCount(_ request.Params) (interface{}, *response.Er
return s.coreServer.PeerCount(), nil 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 var hash util.Uint256
param, ok := reqParams.Value(0)
if !ok {
return nil, response.ErrInvalidParams
}
switch param.Type { switch param.Type {
case request.StringT: case request.StringT:
var err error var err error
hash, err = param.GetUint256() hash, err = param.GetUint256()
if err != nil { if err != nil {
return nil, response.ErrInvalidParams return hash, response.ErrInvalidParams
} }
case request.NumberT: case request.NumberT:
num, err := s.blockHeightFromParam(param) num, err := s.blockHeightFromParam(param)
if err != nil { if err != nil {
return nil, response.ErrInvalidParams return hash, response.ErrInvalidParams
} }
hash = s.chain.GetHeaderHash(num) hash = s.chain.GetHeaderHash(num)
default: 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 return nil, response.ErrInvalidParams
} }
hash, respErr := s.blockHashFromParam(param)
if respErr != nil {
return nil, respErr
}
block, err := s.chain.GetBlock(hash) block, err := s.chain.GetBlock(hash)
if err != nil { if err != nil {
return nil, response.NewInternalServerError(fmt.Sprintf("Problem locating block with hash: %s", hash), err) 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) { func (s *Server) getBlockHeader(reqParams request.Params) (interface{}, *response.Error) {
var verbose bool var verbose bool
param, ok := reqParams.ValueWithType(0, request.StringT) param, ok := reqParams.Value(0)
if !ok { if !ok {
return nil, response.ErrInvalidParams return nil, response.ErrInvalidParams
} }
hash, err := param.GetUint256()
if err != nil { hash, respErr := s.blockHashFromParam(param)
return nil, response.ErrInvalidParams if respErr != nil {
return nil, respErr
} }
param, ok = reqParams.ValueWithType(1, request.NumberT) param, ok = reqParams.ValueWithType(1, request.NumberT)

View file

@ -910,6 +910,10 @@ func testRPCProtocol(t *testing.T, doRPCCall func(string, string, *testing.T) []
t.Run("verbose=0", func(t *testing.T) { t.Run("verbose=0", func(t *testing.T) {
runCase(t, fmt.Sprintf(rpc, `["`+testHeaderHash+`", 0]`), &encoded, new(string)) 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) { t.Run("verbose != 0", func(t *testing.T) {