@ -114,10 +114,8 @@ func (self *peers) addToBlacklist(id string) {
self . blacklist [ id ] = time . Now ( )
self . blacklist [ id ] = time . Now ( )
}
}
// suspended checks if peer is still suspended
// suspended checks if peer is still suspended, caller should hold peers.lock
func ( self * peers ) suspended ( id string ) ( s bool ) {
func ( self * peers ) suspended ( id string ) ( s bool ) {
self . lock . Lock ( )
defer self . lock . Unlock ( )
if suspendedAt , ok := self . blacklist [ id ] ; ok {
if suspendedAt , ok := self . blacklist [ id ] ; ok {
if s = suspendedAt . Add ( self . bp . Config . PeerSuspensionInterval ) . After ( time . Now ( ) ) ; ! s {
if s = suspendedAt . Add ( self . bp . Config . PeerSuspensionInterval ) . After ( time . Now ( ) ) ; ! s {
// no longer suspended, delete entry
// no longer suspended, delete entry
@ -205,13 +203,14 @@ func (self *peers) addPeer(
peerError func ( * errs . Error ) ,
peerError func ( * errs . Error ) ,
) ( best bool , suspended bool ) {
) ( best bool , suspended bool ) {
self . lock . Lock ( )
defer self . lock . Unlock ( )
var previousBlockHash common . Hash
var previousBlockHash common . Hash
if self . suspended ( id ) {
if self . suspended ( id ) {
suspended = true
suspended = true
return
return
}
}
self . lock . Lock ( )
defer self . lock . Unlock ( )
p , found := self . peers [ id ]
p , found := self . peers [ id ]
if found {
if found {
// when called on an already connected peer, it means a newBlockMsg is received
// when called on an already connected peer, it means a newBlockMsg is received
@ -255,7 +254,7 @@ func (self *peers) addPeer(
p . headSectionC <- nil
p . headSectionC <- nil
if entry := self . bp . get ( previousBlockHash ) ; entry != nil {
if entry := self . bp . get ( previousBlockHash ) ; entry != nil {
plog . DebugDetailf ( "addPeer: <%s> previous head : %v found in pool, activate" , id , hex ( previousBlockHash ) )
plog . DebugDetailf ( "addPeer: <%s> previous head : %v found in pool, activate" , id , hex ( previousBlockHash ) )
self . bp . activateChain ( entry . section , p , nil )
self . bp . activateChain ( entry . section , p , p . switchC , nil )
p . sections = append ( p . sections , previousBlockHash )
p . sections = append ( p . sections , previousBlockHash )
}
}
}
}
@ -265,8 +264,8 @@ func (self *peers) addPeer(
currentTD := self . bp . getTD ( )
currentTD := self . bp . getTD ( )
bestpeer := self . best
bestpeer := self . best
if bestpeer != nil {
if bestpeer != nil {
bestpeer . lock . Lock ( )
bestpeer . lock . R Lock( )
defer bestpeer . lock . Unlock ( )
defer bestpeer . lock . R Unlock( )
currentTD = self . best . td
currentTD = self . best . td
}
}
if td . Cmp ( currentTD ) > 0 {
if td . Cmp ( currentTD ) > 0 {
@ -362,14 +361,14 @@ func (self *BlockPool) switchPeer(oldp, newp *peer) {
if connected [ hash ] == nil {
if connected [ hash ] == nil {
// if not deleted, then reread from pool (it can be orphaned top half of a split section)
// if not deleted, then reread from pool (it can be orphaned top half of a split section)
if entry := self . get ( hash ) ; entry != nil {
if entry := self . get ( hash ) ; entry != nil {
self . activateChain ( entry . section , newp , connected )
self . activateChain ( entry . section , newp , newp . switchC , connected )
connected [ hash ] = entry . section
connected [ hash ] = entry . section
sections = append ( sections , hash )
sections = append ( sections , hash )
}
}
}
}
}
}
plog . DebugDetailf ( "<%s> section processes (%v non-contiguous sequences, was %v before)" , newp . id , len ( sections ) , len ( newp . sections ) )
plog . DebugDetailf ( "<%s> section processes (%v non-contiguous sequences, was %v before)" , newp . id , len ( sections ) , len ( newp . sections ) )
// need to lock now that newp is exposed to section processes
// need to lock now that newp is exposed to section processesr
newp . lock . Lock ( )
newp . lock . Lock ( )
newp . sections = sections
newp . sections = sections
newp . lock . Unlock ( )
newp . lock . Unlock ( )
@ -457,6 +456,8 @@ func (self *peer) getCurrentBlock(currentBlock *types.Block) {
}
}
func ( self * peer ) getBlockHashes ( ) bool {
func ( self * peer ) getBlockHashes ( ) bool {
self . lock . Lock ( )
defer self . lock . Unlock ( )
//if connecting parent is found
//if connecting parent is found
if self . bp . hasBlock ( self . parentHash ) {
if self . bp . hasBlock ( self . parentHash ) {
plog . DebugDetailf ( "HeadSection: <%s> parent block %s found in blockchain" , self . id , hex ( self . parentHash ) )
plog . DebugDetailf ( "HeadSection: <%s> parent block %s found in blockchain" , self . id , hex ( self . parentHash ) )
@ -470,7 +471,7 @@ func (self *peer) getBlockHashes() bool {
self . bp . status . badPeers [ self . id ] ++
self . bp . status . badPeers [ self . id ] ++
} else {
} else {
// XXX added currentBlock check (?)
// XXX added currentBlock check (?)
if self . currentBlock != nil && self . currentBlock . Td != nil {
if self . currentBlock != nil && self . currentBlock . Td != nil && ! self . currentBlock . Queued ( ) {
if self . td . Cmp ( self . currentBlock . Td ) != 0 {
if self . td . Cmp ( self . currentBlock . Td ) != 0 {
// self.addError(ErrIncorrectTD, "on block %x", self.currentBlockHash)
// self.addError(ErrIncorrectTD, "on block %x", self.currentBlockHash)
// self.bp.status.badPeers[self.id]++
// self.bp.status.badPeers[self.id]++
@ -499,7 +500,7 @@ func (self *peer) getBlockHashes() bool {
self . bp . newSection ( [ ] * node { n } ) . activate ( self )
self . bp . newSection ( [ ] * node { n } ) . activate ( self )
} else {
} else {
plog . DebugDetailf ( "HeadSection: <%s> connecting parent %s found in pool...head section [%s] exists...not requesting hashes" , self . id , hex ( self . parentHash ) , sectionhex ( parent . section ) )
plog . DebugDetailf ( "HeadSection: <%s> connecting parent %s found in pool...head section [%s] exists...not requesting hashes" , self . id , hex ( self . parentHash ) , sectionhex ( parent . section ) )
self . bp . activateChain ( parent . section , self , nil )
self . bp . activateChain ( parent . section , self , self . switchC , nil )
}
}
} else {
} else {
plog . DebugDetailf ( "HeadSection: <%s> section [%s] requestBlockHashes" , self . id , sectionhex ( self . headSection ) )
plog . DebugDetailf ( "HeadSection: <%s> section [%s] requestBlockHashes" , self . id , sectionhex ( self . headSection ) )
@ -523,6 +524,7 @@ func (self *peer) run() {
self . lock . RLock ( )
self . lock . RLock ( )
switchC := self . switchC
switchC := self . switchC
plog . Debugf ( "HeadSection: <%s> section process for head %s started" , self . id , hex ( self . currentBlockHash ) )
self . lock . RUnlock ( )
self . lock . RUnlock ( )
self . blockHashesRequestTimer = nil
self . blockHashesRequestTimer = nil
@ -589,6 +591,7 @@ LOOP:
plog . Debugf ( "HeadSection: <%s> (headsection [%s]) quit channel closed : timed out without providing new blocks...quitting" , self . id , sectionhex ( self . headSection ) )
plog . Debugf ( "HeadSection: <%s> (headsection [%s]) quit channel closed : timed out without providing new blocks...quitting" , self . id , sectionhex ( self . headSection ) )
}
}
}
}
if ! self . idle {
if ! self . idle {
self . idle = true
self . idle = true
self . bp . wg . Done ( )
self . bp . wg . Done ( )