Skip to content

Commit

Permalink
use catalyst-core python coding conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
saibatizoku committed Mar 31, 2023
1 parent 26e9f64 commit 93de80a
Show file tree
Hide file tree
Showing 8 changed files with 810 additions and 146 deletions.
882 changes: 785 additions & 97 deletions services/voting-node/poetry.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions services/voting-node/poetry.toml
@@ -1,5 +1,7 @@
[virtualenvs]
in-project = true
prefer-active-python = true

[virtualenvs.options]
no-pip = true
no-setuptools = true
6 changes: 5 additions & 1 deletion services/voting-node/pyproject.toml
Expand Up @@ -12,7 +12,7 @@ requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.poetry.dependencies]
python = "^3.10"
python = "^3.11"
# formatting
black = "^23.1.0"
# linting
Expand All @@ -34,6 +34,10 @@ aiofile = "^3.8.5"
[tool.poetry.scripts]
voting-node = "voting_node.main:cli"

[tool.black]
line-length = 132
target-version = ['py311']

[tool.ruff.per-file-ignores]
# ignore long lines in templates
"voting_node/templates.py" = ["E501"]
8 changes: 2 additions & 6 deletions services/voting-node/voting_node/db.py
Expand Up @@ -148,18 +148,14 @@ async def fetch_voteplans(self, event_id: int) -> List[VotePlan]:
logger.debug(f"voteplans retrieved from DB: {len(voteplans)}")
return list(map(lambda r: VotePlan(**dict(r)), voteplans))

async def insert_block0_info(
self, event_row_id: int, block0_bytes: bytes, block0_hash: str
):
async def insert_block0_info(self, event_row_id: int, block0_bytes: bytes, block0_hash: str):
# insert the hostname row into the voting_node table
columns = "block0 = $1, block0_hash = $2"
condition = "row_id = $3"
returning = "name"
query = f"UPDATE event SET {columns} WHERE {condition} RETURNING {returning}"
try:
result = await self.conn.execute(
query, block0_bytes, block0_hash, event_row_id
)
result = await self.conn.execute(query, block0_bytes, block0_hash, event_row_id)
if result is None:
raise Exception("failed to insert block0 info from DB")
logger.debug(f"block0 info added to event: {result}")
Expand Down
4 changes: 1 addition & 3 deletions services/voting-node/voting_node/jcli.py
Expand Up @@ -118,9 +118,7 @@ async def votes_committee_communication_key_to_public(self, input_key: str) -> s
commid = stdout.decode().rstrip()
return commid

async def vote_committee_member_key_generate(
self, comm_pub_keys: list[str], threshold: int
) -> str:
async def vote_committee_member_key_generate(self, comm_pub_keys: list[str], threshold: int) -> str:
...

async def genesis_encode(self, block0_bin: Path, genesis_yaml: Path):
Expand Down
4 changes: 1 addition & 3 deletions services/voting-node/voting_node/service.py
Expand Up @@ -146,9 +146,7 @@ async def jormungandr_subprocess_exec(self):
logger.warning(f"[stderr]\n{stderr.decode()}")

if proc.returncode != 0:
raise Exception(
f"jormungandr exited with non-zero status: {proc.returncode}"
)
raise Exception(f"jormungandr exited with non-zero status: {proc.returncode}")
except Exception as e:
logger.warning(f"jorm node error: {e}")
raise e
24 changes: 8 additions & 16 deletions services/voting-node/voting_node/tasks.py
@@ -1,6 +1,6 @@
from datetime import datetime
from pathlib import Path
from typing import Final, NoReturn, Optional, Tuple
from typing import Final, NoReturn, Optional

from . import utils
from .db import EventDb
Expand Down Expand Up @@ -83,9 +83,7 @@ def reset_data(self) -> None:
"""Resets data kept by the schedule runner."""
self.current_task = None

