commit d371e1c807f3cf8b0a65d03f470f104baf5416cc
parent f46b403a8d2ffc382d12b273932bd73d7127df03
Author: ThomasV <thomasv@electrum.org>
Date: Wed, 29 Mar 2017 17:21:18 +0200
network: request checkpoint when opening a new connection
Diffstat:
2 files changed, 13 insertions(+), 2 deletions(-)
diff --git a/lib/blockchain.py b/lib/blockchain.py
@@ -54,12 +54,17 @@ class Blockchain(util.PrintError):
t.daemon = True
t.start()
+ def pass_checkpoint(self, header):
+ if header.get('block_height') != self.checkpoint_height:
+ return True
+ return self.hash_header(header) == self.checkpoint_hash
+
def verify_header(self, header, prev_header, bits, target):
prev_hash = self.hash_header(prev_header)
_hash = self.hash_header(header)
if prev_hash != header.get('prev_block_hash'):
raise BaseException("prev hash mismatch: %s vs %s" % (prev_hash, header.get('prev_block_hash')))
- if self.checkpoint_height == header.get('block_height') and self.checkpoint_hash != _hash:
+ if not self.pass_checkpoint(header):
raise BaseException('failed checkpoint')
if self.checkpoint_height == header.get('block_height'):
self.print_error("validated checkpoint", self.checkpoint_height)
diff --git a/lib/network.py b/lib/network.py
@@ -693,6 +693,7 @@ class Network(util.DaemonThread):
def new_interface(self, server, socket):
self.add_recent_server(server)
self.interfaces[server] = interface = Interface(server, socket)
+ self.queue_request('blockchain.block.get_header', [self.blockchain.checkpoint_height], interface)
self.queue_request('blockchain.headers.subscribe', [], interface)
if server == self.default_server:
self.switch_to_interface(server)
@@ -774,6 +775,11 @@ class Network(util.DaemonThread):
def on_get_header(self, interface, response):
'''Handle receiving a single block header'''
+ # close connection if header does not pass checkpoint
+ if not self.blockchain.pass_checkpoint(response['result']):
+ interface.print_error("header did not pass checkpoint, dismissing interface", interface.host)
+ self.connection_down(interface.server)
+ return
if self.blockchain.downloading_headers:
return
if self.bc_requests:
@@ -790,7 +796,7 @@ class Network(util.DaemonThread):
self.notify('updated')
else:
interface.print_error("header didn't connect, dismissing interface")
- interface.close()
+ self.connection_down(interface.server)
else:
self.request_header(interface, data, next_height)