2018-11-04 06:55:50 +01:00
|
|
|
import logging
|
|
|
|
from torba.testcase import IntegrationTestCase
|
2018-08-16 06:43:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
class BlockchainReorganizationTests(IntegrationTestCase):
|
|
|
|
|
2018-11-04 06:55:50 +01:00
|
|
|
VERBOSITY = logging.WARN
|
2018-08-16 06:43:38 +02:00
|
|
|
|
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-11-04 06:55:50 +01:00
|
|
|
# simple fork (rewind+advance to immediate best)
|
2018-09-26 03:06:22 +02:00
|
|
|
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)
|
2018-11-04 06:55:50 +01:00
|
|
|
# lagged fork (rewind+batch catch up with immediate best)
|
2018-09-26 03:06:22 +02:00
|
|
|
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)
|
2018-11-04 06:55:50 +01:00
|
|
|
self.assertEqual(height, self.ledger.headers.height)
|
2018-09-26 03:06:22 +02:00
|
|
|
return height
|