Skip to content

fix(worker): don't send an offer that is already on-chain - #855

Closed
cjorge-thegraph wants to merge 1 commit into
mainfrom
cjorge/skip-an-offer-already-made
Closed

cjorge-thegraph wants to merge 1 commit into
mainfrom
cjorge/skip-an-offer-already-made

Conversation

@cjorge-thegraph

Copy link
Copy Markdown
Contributor

A submit_offer job can run again after its offer has mined. If dipper crashes, or its shutdown runs out of time for the job, the job's queue row rolls back and runs again on the next start. Nothing checked whether the offer was already made, so the job sent it again. That costs gas, plus whatever the contract does with a 2nd offerAgreement for an agreement it already holds.

Before sending, the job now asks the RecurringCollector for the agreement, through agreement_on_chain (getAgreementDetails), the same read the cancel paths already use. The agreement id is derived from the offer's terms, so the chain can show it only if this very offer landed:

  • Live (waiting to be accepted, or accepted) or ended by the indexer: the offer is already there. The job sends nothing. It still runs the after-send check, so if dipper cancelled the agreement meanwhile, the offer is still withdrawn.
  • Not live: the job sends as before.
  • The chain can't be asked: the job retries with the transient backoff rather than send blind.

Still sent again, so this is not full idempotency:

  • An offer still in the mempool when dipper stopped: it is not on-chain yet, so the check can't see it. Closing that needs the tx hash stored before the send, plus a receipt or nonce check on restart.
  • An offer already withdrawn, or past its deadline: the chain no longer tells these from one never made. Dipper's own withdrawals go through a cancel, so they take the withdraw path instead and don't reach here.

Open question for whoever owns the contracts: what does RecurringAgreementManager.offerAgreement do with a re-offer of an agreement id it has already seen, whether pending, accepted, withdrawn or expired? Does it revert, overwrite, or reserve escrow twice? This PR avoids the common case. The answer decides whether the remaining cases matter.

Tests (all three failed on main, with the mock's "sent when it must not" panic):

  • a_rerun_after_the_offer_mined_sends_nothing: a 1st run offers, and a 2nd run on the same chain state sends nothing.
  • sends_nothing_for_an_agreement_already_on_chain: for both Live and EndedByIndexer.
  • retries_without_sending_when_the_chain_cannot_be_asked: returns Retryable, and nothing is sent.
  • cargo +nightly fmt, cargo clippy --all-targets -D warnings and the workspace tests pass locally.

🤖 Generated with Claude Code

A `submit_offer` job can run again after its offer mined: dipper dropping it in a crash, or a
shutdown running out of time for it, rolls its queue row back, and the next start runs it again.
Nothing checked, so it sent the offer again, at the cost of the gas and of whatever the contract
does with a 2nd `offerAgreement` for an agreement it already holds.

Before sending, the job now asks the RecurringCollector for the agreement
(`getAgreementDetails`, as `agreement_on_chain` already does for cancels). Its id is derived from
the offer's terms, so if the chain shows it waiting to be accepted, accepted, or ended by the
indexer, this offer landed already: the job sends nothing, and still withdraws it if dipper
cancelled the agreement meanwhile. If the chain cannot be asked, the job retries later rather
than send blind.

2 cases still send again: an offer still in the mempool when dipper stopped, which is not on-chain
yet, and an offer already withdrawn or past its deadline, which the chain no longer tells from 1
never made.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cjorge-thegraph

Copy link
Copy Markdown
Contributor Author

Closing: checking the chain before sending makes a repeated offer less likely, but can't rule it out. An offer still in the mempool isn't visible yet, and a withdrawn or expired offer looks like one never made.

The proper fix is to record each offer as an intent, keyed by agreement, in the same transaction that moves the agreement on, and to send it from that record with a nonce assigned in the database. A re-run then finds the intent instead of sending again. That replaces this check rather than building on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant