Skip to content

Commit

Permalink
Problem: config patch don't support list (crypto-org-chain#1591)
Browse files Browse the repository at this point in the history
* Problem: config patch don't support list

Solution:
- change to jsonmerge package

* fix patch genesis
  • Loading branch information
yihuang authored Sep 19, 2024
1 parent 73c19da commit b330f22
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 44 deletions.
60 changes: 37 additions & 23 deletions testground/benchmark/benchmark/peer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pathlib import Path
from typing import List

import jsonmerge
from pydantic.json import pydantic_encoder

from .cli import ChainCommand
Expand Down Expand Up @@ -133,41 +134,54 @@ def gen_genesis(
print("genesis validated")
return patch_json(
leader_home / "config" / "genesis.json",
{
"consensus.params.block.max_gas": "163000000",
"app_state.evm.params.evm_denom": "basecro",
"app_state.feemarket.params.no_base_fee": True,
**genesis_patch,
},
jsonmerge.merge(
{
"consensus": {"params": {"block": {"max_gas": "163000000"}}},
"app_state": {
"evm": {"params": {"evm_denom": "basecro"}},
"feemarket": {"params": {"no_base_fee": True}},
},
},
genesis_patch,
),
)


def patch_configs(home: Path, peers: str, config_patch: dict, app_patch: dict):
default_config_patch = {
"db_backend": "rocksdb",
"p2p.addr_book_strict": False,
"mempool.recheck": False,
"mempool.size": MEMPOOL_SIZE,
"consensus.timeout_commit": "1s",
"tx_index.indexer": "null",
"p2p": {"addr_book_strict": False},
"mempool": {
"recheck": False,
"size": MEMPOOL_SIZE,
},
"consensus": {"timeout_commit": "1s"},
"tx_index": {"indexer": "null"},
}
default_app_patch = {
"minimum-gas-prices": "0basecro",
"index-events": ["ethereum_tx.ethereumTxHash"],
"memiavl.enable": True,
"mempool.max-txs": MEMPOOL_SIZE,
"evm.block-executor": "block-stm", # or "sequential"
"evm.block-stm-workers": 0,
"evm.block-stm-pre-estimate": True,
"json-rpc.enable-indexer": True,
"memiavl": {
"enable": True,
"cache-size": 0,
},
"mempool": {"max-txs": MEMPOOL_SIZE},
"evm": {
"block-executor": "block-stm", # or "sequential"
"block-stm-workers": 0,
"block-stm-pre-estimate": True,
},
"json-rpc": {"enable-indexer": True},
}
# update persistent_peers and other configs in config.toml
config_patch = {
**default_config_patch,
**config_patch,
"p2p.persistent_peers": peers,
}
app_patch = {**default_app_patch, **app_patch}
config_patch = jsonmerge.merge(
default_config_patch,
jsonmerge.merge(
config_patch,
{"p2p": {"persistent_peers": peers}},
),
)
app_patch = jsonmerge.merge(default_app_patch, app_patch)
patch_toml(home / "config" / "config.toml", config_patch)
patch_toml(home / "config" / "app.toml", app_patch)

Expand Down
8 changes: 0 additions & 8 deletions testground/benchmark/benchmark/stateless.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,6 @@ def _gen(
(outdir / VALIDATOR_GROUP).mkdir(parents=True, exist_ok=True)
(outdir / FULLNODE_GROUP).mkdir(parents=True, exist_ok=True)

config_patch = (
json.loads(config_patch) if isinstance(config_patch, str) else config_patch
)
app_patch = json.loads(app_patch) if isinstance(app_patch, str) else app_patch
genesis_patch = (
json.loads(genesis_patch) if isinstance(genesis_patch, str) else genesis_patch
)

peers = []
for i in range(validators):
print("init validator", i)
Expand Down
23 changes: 11 additions & 12 deletions testground/benchmark/benchmark/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pathlib import Path

import bech32
import jsonmerge
import requests
import tomlkit
import web3
Expand All @@ -15,26 +16,24 @@
LOCAL_RPC = "http://localhost:26657"


def patch_dict(doc, kwargs):
for k, v in kwargs.items():
keys = k.split(".")
assert len(keys) > 0
cur = doc
for section in keys[:-1]:
cur = cur[section]
cur[keys[-1]] = v
def patch_toml_doc(doc, patch):
for k, v in patch.items():
if isinstance(v, dict):
patch_toml_doc(doc.setdefault(k, {}), v)
else:
doc[k] = v


def patch_toml(path: Path, kwargs):
def patch_toml(path: Path, patch):
doc = tomlkit.parse(path.read_text())
patch_dict(doc, kwargs)
patch_toml_doc(doc, patch)
path.write_text(tomlkit.dumps(doc))
return doc


def patch_json(path: Path, kwargs):
def patch_json(path: Path, patch):
doc = json.loads(path.read_text())
patch_dict(doc, kwargs)
doc = jsonmerge.merge(doc, patch)
path.write_text(json.dumps(doc))
return doc

Expand Down
16 changes: 15 additions & 1 deletion testground/benchmark/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions testground/benchmark/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ bech32 = "^1"
requests = "^2.32"
click = "^8.1.7"
ujson = "^5.10.0"
jsonmerge = "^1.9.2"

[tool.poetry.dev-dependencies]
pytest = "^8.2"
Expand Down

0 comments on commit b330f22

Please sign in to comment.