Blockchain java code implementation, blockchain java code
Overview
MerkleTree is widely used in Bitcoin technology. This article aims to implement a simple MerkleTree through code and calculate the TreeRoot of Merkle tree.
Merkle Tree is a data structure used to verify any type of data stored, processed, and transmitted between computers.
Currently, the primary purpose of the Merkle tree is to ensure that the data blocks received from the peer network are not damaged or changed, and check that other peer networks do not lie about sending fake data blocks.
Merkle Tree application example
Bitcoin
GitA
Mazon's Dynamo
Gassandra
Applications in Bitcoin
Each block in Bitcoin contains the set signature of all transactions. This signature is implemented using the Merkle tree. The Merkle tree is used for bitcoin to aggregate all transactions in the block, generates the overall digital fingerprint of the entire transaction set and provides a very effective process to verify whether the transaction is included in the block.
A very important use of the Merkle tree is to check whether a block contains a specified transaction. The Merkle tree is constructed by recursive hash node pairs until there is only one hash.
Merkle tree code implementation
The heel node of the hash tree is called the Merkle root. The Merkle tree can only use the time complexity of log2 (N) to check whether any data element is contained in the tree:
Package test; import java. security. messageDigest; import java. util. arrayList; import java. util. list; public class MerkleTrees {// transaction List <String> txList; // Merkle Root String root; /*** constructor * @ param txList transaction 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 ;}}
Data Preparation
We put the transaction data into the List:
List<String> tempTxList = new ArrayList<String>();tempTxList.add("a");tempTxList.add("b");tempTxList.add("c");tempTxList.add("d");tempTxList.add("e");
Implementation Process
Prepare transaction data
Calculate the hash value of each data and gradually form the Left and Right nodes of the tree from left to right.
The execution cycle knows that only one data is left.
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++; }
Test
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()); } }
Execution result
In this paper, a simple MerkleTree is implemented in the form of a simple binary tree, And TreeRoot is calculated. However, in fact, MerkleTree is not restrained and Binary Trees may also be multi-Forks.
This article is translated in 90%.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.