Skip to content

Commit

Permalink
Remove auction_transaction (#2283)
Browse files Browse the repository at this point in the history
# Description
Part of #2282 &
#2275

Exchanges `tx_from/tx_nonce` for `auction_id` which can be directly
encoded from the settlement transaction since we shipped colocation.
This allows us to remove an entire database table and a bunch of other
complicated code.

I'm not yet removing the old columns in case something goes bad with
this change.

# Changes
<!-- List of detailed changes (how the change is accomplished) -->

- [x] Removed `auction_transaction` table.
- [x] Redesigned `settlements` table to contain auction id

## How to test
1. db tests and e2e tests cover the new functionality.
2. Locally I've tested the db migration file like this:
- First comment out the V_58 migration file and apply old schema.
- Insert 10 settlements, of which first 5 have `auction_transaction`
record (except one settlement (block_number 3), to simulate the
existence of settlement from other environment staging/prod)
- Then, manually apply migration script V_58
- Observed the `settlements` table,`auction_id` were populated as
expected.

---------

Co-authored-by: Felix Leupold <[email protected]>
  • Loading branch information
sunce86 and fleupold authored Jan 26, 2024
1 parent 8f8f317 commit 0213ec8
Show file tree
Hide file tree
Showing 12 changed files with 257 additions and 446 deletions.
1 change: 0 additions & 1 deletion crates/autopilot/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use {

mod auction;
pub mod auction_prices;
pub mod auction_transaction;
pub mod competition;
pub mod ethflow_events;
mod events;
Expand Down
50 changes: 0 additions & 50 deletions crates/autopilot/src/database/auction_transaction.rs

This file was deleted.

48 changes: 15 additions & 33 deletions crates/autopilot/src/database/on_settlement_event_updater.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use {
anyhow::{Context, Result},
database::{byte_array::ByteArray, settlement_observations::Observation},
ethcontract::{H160, U256},
ethcontract::U256,
model::order::OrderUid,
number::conversions::u256_to_big_decimal,
sqlx::PgConnection,
Expand All @@ -13,20 +13,19 @@ pub type AuctionId = i64;

#[derive(Debug, Default, Clone)]
pub struct AuctionData {
pub auction_id: AuctionId,
pub gas_used: U256,
pub effective_gas_price: U256,
pub surplus: U256,
pub fee: U256,
pub order_executions: Vec<(OrderUid, ExecutedFee)>,
}

#[derive(Debug, Default, Clone)]
#[derive(Debug, Clone)]
pub struct SettlementUpdate {
pub block_number: i64,
pub log_index: i64,
pub tx_from: H160,
pub tx_nonce: i64,
pub auction_id: AuctionId,
/// Only set if the auction is for this environment.
pub auction_data: Option<AuctionData>,
}

Expand All @@ -41,31 +40,16 @@ impl super::Postgres {
.start_timer();

// update settlements
database::auction_transaction::insert_settlement_tx_info(
database::settlements::update_settlement_auction(
ex,
settlement_update.block_number,
settlement_update.log_index,
&ByteArray(settlement_update.tx_from.0),
settlement_update.tx_nonce,
settlement_update.auction_id,
)
.await
.context("insert_settlement_tx_info")?;

if let Some(auction_data) = settlement_update.auction_data {
// Link the `auction_id` to the settlement tx. This is needed for
// colocated solutions and is a no-op for centralized
// solutions.
let insert_succesful = database::auction_transaction::try_insert_auction_transaction(
ex,
auction_data.auction_id,
&ByteArray(settlement_update.tx_from.0),
settlement_update.tx_nonce,
)
.await
.context("failed to insert auction_transaction")?;

// in case of deep reindexing we might already have the observation, so just
// overwrite it
database::settlement_observations::upsert(
ex,
Observation {
Expand All @@ -80,17 +64,15 @@ impl super::Postgres {
.await
.context("insert_settlement_observations")?;

if insert_succesful {
for (order, executed_fee) in auction_data.order_executions {
database::order_execution::save(
ex,
&ByteArray(order.0),
auction_data.auction_id,
&u256_to_big_decimal(&executed_fee),
)
.await
.context("save_order_executions")?;
}
for (order, executed_fee) in auction_data.order_executions {
database::order_execution::save(
ex,
&ByteArray(order.0),
settlement_update.auction_id,
&u256_to_big_decimal(&executed_fee),
)
.await
.context("save_order_executions")?;
}
}
Ok(())
Expand Down
Loading

0 comments on commit 0213ec8

Please sign in to comment.