Skip to content

Commit

Permalink
1. Finer grained control of account space growth.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark A. Greenslade committed Feb 5, 2021
1 parent 1e93e01 commit 304cfed
Show file tree
Hide file tree
Showing 33 changed files with 299 additions and 232 deletions.
2 changes: 1 addition & 1 deletion sh/scripts/view_run_deploys.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def _render_table(args, network_id, data):
i.typeof.name,
i.status.name,
i.label_finalization_duration,
f"{i.consensus_era_id or '--'}::{i.consensus_round_id or '??'}",
f"{i.era_id or '--'}::{i.round_id or '??'}",
i.block_hash or "--"
], data)

Expand Down
8 changes: 4 additions & 4 deletions stests/core/factory/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ def create_block_statistics_on_addition(
block_hash: str,
block_hash_parent: str,
chain_name: str,
consensus_era_id: int,
deploy_cost_total: int,
deploy_count: int,
deploy_gas_price_avg: int,
era_id: int,
height: int,
is_switch_block: bool,
network: str,
Expand All @@ -177,10 +177,10 @@ def create_block_statistics_on_addition(
block_hash = block_hash,
block_hash_parent = block_hash_parent,
chain_name = chain_name,
consensus_era_id = consensus_era_id,
deploy_cost_total = deploy_cost_total,
deploy_count = deploy_count,
deploy_gas_price_avg = deploy_gas_price_avg,
era_id = era_id,
height = height,
is_switch_block = is_switch_block,
network = network,
Expand Down Expand Up @@ -211,19 +211,19 @@ def create_deploy_for_run(
associated_account=associated_account.account_key if associated_account else None,
associated_account_index=associated_account.index if associated_account else None,
block_hash=None,
consensus_era_id=None,
consensus_round_id=None,
deploy_cost=None,
deploy_hash=deploy_hash,
dispatch_attempts=dispatch_attempts,
dispatch_duration=dispatch_duration,
dispatch_node_index=node.index,
dispatch_timestamp=datetime.utcnow(),
era_id=None,
finalization_duration=None,
finalization_node_index=None,
finalization_timestamp=None,
network=ctx.network,
phase_index=ctx.phase_index,
round_id=None,
run_index=ctx.run_index,
run_type=ctx.run_type,
state_root_hash=None,
Expand Down
2 changes: 1 addition & 1 deletion stests/core/orchestration/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def on_run_end(ctx: ExecutionContext):
cache.orchestration.delete_locks(ctx)

# Cache can now be pruned.
if ctx.prune_on_completion == True:
if bool(ctx.prune_on_completion):
cache.orchestration.prune_on_run_completion(ctx)
cache.state.prune_on_run_completion(ctx)

Expand Down
6 changes: 3 additions & 3 deletions stests/core/types/chain/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ class BlockStatistics:
# Name of chain emitting block.
chain_name: str

# Consensus era identifier.
consensus_era_id: int

# Motes spent during block processing.
deploy_cost_total: int

Expand All @@ -86,6 +83,9 @@ class BlockStatistics:
# Average price of deploys.
deploy_gas_price_avg: int

# Consensus era identifier.
era_id: int

# Height of block within liner chain.
height: int

Expand Down
12 changes: 6 additions & 6 deletions stests/core/types/chain/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ class Deploy:
# Associated block hash in event of finalization.
block_hash: typing.Optional[str]

# Consensus era identifier.
consensus_era_id: int

# Consensus round identifier.
consensus_round_id: int

# Cost of deploy (in motes).
deploy_cost: typing.Optional[int]

Expand All @@ -51,6 +45,9 @@ class Deploy:
# Moment in time when deploy dispatched to CSPR network.
dispatch_timestamp: datetime

# Consensus era identifier.
era_id: int

# Time between dispatch & deploy finality.
finalization_duration: typing.Optional[float]

Expand Down Expand Up @@ -78,6 +75,9 @@ class Deploy:
# Numerical index to distinguish between multiple phase within a generator.
phase_index: typing.Optional[int]

# Consensus round identifier.
round_id: int

# Numerical index to distinguish between multiple runs of the same generator.
run_index: typing.Optional[int]

Expand Down
92 changes: 69 additions & 23 deletions stests/generators/utils/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
from stests.core.types.infra import Node
from stests.core.types.infra import Network
from stests.core.types.orchestration import ExecutionContext
from stests.generators.utils import constants
from stests.generators.utils.infra import get_network_node



# Queue to which messages will be dispatched.
_QUEUE = "orchestration.generators.accounts"

Expand Down Expand Up @@ -70,7 +72,8 @@ def do_transfer(

# Set amount to transfer (in the case of refunds).
if amount is None:
amount = get_user_account_balance(network, node, cp1) - chain.DEFAULT_TX_FEE
amount = get_account_balance(network, node, cp1) - chain.DEFAULT_TX_FEE
print(amount)

# Dispatch tx -> chain.
dispatch_info = chain.DeployDispatchInfo(cp1, network, node)
Expand Down Expand Up @@ -99,7 +102,7 @@ def do_transfer(
def do_transfer_fire_forget(
ctx: ExecutionContext,
cp1_index: int,
cp2_range: typing.List,
cp2_index: int,
amount: int,
transfer_type: DeployType,
):
Expand All @@ -114,29 +117,12 @@ def do_transfer_fire_forget(
"""
network, node = get_network_node(ctx)
cp1 = get_account(ctx, network, cp1_index)
cp2 = factory.create_account_for_run(ctx, cp2_index)
dispatch_info = chain.DeployDispatchInfo(cp1, network, node)
dispatch_fn = TFR_TYPE_TO_TFR_FN[transfer_type]
dispatch_fn(dispatch_info, cp2, amount)

# Unique account per transfer.
if ctx.args.accounts == 0:
for cp2_index in cp2_range:
dispatch_fn(
dispatch_info,
factory.create_account_for_run(ctx, cp2_index),
amount,
)
# Single account for all transfers.
else:
account = factory.create_account_for_run(ctx)
for cp2_index in cp2_range:
dispatch_fn(
dispatch_info,
account,
amount,
)

# Increment deploy counts.
cache.orchestration.increment_deploy_counts(ctx, len(cp2_range))
cache.orchestration.increment_deploy_counts(ctx, 1)


def get_account(ctx: ExecutionContext, network: Network, account_index: int) -> Account:
Expand All @@ -157,10 +143,70 @@ def get_account(ctx: ExecutionContext, network: Network, account_index: int) ->
return factory.create_account_for_run(ctx, account_index)


def get_user_account_balance(network: Network, node: Node, account: Account) -> int:
def get_account_balance(network: Network, node: Node, account: Account) -> int:
"""Returns either a faucet account or a user account.
"""
purse_uref = chain.get_account_main_purse_uref(network, node, account.account_key)

return chain.get_account_balance(network, node, purse_uref)


def get_account_idx_for_deploy(accounts: int, deploy_idx: int) -> int:
"""Returns account index to use for a particular transfer.
:param accounts: Number of accounts within batch.
:param deploy_idx: Index of deploy within batch.
:returns: Ordinal index of account used to dispatch deploy.
"""
return deploy_idx if accounts == 0 else \
deploy_idx % accounts or accounts


def get_account_idx_for_network_faucet() -> int:
"""Returns network specific faucet account index.
:returns: Ordinal index of account acting as network faucet.
"""
return constants.ACC_NETWORK_FAUCET


def get_account_idx_for_run_faucet(accounts: int, deploys: int) -> int:
"""Returns run specific faucet account index when dispatching a deploy batch.
:param accounts: Number of accounts within batch.
:param deploys: Number of deploys within batch.
:returns: Ordinal index of account acting as run faucet.
"""
return (deploys if accounts == 0 else accounts) + 1


def get_account_range(accounts: int, deploys: int) -> int:
"""Returns run specific faucet account index whcn dispatching a deploy batch.
:param accounts: Number of accounts within batch.
:param deploys: Number of deploys within batch.
:returns: Ordinal index of account acting as run faucet.
"""
return range(1, (deploys if accounts == 0 else accounts) + 1)


def get_account_transfer_count(accounts: int, account_idx: int, deploys: int) -> int:
"""Returns account index to use for a particular transfer.
"""
if accounts == 0:
return 1

return 10


def get_faucet_initial_balance(transfers, amount) -> int:
"""Returns initial faucet account CSPR balance.
"""
return (transfers * amount) + (((2 * transfers) + 1) * chain.DEFAULT_TX_FEE)
4 changes: 2 additions & 2 deletions stests/generators/utils/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ def get_argparser(description: str) -> argparse.ArgumentParser:
"--prune",
dest="prune_on_completion",
help="Flag indicating whether the orchestration engine will clean up cached data upon successful run completion.",
type=bool,
default=True,
type=int,
default=1,
)

return args
18 changes: 17 additions & 1 deletion stests/generators/utils/verification.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from stests.generators.utils.constants import ACC_RUN_USERS



def verify_deploy(ctx: ExecutionContext, block_hash: str, deploy_hash: str, expected_status=DeployStatus.ADDED) -> Deploy:
"""Verifies that a deploy is in a finalized state.
Expand All @@ -33,6 +32,23 @@ def verify_deploy_count(ctx: ExecutionContext, expected: int, aspect: ExecutionA
IgnoreableAssertionError(f"deploy count mismatch: actual={count}, expected={expected}")


def verify_account_balance(ctx: ExecutionContext, account_index: int, expected: int) -> Account:
"""Verifies that an account balance is as per expectation.
"""
account = cache.state.get_account_by_index(ctx, account_index)
network, node = get_network_node(ctx)
state_root_hash = chain.get_state_root_hash(network, node)

purse_uref = chain.get_account_main_purse_uref(network, node, account.account_key, state_root_hash)
assert purse_uref is not None, \
f"account {account_index} main purse uref could not be retrieved - probably on-chain account does not exist"

balance = chain.get_account_balance(network, node, purse_uref, state_root_hash)
assert balance == expected, \
f"account balance mismatch: account_index={account_index}, account_key={account.account_key}, expected={expected}, actual={balance}"


def verify_account_balance_on_transfer(
ctx: ExecutionContext,
node_id: NodeIdentifier,
Expand Down
25 changes: 16 additions & 9 deletions stests/generators/wg_100/args.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import argparse
import dataclasses

from stests import chain
from stests.generators.utils import constants
from stests.generators.utils.args import get_argparser


Expand All @@ -12,16 +10,15 @@ class Arguments:
"""Custom generator arguments passed along chain of execution.
"""
# Number of transfers to dispatch. Default=1000.
transfers: int
# Controls number of accounts to be generated during the run.
accounts: int

# Motes per transfer to transfer.
amount: int

@property
def faucet_initial_balance(self):
"""Initial faucet account CSPR balance."""
return (self.transfers * self.amount) + (((2 * self.transfers) + 1) * chain.DEFAULT_TX_FEE)
# Number of transfers to dispatch. Default=1000.
transfers: int


@classmethod
def create(cls, args: argparse.Namespace):
Expand All @@ -31,8 +28,9 @@ def create(cls, args: argparse.Namespace):
"""
return cls(
transfers='transfers' in args and args.transfers,
accounts='accounts' in args and args.accounts,
amount='amount' in args and args.amount,
transfers='transfers' in args and args.transfers,
)


Expand All @@ -48,6 +46,15 @@ def create(cls, args: argparse.Namespace):
default=100
)

# CLI argument: # transfers to dispatch.
ARGS.add_argument(
"--accounts",
help="Number of target accounts to create on the fly. If set to 0 then each target account is unique, otherwise a single target account is created.",
dest="accounts",
type=int,
default=1
)

# CLI argument: motes per transfer.
ARGS.add_argument(
"--amount",
Expand Down
3 changes: 2 additions & 1 deletion stests/generators/wg_100/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from stests.generators.wg_100 import p1s1_set_accounts
from stests.generators.wg_100 import p2s1_fund_faucet
from stests.generators.wg_100 import p2s2_fund_users
from stests.generators.wg_100 import p2s3_assert_balances
from stests.generators.wg_100 import p3s1_refund_users
from stests.generators.wg_100 import p3s2_refund_faucet

Expand All @@ -20,7 +21,7 @@
# Workflow phases/steps.
PHASES = (
(p1s1_set_accounts,),
(p2s1_fund_faucet, p2s2_fund_users,),
(p2s1_fund_faucet, p2s2_fund_users, p2s3_assert_balances,),
(p3s1_refund_users, p3s2_refund_faucet,),
)

Expand Down
Loading

0 comments on commit 304cfed

Please sign in to comment.