Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: fix algorithm overflow issues #2173

Merged
merged 34 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
889618c
Add abstraction for da costs
MitchTurner Sep 3, 2024
3911903
Remove generated chart
MitchTurner Sep 3, 2024
a300b02
Refactor
MitchTurner Sep 4, 2024
af9a072
WIP add csv reader
MitchTurner Sep 4, 2024
3601cd1
Modify units to accomodate bigger values
MitchTurner Sep 4, 2024
6d90d66
Refactor
MitchTurner Sep 5, 2024
0037ca5
Create directory if doesn't exist
xgreenx Sep 5, 2024
d75a0d2
Merge branch 'master' into feature/allow-predefined-blob-tx-costs
MitchTurner Sep 5, 2024
17582b8
Move da cost calc stuff to its own mod
MitchTurner Sep 5, 2024
a92e090
Appease Clippy-sama
MitchTurner Sep 5, 2024
6a53157
Add results, fix gitignore
MitchTurner Sep 6, 2024
f8573e2
Rename function, fix iterators, add other PR review suggestions
MitchTurner Sep 6, 2024
6452ae1
Merge branch 'master' into feature/allow-predefined-blob-tx-costs
MitchTurner Sep 6, 2024
0b6fcd0
empty commit to bump CI
MitchTurner Sep 6, 2024
e541435
Add one passing test
MitchTurner Sep 6, 2024
1836a24
Improve documentation for simulation code
MitchTurner Sep 6, 2024
2286da4
Revert "Improve documentation for simulation code"
MitchTurner Sep 6, 2024
af7391b
Fix dumb bug in simulation
MitchTurner Sep 9, 2024
2912ea9
Parallelize optimization a bit
MitchTurner Sep 9, 2024
b709b5a
Fix a bunch of casts and conversions
MitchTurner Sep 10, 2024
3141914
Fix warnings
MitchTurner Sep 10, 2024
ab5b9dd
Appease Clippy-sama
MitchTurner Sep 10, 2024
412bc95
Merge remote-tracking branch 'origin' into bug/fix-algorithm-giving-u…
MitchTurner Sep 10, 2024
1857fe2
Improve documentation for simulation code
MitchTurner Sep 6, 2024
06d37e8
Add better testing with proptests :)
MitchTurner Sep 10, 2024
fcfd024
Lint toml
MitchTurner Sep 10, 2024
c26ca94
Update crates/fuel-gas-price-algorithm/src/v1/tests/update_da_record_…
MitchTurner Sep 10, 2024
ce9c0ba
Update crates/fuel-gas-price-algorithm/src/v1.rs
MitchTurner Sep 10, 2024
88af11d
Update crates/fuel-gas-price-algorithm/src/v1.rs
MitchTurner Sep 10, 2024
c2ce485
Update crates/fuel-gas-price-algorithm/src/v1.rs
MitchTurner Sep 10, 2024
7f4f785
Clarify percentage types
MitchTurner Sep 10, 2024
13693ef
Add newtype for threshold percentage
MitchTurner Sep 10, 2024
604279b
Use more arbitrary number, reduce feature deps for tokio
MitchTurner Sep 10, 2024
a4eff83
Merge branch 'master' into bug/fix-algorithm-giving-up-2164
MitchTurner Sep 10, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ anyhow = "1.0.86"
clap = { version = "4.5.16", features = ["derive"] }
csv = "1.3.0"
fuel-gas-price-algorithm = { path = ".." }
futures = "0.3.30"
plotters = "0.3.5"
rand = "0.8.5"
rand_distr = "0.4.3"
serde = { version = "1.0.209", features = ["derive"] }
tokio = { version = "1.40.0", features = ["full"] }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to not use full if you only need something specific=)

Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ enum Source {
},
}

