From 26a618f48f8c5c61de16ef1f3634c982370cb61b Mon Sep 17 00:00:00 2001 From: Slavik Date: Sat, 21 Dec 2024 16:11:05 +0100 Subject: [PATCH] feat: add xxhash --- Cargo.toml | 7 ++++--- rust-toolchain.toml | 3 +++ src/chain.rs | 15 ++++++++------- 3 files changed, 15 insertions(+), 10 deletions(-) create mode 100644 rust-toolchain.toml diff --git a/Cargo.toml b/Cargo.toml index 97b3d0a..eedf605 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,8 +17,9 @@ name = "blockchain" path = "src/lib.rs" [dependencies] -chrono = "0.4.38" +chrono = "0.4.39" rand = "0.8.5" -serde = { version = "1.0.210", features = ["derive"] } -serde_json = "1.0.132" +serde = { version = "1.0.216", features = ["derive"] } +serde_json = "1.0.133" sha2 = "0.10.8" +twox-hash = "2.1.0" diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..d8dbc11 --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,3 @@ +[toolchain] +channel = "1.83.0" +components = ["clippy", "rustfmt"] diff --git a/src/chain.rs b/src/chain.rs index e57fe63..f810d42 100644 --- a/src/chain.rs +++ b/src/chain.rs @@ -3,6 +3,7 @@ use std::{collections::HashMap, fmt::Write, iter}; use rand::Rng; use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; +use twox_hash::xxhash64::RandomState; use crate::{Block, Transaction, Wallet}; @@ -27,8 +28,8 @@ pub struct Chain { /// Transaction fee. pub fee: f64, - /// A map to associate wallets with their corresponding addresses and balances. - pub wallets: HashMap, + /// Map to associate wallets with their corresponding addresses and balances. + pub wallets: HashMap, } impl Chain { @@ -46,9 +47,9 @@ impl Chain { fee, reward, difficulty, - chain: Vec::new(), - wallets: HashMap::new(), - current_transactions: Vec::new(), + chain: vec![], + wallets: HashMap::default(), + current_transactions: vec![], address: Chain::generate_address(42), }; @@ -67,7 +68,7 @@ impl Chain { /// A reference to a vector containing the current transactions for the specified page. pub fn get_transactions(&self, page: usize, size: usize) -> Vec { // Calculate the total number of pages - let total_pages = (self.current_transactions.len() + size - 1) / size; + let total_pages = self.current_transactions.len().div_ceil(size); // Return an empty vector if the page is greater than the total number of pages if page > total_pages { @@ -238,7 +239,7 @@ impl Chain { let mut result = Vec::new(); // Calculate the total number of pages - let total_pages = (self.current_transactions.len() + size - 1) / size; + let total_pages = self.current_transactions.len().div_ceil(size); // Return an empty vector if the page is greater than the total number of pages if page > total_pages {