Skip to content

Commit

Permalink
Merge pull request #4 from konrad0960/add-realm-header
Browse files Browse the repository at this point in the history
Added subnet realm header
  • Loading branch information
ppolewicz authored Sep 26, 2024
2 parents ce1a6a2 + 8b044b4 commit b1d00ed
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 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,15 @@ 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",
)
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 +55,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 +78,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 +99,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 +126,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 +145,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 +169,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

0 comments on commit b1d00ed

Please sign in to comment.