api.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU 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. // go-ethereum 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 General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package miner
  17. import (
  18. "fmt"
  19. "github.com/ethereum/go-ethereum/common"
  20. "github.com/ethereum/go-ethereum/logger/glog"
  21. rpc "github.com/ethereum/go-ethereum/rpc/v2"
  22. )
  23. // PublicMinerAPI provides an API to control the miner.
  24. // It offers only methods that operate on data that pose no security risk when it is publicly accessible.
  25. type PublicMinerAPI struct {
  26. miner *Miner
  27. agent *RemoteAgent
  28. }
  29. // NewPublicMinerAPI create a new PublicMinerAPI instance.
  30. func NewPublicMinerAPI(miner *Miner) *PublicMinerAPI {
  31. return &PublicMinerAPI{miner, NewRemoteAgent()}
  32. }
  33. // Mining returns an indication if this node is currently mining.
  34. func (s *PublicMinerAPI) Mining() bool {
  35. return s.miner.Mining()
  36. }
  37. // SubmitWork can be used by external miner to submit their POW solution. It returns an indication if the work was
  38. // accepted. Note, this is not an indication if the provided work was valid!
  39. func (s *PublicMinerAPI) SubmitWork(nonce rpc.HexNumber, solution, digest common.Hash) bool {
  40. return s.agent.SubmitWork(nonce.Uint64(), digest, solution)
  41. }
  42. // GetWork returns a work package for external miner. The work package consists of 3 strings
  43. // result[0], 32 bytes hex encoded current block header pow-hash
  44. // result[1], 32 bytes hex encoded seed hash used for DAG
  45. // result[2], 32 bytes hex encoded boundary condition ("target"), 2^256/difficulty
  46. func (s *PublicMinerAPI) GetWork() ([]string, error) {
  47. if !s.Mining() {
  48. s.miner.Start(s.miner.coinbase, 0)
  49. }
  50. if work, err := s.agent.GetWork(); err == nil {
  51. return work[:], nil
  52. } else {
  53. glog.Infof("%v\n", err)
  54. }
  55. return nil, fmt.Errorf("mining not ready")
  56. }
  57. // SubmitHashrate can be used for remote miners to submit their hash rate. This enables the node to report the combined
  58. // hash rate of all miners which submit work through this node. It accepts the miner hash rate and an identifier which
  59. // must be unique between nodes.
  60. func (s *PublicMinerAPI) SubmitHashrate(hashrate rpc.HexNumber, id common.Hash) bool {
  61. s.agent.SubmitHashrate(id, hashrate.Uint64())
  62. return true
  63. }