使用Java创建第一个区块链 _JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 使用Java创建第一个区块链

使用Java创建第一个区块链

 2019/1/8 15:37:33  it_node  程序员俱乐部  我要评论(0)
  • 摘要:本系列教程的目的是帮助你了解如何开发区块链技术。在本教程中,我们将:创建你的第一个非常基础的区块链。实施简单的工作量证明系统(采矿)。探讨任何的可能性。我假设你对面向对象编程有基本的了解。值得注意的是,这不是一个功能齐全的生产区块链。相反,这是一个概念验证实现,可帮助你了解区块链对于未来教程中区块链的作用。配置我们将使用Java,但你应该能够使用任何OOP语言。我将使用Eclipse,但你可以使用任何新的花哨的文本编辑器(虽然你会错过很多好的扩展)。你会需要:安装了Java和JDK
  • 标签:

class="line">本系列教程的目的是帮助你了解如何开发区块链技术。在本教程中,我们将:

  • 创建你的第一个非常基础的区块链。
  • 实施简单的工作量证明系统(采矿)。
  • 探讨任何的可能性。

我假设你对面向对象编程有基本的了解。值得注意的是,这不是一个功能齐全的生产区块链。相反,这是一个概念验证实现,可帮助你了解区块链对于未来教程中区块链的作用。

配置

我们将使用Java,但你应该能够使用任何OOP语言。我将使用Eclipse,但你可以使用任何新的花哨的文本编辑器(虽然你会错过很多好的扩展)。

你会需要:

  • 安装了Java和JDK。
  • Eclipse或其他IDE。

或者你可以通过谷歌获取GSON库。这将允许我们将对象转换为Json。这是一个非常有用的库,我们也将在peer2peer中使用更多的东西,但是可以随意使用替代方法。

在Eclipse中(file> new>)创建一个Java项目。我将把我的项目称为monospace; font-size: 12px; padding: 2px 4px; background-color: #f6f6f6; vertical-align: middle; border: none; color: #c7254e; white-space: pre-wrap;">noobchain,并使用相同的名称创建一个新类NoobChain

现在你就可以去试试:)

创建区块链。

区块链只是一个链/列表块。区块链中的每个区块都有自己的数字签名,包含前一个区块的数字签名,并且有一些数据(例如,这些数据可能是交易)。

DBA2BA8F55494.png" style="vertical-align: middle; max-width: 100%; width: auto; height: auto;">

哈希=数字签名。

每个块不仅包含之前块的哈希值,而且它自己的哈希部分是从前一个哈希计算的。如果前一个块的数据被改变,那么前一个块的哈希将改变(因为它部分地由数据计算),进而影响其后的块的所有哈希。计算和比较哈希值可以让我们看到区块链是否无效。

这是什么意思?...更改此列表中的任何数据,将更改签名并破坏区块链。

所以先让我们创建构成区块链的类Block:

import java.util.Date;

public class Block {

    public String hash;
    public String previousHash;
    private String data; //our data will be a simple message.
    private long timeStamp; //as number of milliseconds since 1/1/1970.

    //Block Constructor.
    function">public Block(String data,String previousHash ) {
        this.data = data;
        this.previousHash = previousHash;
        this.timeStamp = new Date().getTime();
    }
}

正如你所看到的,我们的基本块包含一个String hash,它将保存我们的数字签名。变量previousHash用于保存前一个块的哈希和String data以保存我们的块数据。

接下来我们需要一种生成数字签名的方法,你可以选择许多加密算法,但SHA256适用于此示例。我们可以import java.security.MessageDigest;访问SHA256算法。

我们需要稍后使用SHA256,以便在新的StringUtil?utility类中创建一个方便的helper方法:

import java.security.MessageDigest;

