Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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
435 changes: 350 additions & 85 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ proc-macro2 = "1.0.103"
alloy-sol-macro-input = "1.4.1"
alloy-sol-macro-expander = "1.4.1"
jsonrpc-core = "18.0.0"
pod-sdk = "0.5.1"

[workspace.lints]
clippy.cast_possible_wrap = "deny"
2 changes: 1 addition & 1 deletion crates/autopilot/src/domain/competition/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub struct TradedOrder {
Eq,
Ord,
)]
pub struct Score(eth::Ether);
pub struct Score(pub eth::Ether);

impl Score {
pub fn try_new(score: eth::Ether) -> Result<Self, ZeroScore> {
Expand Down
82 changes: 81 additions & 1 deletion crates/autopilot/src/domain/competition/winner_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use {
fee,
},
::winner_selection::state::{HasState, RankedItem, ScoredItem, UnscoredItem},
alloy::primitives::keccak256,
std::collections::HashMap,
winner_selection::{self as winsel},
};
Expand All @@ -57,6 +58,9 @@ impl Arbitrator {

/// Runs the entire auction mechanism on the passed in solutions.
pub fn arbitrate(&self, bids: Vec<Bid<Unscored>>, auction: &domain::Auction) -> Ranking {
let mut bids = bids;
bids.sort_by_cached_key(|b| hash_solution(b.solution()));
Comment thread
ogabrielides marked this conversation as resolved.
Outdated

let context = auction.into();
let mut bid_by_key = HashMap::with_capacity(bids.len());
let mut solutions = Vec::with_capacity(bids.len());
Expand Down Expand Up @@ -104,11 +108,28 @@ impl Arbitrator {
);
}

Ranking {
let rank = Ranking {
filtered_out,
ranked,
reference_scores,
};

let winners = rank.winners().collect::<Vec<_>>();
let non_winners = rank.non_winners().collect::<Vec<_>>();
tracing::info!(
num_winners = winners.len(),
num_non_winners = non_winners.len(),
"[pod] CoW arbitration completed"
);
for winner in winners {
tracing::info!(
auction_id = ?auction.id,
submission_address = %winner.driver().submission_address.to_string(),
computed_score = ?winner.score(),
"[pod] CoW winner selected"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just to double check are all the logs marked with [pod] used by your script to analyze the outcomes of the auction?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

No.
All logs regarding the pod integration are marked with [pod].
But the script analyzes only a subset of them: an auction winner selection outcome:

  • bidder winner address.
  • bidder winner score.

);
}
rank
}
}

Expand Down Expand Up @@ -277,6 +298,65 @@ impl Ranking {
}
}

fn u64_be(x: u64) -> [u8; 8] {
x.to_be_bytes()
}

fn u256_be(x: &winner_selection::U256) -> [u8; 32] {
x.to_be_bytes()
}

fn side_to_u8(side: crate::domain::auction::order::Side) -> u8 {
match side {
crate::domain::auction::order::Side::Buy => 0,
crate::domain::auction::order::Side::Sell => 1,
}
}

fn encode_traded_order(buf: &mut Vec<u8>, order: &crate::domain::competition::TradedOrder) {
buf.push(side_to_u8(order.side));

buf.extend_from_slice((order.sell.token).0.0.as_slice());
buf.extend_from_slice(&u256_be(&(order.sell.amount).0));

buf.extend_from_slice((order.buy.token).0.0.as_slice());
buf.extend_from_slice(&u256_be(&(order.buy.amount).0));

buf.extend_from_slice(&u256_be(&(order.executed_sell).0));
buf.extend_from_slice(&u256_be(&(order.executed_buy).0));
}

pub fn hash_solution(sol: &Solution) -> [u8; 32] {
let mut buf = Vec::new();

buf.extend_from_slice(&u64_be(sol.id));

buf.extend_from_slice(sol.solver.0.as_slice());

let mut orders: Vec<_> = sol.orders.iter().collect();
orders.sort_by(|(uid1, _), (uid2, _)| uid1.0.cmp(&uid2.0));

buf.extend_from_slice(&u64_be(orders.len() as u64));

for (uid, order) in orders {
buf.extend_from_slice(&uid.0);

encode_traded_order(&mut buf, order);
}

let mut prices: Vec<_> = sol.prices.iter().collect();
prices.sort_by(|(t1, _), (t2, _)| t1.0.cmp(&t2.0));

buf.extend_from_slice(&u64_be(prices.len() as u64));

for (token_addr, price) in prices {
buf.extend_from_slice(token_addr.0.0.as_slice());
buf.extend_from_slice(&u256_be(&price.get().0));
}

keccak256(&buf).into()
}

#[cfg(test)]
mod tests {
use {
Expand Down
6 changes: 6 additions & 0 deletions crates/autopilot/src/domain/eth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ impl From<Address> for WrappedNativeToken {
}
}

impl From<WrappedNativeToken> for Address {
fn from(value: WrappedNativeToken) -> Self {
value.0.into()
}
}

/// An ERC20 token amount.
///
/// https://eips.ethereum.org/EIPS/eip-20
Expand Down
2 changes: 1 addition & 1 deletion crates/autopilot/src/domain/fee/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

mod policy;

use {
pub use {
crate::{
arguments::{self},
boundary::{self},
Expand Down
5 changes: 5 additions & 0 deletions crates/driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ observe = { workspace = true, features = ["axum-tracing"] }
shared = { workspace = true }
solver = { workspace = true }
tracing = { workspace = true }
primitive-types = { workspace = true }
pod-sdk = { workspace = true }
derivative = { workspace = true }
autopilot = { workspace = true }
winner-selection = { workspace = true }

[dev-dependencies]
app-data = { workspace = true, features = ["test_helpers"] }
Expand Down
4 changes: 4 additions & 0 deletions crates/driver/src/domain/competition/auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ impl Tokens {
pub fn iter(&self) -> impl Iterator<Item = &Token> {
self.0.values()
}

pub fn iter_keys_values(&self) -> impl Iterator<Item = (&eth::TokenAddress, &Token)> {
self.0.iter()
}
}

#[derive(Debug, Clone)]
Expand Down
Loading
Loading