view src/main/java/christie/blockchain/BlockChain.java @ 110:eab161e557bd

fix Refactor
author akahori
date Mon, 19 Nov 2018 12:31:36 +0900
parents 31f87de2a1d4
children 694ea96a557a
line wrap: on
line source

package christie.blockchain;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class BlockChain {

    public ArrayList<Block> blockList = new ArrayList<Block>();

    public static void main(String[] args) {
        int difficulty = 1;
        BlockChain blockChain = new BlockChain();
        Miner miner = new Miner();
        long startTime = System.currentTimeMillis();

        Block genesisBlock = blockChain.createGenesisBlock("Hi im the first block");
        miner.mineBlock(genesisBlock, difficulty);
        System.out.println("Hash for block 1 : " + genesisBlock.getData() + " Nonce : " + genesisBlock.getNonce());

        Block secondBlock = blockChain.createNewBlock(genesisBlock, "Yo im the second block");
        miner.mineBlock(secondBlock, difficulty);
        System.out.println("Hash for block 2 : " + secondBlock.getData() + " Nonce : " + secondBlock.getNonce());

        Block thirdBlock = blockChain.createNewBlock(secondBlock, "Hey im the third block");
        miner.mineBlock(thirdBlock, difficulty);
        System.out.println("Hash for block 3 : " + thirdBlock.getData() + " Nonce : " + thirdBlock.getNonce());


        blockChain.blockList.add(genesisBlock);
        blockChain.blockList.add(secondBlock);
        blockChain.blockList.add(thirdBlock);

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

        long endTime = System.currentTimeMillis() - startTime;

        System.out.println("end Time: " + endTime);

    }

    public Boolean isChainValid(int difficulty) {
        Block currentBlock;
        Block parentBlock;

        String hashTarget = new String(new char[difficulty]).replace('\0', '0');

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

            if(!Arrays.equals(parentBlock.getPresentHash(), currentBlock.getParentHash())){
                System.out.println("Previous Hashes not equal");
                return false;
            }

            String hashStr = new String(currentBlock.getPresentHash(), Charset.forName("utf-8"));
            if(!hashStr.substring( 0, difficulty).equals(hashTarget)) {
                System.out.println("This block hasn't been mined");
                return false;
            }
        }
        return true;
    }

    public synchronized Block createNewBlock(Block parent, String data){
        long time = System.currentTimeMillis() / 1000;
        // もし, 時差があって, timeが親のタイムスタンプより低いなら, 親のタイムスタンプ + 1 を代入する
        if (parent.getTimestamp() >= time) time = parent.getTimestamp() + 1;

        Block block = new Block(parent.getPresentHash(), time, data);
        return block;
    }

    public synchronized Block createGenesisBlock(String data){
        long time = System.currentTimeMillis() / 1000;
        Block block = new Block(time, data);
        return block;
    }


}