-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pull gas limit computation to common crate
- Loading branch information
1 parent
834940b
commit 1c5ce06
Showing
3 changed files
with
57 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use std::cmp::Ordering; | ||
|
||
pub const GAS_BOUND_DIVISOR: u64 = 1024; | ||
|
||
pub fn compute_preferred_gas_limit(preferred_gas_limit: u64, parent_gas_limit: u64) -> u64 { | ||
match preferred_gas_limit.cmp(&parent_gas_limit) { | ||
Ordering::Equal => preferred_gas_limit, | ||
Ordering::Greater => { | ||
let bound = parent_gas_limit + parent_gas_limit / GAS_BOUND_DIVISOR; | ||
preferred_gas_limit.min(bound - 1) | ||
} | ||
Ordering::Less => { | ||
let bound = parent_gas_limit - parent_gas_limit / GAS_BOUND_DIVISOR; | ||
preferred_gas_limit.max(bound + 1) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
fn verify_limits(gas_limit: u64, parent_gas_limit: u64) -> bool { | ||
match gas_limit.cmp(&parent_gas_limit) { | ||
Ordering::Equal => true, | ||
Ordering::Greater => { | ||
let bound = parent_gas_limit + parent_gas_limit / GAS_BOUND_DIVISOR; | ||
gas_limit < bound | ||
} | ||
Ordering::Less => { | ||
let bound = parent_gas_limit - parent_gas_limit / GAS_BOUND_DIVISOR; | ||
gas_limit > bound | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_compute_preferred_gas_limit() { | ||
for t in &[ | ||
// preferred, parent, computed | ||
(30_000_000, 30_000_000, 30_000_000), | ||
(30_029_000, 30_000_000, 30_029_000), | ||
(30_029_300, 30_000_000, 30_029_295), | ||
(29_970_710, 30_000_000, 29_970_710), | ||
(29_970_700, 30_000_000, 29_970_705), | ||
] { | ||
assert_eq!(compute_preferred_gas_limit(t.0, t.1), t.2); | ||
assert!(verify_limits(t.2, t.1)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters