Skip to content

Commit

Permalink
increase sensitivity for seconds_behind
Browse files Browse the repository at this point in the history
  • Loading branch information
Theodus committed Apr 30, 2024
1 parent 7b79c49 commit 24b960e
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 44 deletions.
55 changes: 12 additions & 43 deletions indexer-selection/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,22 @@ impl candidate_selection::Candidate for Candidate {
}
}

// When picking curves to use consider the following reference:
// https://en.wikipedia.org/wiki/Logistic_function

/// Avoid serving deployments at versions behind, unless newer versions have poor indexer support.
fn score_subgraph_versions_behind(subgraph_versions_behind: u8) -> Normalized {
Normalized::new(0.25_f64.powi(subgraph_versions_behind as i32)).unwrap()
}

/// https://www.desmos.com/calculator/wmgkasfvza
/// https://www.desmos.com/calculator/gzmp7rbiai
fn score_seconds_behind(seconds_behind: u32) -> Normalized {
Normalized::new(1.0 - E.powf(-32.0 / seconds_behind.max(1) as f64)).unwrap()
let b: f64 = 1e-6;
let l: f64 = 1.6;
let k: f64 = 0.017;
let x_0: i64 = 30;
let u = b + (l / (1.0 + E.powf(k * (seconds_behind as i64 - x_0) as f64)));
Normalized::new(u).unwrap()
}

/// https://www.desmos.com/calculator/iqhjcdnphv
Expand All @@ -144,50 +152,11 @@ fn score_zero_allocation(zero_allocation: bool) -> Normalized {

/// https://www.desmos.com/calculator/v2vrfktlpl
pub fn score_latency(latency_ms: u32) -> Normalized {
let sigmoid = |x: u32| 1.0 + std::f64::consts::E.powf(((x as f64) - 400.0) / 300.0);
Normalized::new(sigmoid(0) / sigmoid(latency_ms)).unwrap()
let s = |x: u32| 1.0 + E.powf(((x as f64) - 400.0) / 300.0);
Normalized::new(s(0) / s(latency_ms)).unwrap()
}

/// https://www.desmos.com/calculator/df2keku3ad
fn score_success_rate(success_rate: Normalized) -> Normalized {
Normalized::new(success_rate.as_f64().powi(7).max(0.01)).unwrap()
}

#[cfg(test)]
mod tests {
use candidate_selection::criteria::performance::ExpectedPerformance;
use candidate_selection::Normalized;

use super::Candidate;

#[test]
fn candidate_should_use_url_display_for_debug() {
//* Given
let expected_url = "https://example.com/candidate/test/url";

let candidate = Candidate {
indexer: Default::default(),
deployment: "QmWmyoMoctfbAaiEs2G46gpeUmhqFRDW6KWo64y5r581Vz"
.parse()
.unwrap(),
url: expected_url.parse().expect("valid url"),
perf: ExpectedPerformance {
success_rate: Normalized::ONE,
latency_success_ms: 0,
latency_failure_ms: 0,
},
fee: Normalized::ONE,
seconds_behind: 0,
slashable_grt: 0,
subgraph_versions_behind: 0,
zero_allocation: false,
};

//* When
let debug = format!("{:?}", candidate);

//* Then
// Assert that the debug string contains the url in the expected format
assert!(debug.contains(expected_url));
}
}
81 changes: 80 additions & 1 deletion indexer-selection/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::*;
use candidate_selection::{num::assert_within, Candidate as _};
use proptest::{prop_assert, prop_compose, proptest};
use std::ops::RangeInclusive;
use thegraph_core::types::alloy_primitives::{hex, FixedBytes};

mod limits {
use super::*;
Expand All @@ -21,7 +22,7 @@ prop_compose! {
fn candidate()(
fee in normalized(),
subgraph_versions_behind in 0..=3_u8,
seconds_behind: u16,
seconds_behind in 0..=7500_u16,
slashable_grt: u32,
zero_allocation: bool,
avg_latency_ms: u16,
Expand Down Expand Up @@ -84,3 +85,81 @@ proptest! {
}
}
}
#[test]
fn sensitivity_seconds_behind() {
let candidates = [
Candidate {
indexer: hex!("0000000000000000000000000000000000000000").into(),
deployment: hex!("0000000000000000000000000000000000000000000000000000000000000000")
.into(),
url: "https://example.com".parse().unwrap(),
perf: ExpectedPerformance {
success_rate: Normalized::ONE,
latency_success_ms: 0,
latency_failure_ms: 0,
},
fee: Normalized::ZERO,
seconds_behind: 86400,
slashable_grt: 1_000_000,
subgraph_versions_behind: 0,
zero_allocation: false,
},
Candidate {
indexer: hex!("0000000000000000000000000000000000000001").into(),
deployment: hex!("0000000000000000000000000000000000000000000000000000000000000000")
.into(),
url: "https://example.com".parse().unwrap(),
perf: ExpectedPerformance {
success_rate: Normalized::new(0.5).unwrap(),
latency_success_ms: 1000,
latency_failure_ms: 1000,
},
fee: Normalized::ONE,
seconds_behind: 120,
slashable_grt: 100_000,
subgraph_versions_behind: 0,
zero_allocation: false,
},
];

println!(
"score {} {:?}",
candidates[0].indexer,
candidates[0].score(),
);
println!(
"score {} {:?}",
candidates[1].indexer,
candidates[1].score(),
);
assert!(candidates[0].score() <= candidates[1].score());

let selections: ArrayVec<&Candidate, 3> = crate::select(&candidates);
assert_eq!(1, selections.len(), "select exatly one candidate");
assert_eq!(
Some(candidates[1].indexer),
selections.first().map(|s| s.indexer),
"select candidate closer to chain head",
);
}

#[test]
fn candidate_should_use_url_display_for_debug() {
let expected_url = "https://example.com/candidate/test/url";
let candidate = Candidate {
indexer: Default::default(),
deployment: FixedBytes::default().into(),
url: expected_url.parse().expect("valid url"),
perf: ExpectedPerformance {
success_rate: Normalized::ZERO,
latency_success_ms: 0,
latency_failure_ms: 0,
},
fee: Normalized::ZERO,
seconds_behind: 0,
slashable_grt: 0,
subgraph_versions_behind: 0,
zero_allocation: false,
};
assert!(format!("{candidate:?}").contains(expected_url));
}

0 comments on commit 24b960e

Please sign in to comment.