Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions crates/autopilot/src/infra/persistence/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,19 +387,17 @@ impl Persistence {

let mut ex = self.postgres.pool.acquire().await?;

let order_uids: Vec<_> = auction
.orders
.iter()
.map(|order| ByteArray(order.uid.0))
.collect();

database::auction::save(
&mut ex,
database::auction::Auction {
id: auction.id,
block: i64::try_from(auction.block).context("block overflow")?,
deadline: i64::try_from(deadline).context("deadline overflow")?,
order_uids: order_uids.clone(),
order_uids: auction
.orders
.iter()
.map(|order| ByteArray(order.uid.0))
.collect(),
price_tokens: auction
.prices
.keys()
Expand All @@ -419,8 +417,6 @@ impl Persistence {
)
.await?;

database::auction::save_auction_orders(&mut ex, auction.id, &order_uids).await?;

Ok(())
}

Expand Down
20 changes: 1 addition & 19 deletions crates/database/src/auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,30 +110,12 @@ pub async fn get_order_uids(
Ok(record.map(|(order_uids,)| order_uids))
}

pub async fn save_auction_orders(
ex: &mut PgConnection,
auction_id: AuctionId,
order_uids: &[OrderUid],
) -> Result<(), sqlx::Error> {
const QUERY: &str = r#"
INSERT INTO auction_orders (auction_id, order_uid)
SELECT $1, unnest($2::bytea[])
ON CONFLICT DO NOTHING
"#;
sqlx::query(QUERY)
.bind(auction_id)
.bind(order_uids)
.execute(ex)
.await?;
Ok(())
}

pub async fn fetch_auction_ids_by_order_uid(
ex: &mut PgConnection,
order_uid: &OrderUid,
) -> Result<Vec<AuctionId>, sqlx::Error> {
const QUERY: &str =
"SELECT auction_id FROM auction_orders WHERE order_uid = $1 ORDER BY auction_id";
"SELECT id FROM competition_auctions WHERE order_uids @> ARRAY[$1::bytea] ORDER BY id";
let rows: Vec<(AuctionId,)> = sqlx::query_as(QUERY).bind(order_uid).fetch_all(ex).await?;
Ok(rows.into_iter().map(|(id,)| id).collect())
}
Expand Down
1 change: 0 additions & 1 deletion crates/database/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ pub const TABLES: &[&str] = &[

/// The names of potentially big volume tables we use in the db.
pub const LARGE_TABLES: &[&str] = &[
"auction_orders",
"auction_prices",
"competition_auctions",
"fee_policies",
Expand Down
1 change: 1 addition & 0 deletions database/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Contains all auctions for which a valid solver competition exists.
Indexes:
- PRIMARY KEY: btree(`id`)
- competition_auction_deadline: btree(`deadline`)
- competition_auctions_order_uids_gin: gin(`order_uids`)

### ethflow\_orders

Expand Down
3 changes: 3 additions & 0 deletions database/sql/V111__gin_index_orders_in_auction.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- index to efficiently find auctions which contained a given order
CREATE INDEX CONCURRENTLY IF NOT EXISTS competition_auctions_order_uids_gin
ON competition_auctions USING GIN (order_uids);
2 changes: 2 additions & 0 deletions database/sql/V112__drop_auction_orders.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- with the new index on `competition_auctions::order_uids` we don't need this redundant table anymore
DROP TABLE auction_orders;
9 changes: 4 additions & 5 deletions docs/COW_ORDER_DEBUG_SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -496,11 +496,10 @@ WHERE oq.order_uid = '\x$ORDER_UID_HEX';

### Check auction inclusion history:
```sql
SELECT
auction_id, order_uid, included, filtered_reason
FROM auction_orders
WHERE order_uid = '\x$ORDER_UID_HEX'
ORDER BY auction_id DESC
SELECT id AS auction_id
FROM competition_auctions
WHERE order_uids @> ARRAY['\x$ORDER_UID_HEX'::bytea]
ORDER BY id DESC
LIMIT 20;
```

Expand Down
Loading