diff --git a/src/noobchain/Block.java b/src/noobchain/Block.java index f8b63b6..7f81e2c 100644 --- a/src/noobchain/Block.java +++ b/src/noobchain/Block.java @@ -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; @@ -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) + @@ -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(); } diff --git a/src/noobchain/NoobChain.java b/src/noobchain/NoobChain.java index d9541a1..a86a79e 100644 --- a/src/noobchain/NoobChain.java +++ b/src/noobchain/NoobChain.java @@ -1,6 +1,5 @@ package noobchain; import java.util.ArrayList; -import com.google.gson.GsonBuilder; public class NoobChain { @@ -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()); @@ -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; } @@ -59,4 +60,5 @@ public static void addBlock(Block newBlock) { newBlock.mineBlock(difficulty); blockchain.add(newBlock); } + } diff --git a/src/noobchain/StringUtil.java b/src/noobchain/StringUtil.java index d0a8ecb..8015743 100644 --- a/src/noobchain/StringUtil.java +++ b/src/noobchain/StringUtil.java @@ -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(); @@ -37,5 +39,4 @@ public static String getDificultyString(int difficulty) { return new String(new char[difficulty]).replace('\0', '0'); } - }