github.com/jeffallen/go-ethereum@v1.1.4-0.20150910155051-571d3236c49c/rpc/api/miner_args.go (about)

     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  
    17  package api
    18  
    19  import (
    20  	"encoding/json"
    21  
    22  	"math/big"
    23  
    24  	"github.com/ethereum/go-ethereum/common"
    25  	"github.com/ethereum/go-ethereum/rpc/shared"
    26  )
    27  
    28  type StartMinerArgs struct {
    29  	Threads int
    30  }
    31  
    32  func (args *StartMinerArgs) UnmarshalJSON(b []byte) (err error) {
    33  	var obj []interface{}
    34  	if err := json.Unmarshal(b, &obj); err != nil {
    35  		return shared.NewDecodeParamError(err.Error())
    36  	}
    37  
    38  	if len(obj) == 0 || obj[0] == nil {
    39  		args.Threads = -1
    40  		return nil
    41  	}
    42  
    43  	var num *big.Int
    44  	if num, err = numString(obj[0]); err != nil {
    45  		return err
    46  	}
    47  	args.Threads = int(num.Int64())
    48  	return nil
    49  }
    50  
    51  type SetExtraArgs struct {
    52  	Data string
    53  }
    54  
    55  func (args *SetExtraArgs) UnmarshalJSON(b []byte) (err error) {
    56  	var obj []interface{}
    57  	if err := json.Unmarshal(b, &obj); err != nil {
    58  		return shared.NewDecodeParamError(err.Error())
    59  	}
    60  
    61  	if len(obj) < 1 {
    62  		return shared.NewInsufficientParamsError(len(obj), 1)
    63  	}
    64  
    65  	extrastr, ok := obj[0].(string)
    66  	if !ok {
    67  		return shared.NewInvalidTypeError("Price", "not a string")
    68  	}
    69  	args.Data = extrastr
    70  
    71  	return nil
    72  }
    73  
    74  type GasPriceArgs struct {
    75  	Price string
    76  }
    77  
    78  func (args *GasPriceArgs) UnmarshalJSON(b []byte) (err error) {
    79  	var obj []interface{}
    80  	if err := json.Unmarshal(b, &obj); err != nil {
    81  		return shared.NewDecodeParamError(err.Error())
    82  	}
    83  
    84  	if len(obj) < 1 {
    85  		return shared.NewInsufficientParamsError(len(obj), 1)
    86  	}
    87  
    88  	if pricestr, ok := obj[0].(string); ok {
    89  		args.Price = pricestr
    90  		return nil
    91  	}
    92  
    93  	return shared.NewInvalidTypeError("Price", "not a string")
    94  }
    95  
    96  type SetEtherbaseArgs struct {
    97  	Etherbase common.Address
    98  }
    99  
   100  func (args *SetEtherbaseArgs) UnmarshalJSON(b []byte) (err error) {
   101  	var obj []interface{}
   102  	if err := json.Unmarshal(b, &obj); err != nil {
   103  		return shared.NewDecodeParamError(err.Error())
   104  	}
   105  
   106  	if len(obj) < 1 {
   107  		return shared.NewInsufficientParamsError(len(obj), 1)
   108  	}
   109  
   110  	if addr, ok := obj[0].(string); ok {
   111  		args.Etherbase = common.HexToAddress(addr)
   112  		if (args.Etherbase == common.Address{}) {
   113  			return shared.NewInvalidTypeError("Etherbase", "not a valid address")
   114  		}
   115  		return nil
   116  	}
   117  
   118  	return shared.NewInvalidTypeError("Etherbase", "not a string")
   119  }
   120  
   121  type MakeDAGArgs struct {
   122  	BlockNumber int64
   123  }
   124  
   125  func (args *MakeDAGArgs) UnmarshalJSON(b []byte) (err error) {
   126  	args.BlockNumber = -1
   127  	var obj []interface{}
   128  
   129  	if err := json.Unmarshal(b, &obj); err != nil {
   130  		return shared.NewDecodeParamError(err.Error())
   131  	}
   132  
   133  	if len(obj) < 1 {
   134  		return shared.NewInsufficientParamsError(len(obj), 1)
   135  	}
   136  
   137  	if err := blockHeight(obj[0], &args.BlockNumber); err != nil {
   138  		return err
   139  	}
   140  
   141  	return nil
   142  }