MerkleTree 构建QT实现UI
区块链学习笔记(三)
使用qt实现Merkle Tree构造以及存在性验证
前言
经过前面两篇blog的代码编写,初步实现了控制台的交互以及相应的后端,本篇blog在前两篇的基础上实现了前端。
前面两篇blog:
Merkle Tree 存在性功能第一次修改
Merkle Tree 构建(C++实现)
一、树上存在
二、树上不存在
三、部分代码
mainwindow.cpp
#include "mainwindow.h"
#include "truenode.h"
#include "falsenode.h"
#include "outbide.h"
#include "ui_mainwindow.h"
#include <iostream>
#include <time.h>
#include "tree.h"
#include "sha256.h"
using namespace std;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
tree ntree;
void MainWindow::on_MerkleTreeButton_clicked()
{
ui->Num->clear();
ui->AllHASH->clear();
vector<string> v; //叶子节点为string类
vector<long> v2;
long long k=20;
if(!ui->LeafCount->text().isEmpty()){
k=ui->LeafCount->text().toInt();
}
//生成随机的k个数字
srand(time(NULL));
for (int i = 0; i <k; i++) {
long t = rand() % 100001;//生成的数字在0-100000
v2.push_back(t);
}
string temp="",temp1="";
for (int i = 0; i < v2.size(); i++) {
v.push_back(to_string(v2[i]));
temp=to_string(v2[i]);
temp1=sha256::hash256_hex_string(temp);
QTreeWidgetItem * liItem = new QTreeWidgetItem(QStringList()<<QString::fromStdString(temp)<<QString::fromStdString(temp1));
ui->Num->addTopLevelItem(liItem);
}
ntree.buildBaseLeafes(v);
ntree.buildTree();
for(int j=0;j<ntree.base.size();j++){
for (int i = 0; i < ntree.base[j].size(); i++) {
QTreeWidgetItem * liItem = new QTreeWidgetItem(QStringList()<<QString::fromStdString(ntree.base[j][i]->getHash()));
ui->AllHASH->addTopLevelItem(liItem);
}
}
}
void MainWindow::on_pushButton_clicked()
{
ui->VHASH->clear();
ui->UseHASH->clear();
ntree.base2.clear();
string check_str = "";
check_str=ui->VNum->text().toStdString(); //输入想验证的叶子节点
check_str = sha256::hash256_hex_string(check_str);
QTreeWidgetItem * liItem = new QTreeWidgetItem(QStringList()<<QString::fromStdString(check_str));
ui->VHASH->addTopLevelItem(liItem);
if (ntree.verify(check_str))//验证有无这个节点
{
for(int i=0;i<ntree.base2.size();i++){
QTreeWidgetItem * liItem = new QTreeWidgetItem(QStringList()<<QString::fromStdString(ntree.base2[i]));
ui->UseHASH->addTopLevelItem(liItem);
}
truenode *configWindow = new truenode;
configWindow->show();
}
else
{
if(ntree.base2.empty()){
Outbide *configWindow = new Outbide;
configWindow->show();
}
else{
for(int i=0;i<ntree.base2.size();i++){
QTreeWidgetItem * liItem = new QTreeWidgetItem(QStringList()<<QString::fromStdString(ntree.base2[i]));
ui->UseHASH->addTopLevelItem(liItem);
}
falsenode *configWindow = new falsenode;
configWindow->show();
}
}
}
mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1468</width>
<height>763</height>
</rect>
</property>
<property name="windowTitle">
<string>Merkle Tree UI</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>10</x>
<y>20</y>
<width>401</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>请输入构建Merkle Tree随机数的个数(默认20个数字):</string>
</property>
</widget>
<widget class="QLineEdit" name="LeafCount">
<property name="geometry">
<rect>
<x>400</x>
<y>10</y>
<width>91</width>
<height>51</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="MerkleTreeButton">
<property name="geometry">
<rect>
<x>210</x>
<y>590</y>
<width>201</width>
<height>91</height>
</rect>
</property>
<property name="text">
<string>构建Merkle Tree</string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>10</x>
<y>100</y>
<width>231</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>生成的相应的随机数及哈希值:</string>
</property>
</widget>
<widget class="QTreeWidget" name="Num">
<property name="geometry">
<rect>
<x>10</x>
<y>130</y>
<width>731</width>
<height>141</height>
</rect>
</property>
<column>
<property name="text">
<string>随机数</string>
</property>
<property name="textAlignment">
<set>AlignCenter</set>
</property>
</column>
<column>
<property name="text">
<string>HASH值</string>
</property>
<property name="textAlignment">
<set>AlignCenter</set>
</property>
</column>
</widget>
<widget class="QTreeWidget" name="AllHASH">
<property name="geometry">
<rect>
<x>10</x>
<y>340</y>
<width>731</width>
<height>231</height>
</rect>
</property>
<column>
<property name="text">
<string>HASH值</string>
</property>
<property name="textAlignment">
<set>AlignCenter</set>
</property>
</column>
</widget>
<widget class="QTreeWidget" name="UseHASH">
<property name="geometry">
<rect>
<x>770</x>
<y>320</y>
<width>681</width>
<height>171</height>
</rect>
</property>
<column>
<property name="text">
<string>HASH值</string>
</property>
<property name="textAlignment">
<set>AlignCenter</set>
</property>
</column>
</widget>
<widget class="QLabel" name="label_3">
<property name="geometry">
<rect>
<x>10</x>
<y>300</y>
<width>351</width>
<height>20</height>
</rect>
</property>
<property name="text">
<string>构造Merkle Tree的过程中产生的所有HASH:</string>
</property>
</widget>
<widget class="QLabel" name="label_4">
<property name="geometry">
<rect>
<x>760</x>
<y>270</y>
<width>171</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>验证过程使用到的HASH:</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>990</x>
<y>520</y>
<width>241</width>
<height>91</height>
</rect>
</property>
<property name="text">
<string>验证</string>
</property>
</widget>
<widget class="QLineEdit" name="VNum">
<property name="geometry">
<rect>
<x>880</x>
<y>30</y>
<width>121</width>
<height>61</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label_5">
<property name="geometry">
<rect>
<x>770</x>
<y>50</y>
<width>111</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>验证的数据:</string>
</property>
</widget>
<widget class="QTreeWidget" name="VHASH">
<property name="geometry">
<rect>
<x>770</x>
<y>160</y>
<width>671</width>
<height>71</height>
</rect>
</property>
<column>
<property name="text">
<string>HASH值</string>
</property>
<property name="textAlignment">
<set>AlignCenter</set>
</property>
</column>
</widget>
<widget class="QLabel" name="label_6">
<property name="geometry">
<rect>
<x>770</x>
<y>120</y>
<width>161</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>验证数据的HASH值:</string>
</property>
</widget>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
四、相关代码资源
Merkle Tree QT 实现UI文章来源:https://www.toymoban.com/news/detail-684934.html
写在最后
以上即为Merkle Tree 的qt实现,仅供参考。如有相关专业问题,请在评论区留言。文章来源地址https://www.toymoban.com/news/detail-684934.html
到了这里,关于MerkleTree 构建QT实现UI的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!