加密货币第一课作业Scrooge Coin

比特币和数字货币技术
https://www.coursera.org/learn/cryptocurrency/

奴才币(ScroogeCoin)中奴才的指定实体将负责公布包含所有发生过的交易历史记录和仅曾账目(append-only ledger)。我们将需要实现奴才处理交易维护账簿的逻辑。奴才可以建立一个区块链,对于区块链奴才需要进行数字签名,因此,形成一系列都包含多笔交易的区块。奴才将会收到多笔交易,并验证交易合法性返回正确的交易

关于校验提供了以下几个标准:

  1. 同一个块中的交易可以依赖于另一个,也就是说不能独立的验证每一笔交易。
  2. 避免双花问题,也就是大于等于两个交易使用了相同的output
  3. 当前所使用的output未被使用。
  4. 多有的output均为正数。
  5. 每个input上的签名都是有效的
  6. 所有input总和 > output

解决方案: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
100
101
102
103
104
105
106
107
108
109
110
	import java.util.ArrayList;


public class TxHandler {

/**
* 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.
*/

private UTXOPool utxoPool;

public TxHandler(UTXOPool utxoPool) {
// IMPLEMENT THIS
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> inputs = tx.getInputs();
ArrayList<Transaction.Output> outputs = tx.getOutputs();
//2-存储已经使用过的utxo,避免双发花问题.
ArrayList<UTXO> utxoUsed = new ArrayList<UTXO>();

double sumOutput = 0;
double sumInput = 0;

//遍历交易中的input实现签名的逐一校验
for (int i = 0; i < inputs.size() ; i++){
//根据input中存储的 上一笔交易的哈希值 在UTXOPool 中找到对应的output
UTXO u = new UTXO(inputs.get(i).prevTxHash, inputs.get(i).outputIndex);
Transaction.Output output = utxoPool.getTxOutput(u);

if(output==null){return false;}
//5
if(!Crypto.verifySignature(output.address,
tx.getRawDataToSign(i),
inputs.get(i).signature)){
return false;
}
//2
if(utxoUsed.contains(u))return false;
utxoUsed.add(u);

sumInput += output.value;
}

if (outputs == null) {
return false;
}

for (int i = 0; i < outputs.size(); i++) {
//4
if(outputs.get(i).value<0)return false;
sumOutput += outputs.get(i).value;
}


//6
if(sumInput < sumOutput){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> txs = new ArrayList<>();
boolean foundtx = false;
//1-存在互相依赖不可以单独校验一次
do {
foundtx = false;
for (Transaction tx : possibleTxs) {
if(txs.contains(tx))continue;
if (isValidTx(tx)) {
foundtx = true;
txs.add(tx);
// 从UTXOPool中移除已花费的 即所有input
for (Transaction.Input in : tx.getInputs()) {
UTXO utxo = new UTXO(in.prevTxHash, in.outputIndex);
utxoPool.removeUTXO(utxo);
}
// 向UTXOPool 中添加新的utxo 即output
int idx = 0;
for (Transaction.Output out : tx.getOutputs()) {
UTXO utxo = new UTXO(tx.getHash(), idx++);
utxoPool.addUTXO(utxo, out);
}
}
}
}while (foundtx);
return txs.toArray(new Transaction[txs.size()]);
}


}
坚持原创技术分享,记录点滴成长历程!