Skip to content

Commit

Permalink
Merge pull request #3 from comnics/dev-init
Browse files Browse the repository at this point in the history
blockchain리스트와 isChainValid()함수 추가.
  • Loading branch information
comnics authored Mar 14, 2019
2 parents 76968db + f3a47f4 commit 404392d
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 5 deletions.
7 changes: 6 additions & 1 deletion .classpath
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="libs/gson-2.8.2.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Binary file modified bin/org/kbda/.DS_Store
Binary file not shown.
Binary file modified bin/org/kbda/blockchain/.DS_Store
Binary file not shown.
Binary file modified src/org/kbda/.DS_Store
Binary file not shown.
58 changes: 54 additions & 4 deletions src/org/kbda/blockchain/KbdaBlockchain.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,67 @@
package org.kbda.blockchain;

import java.util.ArrayList;

import org.kbda.blockchain.core.Block;

import com.google.gson.GsonBuilder;

public class KbdaBlockchain {

public static int difficulty = 5;
//blockchain ArrayList
public static ArrayList<Block> blockchain = new ArrayList<Block>();

public static int difficulty = 3;

public static void main(String[] arg) {

//블럭을 만듭니다.
Block block = new Block("Genesis block", "0");
block.mineBlock(difficulty);
//초기 블럭을 만듭니다.
blockchain.add(new Block("Genesis block", "0"));
System.out.println("\nTrying to Mine Genesis block!");
blockchain.get(0).mineBlock(difficulty);
//이후 블럭을 생성합니다.
for(int i = 1 ; i <= 10 ; i++){
blockchain.add(new Block("block " + i, blockchain.get(blockchain.size()-1).hash));
System.out.printf("\nTrying to Mine block #%d", i+1 );
blockchain.get(i).mineBlock(difficulty);
}
//전체 blockchain이 정상인지 체크합니다.
System.out.println("\nBlockchain is Valid : " + isChainValid());

//전체 블럭을 출력합니다.
String blockchainJson = new GsonBuilder().setPrettyPrinting().create().toJson(blockchain);
System.out.println("\nOpenchain Block list : ");
System.out.println(blockchainJson);

}

/**
* blockchain이 유효한지 체크합니다.
* - 현재 블럭의 hash가 유효한 값인지 체크한다.
* - 이전 블럭의 hash값과 동일한지 체크한다.
* @return
*/
public static Boolean isChainValid() {
Block currentBlock;
Block previousBlock;

//전체 블럭을 체크합니다.
for(int i=1; i < blockchain.size(); i++) {
currentBlock = blockchain.get(i);
previousBlock = blockchain.get(i-1);

//현재 블럭의 hash가 맞는지 체크합니다.
if(!currentBlock.hash.equals(currentBlock.calculateHash()) ){
System.out.println("Current Hashes not equal");
return false;
}

//이전 블럭의 hash값과 동일한지 체크합니다.
if(!previousBlock.hash.equals(currentBlock.previousHash) ) {
System.out.println("Previous Hashes not equal");
return false;
}
}
return true;
}
}

0 comments on commit 404392d

Please sign in to comment.