Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added subnet realm header #4

Merged
merged 4 commits into from
Sep 26, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions src/bt_auto_dumper/_v2/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ def main(apiver: str | None = None):
apiver = apiver or pathlib.Path(__file__).parent.name
parser = argparse.ArgumentParser(description=f"BT Auto Dumper CLI {apiver}")
parser.add_argument("--note", help="Comment or note for the operation", type=str, default="")
parser.add_argument("subnet_identifier", help="Subnet Identifier", type=str)
parser.add_argument("autovalidator_address", help="AutoValidator Address", type=str)
parser.add_argument("subnet_realm", help="Subnet Realm", type=str, choices=["testnet", "mainnet", "devnet"], default="mainnet")
ppolewicz marked this conversation as resolved.
Show resolved Hide resolved
parser.add_argument("--set-autovalidator-address", help="Set a new autovalidator address", type=str)
parser.add_argument("--set-codename", help="Set a new Subnet Identifier codename", type=str)

Expand All @@ -46,19 +49,21 @@ def main(apiver: str | None = None):
)
print(f"Configuration updated successfully at {config_path}")

autovalidator_address, subnet_identifier = load_config(config_path=config_path)
dump_and_upload(subnet_identifier, autovalidator_address, args.note)
if not (subnet_identifier := args.subnet_identifier) or not (autovalidator_address := args.autovalidator_address):
autovalidator_address, subnet_identifier = load_config(config_path=config_path)
dump_and_upload(subnet_identifier, args.subnet_realm, autovalidator_address, args.note)


def dump_and_upload(subnet_identifier: str, autovalidator_address: str, note: str):
def dump_and_upload(subnet_identifier: str, subnet_realm: str, autovalidator_address: str, note: str):
"""
Dump and upload the output of the commands to the AutoValidator
Args:
subnet_identifier: Subnet Identifier
subnet_realm: Subnet Realm
autovalidator_address: AutoValidator Address
note: Comment or note for the operation
Example:
dump_and_upload("computehorde", "http://localhost:8000", "Test")
dump_and_upload("computehorde", "mainnet", "http://localhost:8000", "Test")
"""
subnets = {
"computehorde": ["echo 'Mainnet Command 1'", "echo 'Mainnet Command 2'"],
Expand All @@ -67,6 +72,7 @@ def dump_and_upload(subnet_identifier: str, autovalidator_address: str, note: st

wallet = bt.wallet(name="validator", hotkey="validator-hotkey")
normalized_subnet_identifier = re.sub(r"[_\-.]", "", str.lower(subnet_identifier))
commands = {}
if normalized_subnet_identifier in subnets:
commands = {normalized_subnet_identifier: subnets[normalized_subnet_identifier]}

Expand All @@ -87,10 +93,12 @@ def dump_and_upload(subnet_identifier: str, autovalidator_address: str, note: st
with zipfile.ZipFile(zip_filename, "w") as zipf:
for file in output_files:
zipf.write(file)
send_to_autovalidator(zip_filename, wallet, autovalidator_address, note, normalized_subnet_identifier)
send_to_autovalidator(zip_filename, wallet, autovalidator_address, note, normalized_subnet_identifier, subnet_realm)


def make_signed_request(method: str, url: str, headers: dict, file_path: str, wallet: bt.wallet) -> requests.Response:
def make_signed_request(
method: str, url: str, headers: dict, file_path: str, wallet: bt.wallet, subnet_realm: str
) -> requests.Response:
"""
Make a signed request to the AutoValidator
Args:
Expand All @@ -112,6 +120,7 @@ def make_signed_request(method: str, url: str, headers: dict, file_path: str, wa
"""
headers["Nonce"] = str(time.time())
headers["Hotkey"] = wallet.hotkey.ss58_address
headers["Realm"] = subnet_realm
files = {"file": open(file_path, "rb")}
file = files.get("file")
file_content = b""
Expand All @@ -130,7 +139,12 @@ def make_signed_request(method: str, url: str, headers: dict, file_path: str, wa


def send_to_autovalidator(
zip_filename: str, wallet: bt.wallet, autovalidator_address: str, note: str, subnet_identifier: str
zip_filename: str,
wallet: bt.wallet,
autovalidator_address: str,
note: str,
subnet_identifier: str,
subnet_realm: str,
):
"""
Send the dump file to the AutoValidator
Expand All @@ -149,7 +163,7 @@ def send_to_autovalidator(
"Note": note,
"SubnetID": subnet_identifier,
}
response = make_signed_request("POST", url, headers, zip_filename, wallet)
response = make_signed_request("POST", url, headers, zip_filename, wallet, subnet_realm)
if response.status_code == 201:
print("File successfully uploaded and resource created.")
elif response.status_code == 200:
Expand Down
Loading