Conversation
There was a problem hiding this comment.
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>
There was a problem hiding this comment.
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.
| COALESCE( | ||
| (SELECT LEAST($21, valid_to) FROM ethflow_orders WHERE uid = $1), | ||
| $21 | ||
| ) |
There was a problem hiding this comment.
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 = $2There was a problem hiding this comment.
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
Description
true_valid_tois not always updated as needed. It can happen that ethflow order events are parsed first, before order creation takes place which results intrue_valid_toto first be set correctly, and then set tou32::MAXwhen inserting the order.Changes
Adds a clause
to INSERT_ORDER query which will always take the smallest of
ethflow_orders.valid_toand the value currently being inserted.How to test
Tested on ethflow E2E test
local_node_eth_flow_txconfirmed the faulty behaviour can occur and added assert that ensures the resulting true_valid_to is notu32::MAX