def reset_schedule(
self, msg: str = SCHEDULE_RESET_MSG, reset_data: bool = RESET_DATA
) -> NoReturn:
def reset_schedule(self, msg: str = SCHEDULE_RESET_MSG, reset_data: bool = RESET_DATA) -> NoReturn:
"""Reset the schedule by setting the current task to None, and raising
an exception that can be handled by the calling service.
Expand Down Expand Up @@ -316,9 +314,7 @@ async def set_node_config(self):
)

# convert to yaml and save
node_config_yaml = NodeConfigYaml(
config, self.node.storage.joinpath("node_config.yaml")
)
node_config_yaml = NodeConfigYaml(config, self.node.storage.joinpath("node_config.yaml"))
await node_config_yaml.save()
logger.debug(f"{node_config_yaml}")

Expand Down Expand Up @@ -464,17 +460,15 @@ async def setup_block0(self):
self.reset_schedule("event has no start time")

# create the commitee for this event
logger.info(f"creating committee address keyset")
logger.info("creating committee address keyset")
_, _, committee_id = await utils.create_address_keyset(self.jcli())
logger.info(f"creating committee communication keyset")
logger.info("creating committee communication keyset")
comm_keyset = await utils.create_comm_keyset(self.jcli())
logger.debug(f"comm keyset: {comm_keyset}")

# make committee member keys
logger.info(f"creating committee member keys")
_ = await utils.create_committee_member_keys(
self.jcli(), event.committee_size, event.committee_threshold
)
logger.info("creating committee member keys")
_ = await utils.create_committee_member_keys(self.jcli(), event.committee_size, event.committee_threshold)

# generate genesis file to make block0
logger.debug("generating genesis content")
Expand Down Expand Up @@ -503,9 +497,7 @@ async def publish_block0(self):
match self.node.block0:
case Block0(bin=block0_bytes, hash=block0_hash):
# push block0 to event table
await self.db.insert_block0_info(
event.row_id, block0_bytes, block0_hash
)
await self.db.insert_block0_info(event.row_id, block0_bytes, block0_hash)
# if all is good, we reset the schedule
logger.debug("inserted block0 info")
case None:
Expand Down
26 changes: 6 additions & 20 deletions services/voting-node/voting_node/utils.py
Expand Up @@ -154,9 +154,7 @@ def follower_node_config(
# get the path to the log directory, create it if necessary
persistent_log = storage.joinpath("persistent_log")
persistent_log.mkdir(parents=True, exist_ok=True)
node_config_dict["mempool"]["persistent_log"][
"dir"
] = f"{persistent_log.absolute()}"
node_config_dict["mempool"]["persistent_log"]["dir"] = f"{persistent_log.absolute()}"
# follower and leader nodes use these settings
node_config_dict["bootstrap_from_trusted_peers"] = True
node_config_dict["skip_bootstrap"] = False
Expand Down Expand Up @@ -222,9 +220,7 @@ async def create_comm_keyset(jcli: jcli.JCli) -> Tuple[str, str, str]:
return comm_sk, comm_pk, comm_id


async def create_committee_member_keys(
jcli: jcli.JCli, size: int, threshold: int
) -> Tuple[list, list, list]:
async def create_committee_member_keys(jcli: jcli.JCli, size: int, threshold: int) -> Tuple[list, list, list]:
match size:
case 0:
logger.info("no committee members")
Expand All @@ -234,25 +230,17 @@ async def create_committee_member_keys(
f"""creating {n} committee member(s), threshold is
{threshold}, votes will be private"""
)
committee_skeys = [
await jcli.votes_committee_communication_key_generate()
for _ in range(n)
]
committee_skeys = [await jcli.votes_committee_communication_key_generate() for _ in range(n)]
logger.debug(f"{len(committee_skeys)} member sk: {committee_skeys}")
committee_pkeys = [
await jcli.votes_committee_communication_key_to_public(key)
for key in committee_skeys
]
committee_pkeys = [await jcli.votes_committee_communication_key_to_public(key) for key in committee_skeys]
logger.debug(f"{len(committee_pkeys)} member pk: {committee_pkeys}")
committee_ids = [await jcli.key_to_bytes(key) for key in committee_pkeys]
return committee_skeys, committee_pkeys, committee_ids
case _:
raise Exception(f"expected threshold {threshold}, to be less than {size}")


def make_genesis_content(
event: Event, peers: List[LeaderHostInfo], committee_ids: List[str]
) -> Genesis:
def make_genesis_content(event: Event, peers: List[LeaderHostInfo], committee_ids: List[str]) -> Genesis:
start_time = event.get_start_time()
genesis = yaml.safe_load(GENESIS_YAML)
consensus_leader_ids = [peer.consensus_leader_id for peer in peers]
Expand All @@ -264,9 +252,7 @@ def make_genesis_content(
return Genesis(genesis)


async def make_block0(
jcli_path: str, storage: Path, genesis_path: Path
) -> Tuple[Path, str]:
async def make_block0(jcli_path: str, storage: Path, genesis_path: Path) -> Tuple[Path, str]:
block0_path = storage.joinpath("block0.bin")
jcli_exec = jcli.JCli(jcli_path)
await jcli_exec.genesis_encode(block0_path, genesis_path)
Expand Down

0 comments on commit 93de80a

Please sign in to comment.