feat(mempool): revm post-state replay fallback for V3 tick-crossing swaps - #158
Merged
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Adds aether_simulator::post_state_replay with replay_v3_post_state_rpc: forks pre-victim state via RpcForkedState, transact_commit the victim, then transact slot0() + liquidity() against the post-victim state and return a V3PostState the caller can feed into the graph-edge update. Curve and Balancer reader hooks intentionally stubbed and surface ReplayError::UnimplementedProtocol so the metric label space is already pinned for the follow-up PR.
Adds predict_post_state_with_replay sibling to predict_post_state_with_fallback. On low-confidence flag the new function invokes a caller-provided replay closure with the ReplayProtocol family; on Some the unified post-state proceeds, on None the candidate skips. Backwards-compatible — every existing call site of predict_post_state_with_fallback keeps its current shape.
Adds aether_mempool_post_state_replay_total{outcome} with pre-touched
labels (success / victim_reverted / victim_halted / read_call_failed /
decode_failed / sim_error / unimplemented_protocol / timeout) and
aether_mempool_post_state_replay_latency_ms histogram. Label space
matches ReplayError::as_str() so dashboards stay enumerable.
Replaces predict_post_state_with_fallback with the replay-aware sibling. On V3 tick-cross the pipeline now dispatches to replay_v3_post_state_rpc when MEMPOOL_POST_STATE_REPLAY=1 and a BackrunValidatorConfig provider is wired; otherwise the dormant behaviour from develop is preserved. Curve and Balancer escalations short-circuit to unimplemented_protocol because their reader hooks land in a follow-up PR. Concurrency is bounded by the existing sim_semaphore so a replay storm cannot push past the configured ceiling.
0xfandom
force-pushed
the
feat/revm-post-state-fallback
branch
from
May 25, 2026 19:13
5b720ac to
bb10975
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
aether_simulator::post_state_replaywithreplay_v3_post_state_rpc— forks pre-victim state, commits the victim, then readsslot0() + liquidity()to recover the V3 pool's analytical post-stateaether_pools::predict_post_state_with_replay— replay-aware sibling ofpredict_post_state_with_fallback; when the analytical predictor's confidence flag is low, dispatches into a caller-provided closure for the EVM-fork escalationMEMPOOL_POST_STATE_REPLAY=1; V3 tick-crossing swaps that today bumpaether_sim_evm_fallback_total{reason="v3_tick_crossed"}and skip now produce a realUnifiedPostState::UniswapV3from the forked EVM and continue through the candidate pathaether_mempool_post_state_replay_total{outcome}+aether_mempool_post_state_replay_latency_mshistogram with pre-touched labels so dashboards have stable series from cold startsim_semaphorefromBackrunValidatorConfigso replay concurrency is bounded by the same ceiling as the backrun validatorWhy
Three cases currently land in
predict_post_state_with_fallback's "low-confidence" branch and silently lose the candidate: V3 swaps that cross at least one tick, Curve StableSwap iterations that don't converge, and Balancer pools with unequal weights. The doc on the function explicitly flagged this as scaffolding ("Real EVM fork-replay implementation … lands once the simulator gains a generic 'apply this transaction and read post-state' entry point"). Without it, every multi-tick V3 swap on mainnet — the bulk of V3 traffic — sits in the metric and never produces a graph-edge update.This PR closes that gap for V3. The replay reads
slot0()directly from the forked EVM after the victim is committed, so the answer is exact rather than the bucket-clamped estimate the analytical predictor returns whensingle_tick = false.Files Changed
crates/simulator/src/post_state_replay.rsReplayErrortaxonomy + 5 unit testscrates/simulator/src/lib.rspub mod post_state_replay;crates/pools/src/lib.rspredict_post_state_with_replay+ReplayProtocolenum + 3 unit testscrates/grpc-server/src/metrics.rsmempool_post_state_replay_total{outcome}counter +mempool_post_state_replay_latency_mshistogram, pre-touched outcome labelscrates/grpc-server/src/mempool_pipeline.rspredict_post_state_with_fallbackcall with replay-aware sibling. Newtry_post_state_replayhelper handles the V3 fork → commit → read flow. 3 new pipeline-level tests.SimContext::with_post_state_replaybuilder + fieldcrates/grpc-server/src/main.rsMEMPOOL_POST_STATE_REPLAY=1env flag wires the bootstrap toggleImplementation notes
replay_v3_post_state_rpcis a synchronous function overRpcForkedState(the same shapevalidate_backrun_rpcalready uses). The pipeline calls it from aspawn_blockingworker —AlloyDB'sWrapDatabaseAsyncdoes the right thing viablock_in_placefrom within blocking workers.DynProvider<Ethereum>already attached toBackrunValidatorConfig. When that config is absent or has no provider the replay short-circuits and counts no outcome (no spurioussuccessbumps).try_acquire_ownedoncfg.sim_semaphore— saturation bumpssim_errorand skips, same backpressure semantics the backrun validator already uses.single_tick = trueon revm-derived V3PostState because the post-state is read directly from post-execution storage; the multi-tick precision concern that motivates the flag does not apply to revm-derived values.amount_out = U256::ZEROon the returnedV3PostState. The downstreamunified_to_post_reservesreadsnew_sqrt_price_x96only for V3, so this field is dead for graph-edge updates. Set to zero with a comment so it doesn't lie to a future caller that may read it.Scope cut
Curve and Balancer reader hooks are stubbed and surface
ReplayError::UnimplementedProtocol. Follow-up PR adds:balances(uint256 i)view interface + decode for both coin indices; newCurvePostStatemappinggetPoolTokens(bytes32 poolId)view call against the BalancerV2 Vault; newBalancerPostStatemappingunified_to_post_reservesarms andPredictedPostStatewriter variantsSplitting keeps each review surface ~300-400 LOC; the EVM-commit machinery is identical and validated by the V3 path here.
Acceptance criteria
predict_post_state_with_replayinvokes the replay closure on V3 tick-cross and uses its result whenSomepredict_post_state_with_replayskips the closure when the analytical predictor succeedsreplay_v3_post_state_rpcreturnsDecodeFailed("slot0")against a non-V3 pool addresstry_post_state_replayshort-circuits with no metric bump whenpost_state_replay_enabled = falsetry_post_state_replaybumpsunimplemented_protocolfor Curve and Balancer when enabledMEMPOOL_POST_STATE_REPLAYenv flag wires the bootstrap toggle without touching the develop defaultaether_mempool_post_state_replay_total{outcome="success"}increments under V3 tick-crossing traffic (follow-up smoke after merge)Test plan
cargo build --releasecleancargo clippy --workspace --all-targets --release -- -D warningscleancargo test --workspace --release -- --test-threads=1all green (5 new replayer unit tests + 3 new predictor tests + 3 new pipeline tests)go build ./...+go test ./... -count=1green (no Go changes, regression check)forge build+forge testgreen (no Solidity changes, regression check)MEMPOOL_POST_STATE_REPLAY=1— confirmaether_mempool_post_state_replay_total{outcome="success"}increments and thelatency_mshistogram populates