forked from capythulhu/coxinhacoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
172 lines (142 loc) · 5.06 KB
/
main.c
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
#define STDIO_H
#include <stdio.h>
#define STDBOOL_H
#include <stdbool.h>
#define TIME_H
#include <time.h>
#include "bytes.h"
#include "hash.h"
#include "block.h"
#include "transaction.h"
#include "wallet.h"
#include "list.h"
#include "hashmap.h"
list *blockchain;
transaction *gTransaction;
int main(void);
void add_block(block *b);
// Adiciona um bloco à blockchain
void add_block(block *b) {
mine_block(b);
put_val_on_list(blockchain, b);
}
// Checa a integridade da blockchain
bool check_blockchain_integrity(list *l) {
block *b;
block *c;
hashmap *transactionOuts = new_hashmap();
put_val_on_hashmap(
transactionOuts,
((transactionout*)get_val_from_list(gTransaction->outputs, 0))->id,
get_val_from_list(gTransaction->outputs, 0)
);
int i, j;
for(i = 1; i < l->size; i++) {
b = (block*)get_val_from_list(l, i);
c = (block*)get_val_from_list(l, i - 1);
if(!compare_buffer(b->id, get_block_hash(b))) {
printf("Current hashes are not valid.\n");
return false;
}
if(!compare_buffer(c->id, get_block_hash(c))) {
printf("Previous hashes are not valid.\n");
return false;
}
if(!mine(b->id, b->gold, false)) {
printf("Block has not been mined.\n");
return false;
}
transactionout *out;
for(j = 0; j < b->transactions->size; j++) {
transaction *t = get_val_from_list(b->transactions, j);
if(!check_signature(t)) {
printf("Transaction signature does not match.\n");
return false;
}
if(get_transaction_inputs_value(t) != get_transaction_outputs_value(t)) {
printf("Transactions inputs and outputs are not equal.\n");
return false;
}
listnode *temp = t->inputs->first;
while(temp) {
out = (transactionout*)get_val_from_hashmap(
transactionOuts,
((transactionin*)temp->val)->outputId
);
if(!out) {
printf("Transaction references to invalid input.\n");
return false;
}
if(((transactionin*)temp->val)->output->value != out->value) {
printf("Transaction input value is invalid.\n");
return false;
}
rem_key_from_hashmap(
transactionOuts,
((transactionin*)temp->val)->outputId
);
temp = temp->next;
}
temp = t->outputs->first;
while(temp) {
put_val_on_hashmap(
transactionOuts,
((transactionout*)temp->val)->id,
temp->val
);
temp = temp->next;
}
if(((transactionout*)get_val_from_list(t->outputs, 0))->recipientKey
!= t->recipientKey) {
printf("Transaction output recipient is invalid.\n");
return false;
}
if(((transactionout*)get_val_from_list(t->outputs, 1))->recipientKey
!= t->senderKey.key) {
printf("Transaction output change recipient is invalid.\n");
return false;
}
}
}
printf("Blockchain is valid.\n");
return true;
}
int main(void) {
blockchain = new_list();
hashmap *transactionOuts = new_hashmap();
srand(time(NULL));
wallet *coinbase = new_wallet();
wallet *w1 = new_wallet();
wallet *w2 = new_wallet();
gTransaction = new_transaction(coinbase->publicKey, w1->publicKey.key, 100, NULL);
gTransaction->signature = encrypt(gTransaction->id, coinbase->privateKey);
gTransaction->id = (buffer) {"0", 1};
put_val_on_list(gTransaction->outputs,
new_transactionout(
gTransaction->recipientKey,
gTransaction->value,
gTransaction->id
)
);
put_val_on_hashmap(transactionOuts,
((transactionout*)get_val_from_list(gTransaction->outputs, 0))->id,
get_val_from_list(gTransaction->outputs, 0)
);
block *gBlock = new_block(new_buffer(1));
put_transaction_on_block(gBlock, gTransaction, transactionOuts);
getchar();
add_block(gBlock);
// Teste
block *block1 = new_block(gBlock->id);
printf("W1: %.6f\n", get_balance(w1, transactionOuts));
printf("W2: %.6f\n", get_balance(w2, transactionOuts));
put_transaction_on_block(block1,
send_funds(w1, w2->publicKey.key, 40, transactionOuts),
transactionOuts
);
getchar();
add_block(block1);
printf("W1: %.6f\n", get_balance(w1, transactionOuts));
printf("W2: %.6f\n", get_balance(w2, transactionOuts));
check_blockchain_integrity(blockchain);
}