Skip to content

Commit

Permalink
fix: subgraph versions behind
Browse files Browse the repository at this point in the history
  • Loading branch information
Theodus committed Dec 16, 2023
1 parent 53a390d commit f1ced33
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 22 deletions.
13 changes: 12 additions & 1 deletion graph-gateway/src/client_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,13 @@ async fn handle_client_query_inner(
budget_grt = f64::from(budget.0) as f32,
);

let versions_behind: BTreeMap<DeploymentId, u8> = deployments
.iter()
.rev()
.enumerate()
.map(|(index, deployment)| (deployment.id, index.try_into().unwrap_or(u8::MAX)))
.collect();

let candidates: Vec<Candidate> = candidates
.into_iter()
.filter_map(|indexing| {
Expand All @@ -432,7 +439,11 @@ async fn handle_client_query_inner(
return None;
}
};
Some(Candidate { indexing, fee })
Some(Candidate {
indexing,
fee,
versions_behind: *versions_behind.get(&indexing.deployment).unwrap_or(&0),
})
})
.collect();

Expand Down
5 changes: 1 addition & 4 deletions graph-gateway/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,13 +444,11 @@ async fn write_indexer_inputs(

let mut indexings: HashMap<Indexing, indexer_selection::IndexingStatus> = HashMap::new();
let mut allocations: HashMap<Indexing, Address> = HashMap::new();
for (versions_behind, (deployment, indexer)) in deployments.values().flat_map(|deployment| {
for (deployment, indexer) in deployments.values().flat_map(|deployment| {
deployment
.indexers
.iter()
.map(move |indexer| (deployment.as_ref(), indexer.as_ref()))
.rev()
.enumerate()
}) {
let indexing = Indexing {
indexer: indexer.id,
Expand All @@ -469,7 +467,6 @@ async fn write_indexer_inputs(
behind_reported_block: false,
min_block: status.min_block,
}),
versions_behind: versions_behind.try_into().unwrap_or(u8::MAX),
};
allocations.insert(indexing, indexer.largest_allocation);
indexings.insert(indexing, update);
Expand Down
1 change: 0 additions & 1 deletion indexer-selection/src/indexing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ pub struct IndexingStatus {
pub stake: GRT,
pub allocation: GRT,
pub block: Option<BlockStatus>,
pub versions_behind: u8,
}

/// We compare candidate indexers based on their last reported block. Any observation of the indexer
Expand Down
5 changes: 3 additions & 2 deletions indexer-selection/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const MIN_SCORE_CUTOFF: f64 = 0.25;
pub struct Candidate {
pub indexing: Indexing,
pub fee: GRT,
pub versions_behind: u8,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -288,7 +289,7 @@ impl State {
params,
reliability,
perf_success,
state.status.versions_behind,
candidate.versions_behind,
blocks_behind,
slashable_usd,
zero_allocation,
Expand All @@ -300,7 +301,7 @@ impl State {
Ok(SelectionFactors {
indexing: candidate.indexing,
url: state.status.url.clone(),
versions_behind: state.status.versions_behind,
versions_behind: candidate.versions_behind,
reliability,
perf_success,
perf_failure: state.perf_failure.expected_value(),
Expand Down
2 changes: 1 addition & 1 deletion indexer-selection/src/simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ pub async fn simulate(
behind_reported_block: false,
min_block: None,
}),
versions_behind: 0,
},
);
}
Expand All @@ -84,6 +83,7 @@ pub async fn simulate(
deployment,
},
fee: c.fee,
versions_behind: 0,
})
.collect();
let characteristics: HashMap<&Address, &IndexerCharacteristics> =
Expand Down
21 changes: 8 additions & 13 deletions indexer-selection/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ fn base_indexing_status() -> IndexingStatus {
behind_reported_block: false,
min_block: None,
}),
versions_behind: 0,
}
}

Expand Down Expand Up @@ -265,6 +264,7 @@ async fn fuzz() {
Candidate {
fee: *topology.fees.get(&indexing).unwrap(),
indexing,
versions_behind: 0,
}
})
.collect();
Expand All @@ -284,19 +284,20 @@ async fn fuzz() {
fn favor_higher_version() {
let mut rng = SmallRng::from_entropy();

let mut versions_behind = [rng.gen_range(0..3), rng.gen_range(0..3)];
if versions_behind[0] == versions_behind[1] {
versions_behind[1] += 1;
}
let candidates: Vec<Candidate> = (0..2)
.map(|i| Candidate {
indexing: Indexing {
indexer: bytes_from_id(0).into(),
deployment: bytes_from_id(i).into(),
},
fee: GRT(UDecimal18::from(1)),
versions_behind: versions_behind[i],
})
.collect();
let mut versions_behind = [rng.gen_range(0..3), rng.gen_range(0..3)];
if versions_behind[0] == versions_behind[1] {
versions_behind[1] += 1;
}

let mut state = State {
network_params: NetworkParameters {
Expand All @@ -307,17 +308,11 @@ fn favor_higher_version() {
};
state.indexings.insert(
candidates[0].indexing,
IndexingState::new(IndexingStatus {
versions_behind: versions_behind[0],
..base_indexing_status()
}),
IndexingState::new(base_indexing_status()),
);
state.indexings.insert(
candidates[1].indexing,
IndexingState::new(IndexingStatus {
versions_behind: versions_behind[1],
..base_indexing_status()
}),
IndexingState::new(base_indexing_status()),
);

let params = utiliy_params(
Expand Down

0 comments on commit f1ced33

Please sign in to comment.