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