-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTxHandler.java
executable file
·99 lines (88 loc) · 3.79 KB
/
TxHandler.java
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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class TxHandler {
private UTXOPool utxoPool;
/**
* Creates a public ledger whose current UTXOPool (collection of unspent transaction outputs) is
* {@code utxoPool}. This should make a copy of utxoPool by using the UTXOPool(UTXOPool uPool)
* constructor.
*/
public TxHandler(UTXOPool utxoPool) {
this.utxoPool = new UTXOPool(utxoPool);
}
/**
* @return true if:
* (1) all outputs claimed by {@code tx} are in the current UTXO pool,
* (2) the signatures on each input of {@code tx} are valid,
* (3) no UTXO is claimed multiple times by {@code tx},
* (4) all of {@code tx}s output values are non-negative, and
* (5) the sum of {@code tx}s input values is greater than or equal to the sum of its output
* values; and false otherwise.
*/
public boolean isValidTx(Transaction tx) {
// IMPLEMENT THIS
ArrayList<Transaction.Input> txInputs = tx.getInputs();
ArrayList<Transaction.Output> txOutputs = tx.getOutputs();
ArrayList<UTXO> allCurrentUTXOs = this.utxoPool.getAllUTXO();
double totalOfInputValues = 0.0;
double totalOfOutputValues = 0.0;
for (int i=0; i < txInputs.size(); i++) {
Transaction.Input currentInput = tx.getInput(i);
UTXO currentUTXO = new UTXO(currentInput.prevTxHash, currentInput.outputIndex);
if (!allCurrentUTXOs.contains(currentUTXO))
return false;
allCurrentUTXOs.remove(currentUTXO);
Transaction.Output prevOutput = this.utxoPool.getTxOutput(currentUTXO);
if (!Crypto.verifySignature(prevOutput.address, tx.getRawDataToSign(i), currentInput.signature))
return false;
totalOfInputValues += prevOutput.value;
}
for (Transaction.Output currentOutput : txOutputs) {
if (currentOutput.value < 0)
return false;
totalOfOutputValues += currentOutput.value;
}
if (totalOfInputValues < totalOfOutputValues)
return false;
return true;
}
/**
* Handles each epoch by receiving an unordered array of proposed transactions, checking each
* transaction for correctness, returning a mutually valid array of accepted transactions, and
* updating the current UTXO pool as appropriate.
*/
public Transaction[] handleTxs(Transaction[] possibleTxs) {
// IMPLEMENT THIS
ArrayList<Transaction> validTxns = new ArrayList<>();
ArrayList<Transaction> tempInvalidTxns = new ArrayList<>(Arrays.asList(possibleTxs));
boolean txnTurnedValid = true;
while (txnTurnedValid) {
txnTurnedValid = false;
List<Transaction> toBeRemoved = new ArrayList<>();
for (Transaction txn : tempInvalidTxns) {
if (isValidTx(txn)) {
validTxns.add(txn);
adjustTxnPool(txn);
txnTurnedValid = true;
toBeRemoved.add(txn);
}
}
tempInvalidTxns.removeAll(toBeRemoved);
}
Transaction[] finalTxns = new Transaction[validTxns.size()];
finalTxns = validTxns.toArray(finalTxns);
return finalTxns;
}
private void adjustTxnPool(Transaction txn) {
ArrayList<Transaction.Input> inputs = txn.getInputs();
for (Transaction.Input input : inputs) {
UTXO utxo = new UTXO(input.prevTxHash, input.outputIndex);
this.utxoPool.removeUTXO(utxo);
}
for (int i=0; i < txn.numOutputs(); i++) {
Transaction.Output output = txn.getOutput(i);
this.utxoPool.addUTXO(new UTXO(txn.getHash(), i), output);
}
}
}