github.com/jeffallen/go-ethereum@v1.1.4-0.20150910155051-571d3236c49c/rpc/api/miner.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 "github.com/ethereum/ethash" 21 "github.com/ethereum/go-ethereum/common" 22 "github.com/ethereum/go-ethereum/eth" 23 "github.com/ethereum/go-ethereum/rpc/codec" 24 "github.com/ethereum/go-ethereum/rpc/shared" 25 ) 26 27 const ( 28 MinerApiVersion = "1.0" 29 ) 30 31 var ( 32 // mapping between methods and handlers 33 MinerMapping = map[string]minerhandler{ 34 "miner_hashrate": (*minerApi).Hashrate, 35 "miner_makeDAG": (*minerApi).MakeDAG, 36 "miner_setExtra": (*minerApi).SetExtra, 37 "miner_setGasPrice": (*minerApi).SetGasPrice, 38 "miner_setEtherbase": (*minerApi).SetEtherbase, 39 "miner_startAutoDAG": (*minerApi).StartAutoDAG, 40 "miner_start": (*minerApi).StartMiner, 41 "miner_stopAutoDAG": (*minerApi).StopAutoDAG, 42 "miner_stop": (*minerApi).StopMiner, 43 } 44 ) 45 46 // miner callback handler 47 type minerhandler func(*minerApi, *shared.Request) (interface{}, error) 48 49 // miner api provider 50 type minerApi struct { 51 ethereum *eth.Ethereum 52 methods map[string]minerhandler 53 codec codec.ApiCoder 54 } 55 56 // create a new miner api instance 57 func NewMinerApi(ethereum *eth.Ethereum, coder codec.Codec) *minerApi { 58 return &minerApi{ 59 ethereum: ethereum, 60 methods: MinerMapping, 61 codec: coder.New(nil), 62 } 63 } 64 65 // Execute given request 66 func (self *minerApi) Execute(req *shared.Request) (interface{}, error) { 67 if callback, ok := self.methods[req.Method]; ok { 68 return callback(self, req) 69 } 70 71 return nil, &shared.NotImplementedError{req.Method} 72 } 73 74 // collection with supported methods 75 func (self *minerApi) Methods() []string { 76 methods := make([]string, len(self.methods)) 77 i := 0 78 for k := range self.methods { 79 methods[i] = k 80 i++ 81 } 82 return methods 83 } 84 85 func (self *minerApi) Name() string { 86 return shared.MinerApiName 87 } 88 89 func (self *minerApi) ApiVersion() string { 90 return MinerApiVersion 91 } 92 93 func (self *minerApi) StartMiner(req *shared.Request) (interface{}, error) { 94 args := new(StartMinerArgs) 95 if err := self.codec.Decode(req.Params, &args); err != nil { 96 return nil, err 97 } 98 if args.Threads == -1 { // (not specified by user, use default) 99 args.Threads = self.ethereum.MinerThreads 100 } 101 102 self.ethereum.StartAutoDAG() 103 err := self.ethereum.StartMining(args.Threads) 104 if err == nil { 105 return true, nil 106 } 107 108 return false, err 109 } 110 111 func (self *minerApi) StopMiner(req *shared.Request) (interface{}, error) { 112 self.ethereum.StopMining() 113 return true, nil 114 } 115 116 func (self *minerApi) Hashrate(req *shared.Request) (interface{}, error) { 117 return self.ethereum.Miner().HashRate(), nil 118 } 119 120 func (self *minerApi) SetExtra(req *shared.Request) (interface{}, error) { 121 args := new(SetExtraArgs) 122 if err := self.codec.Decode(req.Params, &args); err != nil { 123 return nil, err 124 } 125 126 if err := self.ethereum.Miner().SetExtra([]byte(args.Data)); err != nil { 127 return false, err 128 } 129 130 return true, nil 131 } 132 133 func (self *minerApi) SetGasPrice(req *shared.Request) (interface{}, error) { 134 args := new(GasPriceArgs) 135 if err := self.codec.Decode(req.Params, &args); err != nil { 136 return false, err 137 } 138 139 self.ethereum.Miner().SetGasPrice(common.String2Big(args.Price)) 140 return true, nil 141 } 142 143 func (self *minerApi) SetEtherbase(req *shared.Request) (interface{}, error) { 144 args := new(SetEtherbaseArgs) 145 if err := self.codec.Decode(req.Params, &args); err != nil { 146 return false, err 147 } 148 self.ethereum.SetEtherbase(args.Etherbase) 149 return nil, nil 150 } 151 152 func (self *minerApi) StartAutoDAG(req *shared.Request) (interface{}, error) { 153 self.ethereum.StartAutoDAG() 154 return true, nil 155 } 156 157 func (self *minerApi) StopAutoDAG(req *shared.Request) (interface{}, error) { 158 self.ethereum.StopAutoDAG() 159 return true, nil 160 } 161 162 func (self *minerApi) MakeDAG(req *shared.Request) (interface{}, error) { 163 args := new(MakeDAGArgs) 164 if err := self.codec.Decode(req.Params, &args); err != nil { 165 return nil, err 166 } 167 168 if args.BlockNumber < 0 { 169 return false, shared.NewValidationError("BlockNumber", "BlockNumber must be positive") 170 } 171 172 err := ethash.MakeDAG(uint64(args.BlockNumber), "") 173 if err == nil { 174 return true, nil 175 } 176 return false, err 177 }