From bed08d3f89c1f8794ef9fe571f214a09a5798229 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 4 Jun 2020 15:43:37 +0300 Subject: [PATCH] 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. --- pkg/rpc/request/param.go | 15 +++++++++++++++ pkg/rpc/server/server.go | 28 ++++------------------------ 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/pkg/rpc/request/param.go b/pkg/rpc/request/param.go index 7df182043..d15923624 100644 --- a/pkg/rpc/request/param.go +++ b/pkg/rpc/request/param.go @@ -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 { diff --git a/pkg/rpc/server/server.go b/pkg/rpc/server/server.go index 5fce6c7be..36c5e367b 100644 --- a/pkg/rpc/server/server.go +++ b/pkg/rpc/server/server.go @@ -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)