Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix solver competition reading #3171

Merged
merged 2 commits into from
Dec 19, 2024
Merged

Conversation

sunce86
Copy link
Contributor

@sunce86 sunce86 commented Dec 18, 2024

Description

Fixes a bug where transaction hashes from different environment (prod/staging) are shown on solver competition endpoint.

Example: https://api.cow.fi/mainnet/api/v1/solver_competition/9861815

Here, transaction 0xb608f1e2a644befda65a6a87ce6b93fc8a3441d9729b8ce4b2a2db02c1247e60 is from production enviroment and valid one, while 0xc85cd0ad8e3aac389836a71da9795bdd1bedce7b21da1950b48f93165c9f6be8 is from staging environment and mistakenly added to result.

The reason why this happens is:

  1. The solver competition with the same auction_id can be saved in both environments, regardless if the competition ended up with mined settlement (which is fine).
  2. settlement::Observer saves the relationship <auction, settlement> even for settlements from another environment (this is a decision made long time ago, before refactoring, reference code here )

But, what this PR introduces is leveraging the fact that, for settlement from another environment, there is no matching entry in settlement_observations table, reference code here. Note how the saving is executed only if the settlement exists, which if false for settlements from another environment.

Changes

  • Update reading functions for solver competition, to exclude transaction hashes for settlements from another environment.

How to test

Updated unit test.
Existing e2e tests.

Manually tested all updated functions on production database, works as expected.

@sunce86 sunce86 added the bug Something isn't working label Dec 18, 2024
@sunce86 sunce86 requested a review from a team as a code owner December 18, 2024 12:21
FROM solver_competitions sc
-- outer joins because the data might not have been indexed yet
LEFT OUTER JOIN settlements s ON sc.id = s.auction_id
-- exclude settlements from another environment for which observation is guaranteed to not exist
LEFT OUTER JOIN settlement_observations so
Copy link
Contributor Author

Choose a reason for hiding this comment

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

LEFT OUTER JOIN makes sure there is no change in logic whether solver competition entry is returned or not

@@ -38,10 +38,14 @@ pub async fn load_by_id(
id: AuctionId,
) -> Result<Option<LoadCompetition>, sqlx::Error> {
const QUERY: &str = r#"
SELECT sc.json, sc.id, COALESCE(ARRAY_AGG(s.tx_hash) FILTER (WHERE s.tx_hash IS NOT NULL), '{}') AS tx_hashes
SELECT sc.json, sc.id, COALESCE(ARRAY_AGG(s.tx_hash) FILTER (WHERE so.block_number IS NOT NULL), '{}') AS tx_hashes
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Return transaction hash only if settlement_observation entry exists.

@@ -72,11 +80,17 @@ WITH competition AS (
SELECT sc.id
FROM solver_competitions sc
JOIN settlements s ON sc.id = s.auction_id
JOIN settlement_observations so
Copy link
Contributor Author

Choose a reason for hiding this comment

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

JOIN because we don't want to return solver competition entry for a transaction that was settled in different environment.

Copy link
Contributor

@MartinquaXD MartinquaXD left a comment

Choose a reason for hiding this comment

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

Nice, a fix without having to update the db at all. 🚀

Also thanks for adding a test. 👍

@sunce86
Copy link
Contributor Author

sunce86 commented Dec 18, 2024

The only drawback of this solution is that settlement_observation table exists from 1 March of 2023, so for competitions ran before that date, this logic will not work 100% (transaction hashes will be missing).

But I think this is a trade off we can live with.

@MartinquaXD
Copy link
Contributor

I agree that this drawback shouldn't block this fix.

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.

The only drawback of this solution is that settlement_observation table exists from 1 March of 2023, so for competitions ran before that date, this logic will not work 100% (transaction hashes will be missing).

That’s a very insightful remark.

@sunce86 sunce86 enabled auto-merge (squash) December 19, 2024 08:02
@sunce86 sunce86 merged commit bf213b9 into main Dec 19, 2024
11 checks passed
@sunce86 sunce86 deleted the fix-solver-competition-endpoint branch December 19, 2024 08:08
@github-actions github-actions bot locked and limited conversation to collaborators Dec 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants