JobPlus知识库 IT 工业智能4.0 文章
用 Java 开发一个区块链

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


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


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

下面我们就用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;

    }

}


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


如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

¥ 打赏支持
67人赞 举报
分享到
用户评价(0)

暂无评价,你也可以发布评价哦:)

扫码APP

扫描使用APP

扫码使用

扫描使用小程序