diff --git a/testground/benchmark/benchmark/peer.py b/testground/benchmark/benchmark/peer.py index d835c7936f..cacc84d9b6 100644 --- a/testground/benchmark/benchmark/peer.py +++ b/testground/benchmark/benchmark/peer.py @@ -3,19 +3,21 @@ from pathlib import Path from typing import List +from pydantic.json import pydantic_encoder + from .cli import ChainCommand from .context import Context from .network import get_data_ip from .topology import connect_all -from .types import GenesisAccount, PeerPacket +from .types import Balance, GenesisAccount, PeerPacket from .utils import eth_to_bech32, gen_account, patch_json, patch_toml +DEFAULT_DENOM = "basecro" VAL_ACCOUNT = "validator" -VAL_INITIAL_AMOUNT = "100000000000000000000basecro" -VAL_STAKED_AMOUNT = "10000000000000000000basecro" -ACC_INITIAL_AMOUNT = "10000000000000000000000000basecro" +VAL_INITIAL_AMOUNT = Balance(amount="100000000000000000000", denom=DEFAULT_DENOM) +VAL_STAKED_AMOUNT = Balance(amount="10000000000000000000", denom=DEFAULT_DENOM) +ACC_INITIAL_AMOUNT = Balance(amount="10000000000000000000000000", denom=DEFAULT_DENOM) MEMPOOL_SIZE = 10000 -DEFAULT_DENOM = "basecro" VALIDATOR_GROUP = "validators" FULLNODE_GROUP = "fullnodes" CONTAINER_CRONOSD_PATH = "/bin/cronosd" @@ -85,12 +87,13 @@ def init_node( ) accounts = [ GenesisAccount( - address=eth_to_bech32(val_acct.address), balance=VAL_INITIAL_AMOUNT + address=eth_to_bech32(val_acct.address), + coins=[VAL_INITIAL_AMOUNT], ), ] + [ GenesisAccount( address=eth_to_bech32(gen_account(global_seq, i + 1).address), - balance=ACC_INITIAL_AMOUNT, + coins=[ACC_INITIAL_AMOUNT], ) for i in range(num_accounts) ] @@ -115,7 +118,7 @@ def gen_genesis( ): for peer in peers: with tempfile.NamedTemporaryFile() as fp: - fp.write(json.dumps(peer.bulk_genesis_accounts()).encode()) + fp.write(json.dumps(peer.accounts, default=pydantic_encoder).encode()) fp.flush() cli( "genesis", @@ -171,7 +174,7 @@ def gentx(cli, **kwargs): "genesis", "add-genesis-account", VAL_ACCOUNT, - VAL_INITIAL_AMOUNT, + str(VAL_INITIAL_AMOUNT), **kwargs, ) with tempfile.TemporaryDirectory() as tmp: diff --git a/testground/benchmark/benchmark/types.py b/testground/benchmark/benchmark/types.py index 451ec5c0a9..14bc02e600 100644 --- a/testground/benchmark/benchmark/types.py +++ b/testground/benchmark/benchmark/types.py @@ -2,14 +2,20 @@ from pydantic import BaseModel -from .utils import bech32_to_eth, parse_coins +from .utils import bech32_to_eth -DEFAULT_DENOM = "basecro" + +class Balance(BaseModel): + amount: str + denom: str + + def __str__(self): + return f"{self.amount}{self.denom}" class GenesisAccount(BaseModel): address: str - balance: str + coins: List[Balance] @property def eth_address(self) -> str: @@ -22,15 +28,3 @@ class PeerPacket(BaseModel): peer_id: str accounts: List[GenesisAccount] gentx: Optional[dict] = None - - def bulk_genesis_accounts(self): - """ - convert accounts to the format required in `bulk-add-genesis-account` command - """ - return [ - { - "address": acct.address, - "coins": [parse_coins(acct.balance)], - } - for acct in self.accounts - ] diff --git a/testground/benchmark/benchmark/utils.py b/testground/benchmark/benchmark/utils.py index 241fd91c51..ace58baa7f 100644 --- a/testground/benchmark/benchmark/utils.py +++ b/testground/benchmark/benchmark/utils.py @@ -140,21 +140,3 @@ def gen_account(global_seq: int, index: int) -> Account: index 0 is reserved for validator account. """ return Account.from_key(((global_seq + 1) << 32 | index).to_bytes(32)) - - -def parse_coins(s: str) -> dict: - """ - split denom from coins string. - for example: `"1000.0stake,1000basetcro"` to - `[{'amount': '1000.0', 'denom': 'stake'}, {'amount': '1000', 'denom': 'basetcro'}]` - """ - coins = [] - for coin in s.split(","): - amount = "".join(takewhile(is_float, coin)) - denom = "".join(dropwhile(is_float, coin)) - coins.append({"amount": amount, "denom": denom.strip()}) - return coins - - -def is_float(s): - return str.isdigit(s) or s == "."