fn main() -> anyhow::Result<()> {
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let args = Arg::parse();

const UPDATE_PERIOD: usize = 12;
Expand Down Expand Up @@ -103,7 +104,7 @@ fn main() -> anyhow::Result<()> {
);
let simulator = Simulator::new(da_cost_per_byte);
let (results, (p, d)) =
naive_optimisation(&simulator, iterations as usize, UPDATE_PERIOD);
naive_optimisation(&simulator, iterations as usize, UPDATE_PERIOD).await;
println!(
"Optimization results: P: {}, D: {}",
prettify_number(p),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use futures::future::join_all;

fn da_pid_factors(size: usize) -> Vec<(i64, i64)> {
let mut rng = StdRng::seed_from_u64(10902);
Expand All @@ -11,19 +12,28 @@ fn da_pid_factors(size: usize) -> Vec<(i64, i64)> {
.collect()
}

pub fn naive_optimisation(
pub async fn naive_optimisation(
simulator: &Simulator,
iterations: usize,
da_recording_rate: usize,
) -> (SimulationResults, (i64, i64)) {
da_pid_factors(iterations)
.iter()
let tasks = da_pid_factors(iterations)
.into_iter()
.map(|(p, d)| {
(
simulator.run_simulation(*p, *d, da_recording_rate),
(*p, *d),
)
let new_simulator = simulator.clone();
let f = move || {
(
new_simulator.run_simulation(p, d, da_recording_rate),
(p, d),
)
};
tokio::task::spawn_blocking(f)
})
.collect::<Vec<_>>();
join_all(tasks)
.await
.into_iter()
.map(Result::unwrap)
.min_by_key(|(results, _)| {
let SimulationResults { actual_profit, .. } = results;
let err = actual_profit.iter().map(|p| p.abs()).sum::<i128>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,7 @@ use fuel_gas_price_algorithm::v1::{
AlgorithmUpdaterV1,
RecordedBlock,
};
use std::{
iter,
iter::{
Enumerate,
Zip,
},
num::NonZeroU64,
slice::Iter,
};
use std::num::NonZeroU64;

use super::*;

Expand All @@ -27,6 +19,7 @@ pub struct SimulationResults {
pub pessimistic_costs: Vec<u128>,
}

#[derive(Clone, Debug)]
pub struct Simulator {
da_cost_per_byte: Vec<u64>,
}
Expand Down Expand Up @@ -70,29 +63,24 @@ impl Simulator {
da_p_component: i64,
da_d_component: i64,
) -> AlgorithmUpdaterV1 {
// Scales the gas price internally, value is arbitrary
let gas_price_factor = 100;
let updater = AlgorithmUpdaterV1 {
min_exec_gas_price: 10,
min_da_gas_price: 10,
// Change to adjust where the gas price starts on block 0
MitchTurner marked this conversation as resolved.
Show resolved Hide resolved
new_scaled_exec_price: 10 * gas_price_factor,
// Change to adjust where the gas price starts on block 0
// last_da_gas_price: *starting_da_gas_price,
last_da_gas_price: 100,
gas_price_factor: NonZeroU64::new(gas_price_factor).unwrap(),
l2_block_height: 0,
// Choose the ideal fullness percentage for the L2 block
l2_block_fullness_threshold_percent: 50,
// Increase to make the exec price change faster
exec_gas_price_change_percent: 2,
// Increase to make the da price change faster
max_da_gas_price_change_percent: 10,
total_da_rewards: 0,
total_da_rewards_excess: 0,
da_recorded_block_height: 0,
// Change to adjust the cost per byte of the DA on block 0
// latest_da_cost_per_byte: *starting_da_gas_price as u128,
latest_da_cost_per_byte: 0,
projected_total_da_cost: 0,
latest_known_total_da_cost: 0,
latest_known_total_da_cost_excess: 0,
unrecorded_blocks: vec![],
da_p_component,
da_d_component,
Expand Down Expand Up @@ -123,18 +111,6 @@ impl Simulator {
exec_gas_prices.push(updater.new_scaled_exec_price);
let gas_price = updater.algorithm().calculate(max_block_bytes);
gas_prices.push(gas_price);
// Update DA blocks on the occasion there is one

if let Some(mut da_blocks) = da_block.clone() {
let mut total_costs = updater.latest_known_total_da_cost;
for block in &mut da_blocks {
total_costs += block.block_cost as u128;
actual_costs.push(total_costs);
}
updater.update_da_record_data(da_blocks.to_owned()).unwrap();
assert_eq!(total_costs, updater.projected_total_da_cost);
assert_eq!(total_costs, updater.latest_known_total_da_cost);
}
updater
.update_l2_block_data(
height,
Expand All @@ -147,17 +123,26 @@ impl Simulator {
da_gas_prices.push(updater.last_da_gas_price);
pessimistic_costs
.push(max_block_bytes as u128 * updater.latest_da_cost_per_byte);
actual_reward_totals.push(updater.total_da_rewards);
actual_reward_totals.push(updater.total_da_rewards_excess);
projected_cost_totals.push(updater.projected_total_da_cost);
}

// Update DA blocks on the occasion there is one
if let Some(da_blocks) = &da_block {
let mut total_cost = updater.latest_known_total_da_cost_excess;
for block in da_blocks {
total_cost += block.block_cost as u128;
actual_costs.push(total_cost);
}
updater.update_da_record_data(&da_blocks).unwrap();
}
}
let (fullness_without_capacity, bytes): (Vec<_>, Vec<_>) =
fullness_and_bytes.iter().cloned().unzip();
let fullness = fullness_without_capacity
let fullness: Vec<_> = fullness_without_capacity
.iter()
.map(|&fullness| (fullness, capacity))
.collect();
let bytes_and_costs = bytes
let bytes_and_costs: Vec<_> = bytes
.iter()
.zip(self.da_cost_per_byte.iter())
.map(|(bytes, cost_per_byte)| (*bytes, (*bytes * cost_per_byte) as u64))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub fn get_da_cost_per_byte_from_source(
}
}

#[allow(dead_code)]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where do we use it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's used in CSV reader.

#[derive(Debug, serde::Deserialize)]
struct Record {
block_number: u64,
Expand Down
Loading
Loading