Browse Source

network: request checkpoint when opening a new connection

2.9.x
ThomasV 8 years ago
parent
commit
d371e1c807
  1. 7
      lib/blockchain.py
  2. 8
      lib/network.py

7
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)

8
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)

Loading…
Cancel
Save