diff --git a/src/consensus/pob/miner.rs b/src/consensus/pob/miner.rs index c172bbec8263888cd535555c3a4033d2ff1c6902..d335a8903c1c7a9618e2ca6788ba871c36087aed 100644 --- a/src/consensus/pob/miner.rs +++ b/src/consensus/pob/miner.rs @@ -963,7 +963,7 @@ pub fn check_behavior_for_duplicates<SE: EnvironmentKind>( txn: &MdbxTransaction<RO, SE>, behavior: Behavior, ) -> anyhow::Result<()> { - // Check behaviors' data + // TODO Check behaviors' input data let result = bl::read(txn, behavior.hash())?; if result == None { @@ -1902,14 +1902,14 @@ mod tests { let pob_m = Behavior { chain_id: ChainId(63), timestamp: 1672534861, - quantity: U256::from((100_000u64 << 53) / 6500), + quantity: U256::from(100_000u64), input: BEHAVIOR_INPUT_EXAMPLE, }; - let network_difficulty = 1_000; + let network_difficulty = 1_000_000; // ACT let result = prepare_work(parent_hash, pob_m, network_difficulty); // ASSERT - assert_eq!(19500, result); + assert_eq!(155, result); } #[test] diff --git a/src/consensus/pob/vdf.rs b/src/consensus/pob/vdf.rs index ead2570f9cde038e5561a999adab445a6e0f37e3..ee340badc8ed558946b023d71b562195e35c7b03 100644 --- a/src/consensus/pob/vdf.rs +++ b/src/consensus/pob/vdf.rs @@ -1,5 +1,8 @@ +use std::time::{Duration, Instant}; + use ethereum_types::H256; use ethnum::U256; +use tracing::log::debug; use vdf::{InvalidIterations, InvalidProof, VDFParams, WesolowskiVDFParams, VDF}; use crate::h256_to_u256; @@ -11,16 +14,16 @@ const INT_SIZE_BITS: u16 = 2048; // vdf-0.1.0/src/proof_wesolowski.rs:271:5 // const MAXIMUM_ITERATIONS: u64 = (1u64 << 53) - 1; -const MAXIMUM_ITERATIONS: u64 = (1u64 << 53) - 1; +const MAXIMUM_ITERATIONS: u64 = 800_000_000; //approximately one day /// Compute the delay parameter in seconds of a VDF to run pub fn compute_vdf_difficulty(network_difficulty: u64, hash: H256, quantity: U256) -> u64 { - network_difficulty - .checked_mul((h256_to_u256(hash) / quantity).as_u64()) - .unwrap_or(MAXIMUM_ITERATIONS) - // .min(MAXIMUM_ITERATIONS) - // FIXME temporary min for tests, issue #97 - .min(3 * VDF_DIFFICULTY_PER_SEC) + let vdf_difficulty = U256::from(network_difficulty) + .wrapping_mul(h256_to_u256(hash)) + .as_u64() + % MAXIMUM_ITERATIONS + / quantity.as_u64(); + vdf_difficulty } /// Launch a VDF and returns its output. @@ -38,7 +41,19 @@ pub async fn vdf<'a>( if difficulty_check.is_err() { return Err(difficulty_check.err().unwrap()); } - wesolowski_vdf.solve(&*challenge, vdf_difficulty) + debug!( + "Solving wesolowski_vdf, estimated time: {:?} (={:?}min)", + Duration::from_secs(vdf_difficulty / VDF_DIFFICULTY_PER_SEC), + vdf_difficulty / VDF_DIFFICULTY_PER_SEC / 60 + ); + let start = Instant::now(); + let result = wesolowski_vdf.solve(&*challenge, vdf_difficulty); + debug!( + "Solved wesolowski_vdf in {:?} (estimated: {:?})", + start.elapsed(), + Duration::from_secs(vdf_difficulty / VDF_DIFFICULTY_PER_SEC) + ); + result }) .await .unwrap() @@ -66,22 +81,46 @@ mod tests { #[test] fn compute_vdf_difficulty_should_work() { assert_eq!( + 601, compute_vdf_difficulty( 1, H256::from(hex!( "9fa752911d55c3a1246133fe280785afbdba41f357e9cae1131d5f5b0a078b9c" )), - U256::from(20_000_u128), - ), - 4428209782989260873 - ) + U256::from(100_000_u128), + ) + ); + assert_eq!( + 416, + compute_vdf_difficulty( + 100_000, + H256::from(hex!( + "977c701ceeaeeca4a3693141b0016c1e0a743f465b2d560023a1cbb1eb1f5cd6" + )), + U256::from(1_210_000_u128), + ) + ); } #[test] fn compute_vdf_difficulty_overflow() { assert_eq!( - compute_vdf_difficulty(u64::MAX - 1, H256::repeat_byte(0xFF), U256::ONE), - MAXIMUM_ITERATIONS + 2, + compute_vdf_difficulty(u64::MAX - 1, H256::repeat_byte(0xFF), U256::ONE) + ) + } + + #[test] + fn compute_vdf_difficulty_max() { + assert_eq!( + MAXIMUM_ITERATIONS - 1, + compute_vdf_difficulty( + 1, + H256::from(hex!( + "000000000000000000000000000000000000000000000000000000002FAF07FF" + )), + U256::ONE, + ) ) } diff --git a/src/execution/processor.rs b/src/execution/processor.rs index 7b005bc26e6a2c86b96b96bfe6710059d99efc7e..5a69966cbec762814b1750dd0c2a998942ae9da8 100644 --- a/src/execution/processor.rs +++ b/src/execution/processor.rs @@ -557,7 +557,7 @@ mod tests { use hex_literal::hex; use crate::{ - chain::protocol_param::{fee}, + chain::protocol_param::fee, execution::{address::create_address, tracer::NoopTracer}, res::chainspec::MAINNET, InMemoryState, StateReader, StateWriter,