-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitcoin.c
289 lines (242 loc) · 7.62 KB
/
bitcoin.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
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
#include "bitcoin.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
#include <string.h>
#include "queue.h"
typedef void *pointer;
struct BitCoin {
unsigned long int bid;
bcNode root;
unsigned long int nodes;
};
struct Node {
struct Transaction *transaction;
char *walletId;
unsigned long int balance;
bcNode parent, left, right;
};
/***Private functions***/
bool _isLeaf(bcNode node) {
return node->left == NULL && node->right == NULL;
}
bool _isLeft(bcNode root) {
return root->parent->left == root;
}
bool _isRight(bcNode root) {
return root->parent->right == root;
}
bool _isTarget(bcNode node, char *walletId) {
return !strcmp(node->walletId, walletId);
}
bcNode _createNode(bcNode parent, unsigned long int balance, char *walletId, Transaction transaction) {
bcNode node = (bcNode) malloc(sizeof(struct Node));
if (node != NULL) {
node->balance = balance;
node->walletId = malloc(strlen(walletId) * sizeof(char) + 1);
strcpy(node->walletId, walletId);
node->transaction = transaction;
node->parent = parent;
node->right = NULL;
node->left = NULL;
return node;
} else
return NULL;
}
/***Public functions***/
void bcTrace(bitcoin bc, unsigned long int *value, unsigned long int *transactions, unsigned long int *unspent,
bool traceTransaction, bool printTransactions) {
assert(bc != NULL);
Queue queue = NULL;
queueCreate(&queue, bc->nodes);
bcNode root = bc->root;
List list = NULL;
listCreate(&list, "");
Transaction transaction = NULL;
bool duplicate;
printf("Trace for bitcoin %lu with initial owner '%s' and initial balance: %lu$\n",
bc->bid, bc->root->walletId, root->balance);
*value = root->balance;
while (root != NULL) {
duplicate = false;
if (root->parent != NULL) {
if (_isLeaf(root) && _isRight(root)) {
(*unspent) += root->balance;
}
}
if (traceTransaction && root->transaction != NULL) {
while ((transaction = listNext(list)) != NULL) {
if (!strcmp(transaction->transactionId, root->transaction->transactionId)) {
duplicate = true;
}
}
if (!duplicate && _isLeft(root)) {
(*transactions)++;
if (printTransactions) {
listInsert(list, root->transaction);
}
}
}
if (root->left != NULL) {
enQueue(queue, root->left);
}
if (root->right != NULL) {
enQueue(queue, root->right);
}
root = deQueue(queue);
}
listSetCurrentToStart(list);
while ((transaction = listNext(list)) != NULL) {
transactionPrint(transaction);
}
queueDestroy(&queue);
}
bool bcInsert(bitcoin bc, unsigned long int *rest, unsigned long int *amount, Transaction transaction) {
assert(bc != NULL);
assert(*rest > 0);
assert(transaction != NULL);
Queue queue = NULL;
queueCreate(&queue, bc->nodes);
bcNode r = bc->root;
unsigned long int balance = 0;
while (r != NULL) {
printf("Visit: [%p][%s|%lu$]\n", r, r->walletId, r->balance);
balance = r->balance;
if (r->left != NULL) {
enQueue(queue, r->left);
}
if (r->right != NULL) {
enQueue(queue, r->right);
}
/* Check if this node is target*/
if (_isTarget(r, transaction->senderWalletId)) {
/* Is current node a leaf ?*/
if (_isLeaf(r)) {
*amount += balance;
printf("Target leaf: [%p][%s|%lu$]\n", r, r->walletId, r->balance);
if (*rest >= balance) {
/* Left node for receiver*/
r->left = _createNode(r, balance, transaction->receiverWalletId, transaction);
bc->nodes++;
printf("New left child: [%p][%s|%lu$]\n", r->left, transaction->receiverWalletId, balance);
*rest -= balance;
*amount -= balance;
} else if (*rest < balance) {
/* Left node for receiver*/
r->left = _createNode(r, *rest, transaction->receiverWalletId, transaction);
bc->nodes++;
printf("New left child: [%p][%s|%lu$] | ", r->left, transaction->receiverWalletId, *rest);
*amount -= *rest;
/* Right node for sender's rest*/
r->right = _createNode(r, r->balance - *rest, transaction->senderWalletId, transaction);
bc->nodes++;
printf("New right child: [%p][%s|%lu$]\n", r->right, transaction->senderWalletId,
r->balance - *rest);
*rest = 0;
}
}
}
r = deQueue(queue);
}
queueDestroy(&queue);
return true;
}
unsigned long int bcGetAmount(bitcoin bc, char *walletId) {
assert(bc != NULL);
assert(walletId != NULL);
Queue queue = NULL;
queueCreate(&queue, bc->nodes);
bcNode r = bc->root;
unsigned long int balance = 0, amount = 0;
while (r != NULL) {
//printf("Visit: [%p][%s|%lu$]\n", r, r->walletId, r->balance);
balance = r->balance;
if (r->left != NULL) {
enQueue(queue, r->left);
}
if (r->right != NULL) {
enQueue(queue, r->right);
}
/* Check if this node is target*/
if (_isTarget(r, walletId)) {
/* Is current node a leaf ?*/
if (_isLeaf(r)) {
amount += balance;
//printf("Target leaf: [%p][%s|%lu$]", r, r->walletId, r->balance);
}
}
r = deQueue(queue);
}
queueDestroy(&queue);
return amount;
}
bool bcPrint(bitcoin bc) {
assert(bc != NULL);
Queue queue = NULL;
queueCreate(&queue, bc->nodes);
bcNode r = bc->root;
printf("\nPrint bitcoin %lu nodes:\n", bc->bid);
while (r != NULL) {
printf("[%s|%lu$]\n", r->walletId, r->balance);
if (r->left != NULL) {
enQueue(queue, r->left);
}
if (r->right != NULL) {
enQueue(queue, r->right);
}
r = deQueue(queue);
}
queueDestroy(&queue);
return true;
}
long unsigned int bcGetId(bitcoin bc) {
return bc->bid;
}
/* @Callback
* Initialize & return a new BitCoin*/
bitcoin bcCreate(ht_bitcoin_params *htBitCoinParams) {
bitcoin bc = NULL;
bc = (bitcoin) malloc(sizeof(struct BitCoin));
if (bc != NULL) {
bc->nodes = 1;
bc->bid = htBitCoinParams->bid;
bc->root = _createNode(NULL, htBitCoinParams->v, htBitCoinParams->walletId, NULL);
if (bc->root == NULL) {
bcDestroy(bc);
return NULL;
}
}
return bc;
}
/* @Callback
* Compare bitcoins*/
int bcCompare(bitcoin bc1, bitcoin bc2) {
return bc1->bid != bc2->bid;
}
/* @Callback
* Hash bitcoin*/
unsigned long int bitcoinHash(const long int *bid, unsigned long int capacity) {
return *bid % capacity;
}
/* @Callback
* Destroy bitcoin*/
void bcDestroy(bitcoin bc) {
assert(bc != NULL);
Queue queue = NULL;
queueCreate(&queue, bc->nodes);
bcNode root = bc->root;
while (root) {
if (root->left) {
enQueue(queue, root->left);
}
if (root->right) {
enQueue(queue, root->right);
}
free(root->walletId);
free(root);
root = deQueue(queue);
}
queueDestroy(&queue);
free(bc);
}