Skip to content

Commit

Permalink
Subtle math fixes for cluster history slot counting
Browse files Browse the repository at this point in the history
  • Loading branch information
ebatsell committed Mar 7, 2024
1 parent eb131bb commit d8eaa6a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
1 change: 1 addition & 0 deletions programs/validator-history/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ no-log-ix-name = []
cpi = ["no-entrypoint"]
default = ["custom-heap"]
custom-heap = []
testnet = []

[dependencies]
anchor-lang = "0.28.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ pub fn handle_copy_cluster_info(ctx: Context<CopyClusterInfo>) -> Result<()> {
};

let start_slot = epoch_schedule.get_first_slot_in_epoch(epoch.into());
let end_slot = epoch_schedule.get_last_slot_in_epoch(epoch.into());
let (num_blocks, _) = confirmed_blocks_in_epoch(start_slot, end_slot, *slot_history)?;
let (num_blocks, _) = confirmed_blocks_in_epoch(start_slot, clock.slot, *slot_history)?;
cluster_history_account.set_blocks(epoch, num_blocks)?;
cluster_history_account.set_epoch_start_timestamp(epoch, epoch_start_timestamp)?;

Expand Down Expand Up @@ -95,11 +94,13 @@ pub fn confirmed_blocks_in_epoch(
.ok_or(ValidatorHistoryError::ArithmeticError)?,
)
.ok_or(ValidatorHistoryError::ArithmeticError)?
.min(end_slot)
};

let last_full_block_slot = end_slot
.checked_sub(end_slot % BITVEC_BLOCK_SIZE)
.ok_or(ValidatorHistoryError::ArithmeticError)?;
.ok_or(ValidatorHistoryError::ArithmeticError)?
.max(first_full_block_slot);

// First and last slots, in partial blocks
for i in (start_slot..first_full_block_slot).chain(last_full_block_slot..=end_slot) {
Expand Down
24 changes: 19 additions & 5 deletions tests/tests/test_cluster_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,15 @@ async fn test_copy_cluster_info() {
.await
.expect("clock");

assert!(clock.epoch == 1);
assert!(account.history.idx == 1);
assert!(account.history.arr[0].epoch == 0);
assert!(account.history.arr[0].total_blocks == 3);
assert!(clock.epoch == 1);
assert!(clock.slot == 32);
assert!(clock.slot == latest_slot);
assert!(account.cluster_history_last_update_slot == latest_slot);
assert!(account.history.arr[1].epoch == 1);
assert!(account.history.arr[1].total_blocks == 2);
assert_eq!(account.cluster_history_last_update_slot, latest_slot)
assert!(account.history.arr[1].total_blocks == 1);
}

#[tokio::test]
Expand Down Expand Up @@ -170,7 +172,7 @@ async fn test_cluster_history_compute_limit() {
assert!(account.history.arr[0].epoch as u64 == clock.epoch - 1);
assert!(account.history.arr[0].total_blocks == 216_000);
assert!(account.history.arr[1].epoch as u64 == clock.epoch);
assert!(account.history.arr[1].total_blocks == 216_000);
assert!(account.history.arr[1].total_blocks == 1);
}

// Non-fixture test to ensure that the SlotHistory partial slot logic works
Expand All @@ -183,6 +185,18 @@ fn test_confirmed_blocks_in_epoch_partial_blocks() {
// First partial block: 50 -> 64
// Full block: 64 -> 127
// Last partial block: 128 -> 149
let (num_blocks, _) = confirmed_blocks_in_epoch(50, 149, slot_history).unwrap();
let (num_blocks, _) = confirmed_blocks_in_epoch(50, 149, slot_history.clone()).unwrap();
assert_eq!(num_blocks, 100);

let (num_blocks, _) = confirmed_blocks_in_epoch(50, 99, slot_history.clone()).unwrap();
assert_eq!(num_blocks, 50);

let (num_blocks, _) = confirmed_blocks_in_epoch(64, 127, slot_history.clone()).unwrap();
assert_eq!(num_blocks, 64);

let (num_blocks, _) = confirmed_blocks_in_epoch(64, 64, slot_history.clone()).unwrap();
assert_eq!(num_blocks, 1);

let (num_blocks, _) = confirmed_blocks_in_epoch(100, 149, slot_history.clone()).unwrap();
assert_eq!(num_blocks, 50);
}

0 comments on commit d8eaa6a

Please sign in to comment.