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
extern crate chrono;
extern crate serde;
extern crate serde_json;
extern crate sha2;

use chrono::Utc;
use serde_derive::Serialize;
use sha2::{ Sha256, Digest };
use std::fmt::Write;

/// An exchange of assets between two parties
#[derive(Debug, Clone, Serialize)]
pub struct Transaction {
    /// A transaction hash
    hash: String,

    /// A transaction sender address
    from: String,

    /// A transaction receiver address
    to: String,

    /// A transaction amount
    amount: f32,

    /// A transaction timestamp
    timestamp: i64,
}

/// A identifier of a particular block on an entire blockchain
#[derive(Debug, Serialize)]
pub struct BlockHeader {
    /// A timestamp at which a block was mined
    timestamp: i64,

    /// An integer to achieve the network's difficulty
    nonce: u32,

    /// A hash of a previous block
    previous_hash: String,

    /// A Merkel root hash
    merkle: String,

    /// A current difficulty level of the network
    difficulty: u32,
}

/// A data storage in a blockchain
#[derive(Debug, Serialize)]
pub struct Block {
    /// Information about the block and the miner
    header: BlockHeader,

    /// A total amount of transactions
    count: u32,

    /// An amount of transactions
    transactions: Vec<Transaction>,
}

/// A blockchain
#[derive(Debug, Serialize)]
pub struct Chain {
    /// A chain of blocks
    chain: Vec<Block>,

    /// A list of transactions
    current_transactions: Vec<Transaction>,

    /// A current difficulty level of the network
    difficulty: u32,

    /// A blockchain genesis address
    address: String,

    /// A block reward
    reward: f32,
}

impl Chain {
    pub fn new(address: String, difficulty: u32, reward: f32) -> Chain {
        let mut chain = Chain {
            reward,
            address,
            difficulty,
            chain: Vec::new(),
            current_transactions: Vec::new(),
        };

        chain.generate_new_block();

        chain
    }

    pub fn get_transactions(&mut self) -> &Vec<Transaction> {
        &self.current_transactions
    }

    pub fn get_transaction(&self, hash: String) -> Option<&Transaction> {
        self.current_transactions.iter().find(|&trx| trx.hash == hash)
    }

    pub fn add_transaction(&mut self, from: String, to: String, amount: f32) -> bool {
        let timestamp = Utc::now().timestamp();
        let hash = Chain::hash(&(&from, &to, amount, timestamp));

        self.current_transactions.push(Transaction {
            to,
            from,
            hash,
            amount,
            timestamp: Utc::now().timestamp(),
        });

        true
    }

    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)
    }

    pub fn update_difficulty(&mut self, difficulty: u32) -> bool {
        self.difficulty = difficulty;

        true
    }

    pub fn update_reward(&mut self, reward: f32) -> bool {
        self.reward = reward;

        true
    }

    pub fn generate_new_block(&mut self) -> bool {
        let header = BlockHeader {
            timestamp: Utc::now().timestamp(),
            nonce: 0,
            previous_hash: self.get_last_hash(),
            merkle: String::new(),
            difficulty: self.difficulty,
        };

        let timestamp = Utc::now().timestamp();
        let to = self.address.clone();
        let from = String::from("Root");
        let hash = Chain::hash(&(&from, &to, self.reward, timestamp));

        let reward_trans = Transaction {
            to,
            from,
            hash,
            timestamp,
            amount: self.reward,
        };

        let mut block = Block {
            header,
            count: 0,
            transactions: vec![],
        };

        block.transactions.push(reward_trans);
        block.transactions.append(&mut self.current_transactions);

        block.count = block.transactions.len() as u32;
        block.header.merkle = Chain::get_merkle(block.transactions.clone());

        Chain::proof_of_work(&mut block.header);

        self.chain.push(block);

        true
    }

    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 mut h2 = merkle.remove(0);

            h1.push_str(&mut h2);

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

    pub fn proof_of_work(header: &mut BlockHeader) {
        loop {
            let hash = Chain::hash(header);
            let slice = &hash[..header.difficulty as usize];

            match slice.parse::<u32>() {
                Ok(val) => {
                    if val != 0 {
                        header.nonce += 1;
                    } else {
                        break;
                    }
                }
                Err(_) => {
                    header.nonce += 1;

                    continue;
                }
            };
        }
    }

    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
    }
}

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

    fn setup() -> Chain {
        Chain::new("Address".to_string(), 1, 100.0)
    }

    #[test]
    fn test_add_transaction() {
        let mut chain = setup();

        let result = chain.add_transaction("Sender".to_string(), "Receiver".to_string(), 10.0);

        assert_eq!(result, true);
        assert_eq!(chain.current_transactions.len(), 1);
    }

    #[test]
    fn test_get_transaction_found() {
        let mut chain = setup();

        chain.add_transaction("Sender".to_string(), "Receiver".to_string(), 10.0);

        let transaction = chain.get_transaction(chain.current_transactions[0].hash.clone());

        assert!(transaction.is_some());
        assert_eq!(transaction.unwrap().from, "Sender");
        assert_eq!(transaction.unwrap().to, "Receiver");
    }

    #[test]
    fn test_get_transaction_not_found() {
        let chain = setup();

        let transaction = chain.get_transaction("NonExistentHash".to_string());

        assert!(transaction.is_none());
    }

    #[test]
    fn test_get_transactions() {
        let mut chain = setup();
        chain.add_transaction("Sender1".to_string(), "Receiver1".to_string(), 10.0);
        chain.add_transaction("Sender2".to_string(), "Receiver2".to_string(), 20.0);

        let transactions = chain.get_transactions();

        assert_eq!(transactions.len(), 2);
        assert_eq!(transactions[0].from, "Sender1");
        assert_eq!(transactions[1].from, "Sender2");
    }

    #[test]
    fn test_get_transactions_not_found() {
        let mut chain = setup();

        let transactions = chain.get_transactions();

        assert_eq!(transactions.len(), 0);
    }

    #[test]
    fn test_get_last_hash() {
        let chain = setup();
        let hash = chain.get_last_hash();

        assert!(!hash.is_empty());
    }

    #[test]
    fn test_update_difficulty() {
        let mut chain = setup();

        let result = chain.update_difficulty(4);

        assert_eq!(result, true);
        assert_eq!(chain.difficulty, 4);
    }

    #[test]
    fn test_update_reward() {
        let mut chain = setup();

        let result = chain.update_reward(50.0);

        assert_eq!(result, true);
        assert_eq!(chain.reward, 50.0);
    }

    #[test]
    fn test_generate_new_block() {
        let mut chain = setup();

        let result = chain.generate_new_block();

        assert_eq!(result, true);
        assert_eq!(chain.chain.len(), 2);
    }
}