首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

自己动手写区块链

2018年开始区块链真是火啊。一夜暴富的例子一直在传说。今天我们就自己动手写一个基本的区块链。

先简单的说一下区块链是个什么(相信你早就知道了)。

区块链就是一个链表。把一堆区块串起来就是区块链。每个block有自己的数字签名(就是一串不规则看起来叼叼的字符串),同时包含有上一个block的数字签名,然后包含一些其他的data。

大体就长这样:

是不是很熟悉,链表。

好,继续。

数字签名是什么?就是hash。

而且每个block含有前一个block的hash值,而且每个block自己的hash也是由前一个的hash计算得来的。如果前一个block(数据块)的数据发生改变,那么前一个的hash值也改变了,由此就会影响到之后的数据块的所有hash值。

所以,通过计算和对比hash值这种方式我们就可以知道区块链是不是合法的,是不是已经被篡改。

什么意思呢?意味着只要你修改了区块链中的任何一个块中的数据,都将会改变hash,从而破坏了整个链。

好,不多说。上代码:

block块定义

先新建个block块:

public classBlock {

publicStringhash;

publicStringpreviousHash;

privateStringdata;//our data will be a simple message.

private longtimeStamp;//as number of milliseconds since 1/1/1970.

//Block Constructor.

publicBlock(String data,String previousHash ) {

this.data= data;

this.previousHash= previousHash;

this.timeStamp=newDate().getTime();

}

}

你也看到了我们的Block里有四个字段,hash就是这个块自己的hash值,previousHash就是上一个块的hash值,data就是这个块所持有的数据,timeStamp就是一个时间记录。

数字签名生成

接下来我们就需要生成数字签名。

有很多种的加密算法来生成数字签名。这里我们就选择SHA256。这里先新建一个工具类用来搞定这个件事情:

importjava.security.MessageDigest;//通过导入MessageDigest来使用SHA256

public classStringUtil {

//Applies Sha256 to a string and returns the result.

public staticString 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 =newStringBuffer();// This will contain hash as hexidecimal

for(inti =; i < hash.length; i++) {

String hex = Integer.toHexString(0xff& hash[i]);

if(hex.length() ==1) hexString.append('0');

hexString.append(hex);

}

returnhexString.toString();

}

catch(Exception e) {

throw newRuntimeException(e);

}

}

//Short hand helper to turn Object into a json string

public staticString getJson(Object o) {

return newGsonBuilder().setPrettyPrinting().create().toJson(o);

}

//Returns difficulty string target, to compare to hash. eg difficulty of 5 will return "00000"

public staticString getDificultyString(intdifficulty) {

return newString(new char[difficulty]).replace('\0','0');

}

}

好,现在我们在Block里添加生成hash的方法:

//Calculate new hash based on blocks contents

publicString calculateHash() {

String calculatedhash = StringUtil.applySha256(

previousHash+

Long.toString(timeStamp) +

Integer.toString(nonce) +

data

);

returncalculatedhash;

}

然后我们在构造函数里添加hash值的计算:

//Block Constructor.

publicBlock(String data,String previousHash ) {

this.data= data;

this.previousHash= previousHash;

this.timeStamp=newDate().getTime();

this.hash= calculateHash();//Making sure we do this after we set the other values.

}

一试身手

现在是时候一试身手了。我们新建一个main类来玩耍一次:

public static voidmain(String[] args) {

Block genesisBlock =newBlock("Hi im the first block","0");

System.out.println("block 1的hash值 : "+ genesisBlock.hash);

Block secondBlock =newBlock("Yo im the second block",genesisBlock.hash);

System.out.println("block 2的hash值: "+ secondBlock.hash);

Block thirdBlock =newBlock("Hey im the third block",secondBlock.hash);

System.out.println("block 3的hash值: "+ thirdBlock.hash);

}

输出结果如下:

hash值是不一样的,因为每个block的时间戳不同。

现在每个块都有了自己的数字签名,并且这些数字签名都是基于每个块自身的信息以及前一个块的数字签名联合起来生成的数字签名。

但,现在还不能叫区块链。只是一个个区块。接下来就让我们把这些块装入一个ArrayList中:

public staticArrayListblockchain=newArrayList();

public static voidmain(String[] args) {

//add our blocks to the blockchain ArrayList:

blockchain.add(newBlock("Hi im the first block","0"));

blockchain.add(newBlock("Yo im the second block",blockchain.get(blockchain.size()-1).hash));

blockchain.add(newBlock("Hey im the third block",blockchain.get(blockchain.size()-1).hash));

String blockchainJson =newGsonBuilder().setPrettyPrinting().create().toJson(blockchain);

System.out.println(blockchainJson);

}

现在看起来就比较紧凑了,也像个区块链的样子了:

检查区块链的完整性

