-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlockChain.java
executable file
·109 lines (89 loc) · 3.31 KB
/
BlockChain.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
package blockchain;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
class BlockChain implements Serializable {
private long blockCount = 0;
private final ArrayList<Block> blocks = new ArrayList<>();
private int zeroes = 0;
private static long timeStamp;
private static final BlockChain INSTANCE = new BlockChain();
private static final int HARDSHIP_MINIMUM_SECOND =10;
private static final int HARDSHIP_MAXIMUM_SECOND =30;
public int getZeroes() {
return zeroes;
}
public ArrayList<Block> getBlocks() {
return blocks;
}
public String getLastBlockHash() {
return blockCount > 0 ? blocks.get((int) (blockCount - 1)).getHash() : "0";
}
public long getBlockCount() {
return blockCount;
}
private BlockChain() {
timeStamp = new Date().getTime();
}
public static BlockChain getInstance() {
return INSTANCE;
}
public void saveBlockChain(String blockChainFile) throws IOException {
SerializationUtils.serialize(INSTANCE, blockChainFile);
//Can be more optimal
}
public void loadBlockChain(String blockChainFile) throws IOException, ClassNotFoundException {
File file = new File(blockChainFile);
if (file.exists() & !file.isDirectory()) {
BlockChain tempChain = (SerializationUtils.deserialize(blockChainFile));
INSTANCE.blocks.addAll(tempChain.getBlocks());
INSTANCE.zeroes = tempChain.getZeroes();
INSTANCE.blockCount = tempChain.getBlockCount();
timeStamp = new Date().getTime();
}
}
private void addBlock(Block block) {
blocks.add(block);
blockCount++;
}
public synchronized boolean validateBlock(Block block, int minerId) {
if (block.getHash().startsWith("0".repeat(zeroes)))
if (block.getPreHash().equals(getLastBlockHash())) {
addBlock(block);
System.out.println("BLock:" + "\nCreated by miner # " + minerId + "\n" + block);
checkProofOfWork();
return true;
}
return false;
}
private static void checkProofOfWork() {
long timeTaken = (new Date().getTime() - timeStamp) / 1000;
System.out.println("Block was generating for " + timeTaken + "seconds");
timeStamp = new Date().getTime();
if (timeTaken < HARDSHIP_MINIMUM_SECOND) {
INSTANCE.zeroes++;
System.out.println("N increased to " + INSTANCE.zeroes + "\n");
} else if (timeTaken > HARDSHIP_MAXIMUM_SECOND) {
INSTANCE.zeroes--;
System.out.println("N decreased to " + INSTANCE.zeroes + "\n");
} else
System.out.println("N stayed the same" + "\n");
}
public boolean validate() {
String preHash = "0";
for (Block block : blocks) {
if (!block.getPreHash().equals(preHash)) {
return false;
}
preHash = block.getHash();
}
return true;
}
public void print() {
for (Block block : blocks) {
System.out.println(block);
}
}
}