|
|
|
@ -427,7 +427,7 @@ class Interface(PrintError): |
|
|
|
|
|
|
|
|
|
|
|
chain = blockchain.check_header(header) if 'mock' not in header else header['mock']['check'](header) |
|
|
|
chain = blockchain.check_header(header) if 'mock' not in header else header['mock']['check'](header) |
|
|
|
if chain: |
|
|
|
if chain: |
|
|
|
self.blockchain = chain |
|
|
|
self.blockchain = chain if isinstance(chain, Blockchain) else self.blockchain |
|
|
|
return 'catchup', height+1 |
|
|
|
return 'catchup', height+1 |
|
|
|
|
|
|
|
|
|
|
|
can_connect = blockchain.can_connect(header) if 'mock' not in header else header['mock']['connect'](height) |
|
|
|
can_connect = blockchain.can_connect(header) if 'mock' not in header else header['mock']['connect'](height) |
|
|
|
@ -445,32 +445,36 @@ class Interface(PrintError): |
|
|
|
self.blockchain.save_header(header) |
|
|
|
self.blockchain.save_header(header) |
|
|
|
return 'catchup', height |
|
|
|
return 'catchup', height |
|
|
|
|
|
|
|
|
|
|
|
# binary |
|
|
|
good, height, bad, bad_header = await self._search_headers_binary(height, bad, bad_header, chain) |
|
|
|
if isinstance(chain, Blockchain): # not when mocking |
|
|
|
return await self._resolve_potential_chain_fork_given_forkpoint(good, height, bad, bad_header) |
|
|
|
self.blockchain = chain |
|
|
|
|
|
|
|
|
|
|
|
async def _search_headers_binary(self, height, bad, bad_header, chain): |
|
|
|
|
|
|
|
self.blockchain = chain if isinstance(chain, Blockchain) else self.blockchain |
|
|
|
good = height |
|
|
|
good = height |
|
|
|
height = (bad + good) // 2 |
|
|
|
|
|
|
|
header = await self.get_block_header(height, 'binary') |
|
|
|
|
|
|
|
while True: |
|
|
|
while True: |
|
|
|
self.print_error("binary step") |
|
|
|
assert good < bad, (good, bad) |
|
|
|
|
|
|
|
height = (good + bad) // 2 |
|
|
|
|
|
|
|
self.print_error("binary step. good {}, bad {}, height {}".format(good, bad, height)) |
|
|
|
|
|
|
|
header = await self.get_block_header(height, 'binary') |
|
|
|
chain = blockchain.check_header(header) if 'mock' not in header else header['mock']['check'](header) |
|
|
|
chain = blockchain.check_header(header) if 'mock' not in header else header['mock']['check'](header) |
|
|
|
if chain: |
|
|
|
if chain: |
|
|
|
|
|
|
|
self.blockchain = chain if isinstance(chain, Blockchain) else self.blockchain |
|
|
|
good = height |
|
|
|
good = height |
|
|
|
self.blockchain = self.blockchain if type(chain) in [bool, int] else chain |
|
|
|
|
|
|
|
else: |
|
|
|
else: |
|
|
|
bad = height |
|
|
|
bad = height |
|
|
|
bad_header = header |
|
|
|
bad_header = header |
|
|
|
assert good < bad, (good, bad) |
|
|
|
if good + 1 == bad: |
|
|
|
if bad != good + 1: |
|
|
|
break |
|
|
|
height = (bad + good) // 2 |
|
|
|
|
|
|
|
header = await self.get_block_header(height, 'binary') |
|
|
|
|
|
|
|
continue |
|
|
|
|
|
|
|
mock = bad_header and 'mock' in bad_header and bad_header['mock']['connect'](height) |
|
|
|
mock = bad_header and 'mock' in bad_header and bad_header['mock']['connect'](height) |
|
|
|
real = not mock and self.blockchain.can_connect(bad_header, check_height=False) |
|
|
|
real = not mock and self.blockchain.can_connect(bad_header, check_height=False) |
|
|
|
if not real and not mock: |
|
|
|
if not real and not mock: |
|
|
|
raise Exception('unexpected bad header during binary' + str(bad_header)) # line 948 in 8e69174374aee87d73cd2f8005fbbe87c93eee9c's network.py |
|
|
|
raise Exception('unexpected bad header during binary: {}'.format(bad_header)) |
|
|
|
branch = blockchain.blockchains.get(bad) |
|
|
|
|
|
|
|
self.print_error("binary search exited. good {}, bad {}".format(good, bad)) |
|
|
|
self.print_error("binary search exited. good {}, bad {}".format(good, bad)) |
|
|
|
|
|
|
|
return good, height, bad, bad_header |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def _resolve_potential_chain_fork_given_forkpoint(self, good, height, bad, bad_header): |
|
|
|
|
|
|
|
branch = blockchain.blockchains.get(bad) |
|
|
|
if branch is not None: |
|
|
|
if branch is not None: |
|
|
|
self.print_error("existing fork found at bad height {}".format(bad)) |
|
|
|
self.print_error("existing fork found at bad height {}".format(bad)) |
|
|
|
ismocking = type(branch) is dict |
|
|
|
ismocking = type(branch) is dict |
|
|
|
@ -516,7 +520,7 @@ class Interface(PrintError): |
|
|
|
else: |
|
|
|
else: |
|
|
|
assert bh == good |
|
|
|
assert bh == good |
|
|
|
if bh < self.tip: |
|
|
|
if bh < self.tip: |
|
|
|
self.print_error("catching up from %d"% (bh + 1)) |
|
|
|
self.print_error("catching up from %d" % (bh + 1)) |
|
|
|
height = bh + 1 |
|
|
|
height = bh + 1 |
|
|
|
return 'no_fork', height |
|
|
|
return 'no_fork', height |
|
|
|
|
|
|
|
|
|
|
|
|