-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.c
67 lines (59 loc) · 2.89 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
#include "transaction.h"
#include "operation.h"
#include "occ.h"
bool withdraw_account(BankAccount *bankAccount, uint64_t amount, OCCContainer *occContainer, uint16_t numberTries) {
while (numberTries > 0) {
uint64_t startTime = get_start_time(&(occContainer->validationList));
BankAccountData bankAccountData;
get_shadow_copy(bankAccount, &bankAccountData);
if (!debit_account(&bankAccountData, amount)) return false;
// TODO: Add transaction details
ValidationRequest validationRequest = {.bankAccountData1 = &bankAccountData, .bankAccountData2 = NULL,
.startTime = startTime, .isBankAccount1Updated = true, .isBankAccount2Updated = false};
if (submit_validation_request(&validationRequest,
&occContainer->validationCircularQueue))
return true;
numberTries--;
}
return false;
}
bool deposit_account(BankAccount *bankAccount, uint64_t amount, OCCContainer *occContainer, uint16_t numberTries) {
while (numberTries > 0) {
uint64_t startTime = get_start_time(&(occContainer->validationList));
BankAccountData bankAccountData;
get_shadow_copy(bankAccount, &bankAccountData);
if (!credit_account(&bankAccountData, amount)) return false;
// TODO: Add transaction details
ValidationRequest validationRequest = {.bankAccountData1 = &bankAccountData, .bankAccountData2 = NULL,
.startTime = startTime, .isBankAccount1Updated = true, .isBankAccount2Updated = false};
if (submit_validation_request(&validationRequest,
&occContainer->validationCircularQueue))
return true;
numberTries--;
}
return false;
}
bool pay_account(BankAccount *bankAccountFrom, BankAccount *bankAccountTo, uint64_t amount, OCCContainer *occContainer,
uint16_t numberTries) {
while (numberTries > 0) {
uint64_t startTime = get_start_time(&(occContainer->validationList));
BankAccountData bankAccountDataFrom;
BankAccountData bankAccountDataTo;
get_shadow_copy(bankAccountFrom, &bankAccountDataFrom);
get_shadow_copy(bankAccountTo, &bankAccountDataTo);
if (!debit_account(&bankAccountDataFrom, amount)) return false;
if (!credit_account(&bankAccountDataTo, amount)) return false;
ValidationRequest validationRequest = {
.bankAccountData1 = &bankAccountDataFrom,
.bankAccountData2 = &bankAccountDataTo,
.startTime = startTime,
.isBankAccount1Updated = true,
.isBankAccount2Updated = true};
// TODO: Add transaction details
if (submit_validation_request(&validationRequest,
&occContainer->validationCircularQueue))
return true;
numberTries--;
}
return false;
}