Programming Assignment - Scrooge Coin

This is the worst task I have ever done.

TxHandler.java cannot be built and run, everything is a guess. Because I am unfamiliar with JAVA, I encountered millions of syntax errors after submitting.

I have to:

  • Manually import packages
  • Add “;” after each line
  • Add “()” after “if”
  • Declare variable types

This is why I hate 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
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

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) {
UTXOPool utxoSet = new UTXOPool();
double pSum = 0;
double sum = 0;

for (int i = 0; i < tx.numInputs(); i++) {
Transaction.Input in = tx.getInput(i);
UTXO u = new UTXO(in.prevTxHash, in.outputIndex);
Transaction.Output out = utxoPool.getTxOutput(u);

if ((!utxoPool.contains(u)) || (!Crypto.verifySignature(out.address, tx.getRawDataToSign(i), in.signature)) || (utxoSet.contains(u))) {
return false;
}

utxoSet.addUTXO(u, out);
pSum += out.value;
}

for (Transaction.Output out : tx.getOutputs()) {
if (out.value < 0) {
return false;
}
sum += out.value;
}

if (pSum < sum) {
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) {
Set<Transaction> vTxs = new HashSet<>();

for (Transaction tx : possibleTxs) {
if (isValidTx(tx)) {
vTxs.add(tx);

for (Transaction.Input in : tx.getInputs()) {
UTXO u = new UTXO(in.prevTxHash, in.outputIndex);
utxoPool.removeUTXO(u);
}

for (int i = 0; i < tx.numOutputs(); i++) {
Transaction.Output out = tx.getOutput(i);
UTXO u = new UTXO(tx.getHash(), i);
utxoPool.addUTXO(u, out);
}
}
}

Transaction[] vTxArr = new Transaction[vTxs.size()];
return vTxs.toArray(vTxArr);
}

}

Translated by gpt-3.5-turbo