Skip to content

Commit

Permalink
Rearrange CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
MitchTurner committed Jan 10, 2025
1 parent df42f86 commit 659d8e0
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 91 deletions.
5 changes: 5 additions & 0 deletions crates/services/gas_price_service/simulation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


```shell
cargo run -- --file-path source_data/scrape_4.csv single -p 50000000000000000 -d 100000000000000 -s 18963
```
30 changes: 30 additions & 0 deletions crates/services/gas_price_service/simulation/src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use clap::{
Parser,
Subcommand,
};
use std::path::PathBuf;

#[derive(Subcommand, Debug)]
pub enum SimulationArgs {
Single {
/// DA P component
#[arg(short, long)]
p_component: i64,
/// DA D component
#[arg(short, long)]
d_component: i64,
/// Latest gas price
#[arg(short, long)]
start_gas_price: u64,
},
}

#[derive(Parser, Debug)]
pub struct Args {
/// Which simulation mode to run
#[command(subcommand)]
pub simulation: SimulationArgs,
/// Data source file
#[arg(short, long)]
pub file_path: PathBuf,
}
42 changes: 19 additions & 23 deletions crates/services/gas_price_service/simulation/src/data.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::DataSource;
use fuel_core_gas_price_service::{
common::utils::BlockInfo,
v1::da_source_service::DaBlockCosts,
Expand All @@ -11,7 +10,9 @@ use serde_reflection::{
};
use std::{
collections::HashMap,
fmt::Debug,
ops::RangeInclusive,
path::Path,
};

#[allow(dead_code)]
Expand Down Expand Up @@ -48,7 +49,7 @@ where
fields.iter().map(|f| f.name.clone()).collect()
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Data {
inner: Vec<(BlockInfo, Vec<DaBlockCosts>)>,
}
Expand Down Expand Up @@ -85,29 +86,24 @@ impl From<&Record> for BlockInfo {
}
}

