From 761d62fba730a0f62983d5895c78000c7a9f231f Mon Sep 17 00:00:00 2001 From: Sovenok-Hacker Date: Sun, 28 Apr 2024 00:31:50 +0300 Subject: [PATCH 1/4] Some clippy fixes --- src/blockchaintree.rs | 3 +-- src/chain.rs | 19 +++++++++++-------- src/merkletree.rs | 2 +- src/tools.rs | 29 +++++++++++++++++------------ 4 files changed, 30 insertions(+), 23 deletions(-) diff --git a/src/blockchaintree.rs b/src/blockchaintree.rs index d8c7952..f706109 100644 --- a/src/blockchaintree.rs +++ b/src/blockchaintree.rs @@ -3,10 +3,9 @@ use std::{collections::HashMap, path::Path}; use crate::{ chain, errors::{BCTreeErrorKind, BlockChainTreeError}, - tools, txpool, + tools, }; use error_stack::{Report, ResultExt}; -use lazy_static::lazy_static; use primitive_types::U256; use sled::Db; diff --git a/src/chain.rs b/src/chain.rs index 183cfc7..5f5b036 100644 --- a/src/chain.rs +++ b/src/chain.rs @@ -9,12 +9,11 @@ use tokio::{fs::OpenOptions, io::AsyncWriteExt, sync::RwLock}; use crate::block::DerivativeBlock; use crate::dump_headers::Headers; use crate::{ - block::{self, BasicInfo, Block, SummarizeBlock, TransactionBlock}, - errors::DerivChainErrorKind, + block::{self, BasicInfo, Block, SummarizeBlock}, errors::{BlockChainTreeError, ChainErrorKind}, merkletree::MerkleTree, tools, - transaction::{Transaction, Transactionable}, + transaction::Transactionable, }; use crate::{static_values::*, transaction}; @@ -148,6 +147,7 @@ impl MainChain { let mut file = OpenOptions::new() .write(true) .create(true) + .truncate(true) .open(path_config) .await .change_context(BlockChainTreeError::Chain(ChainErrorKind::DumpConfig))?; @@ -249,7 +249,7 @@ impl MainChain { let raw_transaction = self.get_transaction_raw(transaction_hash)?; if let Some(tr) = raw_transaction { - if !tr.get(0).unwrap_or(&10).eq(&(Headers::Transaction as u8)) { + if !tr.first().unwrap_or(&10).eq(&(Headers::Transaction as u8)) { return Err(BlockChainTreeError::Chain(ChainErrorKind::FindByHashE).into()); } return Ok(Some( @@ -329,7 +329,7 @@ impl MainChain { height.to_big_endian(&mut height_serialized); let mut dump = self .blocks - .get(&height_serialized) + .get(height_serialized) .change_context(BlockChainTreeError::Chain(ChainErrorKind::FindByHeight))?; if let Some(dump) = dump.take() { @@ -529,6 +529,7 @@ impl DerivativeChain { let mut file = OpenOptions::new() .write(true) .create(true) + .truncate(true) .open(path_config) .await .change_context(BlockChainTreeError::Chain(ChainErrorKind::DumpConfig))?; @@ -620,7 +621,7 @@ impl DerivativeChain { let raw_transaction = self.get_transaction_raw(transaction_hash)?; if let Some(tr) = raw_transaction { - if !tr.get(0).unwrap_or(&10).eq(&(Headers::Transaction as u8)) { + if !tr.first().unwrap_or(&10).eq(&(Headers::Transaction as u8)) { return Err(BlockChainTreeError::Chain(ChainErrorKind::FindByHashE).into()); } return Ok(Some( @@ -700,7 +701,7 @@ impl DerivativeChain { height.to_big_endian(&mut height_serialized); let mut dump = self .blocks - .get(&height_serialized) + .get(height_serialized) .change_context(BlockChainTreeError::Chain(ChainErrorKind::FindByHeight))?; if let Some(dump) = dump.take() { @@ -774,7 +775,9 @@ impl DerivativeChain { Some(Arc::new( block::DerivativeBlock::parse(&data[1..]) .change_context(BlockChainTreeError::Chain(ChainErrorKind::FindByHeight)) - .attach_printable(format!("Failed to deserialize latest main chain block",))?, + .attach_printable( + "Failed to deserialize latest main chain block".to_string(), + )?, )) } else { None diff --git a/src/merkletree.rs b/src/merkletree.rs index 039f6a4..32e21cc 100644 --- a/src/merkletree.rs +++ b/src/merkletree.rs @@ -59,7 +59,7 @@ impl MerkleTree { } //hasher.reset(); - let hash = Sha256::digest(&to_hash); + let hash = Sha256::digest(to_hash); *(array_representation.get_unchecked_mut((left_index - 1) / 2)) = hash.as_slice().try_into().unwrap_unchecked(); diff --git a/src/tools.rs b/src/tools.rs index b960de7..722cdb7 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -5,6 +5,7 @@ use error_stack::{Report, Result, ResultExt}; use num_bigint::BigUint; use primitive_types::U256; use sha2::{Digest, Sha256}; +use std::cmp::Ordering; use std::convert::TryInto; use std::fs::File; use std::io::Read; @@ -35,7 +36,7 @@ pub fn dump_u256(number: &U256, buffer: &mut Vec) -> Result<(), ToolsError> let mut counter: u8 = 0; for num in number.0.iter().rev() { - let bytes = unsafe { transmute::(num.to_be()) }; + let bytes = num.to_be().to_ne_bytes(); for byte in bytes { if found_non_null { buffer.push(byte); @@ -216,19 +217,23 @@ pub fn recalculate_difficulty(prev_timestamp: u64, timestamp: u64, prev_difficul break; }; } - if timestamp - prev_timestamp < TIME_PER_BLOCK { - let val = unsafe { prev_difficulty.get_unchecked_mut(non_zero_index) }; - *val = *val >> 1; - } else if timestamp - prev_timestamp > TIME_PER_BLOCK { - let mut val = unsafe { prev_difficulty.get_unchecked_mut(non_zero_index) }; - if non_zero_index == 0 && *val == 0x7f { - return; + match (timestamp - prev_timestamp).cmp(&TIME_PER_BLOCK) { + Ordering::Less => { + let val = unsafe { prev_difficulty.get_unchecked_mut(non_zero_index) }; + *val >>= 1; } - if *val == 0xFF { - val = unsafe { prev_difficulty.get_unchecked_mut(non_zero_index - 1) }; + Ordering::Greater => { + let mut val = unsafe { prev_difficulty.get_unchecked_mut(non_zero_index) }; + if non_zero_index == 0 && *val == 0x7f { + return; + } + if *val == 0xFF { + val = unsafe { prev_difficulty.get_unchecked_mut(non_zero_index - 1) }; + } + *val <<= 1; + *val += 1; } - *val = *val << 1; - *val += 1; + Ordering::Equal => (), } } From 3ec94957242227bf86de2decb4077231b9e44050 Mon Sep 17 00:00:00 2001 From: Sovenok-Hacker Date: Sun, 28 Apr 2024 08:51:01 +0300 Subject: [PATCH 2/4] Some clippy autofixes --- src/tools.rs | 2 +- tests/block_test.rs | 6 +++--- tests/chain_test.rs | 3 +-- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/tools.rs b/src/tools.rs index 722cdb7..40ac4ee 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -242,7 +242,7 @@ mod tests { use primitive_types::U256; - use crate::static_values::BEGINNING_DIFFICULTY; + use super::{dump_u256, load_u256, recalculate_difficulty}; diff --git a/tests/block_test.rs b/tests/block_test.rs index 150ee4f..7fce1fe 100644 --- a/tests/block_test.rs +++ b/tests/block_test.rs @@ -171,7 +171,7 @@ fn dump_parse_derivative_block() { payment_transaction, }; let dumped_block = derivative_block.dump().unwrap(); - let parsed_block = DerivativeBlock::parse(&dumped_block[1..].to_vec()).unwrap(); + let parsed_block = DerivativeBlock::parse(&dumped_block[1..]).unwrap(); assert_eq!( derivative_block.default_info.timestamp, @@ -216,7 +216,7 @@ fn validate_derivative_block() { }; let prev_block = DerivativeBlock { default_info: basic_data, - payment_transaction: payment_transaction, + payment_transaction, }; let payment_transaction = [0; 32]; let basic_data = block::BasicInfo { @@ -229,7 +229,7 @@ fn validate_derivative_block() { }; let derivative_block = DerivativeBlock { default_info: basic_data, - payment_transaction: payment_transaction, + payment_transaction, }; assert!(!derivative_block diff --git a/tests/chain_test.rs b/tests/chain_test.rs index aea8311..e684fba 100644 --- a/tests/chain_test.rs +++ b/tests/chain_test.rs @@ -1,7 +1,6 @@ use blockchaintree::{ block, - chain::{self, Chain, DerivativeChain}, - static_values::{BEGINNING_DIFFICULTY, INCEPTION_TIMESTAMP, ROOT_PUBLIC_ADDRESS}, + chain::{self, Chain}, tools, transaction::{self, Transactionable}, }; From f8d2df4e034e9c6d8b4f51ba8800b22cc0595f2c Mon Sep 17 00:00:00 2001 From: Sovenok-Hacker Date: Sun, 28 Apr 2024 09:03:43 +0300 Subject: [PATCH 3/4] Bump deps. fix tests --- Cargo.toml | 6 +++--- tests/chain_test.rs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8230465..28fd020 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,14 +17,14 @@ log = "0.4.21" num-bigint = "0.4.4" num-traits = "0.2.18" rsa = "0.9.6" -secp256k1 = { version = "0.28.2", features = ["rand-std"] } +secp256k1 = { version = "0.29.0", features = ["rand-std"] } sha2 = "0.10.8" sled = "0.34.7" -thiserror = "1.0.58" +thiserror = "1.0.59" tokio = { version = "1.37.0", features = ["full"] } zstd = "0.13.1" primitive-types = "0.12.2" -async-trait = "0.1.79" +async-trait = "0.1.80" [dev-dependencies] rand = "0.8.5" diff --git a/tests/chain_test.rs b/tests/chain_test.rs index e684fba..3a0350f 100644 --- a/tests/chain_test.rs +++ b/tests/chain_test.rs @@ -1,6 +1,6 @@ use blockchaintree::{ block, - chain::{self, Chain}, + chain, tools, transaction::{self, Transactionable}, }; From d42f4a3b205e2e9033e01fc5619b2ef2a6ab5861 Mon Sep 17 00:00:00 2001 From: Sovenok-Hacker Date: Sun, 28 Apr 2024 09:07:29 +0300 Subject: [PATCH 4/4] Fix the last clippy warning --- src/transaction.rs | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/src/transaction.rs b/src/transaction.rs index adf5c7a..0e53568 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -42,23 +42,7 @@ impl Ord for TransactionableItem { impl PartialOrd for TransactionableItem { fn partial_cmp(&self, other: &Self) -> Option { - Some(match self.get_timestamp().cmp(&other.get_timestamp()) { - Ordering::Less => Ordering::Greater, - Ordering::Equal => { - let tr_hash: [u64; 4] = unsafe { transmute(self.hash()) }; - let other_hash: [u64; 4] = unsafe { transmute(other.hash()) }; - - for (left, right) in tr_hash.iter().zip(other_hash.iter()) { - match left.cmp(right) { - Ordering::Less => return Some(Ordering::Greater), - Ordering::Equal => {} - Ordering::Greater => return Some(Ordering::Less), - } - } - Ordering::Equal - } - Ordering::Greater => Ordering::Less, - }) + Some(self.cmp(other)) } }