Kick off bad peers on bad chains and improved catch up on diverted chain

This commit is contained in:
obscuren 2014-10-02 17:35:38 +02:00
parent a75c92000f
commit 677836cbee
2 changed files with 19 additions and 10 deletions

View File

@ -56,6 +56,11 @@ func (self *BlockPool) Len() int {
return len(self.hashPool) return len(self.hashPool)
} }
func (self *BlockPool) Reset() {
self.pool = make(map[string]*block)
self.hashPool = nil
}
func (self *BlockPool) HasLatestHash() bool { func (self *BlockPool) HasLatestHash() bool {
self.mut.Lock() self.mut.Lock()
defer self.mut.Unlock() defer self.mut.Unlock()
@ -77,7 +82,7 @@ func (self *BlockPool) Blocks() (blocks ethchain.Blocks) {
return return
} }
func (self *BlockPool) FetchHashes(peer *Peer) { func (self *BlockPool) FetchHashes(peer *Peer) bool {
highestTd := self.eth.HighestTDPeer() highestTd := self.eth.HighestTDPeer()
if (self.peer == nil && peer.td.Cmp(highestTd) >= 0) || (self.peer != nil && peer.td.Cmp(self.peer.td) >= 0) || self.peer == peer { if (self.peer == nil && peer.td.Cmp(highestTd) >= 0) || (self.peer != nil && peer.td.Cmp(self.peer.td) >= 0) || self.peer == peer {
@ -95,7 +100,11 @@ func (self *BlockPool) FetchHashes(peer *Peer) {
peerlogger.Debugf("Fetching hashes (%d)\n", amount) peerlogger.Debugf("Fetching hashes (%d)\n", amount)
peer.QueueMessage(ethwire.NewMessage(ethwire.MsgGetBlockHashesTy, []interface{}{peer.lastReceivedHash, uint32(amount)})) peer.QueueMessage(ethwire.NewMessage(ethwire.MsgGetBlockHashesTy, []interface{}{peer.lastReceivedHash, uint32(amount)}))
} }
return true
} }
return false
} }
func (self *BlockPool) AddHash(hash []byte, peer *Peer) { func (self *BlockPool) AddHash(hash []byte, peer *Peer) {
@ -122,7 +131,7 @@ func (self *BlockPool) Add(b *ethchain.Block, peer *Peer) {
self.pool[hash] = &block{peer, peer, b, time.Now(), 0} self.pool[hash] = &block{peer, peer, b, time.Now(), 0}
if !self.eth.BlockChain().HasBlock(b.PrevHash) && self.pool[string(b.PrevHash)] == nil && !self.fetchingHashes { if !self.eth.BlockChain().HasBlock(b.PrevHash) && self.pool[string(b.PrevHash)] == nil && !self.fetchingHashes {
poollogger.Infof("Unknown block, requesting parent (%x...)\n", b.PrevHash[0:4]) poollogger.Infof("Unknown chain, requesting (%x...)\n", b.PrevHash[0:4])
peer.QueueMessage(ethwire.NewMessage(ethwire.MsgGetBlockHashesTy, []interface{}{b.Hash(), uint32(256)})) peer.QueueMessage(ethwire.NewMessage(ethwire.MsgGetBlockHashesTy, []interface{}{b.Hash(), uint32(256)}))
} }
} else if self.pool[hash] != nil { } else if self.pool[hash] != nil {
@ -308,6 +317,7 @@ out:
} }
if err != nil { if err != nil {
self.Reset()
// Remove this bad chain // Remove this bad chain
for _, block := range blocks { for _, block := range blocks {
self.Remove(block.Hash()) self.Remove(block.Hash())

15
peer.go
View File

@ -202,7 +202,7 @@ func NewOutboundPeer(addr string, ethereum *Ethereum, caps Caps) *Peer {
go func() { go func() {
conn, err := p.Connect(addr) conn, err := p.Connect(addr)
if err != nil { if err != nil {
peerlogger.Debugln("Connection to peer failed. Giving up.", err) //peerlogger.Debugln("Connection to peer failed. Giving up.", err)
p.Stop() p.Stop()
return return
} }
@ -517,7 +517,9 @@ func (p *Peer) HandleInbound() {
} }
if !foundCommonHash && msg.Data.Len() != 0 { if !foundCommonHash && msg.Data.Len() != 0 {
p.FetchHashes() if !p.FetchHashes() {
p.doneFetchingHashes = true
}
} else { } else {
peerlogger.Infof("Found common hash (%x...)\n", p.lastReceivedHash[0:4]) peerlogger.Infof("Found common hash (%x...)\n", p.lastReceivedHash[0:4])
p.doneFetchingHashes = true p.doneFetchingHashes = true
@ -553,9 +555,9 @@ func (self *Peer) FetchBlocks(hashes [][]byte) {
} }
} }
func (self *Peer) FetchHashes() { func (self *Peer) FetchHashes() bool {
blockPool := self.ethereum.blockPool blockPool := self.ethereum.blockPool
blockPool.FetchHashes(self) return blockPool.FetchHashes(self)
/* /*
if self.td.Cmp(self.ethereum.HighestTDPeer()) >= 0 { if self.td.Cmp(self.ethereum.HighestTDPeer()) >= 0 {
@ -718,10 +720,7 @@ func (self *Peer) handleStatus(msg *ethwire.Msg) {
// Compare the total TD with the blockchain TD. If remote is higher // Compare the total TD with the blockchain TD. If remote is higher
// fetch hashes from highest TD node. // fetch hashes from highest TD node.
if self.td.Cmp(self.ethereum.BlockChain().TD) > 0 { self.FetchHashes()
self.ethereum.blockPool.AddHash(self.lastReceivedHash, self)
self.FetchHashes()
}
ethlogger.Infof("Peer is [eth] capable. (TD = %v ~ %x) %d / %d", self.td, self.bestHash, protoVersion, netVersion) ethlogger.Infof("Peer is [eth] capable. (TD = %v ~ %x) %d / %d", self.td, self.bestHash, protoVersion, netVersion)