Skip to content

Commit

Permalink
1. Standardising direct dispatch commands in respect of account creat…
Browse files Browse the repository at this point in the history
…ion.
  • Loading branch information
Mark A. Greenslade committed May 13, 2021
1 parent 7d6a49f commit 352a8f4
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 19 deletions.
8 changes: 3 additions & 5 deletions sh/scripts/arg_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,8 @@ def get_network_nodeset_by_node(args) -> typing.Tuple[Network, typing.List[Node]
# A specific node.
if isinstance(args.node, int):
try:
nodeset = [next((x for x in nodeset if x.index > args.node))]
except StopIteration:
raise ValueError("Invalid node index.")
else:
return network, nodeset
return network, [nodeset[args.node - 1]]
except IndexError:
return network, [random.choice(nodeset)]

raise ValueError("Invalid node index.")
46 changes: 42 additions & 4 deletions sh/scripts/dispatch_transfers_native.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

from stests import chain
from stests.core import factory
from stests.core.types.chain import Account
from stests.core.types.chain import AccountType
from stests.core.types.infra import Network
from stests.core.utils import args_validator
from stests.core.utils import cli as utils
from stests.core.utils import env
Expand Down Expand Up @@ -51,6 +53,18 @@
default=int(25e8)
)

# 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
)

# Map: account index <-> account.
_ACCOUNTS = dict()


def main(args):
"""Entry point.
Expand All @@ -62,28 +76,52 @@ def main(args):

network, nodeset = get_network_nodeset_by_node(args)
cp1 = network.faucet
cp2 = factory.create_account(network.name, AccountType.OTHER, index=1)

utils.log(f"... node : {nodeset[0].address if len(nodeset) == 1 else 'any'}")
utils.log(f"... amount / transfer : {args.amount}")
utils.log(f"... counter-party 1 : {cp1.account_key}")
utils.log(f"... counter-party 2 : {cp2.account_key}")

with Timer() as timer:
for idx in range(1, args.transfers + 1):
for deploy_idx in range(1, args.transfers + 1):
chain.set_transfer_native(
chain.DeployDispatchInfo(cp1, network, random.choice(nodeset)),
cp2,
_get_account_for_cp2(network, args.accounts, deploy_idx),
args.amount,
verbose=False,
)

utils.log(f"Dispatch complete")
utils.log(f"... total transfers : {args.transfers}")
utils.log(f"... total accounts : {min(args.accounts if args.accounts != 0 else args.transfers, args.transfers)}")
utils.log(f"... total amount : {args.amount * args.transfers}")
utils.log(f"... total time : {timer.elapsed:.2f} seconds")
utils.log(f"... dispatch rate : {(args.transfers / timer.elapsed):.2f} / second")


def _get_account_for_cp2(network: Network, accounts: int, deploy_idx: int) -> Account:
"""Returns counter-party 2 account.
"""
if accounts != 0:
account_idx = _get_account_idx_for_deploy(accounts, deploy_idx)
if not account_idx in _ACCOUNTS:
_ACCOUNTS[account_idx] = factory.create_account(network.name, AccountType.OTHER, index=account_idx)
return _ACCOUNTS[account_idx]
return factory.create_account(network.name, AccountType.OTHER, index=deploy_idx)


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


# Entry point.
if __name__ == '__main__':
main(ARGS.parse_args())
49 changes: 44 additions & 5 deletions sh/scripts/dispatch_transfers_wasm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

from stests import chain
from stests.core import factory
from stests.core.types.chain import Account
from stests.core.types.chain import AccountType
from stests.core.types.infra import Network
from stests.core.utils import args_validator
from stests.core.utils import cli as utils
from stests.core.utils import env
Expand Down Expand Up @@ -51,6 +53,18 @@
default=int(25e8)
)

# 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
)

# Map: account index <-> account.
_ACCOUNTS = dict()


def main(args):
"""Entry point.
Expand All @@ -60,30 +74,55 @@ def main(args):
"""
utils.log(f"Dispatching {args.transfers} wasm transfers:")

network, nodeset = get_network_nodeset_by_node(args)
network, nodeset = get_network_nodeset_by_node(args)
nodeset = sorted(nodeset, key=lambda n: n.index)
cp1 = network.faucet
cp2 = factory.create_account(network.name, AccountType.OTHER, index=2)

utils.log(f"... node : {nodeset[0].address if len(nodeset) == 1 else 'any'}")
utils.log(f"... amount / transfer : {args.amount}")
utils.log(f"... counter-party 1 : {cp1.account_key}")
utils.log(f"... counter-party 2 : {cp2.account_key}")

with Timer() as timer:
for idx in range(1, args.transfers + 1):
for deploy_idx in range(1, args.transfers + 1):
chain.set_transfer_wasm(
chain.DeployDispatchInfo(cp1, network, random.choice(nodeset)),
cp2,
_get_account_for_cp2(network, args.accounts, deploy_idx),
args.amount,
verbose=False,
)

utils.log(f"Dispatch complete")
utils.log(f"... total transfers : {args.transfers}")
utils.log(f"... total accounts : {min(args.accounts if args.accounts != 0 else args.transfers, args.transfers)}")
utils.log(f"... total amount : {args.amount * args.transfers}")
utils.log(f"... total time : {timer.elapsed:.2f} seconds")
utils.log(f"... dispatch rate : {(args.transfers / timer.elapsed):.2f} / second")


def _get_account_for_cp2(network: Network, accounts: int, deploy_idx: int) -> Account:
"""Returns counter-party 2 account.
"""
if accounts != 0:
account_idx = _get_account_idx_for_deploy(accounts, deploy_idx)
if not account_idx in _ACCOUNTS:
_ACCOUNTS[account_idx] = factory.create_account(network.name, AccountType.OTHER, index=account_idx)
return _ACCOUNTS[account_idx]
return factory.create_account(network.name, AccountType.OTHER, index=deploy_idx)


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


# Entry point.
if __name__ == '__main__':
main(ARGS.parse_args())
2 changes: 1 addition & 1 deletion stests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# ╚═════╝░░░░╚═╝░░░╚══════╝╚═════╝░░░░╚═╝░░░╚═════╝░

__title__ = "stests"
__version__ = "1.3.0"
__version__ = "1.3.1"
__author__ = "Casper Labs AG, Zug, Switzerland"
__license__ = "CasperLabs Open Source License (COSL)"
__copyright__ = "Copyright 2020 Casper Labs"
5 changes: 1 addition & 4 deletions stests/core/crypto/ecc_secp256k1.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
# Curve of interest.
CURVE = ecdsa.SECP256k1

# Use uncompressed public keys.
UNCOMPRESSED = "uncompressed"


def get_key_pair() -> typing.Tuple[bytes, bytes]:
"""Returns an SECP256K1 key pair, each key is a 32 byte array.
Expand Down Expand Up @@ -68,4 +65,4 @@ def _get_key_pair_from_sk(sk: ecdsa.SigningKey) -> typing.Tuple[bytes, bytes]:
"""
return sk.to_string(), \
sk.verifying_key.to_string(UNCOMPRESSED)
sk.verifying_key.to_string("compressed")

0 comments on commit 352a8f4

Please sign in to comment.