Skip to content

Commit

Permalink
Problem: benchmark don't support erc20 tx (#1608)
Browse files Browse the repository at this point in the history
* Problem: benchmark don't support erc20 tx

Solution:
- gen erc20 contract and accounts in genesis
- add option to support multiple tx types

support execute erc20 transfer

update ethermint

fix tx

add gas limit

* cleanup

* accurate gas limit

---------

Co-authored-by: mmsqe <[email protected]>
  • Loading branch information
yihuang and mmsqe authored Sep 25, 2024
1 parent f179fa2 commit c0f7560
Show file tree
Hide file tree
Showing 14 changed files with 185 additions and 62 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,25 @@ jobs:
- name: 'instantiate integration test env'
if: steps.changed-files.outputs.only_changed == 'false'
run: nix-store -r $(nix-instantiate integration_tests/shell.nix)

testground-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: cachix/install-nix-action@v23
with:
nix_path: nixpkgs=channel:nixos-22.11
extra_nix_config: |
access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}
- uses: cachix/cachix-action@v12
with:
name: cronos
# github don't pass secrets for pull request from fork repos,
# in that case the push is disabled naturally.
signingKey: "${{ secrets.CACHIX_SIGNING_KEY }}"
- name: Run tests
run: |
cd testground/benchmark
nix develop -c pytest -vv -s
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ replace (
github.com/dgrijalva/jwt-go => github.com/golang-jwt/jwt/v4 v4.4.2
github.com/ethereum/go-ethereum => github.com/crypto-org-chain/go-ethereum v1.10.20-0.20240425065928-ebb09502e7a7
// develop
github.com/evmos/ethermint => github.com/crypto-org-chain/ethermint v0.6.1-0.20240923011104-7e3076202e6d
github.com/evmos/ethermint => github.com/crypto-org-chain/ethermint v0.6.1-0.20240925024103-f2a562ba9b9f
// Fix upstream GHSA-h395-qcrw-5vmq and GHSA-3vp4-m3rf-835h vulnerabilities.
// TODO Remove it: https://github.com/cosmos/cosmos-sdk/issues/10409
github.com/gin-gonic/gin => github.com/gin-gonic/gin v1.9.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,8 @@ github.com/crypto-org-chain/cosmos-sdk/store v0.0.0-20240916022730-3317ca17f0f1
github.com/crypto-org-chain/cosmos-sdk/store v0.0.0-20240916022730-3317ca17f0f1/go.mod h1:gjE3DZe4t/+VeIk6CmrouyqiuDbZ7QOVDDq3nLqBTpg=
github.com/crypto-org-chain/cosmos-sdk/x/tx v0.0.0-20240916022730-3317ca17f0f1 h1:yKiDUGCgeFiM5RLwS8IPp0BmdQANVJN7qDTCdFCPLyE=
github.com/crypto-org-chain/cosmos-sdk/x/tx v0.0.0-20240916022730-3317ca17f0f1/go.mod h1:RTiTs4hkXG6IvYGknvB8p79YgjYJdcbzLUOGJChsPnY=
github.com/crypto-org-chain/ethermint v0.6.1-0.20240923011104-7e3076202e6d h1:z9acuIXHLTtyZnBuz1NMd226UcdqjMfYX+HATjiZ/Ys=
github.com/crypto-org-chain/ethermint v0.6.1-0.20240923011104-7e3076202e6d/go.mod h1:D2lnc8ARuVmgc2/2IWla2Ky1o8/pjmyrnIt+d46Clco=
github.com/crypto-org-chain/ethermint v0.6.1-0.20240925024103-f2a562ba9b9f h1:Moucz7HfXoyNishzeMJOVbUdeLueOMAbNwnEN5M+iS8=
github.com/crypto-org-chain/ethermint v0.6.1-0.20240925024103-f2a562ba9b9f/go.mod h1:D2lnc8ARuVmgc2/2IWla2Ky1o8/pjmyrnIt+d46Clco=
github.com/crypto-org-chain/go-block-stm v0.0.0-20240919080136-6c49aef68716 h1:OvD5Rm0B6LHUJk6z858UgwdP72jU2DuUdXeclRyKpDI=
github.com/crypto-org-chain/go-block-stm v0.0.0-20240919080136-6c49aef68716/go.mod h1:iwQTX9xMX8NV9k3o2BiWXA0SswpsZrDk5q3gA7nWYiE=
github.com/crypto-org-chain/go-ethereum v1.10.20-0.20240425065928-ebb09502e7a7 h1:V43F3JFcqG4MUThf9W/DytnPblpR6CcaLBw2Wx6zTgE=
Expand Down
4 changes: 2 additions & 2 deletions gomod2nix.toml
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@ schema = 3
hash = "sha256-lE4G5FaRb3MVi9FFVn+WlwsSTOB4SbjmVboKyQ5yB0A="
replaced = "github.com/crypto-org-chain/go-ethereum"
[mod."github.com/evmos/ethermint"]
version = "v0.6.1-0.20240923011104-7e3076202e6d"
hash = "sha256-qduM/pFMVXFBcHyxMBzpM13rk8feN3lhNOdxRwD/1ec="
version = "v0.6.1-0.20240925024103-f2a562ba9b9f"
hash = "sha256-x4DWoXapGF4PmU78TchdrT9Km1hXWrrqV6bIry3J6Sk="
replaced = "github.com/crypto-org-chain/ethermint"
[mod."github.com/fatih/color"]
version = "v1.16.0"
Expand Down
52 changes: 52 additions & 0 deletions testground/benchmark/benchmark/erc20.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import eth_abi
from eth_hash.auto import keccak
from hexbytes import HexBytes

from .utils import eth_to_bech32

INITIAL_AMOUNT = 100000000000000000
CONTRACT_ADDRESS = "0x1" + "0" * 39


def genesis_accounts(contract_address, addresses) -> (list, list):
"""
return list of evm genesis accounts and auth genesis accounts
"""
amount = eth_abi.encode(["uint"], [INITIAL_AMOUNT])
balances = [
{
"key": keccak(eth_abi.encode(["address", "uint"], [addr, 0])).hex(),
"value": amount.hex(),
}
for addr in addresses
]
evm = [
{
"address": contract_address,
"code": bytecode,
"storage": [
{
# Total supply
"key": HexBytes(eth_abi.encode(["uint"], [2])).hex(),
"value": "0x1" + "0" * 63,
},
]
+ balances,
}
]

auth = [
{
"@type": "/ethermint.types.v1.EthAccount",
"base_account": {
"address": eth_to_bech32(contract_address),
"sequence": "1",
},
"code_hash": keccak(bytes.fromhex(bytecode)).hex(),
}
]

return evm, auth


bytecode = "608060405234801561001057600080fd5b50600436106100ea5760003560e01c806395d89b411161008c578063d3d78b9b11610066578063d3d78b9b146102cc578063dd62ed3e14610300578063e6711ef61461032e578063e978206414610336576100ea565b806395d89b411461026c578063a457c2d714610274578063a9059cbb146102a0576100ea565b806323b872dd116100c857806323b872dd146101c6578063313ce567146101fc578063395093511461021a57806370a0823114610246576100ea565b806306fdde03146100ef578063095ea7b31461016c57806318160ddd146101ac575b600080fd5b6100f7610362565b6040805160208082528351818301528351919283929083019185019080838360005b83811015610131578181015183820152602001610119565b50505050905090810190601f16801561015e5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6101986004803603604081101561018257600080fd5b506001600160a01b0381351690602001356103f8565b604080519115158252519081900360200190f35b6101b4610415565b60408051918252519081900360200190f35b610198600480360360608110156101dc57600080fd5b506001600160a01b0381358116916020810135909116906040013561041b565b6102046104a8565b6040805160ff9092168252519081900360200190f35b6101986004803603604081101561023057600080fd5b506001600160a01b0381351690602001356104b1565b6101b46004803603602081101561025c57600080fd5b50356001600160a01b0316610505565b6100f7610520565b6101986004803603604081101561028a57600080fd5b506001600160a01b038135169060200135610581565b610198600480360360408110156102b657600080fd5b506001600160a01b0381351690602001356105ef565b6102fe600480360360608110156102e257600080fd5b506001600160a01b038135169060208101359060400135610603565b005b6101b46004803603604081101561031657600080fd5b506001600160a01b03813581169160200135166106ad565b6101b46106d8565b6102fe6004803603604081101561034c57600080fd5b506001600160a01b0381351690602001356106dd565b60038054604080516020601f60026000196101006001881615020190951694909404938401819004810282018101909252828152606093909290918301828280156103ee5780601f106103c3576101008083540402835291602001916103ee565b820191906000526020600020905b8154815290600101906020018083116103d157829003601f168201915b5050505050905090565b600061040c61040561070b565b848461070f565b50600192915050565b60025490565b60006104288484846107fb565b61049e8461043461070b565b61049985604051806060016040528060288152602001610d33602891396001600160a01b038a1660009081526001602052604081209061047261070b565b6001600160a01b03168152602081019190915260400160002054919063ffffffff61096216565b61070f565b5060019392505050565b60055460ff1690565b600061040c6104be61070b565b8461049985600160006104cf61070b565b6001600160a01b03908116825260208083019390935260409182016000908120918c16815292529020549063ffffffff6109f916565b6001600160a01b031660009081526020819052604090205490565b60048054604080516020601f60026000196101006001881615020190951694909404938401819004810282018101909252828152606093909290918301828280156103ee5780601f106103c3576101008083540402835291602001916103ee565b600061040c61058e61070b565b8461049985604051806060016040528060258152602001610dc560259139600160006105b861070b565b6001600160a01b03908116825260208083019390935260409182016000908120918d1681529252902054919063ffffffff61096216565b600061040c6105fc61070b565b84846107fb565b81810182811015610654576040805162461bcd60e51b8152602060048201526016602482015275736166652d6d6174682d6164642d6f766572666c6f7760501b604482015290519081900360640190fd5b61065e3382610a5a565b604080516001600160a01b03861681526020810185905280820184905290517f937492d2511a2fbc9b51ea08825f1e252247d339dfd50904ebf4f4411f1d81369181900360600190a150505050565b6001600160a01b03918216600090815260016020908152604080832093909416825291909152205490565b602a90565b337389a7ef2f08b1c018d5cc88836249b84dd5392905146106fd57600080fd5b6107078282610b62565b5050565b3390565b6001600160a01b0383166107545760405162461bcd60e51b8152600401808060200182810382526024815260200180610da16024913960400191505060405180910390fd5b6001600160a01b0382166107995760405162461bcd60e51b8152600401808060200182810382526022815260200180610ceb6022913960400191505060405180910390fd5b6001600160a01b03808416600081815260016020908152604080832094871680845294825291829020859055815185815291517f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b9259281900390910190a3505050565b6001600160a01b0383166108405760405162461bcd60e51b8152600401808060200182810382526025815260200180610d7c6025913960400191505060405180910390fd5b6001600160a01b0382166108855760405162461bcd60e51b8152600401808060200182810382526023815260200180610ca66023913960400191505060405180910390fd5b610890838383610c5e565b6108d381604051806060016040528060268152602001610d0d602691396001600160a01b038616600090815260208190526040902054919063ffffffff61096216565b6001600160a01b038085166000908152602081905260408082209390935590841681522054610908908263ffffffff6109f916565b6001600160a01b038084166000818152602081815260409182902094909455805185815290519193928716927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef92918290030190a3505050565b600081848411156109f15760405162461bcd60e51b81526004018080602001828103825283818151815260200191508051906020019080838360005b838110156109b657818101518382015260200161099e565b50505050905090810190601f1680156109e35780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b505050900390565b600082820183811015610a53576040805162461bcd60e51b815260206004820152601b60248201527f536166654d6174683a206164646974696f6e206f766572666c6f770000000000604482015290519081900360640190fd5b9392505050565b6001600160a01b038216610a9f5760405162461bcd60e51b8152600401808060200182810382526021815260200180610d5b6021913960400191505060405180910390fd5b610aab82600083610c5e565b610aee81604051806060016040528060228152602001610cc9602291396001600160a01b038516600090815260208190526040902054919063ffffffff61096216565b6001600160a01b038316600090815260208190526040902055600254610b1a908263ffffffff610c6316565b6002556040805182815290516000916001600160a01b038516917fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9181900360200190a35050565b6001600160a01b038216610bbd576040805162461bcd60e51b815260206004820152601f60248201527f45524332303a206d696e7420746f20746865207a65726f206164647265737300604482015290519081900360640190fd5b610bc960008383610c5e565b600254610bdc908263ffffffff6109f916565b6002556001600160a01b038216600090815260208190526040902054610c08908263ffffffff6109f916565b6001600160a01b0383166000818152602081815260408083209490945583518581529351929391927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9281900390910190a35050565b505050565b6000610a5383836040518060400160405280601e81526020017f536166654d6174683a207375627472616374696f6e206f766572666c6f77000081525061096256fe45524332303a207472616e7366657220746f20746865207a65726f206164647265737345524332303a206275726e20616d6f756e7420657863656564732062616c616e636545524332303a20617070726f766520746f20746865207a65726f206164647265737345524332303a207472616e7366657220616d6f756e7420657863656564732062616c616e636545524332303a207472616e7366657220616d6f756e74206578636565647320616c6c6f77616e636545524332303a206275726e2066726f6d20746865207a65726f206164647265737345524332303a207472616e736665722066726f6d20746865207a65726f206164647265737345524332303a20617070726f76652066726f6d20746865207a65726f206164647265737345524332303a2064656372656173656420616c6c6f77616e63652062656c6f77207a65726fa26469706673582212203ad18e3c8894a8cb3aa50316961c792e96a3dfb497a816e09208ce0d7d83fe1364736f6c63430006060033" # noqa
24 changes: 20 additions & 4 deletions testground/benchmark/benchmark/peer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@
import jsonmerge
from pydantic.json import pydantic_encoder

from . import erc20
from .cli import ChainCommand
from .types import Balance, GenesisAccount, PeerPacket
from .utils import eth_to_bech32, gen_account, patch_json, patch_toml
from .utils import (
bech32_to_eth,
eth_to_bech32,
gen_account,
merge_genesis,
patch_genesis,
patch_toml,
)

DEFAULT_DENOM = "basecro"
VAL_ACCOUNT = "validator"
Expand Down Expand Up @@ -103,13 +111,21 @@ def gen_genesis(
collect_gen_tx(cli, peers, home=leader_home)
cli("genesis", "validate", home=leader_home)
print("genesis validated")
return patch_json(

evm_accounts, auth_accounts = erc20.genesis_accounts(
erc20.CONTRACT_ADDRESS, [bech32_to_eth(acct.address) for acct in accounts]
)
return patch_genesis(
leader_home / "config" / "genesis.json",
jsonmerge.merge(
merge_genesis(
{
"consensus": {"params": {"block": {"max_gas": "163000000"}}},
"app_state": {
"evm": {"params": {"evm_denom": "basecro"}},
"evm": {
"params": {"evm_denom": DEFAULT_DENOM},
"accounts": evm_accounts,
},
"auth": {"accounts": auth_accounts},
"feemarket": {"params": {"no_base_fee": True}},
},
},
Expand Down
11 changes: 9 additions & 2 deletions testground/benchmark/benchmark/stateless.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def validate_json(ctx, param, value):
@click.option("--fullnodes", default=7)
@click.option("--num-accounts", default=10)
@click.option("--num-txs", default=1000)
@click.option("--tx-type", default="simple-transfer")
@click.option("--config-patch", default="{}", callback=validate_json)
@click.option("--app-patch", default="{}", callback=validate_json)
@click.option("--genesis-patch", default="{}", callback=validate_json)
Expand All @@ -77,6 +78,7 @@ def _gen(
fullnodes: int = 7,
num_accounts: int = 10,
num_txs: int = 1000,
tx_type: str = "simple-transfer",
validator_generate_load: bool = True,
config_patch: dict = None,
app_patch: dict = None,
Expand Down Expand Up @@ -138,6 +140,7 @@ def _gen(
"fullnodes": fullnodes,
"num_accounts": num_accounts,
"num_txs": num_txs,
"tx_type": tx_type,
"validator-generate-load": validator_generate_load,
}
(outdir / "config.json").write_text(json.dumps(cfg))
Expand Down Expand Up @@ -203,6 +206,7 @@ def run(outdir: str, datadir: str, cronosd, global_seq):
@click.option("--nodes", default=10)
@click.option("--num-accounts", default=10)
@click.option("--num-txs", default=1000)
@click.option("--tx-type", default="simple-transfer")
@click.option("--node", type=int)
def gen_txs(**kwargs):
return _gen_txs(**kwargs)
Expand All @@ -219,13 +223,14 @@ def _gen_txs(
nodes: int = 10,
num_accounts: int = 10,
num_txs: int = 1000,
tx_type: str = "simple-transfer",
node: Optional[int] = None,
):
outdir = Path(outdir)

def job(global_seq):
print("generating", num_accounts * num_txs, "txs for node", global_seq)
txs = transaction.gen(global_seq, num_accounts, num_txs)
txs = transaction.gen(global_seq, num_accounts, num_txs, tx_type)
transaction.save(txs, outdir, global_seq)
print("saved", len(txs), "txs for node", global_seq)

Expand All @@ -245,7 +250,9 @@ def do_run(
print("loaded", len(txs), "txs")
else:
print("generating", cfg["num_accounts"] * cfg["num_txs"], "txs")
txs = transaction.gen(global_seq, cfg["num_accounts"], cfg["num_txs"])
txs = transaction.gen(
global_seq, cfg["num_accounts"], cfg["num_txs"], cfg["tx_type"]
)
else:
txs = []

Expand Down
9 changes: 9 additions & 0 deletions testground/benchmark/benchmark/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from .utils import merge_genesis


def test_merge_genesis():
g = merge_genesis(
{"app_state": {"auth": {"accounts": [1]}}},
{"app_state": {"auth": {"accounts": [2]}}},
)
assert g["app_state"]["auth"]["accounts"] == [1, 2]
32 changes: 28 additions & 4 deletions testground/benchmark/benchmark/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@
from pathlib import Path

import aiohttp
import eth_abi
import ujson

from .erc20 import CONTRACT_ADDRESS
from .utils import gen_account

GAS_PRICE = 1000000000
CHAIN_ID = 777
LOCAL_JSON_RPC = "http://localhost:8545"
CONNECTION_POOL_SIZE = 1024
TXS_DIR = "txs"
RECIPIENT = "0x1" + "0" * 39


def test_tx(nonce: int):
def simple_transfer_tx(nonce: int):
return {
"to": "0x0000000000000000000000000000000000000000",
"to": RECIPIENT,
"value": 1,
"nonce": nonce,
"gas": 21000,
Expand All @@ -24,12 +27,33 @@ def test_tx(nonce: int):
}


def gen(global_seq, num_accounts, num_txs) -> [str]:
def erc20_transfer_tx(nonce: int):
# data is erc20 transfer function call
data = "0xa9059cbb" + eth_abi.encode(["address", "uint256"], [RECIPIENT, 1]).hex()
return {
"to": CONTRACT_ADDRESS,
"value": 0,
"nonce": nonce,
"gas": 51630,
"gasPrice": GAS_PRICE,
"chainId": CHAIN_ID,
"data": data,
}


TX_TYPES = {
"simple-transfer": simple_transfer_tx,
"erc20-transfer": erc20_transfer_tx,
}


def gen(global_seq, num_accounts, num_txs, tx_type: str) -> [str]:
accounts = [gen_account(global_seq, i + 1) for i in range(num_accounts)]
txs = []
create_tx = TX_TYPES[tx_type]
for i in range(num_txs):
for acct in accounts:
txs.append(acct.sign_transaction(test_tx(i)).rawTransaction.hex())
txs.append(acct.sign_transaction(create_tx(i)).rawTransaction.hex())
if len(txs) % 1000 == 0:
print("generated", len(txs), "txs for node", global_seq)

Expand Down
22 changes: 20 additions & 2 deletions testground/benchmark/benchmark/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,27 @@ def patch_toml(path: Path, patch):
return doc


def patch_json(path: Path, patch):
_merger = jsonmerge.Merger(
{
"properties": {
"app_state": {
"properties": {
"auth": {"properties": {"accounts": {"mergeStrategy": "append"}}},
"evm": {"properties": {"accounts": {"mergeStrategy": "append"}}},
}
}
}
}
)


def merge_genesis(base, head):
return _merger.merge(base, head)


def patch_genesis(path: Path, patch):
doc = json.loads(path.read_text())
doc = jsonmerge.merge(doc, patch)
doc = merge_genesis(doc, patch)
path.write_text(json.dumps(doc))
return doc

Expand Down
45 changes: 2 additions & 43 deletions testground/benchmark/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,7 @@
};

outputs = { self, nixpkgs, flake-utils, poetry2nix }:
let
overrides = { lib, poetry2nix }: poetry2nix.overrides.withDefaults
(self: super:
let
buildSystems = {
pystarport = [ "poetry-core" ];
durations = [ "setuptools" ];
multitail2 = [ "setuptools" ];
docker = [ "hatchling" "hatch-vcs" ];
pyunormalize = [ "setuptools" ];
};
in
lib.mapAttrs
(attr: systems: super.${attr}.overridePythonAttrs
(old: {
nativeBuildInputs = (old.nativeBuildInputs or [ ]) ++ map (a: self.${a}) systems;
}))
buildSystems
);

src = nix-gitignore: nix-gitignore.gitignoreSourcePure [
"/*" # ignore all, then add whitelists
"!/benchmark/"
"!poetry.lock"
"!pyproject.toml"
] ./.;

benchmark = { lib, poetry2nix, python311, nix-gitignore }: poetry2nix.mkPoetryApplication {
projectDir = src nix-gitignore;
python = python311;
overrides = overrides { inherit lib poetry2nix; };
};

benchmark-env = { lib, poetry2nix, python311, nix-gitignore }: poetry2nix.mkPoetryEnv {
projectDir = src nix-gitignore;
python = python311;
overrides = overrides { inherit lib poetry2nix; };
};

in
(flake-utils.lib.eachDefaultSystem
flake-utils.lib.eachDefaultSystem
(system:
let
pkgs = import nixpkgs {
Expand Down Expand Up @@ -78,6 +38,5 @@
buildInputs = [ pkgs.benchmark-testcase-env ];
};
legacyPackages = pkgs;
})
);
});
}
Loading

0 comments on commit c0f7560

Please sign in to comment.