区块链java代码实现
作者:code_xzh 发布时间:2023-02-04 04:00:24
概述
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%来着于翻译,原文地址
来源:http://blog.csdn.net/xiangzhihong8/article/details/53931213


猜你喜欢
- 1、说明isInterrupted()可以判断当前线程是否被中断,仅仅是对interrupt()标识的一个判断,并不会影响标识发生任何改变(
- 在 WinForms 中,有时要执行耗时的操作,在该操作未完成之前操作用户界面,会导致用户界面停止响应。解决的方法就是新开一个线程,把耗时的
- jax-ws handler 的详解及简单实例aop技术一般用于某个对象的函数调用的日志,认证等。webservice是远程的函
- 学习目的:1、掌握在Android中如何建立EditText2、掌握EditText的常用属性3、掌握EditText焦点的事件、按键的事件
- 为大家提供的MySQL忘记密码的解决方案,供大家参考,具体内容如下1.在操作系统windows操作系统,xp或win7.中进入如下目录:C:
- Struts2中提供了数据校验验证数据例如验证邮件、数字等。验证方式有3种:一是通过validate()方法,二是通过Xml,三是使用注解方
- 前言 在软件开发中,我们通常会遇到一种场景,比如某个请求,会依次经过系统中的很多个模块来处理,如果某个模
- 一、项目简述本系统功能包括:通知公告,老人管理,护工管理,问答管理等等功能。二、项目运行环境配置: Jdk1.8 + Tomcat8.5 +
- 一、SpringMVC使用1.工程创建创建maven工程。添加java、resources目录。引入Spring-webmvc 依赖。<
- 1.C语言传统的处理错误的方式传统的错误处理机制:1. 终止程序,如assert,缺陷:用户难以接受。如发生内存错误,除0错误时就会终止程序
- 文件上传页面<%@ page language="java" contentType="text/htm
- 问题(1)自己动手写一个锁需要哪些知识?(2)自己动手写一个锁到底有多简单?(3)自己能不能写出来一个完美的锁?简介本篇文章的目标一是自己动
- 前言:前面总结学习了图片的使用以及Lru算法,今天来学习一下比较优秀的图片缓存开源框架。技术本身就要不断的更迭,从最初的自己使用SoftRe
- 对过滤器doFilter里chain.doFilter()函数的理解关于chain.doFilter()函数在最近的使用中不是很理解,但是考
- JVM内部结构图Java虚拟机主要分为五个区域:方法区、堆、Java栈、PC寄存器、本地方法栈。下面来看一些关于JVM结构的重要问题。1.哪
- 目录对象的创建创建方式对象的内存布局创建过程对象的内存分配分配方式并发安全代码优化逃逸分析的不成熟性实际的对象空间分配过程对象的访问句柄直接
- 前言WebJar官网:https://www.webjars.org/,对于任何与Servlet 3兼容的容器,WEB-INF/lib目录中
- PathVariable 映射 URL 绑定的占位符带占位符的 URL 是 Spring3.0 新增的功能,该功能在SpringMVC 向
- 1. 简介直到4g时代,流量依然是宝贵的东西。而移动网络传输中,最占流量的一种载体:图片,成为了我们移动开发者不得不关注的一个问题。我们关注
- 麦洛开通博客以来,有一段时间没有更新博文了.主要是麦洛这段时间因项目开发实在太忙了.今天周六还在公司加班,苦逼程序猿都是这样生活的.今天在做