remote_agent.go 1.8 KB

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