-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockChain.java
More file actions
181 lines (149 loc) · 5.61 KB
/
BlockChain.java
File metadata and controls
181 lines (149 loc) · 5.61 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
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
/* Block Chain should maintain only limited block nodes to satisfy the functions
You should not have the all the blocks added to the block chain in memory
as it would overflow memory
*/
public class BlockChain {
public static final int CUT_OFF_AGE = 10;
private ByteArrayWrapper oldest;
private ByteArrayWrapper newest;
private HashMap<ByteArrayWrapper, BlockNode> blocks;
private TransactionPool txPool;
private int maxHeight;
// all information required in handling a block in block chain
private class BlockNode {
public Block b;
public BlockNode parent;
public ArrayList<BlockNode> children;
public int height;
// utxo pool for making a new block on top of this block
private UTXOPool uPool;
public BlockNode(Block b, BlockNode parent, UTXOPool uPool) {
this.b = b;
this.parent = parent;
children = new ArrayList<BlockNode>();
this.uPool = uPool;
if (parent != null) {
height = parent.height + 1;
parent.children.add(this);
} else {
height = 1;
}
}
public UTXOPool getUTXOPoolCopy() {
return new UTXOPool(uPool);
}
}
/* create an empty block chain with just a genesis block.
* Assume genesis block is a valid block
*/
public BlockChain(Block genesisBlock) {
TxHandler handler = new TxHandler(new UTXOPool());
ArrayList<Transaction> transArr = new ArrayList<Transaction>();
Transaction coinbase = genesisBlock.getCoinbase();
//Transaction[] genTrans = transArr.toArray(new Transaction[1]);
//Transaction[] tx = handler.handleTxs(genTrans);
//System.out.println(tx.length);
//UTXOPool pool = handler.getUTXOPool();
UTXOPool pool = new UTXOPool();
UTXO coinbaseUTXO = new UTXO(coinbase.getHash(), 0);
pool.addUTXO(coinbaseUTXO, coinbase.getOutput(0));
BlockNode node = new BlockNode(genesisBlock, null, pool);
node.height = 0;
this.maxHeight = node.height;
this.oldest = new ByteArrayWrapper(genesisBlock.getHash());
this.newest = this.oldest;
this.blocks = new HashMap<ByteArrayWrapper, BlockNode>();
this.blocks.put(this.oldest, node);
this.txPool = new TransactionPool();
}
private BlockNode getMaxHeightBlockNode() {
return this.blocks.get(this.newest);
}
private int getMaxHeight() {
return getMaxHeightBlockNode().height;
}
/* Get the maximum height block
*/
public Block getMaxHeightBlock() {
return this.getMaxHeightBlockNode().b;
}
/* Get the UTXOPool for mining a new block on top of
* max height block
*/
public UTXOPool getMaxHeightUTXOPool() {
return this.getMaxHeightBlockNode().uPool;
}
/* Get the transaction pool to mine a new block
*/
public TransactionPool getTransactionPool() {
return this.txPool;
}
/* Add a block to block chain if it is valid.
* For validity, all transactions should be valid
* and block should be at height > (maxHeight - CUT_OFF_AGE).
* For example, you can try creating a new block over genesis block
* (block height 2) if blockChain height is <= CUT_OFF_AGE + 1.
* As soon as height > CUT_OFF_AGE + 1, you cannot create a new block at height 2.
* Return true of block is successfully added
*/
public boolean addBlock(Block b) {
if (b.getPrevBlockHash() == null) {
return false;
}
ByteArrayWrapper prevHash = new ByteArrayWrapper(b.getPrevBlockHash());
BlockNode parent = blocks.get(prevHash);
if (parent == null) {
return false;
}
UTXOPool currPool = parent.getUTXOPoolCopy();
TxHandler handler = new TxHandler(currPool);
ArrayList<Transaction> allTrans = b.getTransactions();
//ArrayList<Transaction> poolTxs = getTransactionPool().getTransactions();
//if (poolTxs.size() > 0) {
// allTrans.addAll(getTransactionPool().getTransactions());
// txPool = new TransactionPool();
//}
int length = allTrans.size();
Transaction[] accepted = handler.handleTxs(allTrans.toArray(new Transaction[length]));
if (accepted.length != length)
return false;
UTXOPool pool = handler.getUTXOPool();
Transaction coinbase = b.getCoinbase();
UTXO coinbaseUTXO = new UTXO(coinbase.getHash(), 0);
pool.addUTXO(coinbaseUTXO, coinbase.getOutput(0));
BlockNode node = new BlockNode(b, parent, pool);
node.height = parent.height + 1;
if (node.height <= maxHeight - CUT_OFF_AGE) {
return false;
}
ByteArrayWrapper hash = new ByteArrayWrapper(b.getHash());
this.blocks.put(hash, node);
if (node.height > this.maxHeight) {
this.maxHeight = node.height;
this.newest = hash;
}
return true;
}
private boolean isValid(Block b) {
ByteArrayWrapper prevHash = new ByteArrayWrapper(b.getPrevBlockHash());
if (!this.blocks.containsKey(prevHash)) {
return false;
}
BlockNode parent = this.blocks.get(prevHash);
int thisHeight = parent.height + 1;
if (thisHeight <= this.getMaxHeight() - this.CUT_OFF_AGE) {
return false;
}
UTXOPool pool = parent.uPool;
TxHandler handler = new TxHandler(pool);
return true;
}
/* Add a transaction in transaction pool
*/
public void addTransaction(Transaction tx) {
this.txPool.addTransaction(tx);
}
}