network: do not wait for headers file on startup

This commit is contained in:
ThomasV 2017-01-07 23:44:06 +01:00
parent 73390f1769
commit 1358bebd37
2 changed files with 17 additions and 13 deletions

View File

@ -44,9 +44,14 @@ class Blockchain(util.PrintError):
return self.local_height
def init(self):
self.init_headers_file()
self.set_local_height()
self.print_error("%d blocks" % self.local_height)
import threading
if os.path.exists(self.path()):
self.downloading_headers = False
return
self.downloading_headers = True
t = threading.Thread(target = self.init_headers_file)
t.daemon = True
t.start()
def verify_header(self, header, prev_header, bits, target):
prev_hash = self.hash_header(prev_header)
@ -107,8 +112,6 @@ class Blockchain(util.PrintError):
def init_headers_file(self):
filename = self.path()
if os.path.exists(filename):
return
try:
import urllib, socket
socket.setdefaulttimeout(30)
@ -119,6 +122,9 @@ class Blockchain(util.PrintError):
except Exception:
self.print_error("download failed. creating file", filename)
open(filename, 'wb+').close()
self.downloading_headers = False
self.set_local_height()
self.print_error("%d blocks" % self.local_height)
def save_chunk(self, index, chunk):
filename = self.path()

View File

@ -747,6 +747,8 @@ class Network(util.DaemonThread):
def on_get_header(self, interface, response):
'''Handle receiving a single block header'''
if self.blockchain.downloading_headers:
return
if self.bc_requests:
req_if, data = self.bc_requests[0]
req_height = data.get('header_height', -1)
@ -769,6 +771,8 @@ class Network(util.DaemonThread):
'''Send a request for the next header, or a chunk of them,
if necessary.
'''
if self.blockchain.downloading_headers:
return False
local_height, if_height = self.get_local_height(), data['if_height']
if if_height <= local_height:
return False
@ -787,14 +791,13 @@ class Network(util.DaemonThread):
# If the connection was lost move on
if not interface in self.interfaces.values():
continue
req_time = data.get('req_time')
if not req_time:
# No requests sent yet. This interface has a new height.
# Request headers if it is ahead of our blockchain
if not self.bc_request_headers(interface, data):
continue
elif time.time() - req_time > 10:
elif time.time() - req_time > 20:
interface.print_error("blockchain request timed out")
self.connection_down(interface.server)
continue
@ -823,12 +826,7 @@ class Network(util.DaemonThread):
self.process_responses(interface)
def run(self):
import threading
t = threading.Thread(target = self.blockchain.init)
t.daemon = True
t.start()
while t.isAlive() and self.is_running():
t.join(1)
self.blockchain.init()
while self.is_running():
self.maintain_sockets()
self.wait_on_sockets()