Improve zg chain port to avoid port conflict in parallel execution

This commit is contained in:
boqiu 2024-11-04 18:29:15 +08:00
parent f26f321272
commit 071185120d
2 changed files with 18 additions and 16 deletions

View File

@ -3,14 +3,9 @@ import subprocess
import tempfile import tempfile
from test_framework.blockchain_node import BlockChainNodeType, BlockchainNode from test_framework.blockchain_node import BlockChainNodeType, BlockchainNode
from utility.utils import blockchain_rpc_port, arrange_port from utility.utils import blockchain_p2p_port, blockchain_rpc_port, blockchain_ws_port, blockchain_rpc_port_tendermint, pprof_port
from utility.build_binary import build_zg from utility.build_binary import build_zg
ZGNODE_PORT_CATEGORY_WS = 0
ZGNODE_PORT_CATEGORY_P2P = 1
ZGNODE_PORT_CATEGORY_RPC = 2
ZGNODE_PORT_CATEGORY_PPROF = 3
def zg_node_init_genesis(binary: str, root_dir: str, num_nodes: int): def zg_node_init_genesis(binary: str, root_dir: str, num_nodes: int):
assert num_nodes > 0, "Invalid number of blockchain nodes: %s" % num_nodes assert num_nodes > 0, "Invalid number of blockchain nodes: %s" % num_nodes
@ -26,7 +21,7 @@ def zg_node_init_genesis(binary: str, root_dir: str, num_nodes: int):
os.mkdir(zgchaind_dir) os.mkdir(zgchaind_dir)
log_file = tempfile.NamedTemporaryFile(dir=zgchaind_dir, delete=False, prefix="init_genesis_", suffix=".log") log_file = tempfile.NamedTemporaryFile(dir=zgchaind_dir, delete=False, prefix="init_genesis_", suffix=".log")
p2p_port_start = arrange_port(ZGNODE_PORT_CATEGORY_P2P, 0) p2p_port_start = blockchain_p2p_port(0)
ret = subprocess.run( ret = subprocess.run(
args=["bash", shell_script, zgchaind_dir, str(num_nodes), str(p2p_port_start)], args=["bash", shell_script, zgchaind_dir, str(num_nodes), str(p2p_port_start)],
@ -71,13 +66,13 @@ class ZGNode(BlockchainNode):
# overwrite json rpc http port: 8545 # overwrite json rpc http port: 8545
"--json-rpc.address", "127.0.0.1:%s" % blockchain_rpc_port(index), "--json-rpc.address", "127.0.0.1:%s" % blockchain_rpc_port(index),
# overwrite json rpc ws port: 8546 # overwrite json rpc ws port: 8546
"--json-rpc.ws-address", "127.0.0.1:%s" % arrange_port(ZGNODE_PORT_CATEGORY_WS, index), "--json-rpc.ws-address", "127.0.0.1:%s" % blockchain_ws_port(index),
# overwrite p2p port: 26656 # overwrite p2p port: 26656
"--p2p.laddr", "tcp://127.0.0.1:%s" % arrange_port(ZGNODE_PORT_CATEGORY_P2P, index), "--p2p.laddr", "tcp://127.0.0.1:%s" % blockchain_p2p_port(index),
# overwrite rpc port: 26657 # overwrite rpc port: 26657
"--rpc.laddr", "tcp://127.0.0.1:%s" % arrange_port(ZGNODE_PORT_CATEGORY_RPC, index), "--rpc.laddr", "tcp://127.0.0.1:%s" % blockchain_rpc_port_tendermint(index),
# overwrite pprof port: 6060 # overwrite pprof port: 6060
"--rpc.pprof_laddr", "127.0.0.1:%s" % arrange_port(ZGNODE_PORT_CATEGORY_PPROF, index), "--rpc.pprof_laddr", "127.0.0.1:%s" % pprof_port(index),
"--log_level", "debug" "--log_level", "debug"
] ]

View File

@ -23,18 +23,25 @@ def rpc_port(n):
def blockchain_p2p_port(n): def blockchain_p2p_port(n):
return PortMin.n + 2 * MAX_NODES + n assert MAX_NODES % 2 == 0 and n <= MAX_NODES / 2
return PortMin.n + 3 * MAX_NODES / 2 + n
def blockchain_rpc_port(n): def blockchain_rpc_port(n):
return PortMin.n + 3 * MAX_NODES + n return PortMin.n + 4 * MAX_NODES / 2 + n
def blockchain_rpc_port_core(n): def blockchain_rpc_port_core(n):
return PortMin.n + 4 * MAX_NODES + n return PortMin.n + 5 * MAX_NODES / 2 + n
def arrange_port(category: int, node_index: int) -> int: def blockchain_ws_port(n):
return PortMin.n + (100 + category) * MAX_NODES + node_index return PortMin.n + 6 * MAX_NODES / 2 + n
def blockchain_rpc_port_tendermint(n):
return PortMin.n + 7 * MAX_NODES / 2 + n
def pprof_port(n):
return PortMin.n + 8 * MAX_NODES / 2 + n
def wait_until(predicate, *, attempts=float("inf"), timeout=float("inf"), lock=None): def wait_until(predicate, *, attempts=float("inf"), timeout=float("inf"), lock=None):
if attempts == float("inf") and timeout == float("inf"): if attempts == float("inf") and timeout == float("inf"):