-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCyberChain.java
52 lines (42 loc) · 1.92 KB
/
CyberChain.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
import java.util.ArrayList;
import com.google.gson.GsonBuilder;
public class CyberChain {
public static ArrayList<Block> blockchain = new ArrayList<Block>();
public static int difficulty = 5;
public static void main(String[] args) {
blockchain.add(new Block("First Block", "0"));
System.out.println("Trying to mine block 1...");
blockchain.get(0).mineBlock(difficulty);
blockchain.add(new Block("Second Block", blockchain.get(blockchain.size() - 1).hash));
System.out.println("Trying to mine block 2...");
blockchain.get(1).mineBlock(difficulty);
blockchain.add(new Block("Third Block", blockchain.get(blockchain.size() - 1).hash));
System.out.println("Trying to mine block 3...");
blockchain.get(2).mineBlock(difficulty);
System.out.println("\n Blockchain is Valid: " + isChainValid());
String blockchainJson = new GsonBuilder().setPrettyPrinting().create().toJson(blockchain);
System.out.println(blockchainJson);
}
public static boolean isChainValid() {
Block currentBlock;
Block previousBlock;
String hashTarget = new String(new char[difficulty]).replace('\0', '0');
for (int i = 1; i < blockchain.size(); i++) {
currentBlock = blockchain.get(i);
previousBlock = blockchain.get(i - 1);
if (!currentBlock.hash.equals(currentBlock.calculateHash())) {
System.out.println("Current Hashes not equal");
return false;
}
if (!previousBlock.hash.equals(currentBlock.previousHash)) {
System.out.println("Previous Hashes not equal");
return false;
}
if (!currentBlock.hash.substring(0, difficulty).equals(hashTarget)) {
System.out.println("Block hasn't been mined");
return false;
}
}
return true;
}
}