Skip to content

Commit

Permalink
Merge branch 'blank_slate'
Browse files Browse the repository at this point in the history
  • Loading branch information
FiveMovesAhead committed Jan 18, 2025
2 parents 0b3a68e + 1bb3a70 commit 0bb08ef
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 1 deletion.
5 changes: 4 additions & 1 deletion tig-benchmarker/master/master/client_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ async def get_batch_data(batch_id: str):
'start_nonce', A.batch_idx * B.batch_size,
'num_nonces', LEAST(B.batch_size, B.num_nonces - A.batch_idx * B.batch_size),
'settings', B.settings,
'sampled_nonces', NULL,
'sampled_nonces', D.sampled_nonces,
'runtime_config', B.runtime_config,
'download_url', B.download_url,
'rand_hash', B.rand_hash,
Expand All @@ -229,6 +229,9 @@ async def get_batch_data(batch_id: str):
INNER JOIN batch_data C
ON A.benchmark_id = C.benchmark_id
AND A.batch_idx = C.batch_idx
LEFT JOIN proofs_batch D
ON A.benchmark_id = D.benchmark_id
AND A.batch_idx = D.batch_idx
"""
)

Expand Down
134 changes: 134 additions & 0 deletions tig-benchmarker/verify_batch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
from common.structs import *
import requests
import json
import random
import os
import subprocess

print("THIS IS AN EXAMPLE SCRIPT TO VERIFY A BATCH")
TIG_WORKER_PATH = input("Enter path of tig-worker executable: ")
NUM_WORKERS = int(input("Enter number of workers: "))
if not os.path.exists(TIG_WORKER_PATH):
raise FileNotFound[ERROR](f"tig-worker not found at path: {TIG_WORKER_PATH}")
MASTER_IP = input("Enter Master IP: ")
MASTER_PORT = input("Enter Master Port: ")

jobs = requests.get(f"http://{MASTER_IP}:{MASTER_PORT}/get-jobs").json()
for i, j in enumerate(jobs):
print(f"{i + 1}) benchmark_id: {j['benchmark_id']}, challenge: {j['challenge']}, algorithm: {j['algorithm']}, status: {j['status']}")
job_idx = int(input("Enter the index of the batch you want to verify: ")) - 1

for i, b in enumerate(jobs[job_idx]["batches"]):
print(f"{i + 1}) slave: {b['slave']}, num_solutions: {b['num_solutions']}, status: {b['status']}")
batch_idx = int(input("Enter the index of the batch you want to verify: ")) - 1

benchmark_id = jobs[job_idx]["benchmark_id"]
url = f"http://{MASTER_IP}:{MASTER_PORT}/get-batch-data/{benchmark_id}_{batch_idx}"
print(f"Fetching batch data from {url}")
data = requests.get(url).json()

batch = data['batch']
merkle_root = data['merkle_root']
solution_nonces = data['solution_nonces']
merkle_proofs = data['merkle_proofs']
if merkle_proofs is not None:
merkle_proofs = {x['leaf']['nonce']: x for x in merkle_proofs}

if (
merkle_proofs is None or
len(solution_nonces := set(merkle_proofs) & set(solution_nonces)) == 0
):
print("No solution data to verify for this batch")
else:
for nonce in solution_nonces:
print(f"Verifying solution for nonce: {nonce}")
cmd = [
TIG_WORKER_PATH, "verify_solution",
json.dumps(batch['settings'], separators=(',', ':')),
batch["rand_hash"],
str(nonce),
json.dumps(merkle_proofs[nonce]['leaf']['solution'], separators=(',', ':')),
]
print(f"Running cmd: {' '.join(cmd)}")
ret = subprocess.run(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
if ret.returncode == 0:
print(f"[SUCCESS]: {ret.stdout.decode()}")
else:
print(f"[ERROR]: {ret.stderr.decode()}")

if merkle_root is not None:
download_url = batch["download_url"]
print(f"Downloading WASM from {download_url}")
resp = requests.get(download_url)
if resp.status_code != 200:
raise Exception(f"status {resp.status_code} when downloading WASM: {resp.text}")
wasm_path = f'{batch["settings"]["algorithm_id"]}.wasm'
with open(wasm_path, 'wb') as f:
f.write(resp.content)
print(f"WASM Path: {wasm_path}")
print("")

if merkle_proofs is None:
print("No merkle proofs to verify for this batch")
else:
for nonce in merkle_proofs:
print(f"Verifying output data for nonce: {nonce}")
cmd = [
TIG_WORKER_PATH, "compute_solution",
json.dumps(batch['settings'], separators=(',', ':')),
batch["rand_hash"],
str(nonce),
wasm_path,
"--mem", str(batch["runtime_config"]["max_memory"]),
"--fuel", str(batch["runtime_config"]["max_fuel"]),
]
print(f"Running cmd: {' '.join(cmd)}")
ret = subprocess.run(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
out = json.loads(ret.stdout.decode())
expected = json.dumps(merkle_proofs[nonce]['leaf'], separators=(',', ':'), sort_keys=True)
actual = json.dumps(out, separators=(',', ':'), sort_keys=True)
if expected == actual:
print(f"[SUCCESS]: output data match")
else:
print(f"[ERROR]: output data mismatch")
print(f"Batch data: {expected}")
print(f"Recomputed: {actual}")
print(f"")

if merkle_root is None:
print("No merkle root to verify for this batch")
else:
print("Verifying merkle root")
cmd = [
TIG_WORKER_PATH, "compute_batch",
json.dumps(batch["settings"]),
batch["rand_hash"],
str(batch["start_nonce"]),
str(batch["num_nonces"]),
str(batch["batch_size"]),
wasm_path,
"--mem", str(batch["runtime_config"]["max_memory"]),
"--fuel", str(batch["runtime_config"]["max_fuel"]),
"--workers", str(NUM_WORKERS),
]
print(f"Running cmd: {' '.join(cmd)}")
ret = subprocess.run(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
if ret.returncode == 0:
out = json.loads(ret.stdout.decode())
if out["merkle_root"] == merkle_root:
print(f"[SUCCESS]: merkle root match")
else:
print(f"[ERROR]: merkle root mismatch")
print(f"Batch data: {expected}")
print(f"Recomputed: {actual}")
else:
print(f"[ERROR]: {ret.stderr.decode()}")

print("")
print("FINISHED")

0 comments on commit 0bb08ef

Please sign in to comment.