-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Description I recently noticed some minor discrepancies between `solvers_dto` crate and `driver::infra::dto` module. Also, we update `solver_dto` separately, and sometimes, it gets forgotten. The proposal is to start using `solvers_dto` in the `driver::infra` to avoid any potential discrepancies in the future, and it should be easier to maintain it. ## How to test Existing tests should be sufficient.
- Loading branch information
1 parent
9c18538
commit ddf8931
Showing
17 changed files
with
510 additions
and
1,010 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,185 +1,99 @@ | ||
use { | ||
crate::{ | ||
domain::{ | ||
competition::{auction, solution}, | ||
eth::{self}, | ||
}, | ||
infra::notify, | ||
util::serialize, | ||
}, | ||
chrono::{DateTime, Utc}, | ||
serde::Serialize, | ||
serde_with::serde_as, | ||
std::collections::BTreeSet, | ||
web3::types::AccessList, | ||
use crate::{ | ||
domain::competition::{auction, solution}, | ||
infra::notify, | ||
}; | ||
|
||
impl Notification { | ||
pub fn new( | ||
auction_id: Option<auction::Id>, | ||
solution_id: Option<solution::Id>, | ||
kind: notify::Kind, | ||
) -> Self { | ||
Self { | ||
auction_id: auction_id.as_ref().map(ToString::to_string), | ||
solution_id: solution_id.map(SolutionId::from_domain), | ||
kind: match kind { | ||
notify::Kind::Timeout => Kind::Timeout, | ||
notify::Kind::EmptySolution => Kind::EmptySolution, | ||
notify::Kind::SimulationFailed(block, tx, succeeded_once) => { | ||
Kind::SimulationFailed { | ||
block: block.0, | ||
tx: Tx { | ||
from: tx.from.into(), | ||
to: tx.to.into(), | ||
input: tx.input.into(), | ||
value: tx.value.into(), | ||
access_list: tx.access_list.into(), | ||
}, | ||
succeeded_once, | ||
} | ||
pub fn new( | ||
auction_id: Option<auction::Id>, | ||
solution_id: Option<solution::Id>, | ||
kind: notify::Kind, | ||
) -> solvers_dto::notification::Notification { | ||
solvers_dto::notification::Notification { | ||
auction_id: auction_id.as_ref().map(|id| id.0), | ||
solution_id: solution_id.map(solution_id_from_domain), | ||
kind: match kind { | ||
notify::Kind::Timeout => solvers_dto::notification::Kind::Timeout, | ||
notify::Kind::EmptySolution => solvers_dto::notification::Kind::EmptySolution, | ||
notify::Kind::SimulationFailed(block, tx, succeeded_once) => { | ||
solvers_dto::notification::Kind::SimulationFailed { | ||
block: block.0, | ||
tx: solvers_dto::notification::Tx { | ||
from: tx.from.into(), | ||
to: tx.to.into(), | ||
input: tx.input.into(), | ||
value: tx.value.into(), | ||
access_list: tx.access_list.into(), | ||
}, | ||
succeeded_once, | ||
} | ||
notify::Kind::ScoringFailed(scoring) => scoring.into(), | ||
notify::Kind::NonBufferableTokensUsed(tokens) => Kind::NonBufferableTokensUsed { | ||
} | ||
notify::Kind::ScoringFailed(scoring) => scoring.into(), | ||
notify::Kind::NonBufferableTokensUsed(tokens) => { | ||
solvers_dto::notification::Kind::NonBufferableTokensUsed { | ||
tokens: tokens.into_iter().map(|token| token.0.0).collect(), | ||
}, | ||
notify::Kind::SolverAccountInsufficientBalance(required) => { | ||
Kind::SolverAccountInsufficientBalance { | ||
required: required.0, | ||
} | ||
} | ||
notify::Kind::DuplicatedSolutionId => Kind::DuplicatedSolutionId, | ||
notify::Kind::DriverError(reason) => Kind::DriverError { reason }, | ||
notify::Kind::Settled(kind) => match kind { | ||
notify::Settlement::Success(hash) => Kind::Success { | ||
transaction: hash.0, | ||
}, | ||
notify::Settlement::Revert(hash) => Kind::Revert { | ||
transaction: hash.0, | ||
}, | ||
notify::Settlement::SimulationRevert => Kind::Cancelled, | ||
notify::Settlement::Fail => Kind::Fail, | ||
notify::Settlement::Expired => Kind::Expired, | ||
} | ||
notify::Kind::SolverAccountInsufficientBalance(required) => { | ||
solvers_dto::notification::Kind::SolverAccountInsufficientBalance { | ||
required: required.0, | ||
} | ||
} | ||
notify::Kind::DuplicatedSolutionId => { | ||
solvers_dto::notification::Kind::DuplicatedSolutionId | ||
} | ||
notify::Kind::DriverError(reason) => { | ||
solvers_dto::notification::Kind::DriverError { reason } | ||
} | ||
notify::Kind::Settled(kind) => match kind { | ||
notify::Settlement::Success(hash) => solvers_dto::notification::Kind::Success { | ||
transaction: hash.0, | ||
}, | ||
notify::Kind::PostprocessingTimedOut => Kind::PostprocessingTimedOut, | ||
notify::Kind::Banned { reason, until } => Kind::Banned { | ||
reason: match reason { | ||
notify::BanReason::UnsettledConsecutiveAuctions => { | ||
BanReason::UnsettledConsecutiveAuctions | ||
} | ||
notify::BanReason::HighSettleFailureRate => { | ||
BanReason::HighSettleFailureRate | ||
} | ||
}, | ||
until, | ||
notify::Settlement::Revert(hash) => solvers_dto::notification::Kind::Revert { | ||
transaction: hash.0, | ||
}, | ||
notify::Settlement::SimulationRevert => solvers_dto::notification::Kind::Cancelled, | ||
notify::Settlement::Fail => solvers_dto::notification::Kind::Fail, | ||
notify::Settlement::Expired => solvers_dto::notification::Kind::Expired, | ||
}, | ||
} | ||
} | ||
} | ||
|
||
impl From<notify::ScoreKind> for Kind { | ||
fn from(value: notify::ScoreKind) -> Self { | ||
match value { | ||
notify::ScoreKind::InvalidClearingPrices => Kind::InvalidClearingPrices, | ||
notify::ScoreKind::InvalidExecutedAmount => Kind::InvalidExecutedAmount, | ||
notify::ScoreKind::MissingPrice(token_address) => Kind::MissingPrice { | ||
token_address: token_address.into(), | ||
notify::Kind::PostprocessingTimedOut => { | ||
solvers_dto::notification::Kind::PostprocessingTimedOut | ||
} | ||
notify::Kind::Banned { reason, until } => solvers_dto::notification::Kind::Banned { | ||
reason: match reason { | ||
notify::BanReason::UnsettledConsecutiveAuctions => { | ||
solvers_dto::notification::BanReason::UnsettledConsecutiveAuctions | ||
} | ||
notify::BanReason::HighSettleFailureRate => { | ||
solvers_dto::notification::BanReason::HighSettleFailureRate | ||
} | ||
}, | ||
until, | ||
}, | ||
} | ||
}, | ||
} | ||
} | ||
|
||
#[serde_as] | ||
#[derive(Debug, Serialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct Notification { | ||
auction_id: Option<String>, | ||
solution_id: Option<SolutionId>, | ||
#[serde(flatten)] | ||
kind: Kind, | ||
} | ||
|
||
#[serde_as] | ||
#[derive(Debug, Serialize)] | ||
#[serde(untagged)] | ||
pub enum SolutionId { | ||
Single(u64), | ||
Merged(Vec<u64>), | ||
fn solution_id_from_domain(id: solution::Id) -> solvers_dto::notification::SolutionId { | ||
match id.solutions().len() { | ||
1 => solvers_dto::notification::SolutionId::Single(*id.solutions().first().unwrap()), | ||
_ => solvers_dto::notification::SolutionId::Merged(id.solutions().to_vec()), | ||
} | ||
} | ||
|
||
impl SolutionId { | ||
pub fn from_domain(id: solution::Id) -> Self { | ||
match id.solutions().len() { | ||
1 => SolutionId::Single(*id.solutions().first().unwrap()), | ||
_ => SolutionId::Merged(id.solutions().to_vec()), | ||
impl From<notify::ScoreKind> for solvers_dto::notification::Kind { | ||
fn from(value: notify::ScoreKind) -> Self { | ||
match value { | ||
notify::ScoreKind::InvalidClearingPrices => { | ||
solvers_dto::notification::Kind::InvalidClearingPrices | ||
} | ||
notify::ScoreKind::InvalidExecutedAmount => { | ||
solvers_dto::notification::Kind::InvalidExecutedAmount | ||
} | ||
notify::ScoreKind::MissingPrice(token_address) => { | ||
solvers_dto::notification::Kind::MissingPrice { | ||
token_address: token_address.into(), | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[serde_as] | ||
#[derive(Debug, Serialize)] | ||
#[serde(rename_all = "camelCase", tag = "kind")] | ||
pub enum Kind { | ||
Timeout, | ||
EmptySolution, | ||
DuplicatedSolutionId, | ||
#[serde(rename_all = "camelCase")] | ||
SimulationFailed { | ||
block: BlockNo, | ||
tx: Tx, | ||
succeeded_once: bool, | ||
}, | ||
InvalidClearingPrices, | ||
#[serde(rename_all = "camelCase")] | ||
MissingPrice { | ||
token_address: eth::H160, | ||
}, | ||
InvalidExecutedAmount, | ||
NonBufferableTokensUsed { | ||
tokens: BTreeSet<eth::H160>, | ||
}, | ||
SolverAccountInsufficientBalance { | ||
#[serde_as(as = "serialize::U256")] | ||
required: eth::U256, | ||
}, | ||
Success { | ||
transaction: eth::H256, | ||
}, | ||
Revert { | ||
transaction: eth::H256, | ||
}, | ||
DriverError { | ||
reason: String, | ||
}, | ||
Cancelled, | ||
Expired, | ||
Fail, | ||
PostprocessingTimedOut, | ||
Banned { | ||
reason: BanReason, | ||
until: DateTime<Utc>, | ||
}, | ||
} | ||
|
||
#[derive(Debug, Serialize)] | ||
#[serde(rename_all = "camelCase", tag = "reason")] | ||
pub enum BanReason { | ||
UnsettledConsecutiveAuctions, | ||
HighSettleFailureRate, | ||
} | ||
|
||
type BlockNo = u64; | ||
|
||
#[serde_as] | ||
#[derive(Debug, Serialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct Tx { | ||
pub from: eth::H160, | ||
pub to: eth::H160, | ||
#[serde_as(as = "serialize::Hex")] | ||
pub input: Vec<u8>, | ||
#[serde_as(as = "serialize::U256")] | ||
pub value: eth::U256, | ||
pub access_list: AccessList, | ||
} |
Oops, something went wrong.