108
|
1 package christie.blockchain;
|
|
2
|
|
3 import java.nio.charset.Charset;
|
|
4 import java.util.ArrayList;
|
|
5 import java.util.Arrays;
|
110
|
6 import java.util.List;
|
108
|
7
|
|
8 public class BlockChain {
|
|
9
|
|
10 public ArrayList<Block> blockList = new ArrayList<Block>();
|
|
11
|
|
12 public static void main(String[] args) {
|
|
13 int difficulty = 1;
|
|
14 BlockChain blockChain = new BlockChain();
|
110
|
15 Miner miner = new Miner();
|
108
|
16 long startTime = System.currentTimeMillis();
|
|
17
|
|
18 Block genesisBlock = blockChain.createGenesisBlock("Hi im the first block");
|
110
|
19 miner.mineBlock(genesisBlock, difficulty);
|
108
|
20 System.out.println("Hash for block 1 : " + genesisBlock.getData() + " Nonce : " + genesisBlock.getNonce());
|
|
21
|
|
22 Block secondBlock = blockChain.createNewBlock(genesisBlock, "Yo im the second block");
|
110
|
23 miner.mineBlock(secondBlock, difficulty);
|
108
|
24 System.out.println("Hash for block 2 : " + secondBlock.getData() + " Nonce : " + secondBlock.getNonce());
|
|
25
|
|
26 Block thirdBlock = blockChain.createNewBlock(secondBlock, "Hey im the third block");
|
110
|
27 miner.mineBlock(thirdBlock, difficulty);
|
108
|
28 System.out.println("Hash for block 3 : " + thirdBlock.getData() + " Nonce : " + thirdBlock.getNonce());
|
|
29
|
|
30
|
|
31 blockChain.blockList.add(genesisBlock);
|
|
32 blockChain.blockList.add(secondBlock);
|
|
33 blockChain.blockList.add(thirdBlock);
|
|
34
|
|
35 System.out.println("\nBlockchain is Valid: " + blockChain.isChainValid(difficulty));
|
|
36
|
|
37 long endTime = System.currentTimeMillis() - startTime;
|
|
38
|
|
39 System.out.println("end Time: " + endTime);
|
|
40
|
|
41 }
|
|
42
|
|
43 public Boolean isChainValid(int difficulty) {
|
|
44 Block currentBlock;
|
|
45 Block parentBlock;
|
|
46
|
|
47 String hashTarget = new String(new char[difficulty]).replace('\0', '0');
|
|
48
|
|
49 //loop through blockchain to check hashes:
|
|
50 for(int i=1; i < blockList.size(); i++) {
|
|
51 currentBlock = blockList.get(i);
|
|
52 parentBlock = blockList.get(i-1);
|
|
53
|
|
54 if(!Arrays.equals(parentBlock.getPresentHash(), currentBlock.getParentHash())){
|
|
55 System.out.println("Previous Hashes not equal");
|
|
56 return false;
|
|
57 }
|
|
58
|
110
|
59 String hashStr = new String(currentBlock.getPresentHash(), Charset.forName("utf-8"));
|
108
|
60 if(!hashStr.substring( 0, difficulty).equals(hashTarget)) {
|
|
61 System.out.println("This block hasn't been mined");
|
|
62 return false;
|
|
63 }
|
|
64 }
|
|
65 return true;
|
|
66 }
|
|
67
|
|
68 public synchronized Block createNewBlock(Block parent, String data){
|
|
69 long time = System.currentTimeMillis() / 1000;
|
|
70 // もし, 時差があって, timeが親のタイムスタンプより低いなら, 親のタイムスタンプ + 1 を代入する
|
|
71 if (parent.getTimestamp() >= time) time = parent.getTimestamp() + 1;
|
|
72
|
|
73 Block block = new Block(parent.getPresentHash(), time, data);
|
|
74 return block;
|
|
75 }
|
|
76
|
|
77 public synchronized Block createGenesisBlock(String data){
|
|
78 long time = System.currentTimeMillis() / 1000;
|
|
79 Block block = new Block(time, data);
|
|
80 return block;
|
|
81 }
|
|
82
|
|
83
|
|
84 }
|