Skip to content

Fix true_valid_to updating#4134

Open
m-sz wants to merge 9 commits intomainfrom
true-valid-to-fix
Open

Fix true_valid_to updating#4134
m-sz wants to merge 9 commits intomainfrom
true-valid-to-fix

Conversation

@m-sz
Copy link
Contributor

@m-sz m-sz commented Feb 6, 2026

Description

true_valid_to is not always updated as needed. It can happen that ethflow order events are parsed first, before order creation takes place which results in true_valid_to to first be set correctly, and then set to u32::MAX when inserting the order.

Changes

Adds a clause

COALESCE(
	(SELECT LEAST($21, valid_to) FROM ethflow_orders WHERE uid = $1),
	$21
)

to INSERT_ORDER query which will always take the smallest of ethflow_orders.valid_to and the value currently being inserted.

How to test

Tested on ethflow E2E test local_node_eth_flow_tx confirmed the faulty behaviour can occur and added assert that ensures the resulting true_valid_to is not u32::MAX

@m-sz m-sz changed the title Add log when inserting ethflow order Fix true_valid_to updating Feb 6, 2026
@m-sz m-sz marked this pull request as ready for review February 6, 2026 17:10
@m-sz m-sz requested a review from a team as a code owner February 6, 2026 17:10
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 addresses a race condition where true_valid_to for ethflow orders could be incorrectly set. The fix modifies the order insertion query to conditionally use the valid_to from the ethflow_orders table, which correctly handles out-of-order event processing. The logic is sound and is well-covered by a new end-to-end test assertion. I have one minor suggestion to improve a comment for clarity.

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@m-sz m-sz marked this pull request as draft February 6, 2026 17:31
@m-sz m-sz marked this pull request as ready for review February 6, 2026 17:55
@m-sz m-sz added the hotfix Labels PRs that should be applied into production right away label Feb 6, 2026
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 addresses a race condition where true_valid_to for an order could be incorrectly set. The fix modifies the INSERT query for orders to ensure the correct validity period is chosen if an ethflow_orders entry already exists. The accompanying E2E test verifies this fix. However, a similar critical issue exists on the UPDATE path in a related file, which is not addressed by this PR. I've added a comment detailing this issue and a suggested fix to ensure the logic is robust across all code paths.

Comment on lines +154 to +157
COALESCE(
(SELECT LEAST($21, valid_to) FROM ethflow_orders WHERE uid = $1),
$21
)
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

This change correctly handles true_valid_to on INSERT. However, a related race condition on the UPDATE path remains, which can lead to incorrect order validity.

The function insert_or_overwrite_ethflow_order in crates/database/src/ethflow_orders.rs uses a direct UPDATE which can overwrite a correct, smaller true_valid_to with a larger one. This is a critical issue as it can make expired orders appear solvable.

To ensure data consistency, the update logic should be consistent with this insert logic. Please also update the query in crates/database/src/ethflow_orders.rs to use LEAST and COALESCE to robustly handle NULL values and race conditions:

UPDATE orders
SET true_valid_to = LEAST(COALESCE(true_valid_to, $1), $1)
WHERE uid = $2

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The ethflow orders valid to is preferred when order is inserted. We only have to guard against a case when order is inserted after the ethflow event has been parsed which would previously result in the validity being u32::MAX

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

Labels

hotfix Labels PRs that should be applied into production right away

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant