From 9110ed742f7fd96ae25cdecd4547e1176fbda634 Mon Sep 17 00:00:00 2001 From: Nickqiaoo Date: Mon, 6 Nov 2023 00:05:28 +0800 Subject: [PATCH] fix:cargo clippy --- src/block.rs | 3 +-- src/blockchain.rs | 1 - src/cli.rs | 30 +++++++++++++----------------- src/iterator.rs | 1 - src/main.rs | 2 +- src/pow.rs | 2 +- src/transaction.rs | 11 +++++------ src/wallet.rs | 3 +-- 8 files changed, 22 insertions(+), 31 deletions(-) diff --git a/src/block.rs b/src/block.rs index 2448151..8f81c14 100644 --- a/src/block.rs +++ b/src/block.rs @@ -1,6 +1,5 @@ use crate::{pow::ProofOfWork, transaction::Transaction}; use serde::{Deserialize, Serialize}; -use serde_json; use sha2::{Digest, Sha256}; use std::{time, vec}; @@ -42,7 +41,7 @@ impl Block { pub fn serialize(&self) -> Vec { let json_str = serde_json::to_string(self).unwrap(); - return json_str.into_bytes(); + json_str.into_bytes() } pub fn hash_transactions(&self) -> Vec { diff --git a/src/blockchain.rs b/src/blockchain.rs index 0355231..3a8d0d5 100644 --- a/src/blockchain.rs +++ b/src/blockchain.rs @@ -4,7 +4,6 @@ use crate::{ transaction::{new_coinbase_tx, Transaction}, transaction_output::TXOutput, }; -use sled; use std::{collections::HashMap, error::Error}; const COINBASEDATA: &str = "coinbase"; diff --git a/src/cli.rs b/src/cli.rs index 2fb051b..4e0585e 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -3,7 +3,7 @@ use std::{println, vec}; use crate::{blockchain::Blockchain, pow::ProofOfWork, transaction, wallet, wallets::Wallets}; use structopt::StructOpt; -pub struct CLI { +pub struct Cli { pub cmd: Command, } @@ -42,7 +42,7 @@ pub enum Command { }, } -impl CLI { +impl Cli { pub fn run(&mut self) { match &self.cmd { Command::CreateBlockChain { address } => self.create_blockchain(address.to_string()), @@ -50,7 +50,7 @@ impl CLI { Command::PrintChain => self.print_chain(), Command::ListAddress => self.list_address(), Command::Send { from, to, amount } => { - self.send(from.to_string(), to.to_string(), amount.clone()) + self.send(from.to_string(), to.to_string(), *amount) } Command::Getbalance { address } => self.get_balance(address.to_string()), } @@ -79,20 +79,16 @@ impl CLI { let bc = Blockchain::new(""); let mut bci = bc.iterator(); - loop { - if let Some(block) = bci.next() { - println!("Prev hash: {:}", hex::encode(&block.prev_block_hash)); - println!("Hash: {:}", hex::encode(&block.hash)); - let pow = ProofOfWork::new(&block); - println!("PoW: {:}", pow.validate()); - println!("Transactions:"); - for (i, tx) in block.transactions.iter().enumerate() { - println!("tx{:}: {:}", i, tx); - } - println!(); - } else { - break; + while let Some(block) = bci.next() { + println!("Prev hash: {:}", hex::encode(&block.prev_block_hash)); + println!("Hash: {:}", hex::encode(&block.hash)); + let pow = ProofOfWork::new(&block); + println!("PoW: {:}", pow.validate()); + println!("Transactions:"); + for (i, tx) in block.transactions.iter().enumerate() { + println!("tx{:}: {:}", i, tx); } + println!(); } } @@ -104,7 +100,7 @@ impl CLI { panic!("Recipient address is not valid") } let mut bc = Blockchain::new(&from); - let tx = transaction::new_utxo_transaction(from.to_string(), to.to_string(), amount, &bc); + let tx = transaction::new_utxo_transaction(from, to, amount, &bc); bc.mine_block(vec![tx]); println!("Success!"); } diff --git a/src/iterator.rs b/src/iterator.rs index 5e9fb73..8ae067e 100644 --- a/src/iterator.rs +++ b/src/iterator.rs @@ -1,5 +1,4 @@ use crate::block::{deserialize_block, Block}; -use sled; pub struct BlockchainIterator<'a> { pub current_hash: Vec, diff --git a/src/main.rs b/src/main.rs index e8923de..74d62a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ mod wallet; mod wallets; fn main() { - let mut c = cli::CLI { + let mut c = cli::Cli { cmd: cli::Command::from_args(), }; c.run(); diff --git a/src/pow.rs b/src/pow.rs index 5c4a702..7f0ac99 100644 --- a/src/pow.rs +++ b/src/pow.rs @@ -53,7 +53,7 @@ impl<'a> ProofOfWork<'a> { let data = self.prepare_data(nonce); let result = Sha256::digest(&data); - hash.copy_from_slice(&result.as_slice()); + hash.copy_from_slice(result.as_slice()); hash_int = BigUint::from_bytes_le(&hash); hash.reverse(); diff --git a/src/transaction.rs b/src/transaction.rs index 0d4e7b0..799d543 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -25,8 +25,7 @@ impl Transaction { } pub fn serialize(&self) -> String { - let json_str = serde_json::to_string(self).unwrap(); - json_str + serde_json::to_string(self).unwrap() } fn hash(&self) -> Vec { @@ -121,7 +120,7 @@ impl Transaction { .unwrap(); let sig = secp256k1::Signature::from_compact(&vin.signature).unwrap(); - if !secp.verify(&tx_copy_message, &sig, &pk).is_ok() { + if secp.verify(&tx_copy_message, &sig, &pk).is_err() { return false; } } @@ -132,12 +131,12 @@ impl Transaction { impl fmt::Display for Transaction { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - _ = write!(f, "{}\n", hex::encode(&self.id)); + _ = writeln!(f, "{}", hex::encode(&self.id)); for (i, v) in self.vin.iter().enumerate() { - _ = write!(f, "vin{}>>>{}\n", i, v); + _ = writeln!(f, "vin{}>>>{}", i, v); } for (i, v) in self.vout.iter().enumerate() { - _ = write!(f, "vout{}>>>{}\n", i, v); + _ = writeln!(f, "vout{}>>>{}", i, v); } Ok(()) } diff --git a/src/wallet.rs b/src/wallet.rs index 51e3d51..161935a 100644 --- a/src/wallet.rs +++ b/src/wallet.rs @@ -1,4 +1,3 @@ -use bs58; use rand::Rng; use ripemd160::{Digest as RipemdDigest, Ripemd160}; use secp256k1::{PublicKey, Secp256k1, SecretKey}; @@ -67,7 +66,7 @@ pub fn hash_pub_key(pub_key: &[u8]) -> Vec { pub fn checksum(payload: &[u8]) -> Vec { let first_sha = Sha256::digest(payload); - let second_sha = Sha256::digest(&first_sha.as_slice()); + let second_sha = Sha256::digest(first_sha.as_slice()); second_sha[0..CHECKSUM_LENGTH].to_vec() }