-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMaxFeeTxHandler.java
103 lines (93 loc) · 3.89 KB
/
MaxFeeTxHandler.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
100
101
102
103
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
public class MaxFeeTxHandler {
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 MaxFeeTxHandler(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) {
UTXOPool uniqueUtxos = new UTXOPool();
double previousTxOutSum = 0;
double currentTxOutSum = 0;
for (int i = 0; i < tx.numInputs(); i++) {
Transaction.Input in = tx.getInput(i);
UTXO utxo = new UTXO(in.prevTxHash, in.outputIndex);
Transaction.Output output = utxoPool.getTxOutput(utxo);
if (!utxoPool.contains(utxo)) return false;
if (!Crypto.verifySignature(output.address, tx.getRawDataToSign(i), in.signature))
return false;
if (uniqueUtxos.contains(utxo)) return false;
uniqueUtxos.addUTXO(utxo, output);
previousTxOutSum += output.value;
}
for (Transaction.Output out : tx.getOutputs()) {
if (out.value < 0) return false;
currentTxOutSum += out.value;
}
return previousTxOutSum >= currentTxOutSum;
}
private double calcTxFees(Transaction tx) {
double sumInputs = 0;
double sumOutputs = 0;
for (Transaction.Input in : tx.getInputs()) {
UTXO utxo = new UTXO(in.prevTxHash, in.outputIndex);
if (!utxoPool.contains(utxo) || !isValidTx(tx)) continue;
Transaction.Output txOutput = utxoPool.getTxOutput(utxo);
sumInputs += txOutput.value;
}
for (Transaction.Output out : tx.getOutputs()) {
sumOutputs += out.value;
}
return sumInputs - sumOutputs;
}
/**
* 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) {
Set<Transaction> txsSortedByFees = new TreeSet<>(new Comparator<Transaction>() {
@Override
public int compare(Transaction tx1, Transaction tx2) {
double tx1Fees = calcTxFees(tx1);
double tx2Fees = calcTxFees(tx2);
return Double.valueOf(tx2Fees).compareTo(tx1Fees);
}
});
Collections.addAll(txsSortedByFees, possibleTxs);
Set<Transaction> acceptedTxs = new HashSet<>();
for (Transaction tx : txsSortedByFees) {
if (isValidTx(tx)) {
acceptedTxs.add(tx);
for (Transaction.Input in : tx.getInputs()) {
UTXO utxo = new UTXO(in.prevTxHash, in.outputIndex);
utxoPool.removeUTXO(utxo);
}
for (int i = 0; i < tx.numOutputs(); i++) {
Transaction.Output out = tx.getOutput(i);
UTXO utxo = new UTXO(tx.getHash(), i);
utxoPool.addUTXO(utxo, out);
}
}
}
Transaction[] validTxArray = new Transaction[acceptedTxs.size()];
return acceptedTxs.toArray(validTxArray);
}
}