-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlockHandler.java
executable file
·47 lines (40 loc) · 1.48 KB
/
BlockHandler.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
import java.security.PublicKey;
public class BlockHandler {
private BlockChain blockChain;
/** assume blockChain has the genesis block */
public BlockHandler(BlockChain blockChain) {
this.blockChain = blockChain;
}
/**
* add {@code block} to the block chain if it is valid.
*
* @return true if the block is valid and has been added, false otherwise
*/
public boolean processBlock(Block block) {
if (block == null)
return false;
return blockChain.addBlock(block);
}
/** create a new {@code block} over the max height {@code block} */
public Block createBlock(PublicKey myAddress) {
Block parent = blockChain.getMaxHeightBlock();
byte[] parentHash = parent.getHash();
Block current = new Block(parentHash, myAddress);
UTXOPool uPool = blockChain.getMaxHeightUTXOPool();
TransactionPool txPool = blockChain.getTransactionPool();
TxHandler handler = new TxHandler(uPool);
Transaction[] txs = txPool.getTransactions().toArray(new Transaction[0]);
Transaction[] rTxs = handler.handleTxs(txs);
for (int i = 0; i < rTxs.length; i++)
current.addTransaction(rTxs[i]);
current.finalize();
if (blockChain.addBlock(current))
return current;
else
return null;
}
/** process a {@code Transaction} */
public void processTx(Transaction tx) {
blockChain.addTransaction(tx);
}
}