Skip to content

Commit fcc1491

Browse files
committed
Very basic blockchain implementation in Java
1 parent eb73332 commit fcc1491

File tree

1 file changed

+46
-24
lines changed

1 file changed

+46
-24
lines changed

src/main/java/com/rampatra/blockchain/Blockchain.java

+46-24
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,66 @@
33
import java.nio.charset.StandardCharsets;
44
import java.security.MessageDigest;
55
import java.security.NoSuchAlgorithmException;
6+
import java.util.ArrayList;
67
import java.util.Date;
78
import java.util.List;
89

910
/**
1011
* @author rampatra
1112
* @since 2019-03-05
1213
*/
13-
public class Blockchain { // TODO: WIP
14+
public class Blockchain { // TODO: P2P
1415

1516
private List<Block> blockchain;
1617
private int difficulty;
1718

1819
public Blockchain(List<Block> blockchain, int difficulty) {
1920
this.blockchain = blockchain;
2021
this.difficulty = difficulty;
22+
this.blockchain.add(getGenesisBlock("Blockchain in Java"));
23+
}
24+
25+
public List<Block> getBlockchain() {
26+
return blockchain;
2127
}
2228

2329
public void mine(String data) {
24-
if (blockchain.size() == 0) {
25-
blockchain.add(getGenesisBlock(data));
30+
Block previousBlock = blockchain.get(blockchain.size() - 1);
31+
Block nextBlock = getNextBlock(previousBlock, data);
32+
33+
if (isValidNextBlock(previousBlock, nextBlock)) {
34+
blockchain.add(nextBlock);
2635
} else {
27-
Block previousBlock = blockchain.get(blockchain.size() - 1);
28-
blockchain.add(getNextBlock(previousBlock, data));
36+
throw new RuntimeException("Invalid block");
37+
}
38+
}
39+
40+
private Block getGenesisBlock(String data) {
41+
final long timestamp = new Date().getTime();
42+
int nonce = 0;
43+
String hash;
44+
while (!isValidHashDifficulty(hash = calculateHashForBlock(0, "0", timestamp, data, nonce))) {
45+
nonce++;
46+
}
47+
48+
return new Block(0, "0", timestamp, data, hash, nonce);
49+
}
50+
51+
private Block getNextBlock(Block previousBlock, String data) {
52+
final int index = previousBlock.getIndex() + 1;
53+
final long timestamp = new Date().getTime();
54+
int nonce = 0;
55+
String hash;
56+
while (!isValidHashDifficulty(
57+
hash = calculateHashForBlock(index, previousBlock.getHash(), timestamp, data, nonce))) {
58+
nonce++;
2959
}
60+
return new Block(index, previousBlock.getHash(), timestamp, data, hash, nonce);
3061
}
3162

3263
private boolean isValidNextBlock(Block previousBlock, Block nextBlock) {
33-
34-
String nextBlockHash = calculateHashForBlock(nextBlock.getIndex(), previousBlock.getHash(),
64+
65+
String nextBlockHash = calculateHashForBlock(nextBlock.getIndex(), previousBlock.getHash(),
3566
nextBlock.getTimestamp(), nextBlock.getData(), nextBlock.getNonce());
3667

3768
if (previousBlock.getIndex() + 1 != nextBlock.getIndex()) {
@@ -47,21 +78,6 @@ private boolean isValidNextBlock(Block previousBlock, Block nextBlock) {
4778
}
4879
}
4980

50-
private Block getGenesisBlock(String data) {
51-
final long timestamp = new Date().getTime();
52-
String hash = calculateHashForBlock(0, "0", timestamp, data, 0);
53-
54-
return new Block(0, "0", timestamp, data, hash, 0);
55-
}
56-
57-
private Block getNextBlock(Block previousBlock, String data) {
58-
final int index = previousBlock.getIndex() + 1;
59-
final long timestamp = new Date().getTime();
60-
61-
final String hash = calculateHashForBlock(index, previousBlock.getHash(), timestamp, data, 0);
62-
return new Block(index, previousBlock.getHash(), timestamp, data, hash, 0);
63-
}
64-
6581
private boolean isValidHashDifficulty(String hash) {
6682
for (int i = 0; i < difficulty; i++) {
6783
if (hash.charAt(i) != '0') {
@@ -79,9 +95,8 @@ private String calculateHashForBlock(final int index, final String previousHash,
7995
(index + previousHash + timestamp + data + nonce).getBytes(StandardCharsets.UTF_8));
8096
return bytesToHex(encodedhash);
8197
} catch (NoSuchAlgorithmException e) {
82-
// do nothing for now
98+
throw new RuntimeException("Encryption Error: {}", e);
8399
}
84-
return "";
85100
}
86101

87102
private static String bytesToHex(byte[] hash) {
@@ -93,4 +108,11 @@ private static String bytesToHex(byte[] hash) {
93108
}
94109
return hexString.toString();
95110
}
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+
}
96118
}

0 commit comments

Comments
 (0)