ブロックチェーンjavaコードの実現


概要
MerkleTreeはビットコイン技術に広く応用されており、本論文ではコードにより簡単なMerkleTreeを実現し、Merkle treeのTree Rootを算出することを目的としている。
Merkle Treeは、コンピュータ間の記憶、処理、転送のいずれかのタイプのデータを検証するためのデータ構造である。
現在、Merkleツリーの主な用途は、ピアネットワークから受信したデータブロックが破損していないか、または変更されていないかを確認することと、他のピアネットワークがうそをついていないかを確認することである。

Merkle Tree応用例
ビットコイン
GitA
mazon's Dyname o
ガスアンドラ
ビットコインの応用
ビットコインの各ブロックにはすべての取引の集合署名が含まれています。この署名はMerkle treeで実現され、Merkleツリーはビットコインに使用されて、ブロック内のすべてのトランザクションをまとめ、事務セット全体の数字の指紋を生成し、非常に効果的なプロセスを提供して、ブロック内に含まれているかどうかを検証します。
这里写图片描述 
Merkleツリーは、特定のトランザクションがブロックに含まれているかどうかを調べるために重要な用途であり、Merkleツリーは、再帰的なハッシュ・ノード・ペアによって構成される。
这里写图片描述
Merkle treeコードの実現
ハッシュツリーのノードをMerkle根と呼び、Merkleツリーは、ロゴ2(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を実現し、Tree Rootを計算したが、実際のMerkleTreeは二叉樹と無関係であり、多叉樹である可能性がある。
本論文の90%は翻訳によるもので、原文の住所
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。