def test_enqueue(block_processor): # The state is updated after receiving a new block (and only if the block is not already known). # Let's start by adding some hashes to last_tips chain_monitor = ChainMonitor([Queue(), Queue()], block_processor, bitcoind_feed_params) chain_monitor.last_tips = [get_random_value_hex(32) for _ in range(5)] # Now we can try to update the state with an hash already seen and see how it doesn't work assert chain_monitor.enqueue(chain_monitor.last_tips[0]) is False # The state should be correctly updated with a new block hash, which should be added as last element of last_tips another_block_hash = get_random_value_hex(32) assert chain_monitor.enqueue(another_block_hash) is True assert chain_monitor.last_tips[-1] == another_block_hash
def test_monitor_chain_single_update(block_processor): # This test tests that if both threads try to add the same block to the queue, only the first one will make it chain_monitor = ChainMonitor([Queue(), Queue()], block_processor, bitcoind_feed_params) chain_monitor.polling_delta = 2 # We will create a block and wait for the polling thread. Then check the queues to see that the block hash has only # been added once. chain_monitor.monitor_chain() chain_monitor.activate() generate_blocks(1) assert len(chain_monitor.receiving_queues) == 2 queue0_block = chain_monitor.receiving_queues[0].get() queue1_block = chain_monitor.receiving_queues[1].get() assert queue0_block == queue1_block assert chain_monitor.receiving_queues[0].empty() assert chain_monitor.receiving_queues[1].empty() # The delta for polling is 2 secs, so let's wait and see time.sleep(2) assert chain_monitor.receiving_queues[0].empty() assert chain_monitor.receiving_queues[1].empty() # We can also force an update and see that it won't go through assert chain_monitor.enqueue(queue0_block) is False chain_monitor.terminate() # The zmq thread needs a block generation to release from the recv method. generate_blocks(1)