aboutsummaryrefslogtreecommitdiffstats
path: root/miner/worker.go
diff options
context:
space:
mode:
authorJeffrey Wilcke <geffobscura@gmail.com>2015-06-29 18:12:30 +0800
committerJeffrey Wilcke <geffobscura@gmail.com>2015-06-29 19:31:49 +0800
commitd1e93db3ebc218044339a6c5c95b4907e6351ede (patch)
tree781e724fb904c051172479d56e0ebdd741bf7b1c /miner/worker.go
parent059a1e9e4edeb75cef6da05936835cfbc61e1350 (diff)
downloadgo-tangerine-d1e93db3ebc218044339a6c5c95b4907e6351ede.tar
go-tangerine-d1e93db3ebc218044339a6c5c95b4907e6351ede.tar.gz
go-tangerine-d1e93db3ebc218044339a6c5c95b4907e6351ede.tar.bz2
go-tangerine-d1e93db3ebc218044339a6c5c95b4907e6351ede.tar.lz
go-tangerine-d1e93db3ebc218044339a6c5c95b4907e6351ede.tar.xz
go-tangerine-d1e93db3ebc218044339a6c5c95b4907e6351ede.tar.zst
go-tangerine-d1e93db3ebc218044339a6c5c95b4907e6351ede.zip
core, miner: added write block method & changed mining propagation
Diffstat (limited to 'miner/worker.go')
-rw-r--r--miner/worker.go56
1 files changed, 28 insertions, 28 deletions
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()