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
#[allow(dead_code)]
extern crate chrono;
extern crate serde;
extern crate serde_json;
use self::chrono::Utc;
use std::string::String;
use hash_util::*;
#[allow(unused_imports)]
use self::serde_json::Error;
#[derive(Clone, Serialize, Deserialize)]
pub struct Block
{
pub index: u64,
pub previous_hash: String,
pub timestamp: String,
pub merkle_root: String,
pub hash: String,
}
impl Block
{
#[allow(dead_code)]
pub fn new( index: u64, merkle_root: String ) -> Block
{
let mut block = Block{
index: index,
previous_hash: empty_hash(),
timestamp: Utc::now().to_string(),
merkle_root: merkle_root,
hash: empty_hash(),
};
block.hash = generate_header_hash( &block );
return block;
}
#[allow(dead_code)]
pub fn origin() -> Block
{
let mut block : Block = Block::new( 0, empty_hash() );
block.hash = empty_hash();
return block;
}
#[allow(dead_code)]
pub fn index( &self ) -> &u64
{
&self.index
}
#[allow(dead_code)]
pub fn timestamp( &self ) -> &String
{
&self.timestamp
}
#[allow(dead_code)]
pub fn previous_hash( &self ) -> &String
{
&self.previous_hash
}
#[allow(dead_code)]
pub fn merkle_root( &self ) -> &String
{
&self.merkle_root
}
#[allow(dead_code)]
pub fn hash( &self ) -> &String
{
&self.hash
}
#[allow(dead_code)]
pub fn set_previous_hash( &mut self, hash: &String )
{
self.previous_hash = hash.clone();
self.hash = generate_header_hash( &self );
}
}