Skip to content

Commit

Permalink
Merge branch 'main' into reduce-try-into-implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
squadgazzz authored Nov 21, 2024
2 parents e7f514e + 27e3c3b commit 5de328e
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 26 deletions.
3 changes: 1 addition & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
]
},
"extensions": [
"rust-lang.rust-analyzer",
"serayuzgur.crates"
"rust-lang.rust-analyzer"
]
}
},
Expand Down
8 changes: 6 additions & 2 deletions crates/autopilot/src/boundary/events/settlement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ impl_event_retrieving! {

pub struct Indexer {
db: Postgres,
start_index: u64,
settlement_observer: settlement::Observer,
}

impl Indexer {
pub fn new(db: Postgres, settlement_observer: settlement::Observer) -> Self {
pub fn new(db: Postgres, settlement_observer: settlement::Observer, start_index: u64) -> Self {
Self {
db,
settlement_observer,
start_index,
}
}
}
Expand All @@ -29,7 +31,9 @@ const INDEX_NAME: &str = "settlements";
#[async_trait::async_trait]
impl EventStoring<contracts::gpv2_settlement::Event> for Indexer {
async fn last_event_block(&self) -> Result<u64> {
super::read_last_block_from_db(&self.db.pool, INDEX_NAME).await
super::read_last_block_from_db(&self.db.pool, INDEX_NAME)
.await
.map(|last_block| last_block.max(self.start_index))
}

async fn persist_last_indexed_block(&mut self, latest_block: u64) -> Result<()> {
Expand Down
7 changes: 4 additions & 3 deletions crates/autopilot/src/infra/solvers/dto/reveal.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use {
serde::{Deserialize, Serialize},
serde_with::serde_as,
serde_with::{serde_as, skip_serializing_none},
};

#[serde_as]
#[skip_serializing_none]
#[derive(Clone, Debug, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Request {
/// Unique ID of the solution (per driver competition), to reveal.
#[serde_as(as = "serde_with::DisplayFromStr")]
pub solution_id: u64,
/// Auction ID in which the specified solution ID is competing.
#[serde_as(as = "serde_with::DisplayFromStr")]
pub auction_id: i64,
#[serde_as(as = "Option<serde_with::DisplayFromStr>")]
pub auction_id: Option<i64>,
}

#[serde_as]
Expand Down
7 changes: 4 additions & 3 deletions crates/autopilot/src/infra/solvers/dto/settle.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use {
primitive_types::H256,
serde::{Deserialize, Serialize},
serde_with::serde_as,
serde_with::{serde_as, skip_serializing_none},
};

#[serde_as]
#[skip_serializing_none]
#[derive(Clone, Debug, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Request {
Expand All @@ -14,8 +15,8 @@ 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 = "serde_with::DisplayFromStr")]
pub auction_id: i64,
#[serde_as(as = "Option<serde_with::DisplayFromStr>")]
pub auction_id: Option<i64>,
}

