Skip to content

Commit 4c5722e

Browse files
Acquire da_finalization_period from the command line (#2240)
## Linked Issues Closes #2218 ## Description This PR extends the set of accepted command line parameters. The user is now able to provide the DA update time via the new `--da-finalization-period` argument. It also fixes the issue with `unrecorded_blocks` collection which was not saturated with blocks. ## Checklist - [X] New behavior is reflected in tests ### Before requesting review - [X] I have reviewed the code myself --------- Co-authored-by: Mitchell Turner <[email protected]>
1 parent 20812f1 commit 4c5722e

File tree

6 files changed

+114
-16
lines changed

6 files changed

+114
-16
lines changed

crates/fuel-gas-price-algorithm/gas-price-analysis/src/charts.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub fn draw_chart(
1010
results: SimulationResults,
1111
p_comp: i64,
1212
d_comp: i64,
13+
da_finalization_period: usize,
1314
file_path: &str,
1415
) -> anyhow::Result<()> {
1516
let SimulationResults {
@@ -53,9 +54,10 @@ pub fn draw_chart(
5354
&projected_profit,
5455
&pessimistic_costs,
5556
&format!(
56-
"Profit p_comp: {}, d_comp: {}",
57+
"Profit p_comp: {}, d_comp: {}, da_finalization_period: {}",
5758
prettify_number(p_comp),
58-
prettify_number(d_comp)
59+
prettify_number(d_comp),
60+
prettify_number(da_finalization_period)
5961
),
6062
)?;
6163
draw_gas_prices(

crates/fuel-gas-price-algorithm/gas-price-analysis/src/main.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ struct Arg {
3737
/// File path to save the chart to. Will not generate chart if left blank
3838
#[arg(short, long)]
3939
file_path: Option<String>,
40+
#[arg(short, long)]
41+
/// DA finalization period in L2 blocks
42+
da_finalization_period: usize,
4043
}
4144

4245
#[derive(Subcommand)]
@@ -80,19 +83,23 @@ async fn main() -> anyhow::Result<()> {
8083

8184
const UPDATE_PERIOD: usize = 12;
8285

86+
let da_finalization_period = args.da_finalization_period;
87+
8388
let (results, (p_comp, d_comp)) = match args.mode {
8489
Mode::WithValues { p, d, source } => {
8590
let da_cost_per_byte =
8691
get_da_cost_per_byte_from_source(source, UPDATE_PERIOD);
8792
let size = da_cost_per_byte.len();
8893
println!(
89-
"Running simulation with P: {}, D: {}, and {} blocks",
94+
"Running simulation with P: {}, D: {}, {} blocks and {} da_finalization_period",
9095
prettify_number(p),
9196
prettify_number(d),
92-
prettify_number(size)
97+
prettify_number(size),
98+
prettify_number(da_finalization_period)
9399
);
94100
let simulator = Simulator::new(da_cost_per_byte);
95-
let result = simulator.run_simulation(p, d, UPDATE_PERIOD);
101+
let result =
102+
simulator.run_simulation(p, d, UPDATE_PERIOD, da_finalization_period);
96103
(result, (p, d))
97104
}
98105
Mode::Optimization { iterations, source } => {
@@ -103,12 +110,18 @@ async fn main() -> anyhow::Result<()> {
103110
"Running optimization with {iterations} iterations and {size} blocks"
104111
);
105112
let simulator = Simulator::new(da_cost_per_byte);
106-
let (results, (p, d)) =
107-
naive_optimisation(&simulator, iterations as usize, UPDATE_PERIOD).await;
113+
let (results, (p, d)) = naive_optimisation(
114+
&simulator,
115+
iterations as usize,
116+
UPDATE_PERIOD,
117+
da_finalization_period,
118+
)
119+
.await;
108120
println!(
109-
"Optimization results: P: {}, D: {}",
121+
"Optimization results: P: {}, D: {}, da_finalization_period: {}",
110122
prettify_number(p),
111-
prettify_number(d)
123+
prettify_number(d),
124+
prettify_number(da_finalization_period)
112125
);
113126
(results, (p, d))
114127
}
@@ -117,7 +130,7 @@ async fn main() -> anyhow::Result<()> {
117130
print_info(&results);
118131

119132
if let Some(file_path) = &args.file_path {
120-
draw_chart(results, p_comp, d_comp, file_path)?;
133+
draw_chart(results, p_comp, d_comp, da_finalization_period, file_path)?;
121134
}
122135

123136
Ok(())

crates/fuel-gas-price-algorithm/gas-price-analysis/src/optimisation.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ fn da_pid_factors(size: usize) -> Vec<(i64, i64)> {
1515
pub async fn naive_optimisation(
1616
simulator: &Simulator,
1717
iterations: usize,
18+
update_period: usize,
1819
da_recording_rate: usize,
1920
) -> (SimulationResults, (i64, i64)) {
2021
let tasks = da_pid_factors(iterations)
@@ -23,7 +24,7 @@ pub async fn naive_optimisation(
2324
let new_simulator = simulator.clone();
2425
let f = move || {
2526
(
26-
new_simulator.run_simulation(p, d, da_recording_rate),
27+
new_simulator.run_simulation(p, d, update_period, da_recording_rate),
2728
(p, d),
2829
)
2930
};

crates/fuel-gas-price-algorithm/gas-price-analysis/src/simulation.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ impl Simulator {
3333
&self,
3434
da_p_component: i64,
3535
da_d_component: i64,
36-
da_recording_rate: usize,
36+
update_period: usize,
37+
da_finalization_rate: usize,
3738
) -> SimulationResults {
3839
let capacity = 30_000_000;
3940
let gas_per_byte = 63;
@@ -42,8 +43,11 @@ impl Simulator {
4243
let fullness_and_bytes = fullness_and_bytes_per_block(size, capacity);
4344

4445
let l2_blocks = fullness_and_bytes.clone().into_iter();
45-
let da_blocks =
46-
self.zip_l2_blocks_with_da_blocks(da_recording_rate, &fullness_and_bytes);
46+
let da_blocks = self.calculate_da_blocks(
47+
update_period,
48+
da_finalization_rate,
49+
&fullness_and_bytes,
50+
);
4751

4852
let blocks = l2_blocks.zip(da_blocks.iter()).enumerate();
4953

@@ -178,11 +182,14 @@ impl Simulator {
178182
}
179183
}
180184

181-
fn zip_l2_blocks_with_da_blocks(
185+
fn calculate_da_blocks(
182186
&self,
183187
da_recording_rate: usize,
188+
da_finalization_rate: usize,
184189
fullness_and_bytes: &Vec<(u64, u64)>,
185190
) -> Vec<Option<Vec<RecordedBlock>>> {
191+
let l2_blocks_with_no_da_blocks =
192+
std::iter::repeat(None).take(da_finalization_rate);
186193
let (_, da_blocks) = fullness_and_bytes
187194
.iter()
188195
.zip(self.da_cost_per_byte.iter())
@@ -208,7 +215,7 @@ impl Simulator {
208215
}
209216
},
210217
);
211-
da_blocks
218+
l2_blocks_with_no_da_blocks.chain(da_blocks).collect()
212219
}
213220
}
214221

crates/fuel-gas-price-algorithm/src/v1.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,12 @@ impl AlgorithmUpdaterV1 {
232232
// gas prices
233233
self.update_exec_gas_price(used, capacity);
234234
self.update_da_gas_price();
235+
236+
// metadata
237+
self.unrecorded_blocks.push(BlockBytes {
238+
height,
239+
block_bytes,
240+
});
235241
Ok(())
236242
}
237243
}

crates/fuel-gas-price-algorithm/src/v1/tests/update_l2_block_data_tests.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::v1::{
22
tests::UpdaterBuilder,
3+
BlockBytes,
34
Error,
45
};
56

@@ -536,3 +537,71 @@ fn update_l2_block_data__negative_profit_increase_gas_price() {
536537
old_gas_price
537538
);
538539
}
540+
541+
#[test]
542+
fn update_l2_block_data__adds_l2_block_to_unrecorded_blocks() {
543+
// given
544+
let starting_block = 0;
545+
546+
let mut updater = UpdaterBuilder::new()
547+
.with_l2_block_height(starting_block)
548+
.build();
549+
550+
let height = 1;
551+
let used = 50;
552+
let capacity = 100.try_into().unwrap();
553+
let block_bytes = 1000;
554+
let new_gas_price = 100;
555+
556+
// when
557+
updater
558+
.update_l2_block_data(height, used, capacity, block_bytes, new_gas_price)
559+
.unwrap();
560+
561+
// then
562+
let block_bytes = BlockBytes {
563+
height,
564+
block_bytes,
565+
};
566+
let contains_block_bytes = updater.unrecorded_blocks.contains(&block_bytes);
567+
assert!(contains_block_bytes);
568+
}
569+
570+
#[test]
571+
fn update_l2_block_data__retains_existing_blocks_and_adds_l2_block_to_unrecorded_blocks()
572+
{
573+
// given
574+
let starting_block = 0;
575+
let preexisting_block = BlockBytes {
576+
height: 0,
577+
block_bytes: 1000,
578+
};
579+
580+
let mut updater = UpdaterBuilder::new()
581+
.with_l2_block_height(starting_block)
582+
.with_unrecorded_blocks(vec![preexisting_block.clone()])
583+
.build();
584+
585+
let height = 1;
586+
let used = 50;
587+
let capacity = 100.try_into().unwrap();
588+
let block_bytes = 1000;
589+
let new_gas_price = 100;
590+
591+
// when
592+
updater
593+
.update_l2_block_data(height, used, capacity, block_bytes, new_gas_price)
594+
.unwrap();
595+
596+
// then
597+
let block_bytes = BlockBytes {
598+
height,
599+
block_bytes,
600+
};
601+
let contains_block_bytes = updater.unrecorded_blocks.contains(&block_bytes);
602+
assert!(contains_block_bytes);
603+
604+
let contains_preexisting_block_bytes =
605+
updater.unrecorded_blocks.contains(&preexisting_block);
606+
assert!(contains_preexisting_block_bytes);
607+
}

0 commit comments

Comments
 (0)