diff options
author | obscuren <geffobscura@gmail.com> | 2014-09-15 21:42:12 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2014-09-15 21:42:12 +0800 |
commit | 33a0dec8a157b9687ca6038f4deb011f3f1f7bdc (patch) | |
tree | 197b792e06dc3952df93957a39fdf6e44582ac96 /block_pool.go | |
parent | 2f614900e82036e3e8f6f6a714efc43e09aca830 (diff) | |
download | dexon-33a0dec8a157b9687ca6038f4deb011f3f1f7bdc.tar.gz dexon-33a0dec8a157b9687ca6038f4deb011f3f1f7bdc.tar.zst dexon-33a0dec8a157b9687ca6038f4deb011f3f1f7bdc.zip |
Improved catching up and refactored
Diffstat (limited to 'block_pool.go')
-rw-r--r-- | block_pool.go | 59 |
1 files changed, 17 insertions, 42 deletions
diff --git a/block_pool.go b/block_pool.go index 38827242d..0a668e111 100644 --- a/block_pool.go +++ b/block_pool.go @@ -52,59 +52,34 @@ func (self *BlockPool) AddHash(hash []byte) { func (self *BlockPool) SetBlock(b *ethchain.Block, peer *Peer) { hash := string(b.Hash()) - if self.pool[hash] == nil { + if self.pool[hash] == nil && !self.eth.BlockChain().HasBlock(b.Hash()) { self.hashPool = append(self.hashPool, b.Hash()) - self.pool[hash] = &block{peer, nil} + self.pool[hash] = &block{peer, b} + } else if self.pool[hash] != nil { + self.pool[hash].block = b } - - self.pool[hash].block = b } -func (self *BlockPool) CheckLinkAndProcess(f func(block *ethchain.Block)) bool { - self.mut.Lock() - defer self.mut.Unlock() - - if self.IsLinked() { - for i, hash := range self.hashPool { - if self.pool[string(hash)] == nil { - continue - } +func (self *BlockPool) CheckLinkAndProcess(f func(block *ethchain.Block)) { - block := self.pool[string(hash)].block - if block != nil { - f(block) - - delete(self.pool, string(hash)) - } else { - self.hashPool = self.hashPool[i:] - - return false - } + var blocks ethchain.Blocks + for _, item := range self.pool { + if item.block != nil { + blocks = append(blocks, item.block) } - - return true } - return false -} - -func (self *BlockPool) IsLinked() bool { - if len(self.hashPool) == 0 { - return false - } + ethchain.BlockBy(ethchain.Number).Sort(blocks) + for _, block := range blocks { + if self.eth.BlockChain().HasBlock(block.PrevHash) { + f(block) - for i := 0; i < len(self.hashPool); i++ { - item := self.pool[string(self.hashPool[i])] - if item != nil && item.block != nil { - if self.eth.BlockChain().HasBlock(item.block.PrevHash) { - self.hashPool = self.hashPool[i:] - - return true - } + hash := block.Hash() + self.hashPool = ethutil.DeleteFromByteSlice(self.hashPool, hash) + delete(self.pool, string(hash)) } - } - return false + } } func (self *BlockPool) Take(amount int, peer *Peer) (hashes [][]byte) { |