miner_args.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library 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. // The go-ethereum library 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 the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package api
  17. import (
  18. "encoding/json"
  19. "math/big"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/rpc/shared"
  22. )
  23. type StartMinerArgs struct {
  24. Threads int
  25. }
  26. func (args *StartMinerArgs) UnmarshalJSON(b []byte) (err error) {
  27. var obj []interface{}
  28. if err := json.Unmarshal(b, &obj); err != nil {
  29. return shared.NewDecodeParamError(err.Error())
  30. }
  31. if len(obj) == 0 || obj[0] == nil {
  32. args.Threads = -1
  33. return nil
  34. }
  35. var num *big.Int
  36. if num, err = numString(obj[0]); err != nil {
  37. return err
  38. }
  39. args.Threads = int(num.Int64())
  40. return nil
  41. }
  42. type SetExtraArgs struct {
  43. Data string
  44. }
  45. func (args *SetExtraArgs) UnmarshalJSON(b []byte) (err error) {
  46. var obj []interface{}
  47. if err := json.Unmarshal(b, &obj); err != nil {
  48. return shared.NewDecodeParamError(err.Error())
  49. }
  50. if len(obj) < 1 {
  51. return shared.NewInsufficientParamsError(len(obj), 1)
  52. }
  53. extrastr, ok := obj[0].(string)
  54. if !ok {
  55. return shared.NewInvalidTypeError("Price", "not a string")
  56. }
  57. args.Data = extrastr
  58. return nil
  59. }
  60. type GasPriceArgs struct {
  61. Price string
  62. }
  63. func (args *GasPriceArgs) UnmarshalJSON(b []byte) (err error) {
  64. var obj []interface{}
  65. if err := json.Unmarshal(b, &obj); err != nil {
  66. return shared.NewDecodeParamError(err.Error())
  67. }
  68. if len(obj) < 1 {
  69. return shared.NewInsufficientParamsError(len(obj), 1)
  70. }
  71. if pricestr, ok := obj[0].(string); ok {
  72. args.Price = pricestr
  73. return nil
  74. }
  75. return shared.NewInvalidTypeError("Price", "not a string")
  76. }
  77. type SetEtherbaseArgs struct {
  78. Etherbase common.Address
  79. }
  80. func (args *SetEtherbaseArgs) UnmarshalJSON(b []byte) (err error) {
  81. var obj []interface{}
  82. if err := json.Unmarshal(b, &obj); err != nil {
  83. return shared.NewDecodeParamError(err.Error())
  84. }
  85. if len(obj) < 1 {
  86. return shared.NewInsufficientParamsError(len(obj), 1)
  87. }
  88. if addr, ok := obj[0].(string); ok {
  89. args.Etherbase = common.HexToAddress(addr)
  90. if (args.Etherbase == common.Address{}) {
  91. return shared.NewInvalidTypeError("Etherbase", "not a valid address")
  92. }
  93. return nil
  94. }
  95. return shared.NewInvalidTypeError("Etherbase", "not a string")
  96. }
  97. type MakeDAGArgs struct {
  98. BlockNumber int64
  99. }
  100. func (args *MakeDAGArgs) UnmarshalJSON(b []byte) (err error) {
  101. args.BlockNumber = -1
  102. var obj []interface{}
  103. if err := json.Unmarshal(b, &obj); err != nil {
  104. return shared.NewDecodeParamError(err.Error())
  105. }
  106. if len(obj) < 1 {
  107. return shared.NewInsufficientParamsError(len(obj), 1)
  108. }
  109. if err := blockHeight(obj[0], &args.BlockNumber); err != nil {
  110. return err
  111. }
  112. return nil
  113. }