diff --git a/tig-benchmarker/src/benchmarker/find_proof_to_submit.rs b/tig-benchmarker/src/benchmarker/find_proof_to_submit.rs index 7ea1dbb..fce0c07 100644 --- a/tig-benchmarker/src/benchmarker/find_proof_to_submit.rs +++ b/tig-benchmarker/src/benchmarker/find_proof_to_submit.rs @@ -14,11 +14,11 @@ pub async fn execute() -> Result)>> { continue; } if let Some(state) = &benchmarks[benchmark_id].state { - let sampled_nonces: HashSet = + let sampled_nonces: HashSet = state.sampled_nonces.clone().unwrap().into_iter().collect(); let mut solutions_data = proof.solutions_data.take().unwrap(); solutions_data.retain(|x| sampled_nonces.contains(&x.nonce)); - let extracted_nonces: HashSet = solutions_data.iter().map(|x| x.nonce).collect(); + let extracted_nonces: HashSet = solutions_data.iter().map(|x| x.nonce).collect(); if extracted_nonces != sampled_nonces { return Err(format!( "No solutions for sampled nonces: '{:?}'", diff --git a/tig-benchmarker/src/benchmarker/mod.rs b/tig-benchmarker/src/benchmarker/mod.rs index 6f862ad..a3502b0 100644 --- a/tig-benchmarker/src/benchmarker/mod.rs +++ b/tig-benchmarker/src/benchmarker/mod.rs @@ -67,54 +67,54 @@ pub struct Job { pub benchmark_id: String, pub settings: BenchmarkSettings, pub solution_signature_threshold: u32, - pub sampled_nonces: Option>, + pub sampled_nonces: Option>, pub wasm_vm_config: WasmVMConfig, } #[derive(Serialize, Debug, Clone)] pub struct NonceIterator { - nonces: Option>, - current: u32, - attempts: u32, + nonces: Option>, + current: u64, + attempts: u64, } impl NonceIterator { - pub fn from_vec(nonces: Vec) -> Self { + pub fn from_vec(nonces: Vec) -> Self { Self { nonces: Some(nonces), current: 0, attempts: 0, } } - pub fn from_u32(start: u32) -> Self { + pub fn from_u64(start: u64) -> Self { Self { nonces: None, current: start, attempts: 0, } } - pub fn attempts(&self) -> u32 { + pub fn attempts(&self) -> u64 { self.attempts } pub fn is_empty(&self) -> bool { - self.nonces.as_ref().is_some_and(|x| x.is_empty()) || self.current == u32::MAX + self.nonces.as_ref().is_some_and(|x| x.is_empty()) || self.current == u64::MAX } pub fn empty(&mut self) { if let Some(nonces) = self.nonces.as_mut() { nonces.clear(); } - self.current = u32::MAX; + self.current = u64::MAX; } } impl Iterator for NonceIterator { - type Item = u32; + type Item = u64; fn next(&mut self) -> Option { if let Some(nonces) = self.nonces.as_mut() { let value = nonces.pop(); - self.attempts += value.is_some() as u32; + self.attempts += value.is_some() as u64; value - } else if self.current < u32::MAX { + } else if self.current < u64::MAX { let value = Some(self.current); self.attempts += 1; self.current += 1; @@ -273,8 +273,8 @@ async fn run_once(num_workers: u32, ms_per_benchmark: u32) -> Result<()> { None => (0..num_workers) .into_iter() .map(|x| { - Arc::new(Mutex::new(NonceIterator::from_u32( - u32::MAX / num_workers * x, + Arc::new(Mutex::new(NonceIterator::from_u64( + u64::MAX / num_workers as u64 * x as u64, ))) }) .collect(), @@ -317,7 +317,7 @@ async fn run_once(num_workers: u32, ms_per_benchmark: u32) -> Result<()> { .. } = &mut (*state().lock().await); if time_left.as_mut().unwrap().update().finished() - || (finished && num_solutions == num_attempts) // nonce_iter is only empty if recomputing + || (finished && num_solutions == (num_attempts as u32)) // nonce_iter is only empty if recomputing || *status == Status::Stopping { break; diff --git a/tig-benchmarker/src/main.rs b/tig-benchmarker/src/main.rs index d14c106..0402b24 100644 --- a/tig-benchmarker/src/main.rs +++ b/tig-benchmarker/src/main.rs @@ -72,7 +72,7 @@ fn cli() -> Command { .long("offset") .help("(Optional) Set nonce offset for each slave") .default_value("5000000") - .value_parser(value_parser!(u32)), + .value_parser(value_parser!(u64)), ) } @@ -87,7 +87,7 @@ async fn main() { let api_url = matches.get_one::("api").unwrap().clone(); let api_key = matches.get_one::("API_KEY").unwrap().clone(); let player_id = matches.get_one::("PLAYER_ID").unwrap().clone(); - let nonce_offset = matches.get_one::("offset").unwrap().clone(); + let nonce_offset = matches.get_one::("offset").unwrap().clone(); if let Some(master) = matches.get_one::("master") { slave_node(master, port, num_workers).await; } else { @@ -158,7 +158,7 @@ async fn slave_node(master: &String, port: u16, num_workers: u32) { ) .await { - Ok(resp) => dejsonify::(&resp).unwrap(), + Ok(resp) => dejsonify::(&resp).unwrap(), Err(e) => { println!("Error getting nonce offset: {:?}", e); sleep(5000).await; @@ -171,8 +171,8 @@ async fn slave_node(master: &String, port: u16, num_workers: u32) { nonce_iters = (0..num_workers) .into_iter() .map(|x| { - Arc::new(Mutex::new(NonceIterator::from_u32( - offset + u32::MAX / num_workers * x, + Arc::new(Mutex::new(NonceIterator::from_u64( + offset + u64::MAX / num_workers as u64 * x as u64, ))) }) .collect(); @@ -237,7 +237,7 @@ async fn master_node( duration: u32, algorithms_path: &PathBuf, port: u16, - nonce_offset: u32, + nonce_offset: u64, ) { benchmarker::setup(api_url, api_key, player_id).await; benchmarker::start(num_workers, duration).await; @@ -247,9 +247,9 @@ async fn master_node( .and(warp::get()) .and(warp::any().map(move || offsets.clone())) .and_then( - move |slave_id: String, offsets: Arc>>| async move { + move |slave_id: String, offsets: Arc>>| async move { let offsets = &mut (*offsets).lock().await; - let len = offsets.len() as u32; + let len = offsets.len() as u64; let o = offsets .entry(slave_id) .or_insert_with(|| (len + 1) * nonce_offset); diff --git a/tig-challenges/src/knapsack.rs b/tig-challenges/src/knapsack.rs index ceb38a1..ab20685 100644 --- a/tig-challenges/src/knapsack.rs +++ b/tig-challenges/src/knapsack.rs @@ -47,7 +47,7 @@ impl TryFrom> for Solution { #[derive(Serialize, Deserialize, Debug)] pub struct Challenge { - pub seed: u32, + pub seed: u64, pub difficulty: Difficulty, pub weights: Vec, pub values: Vec, @@ -62,7 +62,7 @@ pub const KERNEL: Option = None; impl crate::ChallengeTrait for Challenge { #[cfg(feature = "cuda")] fn cuda_generate_instance( - seed: u32, + seed: u64, difficulty: &Difficulty, dev: &Arc, mut funcs: HashMap<&'static str, CudaFunction>, @@ -71,8 +71,8 @@ impl crate::ChallengeTrait for Challenge { Self::generate_instance(seed, difficulty) } - fn generate_instance(seed: u32, difficulty: &Difficulty) -> Result { - let mut rng: StdRng = StdRng::seed_from_u64(seed as u64); + fn generate_instance(seed: u64, difficulty: &Difficulty) -> Result { + let mut rng: StdRng = StdRng::seed_from_u64(seed); let weights: Vec = (0..difficulty.num_items) .map(|_| rng.gen_range(1..50)) diff --git a/tig-challenges/src/lib.rs b/tig-challenges/src/lib.rs index 8fc61fa..c2ac8b2 100644 --- a/tig-challenges/src/lib.rs +++ b/tig-challenges/src/lib.rs @@ -18,30 +18,30 @@ where T: SolutionTrait, U: DifficultyTrait, { - fn generate_instance(seed: u32, difficulty: &U) -> Result; - fn generate_instance_from_str(seed: u32, difficulty: &str) -> Result { + fn generate_instance(seed: u64, difficulty: &U) -> Result; + fn generate_instance_from_str(seed: u64, difficulty: &str) -> Result { Self::generate_instance(seed, &serde_json::from_str(difficulty)?) } - fn generate_instance_from_vec(seed: u32, difficulty: &Vec) -> Result { + fn generate_instance_from_vec(seed: u64, difficulty: &Vec) -> Result { match difficulty.as_slice().try_into() { Ok(difficulty) => Self::generate_instance_from_arr(seed, &difficulty), Err(_) => Err(anyhow!("Invalid difficulty length")), } } - fn generate_instance_from_arr(seed: u32, difficulty: &[i32; N]) -> Result { + fn generate_instance_from_arr(seed: u64, difficulty: &[i32; N]) -> Result { Self::generate_instance(seed, &U::from_arr(difficulty)) } #[cfg(feature = "cuda")] fn cuda_generate_instance( - seed: u32, + seed: u64, difficulty: &U, dev: &Arc, funcs: HashMap<&'static str, CudaFunction>, ) -> Result; #[cfg(feature = "cuda")] fn cuda_generate_instance_from_str( - seed: u32, + seed: u64, difficulty: &str, dev: &Arc, funcs: HashMap<&'static str, CudaFunction>, @@ -50,7 +50,7 @@ where } #[cfg(feature = "cuda")] fn cuda_generate_instance_from_vec( - seed: u32, + seed: u64, difficulty: &Vec, dev: &Arc, funcs: HashMap<&'static str, CudaFunction>, @@ -62,7 +62,7 @@ where } #[cfg(feature = "cuda")] fn cuda_generate_instance_from_arr( - seed: u32, + seed: u64, difficulty: &[i32; N], dev: &Arc, funcs: HashMap<&'static str, CudaFunction>, diff --git a/tig-challenges/src/satisfiability.rs b/tig-challenges/src/satisfiability.rs index 80767fe..d153f52 100644 --- a/tig-challenges/src/satisfiability.rs +++ b/tig-challenges/src/satisfiability.rs @@ -59,7 +59,7 @@ impl TryFrom> for Solution { #[derive(Serialize, Deserialize, Debug)] pub struct Challenge { - pub seed: u32, + pub seed: u64, pub difficulty: Difficulty, pub clauses: Vec>, } @@ -71,7 +71,7 @@ pub const KERNEL: Option = None; impl crate::ChallengeTrait for Challenge { #[cfg(feature = "cuda")] fn cuda_generate_instance( - seed: u32, + seed: u64, difficulty: &Difficulty, dev: &Arc, mut funcs: HashMap<&'static str, CudaFunction>, @@ -80,8 +80,8 @@ impl crate::ChallengeTrait for Challenge { Self::generate_instance(seed, difficulty) } - fn generate_instance(seed: u32, difficulty: &Difficulty) -> Result { - let mut rng = StdRng::seed_from_u64(seed as u64); + fn generate_instance(seed: u64, difficulty: &Difficulty) -> Result { + let mut rng = StdRng::seed_from_u64(seed); let num_clauses = (difficulty.num_variables as f64 * difficulty.clauses_to_variables_percent as f64 / 100.0) diff --git a/tig-challenges/src/vector_search.rs b/tig-challenges/src/vector_search.rs index 56d4856..9036dad 100644 --- a/tig-challenges/src/vector_search.rs +++ b/tig-challenges/src/vector_search.rs @@ -51,7 +51,7 @@ impl TryFrom> for Solution { #[derive(Serialize, Deserialize, Debug)] pub struct Challenge { - pub seed: u32, + pub seed: u64, pub difficulty: Difficulty, pub vector_database: Vec>, pub query_vectors: Vec>, @@ -73,7 +73,7 @@ pub const KERNEL: Option = None; impl ChallengeTrait for Challenge { #[cfg(feature = "cuda")] fn cuda_generate_instance( - seed: u32, + seed: u64, difficulty: &Difficulty, dev: &Arc, mut funcs: HashMap<&'static str, CudaFunction>, @@ -82,8 +82,8 @@ impl ChallengeTrait for Challenge { Self::generate_instance(seed, difficulty) } - fn generate_instance(seed: u32, difficulty: &Difficulty) -> Result { - let mut rng = StdRng::seed_from_u64(seed as u64); + fn generate_instance(seed: u64, difficulty: &Difficulty) -> Result { + let mut rng = StdRng::seed_from_u64(seed); let uniform = Uniform::from(0.0..1.0); let search_vectors = (0..100000) .map(|_| (0..250).map(|_| uniform.sample(&mut rng)).collect()) diff --git a/tig-challenges/src/vehicle_routing.rs b/tig-challenges/src/vehicle_routing.rs index 63f2229..927ecba 100644 --- a/tig-challenges/src/vehicle_routing.rs +++ b/tig-challenges/src/vehicle_routing.rs @@ -46,7 +46,7 @@ impl TryFrom> for Solution { #[derive(Serialize, Deserialize, Debug)] pub struct Challenge { - pub seed: u32, + pub seed: u64, pub difficulty: Difficulty, pub demands: Vec, pub distance_matrix: Vec>, @@ -61,7 +61,7 @@ pub const KERNEL: Option = None; impl crate::ChallengeTrait for Challenge { #[cfg(feature = "cuda")] fn cuda_generate_instance( - seed: u32, + seed: u64, difficulty: &Difficulty, dev: &Arc, mut funcs: HashMap<&'static str, CudaFunction>, @@ -70,8 +70,8 @@ impl crate::ChallengeTrait for Challenge { Self::generate_instance(seed, difficulty) } - fn generate_instance(seed: u32, difficulty: &Difficulty) -> Result { - let mut rng: StdRng = StdRng::seed_from_u64(seed as u64); + fn generate_instance(seed: u64, difficulty: &Difficulty) -> Result { + let mut rng: StdRng = StdRng::seed_from_u64(seed); let num_nodes = difficulty.num_nodes; let max_capacity = 100; diff --git a/tig-protocol/src/context.rs b/tig-protocol/src/context.rs index 8208b0b..1a25b8c 100644 --- a/tig-protocol/src/context.rs +++ b/tig-protocol/src/context.rs @@ -105,13 +105,13 @@ pub trait Context { async fn verify_solution( &self, settings: &BenchmarkSettings, - nonce: u32, + nonce: u64, solution: &Solution, ) -> ContextResult>; async fn compute_solution( &self, settings: &BenchmarkSettings, - nonce: u32, + nonce: u64, wasm_vm_config: &WasmVMConfig, ) -> ContextResult>; async fn get_transaction(&self, tx_hash: &String) -> ContextResult; diff --git a/tig-protocol/src/error.rs b/tig-protocol/src/error.rs index 6047063..d32eecd 100644 --- a/tig-protocol/src/error.rs +++ b/tig-protocol/src/error.rs @@ -12,7 +12,7 @@ pub enum ProtocolError { settings: BenchmarkSettings, }, DuplicateNonce { - nonce: u32, + nonce: u64, }, DuplicateProof { benchmark_id: String, @@ -35,7 +35,7 @@ pub enum ProtocolError { benchmark_id: String, }, InvalidBenchmarkNonce { - nonce: u32, + nonce: u64, }, InvalidBlock { block_id: String, @@ -48,23 +48,23 @@ pub enum ProtocolError { difficulty_parameters: Vec, }, InvalidProofNonces { - expected_nonces: Vec, - submitted_nonces: Vec, + expected_nonces: Vec, + submitted_nonces: Vec, }, InvalidSignatureFromSolutionData { actual_signature: u32, - nonce: u32, + nonce: u64, expected_signature: u32, }, InvalidSolution { - nonce: u32, + nonce: u64, }, InvalidSolutionData { algorithm_id: String, - nonce: u32, + nonce: u64, }, InvalidSolutionSignature { - nonce: u32, + nonce: u64, solution_signature: u32, threshold: u32, }, diff --git a/tig-protocol/src/submit_benchmark.rs b/tig-protocol/src/submit_benchmark.rs index 4c53b6a..d90aeeb 100644 --- a/tig-protocol/src/submit_benchmark.rs +++ b/tig-protocol/src/submit_benchmark.rs @@ -175,7 +175,7 @@ async fn verify_benchmark_settings_are_unique( #[time] fn verify_nonces_are_unique(solutions_meta_data: &Vec) -> ProtocolResult<()> { - let nonces: HashMap = + let nonces: HashMap = solutions_meta_data .iter() .fold(HashMap::new(), |mut acc, s| { @@ -267,7 +267,7 @@ async fn verify_solution_is_valid( solutions_meta_data: &Vec, solution_data: &SolutionData, ) -> ProtocolResult<()> { - let solutions_map: HashMap = solutions_meta_data + let solutions_map: HashMap = solutions_meta_data .iter() .map(|d| (d.nonce, d.solution_signature)) .collect(); diff --git a/tig-protocol/src/submit_proof.rs b/tig-protocol/src/submit_proof.rs index f02a552..67b0012 100644 --- a/tig-protocol/src/submit_proof.rs +++ b/tig-protocol/src/submit_proof.rs @@ -95,8 +95,8 @@ fn verify_sampled_nonces( benchmark: &Benchmark, solutions_data: &Vec, ) -> ProtocolResult<()> { - let sampled_nonces: HashSet = benchmark.state().sampled_nonces().iter().cloned().collect(); - let proof_nonces: HashSet = solutions_data.iter().map(|d| d.nonce).collect(); + let sampled_nonces: HashSet = benchmark.state().sampled_nonces().iter().cloned().collect(); + let proof_nonces: HashSet = solutions_data.iter().map(|d| d.nonce).collect(); if sampled_nonces != proof_nonces { return Err(ProtocolError::InvalidProofNonces { @@ -113,7 +113,7 @@ async fn verify_solutions_are_valid( benchmark: &Benchmark, solutions_data: &Vec, ) -> ProtocolResult<()> { - let solutions_map: HashMap = benchmark + let solutions_map: HashMap = benchmark .solutions_meta_data() .iter() .map(|d| (d.nonce, d.solution_signature)) diff --git a/tig-structs/src/core.rs b/tig-structs/src/core.rs index dbf78a6..f9c727a 100644 --- a/tig-structs/src/core.rs +++ b/tig-structs/src/core.rs @@ -2,7 +2,7 @@ use crate::{config::ProtocolConfig, serializable_struct_with_getters}; use serde::{Deserialize, Serialize}; use serde_json::{Map, Value}; use std::collections::{HashMap, HashSet}; -use tig_utils::{jsonify, u32_from_str}; +use tig_utils::{jsonify, u32_from_str, u64_from_str}; pub use tig_utils::{Frontier, Point, PreciseNumber, Transaction, U256}; serializable_struct_with_getters! { @@ -109,8 +109,8 @@ serializable_struct_with_getters! { } } impl BenchmarkSettings { - pub fn calc_seed(&self, nonce: u32) -> u32 { - u32_from_str(jsonify(&self).as_str()) ^ nonce + pub fn calc_seed(&self, nonce: u64) -> u64 { + u64_from_str(jsonify(&self).as_str()) ^ nonce } } serializable_struct_with_getters! { @@ -122,12 +122,12 @@ serializable_struct_with_getters! { serializable_struct_with_getters! { BenchmarkState { block_confirmed: Option, - sampled_nonces: Option>, + sampled_nonces: Option>, } } serializable_struct_with_getters! { SolutionMetaData { - nonce: u32, + nonce: u64, solution_signature: u32, } } @@ -219,7 +219,7 @@ serializable_struct_with_getters! { pub type Solution = Map; serializable_struct_with_getters! { SolutionData { - nonce: u32, + nonce: u64, runtime_signature: u32, fuel_consumed: u64, solution: Solution, diff --git a/tig-utils/Cargo.toml b/tig-utils/Cargo.toml index 03b580c..e2f9a0e 100644 --- a/tig-utils/Cargo.toml +++ b/tig-utils/Cargo.toml @@ -18,6 +18,7 @@ rand = { version = "0.8.5", default-features = false, features = ["std_rng"] } reqwest = { version = "0.12.2", optional = true } serde = { version = "1.0.196", features = ["derive"] } serde_json = { version = "1.0.113", features = ["preserve_order"] } +sha3 = "0.10.8" uint = "0.9.5" wasm-bindgen = { version = "0.2.91", optional = true } wasm-bindgen-futures = { version = "0.4.41", optional = true } diff --git a/tig-utils/src/hash.rs b/tig-utils/src/hash.rs index 6d862c2..98f7276 100644 --- a/tig-utils/src/hash.rs +++ b/tig-utils/src/hash.rs @@ -1,4 +1,5 @@ use md5; +use sha3::{Digest, Keccak256}; pub fn md5_from_str(input: &str) -> String { md5_from_bytes(input.as_bytes()) @@ -13,3 +14,14 @@ pub fn u32_from_str(input: &str) -> u32 { let bytes = result[12..16].try_into().expect("Should not ever panic.."); u32::from_le_bytes(bytes) } + +pub fn u64_from_str(input: &str) -> u64 { + let mut hasher = Keccak256::new(); + hasher.update(input.as_bytes()); + let result = hasher.finalize(); + u64::from_le_bytes( + result.as_slice()[0..8] + .try_into() + .expect("Should not ever panic.."), + ) +} diff --git a/tig-worker/src/main.rs b/tig-worker/src/main.rs index b757c75..fabaa22 100644 --- a/tig-worker/src/main.rs +++ b/tig-worker/src/main.rs @@ -15,7 +15,7 @@ fn cli() -> Command { arg!( "Settings json string or path to json file") .value_parser(clap::value_parser!(String)), ) - .arg(arg!( "Nonce value").value_parser(clap::value_parser!(u32))) + .arg(arg!( "Nonce value").value_parser(clap::value_parser!(u64))) .arg(arg!( "Path to a wasm file").value_parser(clap::value_parser!(PathBuf))) .arg( arg!(--fuel [FUEL] "Optional maximum fuel parameter for WASM VM") @@ -35,7 +35,7 @@ fn cli() -> Command { arg!( "Settings json string or path to json file") .value_parser(clap::value_parser!(String)), ) - .arg(arg!( "Nonce value").value_parser(clap::value_parser!(u32))) + .arg(arg!( "Nonce value").value_parser(clap::value_parser!(u64))) .arg( arg!( "Solution json string or path to json file") .value_parser(clap::value_parser!(String)), @@ -49,14 +49,14 @@ fn main() { match matches.subcommand() { Some(("compute_solution", sub_m)) => compute_solution( sub_m.get_one::("SETTINGS").unwrap().clone(), - *sub_m.get_one::("NONCE").unwrap(), + *sub_m.get_one::("NONCE").unwrap(), sub_m.get_one::("WASM").unwrap().clone(), *sub_m.get_one::("mem").unwrap(), *sub_m.get_one::("fuel").unwrap(), ), Some(("verify_solution", sub_m)) => verify_solution( sub_m.get_one::("SETTINGS").unwrap().clone(), - *sub_m.get_one::("NONCE").unwrap(), + *sub_m.get_one::("NONCE").unwrap(), sub_m.get_one::("SOLUTION").unwrap().clone(), ), _ => {} @@ -65,7 +65,7 @@ fn main() { fn compute_solution( mut settings: String, - nonce: u32, + nonce: u64, wasm_path: PathBuf, max_memory: u64, max_fuel: u64, @@ -114,7 +114,7 @@ fn compute_solution( } } -fn verify_solution(mut settings: String, nonce: u32, mut solution: String) { +fn verify_solution(mut settings: String, nonce: u64, mut solution: String) { if settings.ends_with(".json") { settings = fs::read_to_string(&settings).unwrap_or_else(|_| { eprintln!("Failed to read settings file: {}", settings); diff --git a/tig-worker/src/worker.rs b/tig-worker/src/worker.rs index 3486309..0a5dc6a 100644 --- a/tig-worker/src/worker.rs +++ b/tig-worker/src/worker.rs @@ -7,7 +7,7 @@ use wasmi::{Config, Engine, Linker, Module, Store, StoreLimitsBuilder}; pub fn compute_solution( settings: &BenchmarkSettings, - nonce: u32, + nonce: u64, wasm: &[u8], max_memory: u64, max_fuel: u64, @@ -118,7 +118,7 @@ pub fn compute_solution( pub fn verify_solution( settings: &BenchmarkSettings, - nonce: u32, + nonce: u64, solution: &Solution, ) -> Result<()> { let seed = settings.calc_seed(nonce);