api.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2018 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 ethash
  17. import (
  18. "errors"
  19. "github.com/ethereum/go-ethereum/common"
  20. "github.com/ethereum/go-ethereum/common/hexutil"
  21. "github.com/ethereum/go-ethereum/core/types"
  22. )
  23. var errEthashStopped = errors.New("ethash stopped")
  24. // API exposes ethash related methods for the RPC interface.
  25. type API struct {
  26. ethash *Ethash // Make sure the mode of ethash is normal.
  27. }
  28. // GetWork returns a work package for external miner.
  29. //
  30. // The work package consists of 3 strings:
  31. // result[0] - 32 bytes hex encoded current block header pow-hash
  32. // result[1] - 32 bytes hex encoded seed hash used for DAG
  33. // result[2] - 32 bytes hex encoded boundary condition ("target"), 2^256/difficulty
  34. func (api *API) GetWork() ([3]string, error) {
  35. if api.ethash.config.PowMode != ModeNormal && api.ethash.config.PowMode != ModeTest {
  36. return [3]string{}, errors.New("not supported")
  37. }
  38. var (
  39. workCh = make(chan [3]string, 1)
  40. errc = make(chan error, 1)
  41. )
  42. select {
  43. case api.ethash.fetchWorkCh <- &sealWork{errc: errc, res: workCh}:
  44. case <-api.ethash.exitCh:
  45. return [3]string{}, errEthashStopped
  46. }
  47. select {
  48. case work := <-workCh:
  49. return work, nil
  50. case err := <-errc:
  51. return [3]string{}, err
  52. }
  53. }
  54. // SubmitWork can be used by external miner to submit their POW solution.
  55. // It returns an indication if the work was accepted.
  56. // Note either an invalid solution, a stale work a non-existent work will return false.
  57. func (api *API) SubmitWork(nonce types.BlockNonce, hash, digest common.Hash) bool {
  58. if api.ethash.config.PowMode != ModeNormal && api.ethash.config.PowMode != ModeTest {
  59. return false
  60. }
  61. var errc = make(chan error, 1)
  62. select {
  63. case api.ethash.submitWorkCh <- &mineResult{
  64. nonce: nonce,
  65. mixDigest: digest,
  66. hash: hash,
  67. errc: errc,
  68. }:
  69. case <-api.ethash.exitCh:
  70. return false
  71. }
  72. err := <-errc
  73. return err == nil
  74. }
  75. // SubmitHashrate can be used for remote miners to submit their hash rate.
  76. // This enables the node to report the combined hash rate of all miners
  77. // which submit work through this node.
  78. //
  79. // It accepts the miner hash rate and an identifier which must be unique
  80. // between nodes.
  81. func (api *API) SubmitHashRate(rate hexutil.Uint64, id common.Hash) bool {
  82. if api.ethash.config.PowMode != ModeNormal && api.ethash.config.PowMode != ModeTest {
  83. return false
  84. }
  85. var done = make(chan struct{}, 1)
  86. select {
  87. case api.ethash.submitRateCh <- &hashrate{done: done, rate: uint64(rate), id: id}:
  88. case <-api.ethash.exitCh:
  89. return false
  90. }
  91. // Block until hash rate submitted successfully.
  92. <-done
  93. return true
  94. }
  95. // GetHashrate returns the current hashrate for local CPU miner and remote miner.
  96. func (api *API) GetHashrate() uint64 {
  97. return uint64(api.ethash.Hashrate())
  98. }