|
|
@ -51,7 +51,7 @@ type Work struct { |
|
|
|
|
|
|
|
|
|
|
|
type Agent interface { |
|
|
|
type Agent interface { |
|
|
|
Work() chan<- *types.Block |
|
|
|
Work() chan<- *types.Block |
|
|
|
SetWorkCh(chan<- Work) |
|
|
|
SetReturnCh(chan<- *types.Block) |
|
|
|
Stop() |
|
|
|
Stop() |
|
|
|
Start() |
|
|
|
Start() |
|
|
|
GetHashRate() int64 |
|
|
|
GetHashRate() int64 |
|
|
@ -60,7 +60,7 @@ type Agent interface { |
|
|
|
type worker struct { |
|
|
|
type worker struct { |
|
|
|
mu sync.Mutex |
|
|
|
mu sync.Mutex |
|
|
|
agents []Agent |
|
|
|
agents []Agent |
|
|
|
recv chan Work |
|
|
|
recv chan *types.Block |
|
|
|
mux *event.TypeMux |
|
|
|
mux *event.TypeMux |
|
|
|
quit chan struct{} |
|
|
|
quit chan struct{} |
|
|
|
pow pow.PoW |
|
|
|
pow pow.PoW |
|
|
@ -82,7 +82,7 @@ func newWorker(coinbase common.Address, eth core.Backend) *worker { |
|
|
|
return &worker{ |
|
|
|
return &worker{ |
|
|
|
eth: eth, |
|
|
|
eth: eth, |
|
|
|
mux: eth.EventMux(), |
|
|
|
mux: eth.EventMux(), |
|
|
|
recv: make(chan Work), |
|
|
|
recv: make(chan *types.Block), |
|
|
|
chain: eth.ChainManager(), |
|
|
|
chain: eth.ChainManager(), |
|
|
|
proc: eth.BlockProcessor(), |
|
|
|
proc: eth.BlockProcessor(), |
|
|
|
possibleUncles: make(map[common.Hash]*types.Block), |
|
|
|
possibleUncles: make(map[common.Hash]*types.Block), |
|
|
@ -112,7 +112,7 @@ func (self *worker) stop() { |
|
|
|
|
|
|
|
|
|
|
|
func (self *worker) register(agent Agent) { |
|
|
|
func (self *worker) register(agent Agent) { |
|
|
|
self.agents = append(self.agents, agent) |
|
|
|
self.agents = append(self.agents, agent) |
|
|
|
agent.SetWorkCh(self.recv) |
|
|
|
agent.SetReturnCh(self.recv) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *worker) update() { |
|
|
|
func (self *worker) update() { |
|
|
@ -155,30 +155,30 @@ func (self *worker) addUncle(uncle *types.Block) { |
|
|
|
|
|
|
|
|
|
|
|
func (self *worker) wait() { |
|
|
|
func (self *worker) wait() { |
|
|
|
for { |
|
|
|
for { |
|
|
|
for work := range self.recv { |
|
|
|
for block := range self.recv { |
|
|
|
// Someone Successfully Mined!
|
|
|
|
// Someone Successfully Mined!
|
|
|
|
block := self.current.block |
|
|
|
//block := self.current.block
|
|
|
|
if block.Number().Uint64() == work.Number && block.Nonce() == 0 { |
|
|
|
//if block.Number().Uint64() == work.Number && block.Nonce() == 0 {
|
|
|
|
self.current.block.SetNonce(work.Nonce) |
|
|
|
//self.current.block.SetNonce(work.Nonce)
|
|
|
|
self.current.block.Header().MixDigest = common.BytesToHash(work.MixDigest) |
|
|
|
//self.current.block.Header().MixDigest = common.BytesToHash(work.MixDigest)
|
|
|
|
|
|
|
|
|
|
|
|
jsonlogger.LogJson(&logger.EthMinerNewBlock{ |
|
|
|
jsonlogger.LogJson(&logger.EthMinerNewBlock{ |
|
|
|
BlockHash: block.Hash().Hex(), |
|
|
|
BlockHash: block.Hash().Hex(), |
|
|
|
BlockNumber: block.Number(), |
|
|
|
BlockNumber: block.Number(), |
|
|
|
ChainHeadHash: block.ParentHeaderHash.Hex(), |
|
|
|
ChainHeadHash: block.ParentHeaderHash.Hex(), |
|
|
|
BlockPrevHash: block.ParentHeaderHash.Hex(), |
|
|
|
BlockPrevHash: block.ParentHeaderHash.Hex(), |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
if err := self.chain.InsertChain(types.Blocks{self.current.block}); err == nil { |
|
|
|
if err := self.chain.InsertChain(types.Blocks{block}); err == nil { |
|
|
|
for _, uncle := range self.current.block.Uncles() { |
|
|
|
for _, uncle := range block.Uncles() { |
|
|
|
delete(self.possibleUncles, uncle.Hash()) |
|
|
|
delete(self.possibleUncles, uncle.Hash()) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.mux.Post(core.NewMinedBlockEvent{self.current.block}) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
self.commitNewWork() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.mux.Post(core.NewMinedBlockEvent{block}) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
self.commitNewWork() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//}
|
|
|
|
break |
|
|
|
break |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -191,7 +191,7 @@ func (self *worker) push() { |
|
|
|
|
|
|
|
|
|
|
|
// push new work to agents
|
|
|
|
// push new work to agents
|
|
|
|
for _, agent := range self.agents { |
|
|
|
for _, agent := range self.agents { |
|
|
|
agent.Work() <- self.current.block |
|
|
|
agent.Work() <- self.current.block.Copy() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|