Skip to content

Commit d589f80

Browse files
committed
Cleanup code
1 parent 2fb61b7 commit d589f80

File tree

2 files changed

+44
-44
lines changed

2 files changed

+44
-44
lines changed

src/noobchain/Block.java

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,40 @@
33
import java.util.Date;
44

55
public class Block {
6-
7-
public String hash;
8-
public String previousHash;
9-
private String data; //our data will be a simple message.
10-
private long timeStamp; //as number of milliseconds since 1/1/1970.
11-
private int nonce;
12-
13-
//Block Constructor.
14-
public Block(String data,String previousHash ) {
15-
this.data = data;
16-
this.previousHash = previousHash;
17-
this.timeStamp = new Date().getTime();
18-
19-
this.hash = calculateHash(); //Making sure we do this after we set the other values.
20-
}
21-
22-
//Calculate new hash based on blocks contents
23-
public String calculateHash() {
24-
String calculatedhash = StringUtil.applySha256(
25-
previousHash +
26-
Long.toString(timeStamp) +
27-
Integer.toString(nonce) +
28-
data
29-
);
30-
return calculatedhash;
31-
}
32-
33-
//Increases nonce value until hash target is reached.
34-
public void mineBlock(int difficulty) {
35-
String target = StringUtil.getDificultyString(difficulty); //Create a string with difficulty * "0"
36-
while(!hash.substring( 0, difficulty).equals(target)) {
37-
nonce ++;
38-
hash = calculateHash();
39-
}
40-
System.out.println("Block Mined!!! : " + hash);
41-
}
42-
6+
7+
public String hash;
8+
public String previousHash;
9+
private final String data; //our data will be a simple message.
10+
private final long timeStamp; //as number of milliseconds since 1/1/1970.
11+
private int nonce;
12+
13+
//Block Constructor.
14+
public Block(String data, String previousHash) {
15+
this.data = data;
16+
this.previousHash = previousHash;
17+
this.timeStamp = new Date().getTime();
18+
19+
this.hash = calculateHash(); //Making sure we do this after we set the other values.
20+
}
21+
22+
//Calculate new hash based on blocks contents
23+
public String calculateHash() {
24+
return StringUtil.applySha256(
25+
previousHash +
26+
timeStamp +
27+
nonce +
28+
data
29+
);
30+
}
31+
32+
//Increases nonce value until hash target is reached.
33+
public void mineBlock(int difficulty) {
34+
String target = StringUtil.getDifficultyString(difficulty); //Create a string with difficulty * "0"
35+
while (!hash.substring(0, difficulty).equals(target)) {
36+
nonce++;
37+
hash = calculateHash();
38+
}
39+
System.out.println("Block Mined!!! : " + hash);
40+
}
41+
4342
}

src/noobchain/StringUtil.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package noobchain;
2+
import java.nio.charset.StandardCharsets;
23
import java.security.MessageDigest;
34

45
import com.google.gson.GsonBuilder;
@@ -12,12 +13,12 @@ public static String applySha256(String input){
1213
MessageDigest digest = MessageDigest.getInstance("SHA-256");
1314

1415
//Applies sha256 to our input,
15-
byte[] hash = digest.digest(input.getBytes("UTF-8"));
16+
byte[] hash = digest.digest(input.getBytes(StandardCharsets.UTF_8));
1617

17-
StringBuffer hexString = new StringBuffer(); // This will contain hash as hexidecimal
18-
for (int i = 0; i < hash.length; i++) {
19-
String hex = Integer.toHexString(0xff & hash[i]);
20-
if(hex.length() == 1) hexString.append('0');
18+
StringBuilder hexString = new StringBuilder(); // This will contain hash as hexidecimal
19+
for (byte b : hash) {
20+
String hex = Integer.toHexString(0xff & b);
21+
if (hex.length() == 1) hexString.append('0');
2122
hexString.append(hex);
2223
}
2324
return hexString.toString();
@@ -33,8 +34,8 @@ public static String getJson(Object o) {
3334
}
3435

3536
//Returns difficulty string target, to compare to hash. eg difficulty of 5 will return "00000"
36-
public static String getDificultyString(int difficulty) {
37-
return new String(new char[difficulty]).replace('\0', '0');
37+
public static String getDifficultyString(int difficulty) {
38+
return "0".repeat(difficulty);
3839
}
3940

4041

0 commit comments

Comments
 (0)