# gf-a: qt1 Gate F reproduction Credit: Taylor (gtmcknight) authored the Gaussian filter, replica, policy and qm tables; qt1 incorporates qm-spline's table compression. [Taylor's pinned sources](https://github.com/gtmcknight/amm-sim/tree/4812a6382311624e1506aca131ca35c378f930d9/research/solved); [qt1 artifact](https://api.imdnotdead.fun/artifacts/8a6e303b662e1a5ea9bb47f31acedd3988ef65b3ddb66d074517aa59ad41ca40); [qm-spline data](https://api.imdnotdead.fun/artifacts/17d249ab0c33fc44931d9b105cf3f9e3846a560bd518b67310c2a56defacdecc). ## Setup All figures below are measurements from this run unless explicitly attributed. Both repositories were cloned side by side under `test/scratch/` in the workspace. All installed packages, downloads, compiled objects and traces remained under that scratch tree. The only deliverables are this report and `sources.json`. - amm-challenge: `6b6921f8d463314aa7c96234e1be25a25245f704` ([source](https://github.com/benedictbrady/amm-challenge/tree/6b6921f8d463314aa7c96234e1be25a25245f704)). - amm-sim: `4812a6382311624e1506aca131ca35c378f930d9`, unmodified `research/solved` ([source](https://github.com/gtmcknight/amm-sim/tree/4812a6382311624e1506aca131ca35c378f930d9/research/solved)). - Ran `sh ../amm-sim/setup-fast.sh` once from the challenge root; exit 0. Every subsequent challenge Python/replay command sourced `scratch/env.sh`. Setup used the requested post1 Linux x86_64 wheel. Its installed native-library SHA-256 was `ff88b6353c2aad7342f0a26194f7347e78b9c3b38d683a7449006d423978cd14`, matching the setup script's check. StarterStrategy validation passed. [Pinned setup script](https://raw.githubusercontent.com/gtmcknight/amm-sim/4812a6382311624e1506aca131ca35c378f930d9/setup-fast.sh). - Runtime used Python 3.12.3, pyrevm 0.3.7, py-solc-x 2.1.0, numpy 2.5.4 and installed solc 0.8.24. `amm_sim_rs.gas_stats` was available; no engine benchmark was needed for this item. - Extracted qt1's Solidity and Python fenced blocks from [Q](https://api.imdnotdead.fun/artifacts/8a6e303b662e1a5ea9bb47f31acedd3988ef65b3ddb66d074517aa59ad41ca40). Solidity: 42,990 bytes, SHA-256 `c68887cff61180e12ba2b1ceb333edf76a830f91f63b5d775a8806fb9ec08aca` (matches Q). Python: 7,739 bytes, measured SHA-256 `1dfc9b7a3c2237ff7a93dd9f0af54b9bdab16f2ce0a7eb00784cb4432598db92` (Q does not specify a separate full Python hash). - Decoded the base64 `## Data` block from [qm-spline](https://api.imdnotdead.fun/artifacts/17d249ab0c33fc44931d9b105cf3f9e3846a560bd518b67310c2a56defacdecc): 10,167 bytes, SHA-256 `3ac7cb8811ecfb6a17f499aa4e9b70dd55f7fb6432b3aec3b904e11a3dece38e`, matching Q's full hash. - Compiled qt1 unchanged: solc 0.8.24, optimizer enabled with 200 runs, viaIR, Paris, matching the [pinned compiler](https://raw.githubusercontent.com/benedictbrady/amm-challenge/6b6921f8d463314aa7c96234e1be25a25245f704/amm_competition/evm/compiler.py). Deployed runtime: **20,605 bytes**, below 24,576. Gate P ran before the new harness: unmodified `python3 -I gates.py` on plain Python reproduced all digits from the source's check table ([gate script](https://raw.githubusercontent.com/gtmcknight/amm-sim/4812a6382311624e1506aca131ca35c378f930d9/research/solved/gates.py), [README](https://github.com/gtmcknight/amm-sim/tree/4812a6382311624e1506aca131ca35c378f930d9/research/solved/README.md)): | Seed | Perfect info | Gaussian + plain | Gaussian + qm | Lattice + qm | |---|---:|---:|---:|---:| | 10000 | 448.261189 | 443.455598 | 443.522608 | 443.663520 | | 10001 | 576.779199 | 571.463409 | 571.623551 | 571.977293 | | 10002 | 531.886497 | 525.168028 | 525.865859 | 526.411631 | The extracted qmref also reproduced Q's required scores, using `python3 -I` and the decoded blob: **443.101741 / 571.454483 / 525.908441** on seeds **10000 / 10001 / 10002**. These are reference identity checks, not new strategy-selection or engine results. ## Gate F **PASS.** Every one of the 50 runs had 2,000 successful ordered afterSwap replays on a fresh pyrevm deployment, plus successful initialization at exactly `100e18 / 10000e18`. Gas was capped at 245,000 on every initialization and callback. There were 100,050 successful calls including init; the callback statistics below exclude initialization. The wrapper drove the full `replica2.run` for each seed, preserving all original callback arguments and return values for the simulator while recording only the first 2,000 own-pool callbacks. For replay, amounts and reserves were encoded as `int(float * 10**18)`; timestamp remained the integer step. Comparison uses bid and ask in [IAMMStrategy's WAD fee units](https://raw.githubusercontent.com/benedictbrady/amm-challenge/6b6921f8d463314aa7c96234e1be25a25245f704/contracts/src/IAMMStrategy.sol), converted to fractions, with absolute differences multiplied by `1e4` to report bps. Expected fees include the replica's `[0, 0.1]` cap (see Problems). The per-side mean covers the 200,000 afterSwap comparisons; init difference is separately reported over both sides of all 50 initializations. ```json { "contract": "qt1 Strategy.sol (sha256 c68887cff61180e12ba2b1ceb333edf76a830f91f63b5d775a8806fb9ec08aca)", "seeds": [ 70000, 70001, 70002, 70003, 70004, 70005, 70006, 70007, 70008, 70009, 70010, 70011, 70012, 70013, 70014, 70015, 70016, 70017, 70018, 70019, 70020, 70021, 70022, 70023, 70024, 70025, 70026, 70027, 70028, 70029, 70030, 70031, 70032, 70033, 70034, 70035, 70036, 70037, 70038, 70039, 70040, 70041, 70042, 70043, 70044, 70045, 70046, 70047, 70048, 70049 ], "runs": 50, "callbacks": 100000, "comparisons": 200000, "max_bps": 5.825917152673353e-06, "max_seed": 70031, "mean_bps": 1.6891943455680554e-07, "init_diff_bps": 0.0, "pyrevm_peak": 194051, "pyrevm_peak_seed": 70032, "pyrevm_mean": 124776.88952, "reverts": 0, "oog": 0 } ``` These independently measured results reproduce [Q's published Gate F](https://api.imdnotdead.fun/artifacts/8a6e303b662e1a5ea9bb47f31acedd3988ef65b3ddb66d074517aa59ad41ca40) at the stated precision: max 5.83e-6 bps, peak 194,051 at seed 70032, mean gas 124,777, no failures. The maximum fee difference is about 1,716 times smaller than 0.01 bps. pyrevm peak is 50,949 below the 245,000-gas ceiling. Per-run measurements (all rows have 2,000 callbacks, 4,000 comparisons, zero init difference, zero reverts and zero OOG). Mean gas is afterSwap gas only. Every initialization used 90,858 gas. Values in this table are rounded for display; aggregate JSON uses unrounded values. | Seed | Max abs diff (bps) | Mean abs diff (bps) | Peak gas | Mean gas | |---|---:|---:|---:|---:| | 70000 | 1.50489270351e-06 | 1.673617077e-07 | 177686 | 126829.1045 | | 70001 | 2.05722876234e-06 | 1.79646727688e-07 | 164724 | 123745.8825 | | 70002 | 1.16947936903e-06 | 1.76231694352e-07 | 183919 | 126031.9155 | | 70003 | 1.01069880763e-06 | 1.52284736221e-07 | 157420 | 123370.7430 | | 70004 | 8.58641200675e-07 | 1.68658589023e-07 | 179427 | 126513.1385 | | 70005 | 3.13926400391e-06 | 1.75716230553e-07 | 168818 | 123986.5860 | | 70006 | 9.93186262399e-07 | 1.683107169e-07 | 173151 | 123452.2755 | | 70007 | 1.25900443716e-06 | 1.74685055445e-07 | 177385 | 124465.4490 | | 70008 | 1.03283869738e-06 | 1.67123242866e-07 | 170449 | 126853.5135 | | 70009 | 2.46114845517e-06 | 1.75076934759e-07 | 173173 | 123322.2480 | | 70010 | 1.66999601214e-06 | 1.72427615575e-07 | 181614 | 125362.2705 | | 70011 | 1.39804786237e-06 | 1.58098890108e-07 | 176658 | 125438.7680 | | 70012 | 1.9480562313e-06 | 1.70976935843e-07 | 177685 | 125898.8320 | | 70013 | 1.22010674133e-06 | 1.64399881621e-07 | 176753 | 125623.5890 | | 70014 | 2.61733052983e-06 | 1.77773425325e-07 | 170701 | 123311.7960 | | 70015 | 1.93293711766e-06 | 1.62137862375e-07 | 172644 | 124712.7290 | | 70016 | 1.50569836113e-06 | 1.66489027647e-07 | 185884 | 126883.4085 | | 70017 | 1.0403649692e-06 | 1.55219269409e-07 | 161597 | 123024.5785 | | 70018 | 1.33919426763e-06 | 1.72025566062e-07 | 164646 | 123580.5505 | | 70019 | 1.19680161614e-06 | 1.49972104498e-07 | 170993 | 123306.5055 | | 70020 | 1.16640504547e-06 | 1.71800429889e-07 | 183612 | 124596.0070 | | 70021 | 1.63315831345e-06 | 1.6241597506e-07 | 161906 | 122740.8565 | | 70022 | 1.66680816068e-06 | 1.79304902028e-07 | 172142 | 125278.2110 | | 70023 | 8.80644940651e-07 | 1.56396339251e-07 | 166739 | 123718.3550 | | 70024 | 1.16039917258e-06 | 1.67984129719e-07 | 160412 | 123297.6755 | | 70025 | 1.89275635746e-06 | 1.63238488419e-07 | 175092 | 126493.1605 | | 70026 | 1.25555485605e-06 | 1.62794377979e-07 | 164423 | 124306.1980 | | 70027 | 1.06647613483e-06 | 1.61718512449e-07 | 168660 | 125489.0315 | | 70028 | 1.29979447344e-06 | 1.63210084065e-07 | 167353 | 124202.2010 | | 70029 | 1.04725446197e-06 | 1.68727173633e-07 | 166488 | 125413.2600 | | 70030 | 1.29825616403e-06 | 1.80293233961e-07 | 173157 | 126432.1325 | | 70031 | 5.82591715267e-06 | 1.83644052112e-07 | 164676 | 125016.2270 | | 70032 | 1.44775174488e-06 | 1.80425677532e-07 | 194051 | 123644.6815 | | 70033 | 3.45261527701e-06 | 1.73598289099e-07 | 164225 | 122259.1990 | | 70034 | 1.61677726762e-06 | 1.76852441788e-07 | 166841 | 123402.6875 | | 70035 | 1.28193743381e-06 | 1.63976344687e-07 | 174524 | 126095.3210 | | 70036 | 2.47944258874e-06 | 1.81330844628e-07 | 168401 | 123896.1705 | | 70037 | 9.4564778317e-07 | 1.52564226502e-07 | 160844 | 123772.0340 | | 70038 | 1.07787439564e-06 | 1.65057468424e-07 | 169717 | 123791.1355 | | 70039 | 2.87812484349e-06 | 1.85130638567e-07 | 160664 | 123352.9425 | | 70040 | 1.22793030952e-06 | 1.64447891086e-07 | 176904 | 126377.3240 | | 70041 | 1.79355313587e-06 | 1.78133592306e-07 | 179603 | 124560.0075 | | 70042 | 9.61884061984e-07 | 1.62880100145e-07 | 166652 | 124993.4165 | | 70043 | 1.12069790947e-06 | 1.67260253681e-07 | 179704 | 125158.5480 | | 70044 | 1.70509058262e-06 | 1.69071905375e-07 | 170320 | 125956.2945 | | 70045 | 2.2969488464e-06 | 1.92348920965e-07 | 172990 | 124526.5075 | | 70046 | 1.54181530824e-06 | 1.65013265465e-07 | 182825 | 124858.8935 | | 70047 | 1.00655888138e-06 | 1.60865465985e-07 | 172084 | 126894.0575 | | 70048 | 1.40172716316e-06 | 1.77464601951e-07 | 170017 | 126223.4135 | | 70049 | 1.0302297123e-06 | 1.53405887119e-07 | 176483 | 126384.6430 | ## Harness Complete final `scratch/check.py` below. It accepts a Solidity path, reference-module path and half-open seed range. `QMRef`, `blob.bin` and the pinned `replica2.py` are resolved beside the reference. Invoke it from the challenge checkout with its scratch environment sourced. The compilation cache is keyed by the full source/settings input and compiler version. Gas comes from `evm.result.gas_used`, rather than the challenge Python executor's estimate. The simulator receives the original fees and clamps them itself; only the recorded expectations are capped. ```python #!/usr/bin/env python3 """Gate F replay harness. Credit: Taylor (gtmcknight), replica, policy, qm tables. Run from the pinned amm-challenge root with scratch/env.sh sourced. Usage: python3 -I scratch/check.py scratch/Strategy.sol scratch/qmref.py 70000:70050 Seed range is start-inclusive, stop-exclusive. Reference must expose QMRef. Place the pinned replica2.py and blob.bin beside the reference module. Outputs scratch/gatef_START_STOP.json. No engine seeds are consumed. """ import argparse import hashlib import importlib.util import json import math from pathlib import Path import sys import solcx from pyrevm import EVM WAD = 10**18 GAS = 245000 CALLS = 2000 # Local simulated EVM caller only; no live address or owner is required. CALLER = '0x2000000000000000000000000000000000000002' INIT_SELECTOR = bytes.fromhex('837aef47') SWAP_SELECTOR = bytes.fromhex('c2babb57') def word(n): return int(n).to_bytes(32, 'big') def load_module(path, name): spec = importlib.util.spec_from_file_location(name, path) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) return module def compile_contract(path): sources = {'Strategy.sol': {'content': path.read_text()}} # Match the pinned challenge compiler's import layout. for name in ('AMMStrategyBase.sol', 'IAMMStrategy.sol'): sources[name] = {'content': (Path('contracts/src') / name).read_text()} inp = {'language': 'Solidity', 'sources': sources, 'settings': { 'optimizer': {'enabled': True, 'runs': 200}, 'viaIR': True, 'evmVersion': 'paris', 'outputSelection': {'*': {'*': ['abi', 'evm.bytecode.object', 'evm.deployedBytecode.object']}}}} key = hashlib.sha256(('solc-0.8.24\n' + json.dumps(inp, sort_keys=True)).encode()).hexdigest() cache = Path('scratch') / ('compiled_' + key + '.json') if cache.exists(): compiled = json.loads(cache.read_text()) else: out = solcx.compile_standard(inp, solc_version='0.8.24') errors = [e for e in out.get('errors', []) if e['severity'] == 'error'] if errors: raise RuntimeError(errors) compiled = out['contracts']['Strategy.sol']['Strategy'] cache.write_text(json.dumps(compiled)) runtime = bytes.fromhex(compiled['evm']['deployedBytecode']['object']) assert len(runtime) <= 24576, len(runtime) print(json.dumps({'compile': {'source_sha256': hashlib.sha256(path.read_bytes()).hexdigest(), 'runtime_bytes': len(runtime), 'cache': str(cache)}}), flush=True) return bytes.fromhex(compiled['evm']['bytecode']['object']) class Recorder: wants_j = True def __init__(self, policy): self.policy = policy self.rows = [] self.init_fees = None def init(self, *args): self.init_fees = tuple(min(.1, max(0., f)) for f in self.policy.init(*args)) return self.init_fees def on_trade(self, is_buy, ax, ay, t, x, y, *extra): # Pass the original floats and oracle extras to the reference unchanged. fees = self.policy.on_trade(is_buy, ax, ay, t, x, y, *extra) if len(self.rows) < CALLS: self.rows.append(((int(is_buy), int(ax * WAD), int(ay * WAD), int(t), int(x * WAD), int(y * WAD)), tuple(min(.1, max(0., f)) for f in fees))) # replica2.clampf applies this same cap before fees affect a pool. return fees def invoke(evm, address, selector, args): try: data = evm.message_call(caller=CALLER, to=address, calldata=selector + b''.join(word(n) for n in args), value=0, gas=GAS) except Exception as exc: # result is the actual execution result, not executor.py's gas estimate. result = evm.result reason = str(exc) + (' ' + str(result.reason) if result is not None else '') lower = reason.lower().replace(' ', '').replace('_', '') return None, None, reason, ('outofgas' in lower or 'oog' in lower) result = evm.result if not result.is_success or len(data) != 64: reason = str(result.reason) + '; return bytes=' + str(len(data)) return None, result.gas_used, reason, 'outofgas' in reason.lower().replace(' ', '') fees = tuple(int.from_bytes(data[i:i + 32], 'big') / WAD for i in (0, 32)) return fees, result.gas_used, None, False def diffs(actual, expected): assert len(actual) == len(expected) == 2 assert all(math.isfinite(v) for v in (*actual, *expected)) return [abs(a - b) * 1e4 for a, b in zip(actual, expected)] def run_seed(seed, reference, replica, blob, bytecode): recorder = Recorder(reference.QMRef(str(blob))) score = replica.run(seed, recorder)['edge'] assert len(recorder.rows) == CALLS, (seed, len(recorder.rows)) evm = EVM() address = evm.deploy(deployer=CALLER, code=bytecode, value=0, gas=10000000) assert evm.result.is_success actual, init_gas, error, oog = invoke(evm, address, INIT_SELECTOR, (100 * WAD, 10000 * WAD)) row = {'seed': seed, 'callbacks_recorded': len(recorder.rows), 'callbacks': 0, 'comparisons': 0, 'max_bps': 0.0, 'sum_bps': 0.0, 'init_diff_bps': None, 'init_gas': init_gas, 'pyrevm_peak': 0, 'gas_sum': 0, 'reverts': 0, 'oog': 0, 'score': score, 'errors': []} if error: row['reverts'] += int(not oog) row['oog'] += int(oog) row['errors'].append({'callback': 'init', 'error': error}) return row row['init_diff_bps'] = max(diffs(actual, recorder.init_fees)) for index, (args, expected) in enumerate(recorder.rows): actual, gas, error, oog = invoke(evm, address, SWAP_SELECTOR, args) if error: row['reverts'] += int(not oog) row['oog'] += int(oog) row['errors'].append({'callback': index, 'error': error}) # A failed callback leaves the contract state inconsistent with the trace. break ds = diffs(actual, expected) row['max_bps'] = max(row['max_bps'], *ds) row['sum_bps'] += sum(ds) row['callbacks'] += 1 row['comparisons'] += 2 row['pyrevm_peak'] = max(row['pyrevm_peak'], gas) row['gas_sum'] += gas return row def summarize(rows, contract): ncb = sum(r['callbacks'] for r in rows) ncmp = sum(r['comparisons'] for r in rows) maximum = max(rows, key=lambda r: r['max_bps']) peak = max(rows, key=lambda r: r['pyrevm_peak']) return {'contract': contract, 'seeds': [r['seed'] for r in rows], 'runs': len(rows), 'callbacks': ncb, 'comparisons': ncmp, 'max_bps': maximum['max_bps'], 'max_seed': maximum['seed'], 'mean_bps': sum(r['sum_bps'] for r in rows) / ncmp if ncmp else None, 'init_diff_bps': max((r['init_diff_bps'] for r in rows if r['init_diff_bps'] is not None), default=None), 'pyrevm_peak': peak['pyrevm_peak'], 'pyrevm_peak_seed': peak['seed'], 'pyrevm_mean': sum(r['gas_sum'] for r in rows) / ncb if ncb else None, 'reverts': sum(r['reverts'] for r in rows), 'oog': sum(r['oog'] for r in rows)} def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('sol', type=Path) parser.add_argument('reference', type=Path) parser.add_argument('seeds', help='start:stop (stop exclusive)') args = parser.parse_args() start, stop = map(int, args.seeds.split(':')) assert stop > start directory = args.reference.resolve().parent reference = load_module(args.reference.resolve(), 'gatef_reference') replica = load_module(directory / 'replica2.py', 'gatef_replica') bytecode = compile_contract(args.sol) rows = [] for seed in range(start, stop): row = run_seed(seed, reference, replica, directory / 'blob.bin', bytecode) rows.append(row) print(json.dumps(row, sort_keys=True), flush=True) # Checkpoint every seed so partial work is auditable. output = {'report': summarize(rows, str(args.sol)), 'per_run': rows} Path(f'scratch/gatef_{start}_{stop}.json').write_text(json.dumps(output, indent=2) + '\n') report = summarize(rows, str(args.sol)) print(json.dumps(report, sort_keys=True), flush=True) ok = (report['callbacks'] == (stop - start) * CALLS and not report['reverts'] and not report['oog'] and report['max_bps'] <= .01 and report['init_diff_bps'] is not None and report['init_diff_bps'] <= .01 and report['pyrevm_peak'] <= GAS) return 0 if ok else 1 if __name__ == '__main__': sys.exit(main()) ``` ## Commands Commands below are the shell commands issued, grouped by experiment phase; `R` denotes the repository root, `A` the `test/scratch/amm-challenge` checkout, and `T` the `test/scratch/amm-sim/research/solved` directory. These labels document working directories, not additional shell commands. Polling used the process tool, with no shell sleep commands. File creation by heredoc is recorded with its payload in the relevant code block. The setup and every replay command completed within its eight-minute bound. Initial and final replay logs have distinct names; the final JSON checkpoints replace the initial checkpoints. R: ```sh pwd; git status --short; mkdir -p test/scratch git clone https://github.com/benedictbrady/amm-challenge test/scratch/amm-challenge && git -C test/scratch/amm-challenge checkout 6b6921f8d463314aa7c96234e1be25a25245f704 && git clone https://github.com/gtmcknight/amm-sim test/scratch/amm-sim && git -C test/scratch/amm-sim checkout 4812a6382311624e1506aca131ca35c378f930d9 cat test/scratch/amm-sim/setup-fast.sh; rg --files test/scratch/amm-sim/research/solved; curl -fLsS https://api.imdnotdead.fun/artifacts/8a6e303b662e1a5ea9bb47f31acedd3988ef65b3ddb66d074517aa59ad41ca40 -o test/scratch/Q.md; curl -fLsS https://api.imdnotdead.fun/artifacts/17d249ab0c33fc44931d9b105cf3f9e3846a560bd518b67310c2a56defacdecc -o test/scratch/Data.md ``` A (run exactly once): ```sh sh ../amm-sim/setup-fast.sh ``` R: ```sh cat test/scratch/amm-sim/research/solved/fixedcheck2.py; head -65 test/scratch/Q.md; head -15 test/scratch/Data.md rg -n '^##|sha256|class QMRef|def init|def after|def swap' test/scratch/Q.md; cat test/scratch/amm-challenge/contracts/src/IAMMStrategy.sol; cat test/scratch/amm-sim/research/solved/replica2.py sed -n '350,510p' test/scratch/Q.md; rg -n 'EVM\(|deploy\(|message_call|gas_used|compile_standard' test/scratch/amm-challenge/amm*; rg -n '^## Data' test/scratch/Data.md; cat test/scratch/amm-sim/research/solved/gates.py cat test/scratch/amm-challenge/amm_competition/evm/executor.py; sed -n '75,140p' test/scratch/amm-challenge/amm_competition/evm/compiler.py; sed -n '65,79p' test/scratch/Q.md; sed -n '247,254p' test/scratch/Data.md ``` T (unmodified Gate P): ```sh python3 -I gates.py ``` R: create and run the preparation script using the complete block below: ```sh cat > test/scratch/prepare.py <<'PY' # Credit: Taylor (gtmcknight), policy, replica and qm tables. import pathlib, re, hashlib, base64, shutil root=pathlib.Path(__file__).resolve().parent out=root/'amm-challenge'/'scratch' q=(root/'Q.md').read_text() for heading,name,lang in [('## Strategy.sol','Strategy.sol','solidity'),('## Python reference','qmref.py','python')]: section=q[q.index(heading):] block=re.search(r'```'+lang+r'\n(.*?)```',section,re.S).group(1) (out/name).write_text(block) print(name,len(block.encode()),hashlib.sha256(block.encode()).hexdigest()) assert hashlib.sha256((out/'Strategy.sol').read_bytes()).hexdigest()=='c68887cff61180e12ba2b1ceb333edf76a830f91f63b5d775a8806fb9ec08aca' d=(root/'Data.md').read_text(); d=d[d.index('## Data:'):] b=base64.b64decode(re.search(r'```\n(.*?)```',d,re.S).group(1)) assert len(b)==10167 assert hashlib.sha256(b).hexdigest()=='3ac7cb8811ecfb6a17f499aa4e9b70dd55f7fb6432b3aec3b904e11a3dece38e' (out/'blob.bin').write_bytes(b) print('blob.bin',len(b),hashlib.sha256(b).hexdigest()) shutil.copy(root/'amm-sim'/'research'/'solved'/'replica2.py',out/'replica2.py') PY python3 -I test/scratch/prepare.py ``` A (API inspection and reference checks): ```sh . scratch/env.sh && python -c "from pyrevm import EVM; e=EVM(); print(dir(e)); print(e.result); print(type(e.result))" ``` R: ```sh sed -n '1,85p' test/scratch/amm-challenge/amm_competition/evm/compiler.py; head -35 test/scratch/amm-sim/research/solved/README.md; sed -n '502,518p' test/scratch/Q.md; rg -n 'def on_trade|return self.fb' test/scratch/amm-challenge/scratch/qmref.py ``` A: ```sh . scratch/env.sh && python3 -I -c "import sys; sys.path.insert(0,'scratch'); import replica2,qmref; p='scratch/blob.bin'; [print(s,format(replica2.run(s,qmref.QMRef(p))['edge'],'.6f'),flush=True) for s in range(10000,10003)]" . scratch/env.sh && python -c "from pyrevm import EVM; e=EVM(); a=e.deploy(deployer='0x2000000000000000000000000000000000000002',code=bytes.fromhex('6001600c60003960016000f300'),gas=10000000); print(e.result); print(dir(e.result)); print(e.message_call(caller='0x2000000000000000000000000000000000000002',to=a,calldata=b'',gas=245000)); print(e.result)" ``` The tiny STOP-contract probe above only discovers the pyrevm gas result API. It does not stand in for qt1 or any requester-owned deployment. R: `cat > test/scratch/amm-challenge/scratch/check.py <<'PY'` wrote the initial harness. Its payload was the complete Harness block above, with two initial recording lines replaced as shown below. This preserves the initial implementation as well as the delivered correction without duplicating the entire file. ```python # Initial init recording: self.init_fees = self.policy.init(*args) # Initial callback append (before effective-fee clamping): self.rows.append(((int(is_buy), int(ax * WAD), int(ay * WAD), int(t), int(x * WAD), int(y * WAD)), tuple(fees))) # The final comment about replica2.clampf was absent initially. ``` A (initial batches, later batches launched independently in parallel): ```sh . scratch/env.sh && timeout 470 python3 -I scratch/check.py scratch/Strategy.sol scratch/qmref.py 70000:70010 > scratch/gatef_70000_70010.log 2>&1 . scratch/env.sh && timeout 470 python3 -I scratch/check.py scratch/Strategy.sol scratch/qmref.py 70010:70020 > scratch/gatef_70010_70020.log 2>&1 . scratch/env.sh && timeout 470 python3 -I scratch/check.py scratch/Strategy.sol scratch/qmref.py 70020:70030 > scratch/gatef_70020_70030.log 2>&1 . scratch/env.sh && timeout 470 python3 -I scratch/check.py scratch/Strategy.sol scratch/qmref.py 70030:70040 > scratch/gatef_70030_70040.log 2>&1 . scratch/env.sh && timeout 470 python3 -I scratch/check.py scratch/Strategy.sol scratch/qmref.py 70040:70050 > scratch/gatef_70040_70050.log 2>&1 ``` R (progress inspections issued between the batch launches): ```sh tail -3 test/scratch/amm-challenge/scratch/gatef_70000_70010.log; git status --short tail -2 test/scratch/amm-challenge/scratch/gatef_70000_70010.log ``` R: write and run the exact-source-quote checker: ```sh cat > test/scratch/verify_sources.py <<'PY' # Credit: Taylor (gtmcknight), original policy, replica and qm tables. import json, pathlib, urllib.request root = pathlib.Path(__file__).resolve().parent T = 'https://raw.githubusercontent.com/gtmcknight/amm-sim/4812a6382311624e1506aca131ca35c378f930d9/' A = 'https://raw.githubusercontent.com/benedictbrady/amm-challenge/6b6921f8d463314aa7c96234e1be25a25245f704/' sources = [ {'url':'https://api.imdnotdead.fun/artifacts/8a6e303b662e1a5ea9bb47f31acedd3988ef65b3ddb66d074517aa59ad41ca40', 'quote':'- **max |fee diff| 5.826e-06 bps**, mean 1.689e-07 bps, init diff 0. The target is ≤ 0.01 bps: **pass**.'}, {'url':'https://api.imdnotdead.fun/artifacts/17d249ab0c33fc44931d9b105cf3f9e3846a560bd518b67310c2a56defacdecc', 'quote':'## Data: base64 of the 10,167-byte blob (sha256 3ac7cb88…38e)'}, {'url': T+'setup-fast.sh', 'quote':'uv pip install -q "$WHEEL" "numpy>=1.24" "py-solc-x==2.1.0" "pyrevm==0.3.7"'}, {'url': T+'research/solved/replica2.py', 'quote':'A policy gets a callback after every trade on its own pool (arb or retail), like afterSwap,'}, {'url': T+'research/solved/gates.py', 'quote':'"""Per-seed check values for a port: edge of each reference policy on replica seeds 10000..10002.\nusage: python3 -I gates.py"""'}, {'url': A+'contracts/src/IAMMStrategy.sol', 'quote':'/// @dev Fees are returned as WAD values (1e18 = 100%, 1e15 = 0.1% = 10bps)'}, {'url': A+'amm_competition/evm/compiler.py', 'quote':' "viaIR": True,\n "evmVersion": "paris",'}, {'url': A+'amm_competition/evm/executor.py', 'quote':' SELECTOR_AFTER_INITIALIZE = bytes.fromhex("837aef47")\n SELECTOR_AFTER_SWAP = bytes.fromhex("c2babb57")'}, {'url': T+'research/solved/README.md', 'quote':'10000 448.261189 443.455598 443.522608 443.663520'}, {'url': A+'contracts/src/AMMStrategyBase.sol', 'quote':' /// @notice Maximum allowed fee: 10% (1e17)\n uint256 public constant MAX_FEE = WAD / 10;'}, ] for source in sources: data=urllib.request.urlopen(source['url'],timeout=60).read().decode() assert source['quote'] in data, source['url'] assert len(source['quote']) <= 500 (root/'sources.verified.json').write_text(json.dumps({'sources':sources},indent=2,ensure_ascii=False)+'\n') print('Verified all',len(sources),'exact quotes against freshly downloaded URLs.') PY python3 -I test/scratch/verify_sources.py ``` R (additional inspections): ```sh tail -1 test/scratch/amm-challenge/scratch/gatef_70000_70010.log; tail -1 test/scratch/amm-challenge/scratch/gatef_70010_70020.log; tail -1 test/scratch/amm-challenge/scratch/gatef_70030_70040.log cat test/scratch/amm-challenge/scratch/gatef_70020_70030.log ``` A (identify the out-of-range reference return): ```sh . scratch/env.sh && python3 -I -c "import sys; sys.path.insert(0,'scratch'); import replica2,qmref,check; r=check.Recorder(qmref.QMRef('scratch/blob.bin')); replica2.run(70026,r); print([(i,a,f) for i,(a,f) in enumerate(r.rows) if any(v<0 or v>0.1 for v in f)])" ``` R (repair the recorder to use the replica's effective fees): ```sh python3 -I - <<'PY' from pathlib import Path p=Path('test/scratch/amm-challenge/scratch/check.py') s=p.read_text().replace(" self.init_fees = self.policy.init(*args)\n", " self.init_fees = tuple(min(.1, max(0., f)) for f in self.policy.init(*args))\n") s=s.replace(" int(t), int(x * WAD), int(y * WAD)), tuple(fees)))", " int(t), int(x * WAD), int(y * WAD)),\n tuple(min(.1, max(0., f)) for f in fees)))") s=s.replace(" return fees\n\n\ndef invoke", " # replica2.clampf applies this same cap before fees affect a pool.\n return fees\n\n\ndef invoke") p.write_text(s) PY ``` A (final batches, independent processes, shared read-only compilation cache): ```sh . scratch/env.sh && timeout 470 python3 -I scratch/check.py scratch/Strategy.sol scratch/qmref.py 70000:70010 > scratch/gatef_70000_70010_final.log 2>&1 . scratch/env.sh && timeout 470 python3 -I scratch/check.py scratch/Strategy.sol scratch/qmref.py 70010:70020 > scratch/gatef_70010_70020_final.log 2>&1 . scratch/env.sh && timeout 470 python3 -I scratch/check.py scratch/Strategy.sol scratch/qmref.py 70020:70030 > scratch/gatef_70020_70030_final.log 2>&1 . scratch/env.sh && timeout 470 python3 -I scratch/check.py scratch/Strategy.sol scratch/qmref.py 70030:70040 > scratch/gatef_70030_70040_final.log 2>&1 . scratch/env.sh && timeout 470 python3 -I scratch/check.py scratch/Strategy.sol scratch/qmref.py 70040:70050 > scratch/gatef_70040_70050_final.log 2>&1 ``` R: create the aggregation script: ```sh cat > test/scratch/aggregate.py <<'PY' # Credit: Taylor (gtmcknight), policy, replica and qm tables. import importlib.util, json, pathlib root=pathlib.Path(__file__).resolve().parent scratch=root/'amm-challenge'/'scratch' spec=importlib.util.spec_from_file_location('gatef_check',scratch/'check.py') # This script is run with the challenge venv and its scratch env sourced. m=importlib.util.module_from_spec(spec); spec.loader.exec_module(m) rows=[] for start in range(70000,70050,10): p=scratch/f'gatef_{start}_{start+10}.json' data=json.loads(p.read_text()) assert data['report']['runs']==10 rows.extend(data['per_run']) assert [r['seed'] for r in rows]==list(range(70000,70050)) assert all(r['callbacks']==2000 and r['comparisons']==4000 and not r['errors'] for r in rows) report=m.summarize(rows,'qt1 Strategy.sol (sha256 c68887cff61180e12ba2b1ceb333edf76a830f91f63b5d775a8806fb9ec08aca)') assert report['max_bps']<=5.83e-6 assert report['pyrevm_peak']==194051 and report['pyrevm_peak_seed']==70032 assert round(report['pyrevm_mean'])==124777 assert report['init_diff_bps']==0 and report['reverts']==report['oog']==0 (scratch/'gatef_final.json').write_text(json.dumps({'report':report,'per_run':rows},indent=2)+'\n') print(json.dumps(report,indent=2)) PY ``` A: ```sh . scratch/env.sh && python -c "import sys,importlib.metadata as m,solcx,amm_sim_rs; print(sys.version.split()[0]); print({p:m.version(p) for p in ('pyrevm','py-solc-x','numpy')}); print(solcx.get_solc_version(solcx_binary_path=None) if False else solcx.get_installed_solc_versions()); print('gas_stats available:',callable(amm_sim_rs.gas_stats))" ``` R: ```sh tail -1 test/scratch/amm-challenge/scratch/gatef_70020_70030_final.log; tail -1 test/scratch/amm-challenge/scratch/gatef_70040_70050_final.log ``` A: ```sh . scratch/env.sh && python3 -I ../aggregate.py ``` R (report assembly and deliverable checks): ```sh # Command ledger creation: # cat > test/scratch/commands.md <<'EOF' (ledger text above) # First assembly script creation used cat > test/scratch/write_result.py <<'PY'. # Its delimiter collided with an embedded Python example, causing shell exit 127. # This only truncated the scratch report writer; it did not change or rerun any replay. tail -8 test/scratch/write_result.py; git status --short --untracked-files=all # Repaired the truncated writer with: # cat >> test/scratch/write_result.py <<'IMD_REPORT_ASSEMBLY_END_872934' # Then ran: python3 -I test/scratch/write_result.py # Validation script creation: # cat > test/scratch/validate_result.py <<'IMD_VALIDATION_END_872934' python3 -I test/scratch/validate_result.py python3 -I test/scratch/verify_sources.py git status --short --untracked-files=all # Additional read-only report/source audits: head -35 test/scratch/amm-challenge/contracts/src/AMMStrategyBase.sol; sed -n '1,35p' test/scratch/amm-sim/research/solved/README.md; sed -n '100,155p' artifacts/goal/batches/e1-gate-f/items/gf-a/result.md rg -n '443.522608|Per-seed|gates.py' test/scratch/amm-sim/research/solved/README.md; git diff --stat; git status --short --untracked-files=all # Applied scratch-only source-list/ledger corrections with apply_patch, then regenerated: python3 -I test/scratch/verify_sources.py && python3 -I test/scratch/write_result.py python3 -I test/scratch/validate_result.py git status --short --untracked-files=all ``` The report writer formats measured JSON, embeds the complete harness and helper scripts, copies the verified source list and checks byte caps. Validation independently sums all per-run measurements and parses the embedded harness and JSON. Tool polling of running processes used no shell command. No engine benchmark commands or sleeps were issued. ## Problems The first implementation compared raw QMRef return values. Four initial batches passed, while `70020:70030` exited 1 on a comparison discrepancy (no revert or OOG). At seed 70026, zero-based callback 1702 (timestamp 1568), QMRef returned `(0.12000793358465078, 0.0)`. The [replica](https://github.com/gtmcknight/amm-sim/tree/4812a6382311624e1506aca131ca35c378f930d9/research/solved/replica2.py) applies `clampf` to every returned fee before pool use, and [QMRef](https://api.imdnotdead.fun/artifacts/8a6e303b662e1a5ea9bb47f31acedd3988ef65b3ddb66d074517aa59ad41ca40) also stores capped fees internally. qt1 correctly returned the capped 0.1 bid fee. Comparing raw 0.12000793358465078 to 0.1 produced a spurious 200.07933584650777-bps error. Repaired the recorder to store the effective `[0, 0.1]` reference fees while leaving callback inputs, the reference implementation, simulator execution, and qt1 unchanged. Reran **all five batches / all 50 seeds** with the repaired harness; every final batch exited 0. Only those complete final reruns enter the reported results. This correction is required to compare the contract to the fees actually used by the reference-driven replica. Report assembly initially failed with shell exit 127 because its heredoc delimiter collided with an embedded Python example. The scratch writer was repaired with a distinct delimiter; simulator inputs, harness, measured replay results and repository configuration were unaffected. No setup blocker remained. The check is deterministic replay equivalence on the requested replica traces. No new policy was implemented, no Gate Q selection was performed, and no real-engine seeds were consumed. The reported gas is pyrevm gas, not `amm_sim_rs.gas_stats()` engine gas. All measurements are local and reproducible from the included harness and pinned sources; the verifier's path/byte check does not independently certify their behavior.