Skip to content

Commit fa65d9b

Browse files
Merge pull request #57 from Sovenok-Hacker/rewrite
Some clippy fixes
2 parents 340510f + d42f4a3 commit fa65d9b

File tree

8 files changed

+39
-49
lines changed

8 files changed

+39
-49
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ log = "0.4.21"
1717
num-bigint = "0.4.4"
1818
num-traits = "0.2.18"
1919
rsa = "0.9.6"
20-
secp256k1 = { version = "0.28.2", features = ["rand-std"] }
20+
secp256k1 = { version = "0.29.0", features = ["rand-std"] }
2121
sha2 = "0.10.8"
2222
sled = "0.34.7"
23-
thiserror = "1.0.58"
23+
thiserror = "1.0.59"
2424
tokio = { version = "1.37.0", features = ["full"] }
2525
zstd = "0.13.1"
2626
primitive-types = "0.12.2"
27-
async-trait = "0.1.79"
27+
async-trait = "0.1.80"
2828

2929
[dev-dependencies]
3030
rand = "0.8.5"

src/blockchaintree.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ use std::{collections::HashMap, path::Path};
33
use crate::{
44
chain,
55
errors::{BCTreeErrorKind, BlockChainTreeError},
6-
tools, txpool,
6+
tools,
77
};
88
use error_stack::{Report, ResultExt};
9-
use lazy_static::lazy_static;
109
use primitive_types::U256;
1110
use sled::Db;
1211

src/chain.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ use tokio::{fs::OpenOptions, io::AsyncWriteExt, sync::RwLock};
99
use crate::block::DerivativeBlock;
1010
use crate::dump_headers::Headers;
1111
use crate::{
12-
block::{self, BasicInfo, Block, SummarizeBlock, TransactionBlock},
13-
errors::DerivChainErrorKind,
12+
block::{self, BasicInfo, Block, SummarizeBlock},
1413
errors::{BlockChainTreeError, ChainErrorKind},
1514
merkletree::MerkleTree,
1615
tools,
17-
transaction::{Transaction, Transactionable},
16+
transaction::Transactionable,
1817
};
1918
use crate::{static_values::*, transaction};
2019

