Skip to content

Commit

Permalink
Colocation - Notify asset flow (#2053)
Browse files Browse the repository at this point in the history
# Description
Noticed that some of the solvers are not populating `inputs` and
`outputs` fields in `interactions` data, leading to solutions from these
solvers being discarded in colocated driver.
This PR adds a new notification type to inform solvers about their bad
expectation, but is also useful for debugging in case a solver does not
populate these fields (since the missmatches will be full amounts of
tokens in that case).

Missmatches take the range `[-U256, U256]` so `String` seemed like the
easiest way to serialize it.
  • Loading branch information
sunce86 authored Nov 10, 2023
1 parent 1f0f69b commit bd20530
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 7 deletions.
2 changes: 1 addition & 1 deletion crates/driver/src/infra/notify/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub fn encoding_failed(
notification::Kind::SimulationFailed(error.tx.clone())
}
solution::Error::Simulation(simulator::Error::Basic(_)) => return,
solution::Error::AssetFlow(_) => return,
solution::Error::AssetFlow(missmatch) => notification::Kind::AssetFlow(missmatch.clone()),
solution::Error::Execution(_) => return,
solution::Error::FailingInternalization => return,
solution::Error::DifferentSolvers => return,
Expand Down
7 changes: 6 additions & 1 deletion crates/driver/src/infra/notify/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ use {
competition::{auction, score::Quality, solution, Score},
eth::{self, Ether, GasCost, TokenAddress},
},
std::collections::BTreeSet,
std::collections::{BTreeSet, HashMap},
};

type RequiredEther = Ether;
type TokensUsed = BTreeSet<TokenAddress>;
type TransactionHash = eth::TxId;
type Transaction = eth::Tx;
type Missmatches = HashMap<eth::TokenAddress, num::BigInt>;

/// A notification sent to solvers in case of important events in the driver.
#[derive(Debug)]
Expand All @@ -36,6 +37,10 @@ pub enum Kind {
NonBufferableTokensUsed(TokensUsed),
/// Solver don't have enough balance to submit the solution onchain.
SolverAccountInsufficientBalance(RequiredEther),
/// For some of the tokens used in the solution, the amount leaving the
/// settlement contract is higher than amount entering the settlement
/// contract.
AssetFlow(Missmatches),
/// Result of winning solver trying to settle the transaction onchain.
Settled(Settlement),
}
Expand Down
11 changes: 10 additions & 1 deletion crates/driver/src/infra/solver/dto/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use {
},
serde::Serialize,
serde_with::serde_as,
std::collections::BTreeSet,
std::collections::{BTreeSet, HashMap},
web3::types::AccessList,
};

Expand Down Expand Up @@ -62,6 +62,12 @@ impl Notification {
required: required.0,
}
}
notify::Kind::AssetFlow(amounts) => Kind::AssetFlow {
amounts: amounts
.into_iter()
.map(|(token, amount)| (token.0 .0, amount.to_string()))
.collect(),
},
notify::Kind::DuplicatedSolutionId => Kind::DuplicatedSolutionId,
notify::Kind::Settled(kind) => Kind::Settled(match kind {
notify::Settlement::Success(hash) => Settlement::Success {
Expand Down Expand Up @@ -103,6 +109,9 @@ pub enum Kind {
#[serde_as(as = "serialize::U256")]
required: eth::U256,
},
AssetFlow {
amounts: HashMap<eth::H160, String>,
},
Settled(Settlement),
}

Expand Down
5 changes: 5 additions & 0 deletions crates/shared/src/http_solver/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,11 @@ pub enum SolverRejectionReason {
/// Solver balance too low to cover the execution costs.
SolverAccountInsufficientBalance(U256),

/// For some of the tokens used in the solution, the amount leaving the
/// settlement contract is higher than amount entering the settlement
/// contract.
AssetFlow(HashMap<H160, String>),

/// Solution received from solver engine don't have unique id.
DuplicatedSolutionId(u64),
}
Expand Down
18 changes: 17 additions & 1 deletion crates/solvers/src/api/routes/notify/dto/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ use {
domain::{auction, eth, notification},
util::serialize,
},
bigdecimal::Num,
ethereum_types::{H160, H256, U256},
serde::Deserialize,
serde_with::{serde_as, DisplayFromStr},
std::collections::BTreeSet,
std::collections::{BTreeSet, HashMap},
web3::types::AccessList,
};

Expand Down Expand Up @@ -67,6 +68,18 @@ impl Notification {
Kind::SolverAccountInsufficientBalance { required } => {
notification::Kind::SolverAccountInsufficientBalance(eth::Ether(*required))
}
Kind::AssetFlow { amounts } => notification::Kind::AssetFlow(
amounts
.clone()
.into_iter()
.map(|(token, amount)| {
(
token.into(),
num::BigInt::from_str_radix(&amount, 10).unwrap_or_default(),
)
})
.collect(),
),
Kind::DuplicatedSolutionId => notification::Kind::DuplicatedSolutionId,
Kind::Settled(kind) => notification::Kind::Settled(match kind {
Settlement::Success { transaction } => {
Expand Down Expand Up @@ -109,6 +122,9 @@ pub enum Kind {
#[serde_as(as = "serialize::U256")]
required: U256,
},
AssetFlow {
amounts: HashMap<eth::H160, String>,
},
Settled(Settlement),
}

Expand Down
2 changes: 1 addition & 1 deletion crates/solvers/src/api/routes/notify/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub async fn notify(
let notification = notification.to_domain();
let auction_id = notification.auction_id;

tracing::info!(id = %auction_id, "auction");
tracing::trace!(?auction_id, ?notification);
state.notify(notification);

axum::http::StatusCode::OK
Expand Down
2 changes: 1 addition & 1 deletion crates/solvers/src/api/routes/solve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub async fn solve(
.instrument(tracing::info_span!("auction", id = %auction_id))
.await;

tracing::trace!(?solutions);
tracing::trace!(?auction_id, ?solutions);

let solutions = dto::Solutions::from_domain(&solutions);
(
Expand Down
6 changes: 6 additions & 0 deletions crates/solvers/src/boundary/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,12 @@ fn to_boundary_auction_result(notification: &notification::Notification) -> (i64
Kind::SolverAccountInsufficientBalance(required) => AuctionResult::Rejected(
SolverRejectionReason::SolverAccountInsufficientBalance(required.0),
),
Kind::AssetFlow(amounts) => AuctionResult::Rejected(SolverRejectionReason::AssetFlow(
amounts
.iter()
.map(|(token, amount)| (token.0, amount.to_string()))
.collect(),
)),
Kind::DuplicatedSolutionId => {
AuctionResult::Rejected(SolverRejectionReason::DuplicatedSolutionId(
notification
Expand Down
4 changes: 3 additions & 1 deletion crates/solvers/src/domain/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ use {
eth::{self, Ether, TokenAddress},
solution::{self, SuccessProbability},
},
std::collections::BTreeSet,
std::collections::{BTreeSet, HashMap},
};

type RequiredEther = Ether;
type TokensUsed = BTreeSet<TokenAddress>;
type TransactionHash = eth::H256;
type Transaction = eth::Tx;
type Missmatches = HashMap<eth::TokenAddress, num::BigInt>;

/// The notification about important events happened in driver, that solvers
/// need to know about.
Expand All @@ -31,6 +32,7 @@ pub enum Kind {
ScoringFailed(ScoreKind),
NonBufferableTokensUsed(TokensUsed),
SolverAccountInsufficientBalance(RequiredEther),
AssetFlow(Missmatches),
Settled(Settlement),
}

Expand Down

0 comments on commit bd20530

Please sign in to comment.