Fix refund vault selection
Summary
This MR fixes a refund vault-selection edge case where refundTx could force a refund from the original observed vault even when that vault's effective available balance was already consumed by pending outbounds. In edge cases, if the L1 wallet still had enough real balance due to an unconfirmed/unobserved inbound, that wrongly-selected refund could even be signed and broadcast. When the refund was later observed before the inbound was credited to vault accounting, THORNode attempted to debit more than the recorded vault balance and hit the Vault.SubFunds / SafeSub clamp path.
The fix checks the observed vault balance after pending outbound deduction and gas reservation. If the observed vault cannot safely fund the refund, the preselected vault is cleared so normal txout discovery can choose another available vault.
Relation to MR !4802 (merged)
This MR is built on top of !4802 (merged).
MR !4802 (merged) handles the downstream behavior when Vault.SubFunds would clamp during outbound observation: it detects the clamp and halts instead of allowing the accounting gap to persist silently.
This MR addresses the upstream root cause for new refunds: avoiding creation of a refund txout from a vault that only appears funded because pending outbounds have not yet been observed and deducted from vault accounting.
Tests
Added test coverage for:
- refund scheduling skipping the observed vault when pending outbounds make its effective balance insufficient
- rerouting the refund to another vault so later outbound observation does not hit the clamp path
- reproducing the pre-fix incident path where an already-scheduled refund observes after the vault balance was drained
Incident Shape
(https://discord.com/channels/838986635756044328/1500974310003839086)
The incident can be understood as a mismatch between vault balance and L1 wallet balance during an in-flight refund.
The example below uses the real vtxv incident values where they are known. The key edge case is that the L1 wallet balance could be temporarily higher than the vault balance because of an inbound that existed on L1 but had not yet been credited into THORNode vault accounting. That made it possible for the wrongly-selected refund to actually be signed and broadcast.
| Step | Event | Vault balance | L1 wallet balance | Notes |
|---|---|---|---|---|
| 1 | Calm starting point | ~269.82166704 ETH |
~269.82166704 ETH |
Vault accounting and the L1 wallet are aligned. |
| 2 | Large inbound swap F0DFC7...D4D4 reaches vtxv |
~569.82166233 ETH once observed/accounted |
~569.82166233 ETH on L1 |
Large inbound swap with a price limit: +299.99999529 ETH. This produced both a BTC outbound and an ETH refund path. |
| 3 | Other vtxv outbounds are scheduled/committed |
still ~569.82166233 ETH |
still ~569.82166233 ETH |
Scheduling does not debit vault balance yet. Approx later drain: ~553.2146 ETH, or ~553.2205 ETH including gas. |
| 4 | Refund 95DBC7...92DC is created/kept on the same observed vault |
still ~569.82166233 ETH nominally |
still ~569.82166233 ETH |
Pre-fix, refundTx forces the refund back to the observed active/retiring vault. It does not check effective available balance after pending outbounds. Refund amount: 169.23065786 ETH. |
| 5 | Earlier scheduled vtxv outbounds are signed/broadcast/observed before the refund |
drops to about 16.60117573 ETH before refund observation |
also reduced by the real L1 sends | The refund is still pending, but the vault balance has now been mostly consumed by other outbounds. |
| 6 | Second large inbound 17F75C...2DC2 reaches L1 but is not yet credited to vault accounting |
still about 16.60117573 ETH |
about 316.60117573 ETH |
+300 ETH exists in the L1 wallet, but THORChain accounting has not credited it yet. |
| 7 | Refund 95DBC7...92DC is signed/broadcast on Ethereum |
unchanged until observation | about 147.37042866 ETH |
L1 sends 169.23065786 ETH plus ~0.00008921 ETH gas. Without the unobserved inbound, this refund likely could not be sent. |
| 8 | Refund is later observed by THORNode | gas first: 16.60117573 - 0.00008921 = 16.60108652 ETH |
already spent on L1 | THORNode now accounts for the real L1 send. |
| 9 | Vault.SubFunds debits the refund coin amount |
attempts 16.60108652 - 169.23065786 ETH, clamps to 0 |
already spent on L1 | SafeSub clamps instead of recording the missing amount. |
| 10 | Accounting gap appears | missing ~152.62957134 ETH |
no matching credited L1 funds remain | This appears as vault insolvency / phantom credit in THORNode accounting. |
Relevant txs:
-
Large inbound
F0DFC7...D4D4: -
Second large inbound
17F75C...2DC2: -
Refund/outbound
95DBC7...92DC:
This MR prevents the upstream scheduling mistake for new refunds by not forcing the refund from the observed vault when pending outbounds make that vault effectively underfunded.