commit 313d53fe0bccf7c34dc1e245c7f3d9c5608ab459
parent 2bb980c84c9e2fefd4685d48ffc47df63ad2d672
Author: ThomasV <thomasv@electrum.org>
Date: Fri, 21 Jul 2017 07:52:38 +0200
save header immediately when forking a chain
Diffstat:
2 files changed, 20 insertions(+), 16 deletions(-)
diff --git 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
@@ -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':