public class StringUtil {
    //Applies Sha256 to a string and returns the result. 
    public static String applySha256(String input){     
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");            
            //Applies sha256 to our input, 
            byte[] hash = digest.digest(input.getBytes("UTF-8"));           
            StringBuffer hexString = new StringBuffer(); // This will contain hash as hexidecimal
            for (int i = 0; i < hash.length; i++) {
                String hex = Integer.toHexString(0xff & hash[i]);
                if(hex.length() == 1) hexString.append('0');
                hexString.append(hex);
            }
            return hexString.toString();
        }
        catch(Exception e) {
            throw new RuntimeException(e);
        }
    }   
}

如果你不理解这个帮助方法的内容,不要太担心,你需要知道的是它需要一个字符串并对其应用SHA256算法,并将生成的签名作为字符串返回。

现在让我们使用我们的applySha256?helper,在Block类的新方法中计算哈希值。我们必须计算我们不想被篡改的块的所有部分的哈希值。因此,对于我们的块,我们将包括previousHashdatatimeStamp


public String calculateHash() {
    String calculatedhash = StringUtil.applySha256( 
            previousHash +
            Long.toString(timeStamp) +
            data 
            );
    return calculatedhash;
}

并让我们将此方法添加到Block构造函数中 ...

javascript" style="font-family: Consolas, Menlo, Courier, monospace; font-size: 12px; display: block; padding: 0px; background: transparent; color: #c7254e; vertical-align: middle; border: none;">    public Block(String data,String previousHash ) {
        this.data = data;
        this.previousHash = previousHash;
        this.timeStamp = new Date().getTime();
        this.hash = calculateHash(); //Making sure we do this after we set the other values.
    }

一些测试时间......

在我们的主NoobChain类中,我们可以创建一些块并将哈希值打印到屏幕上,以查看所有内容是否正常工作。

让我们测试一下......第一个块称为genesis块,因为没有先前的块,我们只需输入“0”作为前一个哈希。

public class NoobChain {

    public static void main(String[] args) {
        
        Block genesisBlock = new Block("Hi im the first block", "0");
        System.out.println("Hash for block 1 : " + genesisBlock.hash);
        
        Block secondBlock = new Block("Yo im the second block",genesisBlock.hash);
        System.out.println("Hash for block 2 : " + secondBlock.hash);
        
        Block thirdBlock = new Block("Hey im the third block",secondBlock.hash);
        System.out.println("Hash for block 3 : " + thirdBlock.hash);
        
    }
}

输出应该类似于:

你的值会有所不同,因为你的时间戳会有所不同。

每个块现在都有自己的数字签名,基于其信息和前一个块的签名。

目前它不是一个区块链,所以让我们将块存储在ArrayList中,并导入gson以将其视为Json。(单击此处了解如何导入gson库)

import java.util.ArrayList;
import com.google.gson.GsonBuilder;

public class NoobChain {
    
    public static ArrayList<Block> blockchain = new ArrayList<Block>(); 

    public static void main(String[] args) {    
        //add our blocks to the blockchain ArrayList:
        blockchain.add(new Block("Hi im the first block", "0"));        
        blockchain.add(new Block("Yo im the second block",blockchain.get(blockchain.size()-1).hash)); 
        blockchain.add(new Block("Hey im the third block",blockchain.get(blockchain.size()-1).hash));
        
        String blockchainJson = new GsonBuilder().setPrettyPrinting().create().toJson(blockchain);      
        System.out.println(blockchainJson);
    }

}

现在我们的输出应该看起来更接近我们对区块链的期望。

现在我们需要一种方法来检查区块链的完整性。

让我们在NoobChain类中创建一个isChainValid()的布尔值方法,它将遍历链中的所有块并比较哈希值。此方法需要检查哈希变量实际上是否等于计算的哈希值,并且前一个块的哈希值等于previousHash变量。

public static Boolean isChainValid() {
    Block currentBlock; 
    Block previousBlock;
    
    //loop through blockchain to check hashes:
    for(int i=1; i < blockchain.size(); i++) {
        currentBlock = blockchain.get(i);
        previousBlock = blockchain.get(i-1);
        //compare registered hash and calculated hash:
        if(!currentBlock.hash.equals(currentBlock.calculateHash()) ){
            System.out.println("Current Hashes not equal");         
            return false;
        }
        //compare previous hash and registered previous hash
        if(!previousBlock.hash.equals(currentBlock.previousHash) ) {
            System.out.println("Previous Hashes not equal");
            return false;
        }
    }
    return true;
}

