3
3
import java .nio .charset .StandardCharsets ;
4
4
import java .security .MessageDigest ;
5
5
import java .security .NoSuchAlgorithmException ;
6
+ import java .util .ArrayList ;
6
7
import java .util .Date ;
7
8
import java .util .List ;
8
9
9
10
/**
10
11
* @author rampatra
11
12
* @since 2019-03-05
12
13
*/
13
- public class Blockchain { // TODO: WIP
14
+ public class Blockchain { // TODO: P2P
14
15
15
16
private List <Block > blockchain ;
16
17
private int difficulty ;
17
18
18
19
public Blockchain (List <Block > blockchain , int difficulty ) {
19
20
this .blockchain = blockchain ;
20
21
this .difficulty = difficulty ;
22
+ this .blockchain .add (getGenesisBlock ("Blockchain in Java" ));
23
+ }
24
+
25
+ public List <Block > getBlockchain () {
26
+ return blockchain ;
21
27
}
22
28
23
29
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 );
26
35
} 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 ++;
29
59
}
60
+ return new Block (index , previousBlock .getHash (), timestamp , data , hash , nonce );
30
61
}
31
62
32
63
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 (),
35
66
nextBlock .getTimestamp (), nextBlock .getData (), nextBlock .getNonce ());
36
67
37
68
if (previousBlock .getIndex () + 1 != nextBlock .getIndex ()) {
@@ -47,21 +78,6 @@ private boolean isValidNextBlock(Block previousBlock, Block nextBlock) {
47
78
}
48
79
}
49
80
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
-
65
81
private boolean isValidHashDifficulty (String hash ) {
66
82
for (int i = 0 ; i < difficulty ; i ++) {
67
83
if (hash .charAt (i ) != '0' ) {
@@ -79,9 +95,8 @@ private String calculateHashForBlock(final int index, final String previousHash,
79
95
(index + previousHash + timestamp + data + nonce ).getBytes (StandardCharsets .UTF_8 ));
80
96
return bytesToHex (encodedhash );
81
97
} catch (NoSuchAlgorithmException e ) {
82
- // do nothing for now
98
+ throw new RuntimeException ( "Encryption Error: {}" , e );
83
99
}
84
- return "" ;
85
100
}
86
101
87
102
private static String bytesToHex (byte [] hash ) {
@@ -93,4 +108,11 @@ private static String bytesToHex(byte[] hash) {
93
108
}
94
109
return hexString .toString ();
95
110
}
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
+ }
96
118
}
0 commit comments