Skip to content

Commit

Permalink
[refactor]: use Popen's env instead of global os.environ
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Balashov <[email protected]>
  • Loading branch information
0x009922 committed Dec 22, 2023
1 parent 831818c commit 8a23e39
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions scripts/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def __init__(self, args: argparse.Namespace):
self.out_dir = args.out_dir
peers_dir = args.out_dir.joinpath("peers")
os.makedirs(peers_dir, exist_ok=True)
self.shared_env = {}

self.peers = [_Peer(args, i) for i in range(args.n_peers)]

Expand All @@ -45,15 +46,15 @@ def __init__(self, args: argparse.Namespace):
sys.exit(1)
copy_or_prompt_build_bin("iroha", args.root_dir, peers_dir)

os.environ["IROHA_CONFIG"] = str(peers_dir.joinpath("config.json"))
os.environ["IROHA_GENESIS_PUBLIC_KEY"] = self.peers[0].public_key
self.shared_env["IROHA_CONFIG"] = str(peers_dir.joinpath("config.json"))
self.shared_env["IROHA_GENESIS_PUBLIC_KEY"] = self.peers[0].public_key

logging.info("Generating trusted peers...")
self.trusted_peers = []
for peer in self.peers:
peer_entry = {"address": f"{peer.host_ip}:{peer.p2p_port}", "public_key": peer.public_key}
self.trusted_peers.append(json.dumps(peer_entry))
os.environ["SUMERAGI_TRUSTED_PEERS"] = f"[{','.join(self.trusted_peers)}]"
self.shared_env["SUMERAGI_TRUSTED_PEERS"] = f"[{','.join(self.trusted_peers)}]"

def wait_for_genesis(self, n_tries: int):
for i in range(n_tries):
Expand All @@ -76,7 +77,7 @@ def wait_for_genesis(self, n_tries: int):

def run(self):
for i, peer in enumerate(self.peers):
peer.run(submit_genesis=(i == 0))
peer.run(shared_env=self.shared_env, submit_genesis=(i == 0))
self.wait_for_genesis(20)

class _Peer:
Expand Down Expand Up @@ -115,34 +116,32 @@ def __init__(self, args: argparse.Namespace, nth: int):

logging.info(f"Peer {self.name} initialized")

def run(self, submit_genesis: bool = False):
def run(self, shared_env: dict(), submit_genesis: bool = False):
logging.info(f"Running peer {self.name}...")

os.environ["KURA_BLOCK_STORE_PATH"] = str(self.peer_dir.joinpath("storage"))
os.environ["SNAPSHOT_DIR_PATH"] = str(self.peer_dir.joinpath("storage"))
os.environ["LOG_LEVEL"] = "INFO"
os.environ["LOG_FORMAT"] = "\"pretty\""
os.environ["LOG_TOKIO_CONSOLE_ADDR"] = f"{self.host_ip}:{self.tokio_console_port}"
os.environ["IROHA_PUBLIC_KEY"] = self.public_key
os.environ["IROHA_PRIVATE_KEY"] = self.private_key
os.environ["SUMERAGI_DEBUG_FORCE_SOFT_FORK"] = "false"
os.environ["TORII_P2P_ADDR"] = f"{self.host_ip}:{self.p2p_port}"
os.environ["TORII_API_URL"] = f"{self.host_ip}:{self.api_port}"
peer_env = dict(shared_env)
peer_env["KURA_BLOCK_STORE_PATH"] = str(self.peer_dir.joinpath("storage"))
peer_env["SNAPSHOT_DIR_PATH"] = str(self.peer_dir.joinpath("storage"))
peer_env["LOG_LEVEL"] = "INFO"
peer_env["LOG_FORMAT"] = "\"pretty\""
peer_env["LOG_TOKIO_CONSOLE_ADDR"] = f"{self.host_ip}:{self.tokio_console_port}"
peer_env["IROHA_PUBLIC_KEY"] = self.public_key
peer_env["IROHA_PRIVATE_KEY"] = self.private_key
peer_env["SUMERAGI_DEBUG_FORCE_SOFT_FORK"] = "false"
peer_env["TORII_P2P_ADDR"] = f"{self.host_ip}:{self.p2p_port}"
peer_env["TORII_API_URL"] = f"{self.host_ip}:{self.api_port}"

if submit_genesis:
os.environ["IROHA_GENESIS_PRIVATE_KEY"] = self.private_key
peer_env["IROHA_GENESIS_PRIVATE_KEY"] = self.private_key
# Assuming it was copied to the peer's directory
os.environ["IROHA_GENESIS_FILE"] = str(self.peer_dir.joinpath("genesis.json"))
else:
for x in ["IROHA_GENESIS_PRIVATE_KEY", "IROHA_GENESIS_FILE"]:
os.environ.pop(x, None)
peer_env["IROHA_GENESIS_FILE"] = str(self.peer_dir.joinpath("genesis.json"))

# FD never gets closed
stdout_file = open(self.peer_dir.joinpath(".stdout"), "w")
stderr_file = open(self.peer_dir.joinpath(".stderr"), "w")
# These processes are created detached from the parent process already
subprocess.Popen([self.name] + (["--submit-genesis"] if submit_genesis else []),
executable=f"{self.out_dir}/peers/iroha", stdout=stdout_file, stderr=stderr_file)
executable=f"{self.out_dir}/peers/iroha", env=peer_env, stdout=stdout_file, stderr=stderr_file)

def pos_int(arg):
if int(arg) > 0:
Expand Down

0 comments on commit 8a23e39

Please sign in to comment.