本文的目的是帮助您了解如何开发区块链技术。
在教程中,将解决几个问题:
创建你的第一个(非常)基本的“区块链”。
实施简单的工作证明(采矿)系统。
惊叹于可能性。
(我假设您对面向对象编程有基本的了解)
需要注意的是,本教程并没有生产区块链的完整功能。相反,这是一个概念实现的证明,以帮助您理解区块链,为以后的教程打基础。
1,安装
教程中使用 Java,当然你可以使用其他的面向对象编程语言。开发工具是 Eclipse,同样你可以使用其他的文本编辑器(虽然你可能会错过很多好用的功能 。
你需要:
安装 Java 和 JDK;
Eclipse(或者其他 IDE/文本编辑器)。
你还可以使用谷歌发布的 GSON 类库,实现 Java 对象和 JSON 字符串之间的转换。这是个非常有用的类库,会在下文的点对点代码中继续使用,同样的,有其他方法能替换该类库。
在Eclipse中创建一个(file> new>)Java项目。我将把我的项目称为“ noobchain ”,并使用相同的名称创建一个新类。
2,制作区块链
区块链只是一个由区块组成的链表/列表。区块链中的每个区块都包括自己的数字签名、前一个区块的数字签名和一些数据(这些数据可能是交易)。
哈希=数字签名
每个区块不仅包含它前一个区块的哈希,也包含它自己的哈希(根据前一个区块的哈希和自己区块的数据计算得到)。如果前一区块的数据变化,则其哈希也变化(因为它的计算也与区块的数据有关),然后影响后面所有区块的哈希。计算和比较哈希可以检查区块链是否无效。
这意味着什么?更改区块链中的任何数据,将更改签名并破坏区块链。
所有我们先创建构造区块链的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. public Block(String data,String previousHash ) { this.data = data; this.previousHash = previousHash; this.timeStamp = new Date().getTime(); } }
可以看到,我们的区块Block包含一个String类型的hash作为数字签名。变量previousHash作为前一个区块的hash,String类型的data作为区块数据。
接下来我们需要一种方法生成数字签名:
有很多密码算法供你选择,然而SHA256最为合适,我们可以导入java.security.MessageDigest来使用该算法。
下面我们需要使用SHA256算法,所以我们创建一个StringUtil类并在其中定义了一个方法:
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算法,然后就返回一个字符串签名。
现在让我们在Block类中创建一个新方法,调用SHA256算法来计算哈希。我们必须根据不希望被篡改的所有区块来计算hash,所以我们的区块包括previousHash,
data
,timeStamp(时间戳)。
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); } }
public String calculateHash() { String calculatedhash = StringUtil.applySha256( previousHash + Long.toString(timeStamp) + data ); return calculatedhash; }
把该方法添加到Block的构造函数中:
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类中创建些区块并打印它的哈希到屏幕上,确认它正常工作。
第一个区块叫做初始区块,因为它没有前驱区块,所以我们把它前一区块的哈希设为“0”.
输出如下:
你的是不一样的,因为你的时间戳是不同的。
现在每一个区块有自己的基于它自己的信息和前一区块数字签名的数字签名。
现在它还不是区块链,所以我们存储我们的区块到ArrayList,然后导入gson包将其转化为Json来查看。
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()方法,遍历所有的区块并比较它们的哈希。这个方法需要验证当前区块的哈希值是否等于计算得到的哈希值,前一区块的哈希值是否等于当前区块的哈希值。
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。
在比特币网络节点上共享其区块链,并且网络接受最长的有效链。什么阻止某人篡改旧块中的数据然后创建一个全新的更长的区块链并将其呈现给网络?工作量证明。哈希工作量证明系统意味着创建新的区块需要相当长的时间和计算能力。因此攻击者需要有比其他所有同行加起来还有多的算力。
3,开始挖矿
我们要让矿机完成工作量证明机制,通过在区块中尝试不同的变量值直到它的哈希值以某个固定数量的0开头。
在calculateHash()方法中添加一个整数类型变量nonce,添加一个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); } }
实际上每个矿机都从一个随机点开始迭代。一些矿机甚至尝试随机的nonce的值。更困难的可能不仅仅是整数。MAX_VALUE情况下,矿机可以尝试改变时间戳。
mineBlock()方法输入一个整数变量difficulty,这是矿机必须找到的0的数量。在大多数计算机上可以立即处理1或2个0的情况,我建议用4到6个0进行测试。在写本文时,莱特币的难度大约是442592.
我们将difficulty作为静态变量添加到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; } }
运行结果如下
挖掘每个区块需要一些时间(大约3秒)。你应该更改difficulty的值(即挖矿难度)来看看对挖矿时间的影响。
如果有人篡改了你的区块链中的数据:
他们的区块链是无效的;
他们不能创建一个更长的区块链;
在你网络中的区块链会在最长的链上有个时间优势。
一个被篡改的区块链不肯赶上一个更长且有效的链。
除非他们的算力比你网络中其他所有节点的算力总和还要多。
你已经完成了基本的区块链!
你的区块链:
由存储数据的块组成。
由数字签名把区块 链接起来。
需要工作量证明挖矿来验证新的区块。
可以验证其中的数据是否有效且未被篡改。
发表评论