|  | 
|  | 1 | +import asyncio | 
|  | 2 | +import websockets | 
|  | 3 | +import requests | 
|  | 4 | +import json | 
|  | 5 | +import os | 
|  | 6 | + | 
|  | 7 | +L1_WS_ENDPOINT = "ws://localhost:8546" | 
|  | 8 | +L2_HTTP_ENDPOINT = "http://localhost:9545" | 
|  | 9 | +OUTPUT_PROPOSED_TOPIC = "0xa7aaf2512769da4e444e3de247be2564225c2e7a8f74cfe528e46e17d24868e2" | 
|  | 10 | + | 
|  | 11 | +current_dir = os.path.dirname(os.path.abspath(__file__)) | 
|  | 12 | +project_root = os.path.dirname(os.path.dirname(current_dir)) | 
|  | 13 | +optimism_root = os.path.join(project_root, "rvsol/lib/optimism") | 
|  | 14 | + | 
|  | 15 | +with open(os.path.join(optimism_root, ".devnet/addresses.json"), "r") as f: | 
|  | 16 | +    addrs = json.load(f) | 
|  | 17 | + | 
|  | 18 | +logs = [] | 
|  | 19 | +l2_head = "" | 
|  | 20 | + | 
|  | 21 | + | 
|  | 22 | +async def subscribe_logs(): | 
|  | 23 | +    async with websockets.connect(L1_WS_ENDPOINT) as websocket: | 
|  | 24 | +        subscription_request = { | 
|  | 25 | +            "jsonrpc": "2.0", | 
|  | 26 | +            "method": "eth_subscribe", | 
|  | 27 | +            "params": ["logs", {"address": addrs["L2OutputOracleProxy"], "topics": [OUTPUT_PROPOSED_TOPIC]}], | 
|  | 28 | +            "id": 1 | 
|  | 29 | +        } | 
|  | 30 | + | 
|  | 31 | +        await websocket.send(json.dumps(subscription_request)) | 
|  | 32 | + | 
|  | 33 | +        print("Waiting OutputProposed logs...") | 
|  | 34 | +        while True: | 
|  | 35 | +            message = await websocket.recv() | 
|  | 36 | +            res = json.loads(message) | 
|  | 37 | +            if "params" in res: | 
|  | 38 | +                result = res["params"]["result"] | 
|  | 39 | +                logs.append({ | 
|  | 40 | +                    "outputRoot": result["topics"][1], | 
|  | 41 | +                    "l2BlockNumber": int(result["topics"][3], base=16), | 
|  | 42 | +                    "l1BlockNumber": int(result["blockNumber"], base=16), | 
|  | 43 | +                    "l1BlockHash": result["blockHash"] | 
|  | 44 | +                }) | 
|  | 45 | +            if len(logs) == 2: | 
|  | 46 | +                break | 
|  | 47 | + | 
|  | 48 | +    l2_block_reqeust = { | 
|  | 49 | +        "jsonrpc": "2.0", | 
|  | 50 | +        "method": "eth_getBlockByNumber", | 
|  | 51 | +        "params": [hex(logs[0]["l2BlockNumber"]), False], | 
|  | 52 | +        "id": 1 | 
|  | 53 | +    } | 
|  | 54 | +    res = requests.post(L2_HTTP_ENDPOINT, json=l2_block_reqeust).json() | 
|  | 55 | + | 
|  | 56 | +    global l2_head | 
|  | 57 | +    l2_head = res["result"]["hash"] | 
|  | 58 | + | 
|  | 59 | +asyncio.run(subscribe_logs()) | 
|  | 60 | + | 
|  | 61 | +local_cmd = f'''#!/bin/bash | 
|  | 62 | +
 | 
|  | 63 | +./asterisc run \\ | 
|  | 64 | +  --info-at '%10000000' \\ | 
|  | 65 | +  --proof-at never \\ | 
|  | 66 | +  --input ./state.json \\ | 
|  | 67 | +  -- \\ | 
|  | 68 | +  ./op-program \\ | 
|  | 69 | +  --rollup.config ./chain-artifacts/rollup.json \\ | 
|  | 70 | +  --l2.genesis ./chain-artifacts/genesis-l2.json \\ | 
|  | 71 | +  --l1.trustrpc \\ | 
|  | 72 | +  --l1.rpckind debug_geth \\ | 
|  | 73 | +  --l1.head {logs[1]["l1BlockHash"]} \\ | 
|  | 74 | +  --l2.head {l2_head} \\ | 
|  | 75 | +  --l2.outputroot {logs[0]["outputRoot"]} \\ | 
|  | 76 | +  --l2.claim {logs[1]["outputRoot"]} \\ | 
|  | 77 | +  --l2.blocknumber {logs[1]["l2BlockNumber"]} \\ | 
|  | 78 | +  --datadir ./preimages \\ | 
|  | 79 | +  --log.format terminal \\ | 
|  | 80 | +  --server''' | 
|  | 81 | + | 
|  | 82 | +capture_cmd = local_cmd + " --l1 http://127.0.0.1:8545 --l2 http://127.0.0.1:9545" | 
|  | 83 | + | 
|  | 84 | +# Script to capture preimages from the local devnet | 
|  | 85 | +with open(os.path.join(current_dir, "capture_cmd.sh"), "w") as f: | 
|  | 86 | +    f.write(capture_cmd) | 
|  | 87 | +os.chmod("capture_cmd.sh", 0o755) | 
|  | 88 | + | 
|  | 89 | +# Script to run op-program in offline mode | 
|  | 90 | +with open(os.path.join(current_dir, "local_cmd.sh"), "w") as f: | 
|  | 91 | +    f.write(local_cmd) | 
|  | 92 | +os.chmod("local_cmd.sh", 0o755) | 
0 commit comments