Skip to content

Commit

Permalink
Merge branch 'tomas+tiago/fix-pred-epoch-height' (#384)
Browse files Browse the repository at this point in the history
* tomas+tiago/fix-pred-epoch-height:
  changelog: add #384
  shared/storage: fix the height recorded for a new epoch
  changelog: add #1249
  test/ledger: update last_epoch assertion
  ledger: fix last_epoch to only change when committing block
  • Loading branch information
tzemanovic committed Aug 31, 2022
2 parents f8b23ce + 388a69d commit 3b94ff7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
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))
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Fix the value recorded for epoch start block height.
([#384](https://github.com/anoma/namada/issues/384))
19 changes: 8 additions & 11 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 @@ -605,8 +606,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 All @@ -618,7 +617,7 @@ where
let evidence_max_age_num_blocks: u64 = 100000;
self.block
.pred_epochs
.new_epoch(height, evidence_max_age_num_blocks);
.new_epoch(height + 1, evidence_max_age_num_blocks);
tracing::info!("Began a new epoch {}", self.block.epoch);
}
self.update_epoch_in_merkle_tree()?;
Expand Down Expand Up @@ -930,17 +929,19 @@ 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()));
assert_eq!(storage.block.pred_epochs.get_epoch(block_height), Some(epoch_before));
assert_eq!(storage.block.pred_epochs.get_epoch(block_height + 1), 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));
assert_eq!(storage.block.pred_epochs.get_epoch(block_height + 1), 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 @@ -954,7 +955,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 @@ -965,18 +966,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

0 comments on commit 3b94ff7

Please sign in to comment.