Skip to content

Commit

Permalink
Avoid estimating block time of zero
Browse files Browse the repository at this point in the history
  • Loading branch information
sug0 committed Jun 6, 2024
1 parent a2e2776 commit c061834
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions crates/parameters/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use namada_core::chain::ProposalBytes;
pub use namada_core::parameters::*;
use namada_core::storage::{BlockHeight, Key};
use namada_core::time::DurationSecs;
use namada_core::token;
use namada_core::{hints, token};
use namada_storage::{ResultExt, StorageRead, StorageWrite};
pub use storage::get_max_block_gas;
use thiserror::Error;
Expand Down Expand Up @@ -500,7 +500,15 @@ where
} = read_epoch_duration_parameter(storage)?;

checked!(min_duration / min_num_of_blocks)
.map(DurationSecs)
.map(|block_time| {
// NB: fallback to one block per epoch
// if the time to decide a block is zero
DurationSecs(if hints::unlikely(block_time == 0) {
min_duration
} else {
block_time
})
})
.into_storage_result()
}

Expand Down Expand Up @@ -561,6 +569,25 @@ mod tests {
assert_eq!(max_block_time, DurationSecs(2));
}

#[test]
fn test_estimate_max_block_time_from_parameters_short_epoch() {
let mut storage = TestStorage::default();

update_epoch_parameter(
&mut storage,
&EpochDuration {
min_duration: DurationSecs(4),
min_num_of_blocks: 5,
},
)
.unwrap();

let max_block_time =
estimate_max_block_time_from_parameters(&storage).unwrap();

assert_eq!(max_block_time, DurationSecs(4));
}

#[test]
fn test_estimate_max_block_time_from_blocks() {
let mut storage = TestStorage::default();
Expand Down

0 comments on commit c061834

Please sign in to comment.