溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

java 實(shí)例理解區(qū)塊鏈的概念

發(fā)布時(shí)間:2020-07-12 23:03:05 來(lái)源:網(wǎng)絡(luò) 閱讀:345 作者:lifeneedyou 欄目:軟件技術(shù)
   區(qū)塊鏈的核心是去中心化的存儲(chǔ),傳統(tǒng)的數(shù)據(jù)庫(kù)解決方案,包括關(guān)系型數(shù)據(jù)庫(kù),非關(guān)系型數(shù)據(jù)庫(kù),都是屬于中心化的存儲(chǔ)方式。去中心化的存儲(chǔ),就是數(shù)據(jù)沒有中心,并且每個(gè)數(shù)據(jù)節(jié)點(diǎn)都包含了上一個(gè)數(shù)據(jù)節(jié)點(diǎn)的信息。

         通過(guò)一個(gè)實(shí)例來(lái)理解區(qū)塊鏈的數(shù)據(jù)存儲(chǔ)形式:

package com.weihua.blockchains.blackchain;

import java.util.Date;

public class BlockMan {

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 BlockMan(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;
}

//Increases nonce value until hash target is reached.
public void mineBlock(int difficulty) {
    String target = StringUtil.getDificultyString(difficulty); //Create a string with difficulty * "0" 
    while(!hash.substring( 0, difficulty).equals(target)) {
        nonce ++;
        hash = calculateHash();
    }
    System.out.println("Block Mined!!! : " + hash);
}

}

package com.weihua.blockchains.blackchain;
import java.security.MessageDigest;

import com.google.gson.GsonBuilder;

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);
    }
}

//Short hand helper to turn Object into a json string
public static String getJson(Object o) {
    return new GsonBuilder().setPrettyPrinting().create().toJson(o);
}

//Returns difficulty string target, to compare to hash. eg difficulty of 5 will return "00000"  
public static String getDificultyString(int difficulty) {
    return new String(new char[difficulty]).replace('\0', '0');
}

}

package com.weihua.blockchains.blackchain;
import java.util.ArrayList;

public class TestBlockChain {

public static ArrayList<BlockMan> blockchain = new ArrayList<BlockMan>();
public static int difficulty = 5;

public static void main(String[] args) {    
    //add our blocks to the blockchain ArrayList:

    System.out.println("Trying to Mine block 1... ");
    addBlock(new BlockMan("Hi im the first block", "0"));

    System.out.println("Trying to Mine block 2... ");
    addBlock(new BlockMan("Yo im the second block",blockchain.get(blockchain.size()-1).hash));

    System.out.println("Trying to Mine block 3... ");
    addBlock(new BlockMan("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 static Boolean isChainValid() {
    BlockMan currentBlock; 
    BlockMan 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;
}

public static void addBlock(BlockMan newBlock) {
    newBlock.mineBlock(difficulty);
    blockchain.add(newBlock);
}

}

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.weihua.blockchains</groupId>
<artifactId>blackchain</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>blackchain</name>
<url>http://maven.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.1</version>
    </dependency>

</dependencies>

</project>

運(yùn)行起來(lái)看下輸出:
Trying to Mine block 1...
Block Mined!!! : 000005b9947556fa4b14d24349728a58418c1517f8073d10a37ba3daf0ceee9c
Trying to Mine block 2...
Block Mined!!! : 00000015d3f53bd8545a3a9586aaef26abe26d78fce2da6584aac025c60c21e3
Trying to Mine block 3...
Block Mined!!! : 000000d224711a1e308e98c317cc7c900c7f4196d585266262b54e42288da65b

Blockchain is Valid: true

The block chain:
[
{
"hash": "000005b9947556fa4b14d24349728a58418c1517f8073d10a37ba3daf0ceee9c",
"previousHash": "0",
"data": "Hi im the first block",
"timeStamp": 1557928459188,
"nonce": 347042
},
{
"hash": "00000015d3f53bd8545a3a9586aaef26abe26d78fce2da6584aac025c60c21e3",
"previousHash": "000005b9947556fa4b14d24349728a58418c1517f8073d10a37ba3daf0ceee9c",
"data": "Yo im the second block",
"timeStamp": 1557928460017,
"nonce": 1394831
},
{
"hash": "000000d224711a1e308e98c317cc7c900c7f4196d585266262b54e42288da65b",
"previousHash": "00000015d3f53bd8545a3a9586aaef26abe26d78fce2da6584aac025c60c21e3",
"data": "Hey im the third block",
"timeStamp": 1557928462949,
"nonce": 81919
}
]

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI