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

View File

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