api.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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
  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. // result[3] - hex encoded block number
  35. func (api *API) GetWork() ([4]string, error) {
  36. if api.ethash.remote == nil {
  37. return [4]string{}, errors.New("not supported")
  38. }
  39. var (
  40. workCh = make(chan [4]string, 1)
  41. errc = make(chan error, 1)
  42. )
  43. select {
  44. case api.ethash.remote.fetchWorkCh <- &sealWork{errc: errc, res: workCh}:
  45. case <-api.ethash.remote.exitCh:
  46. return [4]string{}, errEthashStopped
  47. }
  48. select {
  49. case work := <-workCh:
  50. return work, nil
  51. case err := <-errc:
  52. return [4]string{}, err
  53. }
  54. }
  55. // SubmitWork can be used by external miner to submit their POW solution.
  56. // It returns an indication if the work was accepted.
  57. // Note either an invalid solution, a stale work a non-existent work will return false.
  58. func (api *API) SubmitWork(nonce types.BlockNonce, hash, digest common.Hash) bool {
  59. if api.ethash.remote == nil {
  60. return false
  61. }
  62. var errc = make(chan error, 1)
  63. select {
  64. case api.ethash.remote.submitWorkCh <- &mineResult{
  65. nonce: nonce,
  66. mixDigest: digest,
  67. hash: hash,
  68. errc: errc,
  69. }:
  70. case <-api.ethash.remote.exitCh:
  71. return false
  72. }
  73. err := <-errc
  74. return err == nil
  75. }
  76. // SubmitHashrate can be used for remote miners to submit their hash rate.
  77. // This enables the node to report the combined hash rate of all miners
  78. // which submit work through this node.
  79. //
  80. // It accepts the miner hash rate and an identifier which must be unique
  81. // between nodes.
  82. func (api *API) SubmitHashrate(rate hexutil.Uint64, id common.Hash) bool {
  83. if api.ethash.remote == nil {
  84. return false
  85. }
  86. var done = make(chan struct{}, 1)
  87. select {
  88. case api.ethash.remote.submitRateCh <- &hashrate{done: done, rate: uint64(rate), id: id}:
  89. case <-api.ethash.remote.exitCh:
  90. return false
  91. }
  92. // Block until hash rate submitted successfully.
  93. <-done
  94. return true
  95. }
  96. // GetHashrate returns the current hashrate for local CPU miner and remote miner.
  97. func (api *API) GetHashrate() uint64 {
  98. return uint64(api.ethash.Hashrate())
  99. }