From c39484bc4b099881c3aa164d33b5ba95c55f36fa Mon Sep 17 00:00:00 2001 From: obscuren Date: Sat, 4 Apr 2015 20:34:10 +0200 Subject: [PATCH] Added thread safe each --- core/block_cache.go | 11 +++++++++++ core/chain_manager.go | 8 ++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/core/block_cache.go b/core/block_cache.go index 768d3bf19..eeef5c41d 100644 --- a/core/block_cache.go +++ b/core/block_cache.go @@ -88,3 +88,14 @@ func (bc *BlockCache) Has(hash common.Hash) bool { _, ok := bc.blocks[hash] return ok } + +func (bc *BlockCache) Each(cb func(int, *types.Block)) { + bc.mu.Lock() + defer bc.mu.Unlock() + + i := 0 + for _, block := range bc.blocks { + cb(i, block) + i++ + } +} diff --git a/core/chain_manager.go b/core/chain_manager.go index b7b6279ca..639812b38 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -438,9 +438,9 @@ type queueEvent struct { func (self *ChainManager) procFutureBlocks() { blocks := make([]*types.Block, len(self.futureBlocks.blocks)) - for i, hash := range self.futureBlocks.hashes { - blocks[i] = self.futureBlocks.Get(hash) - } + self.futureBlocks.Each(func(i int, block *types.Block) { + blocks[i] = block + }) types.BlockBy(types.Number).Sort(blocks) self.InsertChain(blocks) @@ -536,7 +536,7 @@ func (self *ChainManager) InsertChain(chain types.Blocks) error { if len(chain) > 0 && glog.V(logger.Info) { start, end := chain[0], chain[len(chain)-1] - glog.Infof("imported %d blocks #%v [%x / %x]\n", len(chain), end.Number(), start.Hash().Bytes()[:4], end.Hash().Bytes()[:4]) + glog.Infof("imported %d block(s) #%v [%x / %x]\n", len(chain), end.Number(), start.Hash().Bytes()[:4], end.Hash().Bytes()[:4]) } go self.eventMux.Post(queueEvent)