From 0e7b3965961e767ea049921e24310e8f742133c6 Mon Sep 17 00:00:00 2001 From: Anthony Graignic <anthony.graignic@uca.fr> Date: Mon, 22 Jan 2024 09:13:17 +0100 Subject: [PATCH 1/4] Update mobility coefficients for bx quantity --- src/utils/mobility_data.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/utils/mobility_data.rs b/src/utils/mobility_data.rs index 8589aec..4ce2d0b 100644 --- a/src/utils/mobility_data.rs +++ b/src/utils/mobility_data.rs @@ -121,15 +121,21 @@ pub fn compute_stretched_relative_start_time( } /// Provide differents fake quantities depending on mobility e.g. BIKE that will be use for rewards calculation +/// Coefficients per km from https://nosgestesclimat.fr/actions/plus/transport/voiture-5km pub fn quantify_mobility(service: String) -> U256 { + // let base_vdf_divider = + let base_vdf_divider = + // U256::from(1_000_000_000_000_000_000_000_000_000_u128); + U256::from("0x0000000000000000ffffffffffffffffffffffffffffffffffffffffffffffff"); if service == "BIKE_SHARING" { - U256::from(1000000000000000000_u128) + // 0.129/0.0001 = 1290 + base_vdf_divider.saturating_mul(U256::from(1290_u128)) } else if service == "BIKE_RENTAL" { - U256::from(500000000000000000_u128) + base_vdf_divider.saturating_mul(U256::from(1290_u128)) } else if service == "PUBLIC_TRANSPORT" { - U256::from(10000000000000000_u128) + base_vdf_divider.saturating_mul(U256::from(1_u128)) } else { - U256::from(1) + base_vdf_divider } } @@ -283,6 +289,6 @@ mod tests { // ACT let result = quantify_mobility(String::from("BIKE_SHARING")); // ASSERT - assert_eq!(U256::from(1000000000000000000_u128), result); + assert_eq!(U256::from(1290000000000000000000_u128), result); } } -- GitLab From 9b8ddcaedaef482ed556492094e58670f7419572 Mon Sep 17 00:00:00 2001 From: Anthony Graignic <anthony.graignic@uca.fr> Date: Thu, 25 Jan 2024 16:33:56 +0100 Subject: [PATCH 2/4] Add desired_vdf_duration in mining param --- src/main.rs | 21 +++++++++++++++++---- src/utils/miner.rs | 27 +++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index c399ab5..d081a63 100644 --- a/src/main.rs +++ b/src/main.rs @@ -86,6 +86,8 @@ struct MineParams { rpc_url: Option<String>, // Chain ID chain_id: Option<u64>, + /// Desired VDF mean duration in seconds + desired_vdf_duration: Option<u64>, } async fn init() -> (u64, String) { @@ -217,8 +219,6 @@ async fn main() -> Result<(), anyhow::Error> { if params.chain_id.clone().is_some() { chain_id = params.chain_id.unwrap(); } - // println!("Processing {:?}", params.file); - // csv::Reader::from_path(path) let records = read_csv(csv::Reader::from_path(params.file.clone())?); if let Err(err) = records { error!("error running example: {}", err); @@ -444,13 +444,26 @@ async fn main() -> Result<(), anyhow::Error> { let miner_addr = wallet.address(); info!("Mining with 0x{:02x} ...", miner_addr); - let behavior = get_miner_behavior(wallet, chain_id, miner_addr); - // SEND IT let provider = Provider::<Http>::try_from(rpc_url.clone()).unwrap(); + let latest_block = provider.get_block(BlockNumber::Latest).await; + let network_difficulty = Some( + ethnum::U256::from_str(&latest_block.unwrap().unwrap().difficulty.to_string()) + .unwrap(), + ); + + let behavior = get_miner_behavior( + wallet, + chain_id, + miner_addr, + network_difficulty, + params.desired_vdf_duration, + ); + debug!( "Miner behavior: {:?}", serde_json::to_string(&behavior.clone()) ); + // SEND IT let block = provider .request::<[Behavior; 1], Block>("emc_mine", [behavior]) .await diff --git a/src/utils/miner.rs b/src/utils/miner.rs index 48d6eaa..4afe052 100644 --- a/src/utils/miner.rs +++ b/src/utils/miner.rs @@ -13,17 +13,40 @@ use ethers::{ use ethnum::U256; /// Get a RPC Behavior for miner with default values, ready to send. -pub fn get_miner_behavior(wallet: LocalWallet, chain_id: u64, to: Address) -> Behavior { +pub fn get_miner_behavior( + wallet: LocalWallet, + chain_id: u64, + to: Address, + network_difficulty: Option<U256>, + desired_vdf_duration: Option<u64>, +) -> Behavior { let now = SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH) .unwrap() .as_secs(); + // const VDF_DIFFICULTY_FOR_10_MIN: u64 = 72; + // Mean VDF difficulty per sec = 72/600 on current machine (Macbook M1) and a hash maxed to 1 day. + // for a network difficulty of 1000000 + const MEAN_VDF_DIFFICULTY_PER_SEC: u64 = 12; + + let quantity = match network_difficulty { + Some(net_dif) => match desired_vdf_duration { + Some(vdf_dur) => net_dif + .saturating_mul(U256::from(MEAN_VDF_DIFFICULTY_PER_SEC * vdf_dur)) + .saturating_div(U256::from(1000000_u128)), + _ => net_dif + .saturating_mul(U256::from(MEAN_VDF_DIFFICULTY_PER_SEC * 60)) + .saturating_div(U256::from(1000000_u128)), + }, + // default 1 min + _ => U256::from(MEAN_VDF_DIFFICULTY_PER_SEC * 60), + }; let behavior_msg = BehaviorMessage{ chain_id: U64::from(chain_id), to:Some(to), timestamp: U64::from(now), - quantity: U256::from(210000_u64), + quantity, input: Bytes::from("0x015d8eb90000000000000000000000000000000000000000000000000000000000878c1c00000000000000000000000000000000000000000000000000000000644662bc0000000000000000000000000000000000000000000000000000001ee24fba17b7e19cc10812911dfa8a438e0a81a9933f843aa5b528899b8d9e221b649ae0df00000000000000000000000000000000000000000000000000000000000000060000000000000000000000007431310e026b69bfc676c0013e12a1a11411eec9000000000000000000000000000000000000000000000000000000000000083400000000000000000000000000000000000000000000000000000000000f4240") }; // let behavior = wallet. -- GitLab From 192287971a3f1b7d794f326de6bd907cd417722b Mon Sep 17 00:00:00 2001 From: Anthony Graignic <anthony.graignic@uca.fr> Date: Thu, 25 Jan 2024 16:36:14 +0100 Subject: [PATCH 3/4] Updatre base quantity in generated bxs --- src/utils/mobility_data.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/utils/mobility_data.rs b/src/utils/mobility_data.rs index 4ce2d0b..6e15726 100644 --- a/src/utils/mobility_data.rs +++ b/src/utils/mobility_data.rs @@ -123,19 +123,18 @@ pub fn compute_stretched_relative_start_time( /// Provide differents fake quantities depending on mobility e.g. BIKE that will be use for rewards calculation /// Coefficients per km from https://nosgestesclimat.fr/actions/plus/transport/voiture-5km pub fn quantify_mobility(service: String) -> U256 { - // let base_vdf_divider = - let base_vdf_divider = + let base_quantity = U256::from(1000000 * 12); // default network difficulty * mean vdf difficulty per sec + // U256::from(1_000_000_000_000_000_000_000_000_000_u128); - U256::from("0x0000000000000000ffffffffffffffffffffffffffffffffffffffffffffffff"); if service == "BIKE_SHARING" { // 0.129/0.0001 = 1290 - base_vdf_divider.saturating_mul(U256::from(1290_u128)) + base_quantity.saturating_mul(U256::from(1290_u128)) } else if service == "BIKE_RENTAL" { - base_vdf_divider.saturating_mul(U256::from(1290_u128)) + base_quantity.saturating_mul(U256::from(1290_u128)) } else if service == "PUBLIC_TRANSPORT" { - base_vdf_divider.saturating_mul(U256::from(1_u128)) + base_quantity.saturating_mul(U256::from(1_u128)) } else { - base_vdf_divider + base_quantity } } @@ -289,6 +288,6 @@ mod tests { // ACT let result = quantify_mobility(String::from("BIKE_SHARING")); // ASSERT - assert_eq!(U256::from(1290000000000000000000_u128), result); + assert_eq!(U256::from(15480000000_u128), result); } } -- GitLab From c862d9056f3be5eaaeb6b8b21cccacb5c95fb132 Mon Sep 17 00:00:00 2001 From: Anthony Graignic <anthony.graignic@uca.fr> Date: Thu, 25 Jan 2024 16:37:57 +0100 Subject: [PATCH 4/4] Add example in README for mining in a desired time --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index eeff552..931069e 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,9 @@ Options: - (optional) `rpc_url` Node RPC url e.g. http://127.0.0.1:8545 - (optional) `chain_id` Chain id e.g. 636363 +- (optional) `desired_vdf_duration` Desired VDF duration = mining duration e.g. 60secs + +Example: `cargo run mine http://127.0.0.1:8545' 636363 60` ## Env vars -- GitLab