Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/src/resolvers/query_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ impl QueryResolver {

let history = self
.validator_store
.find_by_vote_account(&vote_account, req.start_epoch, req.end_epoch)
.find_pool_stake_by_vote_account(&vote_account, req.start_epoch, req.end_epoch)
.await?
.into_iter()
.map(|v| JitoSolValidatorStakeHistoryEntry {
Expand Down
35 changes: 29 additions & 6 deletions core/src/db_models/validators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,18 @@ pub struct TotalStakeDbResult {
pub total_stake_lamports: u64,
}

/// Projection of a validator record down to its JitoSOL pool stake. Read through this
/// rather than [`Validator`] so records written before the pool fields existed still
/// deserialize (they read as "not staked" instead of failing the whole query).
#[derive(Debug, Deserialize)]
pub struct ValidatorPoolStake {
pub epoch: u64,
#[serde(default)]
pub target_pool_active_lamports: u64,
#[serde(default)]
pub target_pool_staked: bool,
}

impl ValidatorStore {
pub const COLLECTION: &'static str = VALIDATOR_COLLECTION_NAME;

Expand Down Expand Up @@ -216,14 +228,14 @@ impl ValidatorStore {
Ok(validators_map.into_values().collect::<Vec<Validator>>())
}

/// All epoch entries for one validator, ascending by epoch, optionally bounded to
/// `[start_epoch, end_epoch]`. There is one document per (epoch, vote_account).
pub async fn find_by_vote_account(
/// JitoSOL pool stake for one validator at every epoch on record, ascending, optionally
/// bounded to `[start_epoch, end_epoch]`. There is one document per (epoch, vote_account).
pub async fn find_pool_stake_by_vote_account(
&self,
vote_account: &str,
start_epoch: Option<u64>,
end_epoch: Option<u64>,
) -> Result<Vec<Validator>, DataStoreError> {
) -> Result<Vec<ValidatorPoolStake>, DataStoreError> {
let mut filter = doc! { "vote_account": vote_account };
let mut epoch_filter = doc! {};
if let Some(start_epoch) = start_epoch {
Expand All @@ -236,8 +248,19 @@ impl ValidatorStore {
filter.insert("epoch", epoch_filter);
}

let find_options = FindOptions::builder().sort(doc! { "epoch": 1 }).build();
let cursor = self.collection.find(filter, find_options).await?;
let find_options = FindOptions::builder()
.sort(doc! { "epoch": 1 })
.projection(doc! {
"epoch": 1,
"target_pool_active_lamports": 1,
"target_pool_staked": 1,
})
.build();
let cursor = self
.collection
.clone_with_type::<ValidatorPoolStake>()
.find(filter, find_options)
.await?;
Ok(cursor.try_collect().await?)
}

Expand Down
Loading