miner.go 4.7 KB

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