save header immediately when forking a chain

This commit is contained in:
ThomasV 2017-07-21 07:52:38 +02:00
parent 2bb980c84c
commit 313d53fe0b
2 changed files with 21 additions and 17 deletions

View File

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

View File

@ -843,23 +843,27 @@ class Network(util.DaemonThread):
raise BaseException('error')
next_height = None
else:
if interface.blockchain.height() > 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)
else:
assert interface.blockchain.height() == interface.good
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
next_height = None
if bh > interface.good:
if not interface.blockchain.check_header(interface.bad_header):
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:
interface.print_error('already catching up')
next_height = None
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
self.notify('updated')
elif interface.mode == 'catch_up':