debug_args.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package api
  17. import (
  18. "encoding/json"
  19. "fmt"
  20. "math/big"
  21. "reflect"
  22. "github.com/ethereum/go-ethereum/rpc/shared"
  23. )
  24. type WaitForBlockArgs struct {
  25. MinHeight int
  26. Timeout int // in seconds
  27. }
  28. func (args *WaitForBlockArgs) UnmarshalJSON(b []byte) (err error) {
  29. var obj []interface{}
  30. if err := json.Unmarshal(b, &obj); err != nil {
  31. return shared.NewDecodeParamError(err.Error())
  32. }
  33. if len(obj) > 2 {
  34. return fmt.Errorf("waitForArgs needs 0, 1, 2 arguments")
  35. }
  36. // default values when not provided
  37. args.MinHeight = -1
  38. args.Timeout = -1
  39. if len(obj) >= 1 {
  40. var minHeight *big.Int
  41. if minHeight, err = numString(obj[0]); err != nil {
  42. return err
  43. }
  44. args.MinHeight = int(minHeight.Int64())
  45. }
  46. if len(obj) >= 2 {
  47. timeout, err := numString(obj[1])
  48. if err != nil {
  49. return err
  50. }
  51. args.Timeout = int(timeout.Int64())
  52. }
  53. return nil
  54. }
  55. type MetricsArgs struct {
  56. Raw bool
  57. }
  58. func (args *MetricsArgs) UnmarshalJSON(b []byte) (err error) {
  59. var obj []interface{}
  60. if err := json.Unmarshal(b, &obj); err != nil {
  61. return shared.NewDecodeParamError(err.Error())
  62. }
  63. if len(obj) > 1 {
  64. return fmt.Errorf("metricsArgs needs 0, 1 arguments")
  65. }
  66. // default values when not provided
  67. if len(obj) >= 1 && obj[0] != nil {
  68. if value, ok := obj[0].(bool); !ok {
  69. return fmt.Errorf("invalid argument %v", reflect.TypeOf(obj[0]))
  70. } else {
  71. args.Raw = value
  72. }
  73. }
  74. return nil
  75. }