block_chain/blockchain.rs
1//!
2//! This is the Blockchain module.<br>
3//! The Bockchain struct is used to create blockchains.
4//!
5
6use crate::block::Block;
7use crate::wallet::Transaction;
8// use chrono::Utc;
9use serde::{Deserialize, Serialize};
10use serde_json::Value;
11// use uuid::Uuid;
12
13///
14/// Struct Blockchain.<br>
15/// Holds a Vector of type Strut Block.
16///
17#[derive(Serialize, Deserialize)]
18pub struct Blockchain {
19 pub chain: Vec<Block>,
20 pub mempool: Vec<Transaction>, // Transactions waiting to be mined
21}
22
23///
24/// Functions for Struct Blockchain.
25///
26impl Blockchain {
27 ///
28 /// Creates a new Blockchain
29 ///
30 pub fn new() -> Blockchain {
31 Blockchain {
32 chain: Vec::new(),
33 mempool: Vec::new(),
34 }
35 }
36
37 ///
38 /// Function to add a block to the chain.
39 ///
40 pub fn add_block(
41 &mut self,
42 data: Value,
43 miner_address: String,
44 transactions: Vec<Transaction>,
45 ) {
46 let previous_block = self.chain.last().unwrap();
47 let new_block = Block::new(
48 previous_block.block_index + 1,
49 chrono::Utc::now().timestamp() as u64,
50 previous_block.hash.clone(),
51 data,
52 miner_address,
53 transactions,
54 );
55 self.chain.push(new_block);
56 }
57
58 ///
59 /// Function to check if chain is valid.
60 ///
61 pub fn is_chain_valid(&self) -> bool {
62 for i in 1..self.chain.len() {
63 let current_block = &self.chain[i];
64 let previous_block = &self.chain[i - 1];
65
66 if current_block.hash != current_block.calculate_hash() {
67 return false;
68 }
69
70 if current_block.previous_hash != previous_block.hash {
71 return false;
72 }
73 }
74 true
75 }
76
77 ///
78 /// Retrieves the hash of the last block in the chain.
79 ///
80 pub fn get_last_hash(&self) -> Option<&String> {
81 self.chain.last().map(|block| &block.hash)
82 }
83
84 // ///
85 // /// Process a new transaction onto the blockchain.
86 // ///
87 // pub fn process_transaction(
88 // &mut self,
89 // sender_wallet: &mut Wallet,
90 // receiver_wallet: &mut Wallet,
91 // amount: f64,
92 // fee: f64,
93 // miner_address: String,
94 // transactions: Vec<Transaction>,
95 // ) {
96 // if sender_wallet.get_balance() < amount {
97 // println!("Transaction failed: Insufficient balance");
98 // return;
99 // }
100
101 // sender_wallet.update_balance(-amount);
102 // receiver_wallet.update_balance(amount);
103
104 // let transaction = Transaction {
105 // tx_id: Uuid::new_v4().to_string(),
106 // sender: sender_wallet.address.clone(),
107 // receiver: receiver_wallet.address.clone(),
108 // amount,
109 // fee,
110 // timestamp: Utc::now().timestamp() as u64,
111 // };
112
113 // sender_wallet.add_transaction(transaction.clone());
114 // receiver_wallet.add_transaction(transaction.clone());
115
116 // // Convert the transaction to a Value type
117 // let transaction_data =
118 // serde_json::to_value(&transaction).expect("Failed to serialize transaction");
119
120 // self.add_block(transaction_data, miner_address, transactions);
121 // }
122}