summaryrefslogtreecommitdiff
path: root/src/blocks.rs
blob: 1fa848621146de61d944584e2a58bc486584bf3d (plain)
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
use std::{
    hash::{DefaultHasher, Hash, Hasher},
    ops::Deref,
};

use serde::{Deserialize, Serialize};

use crate::{ZERO_PREFIX_AMOUNT, transaction::Transaction};

#[derive(Deserialize, Serialize, Clone, Copy, PartialEq)]
pub struct Block {
    pub prev_hash: u64,
    pub trans: Transaction,
    pub pow: Option<u64>,
}

impl Block {
    fn new(prev_hash: Option<u64>, trans: Transaction) -> Self {
        Self {
            prev_hash: prev_hash.unwrap_or(0), // special blocks
            trans,
            pow: None,
        }
    }

    fn calc_pow(&self) -> u64 {
        for i in 0.. {
            // reset every loop
            let mut hasher = DefaultHasher::new();
            (&self.prev_hash, &self.trans, i).hash(&mut hasher);
            let hashed = hasher.finish();
            if hashed
                .to_string()
                .starts_with(
                    &"0".repeat(ZERO_PREFIX_AMOUNT)
                        .to_string(),
                )
            {
                return hashed;
            }
        }
        unreachable!()
    }

    pub fn verify_pow(&mut self) -> bool {
        let mut hasher = DefaultHasher::new();
        self.hash(&mut hasher);
        hasher
            .finish()
            .to_string()
            .to_string()
            .starts_with(
                &"0".repeat(ZERO_PREFIX_AMOUNT)
                    .to_string(),
            )
    }

    pub fn calc_set_pow(&mut self) {
        self.pow = Some(self.calc_pow());
    }
}

impl Hash for Block {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.prev_hash
            .hash(state);
        self.trans
            .hash(state);
        self.pow
            .expect("Cannot hash whole block with no PoW")
            .hash(state);
    }
}

#[derive(Default, Deserialize, Serialize, Clone, PartialEq)]
pub struct BlockChain {
    pub blocks: Vec<Block>,
}

impl BlockChain {
    pub fn new(blocks: Vec<Block>) -> Self {
        Self {
            blocks,
        }
    }

    pub fn new_block(&self, trans: Transaction) -> Block {
        let mut hasher = DefaultHasher::new();
        Block::new(
            self.blocks
                .last()
                .map(|b| {
                    b.hash(&mut hasher);
                    hasher.finish()
                }),
            trans,
        )
    }

    pub fn store(&mut self, block: Block) {
        // TODO: verify blocks
        self.blocks
            .push(block)
    }
}

impl Deref for BlockChain {
    type Target = Vec<Block>;
    fn deref(&self) -> &Self::Target {
        &self.blocks
    }
}