简单理解区块链知识



package com.redis.testRedis;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.alibaba.fastjson.JSON;

public class BlockChain {

    public static void main(String[] args) {
        List blockChainList =  new ArrayList();

        Block genesis = new Block("BlockChain", 0);
        blockChainList.add(genesis);
        Block helloBlock = new Block("Hello", blockChainList.get(blockChainList.size()-1).getHash());

        blockChainList.add(helloBlock);

        Block worldBlock = new Block("World", blockChainList.get(blockChainList.size()-1).getHash());
        blockChainList.add(worldBlock);
        Block dZoneBlock = new Block("DZone", blockChainList.get(blockChainList.size()-1).getHash());

        blockChainList.add(dZoneBlock);

        System.out.println("---------------------");

        System.out.println("- BlockChain -");

        System.out.println("---------------------");

        for(Block b : blockChainList) {
         System.out.println(JSON.toJSONString(b));

        }
        System.out.println("---------------------");

        System.out.println("Is valid?: " + validate(blockChainList));

        System.out.println("---------------------");

       
        Block hiBlock = new Block("Hi", genesis.getHash());
        int index = blockChainList.indexOf(helloBlock);

        blockChainList.remove(index);

        blockChainList.add(index, hiBlock);
        System.out.println("Corrupted block chain by replacing 'Hello' block with 'Hi' Block");

        System.out.println("---------------------");

        System.out.println("- BlockChain -");
        System.out.println("---------------------");


        for(Block b : blockChainList) {
         System.out.println(JSON.toJSONString(b));
        }
        
        System.out.println("---------------------");
        System.out.println("Is valid?: " + validate(blockChainList));
        System.out.println("---------------------");
    }

    private static boolean validate(List blockChain) {
        boolean result = true;

        Block lastBlock = null;
        for(int i = blockChain.size() -1; i >= 0; i--) {

            if(lastBlock == null) {

                lastBlock = blockChain.get(i);
            }
            else {
                Block current = blockChain.get(i);
                if(lastBlock.getPreviousHash() != current.getHash()) {

                    result = false;
                    break;
                }
                lastBlock = current;
            }
        }
        return result;
    }
}

class Block {

    private int previousHash;

    private String data;

    private int hash;

    public Block(String data, int previousHash) {
        this.data = data;

        this.previousHash = previousHash;
        this.hash  = Arrays.hashCode(new Integer[]{data.hashCode(), previousHash});

    }

 public int getPreviousHash() {
  return previousHash;

 }

 public void setPreviousHash(int previousHash) {
  this.previousHash = previousHash;

 }

 public String getData() {
  return data;

 }

 public void setData(String data) {
  this.data = data;

 }

 public int getHash() {
  return hash;
}

 public void setHash(int hash) {
  this.hash = hash;

 }
}

其实就是把上一个hash后的数据放到下一条hash数据里,不断循环hash

评论