diff --git a/.gitignore b/.gitignore index fd7fd3436..d34f128f1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,10 @@ # Testing artifacts -.artifacts/* -.cli_coverage/* -.reports/* +.artifacts/ +.cli_coverage/ +.reports/ + +# Local temporary files +tmp/ # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/cardano_node_tests/tests/test_staking.py b/cardano_node_tests/tests/test_staking.py index 902bc954a..125682425 100644 --- a/cardano_node_tests/tests/test_staking.py +++ b/cardano_node_tests/tests/test_staking.py @@ -656,7 +656,7 @@ def test_no_reward_unmet_pledge( deposit=0, # no additional deposit, the pool is already registered ) - cluster.wait_for_new_epoch(3) + cluster.wait_for_new_epoch(3, padding_seconds=30) orig_owner_reward = cluster.get_stake_addr_info( pool_rec["reward"].address @@ -667,59 +667,52 @@ def test_no_reward_unmet_pledge( cluster.wait_for_new_epoch(3) - # check that NO new rewards were received by those delegating to the pool - assert ( - orig_user_reward - == cluster.get_stake_addr_info(pool_user.stake.address).reward_account_balance - ), "Received unexpected rewards" - - # check that pool owner is also NOT receiving rewards - assert ( - orig_owner_reward - == cluster.get_stake_addr_info(pool_rec["reward"].address).reward_account_balance - ), "Pool owner received unexpected rewards" - - # fund source (pledge) address - helpers.fund_from_faucet( - pool_owner, - cluster_obj=cluster, - faucet_data=cluster_manager.cache.addrs_data["user1"], - amount=150_000_000, - force=True, - ) - - # update the pool to original parameters by resubmitting the pool registration certificate - cluster.register_stake_pool( - pool_data=loaded_data, - pool_owners=[pool_owner], - vrf_vkey_file=pool_rec["vrf_key_pair"].vkey_file, - cold_key_pair=pool_rec["cold_key_pair"], - reward_account_vkey_file=pool_rec["reward"].vkey_file, - deposit=0, # no additional deposit, the pool is already registered - ) + with cluster_manager.needs_restart_after_failure(): + # check that NO new rewards were received by those delegating to the pool + assert ( + orig_user_reward + == cluster.get_stake_addr_info(pool_user.stake.address).reward_account_balance + ), "Received unexpected rewards" - cluster.wait_for_new_epoch(3) + # check that pool owner is also NOT receiving rewards + assert ( + orig_owner_reward + == cluster.get_stake_addr_info(pool_rec["reward"].address).reward_account_balance + ), "Pool owner received unexpected rewards" + + # fund source (pledge) address + helpers.fund_from_faucet( + pool_owner, + cluster_obj=cluster, + faucet_data=cluster_manager.cache.addrs_data["user1"], + amount=150_000_000, + force=True, + ) - updated_owner_reward = cluster.get_stake_addr_info( - pool_rec["reward"].address - ).reward_account_balance - updated_user_reward = cluster.get_stake_addr_info( - pool_user.stake.address - ).reward_account_balance + # update the pool to original parameters by resubmitting + # the pool registration certificate + cluster.register_stake_pool( + pool_data=loaded_data, + pool_owners=[pool_owner], + vrf_vkey_file=pool_rec["vrf_key_pair"].vkey_file, + cold_key_pair=pool_rec["cold_key_pair"], + reward_account_vkey_file=pool_rec["reward"].vkey_file, + deposit=0, # no additional deposit, the pool is already registered + ) - cluster.wait_for_new_epoch(3) + cluster.wait_for_new_epoch(4, padding_seconds=30) - # check that new rewards were received by those delegating to the pool - assert ( - updated_user_reward - < cluster.get_stake_addr_info(pool_user.stake.address).reward_account_balance - ), "New reward was not received by stake address" + # check that new rewards were received by those delegating to the pool + assert ( + orig_user_reward + < cluster.get_stake_addr_info(pool_user.stake.address).reward_account_balance + ), "New reward was not received by stake address" - # check that pool owner is also receiving rewards - assert ( - updated_owner_reward - < cluster.get_stake_addr_info(pool_rec["reward"].address).reward_account_balance - ), "New reward was not received by pool reward address" + # check that pool owner is also receiving rewards + assert ( + orig_owner_reward + < cluster.get_stake_addr_info(pool_rec["reward"].address).reward_account_balance + ), "New reward was not received by pool reward address" def test_no_reward_unmet_pledge2( self, @@ -782,7 +775,7 @@ def test_no_reward_unmet_pledge2( f"funds: {cluster.get_address_balance(pool_owner.payment.address)}" ) - cluster.wait_for_new_epoch(padding_seconds=30) + cluster.wait_for_new_epoch(3, padding_seconds=30) orig_owner_reward = cluster.get_stake_addr_info( pool_rec["reward"].address @@ -793,57 +786,62 @@ def test_no_reward_unmet_pledge2( cluster.wait_for_new_epoch(3) - # check that NO new rewards were received by those delegating to the pool - assert ( - orig_user_reward - == cluster.get_stake_addr_info(pool_user.stake.address).reward_account_balance - ), "Received unexpected rewards" - - # check that pool owner is also NOT receiving rewards - assert ( - orig_owner_reward - == cluster.get_stake_addr_info(pool_rec["reward"].address).reward_account_balance - ), "Pool owner received unexpected rewards" + with cluster_manager.needs_restart_after_failure(): + # check that NO new rewards were received by those delegating to the pool + assert ( + orig_user_reward + == cluster.get_stake_addr_info(pool_user.stake.address).reward_account_balance + ), "Received unexpected rewards" - # fund user address so it has enough funds for fees etc. - helpers.fund_from_faucet( - pool_user, - cluster_obj=cluster, - faucet_data=cluster_manager.cache.addrs_data["user1"], - amount=150_000_000, - force=True, - ) + # check that pool owner is also NOT receiving rewards + assert ( + orig_owner_reward + == cluster.get_stake_addr_info(pool_rec["reward"].address).reward_account_balance + ), "Pool owner received unexpected rewards" + + # fund user address so it has enough funds for fees etc. + helpers.fund_from_faucet( + pool_user, + cluster_obj=cluster, + faucet_data=cluster_manager.cache.addrs_data["user1"], + amount=150_000_000, + force=True, + ) - # return pledge - destinations = [ - clusterlib.TxOut(address=pool_owner.payment.address, amount=pledge_amount + 100_000_000) - ] - tx_files = clusterlib.TxFiles(signing_key_files=[pool_user.payment.skey_file]) - cluster.send_funds( - src_address=pool_user.payment.address, - destinations=destinations, - tx_files=tx_files, - ) - cluster.wait_for_new_block(new_blocks=2) + # return pledge + destinations = [ + clusterlib.TxOut( + address=pool_owner.payment.address, amount=pledge_amount + 100_000_000 + ) + ] + tx_files = clusterlib.TxFiles(signing_key_files=[pool_user.payment.skey_file]) + cluster.send_funds( + src_address=pool_user.payment.address, + destinations=destinations, + tx_files=tx_files, + ) + cluster.wait_for_new_block(new_blocks=2) - assert cluster.get_address_balance(pool_owner.payment.address) >= loaded_data.pool_pledge, ( - f"Funds still low - pledge: {loaded_data.pool_pledge}, " - f"funds: {cluster.get_address_balance(pool_owner.payment.address)}" - ) + assert ( + cluster.get_address_balance(pool_owner.payment.address) >= loaded_data.pool_pledge + ), ( + f"Funds still low - pledge: {loaded_data.pool_pledge}, " + f"funds: {cluster.get_address_balance(pool_owner.payment.address)}" + ) - cluster.wait_for_new_epoch(3) + cluster.wait_for_new_epoch(5, padding_seconds=30) - # check that new rewards were received by those delegating to the pool - assert ( - orig_user_reward - < cluster.get_stake_addr_info(pool_user.stake.address).reward_account_balance - ), "New reward was not received by stake address" + # check that new rewards were received by those delegating to the pool + assert ( + orig_user_reward + < cluster.get_stake_addr_info(pool_user.stake.address).reward_account_balance + ), "New reward was not received by stake address" - # check that pool owner is also receiving rewards - assert ( - orig_owner_reward - < cluster.get_stake_addr_info(pool_rec["reward"].address).reward_account_balance - ), "New reward was not received by pool reward address" + # check that pool owner is also receiving rewards + assert ( + orig_owner_reward + < cluster.get_stake_addr_info(pool_rec["reward"].address).reward_account_balance + ), "New reward was not received by pool reward address" def test_no_reward_unregistered_stake_addr( self, @@ -895,89 +893,96 @@ def test_no_reward_unregistered_stake_addr( ) cluster.wait_for_new_block(new_blocks=2) - # check that the key deposit was returned - assert ( - cluster.get_address_balance(pool_owner.payment.address) - == src_init_balance - tx_raw_deregister_output.fee + cluster.get_key_deposit() - ), f"Incorrect balance for source address `{pool_owner.payment.address}`" + with cluster_manager.needs_restart_after_failure(): + # check that the key deposit was returned + assert ( + cluster.get_address_balance(pool_owner.payment.address) + == src_init_balance - tx_raw_deregister_output.fee + cluster.get_key_deposit() + ), f"Incorrect balance for source address `{pool_owner.payment.address}`" - cluster.wait_for_new_epoch(3) + cluster.wait_for_new_epoch(3, padding_seconds=30) - # check that the stake address is no longer delegated - assert not cluster.get_stake_addr_info( - pool_owner.stake.address - ), "Stake address still delegated" + # check that the stake address is no longer delegated + assert not cluster.get_stake_addr_info( + pool_owner.stake.address + ), "Stake address still delegated" - orig_owner_reward = cluster.get_stake_addr_info( - pool_rec["reward"].address - ).reward_account_balance - orig_user_reward = cluster.get_stake_addr_info( - pool_user.stake.address - ).reward_account_balance - - cluster.wait_for_new_epoch(3) + orig_owner_reward = cluster.get_stake_addr_info( + pool_rec["reward"].address + ).reward_account_balance + orig_user_reward = cluster.get_stake_addr_info( + pool_user.stake.address + ).reward_account_balance - # check that NO new rewards were received by those delegating to the pool - assert ( - orig_user_reward - == cluster.get_stake_addr_info(pool_user.stake.address).reward_account_balance - ), "Received unexpected rewards" + cluster.wait_for_new_epoch(3) - # check that pool owner is also NOT receiving rewards - assert ( - orig_owner_reward - == cluster.get_stake_addr_info(pool_rec["reward"].address).reward_account_balance - ), "Pool owner received unexpected rewards" + # check that NO new rewards were received by those delegating to the pool + assert ( + orig_user_reward + == cluster.get_stake_addr_info(pool_user.stake.address).reward_account_balance + ), "Received unexpected rewards" - # fund source address - helpers.fund_from_faucet( - pool_owner, - cluster_obj=cluster, - faucet_data=cluster_manager.cache.addrs_data["user1"], - amount=150_000_000, - force=True, - ) + # check that pool owner is also NOT receiving rewards + assert ( + orig_owner_reward + == cluster.get_stake_addr_info(pool_rec["reward"].address).reward_account_balance + ), "Pool owner received unexpected rewards" + + # fund source address + helpers.fund_from_faucet( + pool_owner, + cluster_obj=cluster, + faucet_data=cluster_manager.cache.addrs_data["user1"], + amount=150_000_000, + force=True, + ) - src_updated_balance = cluster.get_address_balance(pool_owner.payment.address) + src_updated_balance = cluster.get_address_balance(pool_owner.payment.address) - # reregister stake address and delegate it to pool - tx_files = clusterlib.TxFiles( - certificate_files=[ - pool_rec["stake_addr_registration_cert"], - pool_rec["stake_addr_delegation_cert"], - ], - signing_key_files=[pool_owner.payment.skey_file, pool_owner.stake.skey_file], - ) - tx_raw_output = cluster.send_tx(src_address=pool_owner.payment.address, tx_files=tx_files) - cluster.wait_for_new_block(new_blocks=2) + # reregister stake address and delegate it to pool + tx_files = clusterlib.TxFiles( + certificate_files=[ + pool_rec["stake_addr_registration_cert"], + pool_rec["stake_addr_delegation_cert"], + ], + signing_key_files=[pool_owner.payment.skey_file, pool_owner.stake.skey_file], + ) + tx_raw_output = cluster.send_tx( + src_address=pool_owner.payment.address, tx_files=tx_files + ) + cluster.wait_for_new_block(new_blocks=2) - # check that the balance for source address was correctly updated - assert ( - cluster.get_address_balance(pool_owner.payment.address) - == src_updated_balance - tx_raw_output.fee - cluster.get_key_deposit() - ), f"Incorrect balance for source address `{pool_owner.payment.address}`" + # check that the balance for source address was correctly updated + assert ( + cluster.get_address_balance(pool_owner.payment.address) + == src_updated_balance - tx_raw_output.fee - cluster.get_key_deposit() + ), f"Incorrect balance for source address `{pool_owner.payment.address}`" - cluster.wait_for_new_epoch(4) + cluster.wait_for_new_epoch(4, padding_seconds=30) - # check that the stake address was delegated - stake_addr_info = cluster.get_stake_addr_info(pool_owner.stake.address) - assert stake_addr_info.delegation, f"Stake address was not delegated yet: {stake_addr_info}" + # check that the stake address was delegated + stake_addr_info = cluster.get_stake_addr_info(pool_owner.stake.address) + assert ( + stake_addr_info.delegation + ), f"Stake address was not delegated yet: {stake_addr_info}" - node_cold = cluster_manager.cache.addrs_data[pool_name]["cold_key_pair"] - stake_pool_id = cluster.get_stake_pool_id(node_cold.vkey_file) - assert stake_pool_id == stake_addr_info.delegation, "Stake address delegated to wrong pool" + node_cold = cluster_manager.cache.addrs_data[pool_name]["cold_key_pair"] + stake_pool_id = cluster.get_stake_pool_id(node_cold.vkey_file) + assert ( + stake_pool_id == stake_addr_info.delegation + ), "Stake address delegated to wrong pool" - # check that new rewards were received by those delegating to the pool - assert ( - orig_user_reward - < cluster.get_stake_addr_info(pool_user.stake.address).reward_account_balance - ), "New reward was not received by stake address" + # check that new rewards were received by those delegating to the pool + assert ( + orig_user_reward + < cluster.get_stake_addr_info(pool_user.stake.address).reward_account_balance + ), "New reward was not received by stake address" - # check that pool owner is also receiving rewards - assert ( - orig_user_reward - < cluster.get_stake_addr_info(pool_rec["reward"].address).reward_account_balance - ), "New reward was not received by pool reward address" + # check that pool owner is also receiving rewards + assert ( + orig_user_reward + < cluster.get_stake_addr_info(pool_rec["reward"].address).reward_account_balance + ), "New reward was not received by pool reward address" def test_no_reward_unregistered_reward_addr( self, @@ -1032,92 +1037,99 @@ def test_no_reward_unregistered_reward_addr( ) cluster.wait_for_new_block(new_blocks=2) - # check that the key deposit was returned - assert ( - cluster.get_address_balance(pool_reward.payment.address) - == src_init_balance - tx_raw_deregister_output.fee + cluster.get_key_deposit() - ), f"Incorrect balance for source address `{pool_reward.payment.address}`" + with cluster_manager.needs_restart_after_failure(): + # check that the key deposit was returned + assert ( + cluster.get_address_balance(pool_reward.payment.address) + == src_init_balance - tx_raw_deregister_output.fee + cluster.get_key_deposit() + ), f"Incorrect balance for source address `{pool_reward.payment.address}`" - cluster.wait_for_new_epoch(3) + cluster.wait_for_new_epoch(3, padding_seconds=30) - # check that the reward address is no longer delegated - assert not cluster.get_stake_addr_info( - pool_reward.stake.address - ), "Stake address still delegated" + # check that the reward address is no longer delegated + assert not cluster.get_stake_addr_info( + pool_reward.stake.address + ), "Stake address still delegated" - orig_pool_reward = cluster.get_stake_addr_info( - pool_reward.stake.address - ).reward_account_balance - orig_user_reward = cluster.get_stake_addr_info( - pool_user.stake.address - ).reward_account_balance + orig_pool_reward = cluster.get_stake_addr_info( + pool_reward.stake.address + ).reward_account_balance + orig_user_reward = cluster.get_stake_addr_info( + pool_user.stake.address + ).reward_account_balance - cluster.wait_for_new_epoch(3) + cluster.wait_for_new_epoch(3) - # check that NO new rewards were received by pool owner - assert ( - orig_pool_reward - == cluster.get_stake_addr_info(pool_reward.stake.address).reward_account_balance - ), "Pool owner received unexpected rewards" + # check that NO new rewards were received by pool owner + assert ( + orig_pool_reward + == cluster.get_stake_addr_info(pool_reward.stake.address).reward_account_balance + ), "Pool owner received unexpected rewards" - # check that new rewards were received by those delegating to the pool - assert ( - orig_user_reward - < cluster.get_stake_addr_info(pool_user.stake.address).reward_account_balance - ), "New reward was not received by stake address" + # check that new rewards were received by those delegating to the pool + assert ( + orig_user_reward + < cluster.get_stake_addr_info(pool_user.stake.address).reward_account_balance + ), "New reward was not received by stake address" - # fund source address - helpers.fund_from_faucet( - pool_reward, - cluster_obj=cluster, - faucet_data=cluster_manager.cache.addrs_data["user1"], - amount=150_000_000, - force=True, - ) + # fund source address + helpers.fund_from_faucet( + pool_reward, + cluster_obj=cluster, + faucet_data=cluster_manager.cache.addrs_data["user1"], + amount=150_000_000, + force=True, + ) - src_updated_balance = cluster.get_address_balance(pool_reward.payment.address) + src_updated_balance = cluster.get_address_balance(pool_reward.payment.address) - node_cold = cluster_manager.cache.addrs_data[pool_name]["cold_key_pair"] - stake_pool_id = cluster.get_stake_pool_id(node_cold.vkey_file) + node_cold = cluster_manager.cache.addrs_data[pool_name]["cold_key_pair"] + stake_pool_id = cluster.get_stake_pool_id(node_cold.vkey_file) - # reregister stake address and delegate it to pool - reward_addr_deleg_cert_file = cluster.gen_stake_addr_delegation_cert( - addr_name=f"addr0_{temp_template}", - stake_vkey_file=pool_reward.stake.vkey_file, - cold_vkey_file=node_cold.vkey_file, - ) - tx_files = clusterlib.TxFiles( - certificate_files=[ - pool_rec["reward_addr_registration_cert"], - reward_addr_deleg_cert_file, - ], - signing_key_files=[pool_reward.payment.skey_file, pool_reward.stake.skey_file], - ) - tx_raw_output = cluster.send_tx(src_address=pool_reward.payment.address, tx_files=tx_files) - cluster.wait_for_new_block(new_blocks=2) + # reregister stake address and delegate it to pool + reward_addr_deleg_cert_file = cluster.gen_stake_addr_delegation_cert( + addr_name=f"addr0_{temp_template}", + stake_vkey_file=pool_reward.stake.vkey_file, + cold_vkey_file=node_cold.vkey_file, + ) + tx_files = clusterlib.TxFiles( + certificate_files=[ + pool_rec["reward_addr_registration_cert"], + reward_addr_deleg_cert_file, + ], + signing_key_files=[pool_reward.payment.skey_file, pool_reward.stake.skey_file], + ) + tx_raw_output = cluster.send_tx( + src_address=pool_reward.payment.address, tx_files=tx_files + ) + cluster.wait_for_new_block(new_blocks=2) - # check that the balance for source address was correctly updated - assert ( - cluster.get_address_balance(pool_reward.payment.address) - == src_updated_balance - tx_raw_output.fee - cluster.get_key_deposit() - ), f"Incorrect balance for source address `{pool_reward.payment.address}`" + # check that the balance for source address was correctly updated + assert ( + cluster.get_address_balance(pool_reward.payment.address) + == src_updated_balance - tx_raw_output.fee - cluster.get_key_deposit() + ), f"Incorrect balance for source address `{pool_reward.payment.address}`" - cluster.wait_for_new_epoch(4) + cluster.wait_for_new_epoch(4, padding_seconds=30) - # check that the stake address was delegated - stake_addr_info = cluster.get_stake_addr_info(pool_reward.stake.address) - assert stake_addr_info.delegation, f"Stake address was not delegated yet: {stake_addr_info}" + # check that the stake address was delegated + stake_addr_info = cluster.get_stake_addr_info(pool_reward.stake.address) + assert ( + stake_addr_info.delegation + ), f"Stake address was not delegated yet: {stake_addr_info}" - assert stake_pool_id == stake_addr_info.delegation, "Stake address delegated to wrong pool" + assert ( + stake_pool_id == stake_addr_info.delegation + ), "Stake address delegated to wrong pool" - # check that new rewards were received by those delegating to the pool - assert ( - orig_user_reward - < cluster.get_stake_addr_info(pool_user.stake.address).reward_account_balance - ), "New reward was not received by stake address" + # check that new rewards were received by those delegating to the pool + assert ( + orig_user_reward + < cluster.get_stake_addr_info(pool_user.stake.address).reward_account_balance + ), "New reward was not received by stake address" - # check that pool owner is also receiving rewards - assert ( - orig_user_reward - < cluster.get_stake_addr_info(pool_rec["reward"].address).reward_account_balance - ), "New reward was not received by pool reward address" + # check that pool owner is also receiving rewards + assert ( + orig_user_reward + < cluster.get_stake_addr_info(pool_rec["reward"].address).reward_account_balance + ), "New reward was not received by pool reward address" diff --git a/cardano_node_tests/utils/parallel_run.py b/cardano_node_tests/utils/parallel_run.py index 38d3beea9..55769fa46 100644 --- a/cardano_node_tests/utils/parallel_run.py +++ b/cardano_node_tests/utils/parallel_run.py @@ -1,9 +1,11 @@ +import contextlib import dataclasses import datetime import logging import os import random from pathlib import Path +from typing import Generator from typing import Optional from _pytest.fixtures import FixtureRequest @@ -144,6 +146,15 @@ def set_needs_restart(self) -> None: self._log("called `_set_needs_restart`") open(self.lock_dir / f"{RESTART_NEEDED_GLOB}_{self.worker_id}", "a").close() + @contextlib.contextmanager + def needs_restart_after_failure(self) -> Generator: + """Indicate that the cluster needs restart if command failed - context manager.""" + try: + yield + except Exception: + self.set_needs_restart() + raise + def _on_marked_test_stop(self) -> None: """Perform actions after marked tests are finished.""" self._log("in `_on_marked_test_stop`")