-
Notifications
You must be signed in to change notification settings - Fork 8
/
processor.cpp
445 lines (376 loc) · 12.2 KB
/
processor.cpp
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
440
441
442
443
444
445
#include "processor.hpp"
using namespace std;
using namespace std::chrono;
////////////////////////////////
///// block implementation /////
////////////////////////////////
// A constructor for the blocl.
/*
This instantiates the block and initializes the various attributes
*/
block::block() {
prev_block_SHA1 = NULL;
magic = 0;
merkle_root = NULL;
max_ind = 0;
verifier_public_key = "a";
finhash = NULL;
}
/*
Calculate the merkle root of the transactions.
This requires hashing pairs of transactions in a binary tree-style format.
\return: the output hash
*/
unsigned char* block::calculate_merkle_root() {
vector<unsigned char*> old_hashes;
vector<unsigned char*> new_hashes;
void *naked = (void *) transaction_array;
for (int i = 0; i < NUM_TRANSACTIONS_PER_BLOCK; ++i) {
unsigned char* hash1 = new unsigned char[SHA_DIGEST_LENGTH];
unsigned char* data_to_hash =
(unsigned char*) transaction_array[0]->sender_public_key;
SHA1(data_to_hash, PUBLIC_KEY_SIZE, hash1);
old_hashes.push_back(hash1);
}
// Until we reach root, construct each level in turn.
while (1) {
unsigned char* buffer = new unsigned char[SHA_DIGEST_LENGTH * 2];
for (int i = 0; i < old_hashes.size(); i += 2) {
unsigned char* hash2 = new unsigned char[SHA_DIGEST_LENGTH];
// Concatenate leaves into a single buffer and SHA-1 it.
memcpy(buffer, old_hashes[i], SHA_DIGEST_LENGTH);
memcpy(buffer + SHA_DIGEST_LENGTH, old_hashes[i+1], SHA_DIGEST_LENGTH);
SHA1(buffer, 2 * SHA_DIGEST_LENGTH, hash2);
new_hashes.push_back(hash2);
}
if (new_hashes.size() == 1) {
break;
}
old_hashes = new_hashes;
new_hashes.clear();
}
if (new_hashes[0] == NULL)
assert(false);
merkle_root = (char*) new_hashes[0];
return new_hashes[0];
}
// Unimplemented
char* block::verify_block_number() {
// Asks neighbors for the previous block, and checks to make sure its
// block_number is one less than this one's.
return NULL;
}
/*
Calculates the overall SHA1 hash for the block by appending the merkle root to he
magic string and taking the SHA1 hash of the result.
*/
void block::calculate_finhash() {
unsigned char* hash = new unsigned char[SHA_DIGEST_LENGTH];
const unsigned char* data_to_hash = this->calculate_merkle_root();
assert(data_to_hash[0] != 0);
unsigned char* buffer =
new unsigned char[SHA_DIGEST_LENGTH + sizeof(unsigned long long)];
memcpy(buffer, data_to_hash, SHA_DIGEST_LENGTH);
memcpy(buffer + SHA_DIGEST_LENGTH, &this->magic, sizeof(unsigned long long));
SHA1(buffer, SHA_DIGEST_LENGTH + sizeof(unsigned long long), hash);
if (hash == NULL)
assert(false);
finhash = (char*)hash;
}
/////////////////////////////////////
///// blockchain implementation /////
/////////////////////////////////////
bool same_hash(const char* a, const char* b) {
for(int i = 0; i < PUBLIC_KEY_SIZE; ++i) {
if (a[i] != b[i]) {
return false;
}
}
return true;
}
/*
A constructor.
\param q, takes a pointer to the thread-safe queue of transactions. This is needed so that in the case
of a block chain repair, transactions can be readded to the queue.
*/
blockchain::blockchain(synchronized_queue<transaction*>* q) {
block* b = new block;
b->block_number = 1;
b->finhash = new char[15];
std::string s = std::string("STARTINGBLOCK");
s.copy(b->finhash, s.length(), 0);
chain_length = 1;
blocks_.push_front(b);
q_ptr_ = q;
}
/*
Iterate over the transactions in the block and make sure none were already in the blockchain.
\param b, the block those transaction are being verified.
\return boolean, indicating whether the block's transactions are all new.
*/
bool blockchain::verify_transactions(block* b) {
// Iterate over all transactions in the block.
for (int i = 0; i < NUM_TRANSACTIONS_PER_BLOCK; ++i) {
// If you ever find one, return false.
if (voted.find(b->transaction_array[i]->sender_public_key)
!= voted.end()) {
return false;
}
}
return true;
}
/*
Unimplemented.
*/
bool blockchain::verify_transactions() {
return true;
}
/*
Return a pointer to a block based on its block number
\param n, the block number
\return block*, the block matching that number
*/
block* blockchain::get_block(int n) {
for (auto it = blocks_.begin(); it != blocks_.end(); ++it) {
if ((*it)->block_number == n) {
return *it;
}
}
return NULL;
}
/*
Return a pointer to a block based on its hash
Assumptions: A block can be found by just comparing the hashes for equality.
This assumptions that two distinct blocks do not share a hash. Based on research about
blockchain and its encryption algorithms, this is a reasonable assumption.
\param n, the hash
\return block*, the block matching that hash
*/
block* blockchain::get_block(const char* hash) {
std::cout << "About to look for block with hash " << hash << std::endl;
for (auto it = blocks_.begin(); it != blocks_.end(); ++it) {
if (same_hash((*it)->finhash, hash)) {
std::cout << "Found block with hash " << hash << std::endl;
return *it;
}
}
std::cout << "Did noneighbort find block with hash " << hash << std::endl;
return NULL;
}
/*
Searches for block b in the chain, and returns the iterator to it if so.
Assumptions: A block can be found by just comparing the hashes for equality.
This assumptions that two distinct blocks do not share a hash. Based on research about
blockchain and its encryption algorithms, this is a reasonable assumption.
\param b, the block being searched.
\return BlockList::iterator to the block. Will return BlockList::end() if not found
*/
BlockList::iterator blockchain::check_if_block_in_chain(block* b) {
// Iterate over the blocks, and check if any have a matching final hash.
for (auto it = blocks_.begin(); it != blocks_.end(); ++it) {
if (same_hash((*it)->finhash, b->finhash)) {
return it;
}
}
return blocks_.end();
}
/*
Makes a request to all peers to looking
*/
block* get_parent_block_from_neighbor(block* b, Client* client) {
assert(b);
assert(b->prev_block_SHA1[0] != 0);
assert(b->finhash[0] != 0);
std::cout << "About to get block " << b->prev_block_SHA1 << "from block " << b->finhash << std::endl;
block* block = client->getBlock(b->prev_block_SHA1);
if (!block) {
// TODO change this to just return null;
assert(false);
}
return block;
}
/*
Remove all of the transactions from block b from the set of votes.
\param b, the block whose transactions are being removed.
*/
void blockchain::remove_transactions_from_set(block* b) {
for (int i = 0; i < NUM_TRANSACTIONS_PER_BLOCK; ++i) {
voted.erase(b->transaction_array[i]->sender_public_key);
}
return;
}
/*
Add all of the transactions from block b to the set of votes.
\param b, the block whose transactions are being added.
*/
void blockchain::add_transactions_to_set(block* b) {
for (int i = 0; i < NUM_TRANSACTIONS_PER_BLOCK; ++i) {
voted.insert(b->transaction_array[i]->sender_public_key);
}
return;
}
/*
Add all of the transactions from block b from the queue of transactinos to process.
\param b, the block whose transactions are being added.
*/
void blockchain::add_transactions_to_queue(block* b) {
for (int i = 0; i < b->max_ind; ++i) {
q_ptr_->push(b->transaction_array[i]);
}
}
/*
Given a new block b, repairs the blockchain to match the history of b.
\param b, the block that is being added, and whose history determines the repair
\return void
*/
bool blockchain::repair_blockchain(block* b, Client* client) {
// This list will contain the blocks that need to be added to blockchain.
std::list<block*> blocks_to_add;
//std::list<block*> blocks_removed;
// Get all of the blocks that need to be added to chain
block* current_block = b;
BlockList::iterator it;
while(true) {
std::cout << "About to check in chain" << std::endl;
it = check_if_block_in_chain(current_block);
std::cout << "Done checking in chain" << std::endl;
if (it != blocks_.end())
break;
blocks_to_add.push_back(current_block);
std::cout << "About to get parent block from neighbor" << std::endl;
block* current_block = get_parent_block_from_neighbor(current_block, client);
std::cout << "Got parent block from neighbor" << std::endl;
// If you are ever not able to get a parent block, give up on the repair.
if (!current_block) {
return false;
}
}
// Remove all of the necessary blocks from current chain.
// Remove their transactions from the set, and readd them
// to the transactions queue.
while(blocks_.begin() != it) {
block* first_block = blocks_.front();
remove_transactions_from_set(first_block);
add_transactions_to_queue(first_block);
blocks_.pop_front();
}
// Add the transactions for the new blocks to the set.
for (const auto& block: blocks_to_add) {
add_transactions_to_set(block);
}
// Add the actual blocks to the chain.
// This will add the blocks_to_add to the front of blocks_.
blocks_.splice(blocks_.begin(), blocks_to_add);
return true;
}
/*
Checks whether the incoming block is valid, and returns the appopriate validity code.
This will indicate whether the block is OK to be added to the chain, has invalid transactions,
or if its previous block doesn't match the head of the current one, meaning a blockchain
repair may be needed.
\param b, the block being considered
\return block_validity_code enum element indicating the result.
*/
block_validity_code blockchain::check_block_validity(block* b) {
// Make sure none of the votes had previously been made.
// Make sure the new block has the correct prev block.
block* head_block = get_head_block();
if (!same_hash(b->prev_block_SHA1, head_block->finhash))
return PREV_BLOCK_NONMATCH;
bool transaction_verification = verify_transactions(b);
if (!transaction_verification)
return TRANSACTION_INVALID;
// If everything checks out, return 1.
return OK;
}
/*
Add a block to the head of a blockchain.
Assumptions: This function assumes that the incoming block is OK to add to the head. It does not
run any additional checks.
\param b, the block being added
\return boolean, indicating success/failure
*/
bool blockchain::add_block(block* b) {
// Add block to the blockchain
chain_length++;
blocks_.push_front(b);
add_transactions_to_set(b);
b->block_number = chain_length;
return true;
}
/*
Get's the block currently at the head of the blockchain.
\return block*, a pointer to hhe head block
*/
block* blockchain::get_head_block(){
// Get the head block. Don't remove it from the chain.
if (chain_length == 0) {
assert(false);
return NULL;
}
return blocks_.front();
}
/////////////////////////////////////////////
///// Synchronized Queue Implementation /////
/////////////////////////////////////////////
// Initialize the queue. In particular, this should initialize the locking code.
template <class T>
void synchronized_queue<T>::init() {
pthread_mutex_init(&lock_, NULL);
pthread_cond_init(&cv_, NULL);
}
/*
Push an element of type T onto the thread-safe queue.
\param t: an element of type T to be pushed on the queue.
*/
template <class T>
void synchronized_queue<T>::push(T t) {
pthread_mutex_lock(&lock_);
queue_.push(t);
pthread_cond_signal(&cv_);
pthread_mutex_unlock(&lock_);
}
/*
Pop from the front of the queue. Block if queue is empty until an item comes in.
\return item of type T from front of queue.
*/
template <class T>
T synchronized_queue<T>::pop() {
T ret;
pthread_mutex_lock(&lock_);
while (queue_.size() == 0) {
pthread_cond_wait(&cv_, &lock_);
}
ret = queue_.back();
queue_.pop();
pthread_mutex_unlock(&lock_);
return ret;
}
/*
Pop from the front of the queue. Return NULL if empty instead of blocking.
\return item of type T from front of queue.
*/
template <class T>
T synchronized_queue<T>::pop_nonblocking() {
T ret;
pthread_mutex_lock(&lock_);
if (queue_.size() == 0) {
pthread_mutex_unlock(&lock_);
return NULL;
}
ret = queue_.back();
queue_.pop();
pthread_mutex_unlock(&lock_);
return ret;
}
/*
Indicate whether queue is empty.
\return boolean indicating whether queue is empty.
*/
template <class T>
bool synchronized_queue<T>::empty() {
return queue_.size() == 0;
}
template class synchronized_queue<transaction*>;
template class synchronized_queue<block*>;
template class synchronized_queue<std::string*>;