-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
executable file
·498 lines (451 loc) · 18.3 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
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <fcntl.h>
#include <zconf.h>
#include <time.h>
#include "hash.h"
#include "list.h"
#include "wallet.h"
#include "bitcoin.h"
#include "transaction.h"
#define BUFFER_SIZE 256
typedef void *pointer;
bool init_complete = false;
time_t max_transaction_timestamp = 0;
Hashtable walletsHT = NULL, bitcoinsHT = NULL, senderHT = NULL, receiverHT = NULL, transactionsHT = NULL;
void wrongOptionValue(char *opt, char *val) {
fprintf(stdout, "Wrong value [%s] for option '%s'\n", val, opt);
exit(EXIT_FAILURE);
}
void readOptions(
int argc,
char **argv,
char **a, /*bitcoinBalancesFile*/
char **t, /*transactionsFile*/
unsigned long int *v, /*bitcoinValue*/
unsigned long int *h1, /*senderHashtableNumOfEntries*/
unsigned long int *h2, /*receiverHashtableNumOfEntries*/
unsigned long int *b /*bucketSize*/
) {
int i;
char *opt, *optVal;
for (i = 1; i < argc; ++i) {
opt = argv[i];
optVal = argv[i + 1];
if (strcmp(opt, "-a") == 0) {
if (optVal != NULL && optVal[0] != '-') {
*a = optVal;
} else {
wrongOptionValue(opt, optVal);
}
} else if (strcmp(opt, "-t") == 0) {
if (optVal != NULL && optVal[0] != '-') {
*t = optVal;
} else {
wrongOptionValue(opt, optVal);
}
} else if (strcmp(opt, "-v") == 0) {
if (optVal != NULL && optVal[0] != '-') {
//*v = atoi(optVal);
*v = (unsigned long int) strtol(optVal, NULL, 10);
} else {
wrongOptionValue(opt, optVal);
}
} else if (strcmp(opt, "-h1") == 0) {
if (optVal != NULL && optVal[0] != '-') {
*h1 = (unsigned long) strtol(optVal, NULL, 10);
} else {
wrongOptionValue(opt, optVal);
}
} else if (strcmp(opt, "-h2") == 0) {
if (optVal != NULL && optVal[0] != '-') {
*h2 = (unsigned long) strtol(optVal, NULL, 10);
} else {
wrongOptionValue(opt, optVal);
}
} else if (strcmp(opt, "-b") == 0) {
if (optVal != NULL && optVal[0] != '-') {
*b = (unsigned long) strtol(optVal, NULL, 10);
} else {
wrongOptionValue(opt, optVal);
}
}
}
}
void init(Hashtable *walletsHT, Hashtable *bitcoinsHT, char *a, unsigned long int v, unsigned long int b,
const unsigned long int h1, const unsigned long int h2) {
FILE *fp = NULL;
Wallet wallet = NULL;
char buf[BUFFER_SIZE], *token = NULL;
unsigned long int bid = 0;
bitcoin bc = NULL;
ht_bitcoin_params htBitCoinParams;
/*Open bitcoinBalancesFile*/
fp = fopen(a, "r");
if (fp != NULL) {
/*Initialize Wallets hashtable*/
HT_Init(
walletsHT,
h1 > h2 ? h1 : h2,
b,
(pointer (*)(pointer)) createWallet,
(int (*)(pointer, pointer)) cmpWallet,
(unsigned long (*)(pointer, unsigned long int)) walletHash,
(unsigned long (*)(pointer)) destroyWallet
);
/*Initialize bitcoins hashtable*/
HT_Init(
bitcoinsHT,
h1 > h2 ? h1 : h2,
b,
(pointer (*)(pointer)) bcCreate,
(int (*)(pointer, pointer)) bcCompare,
(unsigned long (*)(pointer, unsigned long int)) bitcoinHash,
(unsigned long (*)(pointer)) bcDestroy
);
/*Read bitcoinBalancesFile*/
while (fgets(buf, BUFFER_SIZE, fp) != NULL) {
token = strtok(buf, "\n");
token = strtok(token, " ");
if (token != NULL) {
//printf("%s \n", token);
/*Insert wallet, check if the insertion fails*/
if (HT_Insert(*walletsHT, token, token, (void **) &wallet)) {
assert(wallet != NULL);
/*Read bitcoins for current wallet*/
do {
token = strtok(NULL, " ");
if (token != NULL) {
bid = (unsigned long int) strtol(token, NULL, 10);
/*Initialize parameters for bitcoin*/
htBitCoinParams.walletId = wallet->userId;
htBitCoinParams.bid = bid;
htBitCoinParams.v = v;
/*Insert bitcoin into bitcoins hashtable*/
if (HT_Insert(*bitcoinsHT, &bid, &htBitCoinParams, (void **) &bc)) {
assert(bc != NULL);
//printf("[%lu] [%p] \n", bid, bc);
wallet->balance += v;
/*Insert user bitcoin entry in wallet's bitcoin list*/
if (!listInsert(wallet->bitcoins, bc)) {
fprintf(stdout, "\nBitCoin [%lu] was not inserted in wallet list!\n", bid);
};
} else {
fprintf(stdout, "\nHT BitCoin [%p] was not inserted because is duplicate!\n", bc);
HT_Destroy(walletsHT, true);
HT_Destroy(bitcoinsHT, true);
exit(EXIT_FAILURE);
}
}
} while (token != NULL);
} else {
fprintf(stdout, "\nWallet [%s] was not inserted because is duplicate!\n", token);
destroyWallet(wallet);
HT_Destroy(walletsHT, true);
HT_Destroy(bitcoinsHT, true);
exit(EXIT_FAILURE);
}
//printf("\n\n");
}
}
fclose(fp);
} else {
fprintf(stdout, "\nFile '%s' doesn't exists!\n", a);
exit(EXIT_FAILURE);
}
}
void initTransactions(Hashtable *walletsHT, Hashtable *bitcoins, Hashtable *senderHT, Hashtable *receiverHT,
Hashtable *transactionsHT, const unsigned long int h1, const unsigned long int h2,
unsigned long int b,
char *t, unsigned long int v) {
FILE *fp = NULL;
/*Open transactionsFile*/
fp = fopen(t, "r");
if (fp != NULL) {
/* Initialize hashtable for sender transactions list hashtable.*/
HT_Init(
senderHT,
h1,
b,
(pointer (*)(pointer)) createTransactionList,
(int (*)(pointer, pointer)) cmpTransactionList,
(unsigned long (*)(pointer, unsigned long int)) walletHash,
(unsigned long (*)(pointer)) destroyTransactionList
);
/* Initialize hashtable for receiver transactions list hashtable.*/
HT_Init(
receiverHT,
h2,
b,
(pointer (*)(pointer)) createTransactionList,
(int (*)(pointer, pointer)) cmpTransactionList,
(unsigned long (*)(pointer, unsigned long int)) walletHash,
(unsigned long (*)(pointer)) destroyTransactionList
);
/* Initialize hashtable for transactions hashtable.*/
HT_Init(
transactionsHT,
h1 > h2 ? h1 : h2,
b,
(pointer (*)(pointer)) createTransaction,
(int (*)(pointer, pointer)) cmpTransaction,
(unsigned long (*)(pointer, unsigned long int)) transactionHash,
(unsigned long (*)(pointer)) destroyTransaction
);
/* Run transactions from file in order to initialize the simulation.*/
performTransactions(fp, walletsHT, senderHT, receiverHT, transactionsHT, "\n");
fclose(fp);
} else {
fprintf(stdout, "File '%s' doesn't exists!\n", t);
exit(EXIT_FAILURE);
}
}
/* Find transactions through hash table.*/
unsigned long int findTransactions(char *input, Hashtable ht, Wallet *wallet, List *transactions) {
unsigned long int amount = 0;
char *walletId = NULL, *rest = NULL;
struct tm s = {0}, e = {0};
int read = 0;
Transaction transaction = NULL;
time_t start = 0, end = 0;
rest = input;
walletId = strtok_r(input, " ", &rest);
if (walletId != NULL) {
read = sscanf(rest, "%d-%d-%d %d:%d %d-%d-%d %d:%d",
&s.tm_mday, &s.tm_mon, &s.tm_year, &s.tm_hour, &s.tm_min,
&e.tm_mday, &e.tm_mon, &e.tm_year, &e.tm_hour, &e.tm_min
);
if (read == 10) {
s.tm_year = s.tm_year - 1900;
s.tm_mon = s.tm_mon - 1;
s.tm_isdst = -1;
start = mktime(&s);
e.tm_year = e.tm_year - 1900;
e.tm_mon = e.tm_mon - 1;
e.tm_isdst = -1;
end = mktime(&e);
}
*wallet = HT_Get(walletsHT, input);
if (*wallet != NULL) {
*transactions = HT_Get(ht, (*wallet)->userId);
if (*transactions != NULL) {
printf("Transactions of user '%s'\n", (*wallet)->userId);
listSetCurrentToStart(*transactions);
while ((transaction = listNext(*transactions)) != NULL) {
if (read == 10) {
if (transaction->timestamp >= start && transaction->timestamp <= end) {
amount += transaction->value;
transactionPrint(transaction);
}
} else {
amount += transaction->value;
transactionPrint(transaction);
}
}
}
} else {
fprintf(stdout, "~ error: wallet '%s' not found!\n", input);
}
} else {
fprintf(stdout, "~ error: bad input\n");
}
return amount;
}
/* Cli command*/
void requestTransaction(char *input) {
if (input != NULL) {
if (!performTransaction(input, &walletsHT, &senderHT, &receiverHT, &transactionsHT)) {
fprintf(stdout, "~ success: The transaction was executed successfully.\n");
}
} else {
fprintf(stdout, "~ error: bad input!\n");
}
}
/* Cli command*/
void requestTransactions(char *input) {
FILE *fp = NULL;
int q, s;
char buf[BUFFER_SIZE];
if (input != NULL) {
/* Use this sscanf only for check the number of arguments to determine if the input is file of transactions
* or actual transactions.*/
s = sscanf(input, "%s %s %d %d-%d-%d %d:%d;", buf, buf, &q, &q, &q, &q, &q, &q);
if (s == 1) {
fp = fopen(input, "r");
if (fp != NULL) {
performTransactions(fp, &walletsHT, &senderHT, &receiverHT, &transactionsHT, ";\n");
fclose(fp);
} else {
fprintf(stdout, "\nFile '%s' not found!\n", input);
}
} else if (s == 8) {
performTransaction(input, &walletsHT, &senderHT, &receiverHT, &transactionsHT);
performTransactions(stdin, &walletsHT, &senderHT, &receiverHT, &transactionsHT, ";\n");
} else {
fprintf(stdout, "~ error: bad input!\n");
}
} else {
fprintf(stdout, "~ error: bad input!\n");
}
}
/* Cli command*/
void findEarnings(char *input) {
Wallet wallet = NULL;
List transactions = NULL;
unsigned long int amount = 0;
if (input) {
amount = findTransactions(input, receiverHT, &wallet, &transactions);
if (wallet != NULL) {
printf("User '%s' has received %lu$ in his wallet.\n", wallet->userId, amount);
}
} else {
fprintf(stdout, "~ error: bad input\n");
}
}
/* Cli command*/
void findPayments(char *input) {
Wallet wallet = NULL;
List transactions = NULL;
if (input) {
unsigned long int amount = findTransactions(input, senderHT, &wallet, &transactions);
if (wallet != NULL) {
printf("User '%s' has send %lu$ from his wallet.\n", wallet->userId, amount);
}
} else {
fprintf(stdout, "~ error: bad input\n");
}
}
/* Cli command*/
void walletStatus(char *input) {
bitcoin bc = NULL;
if (input != NULL) {
Wallet wallet = HT_Get(walletsHT, input);
if (wallet != NULL) {
printf("Wallet status for '%s' is: %lu$\nBitcoins: ", wallet->userId, wallet->balance);
listSetCurrentToStart(wallet->bitcoins);
/* Access each bitcoin of sender to perform transaction*/
while ((bc = listNext(wallet->bitcoins)) != NULL) {
printf("[%lu|%lu$] ", bcGetId(bc), bcGetAmount(bc, wallet->userId));
}
putchar('\n');
} else {
fprintf(stdout, "~ error: wallet '%s' not found!\n", input);
}
} else {
fprintf(stdout, "~ error: bad input!\n");
}
}
/* Cli command*/
void bitCoinStatus(char *input) {
unsigned long int bitcoinId = 0, value = 0, transactions = 0, unspent = 0;
if (input != NULL) {
bitcoinId = (unsigned long int) strtol(input, NULL, 10);
if (bitcoinId > 0) {
bitcoin bc = HT_Get(bitcoinsHT, &bitcoinId);
if (bc != NULL) {
bcTrace(bc, &value, &transactions, &unspent, true, false);
printf("%lu %lu %lu %lu\n", bitcoinId, value, transactions, unspent);
} else {
fprintf(stdout, "~ error: bitcoin %lu doesn't exists!\n", bitcoinId);
}
} else {
fprintf(stdout, "~ error: bad input, not a number!\n");
}
} else {
fprintf(stdout, "~ error: bad input, expected bitcoin number!\n");
}
}
/* Cli command*/
void traceCoin(char *input) {
unsigned long int bitcoinId = 0, value = 0, transactions = 0, unspent = 0;
if (input != NULL) {
bitcoinId = (unsigned long int) strtol(input, NULL, 10);
if (bitcoinId > 0) {
bitcoin bc = HT_Get(bitcoinsHT, &bitcoinId);
if (bc != NULL) {
bcTrace(bc, &value, &transactions, &unspent, true, true);
} else {
fprintf(stdout, "~ error: bitcoin %lu doesn't exists!\n", bitcoinId);
}
} else {
fprintf(stdout, "~ error: bad input, not a number!\n");
}
} else {
fprintf(stdout, "~ error: bad input, expected bitcoin number!\n");
}
}
/* Command line interface
* Get input from user to perform various cli commands.*/
void cli() {
int fd;
char *input = NULL, *line = NULL, *cmd = NULL;
size_t len = 0;
ssize_t read;
printf("Welcome to bitcoin transactions simulator, write 'exit' to quit from cli or use the available commands "\
"described below:\n\n\t• rt OR requestTransaction\t\t\t\t\t\t\t--> Request transaction from cli.\n"\
"\t• rts [filename] OR requestTransactions [filename]\t--> Request multiple transactions from file.\n"\
"\t• rts OR requestTransactions\t\t\t\t\t\t--> Request multiple transactions from cli with semicolon as delimiter.\n"\
"\t• fe [WalletId] OR findEarnings [WalletId]\t\t\t--> Find all earnings for a specific wallet id.\n"\
"\t• fp [WalletId] OR findPayments [WalletId]\t\t\t--> Find all payments for a specific wallet id.\n"\
"\t• ws [WalletId] OR walletStatus [WalletId]\t\t\t--> Get current status for a specific wallet id.\n"\
"\t• bcs [BitcoinId] OR bitCoinStatus [BitcoinId]\t\t--> Get current status for a specific bitcoin id. \n"\
"\t• trc [BitcoinId] OR traceCoin [BitcoinId]\t\t\t--> Get all transactions that is involved with this bitcoin.\n\n");
putchar('>');
while ((read = getline(&line, &len, stdin)) != EOF) {
input = line;
cmd = strtok_r(input, " \n", &input);
input = strtok(input, "\n");
if (cmd != NULL) {
if (strcmp(cmd, "requestTransaction") == 0 || strcmp(cmd, "rt") == 0 || strcmp(cmd, "RT") == 0) {
requestTransaction(input);
} else if (strcmp(cmd, "requestTransactions") == 0 || strcmp(cmd, "rts") == 0 || strcmp(cmd, "RTS") == 0) {
requestTransactions(input);
} else if (strcmp(cmd, "findEarnings") == 0 || strcmp(cmd, "fe") == 0 || strcmp(cmd, "FE") == 0) {
findEarnings(input);
} else if (strcmp(cmd, "findPayments") == 0 || strcmp(cmd, "fp") == 0 || strcmp(cmd, "FP") == 0) {
findPayments(input);
} else if (strcmp(cmd, "walletStatus") == 0 || strcmp(cmd, "ws") == 0 || strcmp(cmd, "WS") == 0) {
walletStatus(input);
} else if (strcmp(cmd, "bitCoinStatus") == 0 || strcmp(cmd, "bcs") == 0 || strcmp(cmd, "BCS") == 0) {
bitCoinStatus(input);
} else if (strcmp(cmd, "traceCoin") == 0 || strcmp(cmd, "trc") == 0 || strcmp(cmd, "TRC") == 0) {
traceCoin(input);
} else if (strcmp(cmd, "exit") == 0) {
fd = open("/dev/null", O_WRONLY);
dup2(fd, 0);
close(fd);
break;
} else {
fprintf(stdout, "~ error: %s: command not found!\n", cmd);
}
}
putchar('>');
putchar(' ');
}
free(line);
}
int main(int argc, char *argv[]) {
unsigned long int h1 = 0, h2 = 0, b = 0, v = 0;
char *a = NULL, *t = NULL;
srand((unsigned int) time(NULL));
/*Read argument options from command line*/
readOptions(argc, argv, &a, &t, &v, &h1, &h2, &b);
/*Init structures*/
init(&walletsHT, &bitcoinsHT, a, v, b, h1, h2);
/*Initialize with some transactions*/
initTransactions(&walletsHT, &bitcoinsHT, &senderHT, &receiverHT, &transactionsHT, h1, h2, b, t,
v);
/*Set init flag to true*/
init_complete = true;
/*Get input from user to perform various cli commands*/
cli();
/*Deallocate previously allocated memory.*/
HT_Destroy(&walletsHT, true);
HT_Destroy(&bitcoinsHT, true);
HT_Destroy(&senderHT, true);
HT_Destroy(&receiverHT, true);
HT_Destroy(&transactionsHT, true);
return EXIT_SUCCESS;
}