国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開發(fā) > Java > 正文

區(qū)塊鏈java代碼實(shí)現(xiàn)

2024-07-13 10:17:09
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

概述

MerkleTree被廣泛的應(yīng)用在比特幣技術(shù)中,本文旨在通過(guò)代碼實(shí)現(xiàn)一個(gè)簡(jiǎn)單的MerkleTree,并計(jì)算出Merkle tree的 TreeRoot。
Merkle Tree 是一種數(shù)據(jù)結(jié)構(gòu),用于驗(yàn)證在計(jì)算機(jī)之間和之間存儲(chǔ),處理和傳輸?shù)娜魏晤愋偷臄?shù)據(jù)。
目前,Merkle樹的主要用途是確保從對(duì)等網(wǎng)絡(luò)中接收的數(shù)據(jù)塊未受損和未改變,和檢查其他對(duì)等網(wǎng)絡(luò)沒(méi)有撒謊發(fā)送假數(shù)據(jù)塊。

java,區(qū)塊鏈

Merkle Tree應(yīng)用舉例

比特幣

GitA

mazon's Dynamo

Gassandra

比特幣中的應(yīng)用

比特幣中每個(gè)塊中都包含了所有交易的集合簽名,這個(gè)簽名就是用Merkle tree實(shí)現(xiàn)的,Merkle樹用于比特幣以匯總塊中的所有事務(wù),產(chǎn)生整個(gè)事務(wù)集合的整體數(shù)字指紋,提供非常有效的過(guò)程來(lái)驗(yàn)證事務(wù)是否包括在塊中。

java,區(qū)塊鏈

Merkle樹一個(gè)很重要的用處是檢查塊中是否包含指定的交易,Merkle樹是通過(guò)遞歸哈希節(jié)點(diǎn)對(duì)來(lái)構(gòu)造的,直到只有一個(gè)哈希。

java,區(qū)塊鏈

Merkle tree 代碼實(shí)現(xiàn)

哈希樹的跟節(jié)點(diǎn)稱為Merkle根,Merkle樹可以僅用log2(N)的時(shí)間復(fù)雜度檢查任何一個(gè)數(shù)據(jù)元素是否包含在樹中:

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

數(shù)據(jù)準(zhǔn)備

我們將交易的數(shù)據(jù),放入到List中:

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

實(shí)現(xiàn)過(guò)程

準(zhǔn)備交易數(shù)據(jù)
計(jì)算出每個(gè)數(shù)據(jù)的hash值,從左到右逐步組成樹的左右節(jié)點(diǎn)
執(zhí)行循環(huán)知道最后只剩下一個(gè)數(shù)據(jù)

java,區(qū)塊鏈

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

測(cè)試

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

執(zhí)行結(jié)果

java,區(qū)塊鏈

本文從簡(jiǎn)單二叉樹的形式實(shí)現(xiàn)了簡(jiǎn)單的MerkleTree,計(jì)算出TreeRoot,但是實(shí)際上的的MerkleTree不拘謹(jǐn)與二叉樹還可能是多叉樹。

本文90%來(lái)著于翻譯,原文地址

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JAVA教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 鄂托克前旗| 南溪县| 鄱阳县| 六安市| 旌德县| 安仁县| 长海县| 通江县| 邮箱| 克山县| 苏尼特左旗| 武功县| 遂溪县| 嘉荫县| 吉水县| 亚东县| 固原市| 南开区| 化德县| 楚雄市| 元江| 大同县| 同江市| 明溪县| 柞水县| 昆明市| 盱眙县| 海宁市| 诏安县| 陆良县| 德州市| 天津市| 湖北省| 儋州市| 九江市| 龙州县| 巴东县| 安庆市| 如皋市| 红桥区| 桦川县|