对区块链块的任何更改都将导致此方法返回false

比特币网络节点上共享其区块链,并且网络接受最长的有效链。什么阻止某人篡改旧块中的数据然后创建一个全新的更长的区块链并将其呈现给网络?工作量证明。工作系统的hashcash证明意味着创建新块需要相当多的时间和计算能力。因此,攻击者需要比其他对等组合更多的计算能力。

让我们开始挖掘块!

我们将要求矿工通过在块中尝试不同的变量值来进行工作量证明,直到其哈希以一定数量的0开始。

让我们在calculateHash()方法中添加一个名为nonceint,以及非常需要的mineBlock()方法:

import java.util.Date;

public class Block {
    
    public String hash;
    public String previousHash; 
    private String data; //our data will be a simple message.
    private long timeStamp; //as number of milliseconds since 1/1/1970.
    private int nonce;
    
    //Block Constructor.  
    public Block(String data,String previousHash ) {
        this.data = data;
        this.previousHash = previousHash;
        this.timeStamp = new Date().getTime();
        
        this.hash = calculateHash(); //Making sure we do this after we set the other values.
    }
    
    //Calculate new hash based on blocks contents
    public String calculateHash() {
        String calculatedhash = StringUtil.applySha256( 
                previousHash +
                Long.toString(timeStamp) +
                Integer.toString(nonce) + 
                data 
                );
        return calculatedhash;
    }
    
    public void mineBlock(int difficulty) {
        String target = new String(new char[difficulty]).replace('\0', '0'); //Create a string with difficulty * "0" 
        while(!hash.substring( 0, difficulty).equals(target)) {
            nonce ++;
            hash = calculateHash();
        }
        System.out.println("Block Mined!!! : " + hash);
    }
}

实际上,每个矿工将从随机点开始迭代。一些矿工甚至可以尝试随机数来获取随机数。另外值得注意的是,在更难的解决方案可能需要超过integer.MAX_VALUE,矿工可以尝试更改时间戳。

mineBlock()方法接受一个名为difficultyint,这中间必须解决的数量为0的问题。在大多数计算机上几乎可以立即解决像1或2这样的低难度问题,我建议在4-6左右进行难度测试。在撰写本文时,Litecoin的难度大约是442,592。

让我们将难度作为静态变量添加到NoobChain类:

public static int difficulty = 5; 

我们应该更新NoobChain类以触??发每个新块的mineBlock()方法。isChainValid()布尔值还应检查每个块是否具有已解决(通过挖掘)哈希。

import java.util.ArrayList;
import com.google.gson.GsonBuilder;

public class NoobChain {
    
    public static ArrayList<Block> blockchain = new ArrayList<Block>();
    public static int difficulty = 5;

    public static void main(String[] args) {    
        //add our blocks to the blockchain ArrayList:
        
        blockchain.add(new Block("Hi im the first block", "0"));
        System.out.println("Trying to Mine block 1... ");
        blockchain.get(0).mineBlock(difficulty);
        
        blockchain.add(new Block("Yo im the second block",blockchain.get(blockchain.size()-1).hash));
        System.out.println("Trying to Mine block 2... ");
        blockchain.get(1).mineBlock(difficulty);
        
        blockchain.add(new Block("Hey im the third block",blockchain.get(blockchain.size()-1).hash));
        System.out.println("Trying to Mine block 3... ");
        blockchain.get(2).mineBlock(difficulty);    
        
        System.out.println("\nBlockchain is Valid: " + isChainValid());
        
        String blockchainJson = new GsonBuilder().setPrettyPrinting().create().toJson(blockchain);
        System.out.println("\nThe block chain: ");
        System.out.println(blockchainJson);
    }
    
