Skip to content

Commit

Permalink
Merge branch 'main' into estimate-gas-on-latest-block
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinquaXD authored Jan 8, 2025
2 parents 763eb47 + 2f54635 commit c3e3571
Show file tree
Hide file tree
Showing 24 changed files with 198 additions and 94 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ jobs:

test-forked-node:
# Do not run this job on forks since some secrets are required for it.
if: ${{ github.event.pull_request.head.repo.full_name == github.repository }}
if: |
(github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository) ||
(github.event_name == 'push' && github.ref == 'refs/heads/main')
timeout-minutes: 60
runs-on: ubuntu-latest
env:
Expand Down
44 changes: 14 additions & 30 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ clap = { version = "4.5.6", features = ["derive", "env"] }
dashmap = "6.1.0"
derivative = "2.2.0"
derive_more = { version = "1.0.0", features = ["full"] }
ethcontract = { version = "0.25.7", default-features = false, features = ["aws-kms"] }
ethcontract = { version = "0.25.8", default-features = false, features = ["aws-kms"] }
mimalloc = "0.1.43"
ethcontract-generate = { version = "0.25.7", default-features = false }
ethcontract-mock = { version = "0.25.7", default-features = false }
ethcontract-generate = { version = "0.25.8", default-features = false }
ethcontract-mock = { version = "0.25.8", default-features = false }
ethereum-types = "0.14.1"
flate2 = "1.0.30"
futures = "0.3.30"
Expand Down Expand Up @@ -44,7 +44,6 @@ serde_with = "3.8.1"
sqlx = { version = "0.7", default-features = false, features = ["runtime-tokio", "tls-native-tls", "bigdecimal", "chrono", "postgres", "macros"] }
strum = { version = "0.26.2", features = ["derive"] }
tempfile = "3.10.1"
time = { version = "0.3.36", features = ["macros"] }
thiserror = "1.0.61"
toml = "0.8.14"
tokio = { version = "1.38.0", features = ["tracing"] }
Expand Down
4 changes: 2 additions & 2 deletions crates/autopilot/src/infra/solvers/dto/reveal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub struct Request {
/// Unique ID of the solution (per driver competition), to reveal.
pub solution_id: u64,
/// Auction ID in which the specified solution ID is competing.
#[serde_as(as = "Option<serde_with::DisplayFromStr>")]
pub auction_id: Option<i64>,
#[serde_as(as = "serde_with::DisplayFromStr")]
pub auction_id: i64,
}

#[serde_as]
Expand Down
4 changes: 2 additions & 2 deletions crates/autopilot/src/infra/solvers/dto/settle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ pub struct Request {
/// The last block number in which the solution TX can be included
pub submission_deadline_latest_block: u64,
/// Auction ID in which the specified solution ID is competing.
#[serde_as(as = "Option<serde_with::DisplayFromStr>")]
pub auction_id: Option<i64>,
#[serde_as(as = "serde_with::DisplayFromStr")]
pub auction_id: i64,
}
2 changes: 1 addition & 1 deletion crates/autopilot/src/run_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ impl RunLoop {
let request = settle::Request {
solution_id,
submission_deadline_latest_block,
auction_id: None, // Requires 2-stage release for API-break change
auction_id,
};
driver
.settle(&request, self.config.max_settlement_transaction_wait)
Expand Down
2 changes: 1 addition & 1 deletion crates/autopilot/src/shadow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ impl RunLoop {
let revealed = driver
.reveal(&reveal::Request {
solution_id,
auction_id: None, // Requires 2-stage release for API-break change
auction_id: request.id,
})
.await
.map_err(Error::Reveal)?;
Expand Down
52 changes: 52 additions & 0 deletions crates/database/src/trades.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,31 @@ AND t.log_index BETWEEN (SELECT * from previous_settlement) AND $2
.await
}

pub async fn token_first_trade_block(
ex: &mut PgConnection,
token: Address,
) -> Result<Option<i64>, sqlx::Error> {
const QUERY: &str = r#"
SELECT MIN(sub.block_number) AS earliest_block
FROM (
SELECT MIN(t.block_number) AS block_number
FROM trades t
JOIN orders o ON t.order_uid = o.uid
WHERE o.sell_token = $1 OR o.buy_token = $1
UNION ALL
SELECT MIN(t.block_number) AS block_number
FROM trades t
JOIN jit_orders j ON t.order_uid = j.uid
WHERE j.sell_token = $1 OR j.buy_token = $1
) AS sub
"#;

let (block_number,) = sqlx::query_as(QUERY).bind(token).fetch_one(ex).await?;
Ok(block_number)
}

#[cfg(test)]
mod tests {
use {
Expand Down Expand Up @@ -579,4 +604,31 @@ mod tests {
}]
);
}

