This commit is contained in:
obscuren 2014-09-24 11:55:02 +02:00
parent 57dc435f9b
commit b66fcf85df
2 changed files with 32 additions and 22 deletions

View File

@ -383,7 +383,6 @@ func (s *Ethereum) ReapDeadPeerHandler() {
// Start the ethereum // Start the ethereum
func (s *Ethereum) Start(seed bool) { func (s *Ethereum) Start(seed bool) {
s.reactor.Start() s.reactor.Start()
s.blockPool.Start()
// Bind to addr and port // Bind to addr and port
ln, err := net.Listen("tcp", ":"+s.Port) ln, err := net.Listen("tcp", ":"+s.Port)
if err != nil { if err != nil {

49
peer.go
View File

@ -131,6 +131,7 @@ type Peer struct {
// Last received pong message // Last received pong message
lastPong int64 lastPong int64
lastBlockReceived time.Time lastBlockReceived time.Time
lastHashReceived time.Time
host []byte host []byte
port uint16 port uint16
@ -505,6 +506,9 @@ func (p *Peer) HandleInbound() {
for it.Next() { for it.Next() {
hash := it.Value().Bytes() hash := it.Value().Bytes()
p.lastReceivedHash = hash
p.lastHashReceived = time.Now()
if blockPool.HasCommonHash(hash) { if blockPool.HasCommonHash(hash) {
foundCommonHash = true foundCommonHash = true
@ -512,10 +516,6 @@ func (p *Peer) HandleInbound() {
} }
blockPool.AddHash(hash) blockPool.AddHash(hash)
p.lastReceivedHash = hash
p.lastBlockReceived = time.Now()
} }
if foundCommonHash || msg.Data.Len() == 0 { if foundCommonHash || msg.Data.Len() == 0 {
@ -546,12 +546,12 @@ func (p *Peer) HandleInbound() {
if err != nil { if err != nil {
peerlogger.Infoln(err) peerlogger.Infoln(err)
} else { } /*else {
// Don't trigger if there's just one block. // Don't trigger if there's just one block.
if blockPool.Len() != 0 && msg.Data.Len() > 1 { if blockPool.Len() != 0 && msg.Data.Len() > 1 {
p.FetchBlocks() p.FetchBlocks()
} }
} }*/
} }
} }
} }
@ -583,18 +583,29 @@ func (self *Peer) FetchHashes() {
// General update method // General update method
func (self *Peer) update() { func (self *Peer) update() {
serviceTimer := time.NewTicker(5 * time.Second) serviceTimer := time.NewTicker(100 * time.Millisecond)
out: out:
for { for {
select { select {
case <-serviceTimer.C: case <-serviceTimer.C:
since := time.Since(self.lastBlockReceived) if self.IsCap("eth") {
if since > 10*time.Second && self.ethereum.blockPool.Len() != 0 && self.IsCap("eth") { var (
self.FetchHashes() sinceBlock = time.Since(self.lastBlockReceived)
} else if since > 5*time.Second { sinceHash = time.Since(self.lastHashReceived)
)
if sinceBlock > 5*time.Second && sinceHash > 5*time.Second {
self.catchingUp = false self.catchingUp = false
} }
if sinceHash > 10*time.Second && self.ethereum.blockPool.Len() != 0 {
// XXX While this is completely and utterly incorrect, in order to do anything on the test net is to do it this way
// Assume that when fetching hashes timeouts, we are done.
//self.FetchHashes()
self.FetchBlocks()
}
}
case <-self.quit: case <-self.quit:
break out break out
} }
@ -761,6 +772,14 @@ func (p *Peer) handleHandshake(msg *ethwire.Msg) {
return return
} }
// Self connect detection
pubkey := p.ethereum.KeyManager().PublicKey()
if bytes.Compare(pubkey[1:], pub) == 0 {
p.Stop()
return
}
usedPub := 0 usedPub := 0
// This peer is already added to the peerlist so we expect to find a double pubkey at least once // This peer is already added to the peerlist so we expect to find a double pubkey at least once
eachPeer(p.ethereum.Peers(), func(peer *Peer, e *list.Element) { eachPeer(p.ethereum.Peers(), func(peer *Peer, e *list.Element) {
@ -779,16 +798,8 @@ func (p *Peer) handleHandshake(msg *ethwire.Msg) {
// If this is an inbound connection send an ack back // If this is an inbound connection send an ack back
if p.inbound { if p.inbound {
p.port = uint16(port) p.port = uint16(port)
// Self connect detection
pubkey := p.ethereum.KeyManager().PublicKey()
if bytes.Compare(pubkey, p.pubkey) == 0 {
p.Stop()
return
} }
}
p.SetVersion(clientId) p.SetVersion(clientId)
p.versionKnown = true p.versionKnown = true