Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Sep 16, 2024
1 parent bac77b1 commit e741745
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 42 deletions.
21 changes: 12 additions & 9 deletions testground/benchmark/benchmark/peer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
]
Expand All @@ -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",
Expand Down Expand Up @@ -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:
Expand Down
24 changes: 9 additions & 15 deletions testground/benchmark/benchmark/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
]
18 changes: 0 additions & 18 deletions testground/benchmark/benchmark/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "."

0 comments on commit e741745

Please sign in to comment.