Skip to content

Commit 072f4f8

Browse files
committed
feat: store is_eligible
1 parent 3c26dc5 commit 072f4f8

File tree

3 files changed

+83
-20
lines changed

3 files changed

+83
-20
lines changed

bam-writer-service/src/lib.rs

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use stakenet_sdk::utils::accounts::{get_all_validator_history_accounts, get_stak
1212

1313
use crate::{
1414
bam_delegation_criteria::BamDelegationCriteria,
15-
bam_validator_eligibility::BamValidatorEligibility,
15+
bam_validator_eligibility::{BamValidatorEligibility, IneligibilityReason},
1616
};
1717

1818
mod bam_delegation_criteria;
@@ -106,33 +106,62 @@ impl BamWriterService {
106106
.await?;
107107

108108
let eligibility_checker = BamValidatorEligibility::new(epoch, &validator_histories);
109-
let mut bam_eligible_validators: Vec<BamValidator> = Vec::new();
109+
let mut bam_validators: Vec<BamValidator> = Vec::new();
110110

111111
for validator_history in validator_histories.iter() {
112112
if let Some(vote_account) = bam_validator_map.get(&validator_history.vote_account) {
113+
let vote_pubkey = &vote_account.vote_pubkey;
114+
let mut bam_validator = BamValidator::new(
115+
vote_account.activated_stake,
116+
epoch,
117+
&vote_account.node_pubkey,
118+
false,
119+
vote_pubkey,
120+
);
121+
113122
match eligibility_checker.check_eligibility(validator_history) {
114123
Ok(()) => {
115-
let bam_validator = BamValidator::new(
116-
vote_account.activated_stake,
117-
epoch,
118-
&vote_account.node_pubkey,
119-
&vote_account.vote_pubkey,
120-
);
121-
bam_eligible_validators.push(bam_validator);
124+
bam_validator.set_is_eligible(true);
122125
}
123126
Err(reason) => {
124-
log::debug!(
125-
"Validator {} ineligible: {:?}",
126-
vote_account.vote_pubkey,
127-
reason
128-
);
127+
let reason_string = match reason {
128+
IneligibilityReason::NotBamClient => "NotBamClient".to_string(),
129+
IneligibilityReason::NonZeroCommission { epoch, commission } => {
130+
format!("NonZeroCommission: {} in epoch {}", commission, epoch)
131+
}
132+
IneligibilityReason::HighMevCommission {
133+
epoch,
134+
mev_commission,
135+
} => {
136+
format!("HighMevCommission: {} in epoch {}", mev_commission, epoch)
137+
}
138+
IneligibilityReason::InSuperminority { epoch } => {
139+
format!("InSuperminority in epoch {}", epoch)
140+
}
141+
IneligibilityReason::LowVoteCredits {
142+
epoch,
143+
credits,
144+
min_required,
145+
} => {
146+
format!(
147+
"LowVoteCredits: {} credits (required: {}) in epoch {}",
148+
credits, min_required, epoch
149+
)
150+
}
151+
IneligibilityReason::InsufficientHistory => {
152+
"InsufficientHistory: Less than 3 epochs".to_string()
153+
}
154+
};
155+
bam_validator.set_ineligibility_reason(Some(reason_string));
129156
}
130157
}
158+
159+
bam_validators.push(bam_validator);
131160
}
132161
}
133162

134163
self.bam_validators_store
135-
.insert_many(&bam_eligible_validators)
164+
.insert_many(&bam_validators)
136165
.await?;
137166

138167
let total_stake = vote_accounts
@@ -141,8 +170,11 @@ impl BamWriterService {
141170
.map(|v| v.activated_stake)
142171
.sum();
143172

144-
let eligible_bam_validator_count = bam_eligible_validators.len() as u64;
145-
let bam_stake = bam_eligible_validators
173+
let eligible_bam_validators = bam_validators
174+
.into_iter()
175+
.filter(|bv| bv.is_eligible())
176+
.collect::<Vec<BamValidator>>();
177+
let bam_stake = eligible_bam_validators
146178
.iter()
147179
.map(|v| v.get_active_stake())
148180
.sum();
@@ -152,7 +184,7 @@ impl BamWriterService {
152184
bam_stake,
153185
total_stake,
154186
jitosol_stake,
155-
eligible_bam_validator_count,
187+
eligible_bam_validators.len() as u64,
156188
);
157189

158190
let previous_epoch_metric = if let Some(prev_epoch) = epoch.checked_sub(1) {

core/src/db_models/bam_epoch_metric.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub struct BamEpochMetric {
1515
/// Available BAM delegation stake amount in lamports
1616
available_bam_delegation_stake: u64,
1717

18-
/// Total stake amount of BAM validators in lamports
18+
/// Total stake amount of BAM eligible validators in lamports
1919
bam_stake: u64,
2020

2121
/// Eligible BAM validator count

core/src/db_models/bam_validators.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ pub struct BamValidator {
2121
/// Identity account public key
2222
identity_account: String,
2323

24+
/// Is eligible validator
25+
is_eligible: bool,
26+
27+
// The reason of ineligibility
28+
ineligibility_reason: Option<String>,
29+
2430
/// Timestamp
2531
#[serde(with = "ts_seconds")]
2632
timestamp: DateTime<Utc>,
@@ -37,6 +43,8 @@ impl Default for BamValidator {
3743
active_stake: 0,
3844
epoch: 0,
3945
identity_account: String::new(),
46+
is_eligible: false,
47+
ineligibility_reason: None,
4048
timestamp,
4149
vote_account: String::new(),
4250
}
@@ -45,13 +53,21 @@ impl Default for BamValidator {
4553

4654
impl BamValidator {
4755
/// Initialize a [`BamValidator`]
48-
pub fn new(active_stake: u64, epoch: u64, identity_account: &str, vote_account: &str) -> Self {
56+
pub fn new(
57+
active_stake: u64,
58+
epoch: u64,
59+
identity_account: &str,
60+
is_eligible: bool,
61+
vote_account: &str,
62+
) -> Self {
4963
let timestamp = Utc::now();
5064

5165
Self {
5266
active_stake,
5367
epoch,
5468
identity_account: identity_account.to_string(),
69+
is_eligible,
70+
ineligibility_reason: None,
5571
timestamp,
5672
vote_account: vote_account.to_string(),
5773
}
@@ -77,6 +93,21 @@ impl BamValidator {
7793
self.epoch = epoch;
7894
}
7995

96+
/// Eligible validator
97+
pub fn is_eligible(&self) -> bool {
98+
self.is_eligible
99+
}
100+
101+
/// Set is eligible validator
102+
pub fn set_is_eligible(&mut self, is_eligible: bool) {
103+
self.is_eligible = is_eligible;
104+
}
105+
106+
/// Get ineligiblity reason
107+
pub fn set_ineligibility_reason(&mut self, ineligibility_reason: Option<String>) {
108+
self.ineligibility_reason = ineligibility_reason;
109+
}
110+
80111
/// Get identity account pubkey (node public key)
81112
pub fn get_identity_account(&self) -> Result<Pubkey, ParsePubkeyError> {
82113
Pubkey::from_str(&self.identity_account)

0 commit comments

Comments
 (0)