区块链与java的应用开发_用 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 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 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;

}

}

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值