lbry-sdk/tests/integration/blockchain/test_blockchain_reorganization.py

38 lines
1.5 KiB
Python
Raw Normal View History

2018-11-04 06:55:50 +01:00
import logging
2020-03-31 16:30:36 +02:00
from lbry.testcase import CommandTestCase
from lbry.wallet.server.prometheus import REORG_COUNT
2020-03-31 16:30:36 +02:00
class BlockchainReorganizationTests(CommandTestCase):
2018-11-04 06:55:50 +01:00
VERBOSITY = logging.WARN
async def assertBlockHash(self, height):
self.assertEqual(
2020-03-21 08:32:03 +01:00
(await self.ledger.headers.hash(height)).decode(),
await self.blockchain.get_block_hash(height)
)
2018-09-26 03:06:22 +02:00
async def test_reorg(self):
2020-03-31 16:30:36 +02:00
REORG_COUNT.set(0)
2019-06-18 23:50:41 +02:00
# invalidate current block, move forward 2
2020-03-31 16:30:36 +02:00
self.assertEqual(self.ledger.headers.height, 206)
await self.assertBlockHash(206)
await self.blockchain.invalidate_block((await self.ledger.headers.hash(206)).decode())
2019-06-18 23:50:41 +02:00
await self.blockchain.generate(2)
2020-03-31 16:30:36 +02:00
await self.ledger.on_header.where(lambda e: e.height == 207)
self.assertEqual(self.ledger.headers.height, 207)
await self.assertBlockHash(206)
await self.assertBlockHash(207)
self.assertEqual(1, REORG_COUNT._samples()[0][2])
2018-09-26 03:06:22 +02:00
2019-06-18 23:50:41 +02:00
# invalidate current block, move forward 3
2020-03-31 16:30:36 +02:00
await self.blockchain.invalidate_block((await self.ledger.headers.hash(206)).decode())
2019-06-18 23:50:41 +02:00
await self.blockchain.generate(3)
2020-03-31 16:30:36 +02:00
await self.ledger.on_header.where(lambda e: e.height == 208)
self.assertEqual(self.ledger.headers.height, 208)
await self.assertBlockHash(206)
await self.assertBlockHash(207)
await self.assertBlockHash(208)
self.assertEqual(2, REORG_COUNT._samples()[0][2])