improve reorgs

This commit is contained in:
Victor Shyba 2018-09-25 22:06:22 -03:00 committed by Lex Berezhny
parent cdbb34e976
commit b0bc06ae00
3 changed files with 26 additions and 6 deletions

View file

@ -1,17 +1,36 @@
from orchstr8.testcase import IntegrationTestCase from orchstr8.testcase import IntegrationTestCase
from asyncio import sleep
class BlockchainReorganizationTests(IntegrationTestCase): class BlockchainReorganizationTests(IntegrationTestCase):
VERBOSE = True VERBOSE = True
async def test(self): async def test_reorg(self):
self.assertEqual(self.ledger.headers.height, 200) self.assertEqual(self.ledger.headers.height, 200)
await self.blockchain.generate(1) await self.blockchain.generate(1)
await self.on_header(201) await self.on_header(201)
self.assertEqual(self.ledger.headers.height, 201) self.assertEqual(self.ledger.headers.height, 201)
height = 201
await self.blockchain.invalidateblock(self.ledger.headers.hash(201).decode()) #simple fork (rewind+advance to immediate best)
await self.blockchain.generate(2) height = await self._simulate_reorg(height, 1, 1, 2)
await self.on_header(203) height = await self._simulate_reorg(height, 2, 1, 10)
height = await self._simulate_reorg(height, 4, 1, 3)
#lagged fork (rewind+batch catch up with immediate best)
height = await self._simulate_reorg(height, 4, 2, 3)
await self._simulate_reorg(height, 4, 4, 3)
async def _simulate_reorg(self, height, rewind, winners, progress):
for i in range(rewind):
await self.blockchain.invalidateblock(self.ledger.headers.hash(height - i).decode())
await self.blockchain.generate(rewind + winners)
height = height + winners
await self.on_header(height)
for i in range(progress):
await self.blockchain.generate(1)
height += 1
await self.on_header(height)
self.assertEquals(height, self.ledger.headers.height)
return height

View file

@ -107,7 +107,7 @@ class BaseHeaders:
yield threads.deferToThread(self.validate_chunk, height, chunk) yield threads.deferToThread(self.validate_chunk, height, chunk)
except InvalidHeader as e: except InvalidHeader as e:
bail = True bail = True
chunk = chunk[:(height-e.height)*self.header_size] chunk = chunk[:(height-e.height+1)*self.header_size]
written = 0 written = 0
if chunk: if chunk:
self.io.seek(height * self.header_size, os.SEEK_SET) self.io.seek(height * self.header_size, os.SEEK_SET)

View file

@ -20,4 +20,5 @@ commands =
unit: coverage run -p --source={envsitepackagesdir}/torba -m twisted.trial unit unit: coverage run -p --source={envsitepackagesdir}/torba -m twisted.trial unit
integration: orchstr8 download integration: orchstr8 download
integration: coverage run -p --source={envsitepackagesdir}/torba -m twisted.trial --reactor=asyncio integration.test_transactions integration: coverage run -p --source={envsitepackagesdir}/torba -m twisted.trial --reactor=asyncio integration.test_transactions
integration: coverage run -p --source={envsitepackagesdir}/torba -m twisted.trial --reactor=asyncio integration.test_blockchain_reorganization # Too slow on Travis
# integration: coverage run -p --source={envsitepackagesdir}/torba -m twisted.trial --reactor=asyncio integration.test_blockchain_reorganization