#[serde_as]
Expand Down
21 changes: 19 additions & 2 deletions crates/autopilot/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use {
chain::Chain,
clap::Parser,
contracts::{BalancerV2Vault, IUniswapV3Factory},
ethcontract::{dyns::DynWeb3, errors::DeployError, BlockNumber},
ethcontract::{common::DeploymentInformation, dyns::DynWeb3, errors::DeployError, BlockNumber},
ethrpc::block_stream::block_number_to_block_number_hash,
futures::StreamExt,
model::DomainSeparator,
Expand Down Expand Up @@ -363,11 +363,28 @@ pub async fn run(args: Arguments) {
infra::persistence::Persistence::new(args.s3.into().unwrap(), Arc::new(db.clone())).await;
let settlement_observer =
crate::domain::settlement::Observer::new(eth.clone(), persistence.clone());
let settlement_contract_start_index =
if let Some(DeploymentInformation::BlockNumber(settlement_contract_start_index)) =
eth.contracts().settlement().deployment_information()
{
settlement_contract_start_index
} else {
// If the deployment information can't be found, start from 0 (default
// behaviour). For real contracts, the deployment information is specified
// for all the networks, but it isn't specified for the e2e tests which deploy
// the contracts from scratch
tracing::warn!("Settlement contract deployment information not found");
0
};
let settlement_event_indexer = EventUpdater::new(
boundary::events::settlement::GPv2SettlementContract::new(
eth.contracts().settlement().clone(),
),
boundary::events::settlement::Indexer::new(db.clone(), settlement_observer),
boundary::events::settlement::Indexer::new(
db.clone(),
settlement_observer,
settlement_contract_start_index,
),
block_retriever.clone(),
skip_event_sync_start,
);
Expand Down
2 changes: 1 addition & 1 deletion crates/autopilot/src/run_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ impl RunLoop {
let request = settle::Request {
solution_id,
submission_deadline_latest_block,
auction_id,
auction_id: None, // Requires 2-stage release for API-break change
};
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: request.id,
auction_id: None, // Requires 2-stage release for API-break change
})
.await
.map_err(Error::Reveal)?;
Expand Down
5 changes: 4 additions & 1 deletion crates/contracts/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,10 @@ fn main() {
.add_network_str(GOERLI, "0xE592427A0AEce92De3Edee1F18E0157C05861564")
.add_network_str(SEPOLIA, "0xE592427A0AEce92De3Edee1F18E0157C05861564")
.add_network_str(ARBITRUM_ONE, "0xE592427A0AEce92De3Edee1F18E0157C05861564")
// Not available on Gnosis Chain and Base
// For Base it is only available SwapRouter02
// <https://docs.uniswap.org/contracts/v3/reference/deployments/base-deployments>
.add_network_str(BASE, "0x2626664c2603336E57B271c5C0b26F421741e481")
// Not available on Gnosis Chain
});
generate_contract("UniswapV3Pool");
generate_contract_with_config("WETH9", |builder| {
Expand Down
18 changes: 14 additions & 4 deletions crates/driver/src/domain/competition/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,20 @@ impl Competition {
Ok(score)
}

pub async fn reveal(&self, solution_id: u64, auction_id: i64) -> Result<Revealed, Error> {
pub async fn reveal(
&self,
solution_id: u64,
auction_id: Option<i64>,
) -> Result<Revealed, Error> {
let settlement = self
.settlements
.lock()
.unwrap()
.iter()
.find(|s| s.solution().get() == solution_id && s.auction_id.0 == auction_id)
.find(|s| {
s.solution().get() == solution_id
&& auction_id.is_none_or(|id| s.auction_id.0 == id)
})
.cloned()
.ok_or(Error::SolutionNotAvailable)?;
Ok(Revealed {
Expand All @@ -292,15 +299,18 @@ impl Competition {
/// [`Competition::solve`] to generate the solution.
pub async fn settle(
&self,
auction_id: i64,
auction_id: Option<i64>,
solution_id: u64,
submission_deadline: u64,
) -> Result<Settled, Error> {
let settlement = {
let mut lock = self.settlements.lock().unwrap();
let index = lock
.iter()
.position(|s| s.solution().get() == solution_id && s.auction_id.0 == auction_id)
.position(|s| {
s.solution().get() == solution_id
&& auction_id.is_none_or(|id| s.auction_id.0 == id)
})
.ok_or(Error::SolutionNotAvailable)?;
// remove settlement to ensure we can't settle it twice by accident
lock.swap_remove_front(index)
Expand Down
6 changes: 3 additions & 3 deletions crates/driver/src/infra/api/routes/reveal/dto/solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use {serde::Deserialize, serde_with::serde_as};

#[serde_as]
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[serde(rename_all = "camelCase")]
pub struct Solution {
/// Unique ID of the solution (per driver competition), to reveal.
#[serde_as(as = "serde_with::DisplayFromStr")]
pub solution_id: u64,
/// Auction ID in which the specified solution ID is competing.
#[serde_as(as = "serde_with::DisplayFromStr")]
pub auction_id: i64,
#[serde_as(as = "Option<serde_with::DisplayFromStr>")]
pub auction_id: Option<i64>,
}
6 changes: 3 additions & 3 deletions crates/driver/src/infra/api/routes/settle/dto/solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use {serde::Deserialize, serde_with::serde_as};

#[serde_as]
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
#[serde(rename_all = "camelCase")]
pub struct Solution {
/// Unique ID of the solution (per driver competition), to settle.
#[serde_as(as = "serde_with::DisplayFromStr")]
pub solution_id: u64,
/// The last block number in which the solution TX can be included
pub submission_deadline_latest_block: u64,
/// Auction ID in which this solution is competing.
#[serde_as(as = "serde_with::DisplayFromStr")]
pub auction_id: i64,
#[serde_as(as = "Option<serde_with::DisplayFromStr>")]
pub auction_id: Option<i64>,
}
8 changes: 7 additions & 1 deletion crates/shared/src/event_handling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use {
futures::{future, Stream, StreamExt, TryStreamExt},
std::sync::Arc,
tokio::sync::Mutex,
tracing::Instrument,
};

// We expect that there is never a reorg that changes more than the last n
Expand Down Expand Up @@ -537,7 +538,12 @@ where
S: EventStoring<C::Event> + Send + Sync,
{
async fn run_maintenance(&self) -> Result<()> {
self.lock().await.update_events().await
let mut inner = self.lock().await;
let address = inner.contract.get_events().filter.address;
inner
.update_events()
.instrument(tracing::info_span!("address", ?address))
.await
}

fn name(&self) -> &str {
Expand Down

0 comments on commit 5de328e

Please sign in to comment.