remote_agent.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 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. // 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 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 go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package miner
  17. import (
  18. "math/big"
  19. "github.com/ethereum/ethash"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/core/types"
  22. )
  23. type RemoteAgent struct {
  24. work *types.Block
  25. currentWork *types.Block
  26. quit chan struct{}
  27. workCh chan *types.Block
  28. returnCh chan<- *types.Block
  29. }
  30. func NewRemoteAgent() *RemoteAgent {
  31. agent := &RemoteAgent{}
  32. return agent
  33. }
  34. func (a *RemoteAgent) Work() chan<- *types.Block {
  35. return a.workCh
  36. }
  37. func (a *RemoteAgent) SetReturnCh(returnCh chan<- *types.Block) {
  38. a.returnCh = returnCh
  39. }
  40. func (a *RemoteAgent) Start() {
  41. a.quit = make(chan struct{})
  42. a.workCh = make(chan *types.Block, 1)
  43. go a.run()
  44. }
  45. func (a *RemoteAgent) Stop() {
  46. close(a.quit)
  47. close(a.workCh)
  48. }
  49. func (a *RemoteAgent) GetHashRate() int64 { return 0 }
  50. func (a *RemoteAgent) run() {
  51. out:
  52. for {
  53. select {
  54. case <-a.quit:
  55. break out
  56. case work := <-a.workCh:
  57. a.work = work
  58. }
  59. }
  60. }
  61. func (a *RemoteAgent) GetWork() [3]string {
  62. var res [3]string
  63. if a.work != nil {
  64. a.currentWork = a.work
  65. res[0] = a.work.HashNoNonce().Hex()
  66. seedHash, _ := ethash.GetSeedHash(a.currentWork.NumberU64())
  67. res[1] = common.BytesToHash(seedHash).Hex()
  68. // Calculate the "target" to be returned to the external miner
  69. n := big.NewInt(1)
  70. n.Lsh(n, 255)
  71. n.Div(n, a.work.Difficulty())
  72. n.Lsh(n, 1)
  73. res[2] = common.BytesToHash(n.Bytes()).Hex()
  74. }
  75. return res
  76. }
  77. func (a *RemoteAgent) SubmitWork(nonce uint64, mixDigest, seedHash common.Hash) bool {
  78. // Return true or false, but does not indicate if the PoW was correct
  79. // Make sure the external miner was working on the right hash
  80. if a.currentWork != nil && a.work != nil {
  81. a.returnCh <- a.currentWork.WithMiningResult(nonce, mixDigest)
  82. //a.returnCh <- Work{a.currentWork.Number().Uint64(), nonce, mixDigest.Bytes(), seedHash.Bytes()}
  83. return true
  84. }
  85. return false
  86. }