-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Metrics-based bad token detector (#3172)
# Description Follow-up to #3156. This PR introduces an in-memory, ratio-based bad token detection strategy that complements the existing heuristics. Instead of relying on consecutive failures, it keeps track of both successful and failed settlement attempts for each token. The logic is as follows: 1. When a settlement encoding fails, every token involved in that attempt has its statistics updated: both total attempts and failed attempts are incremented. 2. Otherwise, every token involved has its total attempts incremented but not its failures. 3. Before marking a token as unsupported, the detector requires at least 20 recorded attempts. Once this threshold is met, if the token's failure ratio (`fails / attempts`) is at least 90%, it is considered unsupported. This approach is more resilient than just counting consecutive failures. A highly utilized and generally reliable token that occasionally appears in failing trades with problematic tokens won't be prematurely flagged as unsupported because its overall success ratio remains high. Due to the nature of the implementation, all the statistics get discarded on every restart. Implementing a persistence layer might make sense in the future, but problems with bad tokens are usually temporal. ## How to test A forked e2e test for the simulation-based detector is required first, and it is expected to be implemented in a separate PR. --------- Co-authored-by: MartinquaXD <[email protected]> Co-authored-by: Mateo <[email protected]>
- Loading branch information
1 parent
1451574
commit 7e52015
Showing
8 changed files
with
115 additions
and
12 deletions.
There are no files selected for viewing
61 changes: 56 additions & 5 deletions
61
crates/driver/src/domain/competition/bad_tokens/metrics.rs
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,11 +1,62 @@ | ||
use {super::Quality, crate::domain::eth}; | ||
use {super::Quality, crate::domain::eth, dashmap::DashMap, std::sync::Arc}; | ||
|
||
/// Monitors tokens to determine whether they are considered "unsupported" based | ||
/// on the ratio of failing to total settlement encoding attempts. A token must | ||
/// have participated in at least `REQUIRED_MEASUREMENTS` attempts to be | ||
/// evaluated. If, at that point, the ratio of failures is greater than or equal | ||
/// to `FAILURE_RATIO`, the token is considered unsupported. | ||
#[derive(Default, Clone)] | ||
pub struct Detector(Arc<Inner>); | ||
|
||
#[derive(Default)] | ||
pub struct Detector; | ||
struct Inner { | ||
counter: DashMap<eth::TokenAddress, TokenStatistics>, | ||
} | ||
|
||
#[derive(Default)] | ||
struct TokenStatistics { | ||
attempts: u32, | ||
fails: u32, | ||
} | ||
|
||
impl Detector { | ||
pub fn get_quality(&self, _token: eth::TokenAddress) -> Option<Quality> { | ||
// TODO implement a reasonable heuristic | ||
None | ||
/// The ratio of failures to attempts that qualifies a token as unsupported. | ||
const FAILURE_RATIO: f64 = 0.9; | ||
/// The minimum number of attempts required before evaluating a token’s | ||
/// quality. | ||
const REQUIRED_MEASUREMENTS: u32 = 20; | ||
|
||
pub fn get_quality(&self, token: ð::TokenAddress) -> Option<Quality> { | ||
let measurements = self.0.counter.get(token)?; | ||
let is_unsupported = measurements.attempts >= Self::REQUIRED_MEASUREMENTS | ||
&& (measurements.fails as f64 / measurements.attempts as f64) >= Self::FAILURE_RATIO; | ||
|
||
is_unsupported.then_some(Quality::Unsupported) | ||
} | ||
|
||
/// Updates the tokens that participated in settlements by | ||
/// incrementing their attempt count. | ||
/// `failure` indicates whether the settlement was successful or not. | ||
pub fn update_tokens( | ||
&self, | ||
token_pairs: &[(eth::TokenAddress, eth::TokenAddress)], | ||
failure: bool, | ||
) { | ||
token_pairs | ||
.iter() | ||
.flat_map(|(token_a, token_b)| [token_a, token_b]) | ||
.for_each(|token| { | ||
self.0 | ||
.counter | ||
.entry(*token) | ||
.and_modify(|counter| { | ||
counter.attempts += 1; | ||
counter.fails += u32::from(failure) | ||
}) | ||
.or_insert_with(|| TokenStatistics { | ||
attempts: 1, | ||
fails: u32::from(failure), | ||
}); | ||
}); | ||
} | ||
} |
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
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
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
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