Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deal with case where validators.app returns null values we weren't ex… #512

Merged
merged 1 commit into from
Oct 10, 2023
Merged
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
31 changes: 19 additions & 12 deletions bot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2756,13 +2756,16 @@ fn calculate_commission_at_end_of_epoch(
Some(records) => {
// First check if there is a commission change record in `epoch`. The last one will
// give us the commision at the end of the epoch.
let mut rs: Vec<&CommissionChangeIndexHistoryEntry> =
records.iter().filter(|r| r.epoch <= epoch).collect();
let mut rs: Vec<&CommissionChangeIndexHistoryEntry> = records
.iter()
.filter(|r| r.epoch.is_some() && r.epoch.unwrap() <= epoch)
.collect();

if !rs.is_empty() {
rs.sort_by(|a, b| {
a.epoch
.cmp(&b.epoch)
.unwrap()
.cmp(&b.epoch.unwrap())
.then(a.epoch_completion.partial_cmp(&b.epoch_completion).unwrap())
});
rs.last().unwrap().commission_after.unwrap() as u8
Expand All @@ -2771,7 +2774,11 @@ fn calculate_commission_at_end_of_epoch(
// `epoch + 1`. The first one will give us the commission at the end of `epoch`.
let mut rs: Vec<&CommissionChangeIndexHistoryEntry> = records
.iter()
.filter(|r| r.commission_before.is_some() && r.epoch > epoch)
.filter(|r| {
r.commission_before.is_some()
&& r.epoch.is_some()
&& r.epoch.unwrap() > epoch
})
.collect();
if rs.is_empty() {
// no commission changes in epoch `epoch + 1`; commission is the current
Expand Down Expand Up @@ -2917,7 +2924,7 @@ mod test {
CommissionChangeIndexHistoryEntry {
commission_before: Some(expected_commission as f32),
commission_after: Some(10.0),
epoch: epoch + 2,
epoch: Some(epoch + 2),
epoch_completion: 50.0,
..Default::default()
},
Expand Down Expand Up @@ -2946,31 +2953,31 @@ mod test {
CommissionChangeIndexHistoryEntry {
commission_before: Some(50.0),
commission_after: Some(40.0),
epoch: epoch + 1,
epoch: Some(epoch + 1),
epoch_completion: 50.0,
..Default::default()
},
// first
CommissionChangeIndexHistoryEntry {
commission_before: None,
commission_after: Some(10.0),
epoch: 120,
epoch: Some(120),
epoch_completion: 10.0,
..Default::default()
},
// second
CommissionChangeIndexHistoryEntry {
commission_before: Some(10.0),
commission_after: Some(expected_commission),
epoch,
epoch: Some(epoch),
epoch_completion: 99.0,
..Default::default()
},
// third
CommissionChangeIndexHistoryEntry {
commission_before: Some(expected_commission),
commission_after: Some(50.0),
epoch: epoch + 1,
epoch: Some(epoch + 1),
epoch_completion: 10.0,
..Default::default()
},
Expand All @@ -2993,7 +3000,7 @@ mod test {
let history = [CommissionChangeIndexHistoryEntry {
commission_before: Some(expected_commission),
commission_after: Some(current_commission),
epoch: epoch + 1,
epoch: Some(epoch + 1),
epoch_completion: 50.0,
..Default::default()
}]
Expand All @@ -3017,14 +3024,14 @@ mod test {
CommissionChangeIndexHistoryEntry {
commission_before: Some(expected_commission),
commission_after: Some(10.0),
epoch: epoch + 1,
epoch: Some(epoch + 1),
epoch_completion: 50.0,
..Default::default()
},
CommissionChangeIndexHistoryEntry {
commission_before: Some(10.0),
commission_after: Some(50.0),
epoch: epoch + 1,
epoch: Some(epoch + 1),
epoch_completion: 60.0,
..Default::default()
},
Expand Down
5 changes: 3 additions & 2 deletions bot/src/validators_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ pub struct CommissionChangeIndexHistoryEntry {
pub commission_before: Option<f32>,
// This has shown up as null in at least once case. Not sure what it indicates.
pub commission_after: Option<f32>,
pub epoch: u64,
// recently there was an example of epoch being null
pub epoch: Option<u64>,
pub network: String,
pub id: i32,
pub epoch_completion: f32,
Expand All @@ -154,7 +155,7 @@ impl Default for CommissionChangeIndexHistoryEntry {
created_at: "".to_string(),
commission_before: None,
commission_after: None,
epoch: 0,
epoch: None,
network: "".to_string(),
id: 0,
epoch_completion: 0.0,
Expand Down
Loading