forked from NSPG13/agent-bounties
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_open_competition_v1_mainnet_bundle.py
More file actions
291 lines (260 loc) · 13.1 KB
/
Copy pathbuild_open_competition_v1_mainnet_bundle.py
File metadata and controls
291 lines (260 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/env python3
"""Build the unsigned, bytecode-frozen Base mainnet Open Competition V1 bundle."""
from __future__ import annotations
import argparse
import hashlib
import json
import os
from pathlib import Path
import re
from typing import Any
from urllib.error import URLError
from urllib.request import Request, urlopen
from _shared.evm import address_bytes, address_word, artifact_hex, create_address, keccak256
CHAIN_ID = 8_453
CHAIN_ID_HEX = "0x2105"
USDC = "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913"
ADMIN = "0x884834e884d6e93462655a2820140ad03e6747bc"
VERIFIER = "0xcc6059ceeda5bc4ba8a97ecfbffa7488c8fd579e"
VERIFIER_RUNTIME_HASH = "0xbaa3a8305c4b65d0dc20131d0ef207fdaf4763f345393a831370cd04077df9b3"
DIFFICULTY_BITS = 16
DEFAULT_RPC = "https://mainnet.base.org"
MIN_DEPLOYER_ETH_WEI = 100_000_000_000_000
CANARY_SOLVER_REWARD = 1_000_000
CANARY_VERIFIER_REWARD = 100_000
CANARY_INITIAL_FUNDING = CANARY_SOLVER_REWARD + CANARY_VERIFIER_REWARD
CANARY_SOLVER_BOND = CANARY_VERIFIER_REWARD
MIN_CANARY_USDC = CANARY_INITIAL_FUNDING + CANARY_SOLVER_BOND
CANARY_BENCHMARK_PREIMAGE = "leading-zero-work-v1/difficulty-16/canary-benchmark-v1"
CANARY_EVIDENCE_SCHEMA_PREIMAGE = "agent-bounties/leading-zero-work-evidence-v1"
CANARY_BENCHMARK_HASH = keccak256(CANARY_BENCHMARK_PREIMAGE.encode())
CANARY_EVIDENCE_SCHEMA_HASH = keccak256(CANARY_EVIDENCE_SCHEMA_PREIMAGE.encode())
def verifier_profile() -> dict[str, Any]:
return {
"profile_id": "leading-zero-work-v1-difficulty-16-mainnet-canary",
"verifier_address": VERIFIER,
"difficulty_bits": DIFFICULTY_BITS,
"runtime_code_hash": VERIFIER_RUNTIME_HASH,
"benchmark_preimage": CANARY_BENCHMARK_PREIMAGE,
"benchmark_hash": CANARY_BENCHMARK_HASH,
"evidence_schema_preimage": CANARY_EVIDENCE_SCHEMA_PREIMAGE,
"evidence_schema_hash": CANARY_EVIDENCE_SCHEMA_HASH,
"usage": "protocol_canary_only",
"public_inventory_eligible": False,
}
def normalized_sha256(path: Path) -> str:
return hashlib.sha256(path.read_bytes().replace(b"\r\n", b"\n")).hexdigest()
def patched_runtime(artifact: dict[str, Any], values: list[bytes], name: str) -> bytes:
deployed = artifact.get("deployedBytecode")
runtime = bytearray(artifact_hex(deployed, f"{name}.deployedBytecode"))
references = deployed.get("immutableReferences") if isinstance(deployed, dict) else None
if not isinstance(references, dict) or len(references) != len(values):
raise ValueError(f"{name} immutable reference count changed")
for value, (_, locations) in zip(values, sorted(references.items(), key=lambda item: int(item[0]))):
if len(value) != 32 or not isinstance(locations, list) or not locations:
raise ValueError(f"{name} immutable reference is invalid")
for location in locations:
start = int(location["start"])
length = int(location["length"])
if length != 32 or start < 0 or start + length > len(runtime):
raise ValueError(f"{name} immutable reference is out of bounds")
runtime[start : start + length] = value
return bytes(runtime)
def rpc(url: str, method: str, params: list[Any]) -> Any:
payload = json.dumps({"jsonrpc": "2.0", "id": 1, "method": method, "params": params}).encode()
request = Request(
url,
data=payload,
headers={"content-type": "application/json", "user-agent": "agent-bounties-open-competition/1"},
)
try:
with urlopen(request, timeout=30) as response:
body = json.load(response)
except (OSError, URLError) as error:
raise RuntimeError(f"RPC transport failed for {method}: {error}") from error
if body.get("error"):
raise RuntimeError(f"RPC {method} failed: {json.dumps(body['error'], sort_keys=True)}")
return body.get("result")
def balance_of_data(address: str) -> str:
return f"0x70a08231{address_word(address).hex()}"
def difficulty_bits_data() -> str:
return "0x249379ad"
def deployment_action(
*, nonce: int, data: bytes, expected_contract: str, runtime: bytes, implementation: str,
implementation_runtime: bytes
) -> dict[str, Any]:
return {
"name": "deploy_open_competition_factory_v1",
"from": ADMIN,
"from_nonce": nonce,
"to": None,
"value_wei": 0,
"data": f"0x{data.hex()}",
"creation_code_hash": keccak256(data),
"expected_contract": expected_contract.lower(),
"expected_runtime_code": f"0x{runtime.hex()}",
"runtime_code_hash": keccak256(runtime),
"runtime_code_bytes": len(runtime),
"expected_implementation": implementation.lower(),
"expected_implementation_runtime_code": f"0x{implementation_runtime.hex()}",
"implementation_runtime_code_hash": keccak256(implementation_runtime),
"implementation_runtime_code_bytes": len(implementation_runtime),
}
def build_bundle(args: argparse.Namespace) -> dict[str, Any]:
repo = Path(__file__).resolve().parents[1]
deployer = args.deployer.lower()
address_bytes(deployer)
if deployer != ADMIN:
raise ValueError(f"deployer must be the frozen admin wallet {ADMIN}")
if not re.fullmatch(r"[0-9a-f]{40}", args.source_commit):
raise ValueError("source commit must be a full lowercase Git commit")
if not re.fullmatch(r"0x[0-9a-fA-F]{64}", args.preflight_block_hash):
raise ValueError("preflight block hash must be bytes32 hex")
block_tag = hex(args.preflight_block_number)
if args.offline:
if args.preflight_deployer_eth_wei is None or args.preflight_deployer_usdc_base_units is None:
raise ValueError("offline generation requires pinned deployer ETH and USDC balances")
deployer_eth = args.preflight_deployer_eth_wei
deployer_usdc = args.preflight_deployer_usdc_base_units
else:
block = rpc(args.rpc_url, "eth_getBlockByNumber", [block_tag, False])
if not block or block.get("hash", "").lower() != args.preflight_block_hash.lower():
raise RuntimeError("pinned Base mainnet block hash mismatch")
if rpc(args.rpc_url, "eth_chainId", []) != CHAIN_ID_HEX:
raise RuntimeError("RPC is not Base mainnet")
if rpc(args.rpc_url, "eth_getCode", [USDC, block_tag]) == "0x":
raise RuntimeError("native Base mainnet USDC code is unavailable")
verifier_runtime = bytes.fromhex(rpc(args.rpc_url, "eth_getCode", [VERIFIER, block_tag])[2:])
if keccak256(verifier_runtime) != VERIFIER_RUNTIME_HASH:
raise RuntimeError("approved mainnet verifier runtime hash mismatch")
observed_difficulty = int(
rpc(args.rpc_url, "eth_call", [{"to": VERIFIER, "data": difficulty_bits_data()}, block_tag]), 16
)
if observed_difficulty != DIFFICULTY_BITS:
raise RuntimeError("approved mainnet verifier difficulty mismatch")
observed_nonce = int(rpc(args.rpc_url, "eth_getTransactionCount", [deployer, block_tag]), 16)
if observed_nonce != args.deployer_nonce:
raise RuntimeError(
f"deployer nonce mismatch at pinned block: expected {args.deployer_nonce}, got {observed_nonce}"
)
deployer_eth = int(rpc(args.rpc_url, "eth_getBalance", [deployer, block_tag]), 16)
deployer_usdc = int(
rpc(args.rpc_url, "eth_call", [{"to": USDC, "data": balance_of_data(deployer)}, block_tag]), 16
)
if deployer_eth < MIN_DEPLOYER_ETH_WEI:
raise RuntimeError(f"admin wallet needs at least {MIN_DEPLOYER_ETH_WEI} wei for bounded deployment gas")
if deployer_usdc < MIN_CANARY_USDC:
raise RuntimeError(f"admin wallet needs at least {MIN_CANARY_USDC} USDC base units for the bounded canary")
factory_address = create_address(deployer, args.deployer_nonce)
implementation_address = create_address(factory_address, 1)
if not args.offline:
for name, address in (("factory", factory_address), ("implementation", implementation_address)):
if rpc(args.rpc_url, "eth_getCode", [address, block_tag]) != "0x":
raise RuntimeError(f"predicted {name} address is occupied: {address}")
out = repo / "contracts" / "base-escrow" / "out"
factory_artifact = json.loads(
(out / "OpenCompetitionBountyFactoryV1.sol" / "OpenCompetitionBountyFactoryV1.json").read_text(
encoding="utf-8"
)
)
implementation_artifact = json.loads(
(out / "OpenCompetitionBountyV1.sol" / "OpenCompetitionBountyV1.json").read_text(encoding="utf-8")
)
factory_data = artifact_hex(factory_artifact.get("bytecode"), "factory.bytecode") + address_word(USDC)
factory_runtime = patched_runtime(
factory_artifact,
[address_word(USDC), address_word(implementation_address)],
"factory",
)
implementation_runtime = artifact_hex(
implementation_artifact.get("deployedBytecode"), "implementation.deployedBytecode"
)
source_files = [
repo / "contracts/base-escrow/src/OpenCompetitionBountyV1.sol",
repo / "contracts/base-escrow/src/OpenCompetitionBountyFactoryV1.sol",
repo / "contracts/base-escrow/src/LeadingZeroWorkVerifier.sol",
repo / "contracts/base-escrow/src/IAgentBounty.sol",
]
action = deployment_action(
nonce=args.deployer_nonce,
data=factory_data,
expected_contract=factory_address,
runtime=factory_runtime,
implementation=implementation_address,
implementation_runtime=implementation_runtime,
)
return {
"schema_version": "agent-bounties/open-competition-v1-mainnet-bundle-v1",
"protocol_version": "agent-bounties/open-competition-v1",
"network": "base-mainnet",
"chain_id": CHAIN_ID,
"deployment_state": "sepolia_rehearsed_not_ready_to_earn",
"source_commit": args.source_commit,
"compiler": {"solc": "0.8.26", "optimizer": True, "optimizer_runs": 200, "evm": "cancun"},
"source_sha256": {
str(path.relative_to(repo)).replace("\\", "/"): normalized_sha256(path) for path in source_files
},
"deployer": deployer,
"settlement_token": USDC,
"preflight_block": {
"number": args.preflight_block_number,
"hash": args.preflight_block_hash.lower(),
"deployer_nonce": args.deployer_nonce,
"deployer_eth_wei": deployer_eth,
"deployer_usdc_base_units": deployer_usdc,
},
"actions": [action],
"verifier_profile": verifier_profile(),
"factory": factory_address.lower(),
"implementation": implementation_address.lower(),
"hidden_canary": {
"solver_reward_usdc_base_units": CANARY_SOLVER_REWARD,
"verifier_reward_usdc_base_units": CANARY_VERIFIER_REWARD,
"entry_bond_usdc_base_units": CANARY_SOLVER_BOND,
"initial_funding_usdc_base_units": CANARY_INITIAL_FUNDING,
"total_admin_usdc_budget_base_units": MIN_CANARY_USDC,
"max_entries": 4,
"competition_window_seconds": 86_400,
"reveal_window_seconds": 3_600,
"creator_may_compete": False,
"separate_solver_wallet_required": True,
"inventory_visibility": "hidden",
},
"activation": {
"public_creation_enabled": False,
"public_commitments_enabled": False,
"public_inventory_eligible": False,
"required_before_activation": [
"canonical safe-block BountySettled event",
"exact USDC balance reconciliation",
"healthy indexer heartbeat",
"verified API, MCP, CLI, Python SDK, and TypeScript SDK behavior",
],
},
"evidence_boundary": (
"This bundle contains unsigned exact factory deployment calldata, a pinned approved verifier, "
"and bounded hidden-canary parameters. It is not deployment, settlement, payment, or activation evidence."
),
}
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--deployer", default=ADMIN)
parser.add_argument("--deployer-nonce", type=int, required=True)
parser.add_argument("--source-commit", required=True)
parser.add_argument("--preflight-block-number", type=int, required=True)
parser.add_argument("--preflight-block-hash", required=True)
parser.add_argument("--rpc-url", default=os.environ.get("BASE_MAINNET_RPC_URL", DEFAULT_RPC))
parser.add_argument("--offline", action="store_true")
parser.add_argument("--preflight-deployer-eth-wei", type=int)
parser.add_argument("--preflight-deployer-usdc-base-units", type=int)
parser.add_argument("--output", type=Path, required=True)
return parser.parse_args()
def main() -> int:
args = parse_args()
bundle = build_bundle(args)
args.output.parent.mkdir(parents=True, exist_ok=True)
args.output.write_text(json.dumps(bundle, indent=2) + "\n", encoding="utf-8")
print(json.dumps({"output": str(args.output), "factory": bundle["factory"], "implementation": bundle["implementation"]}))
return 0
if __name__ == "__main__":
raise SystemExit(main())