This is simple implementation of blockchain to understand the data structure.
There are two data structures. One is the BlockHeader that contains the version, previous block hash, merkle root hash( Hash tree), timestamp, difficulty of prove and nounce. I will explain the variouable in the next post.
*/
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
public class BlockHeader {
private int version;
private byte[] previousBlockHash;
private int merkleRootHash;
private int timestamp;
private int difficultyProve;
private int nonce;
public BlockHeader(byte[] previousBlockHash, Object[] transactions){
this.previousBlockHash = previousBlockHash;
this.merkleRootHash=this.someMethod(transactions);
}
public byte[] toByteArray(){
String tmpStr="";
if(previousBlockHash != null){
tmpStr+=new String(previousBlockHash,0,previousBlockHash.length);
}
tmpStr +=merkleRootHash;
return tmpStr.getBytes(StandardCharsets.UTF_8);
}
private int someMethod(Object[] transations){
return Arrays.hashCode(transations);
}
}
/*
Second data struture is block that contains the block size, block header and a information of transactions and number of transaction.
*/
Second data struture is block that contains the block size, block header and a information of transactions and number of transaction.
*/
import java.security.NoSuchAlgorithmException;
import java.security.*;
import java.nio.charset.StandardCharsets;
public class Block {
private int blockSize;
private BlockHeader blockHeader;
private int transactionCount;
private Object[] transactions;
public Block(BlockHeader blockHeader, Object[] transactions){
this.blockHeader=blockHeader;
this.transactions=transactions;
}
public String getBlockHash() throws NoSuchAlgorithmException{
String hash="";
MessageDigest messageDigest=MessageDigest.getInstance("SHA-256");
// byte[] blockHash= messageDigest.digest(blockHeader.toString().getBytes());
messageDigest.update(blockHeader.toString().getBytes());
byte[] blockHash = messageDigest.digest();
StringBuffer sb = new StringBuffer();
for(int i =0;i<blockHash.length;i++){
sb.append(Integer.toString((blockHash[i]&0xff)+0x100,16).substring(1));
}
hash = sb.toString();
return hash;
}
}
/*
Main for test
Just making Hash using previous hash data.
*/
/*
Main for test
Just making Hash using previous hash data.
*/
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
public class BlockchainDriver {
List<Block>blockchain = new ArrayList<Block>();
public static void main(String[] args) throws NoSuchAlgorithmException{
String[] transactions = {"unlv computer"};
Block genesisBlock=new Block(new BlockHeader(null,transactions),transactions);
System.out.println("Genesis Hash:\t"+genesisBlock.getBlockHash());
//
// transactions[0]="unlv computer";
// genesisBlock = new Block(new BlockHeader(null, transactions),transactions);
// System.out.println("Block Hash:"+genesisBlock.getBlockHash());
//second block
String[] secondTransactions={"UNLV computer store your bitcoin"};
Block secondBlock = new Block(new BlockHeader(genesisBlock.getBlockHash().getBytes(),secondTransactions),secondTransactions);
System.out.println("Second Hash:\t"+secondBlock.getBlockHash());
//Third block
String[] thirdTransactions={"your bitcoin is in UNLV computer"};
Block thirdBlock = new Block(new BlockHeader(secondBlock.getBlockHash().getBytes(),thirdTransactions),thirdTransactions);
System.out.println("Third Hash:\t"+thirdBlock.getBlockHash());
}
}







No comments:
Post a Comment