CLI: detect stale pending_swap.json when miner is re-reserved#226
Merged
Conversation
The "you have a pending reservation" check trusted reserved_until > now, which conflates "miner is reserved by anyone" with "our reservation is still live". After a swap completed and another user re-reserved the same miner, the local file was reported as ACTIVE. probe_pending_reservation now (1) probes get_miner_active_swaps for our user addresses first — survives extension and into FULFILLED — then (2) compares reservation amounts and bounds reserved_until drift by ttl to distinguish a legitimate extension from a fresh replacement.
probe_pending_reservation was hitting get_miner_active_swaps on every call from alw status / swap now / view reservation. That helper scans the swap-id range backward up to max_gap=50 — wasted RPCs for the common case where the miner has no active swap. Gate it on get_miner_has_active_swap (single bool read). Also drop dead try/except around subtensor.get_current_block() in the two callers that wrapped it: the probe right above already exercised substrate via client.get_* calls, so the bare read can't fail in isolation.
f667f26 to
1d75d2c
Compare
anderdc
approved these changes
Apr 28, 2026
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
alw swap nowandalw view reservationwere treating any localpending_swap.jsonas live whenever the saved miner had an on-chainreserved_until > now. That conflates "this miner is reserved by anyone" with "our reservation is still live" — so when a swap completed and another user (or another process) re-reserved the same miner, the stale local row was reported as ACTIVE.Reported case: user completed swap #3, came back later,
alw swap nowwarned about a pending reservation,alw view reservationshowed it ACTIVE with~16 blocksremaining and the old BTC tx attached, butalw view swap 3 --watchcorrectly said the swap was already resolved.Approach
New
probe_pending_reservationhelper inswap_commands/helpers.pydecides among five outcomes:our_swap—get_miner_active_swapshas a swap on this miner whose(user_from_address, receive_address)match ours. Strongest signal; survivesvote_extend_reservationand intoFULFILLED.expired— no swap match and no reservation row.replaced— reservation amounts differ from saved.replaced— amounts match butreserved_untilis more thanreservation_ttlpast our saved value (a single extension can't push it that far, and chained extensions would have shown up as a swap in step 1).ours_active— within tolerance.Three call sites consume it:
swap.py(start-new prompt),view.py(full table),status.py(dashboard row). Each clearspending_swap.jsonand prints a one-line resolution when the saved state is no longer ours.Tradeoffs
The amount-tuple match in step 4 is almost unique-per-reservation but technically could collide if a different user re-reserves the same miner with the same exact amounts. The contract doesn't expose
Reservation.from_addr, so the amount tuple plus the ttl-bounded drift check is the best disambiguator without a contract change. In practice, step 1 catches every "stale local file" case once a tx is broadcast (the reservation transitions into a swap and the swap match wins). A contract getter forfrom_addrwould close the residual hole — deferring until the collision actually surfaces.Test plan
pytest tests/test_probe_pending_reservation.py— 9 cases covering all 5 branches, including the exact stale-after-completed-swap casepytest tests/) green — 330 passedruff format .+ruff check .cleanpending_swap.jsonbehind, re-reserve same miner with another wallet, verifyalw swap now/alw view reservation/alw statusall auto-clear instead of showing ACTIVE