perf(thorchain): POL add reads the rune pool value only after the deposit succeeds
🔎 Visual review of9199ae73: explainer, review flags and prompts to ask an AI about the change
Summary
Every block, NetworkMgr.POLCycle tries a POL add for each POL pool that is under target. addPOLLiquidity computed the reserve's share of the RUNEPool, which needs the full POL value (polPoolValue: every pool's synth supply and the reserve's LP position). It did that before running the deposit. On mainnet nearly every one of those adds fails validation (e.g. paused deposits), and the value was thrown away. polValueMemo already cuts this to about one computation per block, but that one computation was still the most expensive part of POLCycle.
The deposit now runs in a cache context. The value is read from ctx only once the deposit succeeds, so it still reflects the state from before the deposit, exactly as before. The reserve-to-asgard send that precedes the deposit changes none of what the value reads: pools, synth supply, the reserve's LP record and the rune_pool module balance.
Risk: medium (EndBlock, POL accounting). State, events and gas_used are unchanged; see Evidence.
What changes
manager_network_current.go addPOLLiquidity:
- It checks the balance and sends RUNE to asgard as before, then runs the add-liquidity handler in
depositCtx, actx.CacheContext(). - On success it reads
polValue.runePoolValue(ctx, …)fromctx, where the deposit is not yet visible, then commitsdepositCtx. The deposit's events are then emitted, in the same order as before. - On a handler error it refunds the send, as before; the discarded cache context holds nothing.
Consensus and gas
addPOLLiquidityis only reached throughPOLCycle←NetworkMgr.EndBlock←AppModule.EndBlock. It never runs inside a transaction, so tx gas can't change.- A reviewer subagent compared the old and new code from the same state in seven scenarios: every add succeeds; the first, the middle, or all adds are paused; a zero
rune_poolbalance; zero RUNEPool units; a forced withdraw that resets the memo. It compared every thorchain store key and value, the module RUNE balances and the full ordered event list. All were identical. - One difference remains, and only when
polPoolValueitself errors, i.e. a stored pool or LP record fails to unmarshal. The old code returned before the send. The new code sends, runs the deposit, then refunds and discards it. The state is the same, but the EndBlock events gain one send/refund pair of transfer events. EndBlock events don't feedLastResultsHash. - If the balance is too low, the function now returns nil without computing the value. Its errors only reach a log line in
POLCycle.
Evidence
Mainnet replay (scripts/perf/mainnet-replay.sh, 1,999 blocks 28124001..28125999, replayed through the CometBFT handshake, which checks every block's app hash against mainnet). The same change was applied to the v3.20.3-based replay lab stack (which includes !5274 (merged)/!5280 (merged)/!5281 (merged)), with 3 interleaved rounds per side. CPU profiles summed over the 3 rounds:
| before | after | Δ | |
|---|---|---|---|
addPOLLiquidity |
33.6 s | 7.7 s | −77% |
POLCycle |
44.8 s | 20.0 s | −55% |
NetworkMgr.EndBlock |
79.5 s | 55.8 s | −30% |
AppModule.EndBlock |
280.4 s | 252.0 s | −10% |
FinalizeBlock |
534.3 s | 504.7 s | −5.5% |
polPoolValue dropped out of the profile entirely: no POL add succeeded in this range.
Wall time: 258.4 → 243.5, 249.1 → 248.9, 249.3 → 246.4 s; median 124.7 → 123.2 ms/block (−1.2%). The host was shared with other jobs (1-min load 10–18), so I'd take the CPU numbers above as the effect.
scripts/perf/guard.sh (unit tests, then unfiltered regtest vs develop): PASS. All 2,481 regression output files are identical, including state, events and gas_used.
Tests
manager_network_current_test.go TestPOLLiquidityAddValueBeforeDeposit, with a RUNEPool that has pooler units and a POL LP position:
- A failed add (
PauseLPDeposit-BTC-BTC) leaves the reserve balance and units unchanged, and never computes the value (polValue.okstays false). This fails on the old code. - A successful add prices the reserve's new units at the value from before the deposit. The test asserts that the deposit raises the value, so a read after the commit would give different units. Afterwards the memo is reset.
How it was found
This came from round 8 of the mainnet replay profiling (rounds 4–7: !5274 (merged) to !5281 (merged)). After round 7, POLCycle was 8.6% of FinalizeBlock CPU in the replay, and 77% of it was runePoolValue inside adds that then failed.