0g-storage-node/tests/test_framework/zgs_node.py

137 lines
4.6 KiB
Python
Raw Normal View History

2024-01-03 10:24:52 +00:00
import os
import shutil
2024-01-22 10:19:28 +00:00
import base64
2024-01-03 10:24:52 +00:00
from config.node_config import ZGS_CONFIG, update_config
2024-01-03 10:24:52 +00:00
from test_framework.blockchain_node import NodeType, TestNode
from utility.utils import (
initialize_toml_config,
p2p_port,
rpc_port,
blockchain_rpc_port,
)
class ZgsNode(TestNode):
def __init__(
self,
index,
root_dir,
binary,
updated_config,
log_contract_address,
mine_contract_address,
reward_contract_address,
2024-01-03 10:24:52 +00:00
log,
rpc_timeout=10,
libp2p_nodes=None,
key_file=None,
2024-01-03 10:24:52 +00:00
):
local_conf = ZGS_CONFIG.copy()
if libp2p_nodes is None:
if index == 0:
libp2p_nodes = []
else:
libp2p_nodes = []
for i in range(index):
libp2p_nodes.append(f"/ip4/127.0.0.1/tcp/{p2p_port(i)}")
2024-09-25 08:48:40 +00:00
rpc_listen_address = f"127.0.0.1:{rpc_port(index)}"
2024-01-03 10:24:52 +00:00
indexed_config = {
"network_libp2p_port": p2p_port(index),
"network_discovery_port": p2p_port(index),
2024-09-25 08:48:40 +00:00
"rpc": {
"listen_address": rpc_listen_address,
"listen_address_admin": rpc_listen_address,
},
2024-01-03 10:24:52 +00:00
"network_libp2p_nodes": libp2p_nodes,
"log_contract_address": log_contract_address,
"mine_contract_address": mine_contract_address,
"reward_contract_address": reward_contract_address,
2024-01-03 10:24:52 +00:00
"blockchain_rpc_endpoint": f"http://127.0.0.1:{blockchain_rpc_port(0)}",
}
# Set configs for this specific node.
update_config(local_conf, indexed_config)
2024-01-03 10:24:52 +00:00
# Overwrite with personalized configs.
update_config(local_conf, updated_config)
2024-01-03 10:24:52 +00:00
data_dir = os.path.join(root_dir, "zgs_node" + str(index))
self.key_file = key_file
2024-09-25 08:48:40 +00:00
rpc_url = "http://" + rpc_listen_address
2024-01-03 10:24:52 +00:00
super().__init__(
NodeType.Zgs,
index,
data_dir,
rpc_url,
binary,
local_conf,
log,
rpc_timeout,
)
def setup_config(self):
os.mkdir(self.data_dir)
2024-01-03 10:24:52 +00:00
log_config_path = os.path.join(self.data_dir, self.config["log_config_file"])
with open(log_config_path, "w") as f:
f.write("trace,hyper=info,h2=info")
2024-01-03 10:24:52 +00:00
if self.key_file is not None:
network_dir = os.path.join(self.data_dir, "network")
os.mkdir(network_dir)
shutil.copy(self.key_file, network_dir)
2024-01-03 10:24:52 +00:00
initialize_toml_config(self.config_file, self.config)
def wait_for_rpc_connection(self):
self._wait_for_rpc_connection(lambda rpc: rpc.zgs_getStatus() is not None)
def start(self):
self.log.info("Start zerog_storage node %d", self.index)
super().start()
# rpc
def zgs_get_status(self):
return self.rpc.zgs_getStatus()["connectedPeers"]
def zgs_upload_segment(self, segment):
return self.rpc.zgs_uploadSegment([segment])
def zgs_download_segment(self, data_root, start_index, end_index):
return self.rpc.zgs_downloadSegment([data_root, start_index, end_index])
2024-01-22 10:19:28 +00:00
def zgs_download_segment_decoded(self, data_root: str, start_chunk_index: int, end_chunk_index: int) -> bytes:
encodedSegment = self.rpc.zgs_downloadSegment([data_root, start_chunk_index, end_chunk_index])
return None if encodedSegment is None else base64.b64decode(encodedSegment)
2024-01-03 10:24:52 +00:00
def zgs_get_file_info(self, data_root):
return self.rpc.zgs_getFileInfo([data_root])
def zgs_get_file_info_by_tx_seq(self, tx_seq):
return self.rpc.zgs_getFileInfoByTxSeq([tx_seq])
2024-10-15 06:24:56 +00:00
def zgs_get_flow_context(self, tx_seq):
return self.rpc.zgs_getFlowContext([tx_seq])
2024-01-03 10:24:52 +00:00
def shutdown(self):
self.rpc.admin_shutdown()
self.wait_until_stopped()
def admin_start_sync_file(self, tx_seq):
return self.rpc.admin_startSyncFile([tx_seq])
2024-01-22 10:19:28 +00:00
def admin_start_sync_chunks(self, tx_seq: int, start_chunk_index: int, end_chunk_index: int):
return self.rpc.admin_startSyncChunks([tx_seq, start_chunk_index, end_chunk_index])
2024-01-03 10:24:52 +00:00
def admin_get_sync_status(self, tx_seq):
return self.rpc.admin_getSyncStatus([tx_seq])
def sync_status_is_completed_or_unknown(self, tx_seq):
2024-01-03 10:24:52 +00:00
status = self.rpc.admin_getSyncStatus([tx_seq])
return status == "Completed" or status == "unknown"
def admin_get_file_location(self, tx_seq, all_shards = True):
return self.rpc.admin_getFileLocation([tx_seq, all_shards])
2024-01-03 10:24:52 +00:00
def clean_data(self):
shutil.rmtree(os.path.join(self.data_dir, "db"))