2024-04-09 07:45:02 +00:00
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import tempfile
|
2024-09-19 09:47:50 +00:00
|
|
|
import platform
|
2024-04-09 07:45:02 +00:00
|
|
|
|
|
|
|
from test_framework.blockchain_node import BlockChainNodeType, BlockchainNode
|
|
|
|
from utility.utils import blockchain_rpc_port, arrange_port
|
2024-05-17 08:29:23 +00:00
|
|
|
from utility.build_binary import build_zg
|
2024-04-09 07:45:02 +00:00
|
|
|
|
2024-05-17 08:29:23 +00:00
|
|
|
ZGNODE_PORT_CATEGORY_WS = 0
|
|
|
|
ZGNODE_PORT_CATEGORY_P2P = 1
|
|
|
|
ZGNODE_PORT_CATEGORY_RPC = 2
|
|
|
|
ZGNODE_PORT_CATEGORY_PPROF = 3
|
2024-04-09 07:45:02 +00:00
|
|
|
|
2024-05-17 08:29:23 +00:00
|
|
|
def zg_node_init_genesis(binary: str, root_dir: str, num_nodes: int):
|
2024-04-09 07:45:02 +00:00
|
|
|
assert num_nodes > 0, "Invalid number of blockchain nodes: %s" % num_nodes
|
|
|
|
|
2024-04-25 03:25:05 +00:00
|
|
|
if not os.path.exists(binary):
|
2024-05-17 08:29:23 +00:00
|
|
|
build_zg(os.path.dirname(binary))
|
2024-04-25 03:25:05 +00:00
|
|
|
|
2024-04-09 07:45:02 +00:00
|
|
|
shell_script = os.path.join(
|
|
|
|
os.path.dirname(os.path.realpath(__file__)), # test_framework folder
|
2024-05-17 08:29:23 +00:00
|
|
|
"..", "config", "0gchain-init-genesis.sh"
|
2024-04-09 07:45:02 +00:00
|
|
|
)
|
2024-09-19 09:47:50 +00:00
|
|
|
sys = platform.system().lower()
|
|
|
|
if sys == "darwin":
|
|
|
|
shell_script = os.path.join(
|
|
|
|
os.path.dirname(os.path.realpath(__file__)), # test_framework folder
|
|
|
|
"..", "config", "0gchain-init-genesis-mac.sh"
|
|
|
|
)
|
|
|
|
|
2024-04-09 07:45:02 +00:00
|
|
|
|
2024-05-17 08:29:23 +00:00
|
|
|
zgchaind_dir = os.path.join(root_dir, "0gchaind")
|
|
|
|
os.mkdir(zgchaind_dir)
|
2024-04-09 07:45:02 +00:00
|
|
|
|
2024-05-17 08:29:23 +00:00
|
|
|
log_file = tempfile.NamedTemporaryFile(dir=zgchaind_dir, delete=False, prefix="init_genesis_", suffix=".log")
|
|
|
|
p2p_port_start = arrange_port(ZGNODE_PORT_CATEGORY_P2P, 0)
|
2024-04-09 07:45:02 +00:00
|
|
|
|
|
|
|
ret = subprocess.run(
|
2024-05-17 08:29:23 +00:00
|
|
|
args=["bash", shell_script, zgchaind_dir, str(num_nodes), str(p2p_port_start)],
|
2024-04-09 07:45:02 +00:00
|
|
|
stdout=log_file,
|
|
|
|
stderr=log_file,
|
|
|
|
)
|
|
|
|
|
|
|
|
log_file.close()
|
|
|
|
|
2024-05-17 08:29:23 +00:00
|
|
|
assert ret.returncode == 0, "Failed to init 0gchain genesis, see more details in log file: %s" % log_file.name
|
2024-04-09 07:45:02 +00:00
|
|
|
|
2024-05-17 08:29:23 +00:00
|
|
|
class ZGNode(BlockchainNode):
|
2024-04-09 07:45:02 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
index,
|
|
|
|
root_dir,
|
|
|
|
binary,
|
|
|
|
updated_config,
|
|
|
|
contract_path,
|
|
|
|
log,
|
|
|
|
rpc_timeout=10,
|
|
|
|
):
|
2024-05-17 08:29:23 +00:00
|
|
|
data_dir = os.path.join(root_dir, "0gchaind", "node" + str(index))
|
2024-04-09 07:45:02 +00:00
|
|
|
rpc_url = "http://127.0.0.1:%s" % blockchain_rpc_port(index)
|
|
|
|
|
|
|
|
super().__init__(
|
|
|
|
index,
|
|
|
|
data_dir,
|
|
|
|
rpc_url,
|
|
|
|
binary,
|
|
|
|
{},
|
|
|
|
contract_path,
|
|
|
|
log,
|
2024-05-17 08:29:23 +00:00
|
|
|
BlockChainNodeType.ZG,
|
2024-04-09 07:45:02 +00:00
|
|
|
rpc_timeout,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.config_file = None
|
|
|
|
self.args = [
|
|
|
|
binary, "start",
|
|
|
|
"--home", data_dir,
|
|
|
|
# overwrite json rpc http port: 8545
|
|
|
|
"--json-rpc.address", "127.0.0.1:%s" % blockchain_rpc_port(index),
|
|
|
|
# overwrite json rpc ws port: 8546
|
2024-05-17 08:29:23 +00:00
|
|
|
"--json-rpc.ws-address", "127.0.0.1:%s" % arrange_port(ZGNODE_PORT_CATEGORY_WS, index),
|
2024-04-09 07:45:02 +00:00
|
|
|
# overwrite p2p port: 26656
|
2024-05-17 08:29:23 +00:00
|
|
|
"--p2p.laddr", "tcp://127.0.0.1:%s" % arrange_port(ZGNODE_PORT_CATEGORY_P2P, index),
|
2024-04-09 07:45:02 +00:00
|
|
|
# overwrite rpc port: 26657
|
2024-05-17 08:29:23 +00:00
|
|
|
"--rpc.laddr", "tcp://127.0.0.1:%s" % arrange_port(ZGNODE_PORT_CATEGORY_RPC, index),
|
2024-04-09 07:45:02 +00:00
|
|
|
# overwrite pprof port: 6060
|
2024-05-17 08:29:23 +00:00
|
|
|
"--rpc.pprof_laddr", "127.0.0.1:%s" % arrange_port(ZGNODE_PORT_CATEGORY_PPROF, index),
|
2024-09-06 09:53:00 +00:00
|
|
|
"--log_level", "debug"
|
2024-04-09 07:45:02 +00:00
|
|
|
]
|
|
|
|
|
2024-04-09 09:54:34 +00:00
|
|
|
for k, v in updated_config.items():
|
|
|
|
if type(k) == str and k.startswith("--"):
|
|
|
|
self.args.append(k)
|
|
|
|
self.args.append(str(v))
|
|
|
|
|
2024-04-09 07:45:02 +00:00
|
|
|
def setup_config(self):
|
|
|
|
""" Already batch initialized by shell script in framework """
|