diff --git a/lib/blockchain.py b/lib/blockchain.py index ccdd2d8a4..fb51532db 100644 --- a/lib/blockchain.py +++ b/lib/blockchain.py @@ -300,9 +300,9 @@ class Blockchain(util.PrintError): new_bits = bitsN << 24 | bitsBase return new_bits, bitsBase << (8 * (bitsN-3)) - def can_connect(self, header): + def can_connect(self, header, check_height=True): height = header['block_height'] - if self.height() != height - 1: + if check_height and self.height() != height - 1: return False if height == 0: return hash_header(header) == bitcoin.GENESIS diff --git a/lib/network.py b/lib/network.py index ef64549b7..2d2e58fc4 100644 --- a/lib/network.py +++ b/lib/network.py @@ -843,23 +843,27 @@ class Network(util.DaemonThread): raise BaseException('error') next_height = None else: - if interface.blockchain.height() > interface.good: + bh = interface.blockchain.height() + next_height = None + if bh > interface.good: if not interface.blockchain.check_header(interface.bad_header): - self.blockchains[interface.bad] = b = interface.blockchain.fork(interface.bad) - interface.blockchain = b - interface.print_error("new chain", b.checkpoint) + if interface.blockchain.can_connect(interface.bad_header, check_height=False): + b = interface.blockchain.fork(interface.bad) + b.save_header(interface.bad_header) + self.blockchains[interface.bad] = b + interface.blockchain = b + interface.print_error("new chain", b.checkpoint) + interface.mode = 'catch_up' + next_height = interface.bad + 1 + interface.blockchain.catch_up = interface.server else: - assert interface.blockchain.height() == interface.good + assert bh == interface.good + if interface.blockchain.catch_up is None and bh < interface.tip: + interface.print_error("catching up from %d"% (bh + 1)) + interface.mode = 'catch_up' + next_height = bh + 1 + interface.blockchain.catch_up = interface.server - bh = interface.blockchain.height() - if interface.blockchain.catch_up is None and bh < interface.tip: - interface.print_error("catching up from %d"% (bh + 1)) - interface.mode = 'catch_up' - next_height = bh + 1 - interface.blockchain.catch_up = interface.server - else: - interface.print_error('already catching up') - next_height = None self.notify('updated') elif interface.mode == 'catch_up':