-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(consensus): use versioned constants in fee market, consensus
context
- Loading branch information
1 parent
d3ad325
commit e17763e
Showing
3 changed files
with
47 additions
and
46 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
59 changes: 31 additions & 28 deletions
59
crates/sequencing/papyrus_consensus_orchestrator/src/fee_market/test.rs
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 |
---|---|---|
@@ -1,62 +1,65 @@ | ||
use crate::fee_market::{ | ||
calculate_next_base_gas_price, | ||
GAS_PRICE_MAX_CHANGE_DENOMINATOR, | ||
MAX_BLOCK_SIZE, | ||
MIN_GAS_PRICE, | ||
}; | ||
use crate::fee_market::calculate_next_base_gas_price; | ||
use crate::versioned_constants::VersionedConstants; | ||
|
||
#[test] | ||
fn test_price_calculation_snapshot() { | ||
// Setup: using realistic arbitrary values. | ||
const INIT_PRICE: u64 = 1_000_000; | ||
const GAS_TARGET: u64 = MAX_BLOCK_SIZE / 2; | ||
const HIGH_CONGESTION_GAS_USED: u64 = MAX_BLOCK_SIZE * 3 / 4; | ||
const LOW_CONGESTION_GAS_USED: u64 = MAX_BLOCK_SIZE / 4; | ||
const STABLE_CONGESTION_GAS_USED: u64 = GAS_TARGET; | ||
let init_price: u64 = 1_000_000; | ||
let versioned_constants = VersionedConstants::latest_constants(); | ||
let max_block_size = versioned_constants.max_block_size; | ||
let get_target: u64 = max_block_size / 2; | ||
let high_congestion_gas_used: u64 = max_block_size * 3 / 4; | ||
let low_congestion_gas_used: u64 = max_block_size / 4; | ||
let stable_congestion_gas_used: u64 = get_target; | ||
|
||
// Fixed expected output values. | ||
let increased_price = 1000000 + 10416; // 1000000 + (1000000 * 1 / 4 * MAX_BLOCK_SIZE) / (0.5 * MAX_BLOCK_SIZE * 48); | ||
let decreased_price = 1000000 - 10416; // 1000000 - (1000000 * 1 / 4 * MAX_BLOCK_SIZE) / (0.5 * MAX_BLOCK_SIZE * 48); | ||
let increased_price = 1000000 + 10416; // 1000000 + (1000000 * 1 / 4 * max_block_size) / (0.5 * max_block_size * 48); | ||
let decreased_price = 1000000 - 10416; // 1000000 - (1000000 * 1 / 4 * max_block_size) / (0.5 * max_block_size * 48); | ||
|
||
// Assert. | ||
assert_eq!( | ||
calculate_next_base_gas_price(INIT_PRICE, HIGH_CONGESTION_GAS_USED, GAS_TARGET), | ||
calculate_next_base_gas_price(init_price, high_congestion_gas_used, get_target), | ||
increased_price | ||
); | ||
assert_eq!( | ||
calculate_next_base_gas_price(INIT_PRICE, LOW_CONGESTION_GAS_USED, GAS_TARGET), | ||
calculate_next_base_gas_price(init_price, low_congestion_gas_used, get_target), | ||
decreased_price | ||
); | ||
assert_eq!( | ||
calculate_next_base_gas_price(INIT_PRICE, STABLE_CONGESTION_GAS_USED, GAS_TARGET), | ||
INIT_PRICE | ||
calculate_next_base_gas_price(init_price, stable_congestion_gas_used, get_target), | ||
init_price | ||
); | ||
} | ||
|
||
#[test] | ||
// This test ensures that the gas price calculation does not overflow with extreme values, | ||
fn test_gas_price_with_extreme_values() { | ||
let price = MIN_GAS_PRICE; | ||
let gas_target = MAX_BLOCK_SIZE / 2; | ||
let versioned_constants = VersionedConstants::latest_constants(); | ||
let max_block_size = versioned_constants.max_block_size; | ||
let min_gas_price = versioned_constants.min_gas_price; | ||
let gas_price_max_change_denominator = versioned_constants.gas_price_max_change_denominator; | ||
|
||
let price = min_gas_price; | ||
let gas_target = max_block_size / 2; | ||
let gas_used = 0; | ||
assert_eq!(calculate_next_base_gas_price(price, gas_used, gas_target), MIN_GAS_PRICE); | ||
assert_eq!(calculate_next_base_gas_price(price, gas_used, gas_target), min_gas_price); | ||
|
||
let price = MIN_GAS_PRICE; | ||
let gas_target = MAX_BLOCK_SIZE / 2; | ||
let gas_used = MAX_BLOCK_SIZE; | ||
assert!(calculate_next_base_gas_price(price, gas_used, gas_target) > MIN_GAS_PRICE); | ||
let price = min_gas_price; | ||
let gas_target = max_block_size / 2; | ||
let gas_used = max_block_size; | ||
assert!(calculate_next_base_gas_price(price, gas_used, gas_target) > min_gas_price); | ||
|
||
let price = u64::MAX; | ||
let gas_target = MAX_BLOCK_SIZE / 2; | ||
let gas_target = max_block_size / 2; | ||
let gas_used = 0; | ||
calculate_next_base_gas_price(price, gas_used, gas_target); // Should not panic. | ||
|
||
// To avoid overflow when updating the price, the value is set below a certain threshold so that | ||
// the new price does not exceed u64::MAX. | ||
let max_u128 = u128::from(u64::MAX); | ||
let price_u128 = | ||
max_u128 * GAS_PRICE_MAX_CHANGE_DENOMINATOR / (GAS_PRICE_MAX_CHANGE_DENOMINATOR + 1); | ||
let gas_target = MAX_BLOCK_SIZE / 2; | ||
let gas_used = MAX_BLOCK_SIZE; | ||
max_u128 * gas_price_max_change_denominator / (gas_price_max_change_denominator + 1); | ||
let gas_target = max_block_size / 2; | ||
let gas_used = max_block_size; | ||
calculate_next_base_gas_price(u64::try_from(price_u128).unwrap(), gas_used, gas_target); // Should not panic. | ||
} |
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