improved blockchain downloading

pull/150/head
obscuren 10 years ago
parent 5fa0173c41
commit a34a971b50
  1. 2
      block_pool.go
  2. 21
      peer.go

@ -217,9 +217,7 @@ out:
}
})
if !self.fetchingHashes && len(self.hashPool) > 0 {
self.DistributeHashes()
}
if self.ChainLength < len(self.hashPool) {
self.ChainLength = len(self.hashPool)

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

Loading…
Cancel
Save