2018-11-04 01:55:50 -04:00
|
|
|
import logging
|
2019-12-31 15:30:13 -05:00
|
|
|
from lbry.testcase import IntegrationTestCase
|
2018-08-16 00:43:38 -04:00
|
|
|
|
|
|
|
|
|
|
|
class BlockchainReorganizationTests(IntegrationTestCase):
|
|
|
|
|
2018-11-04 01:55:50 -04:00
|
|
|
VERBOSITY = logging.WARN
|
2018-08-16 00:43:38 -04:00
|
|
|
|
2019-06-18 19:42:18 -04:00
|
|
|
async def assertBlockHash(self, height):
|
|
|
|
self.assertEqual(
|
2020-03-21 04:32:03 -03:00
|
|
|
(await self.ledger.headers.hash(height)).decode(),
|
2019-06-18 19:42:18 -04:00
|
|
|
await self.blockchain.get_block_hash(height)
|
|
|
|
)
|
|
|
|
|
2018-09-25 22:06:22 -03:00
|
|
|
async def test_reorg(self):
|
2019-06-18 17:50:41 -04:00
|
|
|
# invalidate current block, move forward 2
|
2018-08-16 00:43:38 -04:00
|
|
|
self.assertEqual(self.ledger.headers.height, 200)
|
2019-06-18 19:42:18 -04:00
|
|
|
await self.assertBlockHash(200)
|
2020-03-21 04:32:03 -03:00
|
|
|
await self.blockchain.invalidate_block((await self.ledger.headers.hash(200)).decode())
|
2019-06-18 17:50:41 -04:00
|
|
|
await self.blockchain.generate(2)
|
|
|
|
await self.ledger.on_header.where(lambda e: e.height == 201)
|
2018-08-16 00:43:38 -04:00
|
|
|
self.assertEqual(self.ledger.headers.height, 201)
|
2019-06-18 19:42:18 -04:00
|
|
|
await self.assertBlockHash(200)
|
|
|
|
await self.assertBlockHash(201)
|
2018-09-25 22:06:22 -03:00
|
|
|
|
2019-06-18 17:50:41 -04:00
|
|
|
# invalidate current block, move forward 3
|
2020-03-21 04:32:03 -03:00
|
|
|
await self.blockchain.invalidate_block((await self.ledger.headers.hash(200)).decode())
|
2019-06-18 17:50:41 -04:00
|
|
|
await self.blockchain.generate(3)
|
|
|
|
await self.ledger.on_header.where(lambda e: e.height == 202)
|
|
|
|
self.assertEqual(self.ledger.headers.height, 202)
|
2019-06-18 19:42:18 -04:00
|
|
|
await self.assertBlockHash(200)
|
|
|
|
await self.assertBlockHash(201)
|
|
|
|
await self.assertBlockHash(202)
|