rpc: move getblock RPC to a separate func
This commit is contained in:
parent
e442ebdb69
commit
2ec5b9f08a
1 changed files with 39 additions and 42 deletions
|
@ -155,7 +155,6 @@ func (s *Server) methodHandler(w http.ResponseWriter, req *request.In, reqParams
|
||||||
|
|
||||||
incCounter(req.Method)
|
incCounter(req.Method)
|
||||||
|
|
||||||
Methods:
|
|
||||||
switch req.Method {
|
switch req.Method {
|
||||||
case "getapplicationlog":
|
case "getapplicationlog":
|
||||||
results, resultsErr = s.getApplicationLog(reqParams)
|
results, resultsErr = s.getApplicationLog(reqParams)
|
||||||
|
@ -164,47 +163,7 @@ Methods:
|
||||||
results = "0x" + s.chain.CurrentBlockHash().StringLE()
|
results = "0x" + s.chain.CurrentBlockHash().StringLE()
|
||||||
|
|
||||||
case "getblock":
|
case "getblock":
|
||||||
var hash util.Uint256
|
results, resultsErr = s.getBlock(reqParams)
|
||||||
|
|
||||||
param, ok := reqParams.Value(0)
|
|
||||||
if !ok {
|
|
||||||
resultsErr = response.ErrInvalidParams
|
|
||||||
break Methods
|
|
||||||
}
|
|
||||||
|
|
||||||
switch param.Type {
|
|
||||||
case request.StringT:
|
|
||||||
var err error
|
|
||||||
hash, err = param.GetUint256()
|
|
||||||
if err != nil {
|
|
||||||
resultsErr = response.ErrInvalidParams
|
|
||||||
break Methods
|
|
||||||
}
|
|
||||||
case request.NumberT:
|
|
||||||
num, err := s.blockHeightFromParam(param)
|
|
||||||
if err != nil {
|
|
||||||
resultsErr = response.ErrInvalidParams
|
|
||||||
break Methods
|
|
||||||
}
|
|
||||||
hash = s.chain.GetHeaderHash(num)
|
|
||||||
default:
|
|
||||||
resultsErr = response.ErrInvalidParams
|
|
||||||
break Methods
|
|
||||||
}
|
|
||||||
|
|
||||||
block, err := s.chain.GetBlock(hash)
|
|
||||||
if err != nil {
|
|
||||||
resultsErr = response.NewInternalServerError(fmt.Sprintf("Problem locating block with hash: %s", hash), err)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(reqParams) == 2 && reqParams[1].Value == 1 {
|
|
||||||
results = result.NewBlock(block, s.chain)
|
|
||||||
} else {
|
|
||||||
writer := io.NewBufBinWriter()
|
|
||||||
block.EncodeBinary(writer.BinWriter)
|
|
||||||
results = hex.EncodeToString(writer.Bytes())
|
|
||||||
}
|
|
||||||
|
|
||||||
case "getblockcount":
|
case "getblockcount":
|
||||||
results = s.chain.BlockHeight() + 1
|
results = s.chain.BlockHeight() + 1
|
||||||
|
@ -298,6 +257,44 @@ Methods:
|
||||||
s.WriteResponse(req, w, results)
|
s.WriteResponse(req, w, results)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Server) getBlock(reqParams request.Params) (interface{}, 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
|
||||||
|
}
|
||||||
|
case request.NumberT:
|
||||||
|
num, err := s.blockHeightFromParam(param)
|
||||||
|
if err != nil {
|
||||||
|
return nil, response.ErrInvalidParams
|
||||||
|
}
|
||||||
|
hash = s.chain.GetHeaderHash(num)
|
||||||
|
default:
|
||||||
|
return nil, response.ErrInvalidParams
|
||||||
|
}
|
||||||
|
|
||||||
|
block, err := s.chain.GetBlock(hash)
|
||||||
|
if err != nil {
|
||||||
|
return nil, response.NewInternalServerError(fmt.Sprintf("Problem locating block with hash: %s", hash), err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(reqParams) == 2 && reqParams[1].Value == 1 {
|
||||||
|
return result.NewBlock(block, s.chain), nil
|
||||||
|
}
|
||||||
|
writer := io.NewBufBinWriter()
|
||||||
|
block.EncodeBinary(writer.BinWriter)
|
||||||
|
return hex.EncodeToString(writer.Bytes()), nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Server) getBlockHash(reqParams request.Params) (interface{}, error) {
|
func (s *Server) getBlockHash(reqParams request.Params) (interface{}, error) {
|
||||||
param, ok := reqParams.ValueWithType(0, request.NumberT)
|
param, ok := reqParams.ValueWithType(0, request.NumberT)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|
Loading…
Reference in a new issue