From 28b39267d990352883df1c093fd6c36cd532cfdf Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 30 Apr 2015 13:41:47 +0200 Subject: core, eth: verify td of received blocks --- core/chain_manager.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'core') diff --git a/core/chain_manager.go b/core/chain_manager.go index 4fdb2edce..e97ed307c 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -511,6 +511,10 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) { if block == nil { continue } + // Setting block.Td regardless of error (known for example) prevents errors down the line + // in the protocol handler + block.Td = new(big.Int).Set(CalculateTD(block, self.GetBlock(block.ParentHash()))) + // Call in to the block processor and check for errors. It's likely that if one block fails // all others will fail too (unless a known block is returned). logs, err := self.processor.Process(block) @@ -545,8 +549,6 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) { return i, err } - block.Td = new(big.Int).Set(CalculateTD(block, self.GetBlock(block.ParentHash()))) - self.mu.Lock() { cblock := self.currentBlock -- cgit v1.2.3 From e4dba36892477f3ef614dd4e4f1234ae4a4e26d0 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 30 Apr 2015 14:55:21 +0200 Subject: core: check for parent in calc TD. TD = (N != 0 == parent.TD) || (== D) --- core/chain_manager.go | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'core') diff --git a/core/chain_manager.go b/core/chain_manager.go index e97ed307c..9f62d3b47 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -49,6 +49,10 @@ func CalcDifficulty(block, parent *types.Header) *big.Int { } func CalculateTD(block, parent *types.Block) *big.Int { + if parent == nil { + return block.Difficulty() + } + td := new(big.Int).Add(parent.Td, block.Header().Difficulty) return td -- cgit v1.2.3 From 15873fafc02e444c38de722277ab2461cb9b82c5 Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 30 Apr 2015 17:50:23 +0200 Subject: core: added a wait group to chain manager for graceful shutdown --- core/chain_manager.go | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'core') diff --git a/core/chain_manager.go b/core/chain_manager.go index 9f62d3b47..80fd6cd71 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -93,6 +93,7 @@ type ChainManager struct { futureBlocks *BlockCache quit chan struct{} + wg sync.WaitGroup } func NewChainManager(blockDb, stateDb common.Database, mux *event.TypeMux) *ChainManager { @@ -482,6 +483,10 @@ func (self *ChainManager) CalcTotalDiff(block *types.Block) (*big.Int, error) { func (bc *ChainManager) Stop() { close(bc.quit) + + bc.wg.Wait() + + glog.V(logger.Info).Infoln("Chain manager stopped") } type queueEvent struct { @@ -504,6 +509,9 @@ func (self *ChainManager) procFutureBlocks() { // InsertChain will attempt to insert the given chain in to the canonical chain or, otherwise, create a fork. It an error is returned // it will return the index number of the failing block as well an error describing what went wrong (for possible errors see core/errors.go). func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) { + self.wg.Add(1) + defer self.wg.Done() + // A queued approach to delivering events. This is generally faster than direct delivery and requires much less mutex acquiring. var ( queue = make([]interface{}, len(chain)) -- cgit v1.2.3 From b298928c49d1af3dc1165b295fe43fc8d0d6061d Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 1 May 2015 16:00:30 +0200 Subject: core: added 'ignored' status --- core/chain_manager.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'core') diff --git a/core/chain_manager.go b/core/chain_manager.go index 80fd6cd71..e172c5cdb 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -516,7 +516,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) { var ( queue = make([]interface{}, len(chain)) queueEvent = queueEvent{queue: queue} - stats struct{ queued, processed int } + stats struct{ queued, processed, ignored int } tstart = time.Now() ) for i, block := range chain { @@ -532,6 +532,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) { logs, err := self.processor.Process(block) if err != nil { if IsKnownBlockErr(err) { + stats.ignored++ continue } @@ -624,7 +625,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) { if (stats.queued > 0 || stats.processed > 0) && bool(glog.V(logger.Info)) { tend := time.Since(tstart) start, end := chain[0], chain[len(chain)-1] - glog.Infof("imported %d block(s) %d queued in %v. #%v [%x / %x]\n", stats.processed, stats.queued, tend, end.Number(), start.Hash().Bytes()[:4], end.Hash().Bytes()[:4]) + glog.Infof("imported %d block(s) (%d queued %d ignored) in %v. #%v [%x / %x]\n", stats.processed, stats.queued, stats.ignored, tend, end.Number(), start.Hash().Bytes()[:4], end.Hash().Bytes()[:4]) } go self.eventMux.Post(queueEvent) -- cgit v1.2.3 From c6ad3aec05e1c42c3e4a222d1e8306598d5254f3 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 1 May 2015 16:30:02 +0200 Subject: eth,core: changed NewTicker to Tick --- core/chain_manager.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'core') diff --git a/core/chain_manager.go b/core/chain_manager.go index e172c5cdb..4dfa67b8d 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -669,7 +669,7 @@ func (self *ChainManager) merge(oldBlock, newBlock *types.Block) { func (self *ChainManager) update() { events := self.eventMux.Subscribe(queueEvent{}) - futureTimer := time.NewTicker(5 * time.Second) + futureTimer := time.Tick(5 * time.Second) out: for { select { @@ -696,7 +696,7 @@ out: self.eventMux.Post(event) } } - case <-futureTimer.C: + case <-futureTimer: self.procFutureBlocks() case <-self.quit: break out -- cgit v1.2.3 From c5b8acbaf0ae662a6b72568d155880d28d1034a1 Mon Sep 17 00:00:00 2001 From: obscuren Date: Sun, 3 May 2015 14:09:50 +0200 Subject: core: print ignored blocks --- core/chain_manager.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core') diff --git a/core/chain_manager.go b/core/chain_manager.go index 4dfa67b8d..bde5a71d7 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -622,7 +622,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) { } - if (stats.queued > 0 || stats.processed > 0) && bool(glog.V(logger.Info)) { + if (stats.queued > 0 || stats.processed > 0 || stats.ignored > 0) && bool(glog.V(logger.Info)) { tend := time.Since(tstart) start, end := chain[0], chain[len(chain)-1] glog.Infof("imported %d block(s) (%d queued %d ignored) in %v. #%v [%x / %x]\n", stats.processed, stats.queued, stats.ignored, tend, end.Number(), start.Hash().Bytes()[:4], end.Hash().Bytes()[:4]) -- cgit v1.2.3 From 37770ed0d37f9c8ece37fecd443e7c70e915a48a Mon Sep 17 00:00:00 2001 From: obscuren Date: Sun, 3 May 2015 21:44:44 +0200 Subject: core: added unix timestamp to debug output for block proc --- core/chain_manager.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core') diff --git a/core/chain_manager.go b/core/chain_manager.go index bde5a71d7..29830188e 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -604,7 +604,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) (int, error) { queueEvent.canonicalCount++ if glog.V(logger.Debug) { - glog.Infof("inserted block #%d (%d TXs %d UNCs) (%x...)\n", block.Number(), len(block.Transactions()), len(block.Uncles()), block.Hash().Bytes()[0:4]) + glog.Infof("[%v] inserted block #%d (%d TXs %d UNCs) (%x...)\n", time.Now().UnixNano(), block.Number(), len(block.Transactions()), len(block.Uncles()), block.Hash().Bytes()[0:4]) } } else { if glog.V(logger.Detail) { -- cgit v1.2.3