Skip to content
Snippets Groups Projects
Commit f45af81d authored by Luca Keidel's avatar Luca Keidel
Browse files

Provisional fix of double spending/agreeing

Before it was possible that a wallet was able to spend more money than it has. The cause were two pending distributed commits in the system at the same time. Since the wallets only update their ledger when the supervisor sends the commit message, they also agreed to the second request because their ledger was not updated. However, the second request could exceed the rest amount of FUCs in the ledger
parent 89c2be1f
No related branches found
No related tags found
1 merge request!5Configuration system
...@@ -18,7 +18,7 @@ import java.util.List; ...@@ -18,7 +18,7 @@ import java.util.List;
public class Main { public class Main {
private static int numberOfWallets = 2; private static int numberOfWallets = 2;
private static boolean createGUI = false; private static boolean createGUI = true;
private static ActorSystem cSystem; private static ActorSystem cSystem;
......
...@@ -42,11 +42,6 @@ public class ActionCommitDistributedCommittedTransfer extends ClientAction { ...@@ -42,11 +42,6 @@ public class ActionCommitDistributedCommittedTransfer extends ClientAction {
wallet.addLogMsg("ActionCommitDistributedCommittedTransfer is granted? " + granted); wallet.addLogMsg("ActionCommitDistributedCommittedTransfer is granted? " + granted);
if (granted) { if (granted) {
Integer sourceAmount = wallet.amounts.getOrDefault(source, 0);
Integer targetAmount = wallet.amounts.getOrDefault(target, 0);
wallet.amounts.put(source, sourceAmount - amount);
wallet.amounts.put(target, targetAmount + amount);
if (source.compareTo(self) == 0) { if (source.compareTo(self) == 0) {
wallet.setAmount(wallet.getAmount() - amount); wallet.setAmount(wallet.getAmount() - amount);
wallet.addTransactionLogMessageSuccess("Sent " + amount + " FUC to " + target.path().name()); wallet.addTransactionLogMessageSuccess("Sent " + amount + " FUC to " + target.path().name());
...@@ -58,6 +53,12 @@ public class ActionCommitDistributedCommittedTransfer extends ClientAction { ...@@ -58,6 +53,12 @@ public class ActionCommitDistributedCommittedTransfer extends ClientAction {
} else { } else {
wallet.addLogMsg("abort transaction with id" + id); wallet.addLogMsg("abort transaction with id" + id);
// rollback
Integer sourceAmount = wallet.amounts.getOrDefault(source, 0);
Integer targetAmount = wallet.amounts.getOrDefault(target, 0);
wallet.amounts.put(source, sourceAmount + amount);
wallet.amounts.put(target, targetAmount - amount);
if (source.compareTo(self) == 0) { if (source.compareTo(self) == 0) {
wallet.addTransactionLogMessageFail("Failed to send " + amount + " FUC to " + target.path().name() + " (Commit has not been granted)"); wallet.addTransactionLogMessageFail("Failed to send " + amount + " FUC to " + target.path().name() + " (Commit has not been granted)");
} }
......
...@@ -32,6 +32,13 @@ public class ActionPrepareDistributedCommittedTransfer extends Transaction { ...@@ -32,6 +32,13 @@ public class ActionPrepareDistributedCommittedTransfer extends Transaction {
//sender have enough money //sender have enough money
&& wallet.amounts.getOrDefault(source, 0) >= amount)); && wallet.amounts.getOrDefault(source, 0) >= amount));
// precautionly update own ledger to prevent double spending (respectively agreeing)
Integer sourceAmount = wallet.amounts.getOrDefault(source, 0);
Integer targetAmount = wallet.amounts.getOrDefault(target, 0);
wallet.amounts.put(source, sourceAmount - amount);
wallet.amounts.put(target, targetAmount + amount);
sender.tell(new ActionPrepareDistributedCommittedTransferAnswer(source, target, amount, timestamp, granted, id), self); sender.tell(new ActionPrepareDistributedCommittedTransferAnswer(source, target, amount, timestamp, granted, id), self);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment