2018-08-16 06:43:38 +02:00
|
|
|
from orchstr8.testcase import IntegrationTestCase
|
2018-09-26 03:06:22 +02:00
|
|
|
from asyncio import sleep
|
2018-08-16 06:43:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
class BlockchainReorganizationTests(IntegrationTestCase):
|
|
|
|
|
|
|
|
VERBOSE = True
|
|
|
|
|
2018-09-26 03:06:22 +02:00
|
|
|
async def test_reorg(self):
|
2018-08-16 06:43:38 +02:00
|
|
|
self.assertEqual(self.ledger.headers.height, 200)
|
|
|
|
|
|
|
|
await self.blockchain.generate(1)
|
|
|
|
await self.on_header(201)
|
|
|
|
self.assertEqual(self.ledger.headers.height, 201)
|
2018-09-26 03:06:22 +02:00
|
|
|
height = 201
|
2018-08-16 06:43:38 +02:00
|
|
|
|
2018-09-26 03:06:22 +02:00
|
|
|
#simple fork (rewind+advance to immediate best)
|
|
|
|
height = await self._simulate_reorg(height, 1, 1, 2)
|
|
|
|
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
|