|
|
@ -41,16 +41,21 @@ func env(block *types.Block, eth *eth.Ethereum) *environment { |
|
|
|
return env |
|
|
|
return env |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type Work struct { |
|
|
|
|
|
|
|
Number uint64 |
|
|
|
|
|
|
|
Nonce []byte |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type Agent interface { |
|
|
|
type Agent interface { |
|
|
|
Work() chan<- *types.Block |
|
|
|
Work() chan<- *types.Block |
|
|
|
SetNonceCh(chan<- []byte) |
|
|
|
SetNonceCh(chan<- Work) |
|
|
|
Stop() |
|
|
|
Stop() |
|
|
|
Pow() pow.PoW |
|
|
|
Pow() pow.PoW |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type worker struct { |
|
|
|
type worker struct { |
|
|
|
agents []Agent |
|
|
|
agents []Agent |
|
|
|
recv chan []byte |
|
|
|
recv chan Work |
|
|
|
mux *event.TypeMux |
|
|
|
mux *event.TypeMux |
|
|
|
quit chan struct{} |
|
|
|
quit chan struct{} |
|
|
|
pow pow.PoW |
|
|
|
pow pow.PoW |
|
|
@ -61,13 +66,15 @@ type worker struct { |
|
|
|
coinbase []byte |
|
|
|
coinbase []byte |
|
|
|
|
|
|
|
|
|
|
|
current *environment |
|
|
|
current *environment |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mining bool |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func newWorker(coinbase []byte, eth *eth.Ethereum) *worker { |
|
|
|
func newWorker(coinbase []byte, eth *eth.Ethereum) *worker { |
|
|
|
return &worker{ |
|
|
|
return &worker{ |
|
|
|
eth: eth, |
|
|
|
eth: eth, |
|
|
|
mux: eth.EventMux(), |
|
|
|
mux: eth.EventMux(), |
|
|
|
recv: make(chan []byte), |
|
|
|
recv: make(chan Work), |
|
|
|
chain: eth.ChainManager(), |
|
|
|
chain: eth.ChainManager(), |
|
|
|
proc: eth.BlockProcessor(), |
|
|
|
proc: eth.BlockProcessor(), |
|
|
|
coinbase: coinbase, |
|
|
|
coinbase: coinbase, |
|
|
@ -75,11 +82,17 @@ func newWorker(coinbase []byte, eth *eth.Ethereum) *worker { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *worker) start() { |
|
|
|
func (self *worker) start() { |
|
|
|
|
|
|
|
self.mining = true |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.quit = make(chan struct{}) |
|
|
|
|
|
|
|
|
|
|
|
go self.update() |
|
|
|
go self.update() |
|
|
|
go self.wait() |
|
|
|
go self.wait() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *worker) stop() { |
|
|
|
func (self *worker) stop() { |
|
|
|
|
|
|
|
self.mining = false |
|
|
|
|
|
|
|
|
|
|
|
close(self.quit) |
|
|
|
close(self.quit) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -107,27 +120,32 @@ out: |
|
|
|
break out |
|
|
|
break out |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
events.Unsubscribe() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *worker) wait() { |
|
|
|
func (self *worker) wait() { |
|
|
|
for { |
|
|
|
for { |
|
|
|
for nonce := range self.recv { |
|
|
|
for work := range self.recv { |
|
|
|
self.current.block.Header().Nonce = nonce |
|
|
|
if self.current.block.Number().Uint64() == work.Number { |
|
|
|
fmt.Println(self.current.block) |
|
|
|
self.current.block.Header().Nonce = work.Nonce |
|
|
|
|
|
|
|
|
|
|
|
self.chain.InsertChain(types.Blocks{self.current.block}) |
|
|
|
self.chain.InsertChain(types.Blocks{self.current.block}) |
|
|
|
|
|
|
|
} |
|
|
|
break |
|
|
|
break |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *worker) push() { |
|
|
|
func (self *worker) push() { |
|
|
|
|
|
|
|
if self.mining { |
|
|
|
self.current.state.Update(ethutil.Big0) |
|
|
|
self.current.state.Update(ethutil.Big0) |
|
|
|
self.current.block.SetRoot(self.current.state.Root()) |
|
|
|
self.current.block.SetRoot(self.current.state.Root()) |
|
|
|
|
|
|
|
|
|
|
|
for _, agent := range self.agents { |
|
|
|
for _, agent := range self.agents { |
|
|
|
agent.Work() <- self.current.block |
|
|
|
agent.Work() <- self.current.block |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *worker) commitNewWork() { |
|
|
|
func (self *worker) commitNewWork() { |
|
|
|