Repository navigation
Expand file tree
/
Copy pathBlockChain.java
More file actions
executable file
·200 lines (175 loc) · 7.24 KB
/
Copy pathBlockChain.java
File metadata and controls
executable file
·200 lines (175 loc) · 7.24 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Block Chain should maintain only limited block nodes to satisfy the functions
// You should not have all the blocks added to the block chain in memory
// as it would cause a memory overflow.
import java.util.HashMap;
import java.util.ArrayList;
public class BlockChain {
public static final int CUT_OFF_AGE = 10;
private Integer currentHeight = 0;
HashMap<Integer, ArrayList<Block>> heightBlockMap;
HashMap<ByteArrayWrapper, BlockModel> hashBlockMap;
TransactionPool txPool;
/**
* create an empty block chain with just a genesis block. Assume {@code genesisBlock} is a valid
* block
*/
public BlockChain(Block genesisBlock) {
// IMPLEMENT THIS
heightBlockMap = new HashMap<Integer, ArrayList<Block>>();
hashBlockMap = new HashMap<ByteArrayWrapper, BlockModel>();
txPool = new TransactionPool();
addBlock(genesisBlock);
}
/** Get the maximum height block */
public Block getMaxHeightBlock() {
// IMPLEMENT THIS
ArrayList<Block> blockList = heightBlockMap.get(currentHeight);
if (blockList != null) {
Block maxHeightBlock = blockList.get(0);
return maxHeightBlock;
}
return null;
}
/** Get the UTXOPool for mining a new block on top of max height block */
public UTXOPool getMaxHeightUTXOPool() {
// IMPLEMENT THIS
ArrayList<Block> blockList = heightBlockMap.get(currentHeight);
if(blockList != null) {
Block maxHeightBlock = blockList.get(0);
ByteArrayWrapper maxHeightBlockHash = new ByteArrayWrapper(maxHeightBlock.getHash());
BlockModel blockModel = hashBlockMap.get(maxHeightBlockHash);
return blockModel.utxoPool;
}
return null;
}
/** Get the transaction pool to mine a new block */
public TransactionPool getTransactionPool() {
// IMPLEMENT THIS
return txPool;
}
/**
* Add {@code block} to the block chain if it is valid. For validity, all transactions should be
* valid and block should be at {@code height > (maxHeight - CUT_OFF_AGE)}.
*
* <p>
* For example, you can try creating a new block over the genesis block (block height 2) if the
* block chain height is {@code <=
* CUT_OFF_AGE + 1}. As soon as {@code height > CUT_OFF_AGE + 1}, you cannot create a new block
* at height 2.
*
* @return true if block is successfully added
*/
public boolean addBlock(Block block) {
// Genesis block check in a block chain where blocks exist.
byte[] parentHash = block.getPrevBlockHash();
if(currentHeight != 0 && parentHash == null) {
return false;
}
// Process the genesis block
if(parentHash == null) {
// Genesis block. Add it to the blockChain and return.
ArrayList<Block> blockList = new ArrayList<>();
blockList.add(block);
heightBlockMap.put(1, blockList);
UTXOPool uPool = createUTXOPoolForGenesisBlock(block);
hashBlockMap.put(new ByteArrayWrapper(block.getHash()), new BlockModel(block, 1, uPool));
currentHeight++;
return true;
}
BlockModel parentBlockModel = hashBlockMap.get(new ByteArrayWrapper(block.getPrevBlockHash()));
if(parentBlockModel == null)
return false;
// Verify that incoming block is valid.
UTXOPool uPoolAfterBlockAddition = verifyBlock(block, parentBlockModel.utxoPool);
if(uPoolAfterBlockAddition == null)
return false;
// If this is a newly found block corresponding to the root we are on..
Integer blockHeight = parentBlockModel.height + 1;
if(blockHeight > currentHeight) {
// This block is going to be the longest valid branch.
// Remove all transactions from the txPool which are present in this block.
currentHeight = blockHeight;
removeTxsFromTxPool(block);
}
// Insert block in the heightBlock Map
ArrayList<Block> blockHeightArrayList = heightBlockMap.get(blockHeight);
if(blockHeightArrayList == null) {
blockHeightArrayList = new ArrayList<>();
}
blockHeightArrayList.add(block);
heightBlockMap.put(blockHeight, blockHeightArrayList);
// Add coinbase transaction to the uPoolAfterBlockAddition
int idx = 0;
Transaction coinbaseTx = block.getCoinbase();
ArrayList<Transaction.Output> coinbaseOutputs = coinbaseTx.getOutputs();
for(Transaction.Output output : coinbaseOutputs) {
UTXO utxo = new UTXO(coinbaseTx.getHash(), idx);
uPoolAfterBlockAddition.addUTXO(utxo, output);
idx++;
}
// Insert BlockModel in hashBlock Map
BlockModel currentBlockModel = new BlockModel(block, blockHeight, uPoolAfterBlockAddition);
hashBlockMap.put(new ByteArrayWrapper(block.getHash()), currentBlockModel);
removeBlocksLowerThanCutoff();
return true;
}
private void removeTxsFromTxPool(Block block) {
ArrayList<Transaction> txList = block.getTransactions();
for(Transaction tx : txList) {
txPool.removeTransaction(tx.getHash());
}
}
private UTXOPool createUTXOPoolForGenesisBlock(Block block) {
// Genesis block. Add all outputs to utxoPool.
UTXOPool uPool = new UTXOPool();
Transaction coinbaseTx = block.getCoinbase();
int idx = 0;
for(Transaction.Output output : coinbaseTx.getOutputs()) {
UTXO utxo = new UTXO(coinbaseTx.getHash(), idx);
uPool.addUTXO(utxo, output);
idx++;
}
return uPool;
}
private void removeBlocksLowerThanCutoff() {
Integer removeHeight = currentHeight - CUT_OFF_AGE - 1;
if(removeHeight <= 0)
return;
removeBlockWithHeight(removeHeight);
}
private void removeBlockWithHeight(Integer height) {
// First remove blocks from hashBlockMap
ArrayList<Block> blockList = heightBlockMap.get(height);
if(blockList == null)
return;
for(Block block : blockList) {
ByteArrayWrapper blockHash = new ByteArrayWrapper(block.getHash());
hashBlockMap.remove(blockHash);
}
heightBlockMap.remove(height);
}
private UTXOPool verifyBlock(Block block, UTXOPool uPool) {
TxHandler txHandler = new TxHandler(uPool);
ArrayList<Transaction> txList = block.getTransactions();
Transaction[] txArray = txList.toArray(new Transaction[txList.size()]);
Transaction[] acceptedTxs = txHandler.handleTxs(txArray);
if(acceptedTxs.length != txList.size())
return null;
return txHandler.getUTXOPool();
}
/** Add a transaction to the transaction pool */
public void addTransaction(Transaction tx) {
// IMPLEMENT THIS
txPool.addTransaction(tx);
}
private class BlockModel {
Block block;
Integer height;
UTXOPool utxoPool;
public BlockModel(Block block, Integer height, UTXOPool utxoPool) {
this.block = block;
this.height = height;
this.utxoPool = utxoPool;
}
}
}