blockchain/
chain.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
use std::{collections::HashMap, fmt::Write, iter};

use rand::Rng;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use twox_hash::xxhash64::RandomState;

use crate::{Block, Transaction, Wallet};

/// Blockchain.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Chain {
    /// Chain of blocks.
    pub chain: Vec<Block>,

    /// List of transactions.
    pub current_transactions: Vec<Transaction>,

    /// Current difficulty level of the network.
    pub difficulty: f64,

    /// Blockchain genesis address.
    pub address: String,

    /// Block reward.
    pub reward: f64,

    /// Transaction fee.
    pub fee: f64,

    /// Map to associate wallets with their corresponding addresses and balances.
    pub wallets: HashMap<String, Wallet, RandomState>,
}

impl Chain {
    /// Initialize a new blockchain with the specified parameters.
    ///
    /// # Arguments
    /// - `difficulty`: The initial mining difficulty level of the network.
    /// - `reward`: The initial block reward for miners.
    /// - `fee`: The transaction fee.
    ///
    /// # Returns
    /// New `Chain` instance with the given parameters and a genesis block.
    pub fn new(difficulty: f64, reward: f64, fee: f64) -> Self {
        let mut chain = Chain {
            fee,
            reward,
            difficulty,
            chain: vec![],
            wallets: HashMap::default(),
            current_transactions: vec![],
            address: Chain::generate_address(42),
        };

        chain.generate_new_block();

        chain
    }

    /// Get a list of current transactions in the blockchain.
    ///
    /// # Arguments
    /// - `page`: The page number.
    /// - `size`: The number of transactions per page.
    ///
    /// # Returns
    /// Vector containing the current transactions for the specified page.
    pub fn get_transactions(&self, page: usize, size: usize) -> Vec<Transaction> {
        // Calculate the total number of pages
        let total_pages = self.current_transactions.len().div_ceil(size);

        // Return an empty vector if the page is greater than the total number of pages
        if page > total_pages {
            return Vec::new();
        }

        // Calculate the start and end indices for the transactions of the current page
        let start = page.saturating_sub(1) * size;
        let end = start + size;

        // Get the transactions for the current page
        self.current_transactions[start..end.min(self.current_transactions.len())].to_vec()
    }

    /// Get a transaction by its hash.
    ///
    /// # Arguments
    /// - `hash`: The hash of the transaction to retrieve.
    ///
    /// # Returns
    /// An option containing a reference to the transaction if found, or `None` if not found.
    pub fn get_transaction(&self, hash: String) -> Option<&Transaction> {
        self.current_transactions
            .iter()
            .find(|&trx| trx.hash == hash)
    }

    /// Add a new transaction to the blockchain.
    ///
    /// # Arguments
    /// - `from`: The sender's address.
    /// - `to`: The receiver's address.
    /// - `amount`: The amount of the transaction.
    ///
    /// # Returns
    /// `true` if the transaction is successfully added to the current transactions.
    pub fn add_transaction(&mut self, from: String, to: String, amount: f64) -> bool {
        let total = amount * self.fee;

        // Validate the transaction and create a new transaction if it is valid
        let transaction = match self.validate_transaction(&from, &to, total) {
            true => Transaction::new(from.to_owned(), to.to_owned(), self.fee, total),
            false => return false,
        };

        // Update sender's balance
        match self.wallets.get_mut(&from) {
            Some(wallet) => {
                wallet.balance -= total;

                // Add the transaction to the sender's transaction history
                wallet.transactions.push(transaction.hash.to_owned());
            }
            None => return false,
        };

        // Update receiver's balance
        match self.wallets.get_mut(&to) {
            Some(wallet) => {
                wallet.balance += amount;

                // Add the transaction to the receiver's transaction history
                wallet.transactions.push(transaction.hash.to_owned());
            }
            None => return false,
        };

        // Add the transaction to the current transactions
        self.current_transactions.push(transaction);

        true
    }

    /// Validate a transaction.
    ///
    /// # Arguments
    /// - `from`: The sender's address.
    /// - `to`: The receiver's address.
    /// - `amount`: The amount of the transaction.
    ///
    /// # Returns
    /// `true` if the transaction is valid, `false` otherwise.
    pub fn validate_transaction(&self, from: &str, to: &str, amount: f64) -> bool {
        // Validate if the sender is not the root
        if from == "Root" {
            return false;
        }

        // Validate that sender and receiver addresses are different
        if from == to {
            return false;
        }

        // Validate if the amount is non-negative
        if amount <= 0.0 {
            return false;
        }

        // Validate if sender and receiver addresses are valid
        let sender = match self.wallets.get(from) {
            Some(wallet) => wallet,
            None => return false,
        };

        // Validate if the receiver address is valid
        if !self.wallets.contains_key(to) {
            return false;
        }

        // Validate if sender can send the amount of the transaction
        if sender.balance < amount {
            return false;
        }

        true
    }

    /// Create a new wallet with a unique email and an initial balance.
    ///
    /// # Arguments
    /// - `email`: The unique user email.
    ///
    /// # Returns
    /// The newly created wallet address.
    pub fn create_wallet(&mut self, email: String) -> String {
        let address = Chain::generate_address(42);

        let wallet = Wallet::new(email, address.to_owned(), 0.0);

        self.wallets.insert(address.to_string(), wallet);

        address
    }

    /// Get a wallet's balance based on its address.
    ///
    /// # Arguments
    /// - `address`: The unique wallet address.
    ///
    /// # Returns
    /// The wallet balance.
    pub fn get_wallet_balance(&self, address: String) -> Option<f64> {
        self.wallets.get(&address).map(|wallet| wallet.balance)
    }

