|
|
|
@ -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 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
block := self.pool[string(hash)].block |
|
|
|
|
if block != nil { |
|
|
|
|
f(block) |
|
|
|
|
|
|
|
|
|
delete(self.pool, string(hash)) |
|
|
|
|
} else { |
|
|
|
|
self.hashPool = self.hashPool[i:] |
|
|
|
|
func (self *BlockPool) CheckLinkAndProcess(f func(block *ethchain.Block)) { |
|
|
|
|
|
|
|
|
|
return false |
|
|
|
|
var blocks ethchain.Blocks |
|
|
|
|
for _, item := range self.pool { |
|
|
|
|
if item.block != nil { |
|
|
|
|
blocks = append(blocks, item.block) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return true |
|
|
|
|
} |
|
|
|
|
ethchain.BlockBy(ethchain.Number).Sort(blocks) |
|
|
|
|
for _, block := range blocks { |
|
|
|
|
if self.eth.BlockChain().HasBlock(block.PrevHash) { |
|
|
|
|
f(block) |
|
|
|
|
|
|
|
|
|
return false |
|
|
|
|
hash := block.Hash() |
|
|
|
|
self.hashPool = ethutil.DeleteFromByteSlice(self.hashPool, hash) |
|
|
|
|
delete(self.pool, string(hash)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (self *BlockPool) IsLinked() bool { |
|
|
|
|
if len(self.hashPool) == 0 { |
|
|
|
|
return false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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 |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (self *BlockPool) Take(amount int, peer *Peer) (hashes [][]byte) { |
|
|
|
|