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
|
|
|
|
2019-06-19 01:42:18 +02:00
|
|
|
async def assertBlockHash(self, height):
|
|
|
|
self.assertEqual(
|
|
|
|
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):
|
2019-06-18 23:50:41 +02:00
|
|
|
# invalidate current block, move forward 2
|
2018-08-16 06:43:38 +02:00
|
|
|
self.assertEqual(self.ledger.headers.height, 200)
|
2019-06-19 01:42:18 +02:00
|
|
|
await self.assertBlockHash(200)
|
2019-06-18 23:50:41 +02:00
|
|
|
await self.blockchain.invalidate_block(self.ledger.headers.hash(200).decode())
|
|
|
|
await self.blockchain.generate(2)
|
|
|
|
await self.ledger.on_header.where(lambda e: e.height == 201)
|
2018-08-16 06:43:38 +02:00
|
|
|
self.assertEqual(self.ledger.headers.height, 201)
|
2019-06-19 01:42:18 +02:00
|
|
|
await self.assertBlockHash(200)
|
|
|
|
await self.assertBlockHash(201)
|
2018-09-26 03:06:22 +02:00
|
|
|
|
2019-06-18 23:50:41 +02:00
|
|
|
# invalidate current block, move forward 3
|
|
|
|
await self.blockchain.invalidate_block(self.ledger.headers.hash(200).decode())
|
|
|
|
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-19 01:42:18 +02:00
|
|
|
await self.assertBlockHash(200)
|
|
|
|
await self.assertBlockHash(201)
|
|
|
|
await self.assertBlockHash(202)
|