    public static Boolean isChainValid() {
        Block currentBlock; 
        Block previousBlock;
        String hashTarget = new String(new char[difficulty]).replace('\0', '0');
        
        //loop through blockchain to check hashes:
        for(int i=1; i < blockchain.size(); i++) {
            currentBlock = blockchain.get(i);
            previousBlock = blockchain.get(i-1);
            //compare registered hash and calculated hash:
            if(!currentBlock.hash.equals(currentBlock.calculateHash()) ){
                System.out.println("Current Hashes not equal");         
                return false;
            }
            //compare previous hash and registered previous hash
            if(!previousBlock.hash.equals(currentBlock.previousHash) ) {
                System.out.println("Previous Hashes not equal");
                return false;
            }
            //check if hash is solved
            if(!currentBlock.hash.substring( 0, difficulty).equals(hashTarget)) {
                System.out.println("This block hasn't been mined");
                return false;
            }
        }
        return true;
    }
}

请注意,我们还检查并打印isChainValid

运行此结果应如下所示:

挖掘每个区块需要一些时间!(大约3秒)你应该弄乱难度值,看看这会影响挖掘每个区块的时间。

如果有人要篡改区块链系统中的数据:

  • 他们的区块链无效。
  • 他们无法创建更长的区块链。
  • 你网络中的诚实区块链将在最长的链条上具有时间优势。

被篡改的区块链将无法赶上更长且有效的链条。

除非它们的计算速度远远超过网络中所有其他节点的总和。未来的量子计算机或其他东西。

你已经完成了基本的区块链!

你的区块链:

  • 由存储数据的块组成。
  • 具有将你的块链接在一起的数字签名。
  • 需要工作挖掘证明来验证新块。
  • 可以检查其中的数据是否有效且未更改。

你可以在Github上下载项目文件。

======================================================================

分享一些以太坊、EOS、比特币等区块链相关的交互式在线编程实战教程:

  • java以太坊开发教程,主要是针对java和android程序员进行区块链以太坊开发的web3j详解。
  • python以太坊,主要是针对python工程师使用web3.py进行区块链以太坊开发的详解。
  • php以太坊,主要是介绍使用php进行智能合约开发交互,进行账号创建、交易、转账、代币开发以及过滤器和交易等内容。
  • 以太坊入门教程,主要介绍智能合约与dapp应用开发,适合入门。
  • 以太坊开发进阶教程,主要是介绍使用node.js、mongodb、区块链、ipfs实现去中心化电商DApp实战,适合进阶。
  • C#以太坊,主要讲解如何使用C#开发基于.Net的以太坊应用,包括账户管理、状态与交易、智能合约开发与交互、过滤器和交易等。
  • EOS教程,本课程帮助你快速入门EOS区块链去中心化应用的开发,内容涵盖EOS工具链、账户与钱包、发行代币、智能合约开发与部署、使用代码与智能合约交互等核心知识点,最后综合运用各知识点完成一个便签DApp的开发。
  • java比特币开发教程,本课程面向初学者,内容即涵盖比特币的核心概念,例如区块链存储、去中心化共识机制、密钥与脚本、交易与UTXO等,同时也详细讲解如何在Java代码中集成比特币支持功能,例如创建地址、管理钱包、构造裸交易等,是Java工程师不可多得的比特币开发学习课程。
  • php比特币开发教程,本课程面向初学者,内容即涵盖比特币的核心概念,例如区块链存储、去中心化共识机制、密钥与脚本、交易与UTXO等,同时也详细讲解如何在Php代码中集成比特币支持功能,例如创建地址、管理钱包、构造裸交易等,是Php工程师不可多得的比特币开发学习课程。
  • tendermint区块链开发详解,本课程适合希望使用tendermint进行区块链开发的工程师,课程内容即包括tendermint应用开发模型中的核心概念,例如ABCI接口、默克尔树、多版本状态库等,也包括代币发行等丰富的实操代码,是go语言工程师快速入门区块链开发的最佳选择。

汇智网原创翻译,转载请标明出处。这里是原文使用Java创建第一个区块链

  • 相关文章
发表评论
用户名: 匿名