-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from comnics/dev-init
blockchain리스트와 isChainValid()함수 추가.
- Loading branch information
Showing
5 changed files
with
60 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |