Skip to content

Commit e741745

Browse files
author
huangyi
committed
cleanup
1 parent bac77b1 commit e741745

File tree

3 files changed

+21
-42
lines changed

3 files changed

+21
-42
lines changed

testground/benchmark/benchmark/peer.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@
33
from pathlib import Path
44
from typing import List
55

6+
from pydantic.json import pydantic_encoder
7+
68
from .cli import ChainCommand
79
from .context import Context
810
from .network import get_data_ip
911
from .topology import connect_all
10-
from .types import GenesisAccount, PeerPacket
12+
from .types import Balance, GenesisAccount, PeerPacket
1113
from .utils import eth_to_bech32, gen_account, patch_json, patch_toml
1214

15+
DEFAULT_DENOM = "basecro"
1316
VAL_ACCOUNT = "validator"
14-
VAL_INITIAL_AMOUNT = "100000000000000000000basecro"
15-
VAL_STAKED_AMOUNT = "10000000000000000000basecro"
16-
ACC_INITIAL_AMOUNT = "10000000000000000000000000basecro"
17+
VAL_INITIAL_AMOUNT = Balance(amount="100000000000000000000", denom=DEFAULT_DENOM)
18+
VAL_STAKED_AMOUNT = Balance(amount="10000000000000000000", denom=DEFAULT_DENOM)
19+
ACC_INITIAL_AMOUNT = Balance(amount="10000000000000000000000000", denom=DEFAULT_DENOM)
1720
MEMPOOL_SIZE = 10000
18-
DEFAULT_DENOM = "basecro"
1921
VALIDATOR_GROUP = "validators"
2022
FULLNODE_GROUP = "fullnodes"
2123
CONTAINER_CRONOSD_PATH = "/bin/cronosd"
@@ -85,12 +87,13 @@ def init_node(
8587
)
8688
accounts = [
8789
GenesisAccount(
88-
address=eth_to_bech32(val_acct.address), balance=VAL_INITIAL_AMOUNT
90+
address=eth_to_bech32(val_acct.address),
91+
coins=[VAL_INITIAL_AMOUNT],
8992
),
9093
] + [
9194
GenesisAccount(
9295
address=eth_to_bech32(gen_account(global_seq, i + 1).address),
93-
balance=ACC_INITIAL_AMOUNT,
96+
coins=[ACC_INITIAL_AMOUNT],
9497
)
9598
for i in range(num_accounts)
9699
]
@@ -115,7 +118,7 @@ def gen_genesis(
115118
):
116119
for peer in peers:
117120
with tempfile.NamedTemporaryFile() as fp:
118-
fp.write(json.dumps(peer.bulk_genesis_accounts()).encode())
121+
fp.write(json.dumps(peer.accounts, default=pydantic_encoder).encode())
119122
fp.flush()
120123
cli(
121124
"genesis",
@@ -171,7 +174,7 @@ def gentx(cli, **kwargs):
171174
"genesis",
172175
"add-genesis-account",
173176
VAL_ACCOUNT,
174-
VAL_INITIAL_AMOUNT,
177+
str(VAL_INITIAL_AMOUNT),
175178
**kwargs,
176179
)
177180
with tempfile.TemporaryDirectory() as tmp:

testground/benchmark/benchmark/types.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@
22

33
from pydantic import BaseModel
44

5-
from .utils import bech32_to_eth, parse_coins
5+
from .utils import bech32_to_eth
66

7-
DEFAULT_DENOM = "basecro"
7+
8+
class Balance(BaseModel):
9+
amount: str
10+
denom: str
11+
12+
def __str__(self):
13+
return f"{self.amount}{self.denom}"
814

915

1016
class GenesisAccount(BaseModel):
1117
address: str
12-
balance: str
18+
coins: List[Balance]
1319

1420
@property
1521
def eth_address(self) -> str:
@@ -22,15 +28,3 @@ class PeerPacket(BaseModel):
2228
peer_id: str
2329
accounts: List[GenesisAccount]
2430
gentx: Optional[dict] = None
25-
26-
def bulk_genesis_accounts(self):
27-
"""
28-
convert accounts to the format required in `bulk-add-genesis-account` command
29-
"""
30-
return [
31-
{
32-
"address": acct.address,
33-
"coins": [parse_coins(acct.balance)],
34-
}
35-
for acct in self.accounts
36-
]

testground/benchmark/benchmark/utils.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -140,21 +140,3 @@ def gen_account(global_seq: int, index: int) -> Account:
140140
index 0 is reserved for validator account.
141141
"""
142142
return Account.from_key(((global_seq + 1) << 32 | index).to_bytes(32))
143-
144-
145-
def parse_coins(s: str) -> dict:
146-
"""
147-
split denom from coins string.
148-
for example: `"1000.0stake,1000basetcro"` to
149-
`[{'amount': '1000.0', 'denom': 'stake'}, {'amount': '1000', 'denom': 'basetcro'}]`
150-
"""
151-
coins = []
152-
for coin in s.split(","):
153-
amount = "".join(takewhile(is_float, coin))
154-
denom = "".join(dropwhile(is_float, coin))
155-
coins.append({"amount": amount, "denom": denom.strip()})
156-
return coins
157-
158-
159-
def is_float(s):
160-
return str.isdigit(s) or s == "."

0 commit comments

Comments
 (0)