java区块链web应用(用 Java 开发一个区块链)

  java区块链web应用(用 Java 开发一个区块链)

导读:在本篇文章中,我们将区块链剥离到基本部分,并使用Java中的POJO创建一个区块链,我们一起来看一看吧。

  

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

  

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

  

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

  

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

  

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

  

package org。demo;import lombok。Getter;import lombok。ToString;import java。util。Arrays;@Getter@ToStringpublic 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) { ListblockChainList = 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(ListblockChain) { 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; }}

  

  


  

免责声明:本文转载仅作分享,版权归原作者所有。如侵权请联系我们删除!

  
","content_hash"!"4e6e87b2

版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

评论