async def _setup_test_import_blocks(request, event_loop, new_blocks_db, new_blocks, simulate_importing_from_rpc, expect_blocks_to_import, node_min_gas_price=1): client_db, server_db, fresh_db = get_fresh_db(), get_fresh_db( ), get_fresh_db() node_1 = TestnetChain( server_db, TESTNET_GENESIS_PRIVATE_KEY.public_key.to_canonical_address()) node_1.min_gas_db.initialize_historical_minimum_gas_price_at_genesis( min_gas_price=node_min_gas_price, net_tpc_cap=100) node_2 = TestnetChain( client_db, TESTNET_GENESIS_PRIVATE_KEY.public_key.to_canonical_address()) node_2.min_gas_db.initialize_historical_minimum_gas_price_at_genesis( min_gas_price=node_min_gas_price, net_tpc_cap=100) if expect_blocks_to_import: expected_db = new_blocks_db else: expected_db = fresh_db async def waiting_function(client, server): SYNC_TIMEOUT = 100 async def wait_loop(): while ((client.chain_head_db.get_historical_root_hashes()[-1][1] != server.chain_head_db.get_historical_root_hashes()[-1][1]) or not client._new_blocks_to_import.empty() or not server._new_blocks_to_import.empty()): await asyncio.sleep(0.5) await asyncio.wait_for(wait_loop(), SYNC_TIMEOUT) def validation_function(base_db_1, base_db_2): ensure_blockchain_databases_identical(base_db_1, base_db_2) #In this case they are valid blocks so we expect them to match the database where the blocks came from ensure_blockchain_databases_identical(base_db_1, expected_db) await _test_sync_with_variable_sync_parameters( request, event_loop, client_db, server_db, validation_function=validation_function, waiting_function=waiting_function, blocks_to_import=new_blocks, blocks_to_import_from_rpc=simulate_importing_from_rpc)
async def test_import_block_with_high_gas(request, event_loop): simulate_importing_from_rpc = False # Blocks with timestamps before time.time() - ADDITIVE_SYNC_MODE_CUTOFF-TIME_BETWEEN_HEAD_HASH_SAVE should be rejected. new_tx_time = int(time.time() - ADDITIVE_SYNC_MODE_CUTOFF / 2) tx_list = [[GENESIS_PRIVATE_KEY, RECEIVER, 100, new_tx_time, 101]] new_blocks_db = get_fresh_db() add_transactions_to_blockchain_db(new_blocks_db, tx_list) expect_blocks_to_import = True node_new_blocks = MainnetChain( new_blocks_db, GENESIS_PRIVATE_KEY.public_key.to_canonical_address()) new_blocks = node_new_blocks.get_all_chronological_blocks_for_window( int((new_tx_time) / TIME_BETWEEN_HEAD_HASH_SAVE) * TIME_BETWEEN_HEAD_HASH_SAVE) await _setup_test_import_blocks( request, event_loop, new_blocks_db, new_blocks, simulate_importing_from_rpc, expect_blocks_to_import=expect_blocks_to_import, node_min_gas_price=100)
async def test_additive_sync_2(request, event_loop): client_db, server_db = get_random_long_time_blockchain_db( 10), get_fresh_db() node_1 = MainnetChain( server_db, GENESIS_PRIVATE_KEY.public_key.to_canonical_address()) node_1.chaindb.initialize_historical_minimum_gas_price_at_genesis( min_gas_price=1, net_tpc_cap=100, tpc=1) await _test_sync_with_variable_sync_parameters( request, event_loop, client_db, server_db, ensure_blockchain_databases_identical, ADDITIVE_SYNC_STAGE_ID)
async def test_consensus_match_sync_2(request, event_loop): server_db, client_db = get_fresh_db(), get_random_long_time_blockchain_db( 25) node_2 = MainnetChain( client_db, GENESIS_PRIVATE_KEY.public_key.to_canonical_address()) node_2.chaindb.initialize_historical_minimum_gas_price_at_genesis( min_gas_price=1, net_tpc_cap=100, tpc=1) await _test_sync_with_variable_sync_parameters( request, event_loop, client_db, server_db, ensure_blockchain_databases_identical, CONSENSUS_MATCH_SYNC_STAGE_ID)
async def _setup_alice_and_bob_factories(alice_db=None, bob_db=None): cancel_token = CancelToken( 'helios.get_directly_linked_peers_without_handshake') # # Alice # if alice_db is None: alice_db = get_fresh_db() alice_context = get_chain_context(alice_db, generate_random_private_key()) alice_factory_class = HLSPeerFactory alice_factory = alice_factory_class( privkey=ecies.generate_privkey(), context=alice_context, token=cancel_token, ) # # Bob # if bob_db is None: bob_db = get_fresh_db() bob_context = get_chain_context(bob_db, generate_random_private_key()) bob_factory_class = HLSPeerFactory bob_factory = bob_factory_class( privkey=ecies.generate_privkey(), context=bob_context, token=cancel_token, ) return alice_factory, bob_factory
def get_fresh_testnet_chaindb(): fresh_db = get_fresh_db() chaindb = FakeAsyncChainDB(fresh_db) return chaindb
def get_fresh_chain_context(privkey): base_db = get_fresh_db() return get_chain_context(base_db, privkey)
def db_fresh(): return get_fresh_db()