From 059a1e9e4edeb75cef6da05936835cfbc61e1350 Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Sun, 28 Jun 2015 13:38:21 +0200 Subject: miner: broadcast block before insertion/validation --- miner/worker.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'miner') diff --git a/miner/worker.go b/miner/worker.go index 55c23376c..2955a9dee 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -239,12 +239,16 @@ func (self *worker) wait() { continue } + // broadcast before waiting for validation + go self.mux.Post(core.NewMinedBlockEvent{block}) + // insert mined block in to our own chain if _, err := self.chain.InsertChain(types.Blocks{block}); err == nil { + // remove uncles we've previously inserted for _, uncle := range block.Uncles() { delete(self.possibleUncles, uncle.Hash()) } - self.mux.Post(core.NewMinedBlockEvent{block}) + // check staleness and display confirmation var stale, confirm string canonBlock := self.chain.GetBlockByNumber(block.NumberU64()) if canonBlock != nil && canonBlock.Hash() != block.Hash() { @@ -256,6 +260,7 @@ func (self *worker) wait() { glog.V(logger.Info).Infof("🔨 Mined %sblock (#%v / %x). %s", stale, block.Number(), block.Hash().Bytes()[:4], confirm) + // XXX remove old structured json logging jsonlogger.LogJson(&logger.EthMinerNewBlock{ BlockHash: block.Hash().Hex(), BlockNumber: block.Number(), -- cgit From d1e93db3ebc218044339a6c5c95b4907e6351ede Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Mon, 29 Jun 2015 12:12:30 +0200 Subject: core, miner: added write block method & changed mining propagation --- miner/worker.go | 56 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 28 insertions(+), 28 deletions(-) (limited to 'miner') diff --git a/miner/worker.go b/miner/worker.go index 2955a9dee..ab01ba09e 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -239,43 +239,35 @@ func (self *worker) wait() { continue } - // broadcast before waiting for validation - go self.mux.Post(core.NewMinedBlockEvent{block}) - // insert mined block in to our own chain - if _, err := self.chain.InsertChain(types.Blocks{block}); err == nil { - // remove uncles we've previously inserted - for _, uncle := range block.Uncles() { - delete(self.possibleUncles, uncle.Hash()) - } - - // check staleness and display confirmation - var stale, confirm string - canonBlock := self.chain.GetBlockByNumber(block.NumberU64()) - if canonBlock != nil && canonBlock.Hash() != block.Hash() { - stale = "stale " - } else { - confirm = "Wait 5 blocks for confirmation" - self.current.localMinedBlocks = newLocalMinedBlock(block.Number().Uint64(), self.current.localMinedBlocks) - } - - glog.V(logger.Info).Infof("🔨 Mined %sblock (#%v / %x). %s", stale, block.Number(), block.Hash().Bytes()[:4], confirm) + _, err := self.chain.WriteBlock(block) + if err != nil { + glog.V(logger.Error).Infoln("error writing block to chain", err) + continue + } - // XXX remove old structured json logging - jsonlogger.LogJson(&logger.EthMinerNewBlock{ - BlockHash: block.Hash().Hex(), - BlockNumber: block.Number(), - ChainHeadHash: block.ParentHeaderHash.Hex(), - BlockPrevHash: block.ParentHeaderHash.Hex(), - }) + // check staleness and display confirmation + var stale, confirm string + canonBlock := self.chain.GetBlockByNumber(block.NumberU64()) + if canonBlock != nil && canonBlock.Hash() != block.Hash() { + stale = "stale " } else { - self.commitNewWork() + confirm = "Wait 5 blocks for confirmation" + self.current.localMinedBlocks = newLocalMinedBlock(block.Number().Uint64(), self.current.localMinedBlocks) } + + glog.V(logger.Info).Infof("🔨 Mined %sblock (#%v / %x). %s", stale, block.Number(), block.Hash().Bytes()[:4], confirm) + + // broadcast before waiting for validation + go self.mux.Post(core.NewMinedBlockEvent{block}) + + self.commitNewWork() } } } func (self *worker) push() { if atomic.LoadInt32(&self.mining) == 1 { + self.current.state.Sync() self.current.block.SetRoot(self.current.state.Root()) // push new work to agents @@ -302,6 +294,13 @@ func (self *worker) makeCurrent() { if block.Time() <= parent.Time() { block.Header().Time = parent.Header().Time + 1 } + // this will ensure we're not going off too far in the future + if now := time.Now().Unix(); block.Time() > now+4 { + wait := time.Duration(block.Time()-now) * time.Second + glog.V(logger.Info).Infoln("We are too far in the future. Waiting for", wait) + time.Sleep(wait) + } + block.Header().Extra = self.extra // when 08 is processed ancestors contain 07 (quick block) @@ -428,6 +427,7 @@ func (self *worker) commitNewWork() { self.current.block.SetUncles(uncles) core.AccumulateRewards(self.current.state, self.current.block) + self.current.block.Td = new(big.Int).Set(core.CalcTD(self.current.block, self.chain.GetBlock(self.current.block.ParentHash()))) self.current.state.Update() -- cgit From b39042db5672e830ddec41ea97c642d93be61c30 Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Mon, 29 Jun 2015 12:42:47 +0200 Subject: core, miner: implemented canary --- miner/worker.go | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'miner') diff --git a/miner/worker.go b/miner/worker.go index ab01ba09e..1e5d47965 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -267,6 +267,12 @@ func (self *worker) wait() { func (self *worker) push() { if atomic.LoadInt32(&self.mining) == 1 { + if core.Canary(self.current.state) { + glog.Infoln("Toxicity levels rising to deadly levels. Your canary has died. You can go back or continue down the mineshaft --more--") + glog.Infoln("You turn back and abort mining") + return + } + self.current.state.Sync() self.current.block.SetRoot(self.current.state.Root()) -- cgit