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

ledger: fix last_epoch to only change when committing block #1249

Closed
wants to merge 3 commits into from
Closed
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: 2 additions & 0 deletions .changelog/unreleased/bug-fixes/1249-fix-shell-last-epoch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Fix the `last_epoch` field in the shell to only be updated when the block is
committed. ([#1249](https://github.com/anoma/anoma/pull/1249))
13 changes: 4 additions & 9 deletions shared/src/ledger/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ where
};
self.db.write_block(state)?;
self.last_height = self.block.height;
self.last_epoch = self.block.epoch;
self.header = None;
Ok(())
}
Expand Down Expand Up @@ -600,8 +601,6 @@ where
if new_epoch {
// Begin a new epoch
self.block.epoch = self.block.epoch.next();
self.last_epoch = self.last_epoch.next();
debug_assert_eq!(self.block.epoch, self.last_epoch);
let EpochDuration {
min_num_of_blocks,
min_duration,
Expand Down Expand Up @@ -828,17 +827,17 @@ mod tests {
)
{
assert_eq!(storage.block.epoch, epoch_before.next());
assert_eq!(storage.last_epoch, epoch_before.next());
assert_eq!(storage.next_epoch_min_start_height,
block_height + epoch_duration.min_num_of_blocks);
assert_eq!(storage.next_epoch_min_start_time,
block_time + epoch_duration.min_duration);
assert_eq!(storage.block.pred_epochs.get_epoch(block_height), Some(epoch_before.next()));
} else {
assert_eq!(storage.block.epoch, epoch_before);
assert_eq!(storage.last_epoch, epoch_before);
assert_eq!(storage.block.pred_epochs.get_epoch(block_height), Some(epoch_before));
}
// Last epoch should only change when the block is committed
assert_eq!(storage.last_epoch, epoch_before);

// Update the epoch duration parameters
parameters.epoch_duration.min_num_of_blocks =
Expand All @@ -852,7 +851,7 @@ mod tests {
parameters::update_epoch_parameter(&mut storage, &parameters.epoch_duration).unwrap();

// Test for 2.
let epoch_before = storage.last_epoch;
let epoch_before = storage.block.epoch;
let height_of_update = storage.next_epoch_min_start_height.0 ;
let time_of_update = storage.next_epoch_min_start_time;
let height_before_update = BlockHeight(height_of_update - 1);
Expand All @@ -863,18 +862,14 @@ mod tests {
// satisfied
storage.update_epoch(height_before_update, time_before_update).unwrap();
assert_eq!(storage.block.epoch, epoch_before);
assert_eq!(storage.last_epoch, epoch_before);
storage.update_epoch(height_of_update, time_before_update).unwrap();
assert_eq!(storage.block.epoch, epoch_before);
assert_eq!(storage.last_epoch, epoch_before);
storage.update_epoch(height_before_update, time_of_update).unwrap();
assert_eq!(storage.block.epoch, epoch_before);
assert_eq!(storage.last_epoch, epoch_before);

// Update should happen at this or after this height and time
storage.update_epoch(height_of_update, time_of_update).unwrap();
assert_eq!(storage.block.epoch, epoch_before.next());
assert_eq!(storage.last_epoch, epoch_before.next());
// The next epoch's minimum duration should change
assert_eq!(storage.next_epoch_min_start_height,
height_of_update + parameters.epoch_duration.min_num_of_blocks);
Expand Down