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
extern crate serde;
extern crate serde_json;
use std::collections::HashMap;
#[allow(unused_imports)]
use std::fs::{ OpenOptions, File };
use std::io::prelude::*;
use std::io::Error;
use std::string::String;
use block::Block;
use hash_util::*;
use std::*;
#[allow(dead_code)]
#[derive( Serialize, Deserialize, Debug )]
pub struct Chain
{
uid: String,
chain: HashMap< String, Block >,
tail_hash: String,
}
impl Chain
{
#[allow(dead_code)]
pub fn new() -> Chain
{
let mut chain = Chain
{
uid: empty_hash(),
chain: HashMap::new(),
tail_hash: empty_hash(),
};
chain.chain.insert( String::from("0"), Block::origin() );
return chain;
}
#[allow(dead_code)]
pub fn push( &mut self, mut block: Block )
{
let key1 = self.tail_hash.clone();
let key2 = self.tail_hash.clone();
block.set_previous_hash( &self.tail_hash );
self.chain.insert( key1, block );
self.tail_hash = self.chain.get( &key2 ).unwrap().hash().clone();
}
#[allow(dead_code)]
pub fn uid( &self ) -> &String
{
&self.uid
}
#[allow(dead_code)]
pub fn set_uid( &mut self, url: String )
{
self.uid = create_leaf_hash( &url );
}
pub fn len( &self ) -> usize
{
return self.chain.len();
}
#[allow(dead_code)]
pub fn origin( &self ) -> &Block
{
&self.chain.get( "0" ).expect("There is no origin block in this chain.")
}
#[allow(dead_code)]
pub fn tail_hash( &self ) -> &String
{
&self.tail_hash
}
#[allow(dead_code)]
pub fn contains( &self, hash: &String ) -> bool
{
let mut hash_check = String::from("0");
let null_block = Block::new( 0, String::from("-1") );
#[allow(unused_assignments)]
let mut current_block = null_block.clone();
while hash_check != self.tail_hash
{
current_block = self.chain.get( &hash_check ).unwrap_or( &null_block.clone() ).clone();
if *current_block.hash() == *hash
{
return true;
}
hash_check = current_block.hash().clone();
}
return false;
}
#[allow(dead_code)]
pub fn print_chain( &self ) -> Result< (), Error >
{
let json_chain = serde_json::to_string( &self )?;
println!( "{}", json_chain );
Ok( () )
}
#[allow(dead_code)]
pub fn write_to( &self, filename: &str ) -> Result< (), Error >
{
let mut file = OpenOptions::new( ).write( true ).create( true ).open( filename )?;
#[allow(unused_variables)]
let temp = file.write_all( serde_json::to_string( &self )?.as_ref() );
Ok( () )
}
#[allow(dead_code)]
pub fn read_json( filename: &str ) -> Result< String, Error >
{
let mut file = OpenOptions::new( ).read( true ).open( filename )?;
let mut json = String::new();
#[allow(unused_variables)]
let temp = file.read_to_string( &mut json );
Ok( json )
}
#[allow(dead_code)]
pub fn read_and_construct( filename: &str ) -> Result< Chain, Error >
{
let string = Chain::read_json( filename ).expect( "Failed to read in the JSON" );
let chain : Chain = serde_json::from_str( string.as_ref() ).expect( "Failed to convert the JSON to chain" );
Ok( chain )
}
}