现在就让我们在ImportChain中创建一个isChainValid()方法,它会遍历链中每个块,然后对比hash值。这个方法做的事情就是检查hash变量的值是否等于计算出来的hash值以及上一个块的hash是否等于previousHash变量的值。

public staticBoolean isChainValid() {

Block currentBlock;

Block previousBlock;

String hashTarget =newString(new char[difficulty]).replace('\0','0');

//循环遍历每个块检查hash

for(inti=1; i

currentBlock =blockchain.get(i);

previousBlock =blockchain.get(i-1);

//比较注册的hash和计算的hash:

if(!currentBlock.hash.equals(currentBlock.calculateHash()) ){

System.out.println("Current Hashes not equal");

return false;

}

//比较上一个块的hash和注册的上一个hash(也就是previousHash)

if(!previousBlock.hash.equals(currentBlock.previousHash) ) {

System.out.println("Previous Hashes not equal");

return false;

}

//检查hash是否被处理

if(!currentBlock.hash.substring(,difficulty).equals(hashTarget)) {

System.out.println("This block hasn't been mined");

return false;

}

}

return true;

}

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

On the bitcoin network nodes share their blockchains and thelongest valid chain is acceptedby the network. What’s to stop someone tampering with data in an old block then creating a whole new longer blockchain and presenting that to the network ?Proof of work. The hashcash proof of work system means it takes considerable time and computational power to create new blocks. Hence the attacker would need more computational power than the rest of the peers combined.

上面说的就是POW 。之后会介绍。

好,上面基本上把区块链搞完了。

现在我们开始新的征程吧!

挖矿

我们将要求矿工们来做POW,具体就是通过尝试不同的变量直到块的hash以几个0开头。

然后我们添加一个nonce(Number once)到calculateHash() 方法以及mineBlock()方法:

public classImportChain {

public staticArrayListblockchain=newArrayList();

public static intdifficulty=5;

public static voidmain(String[] args) {

//add our blocks to the blockchain ArrayList:

System.out.println("正在尝试挖掘block 1... ");

addBlock(newBlock("Hi im the first block","0"));

System.out.println("正在尝试挖掘block 2... ");

addBlock(newBlock("Yo im the second block",blockchain.get(blockchain.size()-1).hash));

System.out.println("正在尝试挖掘block 3... ");

addBlock(newBlock("Hey im the third block",blockchain.get(blockchain.size()-1).hash));

System.out.println("\nBlockchain is Valid: "+isChainValid());

String blockchainJson = StringUtil.getJson(blockchain);

System.out.println("\nThe block chain: ");

System.out.println(blockchainJson);

}

public staticBoolean isChainValid() {

Block currentBlock;

Block previousBlock;

String hashTarget =newString(new char[difficulty]).replace('\0','0');

//loop through blockchain to check hashes:

for(inti=1; 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(,difficulty).equals(hashTarget)) {

System.out.println("This block hasn't been mined");

return false;

}

}

return true;

}

public static voidaddBlock(Block newBlock) {

newBlock.mineBlock(difficulty);

blockchain.add(newBlock);

}

}

importjava.util.Date;

public classBlock {

publicStringhash;

publicStringpreviousHash;

privateStringdata;//our data will be a simple message.

private longtimeStamp;//as number of milliseconds since 1/1/1970.

private intnonce;

//Block Constructor.

publicBlock(String data,String previousHash ) {

this.data= data;

this.previousHash= previousHash;

this.timeStamp=newDate().getTime();

this.hash= calculateHash();//Making sure we do this after we set the other values.

}

//Calculate new hash based on blocks contents

publicString calculateHash() {

String calculatedhash = StringUtil.applySha256(

previousHash+

Long.toString(timeStamp) +

Integer.toString(nonce) +

data

);

returncalculatedhash;

}

//Increases nonce value until hash target is reached.

public voidmineBlock(intdifficulty) {

String target = StringUtil.getDificultyString(difficulty);//Create a string with difficulty * "0"

while(!hash.substring(, difficulty).equals(target)) {

nonce++;

hash= calculateHash();

}

System.out.println("Block已挖到!!! : "+hash);

}

}

执行main,输出如下:

挖掘每一个块都需要一些时间,大概3秒钟。你可以调整难度,看看是如何影响挖矿时间的。

如果有人要窜改区块链中的数据,那么他们的区块链将是无效的,invalid。

他们将无法创建更长的区块链。

在你的网络中诚实的区块链有更大的时间优势来创建一个最长的链。

被篡改的区块链将无法追上更长、更有效的链。

除非它们比网络中的所有其他节点具有更快的计算速度。比如未来的量子计算机之类的东西。

好,我们已经完成了一个基本的区块链!

总结一下我们的这个区块链:

每个区块上携带数据。

有数字签名。

必须通过POW来挖掘来验证新的区块。

可以验证数据是否合法和是否被修改。

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180222G0TXLD00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券
http://www.vxiaotou.com