區塊鏈java代碼實現,區塊java代碼

來源:互聯網
上載者:User

區塊鏈java代碼實現,區塊java代碼

概述

MerkleTree被廣泛的應用在比特幣技術中,本文旨在通過代碼實現一個簡單的MerkleTree,並計算出Merkle tree的 TreeRoot。
Merkle Tree 是一種資料結構,用於驗證在電腦之間和之間儲存,處理和傳輸的任何類型的資料。
目前,Merkle樹的主要用途是確保從對等網路中接收的資料區塊未受損和未改變,和檢查其他對等網路沒有撒謊發送假資料區塊。

Merkle Tree應用舉例

比特幣

GitA

mazon's Dynamo

Gassandra

比特幣中的應用

比特幣中每個塊中都包含了所有交易的集合簽名,這個簽名就是用Merkle tree實現的,Merkle樹用於比特幣以匯總塊中的所有事務,產生整個事務集合的整體數位指紋,提供非常有效過程來驗證事務是否包括在塊中。

 

Merkle樹一個很重要的用處是檢查塊中是否包含指定的交易,Merkle樹是通過遞迴雜湊節點對來構造的,直到只有一個雜湊。

Merkle tree 代碼實現

雜湊樹的跟節點稱為Merkle根,Merkle樹可以僅用log2(N)的時間複雜度檢查任何一個資料元素是否包含在樹中:

package test;import java.security.MessageDigest;import java.util.ArrayList;import java.util.List;public class MerkleTrees {  // transaction List  List<String> txList;  // Merkle Root  String root;  /**  * constructor  * @param txList transaction List 交易List  */  public MerkleTrees(List<String> txList) {  this.txList = txList;  root = "";  }  /**  * execute merkle_tree and set root.  */  public void merkle_tree() {  List<String> tempTxList = new ArrayList<String>();  for (int i = 0; i < this.txList.size(); i++) {   tempTxList.add(this.txList.get(i));  }  List<String> newTxList = getNewTxList(tempTxList);  while (newTxList.size() != 1) {   newTxList = getNewTxList(newTxList);  }  this.root = newTxList.get(0);  }  /**  * return Node Hash List.  * @param tempTxList  * @return  */  private List<String> getNewTxList(List<String> tempTxList) {  List<String> newTxList = new ArrayList<String>();  int index = 0;  while (index < tempTxList.size()) {   // left   String left = tempTxList.get(index);   index++;   // right   String right = "";   if (index != tempTxList.size()) {   right = tempTxList.get(index);   }   // sha2 hex value   String sha2HexValue = getSHA2HexValue(left + right);   newTxList.add(sha2HexValue);   index++;  }  return newTxList;  }  /**  * Return hex string  * @param str  * @return  */  public String getSHA2HexValue(String str) {   byte[] cipher_byte;   try{    MessageDigest md = MessageDigest.getInstance("SHA-256");    md.update(str.getBytes());    cipher_byte = md.digest();    StringBuilder sb = new StringBuilder(2 * cipher_byte.length);    for(byte b: cipher_byte) {     sb.append(String.format("%02x", b&0xff) );    }    return sb.toString();   } catch (Exception e) {     e.printStackTrace();   }   return "";  }  /**  * Get Root  * @return  */  public String getRoot() {  return this.root;  } }

資料準備

我們將交易的資料,放入到List中:

List<String> tempTxList = new ArrayList<String>();tempTxList.add("a");tempTxList.add("b");tempTxList.add("c");tempTxList.add("d");tempTxList.add("e");

實現過程

準備交易資料
計算出每個資料的hash值,從左至右逐步組成樹的左右節點
執行迴圈知道最後只剩下一個資料

private List<String> getNewTxList(List<String> tempTxList) { List<String> newTxList = new ArrayList<String>(); int index = 0; while (index < tempTxList.size()) {  // left  String left = tempTxList.get(index);  index++;  // right  String right = "";  if (index != tempTxList.size()) {   right = tempTxList.get(index);  }  // sha2 hex value  String sha2HexValue = getSHA2HexValue(left + right);  newTxList.add(sha2HexValue);  index++; }

測試

package test;import java.util.ArrayList;import java.util.List;public class App {   public static void main(String [] args) {    List<String> tempTxList = new ArrayList<String>();    tempTxList.add("a");    tempTxList.add("b");    tempTxList.add("c");    tempTxList.add("d");    tempTxList.add("e");    MerkleTrees merkleTrees = new MerkleTrees(tempTxList);    merkleTrees.merkle_tree();    System.out.println("root : " + merkleTrees.getRoot());   }  }

執行結果

本文從簡單二叉樹的形式實現了簡單的MerkleTree,計算出TreeRoot,但是實際上的的MerkleTree不拘謹與二叉樹還可能是多叉樹。

本文90%來著於翻譯,原文地址

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.