|
3 | 3 | import java.nio.charset.StandardCharsets;
|
4 | 4 | import java.security.MessageDigest;
|
5 | 5 | import java.security.NoSuchAlgorithmException;
|
6 |
| -import java.util.ArrayList; |
7 | 6 | import java.util.Date;
|
8 | 7 | import java.util.List;
|
| 8 | +import java.util.ListIterator; |
9 | 9 |
|
10 | 10 | /**
|
11 | 11 | * @author rampatra
|
12 | 12 | * @since 2019-03-05
|
13 | 13 | */
|
14 |
| -public class Blockchain { // TODO: P2P |
| 14 | +public class Blockchain { |
15 | 15 |
|
16 |
| - private List<Block> blockchain; |
| 16 | + private List<Block> blocks; |
17 | 17 | private int difficulty;
|
18 | 18 |
|
19 |
| - public Blockchain(List<Block> blockchain, int difficulty) { |
20 |
| - this.blockchain = blockchain; |
| 19 | + public Blockchain(List<Block> blocks, int difficulty) { |
| 20 | + this.blocks = blocks; |
21 | 21 | this.difficulty = difficulty;
|
22 |
| - this.blockchain.add(getGenesisBlock("Blockchain in Java")); |
| 22 | + this.blocks.add(getGenesisBlock()); |
23 | 23 | }
|
24 | 24 |
|
25 |
| - public List<Block> getBlockchain() { |
26 |
| - return blockchain; |
| 25 | + public List<Block> getBlocks() { |
| 26 | + return blocks; |
27 | 27 | }
|
28 | 28 |
|
29 |
| - public void mine(String data) { |
30 |
| - Block previousBlock = blockchain.get(blockchain.size() - 1); |
| 29 | + public int getSize() { |
| 30 | + return blocks.size(); |
| 31 | + } |
| 32 | + |
| 33 | + public Block getLatestBlock() { |
| 34 | + if (blocks.isEmpty()) return null; |
| 35 | + |
| 36 | + return blocks.get(blocks.size() - 1); |
| 37 | + } |
| 38 | + |
| 39 | + public void addBlock(Block block) { |
| 40 | + blocks.add(block); |
| 41 | + } |
| 42 | + |
| 43 | + public Block mine(String data) { |
| 44 | + Block previousBlock = getLatestBlock(); |
31 | 45 | Block nextBlock = getNextBlock(previousBlock, data);
|
32 |
| - |
| 46 | + |
33 | 47 | if (isValidNextBlock(previousBlock, nextBlock)) {
|
34 |
| - blockchain.add(nextBlock); |
| 48 | + blocks.add(nextBlock); |
| 49 | + return nextBlock; |
35 | 50 | } else {
|
36 | 51 | throw new RuntimeException("Invalid block");
|
37 | 52 | }
|
38 | 53 | }
|
39 | 54 |
|
40 |
| - private Block getGenesisBlock(String data) { |
| 55 | + public boolean isValidChain() { |
| 56 | + ListIterator<Block> listIterator = blocks.listIterator(); |
| 57 | + listIterator.next(); |
| 58 | + while (listIterator.hasPrevious() && listIterator.hasNext()) { |
| 59 | + if (!isValidNextBlock(listIterator.previous(), listIterator.next())) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + } |
| 63 | + return true; |
| 64 | + } |
| 65 | + |
| 66 | + private Block getGenesisBlock() { |
41 | 67 | final long timestamp = new Date().getTime();
|
42 | 68 | int nonce = 0;
|
| 69 | + String data = "Blockchain in Java"; |
43 | 70 | String hash;
|
44 | 71 | while (!isValidHashDifficulty(hash = calculateHashForBlock(0, "0", timestamp, data, nonce))) {
|
45 | 72 | nonce++;
|
@@ -108,11 +135,4 @@ private static String bytesToHex(byte[] hash) {
|
108 | 135 | }
|
109 | 136 | return hexString.toString();
|
110 | 137 | }
|
111 |
| - |
112 |
| - public static void main(String[] args) { |
113 |
| - Blockchain blockchain = new Blockchain(new ArrayList<>(), 3); |
114 |
| - blockchain.mine("12"); |
115 |
| - blockchain.mine("26"); |
116 |
| - System.out.println("Blockchain: " + blockchain.getBlockchain()); |
117 |
| - } |
118 | 138 | }
|
0 commit comments