rpc/server: unify boolean flag handling

Implement (*Param).GetBoolean() for converting parameter to bool value.
It is used for verbosity flag and is false iff it is either zero number
or empty sting.
This commit is contained in:
Evgenii Stratonikov 2020-06-04 15:43:37 +03:00 committed by Roman Khimov
parent 35f952e44f
commit bed08d3f89
2 changed files with 19 additions and 24 deletions

View file

@ -87,6 +87,21 @@ func (p *Param) GetString() (string, error) {
return str, nil
}
// GetBoolean returns boolean value of the parameter.
func (p *Param) GetBoolean() bool {
if p == nil {
return false
}
switch p.Type {
case NumberT:
return p.Value != 0
case StringT:
return p.Value != ""
default:
return true
}
}
// GetInt returns int value of te parameter.
func (p *Param) GetInt() (int, error) {
if p == nil {

View file

@ -421,7 +421,7 @@ func (s *Server) getBlock(reqParams request.Params) (interface{}, *response.Erro
return nil, response.NewInternalServerError(fmt.Sprintf("Problem locating block with hash: %s", hash), err)
}
if len(reqParams) == 2 && reqParams[1].Value == 1 {
if reqParams.Value(1).GetBoolean() {
return result.NewBlock(block, s.chain), nil
}
writer := io.NewBufBinWriter()
@ -681,26 +681,12 @@ func (s *Server) getrawtransaction(reqParams request.Params) (interface{}, *resp
} else if tx, height, err := s.chain.GetTransaction(txHash); err != nil {
err = errors.Wrapf(err, "Invalid transaction hash: %s", txHash)
return nil, response.NewRPCError("Unknown transaction", err.Error(), err)
} else if len(reqParams) >= 2 {
} else if reqParams.Value(1).GetBoolean() {
_header := s.chain.GetHeaderHash(int(height))
header, err := s.chain.GetHeader(_header)
if err != nil {
resultsErr = response.NewInvalidParamsError(err.Error(), err)
}
param1 := reqParams.Value(1)
if param1 == nil {
param1 = &request.Param{}
}
switch v := param1.Value.(type) {
case int, float64, bool, string:
if v == 0 || v == "0" || v == 0.0 || v == false || v == "false" {
results = hex.EncodeToString(tx.Bytes())
} else {
results = result.NewTransactionOutputRaw(tx, header, s.chain)
}
default:
} else {
results = result.NewTransactionOutputRaw(tx, header, s.chain)
}
} else {
@ -765,19 +751,13 @@ func (s *Server) getBlockSysFee(reqParams request.Params) (interface{}, *respons
// getBlockHeader returns the corresponding block header information according to the specified script hash.
func (s *Server) getBlockHeader(reqParams request.Params) (interface{}, *response.Error) {
var verbose bool
param := reqParams.Value(0)
hash, respErr := s.blockHashFromParam(param)
if respErr != nil {
return nil, respErr
}
v, err := reqParams.ValueWithType(1, request.NumberT).GetInt()
if err == nil {
verbose = v != 0
}
verbose := reqParams.Value(1).GetBoolean()
h, err := s.chain.GetHeader(hash)
if err != nil {
return nil, response.NewRPCError("unknown block", "", nil)