Skip to content

Don't consider inflight orders invalid#4239

Open
fafk wants to merge 3 commits intomainfrom
fix-invalid-event
Open

Don't consider inflight orders invalid#4239
fafk wants to merge 3 commits intomainfrom
fix-invalid-event

Conversation

@fafk
Copy link
Contributor

@fafk fafk commented Mar 6, 2026

Description

When I was working on another feature (the debug endpoint) I noticed that there is a race condition where we sometimes insert "invalid" order event to the DB.

From logs

09:19:13.717  stored order events label=Executing count=1
09:19:13.733  filtered orders reason=insufficient_balance count=1 orders=[0x060a...]
09:19:13.737  stored order events label=Invalid count=1
09:19:13.756  stored order events label=Traded count=1

Order is in executing state, then settles onchain. If the indexing runs now and we set the order to traded all is well. However if the in cache update runs first it's possible the wallet no longer has the necessary balance as it was traded away in the just executed swap and we consider it invalid and we insert the invalid event for an order that went through perfectly well.

Changes

  • Don't consider in-flight orders invalid

How to test

I had an e2e test to reproduce this which failed 6/10 times. After the fix it fails 0/10.

@fafk fafk requested a review from a team as a code owner March 6, 2026 09:27
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a fix for a race condition where an in-flight order could be incorrectly marked as invalid. The change involves fetching in-flight orders from the database and filtering them out from the sets of invalid and filtered orders before storing order events. The implementation is correct and effectively addresses the described issue. The error handling for fetching in-flight orders is graceful, falling back to the previous behavior in case of failure. No critical issues were found in this change.

Note: Security Review did not run due to the size of the PR.

@fafk fafk force-pushed the fix-invalid-event branch from ff1e96c to bc4d2bc Compare March 6, 2026 09:37
Copy link
Contributor

@jmg-duarte jmg-duarte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code and the idea make sense to me, but the comment makes everything confusing again

Comment on lines +295 to +298
// An in-flight order already won a previous auction and is being
// submitted on-chain. It may still fail filters here (e.g. expiry,
// missing native price), but emitting Invalid/Filtered for it would
// be misleading since it's about to become Traded.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit hard to read. Do you mean "in-flight order that already won"? And what filters do you mean?

If the order is submitted on chain and fails, why would placing an invalid be misleading?

Copy link
Contributor Author

@fafk fafk Mar 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the order is submitted on chain and fails, why would placing an invalid be misleading?

Because there is nothing wrong with the order. The solver probably set too low a gas price so the submitted order didn't settle.

Let me tweak the wording a bit though.

Copy link
Contributor

@squadgazzz squadgazzz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we exclude in-flight orders from the cache before we even store any events?

@fafk fafk force-pushed the fix-invalid-event branch from 21b033c to cf88798 Compare March 6, 2026 12:26
@fafk
Copy link
Contributor Author

fafk commented Mar 6, 2026

Should we exclude in-flight orders from the cache before we even store any events?

A just question. I don't know; I think we might want to filter them, but I imagine we might want solvers to keep producing solutions in case the settlement doesn't land on chain.

@squadgazzz
Copy link
Contributor

squadgazzz commented Mar 6, 2026

I mean, something seems off to me. I see 2 options:

  1. Move the store events operation and do that only after the in-flight orders are filtered out.
  2. Move the filtering logic before the event storing.

I am more in favor of the first option, but I haven't dived deeper to understand all the implications.

@fafk
Copy link
Contributor Author

fafk commented Mar 6, 2026

I mean, something seems off to me. I see 2 options:

  1. Move the store events operation and do that only after the in-flight orders are filtered out.
  2. Move the filtering logic before the event storing.

I am more in favor of the first option, but I haven't dived deeper to understand all the implications.

Okay, I think we can do this: https://github.com/cowprotocol/services/pull/4241/changes – instead of filtering in-flight orders later when cutting the auction we can just omit them when building the live orders cache. That change feels a bit less safe, but it's the healthier approach for the codebase & performance (we don't look up in-flight orders twice). What say you?

@fafk fafk mentioned this pull request Mar 6, 2026
@fafk fafk force-pushed the fix-invalid-event branch from cf88798 to c9d75e9 Compare March 6, 2026 14:20
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.

3 participants