Skip to content

Commit

Permalink
fix:cargo clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
Nickqiaoo committed Nov 5, 2023
1 parent f374a14 commit 9110ed7
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 31 deletions.
3 changes: 1 addition & 2 deletions src/block.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -42,7 +41,7 @@ impl Block {

pub fn serialize(&self) -> Vec<u8> {
let json_str = serde_json::to_string(self).unwrap();
return json_str.into_bytes();
json_str.into_bytes()
}

pub fn hash_transactions(&self) -> Vec<u8> {
Expand Down
1 change: 0 additions & 1 deletion src/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
30 changes: 13 additions & 17 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down Expand Up @@ -42,15 +42,15 @@ pub enum Command {
},
}

impl CLI {
impl Cli {
pub fn run(&mut self) {
match &self.cmd {
Command::CreateBlockChain { address } => self.create_blockchain(address.to_string()),
Command::Createwallet => self.create_wallet(),
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()),
}
Expand Down Expand Up @@ -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!();
}
}

Expand All @@ -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!");
}
Expand Down
1 change: 0 additions & 1 deletion src/iterator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::block::{deserialize_block, Block};
use sled;

pub struct BlockchainIterator<'a> {
pub current_hash: Vec<u8>,
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/pow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
11 changes: 5 additions & 6 deletions src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> {
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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(())
}
Expand Down
3 changes: 1 addition & 2 deletions src/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use bs58;
use rand::Rng;
use ripemd160::{Digest as RipemdDigest, Ripemd160};
use secp256k1::{PublicKey, Secp256k1, SecretKey};
Expand Down Expand Up @@ -67,7 +66,7 @@ pub fn hash_pub_key(pub_key: &[u8]) -> Vec<u8> {
pub fn checksum(payload: &[u8]) -> Vec<u8> {
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()
}

0 comments on commit 9110ed7

Please sign in to comment.