Skip to content

Commit 352a8f4

Browse files
author
Mark A. Greenslade
committed
1. Standardising direct dispatch commands in respect of account creation.
1 parent 7d6a49f commit 352a8f4

5 files changed

Lines changed: 91 additions & 19 deletions

File tree

sh/scripts/arg_utils.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,8 @@ def get_network_nodeset_by_node(args) -> typing.Tuple[Network, typing.List[Node]
7070
# A specific node.
7171
if isinstance(args.node, int):
7272
try:
73-
nodeset = [next((x for x in nodeset if x.index > args.node))]
74-
except StopIteration:
75-
raise ValueError("Invalid node index.")
76-
else:
77-
return network, nodeset
73+
return network, [nodeset[args.node - 1]]
74+
except IndexError:
75+
return network, [random.choice(nodeset)]
7876

7977
raise ValueError("Invalid node index.")

sh/scripts/dispatch_transfers_native.py

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
from stests import chain
55
from stests.core import factory
6+
from stests.core.types.chain import Account
67
from stests.core.types.chain import AccountType
8+
from stests.core.types.infra import Network
79
from stests.core.utils import args_validator
810
from stests.core.utils import cli as utils
911
from stests.core.utils import env
@@ -51,6 +53,18 @@
5153
default=int(25e8)
5254
)
5355

56+
# CLI argument: # transfers to dispatch.
57+
ARGS.add_argument(
58+
"--accounts",
59+
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.",
60+
dest="accounts",
61+
type=int,
62+
default=1
63+
)
64+
65+
# Map: account index <-> account.
66+
_ACCOUNTS = dict()
67+
5468

5569
def main(args):
5670
"""Entry point.
@@ -62,28 +76,52 @@ def main(args):
6276

6377
network, nodeset = get_network_nodeset_by_node(args)
6478
cp1 = network.faucet
65-
cp2 = factory.create_account(network.name, AccountType.OTHER, index=1)
6679

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

7284
with Timer() as timer:
73-
for idx in range(1, args.transfers + 1):
85+
for deploy_idx in range(1, args.transfers + 1):
7486
chain.set_transfer_native(
7587
chain.DeployDispatchInfo(cp1, network, random.choice(nodeset)),
76-
cp2,
88+
_get_account_for_cp2(network, args.accounts, deploy_idx),
7789
args.amount,
7890
verbose=False,
7991
)
8092

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

86100

101+
def _get_account_for_cp2(network: Network, accounts: int, deploy_idx: int) -> Account:
102+
"""Returns counter-party 2 account.
103+
104+
"""
105+
if accounts != 0:
106+
account_idx = _get_account_idx_for_deploy(accounts, deploy_idx)
107+
if not account_idx in _ACCOUNTS:
108+
_ACCOUNTS[account_idx] = factory.create_account(network.name, AccountType.OTHER, index=account_idx)
109+
return _ACCOUNTS[account_idx]
110+
return factory.create_account(network.name, AccountType.OTHER, index=deploy_idx)
111+
112+
113+
def _get_account_idx_for_deploy(accounts: int, deploy_idx: int) -> int:
114+
"""Returns account index to use for a particular transfer.
115+
116+
:param accounts: Number of accounts within batch.
117+
:param deploy_idx: Index of deploy within batch.
118+
:returns: Ordinal index of account used to dispatch deploy.
119+
120+
"""
121+
return deploy_idx if accounts == 0 else \
122+
deploy_idx % accounts or accounts
123+
124+
87125
# Entry point.
88126
if __name__ == '__main__':
89127
main(ARGS.parse_args())

sh/scripts/dispatch_transfers_wasm.py

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
from stests import chain
55
from stests.core import factory
6+
from stests.core.types.chain import Account
67
from stests.core.types.chain import AccountType
8+
from stests.core.types.infra import Network
79
from stests.core.utils import args_validator
810
from stests.core.utils import cli as utils
911
from stests.core.utils import env
@@ -51,6 +53,18 @@
5153
default=int(25e8)
5254
)
5355

56+
# CLI argument: # transfers to dispatch.
57+
ARGS.add_argument(
58+
"--accounts",
59+
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.",
60+
dest="accounts",
61+
type=int,
62+
default=1
63+
)
64+
65+
# Map: account index <-> account.
66+
_ACCOUNTS = dict()
67+
5468

5569
def main(args):
5670
"""Entry point.
@@ -60,30 +74,55 @@ def main(args):
6074
"""
6175
utils.log(f"Dispatching {args.transfers} wasm transfers:")
6276

63-
network, nodeset = get_network_nodeset_by_node(args)
77+
network, nodeset = get_network_nodeset_by_node(args)
78+
nodeset = sorted(nodeset, key=lambda n: n.index)
6479
cp1 = network.faucet
65-
cp2 = factory.create_account(network.name, AccountType.OTHER, index=2)
6680

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

7285
with Timer() as timer:
73-
for idx in range(1, args.transfers + 1):
86+
for deploy_idx in range(1, args.transfers + 1):
7487
chain.set_transfer_wasm(
7588
chain.DeployDispatchInfo(cp1, network, random.choice(nodeset)),
76-
cp2,
89+
_get_account_for_cp2(network, args.accounts, deploy_idx),
7790
args.amount,
7891
verbose=False,
7992
)
8093

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

86101

102+
def _get_account_for_cp2(network: Network, accounts: int, deploy_idx: int) -> Account:
103+
"""Returns counter-party 2 account.
104+
105+
"""
106+
if accounts != 0:
107+
account_idx = _get_account_idx_for_deploy(accounts, deploy_idx)
108+
if not account_idx in _ACCOUNTS:
109+
_ACCOUNTS[account_idx] = factory.create_account(network.name, AccountType.OTHER, index=account_idx)
110+
return _ACCOUNTS[account_idx]
111+
return factory.create_account(network.name, AccountType.OTHER, index=deploy_idx)
112+
113+
114+
def _get_account_idx_for_deploy(accounts: int, deploy_idx: int) -> int:
115+
"""Returns account index to use for a particular transfer.
116+
117+
:param accounts: Number of accounts within batch.
118+
:param deploy_idx: Index of deploy within batch.
119+
:returns: Ordinal index of account used to dispatch deploy.
120+
121+
"""
122+
return deploy_idx if accounts == 0 else \
123+
deploy_idx % accounts or accounts
124+
125+
87126
# Entry point.
88127
if __name__ == '__main__':
89128
main(ARGS.parse_args())

stests/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# ╚═════╝░░░░╚═╝░░░╚══════╝╚═════╝░░░░╚═╝░░░╚═════╝░
88

99
__title__ = "stests"
10-
__version__ = "1.3.0"
10+
__version__ = "1.3.1"
1111
__author__ = "Casper Labs AG, Zug, Switzerland"
1212
__license__ = "CasperLabs Open Source License (COSL)"
1313
__copyright__ = "Copyright 2020 Casper Labs"

stests/core/crypto/ecc_secp256k1.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
# Curve of interest.
1111
CURVE = ecdsa.SECP256k1
1212

13-
# Use uncompressed public keys.
14-
UNCOMPRESSED = "uncompressed"
15-
1613

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

0 commit comments

Comments
 (0)