Commit a34a971b authored by obscuren's avatar obscuren

improved blockchain downloading

parent 5fa0173c
...@@ -217,9 +217,7 @@ out: ...@@ -217,9 +217,7 @@ out:
} }
}) })
if !self.fetchingHashes && len(self.hashPool) > 0 {
self.DistributeHashes() self.DistributeHashes()
}
if self.ChainLength < len(self.hashPool) { if self.ChainLength < len(self.hashPool) {
self.ChainLength = len(self.hashPool) self.ChainLength = len(self.hashPool)
......
...@@ -131,7 +131,7 @@ type Peer struct { ...@@ -131,7 +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 doneFetchingHashes bool
host []byte host []byte
port uint16 port uint16
...@@ -178,6 +178,7 @@ func NewPeer(conn net.Conn, ethereum *Ethereum, inbound bool) *Peer { ...@@ -178,6 +178,7 @@ func NewPeer(conn net.Conn, ethereum *Ethereum, inbound bool) *Peer {
version: ethereum.ClientIdentity().String(), version: ethereum.ClientIdentity().String(),
protocolCaps: ethutil.NewValue(nil), protocolCaps: ethutil.NewValue(nil),
td: big.NewInt(0), td: big.NewInt(0),
doneFetchingHashes: true,
} }
} }
...@@ -194,6 +195,7 @@ func NewOutboundPeer(addr string, ethereum *Ethereum, caps Caps) *Peer { ...@@ -194,6 +195,7 @@ func NewOutboundPeer(addr string, ethereum *Ethereum, caps Caps) *Peer {
version: ethereum.ClientIdentity().String(), version: ethereum.ClientIdentity().String(),
protocolCaps: ethutil.NewValue(nil), protocolCaps: ethutil.NewValue(nil),
td: big.NewInt(0), td: big.NewInt(0),
doneFetchingHashes: true,
} }
// Set up the connection in another goroutine so we don't block the main thread // Set up the connection in another goroutine so we don't block the main thread
...@@ -503,20 +505,22 @@ func (p *Peer) HandleInbound() { ...@@ -503,20 +505,22 @@ func (p *Peer) HandleInbound() {
it := msg.Data.NewIterator() it := msg.Data.NewIterator()
for it.Next() { for it.Next() {
hash := it.Value().Bytes() hash := it.Value().Bytes()
p.lastReceivedHash = hash
if blockPool.HasCommonHash(hash) { if blockPool.HasCommonHash(hash) {
foundCommonHash = true foundCommonHash = true
break break
} }
p.lastReceivedHash = hash
p.LastHashReceived = time.Now()
blockPool.AddHash(hash, p) blockPool.AddHash(hash, p)
} }
if !foundCommonHash && msg.Data.Len() != 0 { if !foundCommonHash && msg.Data.Len() != 0 {
p.FetchHashes() p.FetchHashes()
} else {
peerlogger.Infof("Found common hash (%x...)\n", p.lastReceivedHash[0:4])
p.doneFetchingHashes = true
} }
case ethwire.MsgBlockTy: case ethwire.MsgBlockTy:
...@@ -543,11 +547,15 @@ func (p *Peer) HandleInbound() { ...@@ -543,11 +547,15 @@ func (p *Peer) HandleInbound() {
func (self *Peer) FetchBlocks(hashes [][]byte) { func (self *Peer) FetchBlocks(hashes [][]byte) {
if len(hashes) > 0 { if len(hashes) > 0 {
peerlogger.Debugf("Fetching blocks (%d)\n", len(hashes))
self.QueueMessage(ethwire.NewMessage(ethwire.MsgGetBlocksTy, ethutil.ByteSliceToInterface(hashes))) self.QueueMessage(ethwire.NewMessage(ethwire.MsgGetBlocksTy, ethutil.ByteSliceToInterface(hashes)))
} }
} }
func (self *Peer) FetchHashes() { func (self *Peer) FetchHashes() {
self.doneFetchingHashes = false
blockPool := self.ethereum.blockPool blockPool := self.ethereum.blockPool
if self.td.Cmp(self.ethereum.HighestTDPeer()) >= 0 { if self.td.Cmp(self.ethereum.HighestTDPeer()) >= 0 {
...@@ -562,7 +570,7 @@ func (self *Peer) FetchHashes() { ...@@ -562,7 +570,7 @@ func (self *Peer) FetchHashes() {
} }
func (self *Peer) FetchingHashes() bool { func (self *Peer) FetchingHashes() bool {
return time.Since(self.LastHashReceived) < 200*time.Millisecond return !self.doneFetchingHashes
} }
// General update method // General update method
...@@ -576,10 +584,9 @@ out: ...@@ -576,10 +584,9 @@ out:
if self.IsCap("eth") { if self.IsCap("eth") {
var ( var (
sinceBlock = time.Since(self.lastBlockReceived) sinceBlock = time.Since(self.lastBlockReceived)
sinceHash = time.Since(self.LastHashReceived)
) )
if sinceBlock > 5*time.Second && sinceHash > 5*time.Second { if sinceBlock > 5*time.Second {
self.catchingUp = false self.catchingUp = false
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment