编程作业 Scrooge Coin

这是我做过的最糟糕的任务。

TxHandler.java无法构建和运行,一切都要猜测。由于我对JAVA不熟悉,提交后遇到了上百万的语法错误。

我必须:

  • 手动导入软件包
  • 每行后加”;”。
  • if之后的()
  • 声明变量类型

这就是为什么我讨厌 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
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);
}

}