-
Notifications
You must be signed in to change notification settings - Fork 0
/
TxHandler.java
63 lines (53 loc) · 1.97 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
import java.util.ArrayList;
import java.util.HashMap;
import Transaction.Output;
public class TxHandler {
public 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) {
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) {
// consumed cons valid
// not already consumed
// total value out = total value in
// signed by owners of all consumed coins
Boolean isValid = false;
Map<int, List<Output>> validRoots = new HashMap<>(); // Java 7
for (Transaction.Input i : tx.getInputs())
{
if(!UTXOPool.contains(utxo))
}
return isValid;
}
/**
* 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> validTxs = new ArrayList<Transaction>();
for (Transaction t : possibleTxs)
{
if (isValidTx(t))
validTxs.add(t);
}
Transaction[] validTxsArray = new Transaction[validTxs.size()];
validTxsArray = validTxs.toArray(validTxsArray);
return validTxsArray;
}
}