From 556056c60d7578dbd8097da9efb103dd0b9717d3 Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Wed, 19 Jan 2022 10:10:54 -0500 Subject: [PATCH] tests --- .../test_blockchain_reorganization.py | 41 +++++++++---------- tests/integration/blockchain/test_network.py | 8 ++-- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/tests/integration/blockchain/test_blockchain_reorganization.py b/tests/integration/blockchain/test_blockchain_reorganization.py index f0959997b..0894679e0 100644 --- a/tests/integration/blockchain/test_blockchain_reorganization.py +++ b/tests/integration/blockchain/test_blockchain_reorganization.py @@ -9,7 +9,7 @@ class BlockchainReorganizationTests(CommandTestCase): VERBOSITY = logging.WARN async def assertBlockHash(self, height): - bp = self.conductor.spv_node.server.bp + bp = self.conductor.spv_node.writer def get_txids(): return [ @@ -29,15 +29,16 @@ class BlockchainReorganizationTests(CommandTestCase): self.assertListEqual(block_txs, list(txs.keys()), msg='leveldb/lbrycrd transactions are of order') async def test_reorg(self): - bp = self.conductor.spv_node.server.bp + bp = self.conductor.spv_node.writer bp.reorg_count_metric.set(0) # invalidate current block, move forward 2 height = 206 self.assertEqual(self.ledger.headers.height, height) await self.assertBlockHash(height) - await self.blockchain.invalidate_block((await self.ledger.headers.hash(206)).decode()) - await self.generate(2) - await self.ledger.on_header.where(lambda e: e.height == 207) + block_hash = (await self.ledger.headers.hash(206)).decode() + await self.blockchain.invalidate_block(block_hash) + await self.blockchain.generate(2) + await asyncio.wait_for(self.on_header(207), 3.0) self.assertEqual(self.ledger.headers.height, 207) await self.assertBlockHash(206) await self.assertBlockHash(207) @@ -45,15 +46,15 @@ class BlockchainReorganizationTests(CommandTestCase): # invalidate current block, move forward 3 await self.blockchain.invalidate_block((await self.ledger.headers.hash(206)).decode()) - await self.generate(3) - await self.ledger.on_header.where(lambda e: e.height == 208) + await self.blockchain.generate(3) + await asyncio.wait_for(self.on_header(208), 3.0) self.assertEqual(self.ledger.headers.height, 208) await self.assertBlockHash(206) await self.assertBlockHash(207) await self.assertBlockHash(208) self.assertEqual(2, bp.reorg_count_metric._samples()[0][2]) - await self.generate(3) - await self.ledger.on_header.where(lambda e: e.height == 211) + await self.blockchain.generate(3) + await asyncio.wait_for(self.on_header(211), 3.0) await self.assertBlockHash(209) await self.assertBlockHash(210) await self.assertBlockHash(211) @@ -61,8 +62,8 @@ class BlockchainReorganizationTests(CommandTestCase): 'still-valid', '1.0', file_path=self.create_upload_file(data=b'hi!') ) await self.ledger.wait(still_valid) - await self.generate(1) - await self.ledger.on_header.where(lambda e: e.height == 212) + await self.blockchain.generate(1) + await asyncio.wait_for(self.on_header(212), 1.0) claim_id = still_valid.outputs[0].claim_id c1 = (await self.resolve(f'still-valid#{claim_id}'))['claim_id'] c2 = (await self.resolve(f'still-valid#{claim_id[:2]}'))['claim_id'] @@ -70,8 +71,8 @@ class BlockchainReorganizationTests(CommandTestCase): self.assertTrue(c1 == c2 == c3) abandon_tx = await self.daemon.jsonrpc_stream_abandon(claim_id=claim_id) - await self.generate(1) - await self.ledger.on_header.where(lambda e: e.height == 213) + await self.blockchain.generate(1) + await asyncio.wait_for(self.on_header(213), 1.0) c1 = await self.resolve(f'still-valid#{still_valid.outputs[0].claim_id}') c2 = await self.daemon.jsonrpc_resolve([f'still-valid#{claim_id[:2]}']) c3 = await self.daemon.jsonrpc_resolve([f'still-valid']) @@ -113,10 +114,9 @@ class BlockchainReorganizationTests(CommandTestCase): # reorg the last block dropping our claim tx await self.blockchain.invalidate_block(invalidated_block_hash) await self.blockchain.clear_mempool() - await self.generate(2) - - # wait for the client to catch up and verify the reorg + await self.blockchain.generate(2) await asyncio.wait_for(self.on_header(209), 3.0) + await self.assertBlockHash(207) await self.assertBlockHash(208) await self.assertBlockHash(209) @@ -142,9 +142,8 @@ class BlockchainReorganizationTests(CommandTestCase): # broadcast the claim in a different block new_txid = await self.blockchain.sendrawtransaction(hexlify(broadcast_tx.raw).decode()) self.assertEqual(broadcast_tx.id, new_txid) - await self.generate(1) - # wait for the client to catch up + await self.blockchain.generate(1) await asyncio.wait_for(self.on_header(210), 1.0) # verify the claim is in the new block and that it is returned by claim_search @@ -192,7 +191,7 @@ class BlockchainReorganizationTests(CommandTestCase): # reorg the last block dropping our claim tx await self.blockchain.invalidate_block(invalidated_block_hash) await self.blockchain.clear_mempool() - await self.generate(2) + await self.blockchain.generate(2) # wait for the client to catch up and verify the reorg await asyncio.wait_for(self.on_header(209), 3.0) @@ -221,9 +220,7 @@ class BlockchainReorganizationTests(CommandTestCase): # broadcast the claim in a different block new_txid = await self.blockchain.sendrawtransaction(hexlify(broadcast_tx.raw).decode()) self.assertEqual(broadcast_tx.id, new_txid) - await self.generate(1) - - # wait for the client to catch up + await self.blockchain.generate(1) await asyncio.wait_for(self.on_header(210), 1.0) # verify the claim is in the new block and that it is returned by claim_search diff --git a/tests/integration/blockchain/test_network.py b/tests/integration/blockchain/test_network.py index cbb61eec9..98c6785d9 100644 --- a/tests/integration/blockchain/test_network.py +++ b/tests/integration/blockchain/test_network.py @@ -16,7 +16,7 @@ class NetworkTests(IntegrationTestCase): async def test_remote_height_updated_automagically(self): initial_height = self.ledger.network.remote_height - await self.generate(1) + await self.blockchain.generate(1) await self.ledger.network.on_header.first self.assertEqual(self.ledger.network.remote_height, initial_height + 1) @@ -85,7 +85,7 @@ class ReconnectTests(IntegrationTestCase): async def test_direct_sync(self): await self.ledger.stop() initial_height = self.ledger.local_height_including_downloaded_height - await self.generate(100) + await self.blockchain.generate(100) while self.conductor.spv_node.server.session_manager.notified_height < initial_height + 99: # off by 1 await asyncio.sleep(0.1) self.assertEqual(initial_height, self.ledger.local_height_including_downloaded_height) @@ -105,7 +105,7 @@ class ReconnectTests(IntegrationTestCase): # await self.ledger.resolve([], 'derp') # self.assertTrue(self.ledger.network.is_connected) await asyncio.wait_for(self.on_transaction_id(sendtxid), 10.0) # mempool - await self.generate(1) + await self.blockchain.generate(1) await self.on_transaction_id(sendtxid) # confirmed self.assertLess(self.ledger.network.client.response_time, 1) # response time properly set lower, we are fine @@ -123,7 +123,7 @@ class ReconnectTests(IntegrationTestCase): await self.ledger.network.get_transaction(sendtxid) # * goes to pick some water outside... * time passes by and another donation comes in sendtxid = await self.blockchain.send_to_address(address1, 42) - await self.generate(1) + await self.blockchain.generate(1) # (this is just so the test doesn't hang forever if it doesn't reconnect) if not self.ledger.network.is_connected: await asyncio.wait_for(self.ledger.network.on_connected.first, timeout=10.0)