Skip to content
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

Unify networks and add Base support #3091

Merged
merged 9 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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: 14 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/autopilot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ itertools = { workspace = true }
maplit = { workspace = true }
mimalloc = { workspace = true }
model = { path = "../model" }
network = { path = "../network" }
num = { workspace = true }
number = { path = "../number" }
order-validation = { path = "../order-validation" }
Expand Down
19 changes: 5 additions & 14 deletions crates/autopilot/src/domain/settlement/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod auction;
mod observer;
mod trade;
mod transaction;
use network::Network;
pub use {auction::Auction, observer::Observer, trade::Trade, transaction::Transaction};

/// A settled transaction together with the `Auction`, for which it was executed
Expand Down Expand Up @@ -106,11 +107,11 @@ impl Settlement {
pub async fn new(
settled: Transaction,
persistence: &infra::Persistence,
chain: &infra::blockchain::Id,
network: &Network,
) -> Result<Self, Error> {
let auction = persistence.get_auction(settled.auction_id).await?;

if settled.block > auction.block + max_settlement_age(chain) {
if settled.block > auction.block + max_settlement_age(network) {
// A settled transaction references a VERY old auction.
//
// A hacky way to detect processing of production settlements in the staging
Expand Down Expand Up @@ -138,23 +139,13 @@ impl Settlement {
}
}

const MAINNET_BLOCK_TIME: u64 = 13_000; // ms
const GNOSIS_BLOCK_TIME: u64 = 5_000; // ms
const SEPOLIA_BLOCK_TIME: u64 = 13_000; // ms
const ARBITRUM_ONE_BLOCK_TIME: u64 = 100; // ms
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was incorrect, it should've been 250. Any special reason for it @sunce86 ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I just thought it's 100ms 😄


/// How old (in terms of blocks) a settlement should be, to be considered as a
/// settlement from another environment.
///
/// Currently set to ~6h
fn max_settlement_age(chain: &infra::blockchain::Id) -> u64 {
fn max_settlement_age(network: &Network) -> u64 {
const TARGET_AGE: u64 = 6 * 60 * 60 * 1000; // 6h in ms
match chain {
infra::blockchain::Id::Mainnet => TARGET_AGE / MAINNET_BLOCK_TIME,
infra::blockchain::Id::Gnosis => TARGET_AGE / GNOSIS_BLOCK_TIME,
infra::blockchain::Id::Sepolia => TARGET_AGE / SEPOLIA_BLOCK_TIME,
infra::blockchain::Id::ArbitrumOne => TARGET_AGE / ARBITRUM_ONE_BLOCK_TIME,
}
network.blocks_in(TARGET_AGE).round() as u64
}

#[derive(Debug, thiserror::Error)]
Expand Down
2 changes: 1 addition & 1 deletion crates/autopilot/src/domain/settlement/observer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl Observer {
let settlement = match settlement::Settlement::new(
transaction,
&self.persistence,
self.eth.chain(),
self.eth.network(),
)
.await
{
Expand Down
10 changes: 4 additions & 6 deletions crates/autopilot/src/infra/blockchain/authenticator.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use {
crate::{
domain::{self, eth},
infra::blockchain::{
self,
contracts::{deployment_address, Contracts},
},
infra::blockchain::contracts::{deployment_address, Contracts},
},
ethcontract::{dyns::DynWeb3, GasPrice},
network::Network,
};

#[allow(dead_code)]
Expand All @@ -25,13 +23,13 @@ impl Manager {
/// Creates an authenticator which can remove solvers from the allow-list
pub async fn new(
web3: DynWeb3,
chain: blockchain::Id,
network: &Network,
contracts: Contracts,
authenticator_pk: eth::H256,
) -> Self {
let authenticator_role = contracts::Roles::at(
&web3,
deployment_address(contracts::Roles::raw_contract(), &chain).expect("roles address"),
deployment_address(contracts::Roles::raw_contract(), network).expect("roles address"),
);

Self {
Expand Down
17 changes: 5 additions & 12 deletions crates/autopilot/src/infra/blockchain/contracts.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use {
crate::{domain, infra::blockchain},
ethcontract::dyns::DynWeb3,
primitive_types::H160,
};
use {crate::domain, ethcontract::dyns::DynWeb3, network::Network, primitive_types::H160};

#[derive(Debug, Clone)]
pub struct Contracts {
Expand All @@ -24,10 +20,10 @@ pub struct Addresses {
}

impl Contracts {
pub async fn new(web3: &DynWeb3, chain: &blockchain::Id, addresses: Addresses) -> Self {
pub async fn new(web3: &DynWeb3, network: &Network, addresses: Addresses) -> Self {
let address_for = |contract: &ethcontract::Contract, address: Option<H160>| {
address
.or_else(|| deployment_address(contract, chain))
.or_else(|| deployment_address(contract, network))
.unwrap()
};

Expand Down Expand Up @@ -102,12 +98,9 @@ impl Contracts {

/// Returns the address of a contract for the specified network, or `None` if
/// there is no known deployment for the contract on that network.
pub fn deployment_address(
contract: &ethcontract::Contract,
chain: &blockchain::Id,
) -> Option<H160> {
pub fn deployment_address(contract: &ethcontract::Contract, network: &Network) -> Option<H160> {
contract
.networks
.get(chain.network_id())
.get(&network.chain_id().to_string())
.map(|network| network.address)
}
43 changes: 0 additions & 43 deletions crates/autopilot/src/infra/blockchain/id.rs

This file was deleted.

27 changes: 13 additions & 14 deletions crates/autopilot/src/infra/blockchain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use {
crate::{boundary, domain::eth},
ethcontract::dyns::DynWeb3,
ethrpc::block_stream::CurrentBlockWatcher,
network::Network,
primitive_types::U256,
std::time::Duration,
thiserror::Error,
Expand All @@ -11,14 +12,11 @@ use {

pub mod authenticator;
pub mod contracts;
pub mod id;

pub use id::Id;

/// An Ethereum RPC connection.
pub struct Rpc {
web3: DynWeb3,
chain: Id,
network: Network,
url: Url,
}

Expand All @@ -30,18 +28,19 @@ impl Rpc {
ethrpc_args: &shared::ethrpc::Arguments,
) -> Result<Self, Error> {
let web3 = boundary::web3_client(url, ethrpc_args);
let chain = Id::new(web3.eth().chain_id().await?).map_err(|_| Error::UnsupportedChain)?;
let network =
Network::try_from(web3.eth().chain_id().await?).map_err(|_| Error::UnsupportedChain)?;

Ok(Self {
web3,
chain,
network,
url: url.clone(),
})
}

/// Returns the chain id for the RPC connection.
pub fn chain(&self) -> Id {
self.chain
pub fn network(&self) -> Network {
self.network
}

/// Returns a reference to the underlying web3 client.
Expand All @@ -59,7 +58,7 @@ impl Rpc {
#[derive(Clone)]
pub struct Ethereum {
web3: DynWeb3,
chain: Id,
network: Network,
current_block: CurrentBlockWatcher,
contracts: Contracts,
}
Expand All @@ -73,25 +72,25 @@ impl Ethereum {
/// any initialization error.
pub async fn new(
web3: DynWeb3,
chain: Id,
network: &Network,
url: Url,
addresses: contracts::Addresses,
poll_interval: Duration,
) -> Self {
let contracts = Contracts::new(&web3, &chain, addresses).await;
let contracts = Contracts::new(&web3, network, addresses).await;

Self {
current_block: ethrpc::block_stream::current_block_stream(url, poll_interval)
.await
.expect("couldn't initialize current block stream"),
web3,
chain,
network: *network,
contracts,
}
}

pub fn chain(&self) -> &Id {
&self.chain
pub fn network(&self) -> &Network {
&self.network
}

/// Returns a stream that monitors the block chain to inform about the
Expand Down
Loading
Loading