Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions src/noobchain/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

public class Block {

public String hash;
public String previousHash;
private String hash;
private String previousHash;
private String data; //our data will be a simple message.
private long timeStamp; //as number of milliseconds since 1/1/1970.
private int nonce;
Expand All @@ -15,13 +15,22 @@ public Block(String data,String previousHash ) {
this.data = data;
this.previousHash = previousHash;
this.timeStamp = new Date().getTime();

this.hash = calculateHash(); //Making sure we do this after we set the other values.

//Making sure we do this after we set the other values.
this.hash = calculateHash();
}

//Calculate new hash based on blocks contents

public String getHash() {
return hash;
}

public String getPreviousHash() {
return previousHash;
}

//Calculate new hash based on blocks contents
public String calculateHash() {
String calculatedhash = StringUtil.applySha256(
String calculatedhash = StringUtil.applySha256(
previousHash +
Long.toString(timeStamp) +
Integer.toString(nonce) +
Expand All @@ -32,8 +41,9 @@ public String calculateHash() {

//Increases nonce value until hash target is reached.
public void mineBlock(int difficulty) {
String target = StringUtil.getDificultyString(difficulty); //Create a string with difficulty * "0"
while(!hash.substring( 0, difficulty).equals(target)) {
//Create a string with difficulty * "0"
String target = StringUtil.getDificultyString(difficulty);
while (!hash.substring( 0, difficulty).equals(target)) {
nonce ++;
hash = calculateHash();
}
Expand Down
18 changes: 10 additions & 8 deletions src/noobchain/NoobChain.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package noobchain;
import java.util.ArrayList;
import com.google.gson.GsonBuilder;

public class NoobChain {

Expand All @@ -14,10 +13,10 @@ public static void main(String[] args) {
addBlock(new Block("Hi im the first block", "0"));

System.out.println("Trying to Mine block 2... ");
addBlock(new Block("Yo im the second block",blockchain.get(blockchain.size()-1).hash));
addBlock(new Block("Yo im the second block", blockchain.get(blockchain.size()-1).getHash()));

System.out.println("Trying to Mine block 3... ");
addBlock(new Block("Hey im the third block",blockchain.get(blockchain.size()-1).hash));
addBlock(new Block("Hey im the third block", blockchain.get(blockchain.size()-1).getHash()));

System.out.println("\nBlockchain is Valid: " + isChainValid());

Expand All @@ -32,25 +31,27 @@ public static Boolean isChainValid() {
String hashTarget = new String(new char[difficulty]).replace('\0', '0');

//loop through blockchain to check hashes:
for(int i=1; i < blockchain.size(); i++) {
for (int i=1; i < blockchain.size(); i++) {
currentBlock = blockchain.get(i);
previousBlock = blockchain.get(i-1);

//compare registered hash and calculated hash:
if(!currentBlock.hash.equals(currentBlock.calculateHash()) ){
if (!currentBlock.getHash().equals(currentBlock.calculateHash())) {
System.out.println("Current Hashes not equal");
return false;
}

//compare previous hash and registered previous hash
if(!previousBlock.hash.equals(currentBlock.previousHash) ) {
if (!previousBlock.getHash().equals(currentBlock.getPreviousHash())) {
System.out.println("Previous Hashes not equal");
return false;
}

//check if hash is solved
if(!currentBlock.hash.substring( 0, difficulty).equals(hashTarget)) {
if (!currentBlock.getHash().substring( 0, difficulty).equals(hashTarget)) {
System.out.println("This block hasn't been mined");
return false;
}

}
return true;
}
Expand All @@ -59,4 +60,5 @@ public static void addBlock(Block newBlock) {
newBlock.mineBlock(difficulty);
blockchain.add(newBlock);
}

}
13 changes: 7 additions & 6 deletions src/noobchain/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ public static String applySha256(String input){

//Applies sha256 to our input,
byte[] hash = digest.digest(input.getBytes("UTF-8"));

StringBuffer hexString = new StringBuffer(); // This will contain hash as hexidecimal
for (int i = 0; i < hash.length; i++) {
String hex = Integer.toHexString(0xff & hash[i]);
if(hex.length() == 1) hexString.append('0');

// This will contain hash as hexidecimal
StringBuilder hexString = new StringBuilder();

for (byte aHash : hash) {
String hex = Integer.toHexString(0xff & aHash);
if (hex.length() == 1) hexString.append('0');
hexString.append(hex);
}
return hexString.toString();
Expand All @@ -37,5 +39,4 @@ public static String getDificultyString(int difficulty) {
return new String(new char[difficulty]).replace('\0', '0');
}


}