-
Notifications
You must be signed in to change notification settings - Fork 91
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
ON s.block_number = so.block_number | ||
AND s.log_index = so.log_index | ||
WHERE sc.id = $1 | ||
GROUP BY sc.id | ||
;"#; | ||
|
@@ -52,10 +56,14 @@ pub async fn load_latest_competition( | |
ex: &mut PgConnection, | ||
) -> 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 | ||
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 | ||
ON s.block_number = so.block_number | ||
AND s.log_index = so.log_index | ||
GROUP BY sc.id | ||
ORDER BY sc.id DESC | ||
LIMIT 1 | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
ON s.block_number = so.block_number | ||
AND s.log_index = so.log_index | ||
WHERE s.tx_hash = $1 | ||
) | ||
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 | ||
FROM solver_competitions sc | ||
JOIN settlements s ON sc.id = s.auction_id | ||
JOIN settlement_observations so | ||
ON s.block_number = so.block_number | ||
AND s.log_index = so.log_index | ||
WHERE sc.id = (SELECT id FROM competition) | ||
GROUP BY sc.id | ||
;"#; | ||
|
@@ -336,7 +350,9 @@ mod tests { | |
// non-existent auction returns none | ||
assert!(load_by_id(&mut db, 1).await.unwrap().is_none()); | ||
|
||
// insert two settlement events for the same auction id | ||
// insert three settlement events for the same auction id, with one of them not | ||
// having observation (in practice usually meaning it's from different | ||
// environment) | ||
crate::events::insert_settlement( | ||
&mut db, | ||
&EventIndex { | ||
|
@@ -350,6 +366,16 @@ mod tests { | |
) | ||
.await | ||
.unwrap(); | ||
crate::settlement_observations::upsert( | ||
&mut db, | ||
crate::settlement_observations::Observation { | ||
block_number: 0, | ||
log_index: 0, | ||
..Default::default() | ||
}, | ||
) | ||
.await | ||
.unwrap(); | ||
crate::events::insert_settlement( | ||
&mut db, | ||
&EventIndex { | ||
|
@@ -363,12 +389,38 @@ mod tests { | |
) | ||
.await | ||
.unwrap(); | ||
crate::settlement_observations::upsert( | ||
&mut db, | ||
crate::settlement_observations::Observation { | ||
block_number: 0, | ||
log_index: 1, | ||
..Default::default() | ||
}, | ||
) | ||
.await | ||
.unwrap(); | ||
crate::events::insert_settlement( | ||
&mut db, | ||
&EventIndex { | ||
block_number: 0, | ||
log_index: 2, | ||
}, | ||
&Settlement { | ||
solver: Default::default(), | ||
transaction_hash: ByteArray([2u8; 32]), | ||
}, | ||
) | ||
.await | ||
.unwrap(); | ||
crate::settlements::update_settlement_auction(&mut db, 0, 0, 0) | ||
.await | ||
.unwrap(); | ||
crate::settlements::update_settlement_auction(&mut db, 0, 1, 0) | ||
.await | ||
.unwrap(); | ||
crate::settlements::update_settlement_auction(&mut db, 0, 2, 0) | ||
.await | ||
.unwrap(); | ||
|
||
// load by id works, and finds two hashes | ||
let value_ = load_by_id(&mut db, 0).await.unwrap().unwrap(); | ||
|
@@ -389,6 +441,11 @@ mod tests { | |
.unwrap() | ||
.unwrap(); | ||
assert!(value_.tx_hashes.len() == 2); | ||
// this one should not find any hashes since it's from another environment | ||
let value_ = load_by_tx_hash(&mut db, &ByteArray([2u8; 32])) | ||
.await | ||
.unwrap(); | ||
assert!(value_.is_none()); | ||
} | ||
|
||
#[tokio::test] | ||
|
There was a problem hiding this comment.
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.