pub fn get_data(source: &DataSource) -> anyhow::Result<Data> {
pub fn get_data<P: AsRef<Path> + Debug>(path: P) -> anyhow::Result<Data> {
const DA_OFFSET: usize = 0; // Make configurable

let records = match source {
DataSource::File { path } => {
tracing::info!("Loading data from file: {:?}", path);
let headers = csv::StringRecord::from(fields_of_struct_in_order::<Record>());
let mut rdr = csv::ReaderBuilder::new()
.has_headers(true)
.from_path(path)
.unwrap();

let records: Result<Vec<Record>, _> = rdr
.records()
.map(|line_entry| {
let line = line_entry?;
line.deserialize(Some(&headers))
})
.collect();
records?
}
DataSource::Generated => unimplemented!(),
};
tracing::info!("Loading data from file: {:?}", path);
let headers = csv::StringRecord::from(fields_of_struct_in_order::<Record>());
let mut rdr = csv::ReaderBuilder::new()
.has_headers(true)
.from_path(path)
.unwrap();

let records_res: Result<Vec<Record>, _> = rdr
.records()
.map(|line_entry| {
let line = line_entry?;
line.deserialize(Some(&headers))
})
.collect();
let records = records_res?;

let mut data = vec![];
// let groups = records.iter().chunk_by(|record| record.l1_block_number);
Expand Down
109 changes: 59 additions & 50 deletions crates/services/gas_price_service/simulation/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,73 +1,82 @@
use crate::{
data::get_data,
cli::{
Args,
SimulationArgs,
},
data::{
get_data,
Data,
},
display::display_results,
service::get_service_controller,
simulation::simulation,
};
use clap::{
Parser,
Subcommand,
};
use itertools::Itertools;
use plotters::prelude::*;
use std::{
env,
path::PathBuf,
};
use tracing_subscriber::{
layer::SubscriberExt,
util::SubscriberInitExt,
EnvFilter,
Layer,
service::{
get_service_controller,
ConfigValues,
MetadataValues,
},
simulation::{
single_simulation,
SimulationResults,
},
tracing::configure_tracing,
};
use clap::Parser;

pub mod cli;
pub mod data;
pub mod data_sources;
pub mod display;
pub mod service;
pub mod simulation;
pub mod tracing;

#[derive(Subcommand, Debug)]
pub enum DataSource {
/// Load data from a CSV file.
File {
#[arg(short, long)]
path: PathBuf,
},
/// Generate arbitrary data (not supported yet).
Generated,
}

#[derive(Parser, Debug)]
struct Args {
#[command(subcommand)]
data_source: DataSource,
pub async fn run_single_simulation(data: Data) -> anyhow::Result<SimulationResults> {
let starting_height = data.starting_height();
let config_values = ConfigValues {
min_da_gas_price: 1000,
max_da_gas_price: u64::MAX,
da_p_component: 50_000_000__000_000_000,
da_d_component: 100_000__000_000_000,
};
let metadata_values = MetadataValues::new(starting_height, 18_963);
let mut service_controller =
get_service_controller(config_values, metadata_values).await?;
let results = single_simulation(&data, &mut service_controller).await?;
Ok(results)
}

fn configure_tracing() {
let filter = match env::var_os("RUST_LOG") {
Some(_) => {
EnvFilter::try_from_default_env().expect("Invalid `RUST_LOG` provided")
pub async fn run_a_simulation(
args: SimulationArgs,
data: Data,
) -> anyhow::Result<SimulationResults> {
match args {
SimulationArgs::Single {
p_component,
d_component,
start_gas_price,
} => {
let starting_height = data.starting_height();
let config_values = ConfigValues {
min_da_gas_price: 1000,
max_da_gas_price: u64::MAX,
da_p_component: p_component,
da_d_component: d_component,
};
let metadata_values = MetadataValues::new(starting_height, start_gas_price);
let mut service_controller =
get_service_controller(config_values, metadata_values).await?;
let results = single_simulation(&data, &mut service_controller).await?;
Ok(results)
}
None => EnvFilter::new("info"),
};

let fmt = tracing_subscriber::fmt::Layer::default()
.with_level(true)
.boxed();

tracing_subscriber::registry().with(fmt).with(filter).init();
}
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
configure_tracing();

let args = Args::parse();
let data = get_data(&args.data_source)?;
let starting_height = data.starting_height();
let mut service_controller = get_service_controller(starting_height).await?;
let results = simulation(&data, &mut service_controller).await?;
let data = get_data(&args.file_path)?;
let results = run_single_simulation(data).await?;
display_results(results)?;
Ok(())
}
71 changes: 54 additions & 17 deletions crates/services/gas_price_service/simulation/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,29 @@ use std::{
},
};

fn read_config_from_file(_config_path: &str) -> V1AlgorithmConfig {
// TODO: read from file and/or CLI
fn construct_config(config_values: ConfigValues) -> V1AlgorithmConfig {
let ConfigValues {
min_da_gas_price,
max_da_gas_price,
da_p_component,
da_d_component,
} = config_values;

V1AlgorithmConfig {
new_exec_gas_price: 0,
min_exec_gas_price: 0,
exec_gas_price_change_percent: 0,
l2_block_fullness_threshold_percent: 0,
gas_price_factor: NonZero::new(100).unwrap(),
min_da_gas_price: 1_000,
max_da_gas_price: u64::MAX,
// min_da_gas_price: 1_000,
// max_da_gas_price: u64::MAX,
min_da_gas_price,
max_da_gas_price,
max_da_gas_price_change_percent: 10,
da_p_component: 50_000_000__000_000_000,
da_d_component: 100_000__000_000_000,
// da_p_component: 50_000_000__000_000_000,
// da_d_component: 100_000__000_000_000,
da_p_component,
da_d_component,
normal_range_size: 0,
capped_range_size: 0,
decrease_range_size: 0,
Expand All @@ -69,13 +79,17 @@ fn read_config_from_file(_config_path: &str) -> V1AlgorithmConfig {
}
}

fn read_metadata_from_file(_metadata_path: &str, starting_height: u32) -> V1Metadata {
fn construct_metadata(metadata_values: MetadataValues) -> V1Metadata {
let MetadataValues {
l2_block_height,
latest_gas_price,
} = metadata_values;
// TODO: read from file and/or CLI
let l2_block_height = starting_height - 1;
let gas_price_factor = 100;
let gas_price_factor = 1;
// let new_scaled_da_gas_price = 3_547_063 * gas_price_factor / 1_000;
// let new_scaled_da_gas_price = 15_893_241 * gas_price_factor / 1_000;
let new_scaled_da_gas_price = 18_963 * gas_price_factor;
// let new_scaled_da_gas_price = 18_963;
let new_scaled_da_gas_price = latest_gas_price * gas_price_factor;
V1Metadata {
new_scaled_exec_price: 0,
l2_block_height,
Expand All @@ -90,11 +104,33 @@ fn read_metadata_from_file(_metadata_path: &str, starting_height: u32) -> V1Meta
}
}

fn get_updater(starting_height: u32) -> AlgorithmUpdaterV1 {
let metadata_path = "TODO";
let metadata = read_metadata_from_file(metadata_path, starting_height);
let config_path = "TODO";
let config = read_config_from_file(config_path);
pub struct ConfigValues {
pub min_da_gas_price: u64,
pub max_da_gas_price: u64,
pub da_p_component: i64,
pub da_d_component: i64,
}

pub struct MetadataValues {
pub l2_block_height: u32,
pub latest_gas_price: u64,
}

impl MetadataValues {
pub fn new(starting_height: u32, latest_gas_price: u64) -> Self {
Self {
l2_block_height: starting_height - 1,
latest_gas_price,
}
}
}

fn get_updater(
config_values: ConfigValues,
metadata_values: MetadataValues,
) -> AlgorithmUpdaterV1 {
let metadata = construct_metadata(metadata_values);
let config = construct_config(config_values);
v1_algorithm_from_metadata(metadata, &config)
}
type GasPriceStorage = StorageTransaction<InMemoryStorage<GasPriceColumn>>;
Expand Down Expand Up @@ -169,10 +205,11 @@ fn poll_interval() -> Option<std::time::Duration> {
}

pub async fn get_service_controller(
starting_height: u32,
config_values: ConfigValues,
metadata_values: MetadataValues,
) -> anyhow::Result<ServiceController> {
tracing::info!("creating service controller");
let algorithm_updater = get_updater(starting_height);
let algorithm_updater = get_updater(config_values, metadata_values);
let algo = algorithm_updater.algorithm();
let shared_algo = SharedV1Algorithm::new_with_algorithm(algo);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl SimulationResults {
}
}

pub async fn simulation(
pub async fn single_simulation(
data: &Data,
service_controller: &mut ServiceController,
) -> anyhow::Result<SimulationResults> {
Expand Down
22 changes: 22 additions & 0 deletions crates/services/gas_price_service/simulation/src/tracing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::env;
use tracing_subscriber::{
layer::SubscriberExt,
util::SubscriberInitExt,
EnvFilter,
Layer,
};

pub fn configure_tracing() {
let filter = match env::var_os("RUST_LOG") {
Some(_) => {
EnvFilter::try_from_default_env().expect("Invalid `RUST_LOG` provided")
}
None => EnvFilter::new("info"),
};

let fmt = tracing_subscriber::fmt::Layer::default()
.with_level(true)
.boxed();

tracing_subscriber::registry().with(fmt).with(filter).init();
}

0 comments on commit 659d8e0

Please sign in to comment.