-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.c
377 lines (319 loc) · 13.2 KB
/
transaction.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
#include <malloc.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <assert.h>
#include "wallet.h"
#include "transaction.h"
#include "bitcoin.h"
#define BUFFER_SIZE 256
/* Execute
* transaction*/
bool execute(Wallet senderWallet, Wallet receiverWallet, Transaction transaction) {
assert(senderWallet != NULL);
assert(receiverWallet != NULL);
assert(transaction != NULL);
assert(transaction->value > 0);
unsigned long int amount = 0, rest = 0;
bitcoin bc = NULL, wbc = NULL;
bool success = true, duplicate;
rest = transaction->value;
printf("Transaction %s: %s --> %s %lu$\n", transaction->transactionId, transaction->senderWalletId,
transaction->receiverWalletId, transaction->value);
listSetCurrentToStart(senderWallet->bitcoins);
/* Access each bitcoin of sender to perform transaction*/
while (rest > 0 && (bc = listNext(senderWallet->bitcoins)) != NULL) {
printf("\n-USE BITCOIN %lu REST: %lu$\n", bcGetId(bc), rest);
/* Execute transaction.*/
bcInsert(bc, &rest, &amount, transaction);
/* The sender does not share any more this bitcoin with others because he spent all the amount of it for
* this transaction. Bitcoin is about to be removed from sender list!*/
if (amount == 0) {
/* Remove node of this bitcoin for this wallet*/
listRemove(senderWallet->bitcoins, bc);
}
listSetCurrentToStart(receiverWallet->bitcoins);
duplicate = false;
while ((wbc = listNext(receiverWallet->bitcoins)) != NULL) {
if (bcGetId(wbc) == bcGetId(bc)) {
duplicate = true;
}
}
if (!duplicate) {
listInsert(receiverWallet->bitcoins, bc);
}
bcPrint(bc);
printf("-AFTER USE BITCOIN %lu REST: %lu$ WALLET AMOUNT: %lu$\n", bcGetId(bc), rest, amount);
}
if (rest == 0) {
senderWallet->balance -= transaction->value;
receiverWallet->balance += transaction->value;
} else {
success = false;
}
return success;
}
/* Perform
* transaction from input buffer*/
bool performTransaction(char *input, Hashtable *walletsHT, Hashtable *senderHT, Hashtable *receiverHT,
Hashtable *transactionsHT) {
bool error = false;
char *line = NULL, *transactionId = NULL;
int i;
Transaction transaction = NULL;
Wallet senderWallet = NULL, receiverWallet = NULL;
List senderTransactions = NULL, receiverTransactions = NULL;
if (init_complete) {
/* Generate random id for transaction*/
transactionId = malloc(sizeof(char) * 15 + 1);
for (i = 0; i < 15; i++) {
transactionId[i] = (char) (rand() % 26 + 65);
}
transactionId[i] = '\0';
/* Build transaction string*/
line = (char *) malloc(strlen(transactionId) + 1 * sizeof(char) + 1 + strlen(input) * sizeof(char) + 1);
char *tmp = strdup(input);
strcpy(line, transactionId);
strcat(line, " ");
strcat(line, tmp);
free(tmp);
} else {
/* Allocate space for line string to save a copy of token.*/
line = malloc(strlen(input) * sizeof(char) + 1);
}
if (line != NULL) {
if (!init_complete) {
strcpy(line, input);
transactionId = strtok(input, " ");
}
/*Create a transaction through hashtable from parsed line to ensure there is no other one with the same id.*/
if (HT_Insert(*transactionsHT, transactionId, line, (void **) &transaction)) {
assert(transaction != NULL);
/* Get sender's wallet.*/
senderWallet = HT_Get(*walletsHT, transaction->senderWalletId);
if (senderWallet == NULL) {
fprintf(stdout, "Unacceptable transaction '%s': Sender's wallet '%s' doesn't exists!\n",
transaction->transactionId, transaction->senderWalletId);
error = true;
}
/* Create/Get sender transactions list hashtable.*/
if (!error) {
HT_Insert(*senderHT, transaction->senderWalletId, transaction->senderWalletId,
(void **) &senderTransactions);
error = senderTransactions == NULL;
}
/* Get receiver's wallet.*/
if (!error) {
receiverWallet = HT_Get(*walletsHT, transaction->receiverWalletId);
if (receiverWallet == NULL) {
fprintf(stdout, "Unacceptable transaction '%s': Receivers's wallet '%s' doesn't exists!\n",
transaction->transactionId, transaction->receiverWalletId);
error = true;
}
}
/*Create/Get receiver transactions list hashtable.*/
if (!error) {
HT_Insert(*receiverHT, transaction->receiverWalletId, transaction->receiverWalletId,
(void **) &receiverTransactions);
error = receiverTransactions == NULL;
}
/* Check the case where the money is not enough.*/
if (!error) {
if (transaction->value > senderWallet->balance) {
error = true;
fprintf(stdout,
"Unacceptable transaction '%s': Sender's money is not enough!\n",
transaction->transactionId);
}
}
/* Check case of transfer money to the same person.*/
if (!error) {
if (!strcmp(transaction->senderWalletId, transaction->receiverWalletId)) {
error = true;
fprintf(stdout,
"Unacceptable transaction '%s': Ιt's not possible to transfer money to the same person!\n",
transaction->transactionId);
}
}
/* Check if transaction is old.*/
if (!error) {
if (transaction->timestamp < max_transaction_timestamp) {
error = true;
fprintf(stdout, "Unacceptable transaction '%s': Date is less than the current time!\n",
transaction->transactionId);
}
}
/* Finally all is ok, now try to execute the transaction!*/
if (!error) {
printf("\n• • • • • • • • E X E C U T E T R A N S A C T I O N • • • • • • • •\n");
if (execute(senderWallet, receiverWallet, transaction)) {
/* Add transaction in sender's list of transactions.*/
listInsert(senderTransactions, transaction);
/* Add transaction in receiver's list of transactions.*/
listInsert(receiverTransactions, transaction);
/* Update max transaction timestamp for use later.*/
if (transaction->timestamp > max_transaction_timestamp) {
max_transaction_timestamp = transaction->timestamp;
}
} else {
fprintf(stderr, "Transaction '%s' was fail!\n", transaction->transactionId);
error = true;
}
printf("\n• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •\n\n");
}
/* And if the execute of this transaction fails, then it is necessary to delete it*/
if (error) {
/* Remove transaction from hashtable in order to normally inserted the next time.*/
HT_Remove(*transactionsHT, transaction->transactionId, transaction->transactionId, true);
}
} else {
if (transaction != NULL) {
fprintf(stdout, "Unacceptable transaction '%s': Transaction is duplicate!\n",
transaction->transactionId);
} else {
fprintf(stdout, "Unacceptable transaction '%s': Bad format!\n", transactionId);
}
error = true;
}
if (init_complete) {
free(transactionId);
}
free(line);
} else {
error = true;
fprintf(stdout, "Bad transaction format!\n");
}
return error;
}
/* Perform
* transactions from input stream*/
bool performTransactions(FILE *fp, Hashtable *walletsHT, Hashtable *senderHT,
Hashtable *receiverHT, Hashtable *transactionsHT, char *delimiter) {
bool error = false;
char buf[BUF], *token = NULL;
while (fgets(buf, BUF, fp) != NULL) {
token = strtok(buf, delimiter);
if (token != NULL) {
error = performTransaction(token, walletsHT, senderHT, receiverHT, transactionsHT);
} else {
break;
}
}
return error;
}
void transactionPrint(Transaction transaction) {
char buffer[26];
struct tm *time;
if (transaction != NULL) {
time = localtime(&transaction->timestamp);
strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", time);
printf("%s %s %s %lu %s\n", transaction->transactionId,
transaction->senderWalletId,
transaction->receiverWalletId,
transaction->value,
buffer
);
}
}
/* @Callback
* Initialize & return a new transaction*/
Transaction createTransaction(char *transactionStr) {
Transaction transaction = NULL;
/* Initialize t to avoid valgrind errors as described here:
* https://stackoverflow.com/questions/9037631/valgrind-complaining-about-mktime-is-that-my-fault
* by freitass's comment.*/
struct tm t = {0};
int x = 0, q = 0, s = 0;
char buf[BUFFER_SIZE];
s = sscanf(transactionStr, "%s %s %s %d %d-%d-%d %d:%d;", buf, buf, buf, &q, &q, &q, &q, &q, &q);
if (s >= 4 && s <= 9) {
transaction = (Transaction) malloc(sizeof(struct Transaction));
if (transaction != NULL) {
transactionStr = strtok(transactionStr, " "); // TransactionId
transaction->transactionId = malloc(strlen(transactionStr) * sizeof(char *) + 1);
if (transaction->transactionId) {
strcpy(transaction->transactionId, transactionStr);
}
transactionStr = strtok(NULL, " "); // SenderWalletId
transaction->senderWalletId = malloc(strlen(transactionStr) * sizeof(char *) + 1);
if (transaction->senderWalletId != NULL) {
strcpy(transaction->senderWalletId, transactionStr);
}
transactionStr = strtok(NULL, " "); // RecieverWalletId
transaction->receiverWalletId = malloc(strlen(transactionStr) * sizeof(char *) + 1);
if (transaction->receiverWalletId != NULL) {
strcpy(transaction->receiverWalletId, transactionStr);
}
transactionStr = strtok(NULL, " "); // Amount
transaction->value = (unsigned long int) strtol(transactionStr, NULL, 10);
transactionStr = strtok(NULL, "\n"); // Date time
if (transactionStr != NULL) {
/*Parse date & time*/
x = sscanf(transactionStr, "%d-%d-%d %d:%d",
&t.tm_mday, &t.tm_mon, &t.tm_year, &t.tm_hour, &t.tm_min
);
if (x == 5) {
t.tm_year = t.tm_year - 1900;
t.tm_mon = t.tm_mon - 1;
t.tm_isdst = -1;
transaction->timestamp = mktime(&t);
if (transaction->timestamp < 0) {
fprintf(stdout, "\nBad datetime!\n");
return NULL;
}
} else {
time(&transaction->timestamp);
}
} else {
time(&transaction->timestamp);
}
}
}
return transaction;
}
/* @Callback
* Compare transaction*/
int cmpTransaction(Transaction transaction, char *transactionStr) {
int result = 0;
char *token = malloc(strlen(transactionStr) * sizeof(char *) + 1);
strcpy(token, transactionStr);
token = strtok(token, " ");
result = strcmp(transaction->transactionId, token);
free(token);
return result;
}
/* @Callback
* Hash function for transactions*/
unsigned long int transactionHash(char *key, unsigned long int capacity) {
int i, sum = 0;
size_t keyLength = strlen(key);
for (i = 0; i < keyLength; i++) {
sum += key[i];
}
return sum % capacity;
}
/* @Callback
* Destroy transaction*/
void destroyTransaction(Transaction transaction) {
free(transaction->transactionId);
free(transaction->senderWalletId);
free(transaction->receiverWalletId);
free(transaction);
}
/* @Callback
* Initialize & return a new transaction list*/
List createTransactionList(char *userId) {
List list = NULL;
listCreate(&list, userId);
return list;
}
/* @Callback
* Compare keys function for transaction lists hashtable*/
int cmpTransactionList(List tr1, char *userId) {
return strcmp(listGetIdentifier(tr1), userId);
}
/* @Callback
* Destroy function for transaction hashtable*/
void destroyTransactionList(List list) {
listDestroy(&list);
}