#[tokio::test]
#[ignore]
async fn postgres_token_first_trade_block() {
let mut db = PgConnection::connect("postgresql://").await.unwrap();
let mut db = db.begin().await.unwrap();
crate::clear_DANGER_(&mut db).await.unwrap();

let token = Default::default();
assert_eq!(token_first_trade_block(&mut db, token).await.unwrap(), None);

let (owners, order_ids) = generate_owners_and_order_ids(2, 2).await;
let event_index_a = EventIndex {
block_number: 123,
log_index: 0,
};
let event_index_b = EventIndex {
block_number: 124,
log_index: 0,
};
add_order_and_trade(&mut db, owners[0], order_ids[0], event_index_a, None, None).await;
add_order_and_trade(&mut db, owners[1], order_ids[1], event_index_b, None, None).await;
assert_eq!(
token_first_trade_block(&mut db, token).await.unwrap(),
Some(123)
);
}
}
22 changes: 7 additions & 15 deletions crates/driver/src/domain/competition/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,17 +318,14 @@ impl Competition {
pub async fn reveal(
&self,
solution_id: u64,
auction_id: Option<i64>,
auction_id: auction::Id,
) -> Result<Revealed, Error> {
let settlement = self
.settlements
.lock()
.unwrap()
.iter()
.find(|s| {
s.solution().get() == solution_id
&& auction_id.is_none_or(|id| s.auction_id.0 == id)
})
.find(|s| s.solution().get() == solution_id && s.auction_id == auction_id)
.cloned()
.ok_or(Error::SolutionNotAvailable)?;
Ok(Revealed {
Expand All @@ -347,7 +344,7 @@ impl Competition {
/// [`Competition::solve`] to generate the solution.
pub async fn settle(
&self,
auction_id: Option<auction::Id>,
auction_id: auction::Id,
solution_id: u64,
submission_deadline: BlockNo,
) -> Result<Settled, Error> {
Expand Down Expand Up @@ -413,27 +410,22 @@ impl Competition {
tracing::error!(?err, "Failed to send /settle response");
}
}
.instrument(
tracing::info_span!("/settle", solver, auction_id = ?auction_id.map(|id| id.0)),
)
.instrument(tracing::info_span!("/settle", solver, %auction_id))
.await
}
}

async fn process_settle_request(
&self,
auction_id: Option<auction::Id>,
auction_id: auction::Id,
solution_id: u64,
submission_deadline: BlockNo,
) -> Result<Settled, Error> {
let settlement = {
let mut lock = self.settlements.lock().unwrap();
let index = lock
.iter()
.position(|s| {
s.solution().get() == solution_id
&& auction_id.is_none_or(|id| s.auction_id == id)
})
.position(|s| s.solution().get() == solution_id && s.auction_id == auction_id)
.ok_or(Error::SolutionNotAvailable)?;
// remove settlement to ensure we can't settle it twice by accident
lock.swap_remove_front(index)
Expand Down Expand Up @@ -537,7 +529,7 @@ fn merge(solutions: impl Iterator<Item = Solution>, auction: &Auction) -> Vec<So
}

struct SettleRequest {
auction_id: Option<auction::Id>,
auction_id: auction::Id,
solution_id: u64,
submission_deadline: BlockNo,
response_sender: oneshot::Sender<Result<Settled, Error>>,
Expand Down
24 changes: 16 additions & 8 deletions crates/driver/src/domain/mempools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl Mempools {
// settlement. This way we only run iterations in blocks that can potentially
// include the settlement.
let mut block_stream = into_stream(self.ethereum.current_block().clone());
block_stream.next().await;
let block = block_stream.next().await;

// The tx is simulated before submitting the solution to the competition, but a
// delay between that and the actual execution can cause the simulation to be
Expand All @@ -116,7 +116,7 @@ impl Mempools {
?err,
"settlement tx simulation reverted before submitting to the mempool"
);
return Err(Error::SimulationRevert);
return Err(Error::SimulationRevert(block.map(|block| block.number)));
} else {
tracing::warn!(
?err,
Expand All @@ -142,7 +142,12 @@ impl Mempools {
});
match receipt {
TxStatus::Executed => return Ok(hash.clone()),
TxStatus::Reverted => return Err(Error::Revert(hash.clone())),
TxStatus::Reverted => {
return Err(Error::Revert {
tx_id: hash.clone(),
block_number: block.number,
})
}
TxStatus::Pending => {
// Check if the current block reached the submission deadline block number
if block.number >= submission_deadline {
Expand Down Expand Up @@ -172,7 +177,7 @@ impl Mempools {
?err,
"tx started failing in mempool, cancelling"
);
return Err(Error::SimulationRevert);
return Err(Error::SimulationRevert(Some(block.number)));
} else {
tracing::warn!(?hash, ?err, "couldn't re-simulate tx");
}
Expand Down Expand Up @@ -235,10 +240,13 @@ pub enum RevertProtection {

#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Mined reverted transaction: {0:?}")]
Revert(eth::TxId),
#[error("Simulation started reverting during submission")]
SimulationRevert,
#[error("Mined reverted transaction: {tx_id:?}, block number: {block_number}")]
Revert {
tx_id: eth::TxId,
block_number: BlockNo,
},
#[error("Simulation started reverting during submission, block number: {0:?}")]
SimulationRevert(Option<BlockNo>),
#[error("Settlement did not get included in time")]
Expired,
#[error("Strategy disabled for this tx")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ pub struct RevealRequest {
/// Unique ID of the solution (per driver competition), to reveal.
pub solution_id: u64,
/// Auction ID in which the specified solution ID is competing.
#[serde_as(as = "Option<serde_with::DisplayFromStr>")]
pub auction_id: Option<i64>,
#[serde_as(as = "serde_with::DisplayFromStr")]
pub auction_id: i64,
}
Loading

0 comments on commit c3e3571

Please sign in to comment.