@@ -148,6 +147,7 @@ impl MainChain {
148147
let mut file = OpenOptions::new()
149148
.write(true)
150149
.create(true)
150+
.truncate(true)
151151
.open(path_config)
152152
.await
153153
.change_context(BlockChainTreeError::Chain(ChainErrorKind::DumpConfig))?;
@@ -249,7 +249,7 @@ impl MainChain {
249249
let raw_transaction = self.get_transaction_raw(transaction_hash)?;
250250

251251
if let Some(tr) = raw_transaction {
252-
if !tr.get(0).unwrap_or(&10).eq(&(Headers::Transaction as u8)) {
252+
if !tr.first().unwrap_or(&10).eq(&(Headers::Transaction as u8)) {
253253
return Err(BlockChainTreeError::Chain(ChainErrorKind::FindByHashE).into());
254254
}
255255
return Ok(Some(
@@ -329,7 +329,7 @@ impl MainChain {
329329
height.to_big_endian(&mut height_serialized);
330330
let mut dump = self
331331
.blocks
332-
.get(&height_serialized)
332+
.get(height_serialized)
333333
.change_context(BlockChainTreeError::Chain(ChainErrorKind::FindByHeight))?;
334334

335335
if let Some(dump) = dump.take() {
@@ -529,6 +529,7 @@ impl DerivativeChain {
529529
let mut file = OpenOptions::new()
530530
.write(true)
531531
.create(true)
532+
.truncate(true)
532533
.open(path_config)
533534
.await
534535
.change_context(BlockChainTreeError::Chain(ChainErrorKind::DumpConfig))?;
@@ -620,7 +621,7 @@ impl DerivativeChain {
620621
let raw_transaction = self.get_transaction_raw(transaction_hash)?;
621622

622623
if let Some(tr) = raw_transaction {
623-
if !tr.get(0).unwrap_or(&10).eq(&(Headers::Transaction as u8)) {
624+
if !tr.first().unwrap_or(&10).eq(&(Headers::Transaction as u8)) {
624625
return Err(BlockChainTreeError::Chain(ChainErrorKind::FindByHashE).into());
625626
}
626627
return Ok(Some(
@@ -700,7 +701,7 @@ impl DerivativeChain {
700701
height.to_big_endian(&mut height_serialized);
701702
let mut dump = self
702703
.blocks
703-
.get(&height_serialized)
704+
.get(height_serialized)
704705
.change_context(BlockChainTreeError::Chain(ChainErrorKind::FindByHeight))?;
705706

706707
if let Some(dump) = dump.take() {
@@ -774,7 +775,9 @@ impl DerivativeChain {
774775
Some(Arc::new(
775776
block::DerivativeBlock::parse(&data[1..])
776777
.change_context(BlockChainTreeError::Chain(ChainErrorKind::FindByHeight))
777-
.attach_printable(format!("Failed to deserialize latest main chain block",))?,
778+
.attach_printable(
779+
"Failed to deserialize latest main chain block".to_string(),
780+
)?,
778781
))
779782
} else {
780783
None

src/merkletree.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl MerkleTree {
5959
}
6060
//hasher.reset();
6161

62-
let hash = Sha256::digest(&to_hash);
62+
let hash = Sha256::digest(to_hash);
6363

6464
*(array_representation.get_unchecked_mut((left_index - 1) / 2)) =
6565
hash.as_slice().try_into().unwrap_unchecked();

src/tools.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use error_stack::{Report, Result, ResultExt};
55
use num_bigint::BigUint;
66
use primitive_types::U256;
77
use sha2::{Digest, Sha256};
8+
use std::cmp::Ordering;
89
use std::convert::TryInto;
910
use std::fs::File;
1011
use std::io::Read;
@@ -35,7 +36,7 @@ pub fn dump_u256(number: &U256, buffer: &mut Vec<u8>) -> Result<(), ToolsError>
3536
let mut counter: u8 = 0;
3637

3738
for num in number.0.iter().rev() {
38-
let bytes = unsafe { transmute::<u64, [u8; 8]>(num.to_be()) };
39+
let bytes = num.to_be().to_ne_bytes();
3940
for byte in bytes {
4041
if found_non_null {
4142
buffer.push(byte);
@@ -216,19 +217,23 @@ pub fn recalculate_difficulty(prev_timestamp: u64, timestamp: u64, prev_difficul
216217
break;
217218
};
218219
}
219-
if timestamp - prev_timestamp < TIME_PER_BLOCK {
220-
let val = unsafe { prev_difficulty.get_unchecked_mut(non_zero_index) };
221-
*val = *val >> 1;
222-
} else if timestamp - prev_timestamp > TIME_PER_BLOCK {
223-
let mut val = unsafe { prev_difficulty.get_unchecked_mut(non_zero_index) };
224-
if non_zero_index == 0 && *val == 0x7f {
225-
return;
220+
match (timestamp - prev_timestamp).cmp(&TIME_PER_BLOCK) {
221+
Ordering::Less => {
222+
let val = unsafe { prev_difficulty.get_unchecked_mut(non_zero_index) };
223+
*val >>= 1;
226224
}
227-
if *val == 0xFF {
228-
val = unsafe { prev_difficulty.get_unchecked_mut(non_zero_index - 1) };
225+
Ordering::Greater => {
226+
let mut val = unsafe { prev_difficulty.get_unchecked_mut(non_zero_index) };
227+
if non_zero_index == 0 && *val == 0x7f {
228+
return;
229+
}
230+
if *val == 0xFF {
231+
val = unsafe { prev_difficulty.get_unchecked_mut(non_zero_index - 1) };
232+
}
233+
*val <<= 1;
234+
*val += 1;
229235
}
230-
*val = *val << 1;
231-
*val += 1;
236+
Ordering::Equal => (),
232237
}
233238
}
234239

@@ -237,7 +242,7 @@ mod tests {
237242

238243
use primitive_types::U256;
239244

240-
use crate::static_values::BEGINNING_DIFFICULTY;
245+
241246

242247
use super::{dump_u256, load_u256, recalculate_difficulty};
243248

src/transaction.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,7 @@ impl Ord for TransactionableItem {
4242

4343
impl PartialOrd for TransactionableItem {
4444
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
45-
Some(match self.get_timestamp().cmp(&other.get_timestamp()) {
46-
Ordering::Less => Ordering::Greater,
47-
Ordering::Equal => {
48-
let tr_hash: [u64; 4] = unsafe { transmute(self.hash()) };
49-
let other_hash: [u64; 4] = unsafe { transmute(other.hash()) };
50-
51-
for (left, right) in tr_hash.iter().zip(other_hash.iter()) {
52-
match left.cmp(right) {
53-
Ordering::Less => return Some(Ordering::Greater),
54-
Ordering::Equal => {}
55-
Ordering::Greater => return Some(Ordering::Less),
56-
}
57-
}
58-
Ordering::Equal
59-
}
60-
Ordering::Greater => Ordering::Less,
61-
})
45+
Some(self.cmp(other))
6246
}
6347
}
6448

tests/block_test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ fn dump_parse_derivative_block() {
171171
payment_transaction,
172172
};
173173
let dumped_block = derivative_block.dump().unwrap();
174-
let parsed_block = DerivativeBlock::parse(&dumped_block[1..].to_vec()).unwrap();
174+
let parsed_block = DerivativeBlock::parse(&dumped_block[1..]).unwrap();
175175

176176
assert_eq!(
177177
derivative_block.default_info.timestamp,
@@ -216,7 +216,7 @@ fn validate_derivative_block() {
216216
};
217217
let prev_block = DerivativeBlock {
218218
default_info: basic_data,
219-
payment_transaction: payment_transaction,
219+
payment_transaction,
220220
};
221221
let payment_transaction = [0; 32];
222222
let basic_data = block::BasicInfo {
@@ -229,7 +229,7 @@ fn validate_derivative_block() {
229229
};
230230
let derivative_block = DerivativeBlock {
231231
default_info: basic_data,
232-
payment_transaction: payment_transaction,
232+
payment_transaction,
233233
};
234234

235235
assert!(!derivative_block

tests/chain_test.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use blockchaintree::{
22
block,
3-
chain::{self, Chain, DerivativeChain},
4-
static_values::{BEGINNING_DIFFICULTY, INCEPTION_TIMESTAMP, ROOT_PUBLIC_ADDRESS},
3+
chain,
54
tools,
65
transaction::{self, Transactionable},
76
};

0 commit comments

Comments
 (0)