Skip to content

Commit 5541b41

Browse files
author
huangyi
committed
Problem: config patch don't support list
Solution: - change to jsonmerge package
1 parent 73c19da commit 5541b41

File tree

5 files changed

+54
-38
lines changed

5 files changed

+54
-38
lines changed

testground/benchmark/benchmark/peer.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pathlib import Path
55
from typing import List
66

7+
import jsonmerge
78
from pydantic.json import pydantic_encoder
89

910
from .cli import ChainCommand
@@ -145,29 +146,38 @@ def gen_genesis(
145146
def patch_configs(home: Path, peers: str, config_patch: dict, app_patch: dict):
146147
default_config_patch = {
147148
"db_backend": "rocksdb",
148-
"p2p.addr_book_strict": False,
149-
"mempool.recheck": False,
150-
"mempool.size": MEMPOOL_SIZE,
151-
"consensus.timeout_commit": "1s",
152-
"tx_index.indexer": "null",
149+
"p2p": {"addr_book_strict": False},
150+
"mempool": {
151+
"recheck": False,
152+
"size": MEMPOOL_SIZE,
153+
},
154+
"consensus": {"timeout_commit": "1s"},
155+
"tx_index": {"indexer": "null"},
153156
}
154157
default_app_patch = {
155158
"minimum-gas-prices": "0basecro",
156159
"index-events": ["ethereum_tx.ethereumTxHash"],
157-
"memiavl.enable": True,
158-
"mempool.max-txs": MEMPOOL_SIZE,
159-
"evm.block-executor": "block-stm", # or "sequential"
160-
"evm.block-stm-workers": 0,
161-
"evm.block-stm-pre-estimate": True,
162-
"json-rpc.enable-indexer": True,
160+
"memiavl": {
161+
"enable": True,
162+
"cache-size": 0,
163+
},
164+
"mempool": {"max-txs": MEMPOOL_SIZE},
165+
"evm": {
166+
"block-executor": "block-stm", # or "sequential"
167+
"block-stm-workers": 0,
168+
"block-stm-pre-estimate": True,
169+
},
170+
"json-rpc": {"enable-indexer": True},
163171
}
164172
# update persistent_peers and other configs in config.toml
165-
config_patch = {
166-
**default_config_patch,
167-
**config_patch,
168-
"p2p.persistent_peers": peers,
169-
}
170-
app_patch = {**default_app_patch, **app_patch}
173+
config_patch = jsonmerge.merge(
174+
default_config_patch,
175+
jsonmerge.merge(
176+
config_patch,
177+
{"p2p": {"persistent_peers": peers}},
178+
),
179+
)
180+
app_patch = jsonmerge.merge(default_app_patch, app_patch)
171181
patch_toml(home / "config" / "config.toml", config_patch)
172182
patch_toml(home / "config" / "app.toml", app_patch)
173183

testground/benchmark/benchmark/stateless.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,6 @@ def _gen(
9191
(outdir / VALIDATOR_GROUP).mkdir(parents=True, exist_ok=True)
9292
(outdir / FULLNODE_GROUP).mkdir(parents=True, exist_ok=True)
9393

94-
config_patch = (
95-
json.loads(config_patch) if isinstance(config_patch, str) else config_patch
96-
)
97-
app_patch = json.loads(app_patch) if isinstance(app_patch, str) else app_patch
98-
genesis_patch = (
99-
json.loads(genesis_patch) if isinstance(genesis_patch, str) else genesis_patch
100-
)
101-
10294
peers = []
10395
for i in range(validators):
10496
print("init validator", i)

testground/benchmark/benchmark/utils.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pathlib import Path
55

66
import bech32
7+
import jsonmerge
78
import requests
89
import tomlkit
910
import web3
@@ -15,26 +16,24 @@
1516
LOCAL_RPC = "http://localhost:26657"
1617

1718

18-
def patch_dict(doc, kwargs):
19-
for k, v in kwargs.items():
20-
keys = k.split(".")
21-
assert len(keys) > 0
22-
cur = doc
23-
for section in keys[:-1]:
24-
cur = cur[section]
25-
cur[keys[-1]] = v
19+
def patch_toml_doc(doc, patch):
20+
for k, v in patch.items():
21+
if isinstance(v, dict):
22+
patch_toml_doc(doc.setdefault(k, {}), v)
23+
else:
24+
doc[k] = v
2625

2726

28-
def patch_toml(path: Path, kwargs):
27+
def patch_toml(path: Path, patch):
2928
doc = tomlkit.parse(path.read_text())
30-
patch_dict(doc, kwargs)
29+
patch_toml_doc(doc, patch)
3130
path.write_text(tomlkit.dumps(doc))
3231
return doc
3332

3433

35-
def patch_json(path: Path, kwargs):
34+
def patch_json(path: Path, patch):
3635
doc = json.loads(path.read_text())
37-
patch_dict(doc, kwargs)
36+
doc = jsonmerge.merge(doc, patch)
3837
path.write_text(json.dumps(doc))
3938
return doc
4039

testground/benchmark/poetry.lock

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testground/benchmark/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ bech32 = "^1"
1717
requests = "^2.32"
1818
click = "^8.1.7"
1919
ujson = "^5.10.0"
20+
jsonmerge = "^1.9.2"
2021

2122
[tool.poetry.dev-dependencies]
2223
pytest = "^8.2"

0 commit comments

Comments
 (0)