用 Java 开发一个区块链

用 Java 开发一个区块链

在本篇文章中,我们将区块链剥离到基本部分,并使用Java中的POJO创建一个区块链.


相信大家都听说过加密货币和区块链,以及它们是如何相互关联的(如果你不清晰,可以查阅众利软件网站),这是真实的情况。但它们实际上是互相区别并可以独立存在。加密货币更多的是一种产品,而区块链是一种促进无信任团体间交易的技术


完整的生产型区块链应用非常庞大且复杂,但它的核心是很简单且功能强大的实现。区块链可以包含一个或多个交易的区块集合。每个块被哈希(散列)化,然后哈希配对,哈希再次配对,并再次哈希,直到单个哈希被保留,形成Merkle根。


每个块存储前一个块的哈希,并将区块链接在一起。这可确保所有的区块无法被修改块。


用 Java 开发一个区块链


下面我们就用Java,开发最简单的(Hello World)区块链。


这是Java中的简单块表示(POJO)。它将数据保存为一个字符串,但它可以是任何你可以想象到的,包括以太坊风格的智能合约。代码如下:


package org.demo;

import lombok.Getter;
import lombok.ToString;

import java.util.Arrays;

@Getter
@ToString
public class Block {
    private int previousHash;
    private String data;
    private int hash;

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

        // Mix the content of this block with previous hash to create the hash of this new block
        // and that's what makes it block chain
        this.hash  = Arrays.hashCode(new Integer[]{data.hashCode(), previousHash});
    }
}


下面是一个简单的区块链实现,具备基本的验证功能。代码如下:


package org.demo;

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

public class BlockChain {
    public static void main(String[] args) {
        List<Block> 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("---------------------");
        blockChainList.forEach(System.out::println);
        System.out.println("---------------------");
        System.out.println("Is valid?: " + validate(blockChainList));
        System.out.println("---------------------");

        // corrupt block chain by modifying one of the block
        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("---------------------");
        blockChainList.forEach(System.out::println);
        System.out.println("---------------------");
        System.out.println("Is valid?: " + validate(blockChainList));
        System.out.println("---------------------");


    }

    private static boolean validate(List<Block> 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;
    }
}


 希望本文能帮到你,编码愉快。