fix blockchain offset

This commit is contained in:
ThomasV 2017-07-14 06:20:05 +02:00
parent ba2211f52e
commit 37143fd628
2 changed files with 8 additions and 9 deletions

View File

@ -147,7 +147,7 @@ class Blockchain(util.PrintError):
def save_header(self, header): def save_header(self, header):
height = header.get('block_height') height = header.get('block_height')
if not self.is_saved: if not self.is_saved:
assert height == self.checkpoint + len(self.headers) + 1 assert height == self.checkpoint + len(self.headers)
self.headers.append(header) self.headers.append(header)
if len(self.headers) > 10: if len(self.headers) > 10:
self.fork_and_save() self.fork_and_save()

View File

@ -840,7 +840,7 @@ class Network(util.DaemonThread):
self.connection_down(interface.server) self.connection_down(interface.server)
interface.request = None interface.request = None
return return
can_connect = self.can_connect(header) can_connect = interface.blockchain.can_connect(header)
if interface.mode == 'backward': if interface.mode == 'backward':
if can_connect: if can_connect:
interface.good = height interface.good = height
@ -863,23 +863,21 @@ class Network(util.DaemonThread):
if interface.bad != interface.good + 1: if interface.bad != interface.good + 1:
next_height = (interface.bad + interface.good) // 2 next_height = (interface.bad + interface.good) // 2
else: else:
delta1 = interface.blockchain.height() - interface.good interface.print_error("can connect at %d"% interface.good)
delta2 = interface.tip - interface.good interface.blockchain = Blockchain(self.config, interface.blockchain.filename, interface.good)
interface.print_error("chain split detected at %d"%interface.good, delta1, delta2)
interface.blockchain = Blockchain(self.config, False, interface.bad)
interface.blockchain.catch_up = interface.server interface.blockchain.catch_up = interface.server
self.blockchains[interface.bad] = interface.blockchain self.blockchains[interface.good] = interface.blockchain
interface.print_error("catching up") interface.print_error("catching up with new chain")
interface.mode = 'catch_up' interface.mode = 'catch_up'
next_height = interface.good next_height = interface.good
elif interface.mode == 'catch_up': elif interface.mode == 'catch_up':
if can_connect: if can_connect:
interface.blockchain.save_header(header) interface.blockchain.save_header(header)
self.notify('updated')
next_height = height + 1 if height < interface.tip else None next_height = height + 1 if height < interface.tip else None
else: else:
# go back # go back
interface.print_error("cannot connect", height)
interface.mode = 'backward' interface.mode = 'backward'
interface.bad = height interface.bad = height
next_height = height - 1 next_height = height - 1
@ -890,6 +888,7 @@ class Network(util.DaemonThread):
interface.mode = 'default' interface.mode = 'default'
interface.print_error('catch up done', interface.blockchain.height()) interface.print_error('catch up done', interface.blockchain.height())
interface.blockchain.catch_up = None interface.blockchain.catch_up = None
self.notify('updated')
elif interface.mode == 'default': elif interface.mode == 'default':
assert not can_connect assert not can_connect