diff options
Diffstat (limited to 'miner')
-rw-r--r-- | miner/agent.go | 13 | ||||
-rw-r--r-- | miner/miner.go | 10 | ||||
-rw-r--r-- | miner/worker.go | 26 |
3 files changed, 35 insertions, 14 deletions
diff --git a/miner/agent.go b/miner/agent.go index 9046f5d5a..6865d5a08 100644 --- a/miner/agent.go +++ b/miner/agent.go @@ -42,11 +42,16 @@ func (self *CpuMiner) Start() { } func (self *CpuMiner) update() { + justStarted := true out: for { select { case block := <-self.c: - self.quitCurrentOp <- struct{}{} + if justStarted { + justStarted = true + } else { + self.quitCurrentOp <- struct{}{} + } go self.mine(block) case <-self.quit: @@ -69,8 +74,8 @@ done: func (self *CpuMiner) mine(block *types.Block) { minerlogger.Infof("(re)started agent[%d]. mining...\n", self.index) - nonce := self.pow.Search(block, self.quitCurrentOp) - if nonce != nil { - self.returnCh <- Work{block.Number().Uint64(), nonce} + nonce, mixDigest, seedHash := self.pow.Search(block, self.quitCurrentOp) + if nonce != 0 { + self.returnCh <- Work{block.Number().Uint64(), nonce, mixDigest, seedHash} } } diff --git a/miner/miner.go b/miner/miner.go index 6b416be8e..b95c65859 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -5,7 +5,7 @@ import ( "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/logger" - "github.com/ethereum/go-ethereum/pow/ezp" + "github.com/ethereum/go-ethereum/pow" ) var minerlogger = logger.NewLogger("MINER") @@ -18,16 +18,20 @@ type Miner struct { Coinbase []byte mining bool + + pow pow.PoW } -func New(coinbase []byte, eth core.Backend, minerThreads int) *Miner { +func New(coinbase []byte, eth core.Backend, pow pow.PoW, minerThreads int) *Miner { miner := &Miner{ Coinbase: coinbase, worker: newWorker(coinbase, eth), + pow: pow, } + minerThreads = 1 for i := 0; i < minerThreads; i++ { - miner.worker.register(NewCpuMiner(i, ezp.New())) + miner.worker.register(NewCpuMiner(i, miner.pow)) } return miner diff --git a/miner/worker.go b/miner/worker.go index bc1c8e194..29992b327 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -7,6 +7,7 @@ import ( "sync" "time" + "github.com/ethereum/ethash" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethutil" @@ -46,8 +47,10 @@ func env(block *types.Block, eth core.Backend) *environment { } type Work struct { - Number uint64 - Nonce []byte + Number uint64 + Nonce uint64 + MixDigest []byte + SeedHash []byte } type Agent interface { @@ -136,7 +139,7 @@ out: } break out case <-timer.C: - minerlogger.Debugln("Hash rate:", self.HashRate(), "Khash") + minerlogger.Infoln("Hash rate:", self.HashRate(), "Khash") } } @@ -146,15 +149,20 @@ out: func (self *worker) wait() { for { for work := range self.recv { + // Someone Successfully Mined! block := self.current.block - if block.Number().Uint64() == work.Number && block.Nonce() == nil { - self.current.block.Header().Nonce = work.Nonce + if block.Number().Uint64() == work.Number && block.Nonce() == 0 { + self.current.block.SetNonce(work.Nonce) + self.current.block.Header().MixDigest = work.MixDigest + self.current.block.Header().SeedHash = work.SeedHash + jsonlogger.LogJson(&logger.EthMinerNewBlock{ BlockHash: ethutil.Bytes2Hex(block.Hash()), BlockNumber: block.Number(), ChainHeadHash: ethutil.Bytes2Hex(block.ParentHeaderHash), BlockPrevHash: ethutil.Bytes2Hex(block.ParentHeaderHash), }) + if err := self.chain.InsertChain(types.Blocks{self.current.block}); err == nil { self.mux.Post(core.NewMinedBlockEvent{self.current.block}) } else { @@ -182,7 +190,11 @@ func (self *worker) commitNewWork() { self.mu.Lock() defer self.mu.Unlock() - self.current = env(self.chain.NewBlock(self.coinbase), self.eth) + block := self.chain.NewBlock(self.coinbase) + seednum := ethash.GetSeedBlockNum(block.NumberU64()) + block.Header().SeedHash = self.chain.GetBlockByNumber(seednum).SeedHash() + + self.current = env(block, self.eth) parent := self.chain.GetBlock(self.current.block.ParentHash()) self.current.coinbase.SetGasPool(core.CalcGasLimit(parent, self.current.block)) @@ -234,7 +246,7 @@ func (self *worker) commitUncle(uncle *types.Header) error { } if !self.pow.Verify(types.NewBlockWithHeader(uncle)) { - return core.ValidationError("Uncle's nonce is invalid (= %v)", ethutil.Bytes2Hex(uncle.Nonce)) + return core.ValidationError("Uncle's nonce is invalid (= %x)", uncle.Nonce) } uncleAccount := self.current.state.GetAccount(uncle.Coinbase) |