Skip to content

Commit

Permalink
feat(cluster): update address handling and funding logic
Browse files Browse the repository at this point in the history
- Create directory for faucet addresses data among test artifacts for
  better access during testnet cleanup.
- Add more test address records to `ClusterType`.
- Adjust funding logic to distribute funds evenly among new addresses.
- Refactor address creation and funding logic for better clarity.
  • Loading branch information
mkoura committed Oct 2, 2024
1 parent 103a0c1 commit 93aac1b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 16 deletions.
8 changes: 6 additions & 2 deletions cardano_node_tests/cluster_management/cluster_getter.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,12 @@ def _respin(self, start_cmd: str = "", stop_cmd: str = "") -> bool: # noqa: C90
fp_out.write(cluster_instance_id)
self.log(f"c{self.cluster_instance_num}: started cluster instance '{cluster_instance_id}'")

# Create dir for faucet addresses data
addr_data_dir = state_dir / common.ADDRS_DATA_DIRNAME
# Create dir for faucet addresses data among tests artifacts, so it can be accessed
# during testnet cleanup.
addr_data_dir = (
temptools.get_pytest_worker_tmp()
/ f"{common.ADDRS_DATA_DIRNAME}_ci{self.cluster_instance_num}"
)
addr_data_dir.mkdir(parents=True, exist_ok=True)

# Setup faucet addresses
Expand Down
59 changes: 45 additions & 14 deletions cardano_node_tests/utils/cluster_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ class ClusterType:

LOCAL: tp.Final[str] = "local"
TESTNET: tp.Final[str] = "testnet"
test_addr_records: tp.ClassVar[tp.Tuple[str, ...]] = ("user1",)
test_addr_records: tp.ClassVar[tp.Tuple[str, ...]] = (
"user1",
"user2",
"user3",
"user4",
"user5",
)

NODES: tp.ClassVar[tp.Set[str]] = set()

Expand Down Expand Up @@ -129,7 +135,7 @@ def create_addrs_data(
cluster_env = get_cluster_env()
instance_num = cluster_env.instance_num

# create new addresses
# Create new addresses
new_addrs_data: tp.Dict[str, tp.Dict[str, tp.Any]] = {}
for addr_name in self.test_addr_records:
addr_name_instance = f"{addr_name}_ci{instance_num}"
Expand All @@ -141,7 +147,7 @@ def create_addrs_data(
"payment": payment,
}

# create records for existing addresses
# Create records for existing addresses
faucet_addrs_data: tp.Dict[str, tp.Dict[str, tp.Any]] = {"faucet": {"payment": None}}
byron_dir = cluster_env.state_dir / "byron"
shelley_dir = cluster_env.state_dir / "shelley"
Expand All @@ -162,14 +168,15 @@ def create_addrs_data(
msg = "Faucet address file doesn't exist."
raise RuntimeError(msg)

# fund new addresses from faucet address
# Fund new addresses from faucet address
LOGGER.debug("Funding created addresses.")
to_fund = [d["payment"] for d in new_addrs_data.values()]
amount_per_address = 100_000_000_000_000 // len(self.test_addr_records)
faucet.fund_from_faucet(
*to_fund,
cluster_obj=cluster_obj,
faucet_data=faucet_addrs_data["faucet"],
amount=100_000_000_000_000,
amount=amount_per_address,
destination_dir=destination_dir,
force=True,
)
Expand Down Expand Up @@ -238,19 +245,43 @@ def create_addrs_data(
destination_dir: clusterlib.FileType = ".",
) -> tp.Dict[str, tp.Dict[str, tp.Any]]:
"""Create addresses and their keys for usage in tests."""
# Store record of the original faucet address
shelley_dir = get_cluster_env().state_dir / "shelley"
faucet_rec = clusterlib.AddressRecord(
address=clusterlib.read_address_from_file(shelley_dir / "faucet.addr"),
vkey_file=shelley_dir / "faucet.vkey",
skey_file=shelley_dir / "faucet.skey",
)
faucet_addrs_data: tp.Dict[str, tp.Dict[str, tp.Any]] = {
self.test_addr_records[1]: {"payment": faucet_rec}
}

addrs_data: tp.Dict[str, tp.Dict[str, tp.Any]] = {}
for addr_name in self.test_addr_records:
faucet_addr = {
"payment": clusterlib.AddressRecord(
address=clusterlib.read_address_from_file(shelley_dir / "faucet.addr"),
vkey_file=shelley_dir / "faucet.vkey",
skey_file=shelley_dir / "faucet.skey",
)
# Create new addresses
new_addrs_data: tp.Dict[str, tp.Dict[str, tp.Any]] = {}
for addr_name in self.test_addr_records[1:]:
payment = cluster_obj.g_address.gen_payment_addr_and_keys(
name=addr_name,
destination_dir=destination_dir,
)
new_addrs_data[addr_name] = {
"payment": payment,
}
addrs_data[addr_name] = faucet_addr

# Fund new addresses from faucet address
LOGGER.debug("Funding created addresses.")
to_fund = [d["payment"] for d in new_addrs_data.values()]
faucet_balance = cluster_obj.g_query.get_address_balance(address=faucet_rec.address)
amount_per_address = faucet_balance // len(self.test_addr_records)
faucet.fund_from_faucet(
*to_fund,
cluster_obj=cluster_obj,
faucet_data=faucet_addrs_data[self.test_addr_records[1]],
amount=amount_per_address,
destination_dir=destination_dir,
force=True,
)

addrs_data = {**new_addrs_data, **faucet_addrs_data}
return addrs_data


Expand Down

0 comments on commit 93aac1b

Please sign in to comment.