-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblockchain.c
More file actions
209 lines (187 loc) · 6.2 KB
/
blockchain.c
File metadata and controls
209 lines (187 loc) · 6.2 KB
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
#include <openssl/evp.h>
#include <sys/time.h>
#include <stdio.h>
#include <string.h>
#include "blockchain.h"
void Block_sha256_hash(const Block *b, uint8_t *dst) {
if (!EVP_DigestInit_ex(ctx, sha256, NULL)) {
fprintf(stderr, "EVP_DigestInit_ex()");
exit(1);
}
if (!EVP_DigestUpdate(ctx, b, BLOCK_SIZE(b))) {
fprintf(stderr, "EVP_DigestUpdate()");
exit(1);
}
unsigned int len = 0;
if (!EVP_DigestFinal_ex(ctx, (unsigned char *)dst, &len)) {
fprintf(stderr, "EVP_DigestFinal_ex()");
exit(1);
}
}
Block *Block_new(uint32_t num_transactions) {
Block *ans = malloc(BLOCK_HEADER_SIZE + num_transactions * sizeof(Transaction));
ans->num_transactions = num_transactions;
return ans;
}
void Block_delete(Block *b) {
free(b);
}
void Block_init(Block *b, uint32_t index, double timestamp) {
b->index = index;
b->timestamp = timestamp;
b->num_transactions = 0;
memset(b->proof, 0, sizeof(b->proof));
memset(b->previous_hash, 0, sizeof(b->previous_hash));
}
void Block_print(const Block* b, FILE *file) {
fprintf(file, "Block No. %u\n", b->index);
fprintf(file, " - Timestamp: %lf\n", b->timestamp);
fprintf(file, " - %u Transaction(s) \n", b->num_transactions);
for (uint32_t i = 0; i < b->num_transactions; i++) {
fprintf(file, " ");
for (int j = 0; j < WALLET_ID_SIZE; j++) {
fprintf(file, "%02x", BLOCK_TRANSACTIONS_P(b)[i].sender[j]);
}
fprintf(file, " -> ");
for (int j = 0; j < WALLET_ID_SIZE; j++) {
fprintf(file, "%02x", BLOCK_TRANSACTIONS_P(b)[i].recipient[j]);
}
fprintf(file, ": $%llu\n", BLOCK_TRANSACTIONS_P(b)[i].amount);
}
fprintf(file, " - previous_hash:\t");
for (int i = 0; i < DIGEST_SIZE; i++)
fprintf(file, "%02x ", b->previous_hash[i]);
fprintf(file, "\n - prove of work:\t");
for (int i = 0; i < POW_LOT_SIZE; i++)
fprintf(file, "%02x ", b->proof[i]);
fprintf(file, "\n\n");
}
int Block_prove_of_work_trial(Block* b, const uint8_t* proof, uint8_t* digest) {
memcpy(b->proof, proof, sizeof(b->proof));
Block_sha256_hash(b, digest);
for (int i = 0; i < (PREFIX_ZEROS >> 3); i++)
if (digest[i] != 0) return -1;
return 0;
}
int Block_mine(Block *b, size_t max_trials, const uint8_t *proof_start, uint8_t *digest) {
uint8_t proof[POW_LOT_SIZE];
if (proof_start != NULL) {
memcpy(proof, proof_start, sizeof(proof));
} else {
memset(proof, 0, sizeof(proof));
}
for (size_t i = 0; i < max_trials; i++) {
if (Block_prove_of_work_trial(b, proof, digest) == 0) {
printf("Successfully done PoW in %zu trials\n", i);
return 0;
}
// increment proof
for (int i = 0; i < POW_LOT_SIZE; i++) {
if (proof[i] == 255) {
proof[i] = 0;
continue;
}
proof[i] += 1;
break;
}
}
return -1;
}
BlockChain *BlockChain_new(void) {
BlockChain *p = malloc(sizeof(BlockChain));
return p;
}
void BlockChain_delete(BlockChain *b) {
pthread_mutex_lock(&b->mutex);
while (b->head != NULL) {
ChainNode *temp = b->head;
b->head = b->head->prev;
free(temp);
}
pthread_mutex_unlock(&b->mutex);
free(b);
}
// NO THREAD SAFETY, make sure to call it only in initialization.
int BlockChain_init(BlockChain *c, bool genesis) {
pthread_mutex_init(&c->mutex, NULL);
if (!genesis) {
memset(c->last_hash, 0, sizeof(c->last_hash));
return 0;
}
c->head = malloc(sizeof(ChainNode));
c->head->prev = NULL;
struct timeval tv;
gettimeofday(&tv, NULL);
double timestamp = (double)tv.tv_sec + 1e-6 * (double)tv.tv_usec;
Block_init((Block *)&c->head->block, 0, timestamp);
if (Block_mine((Block *)&c->head->block, MAX_TRIALS, NULL, c->last_hash) < 0)
return -1;
return 0;
}
void BlockChain_print(BlockChain* c, FILE* file) {
ChainNode *cn = c->head;
fprintf(file, "Block Chain\t\t");
for (int i = 0; i < DIGEST_SIZE; i++)
fprintf(file, "%02x ", c->last_hash[i]);
fprintf(file, "\n");
while (cn != NULL) {
Block_print(&cn->block, file);
if (cn->prev != NULL) {
fprintf(file, "\t\t\t\t\t\t\t/\\\n\t\t\t\t\t\t\t||\n\n");
cn = cn->prev;
} else {
break;
}
}
}
void BlockChain_add_block(BlockChain *c, const Block *block) {
pthread_mutex_lock(&c->mutex);
ChainNode *temp = c->head;
c->head = malloc(sizeof(ChainNode *) + BLOCK_SIZE(block));
c->head->prev = temp;
memcpy((Block *)&c->head->block, block, BLOCK_SIZE(block));
pthread_mutex_unlock(&c->mutex);
}
ssize_t BlockChain_verify(BlockChain *c) {
pthread_mutex_lock(&c->mutex);
ssize_t len = 0;
ChainNode *ptr = c->head;
const uint8_t *hash_ptr = c->last_hash;
while (ptr != NULL) {
// Test if proof of work done
for (int i = 0; i < (PREFIX_ZEROS >> 3); i++)
if (hash_ptr[i] != 0)
goto failed;
// Test if hash correct
uint8_t digest[DIGEST_SIZE];
Block_sha256_hash(&(ptr->block), digest);
// compare hash
for (size_t i = 0; i < sizeof(digest); i++)
if (digest[i] != hash_ptr[i])
goto failed;
// Test if the transactions has at most 1 record of node reward, and the reward is 1.
bool node_rewarded = false;
for (size_t i = 0; i < ptr->block.num_transactions; i++) {
bool is_reward = true;
Transaction *tr = BLOCK_TRANSACTIONS_P(&ptr->block) + i;
for (int j = 0; j < WALLET_ID_SIZE; j++) {
if (tr->sender[j] != 0) {
is_reward = false;
break;
}
}
if (!is_reward) continue;
if (tr->amount != 1) goto failed;
if (node_rewarded) goto failed;
node_rewarded = true;
}
hash_ptr = ptr->block.previous_hash;
ptr = ptr->prev;
len++;
}
pthread_mutex_unlock(&c->mutex);
return len;
failed:
pthread_mutex_unlock(&c->mutex);
return -1;
}