    /// Get a wallet's transaction history based on its address.
    ///
    /// # Arguments
    /// - `address`: The unique wallet address.
    /// - `page`: The page number.
    /// - `size`: The number of transactions per page.
    ///
    /// # Returns
    /// The wallet transaction history for the specified page.
    pub fn get_wallet_transactions(
        &self,
        address: String,
        page: usize,
        size: usize,
    ) -> Option<Vec<Transaction>> {
        match self
            .wallets
            .get(&address)
            .map(|wallet| wallet.transactions.to_owned())
        {
            // Get the transaction history of the wallet
            Some(txs) => {
                let mut result = Vec::new();

                // Calculate the total number of pages
                let total_pages = self.current_transactions.len().div_ceil(size);

                // Return an empty vector if the page is greater than the total number of pages
                if page > total_pages {
                    return Some(result);
                }

                // Calculate the start and end indices for the transactions of the current page
                let start = page.saturating_sub(1) * size;
                let end = start + size;

                for tx in txs[start..end.min(txs.len())].iter() {
                    match self.get_transaction(tx.to_string()) {
                        Some(transaction) => result.push(transaction.to_owned()),
                        None => continue,
                    }
                }

                Some(result)
            }
            // Return None if the wallet is not found
            None => None,
        }
    }

    /// Get the hash of the last block in the blockchain.
    ///
    /// # Returns
    /// The hash of the last block in the blockchain as a string.
    pub fn get_last_hash(&self) -> String {
        let block = match self.chain.last() {
            Some(block) => block,
            None => {
                return String::from_utf8(vec![48; 64]).unwrap();
            }
        };

        Chain::hash(&block.header)
    }

    /// Update the mining difficulty of the blockchain.
    ///
    /// # Arguments
    /// - `difficulty`: The new mining difficulty level.
    ///
    /// # Returns
    /// `true` if the difficulty is successfully updated.
    pub fn update_difficulty(&mut self, difficulty: f64) -> bool {
        self.difficulty = difficulty;

        true
    }

    /// Update the block reward.
    ///
    /// # Arguments
    /// - `reward`: The new block reward value.
    ///
    /// # Returns
    /// `true` if the reward is successfully updated.
    pub fn update_reward(&mut self, reward: f64) -> bool {
        self.reward = reward;

        true
    }

    /// Update the transaction fee.
    ///
    /// # Arguments
    /// - `fee`: The new transaction fee value.
    ///
    /// # Returns
    /// `true` if the transaction fee is successfully updated.
    pub fn update_fee(&mut self, fee: f64) -> bool {
        self.fee = fee;

        true
    }

    /// Generate a new block and append it to the blockchain.
    ///
    /// # Returns
    /// `true` if a new block is successfully generated and added to the blockchain.
    pub fn generate_new_block(&mut self) -> bool {
        // Create a new block
        let mut block = Block::new(self.get_last_hash(), self.difficulty);

        // Create a reward transaction
        let transaction = Transaction::new(
            "Root".to_string(),
            self.address.to_string(),
            self.fee,
            self.reward,
        );

        // Add the reward transaction to the block
        block.transactions.push(transaction);
        block.transactions.append(&mut self.current_transactions);

        // Update the block count and the Merkle root hash
        block.count = block.transactions.len();
        block.header.merkle = Chain::get_merkle(block.transactions.clone());

        // Perform the proof-of-work process
        Block::proof_of_work(&mut block.header);

        // Add the block to the blockchain
        self.chain.push(block);

        true
    }

    /// Calculate the Merkle root hash for a list of transactions.
    ///
    /// # Arguments
    /// - `transactions`: A vector of transactions for which the Merkle root hash is calculated.
    ///
    /// # Returns
    /// The Merkle root hash as a string.
    pub fn get_merkle(transactions: Vec<Transaction>) -> String {
        let mut merkle = Vec::new();

        for t in &transactions {
            let hash = Chain::hash(t);
            merkle.push(hash);
        }

        if merkle.len() % 2 == 1 {
            let last = merkle.last().cloned().unwrap();
            merkle.push(last);
        }

        while merkle.len() > 1 {
            let mut h1 = merkle.remove(0);
            let h2 = merkle.remove(0);

            h1.push_str(&h2);

            let nh = Chain::hash(&h1);
            merkle.push(nh);
        }

        merkle.pop().unwrap()
    }

    /// Calculate the SHA-256 hash of a serializable item.
    ///
    /// # Arguments
    /// - `item`: A serializable item to be hashed.
    ///
    /// # Returns
    /// The SHA-256 hash of the item as a string.
    pub fn hash<T: serde::Serialize>(item: &T) -> String {
        let input = serde_json::to_string(&item).unwrap();
        let mut hasher = Sha256::new();
        hasher.update(input.as_bytes());
        let res = hasher.finalize();
        let vec_res = res.to_vec();

        let mut result = String::new();

        for b in vec_res.as_slice() {
            write!(&mut result, "{:x}", b).expect("Unable to write");
        }

        result
    }

    /// Generates a random alphanumeric string of a specified length.
    ///
    /// # Arguments
    /// - `length`: The length of the generated string.
    ///
    /// # Returns
    /// A `String` containing the generated alphanumeric string.
    fn generate_address(length: usize) -> String {
        let mut rng = rand::thread_rng();

        let address: String = iter::repeat(())
            .map(|()| rng.sample(rand::distributions::Alphanumeric) as char)
            .take(length)
            .collect();

        address
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_generate_address() {
        let result = Chain::generate_address(42);

        assert_eq!(result.len(), 42);
    }
}