-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathprocessor.hpp
309 lines (241 loc) · 8.91 KB
/
processor.hpp
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
#ifndef __PROCESSOR_H__
#define __PROCESSOR_H__
#include <unordered_map>
#include <string>
#include <vector>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cassert>
#include <pthread.h>
#include <queue>
#include <unistd.h>
#include <list>
#include <chrono>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <random>
#include <cmath>
#include <unordered_set>
#include <openssl/sha.h>
#include <string.h>
#include <math.h>
#include "client.hpp"
class Client;
#define NUM_TRANSACTIONS_PER_BLOCK 8
// This is the number of times to try hashing before
// checking for a new block or a new transaction
#define NUM_MAGIC_TO_TRY 1024
// This is the number of leading zeros in hex
#define NUM_LEADING_ZEROS 3
#define PUBLIC_KEY_SIZE 20
/**
transaction
A class for encapsulating all of the informatio needed for a single transaction, or vote.
Many of these transactions will make up a vote.
*/
struct transaction {
size_t size;
std::string sender_public_key;
std::string vote; // Public key of person you are voting for.
double timestamp;
};
/**
This enum will contain the possible values for a block's validity.
*/
enum block_validity_code {
OK,
PREV_BLOCK_NONMATCH,
TRANSACTION_INVALID
};
/**
Block
This a class for blocks of transactions. This class contains all of the important information for a
discovered or working block. In particular, it contains an array of transaction objects. Furthermore,
it contains information about the individual that discovered the block, and block metadata, such as the has
and magic string.
*/
class block {
public:
// The block number. This indicates where in the chain this block is.
unsigned block_number;
// This is the SHA1 hash of the previous block. It allows for the implicit linking
// of blocks to form a blockchain
char* prev_block_SHA1;
// This is the magic string that is appended to the merkle root prior to getting a SHA1 hash for the whole
// block.
unsigned long long magic;
// The merkle root is a hash of the transactions.
char* merkle_root;
// An array of transactions in this block.
transaction* transaction_array[NUM_TRANSACTIONS_PER_BLOCK];
// This is used to determine the number of transactions in the block.
int max_ind;
// This is the public key of the individual that found this block.
char* verifier_public_key;
// This is the SHA1 hash of the block after combining the merkle root with the magic key.
char* finhash;
// A constructor for the blocl.
/*
This instantiates the block and initializes the various attributes
*/
block();
/*
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 calculate_finhash();
/*
Calculate the merkle root of the transactions. This requires hashing pairs of transactions in a binary tree-style format.
*/
unsigned char* calculate_merkle_root();
/*
This is uninplemented.
*/
char* verify_block_number();
};
/**
A template class for a thread-safe (synchronized) queue
This template class defines an API for a queue that is thread safe. This has only the basic
queue functions of pushing and popping, with the pop blocking until an element is added.
If pop_nonblocking() is used, then the queue returns NULL if nothing is in it.
*/
template <class T>
class synchronized_queue {
private:
// The underlying queue data structure
std::queue<T> queue_;
// A mutex lock used to avoid race conditions
pthread_mutex_t lock_;
// A condvar used for blocking when empty.
pthread_cond_t cv_;
public:
// Initialize the queue. In particular, this should initialize the locking code.
void init();
/*
Push an element of type T onto the thread-safe queue.
\param t: an element of type T to be pushed on the queue.
*/
void push(T t);
/*
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.
*/
T pop();
/*
Pop from the front of the queue. Return NULL if empty instead of blocking.
\return item of type T from front of queue.
*/
T pop_nonblocking();
/*
Indicate whether queue is empty.
\return boolean indicating whether queue is empty.
*/
bool empty();
};
// A simple typedef to make notation cleaner elsewhere.
typedef std::list<block*> BlockList;
/*
blockchain
This class contains the blockchain code. In particular, it contains a doubly linked list with stores the actual
blocks of the blockchain. It also contains a variety of helper functions to interface with the blockchain, including
methods to get blocks, add them, verify their existence, and repair the blockchain when a better chain is found from
a peer.
*/
class blockchain {
public:
/*
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 verify_transactions(block* b);
// Unimplemented. Disregard.
bool verify_transactions();
/*
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 check_block_validity(block* b);
/*
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 add_block(block* b);
/*
Get's the block currently at the head of the blockchain.
\return block*, a pointer to hhe head block
*/
block* get_head_block();
// The current length of the blockchain
int chain_length;
/*
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 repair_blockchain(block* b, Client* client);
/*
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 check_if_block_in_chain(block* b);
/*
Remove all of the transactions from block b from the set of votes.
/param b, the block whose transactions are being removed.
*/
void remove_transactions_from_set(block* b);
/*
Add all of the transactions from block b to the set of votes.
/param b, the block whose transactions are being added.
*/
void add_transactions_to_set(block* b);
/*
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 add_transactions_to_queue(block* b);
/*
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(synchronized_queue<transaction*>* q); // Constructor
/*
Return a pointer to a block based on its block number
\param n, the block number
\return block*, the block matching that number
*/
block* get_block(int n);
/*
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* get_block(const char* hash);
private:
// This is the doubly linked list of blocks
BlockList blocks_;
// This is the reference to the shared transaction queue
synchronized_queue<transaction*>* q_ptr_;
public:
// A set containing the public keys of the people that voted.
// We use an unordered set for maximum efficiency.
std::unordered_set<std::string> voted;
};
#endif