-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerkletree.cpp
More file actions
181 lines (152 loc) · 5.89 KB
/
merkletree.cpp
File metadata and controls
181 lines (152 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "merkletree.h"
#include <openssl/sha.h>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <iostream>
// Helper function for calculating SHA256 hash
static std::string calculateHash(const std::string& data) {
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, data.c_str(), data.size());
SHA256_Final(hash, &sha256);
std::stringstream ss;
for(int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(hash[i]);
}
return ss.str();
}
MerkleNode::MerkleNode(const std::string& hash, const std::string& name)
: hash_(hash)
, name_(name)
, left_(nullptr)
, right_(nullptr)
, level_(0)
{
std::cout << " MerkleNode::MerkleNode " << name_ << " " << hash_ << std::endl;
}
MerkleNode::MerkleNode(std::shared_ptr<MerkleNode> left, std::shared_ptr<MerkleNode> right)
: left_(left)
, right_(right)
, level_(std::max(left->getLevel(), right ? right->getLevel() : 0) + 1)
, name_(left->getName() + (right ? right->getName() : left->getName()))
{
std::string combined = left->getHash() + (right ? right->getHash() : left->getHash());
hash_ = calculateHash(combined);
std::cout << " MerkleNode::MerkleNode " << name_ << " " << hash_ << std::endl;
}
MerkleTree::MerkleTree(const std::vector<Transaction>& transactions) {
std::cout << " MerkleTree::MerkleTree " << std::endl;
if (transactions.empty()) {
root_ = nullptr;
return;
}
// Create leaf nodes from transactions
std::vector<std::shared_ptr<MerkleNode>> nodes;
char ch = 'A';
for (const auto& tx : transactions) {
std::string txHash = tx.getTransactionId();
auto leafNode = std::make_shared<MerkleNode>(txHash, std::string(1, ch));
ch++;
leafNode->setLevel(0);
nodes.push_back(leafNode);
}
// Build the tree
root_ = buildTree(nodes);
// Build proof paths for each transaction
buildProofPaths(root_);
}
void MerkleNode::setLevel(int level) {
level_ = level;
// std::cout << " MerkleNode::setLevel " << name_ << " " << level_ << std::endl;
}
std::string MerkleTree::getRootHash() const {
return root_ ? root_->getHash() : "";
}
bool MerkleTree::verifyTransaction(const Transaction& transaction) const {
if (!root_) return false;
std::string txHash = transaction.getTransactionId();
auto it = proofPaths_.find(txHash);
if (it == proofPaths_.end()) {
return false;
}
return verifyPath(txHash, it->second);
}
void MerkleTree::buildProofPaths(std::shared_ptr<MerkleNode> node, const std::vector<std::pair<std::string, bool>>& path, int level) {
if (!node) return;
std::string indent(level * 2, ' ');
if (node->isLeaf()) {
proofPaths_[node->getHash()] = path;
std::cout << indent << " buildProofPaths leaf " << node->getName() << " " << node->getHash() << std::endl;
for (const auto& [siblingHash, isLeft] : path) {
std::cout << indent << " siblingHash " << siblingHash << " " << isLeft << std::endl;
}
return;
}
// 为左子节点构建路径
std::vector<std::pair<std::string, bool>> leftPath = path;
if (node->getRight()) {
leftPath.push_back({node->getRight()->getHash(), true});
std::cout << indent << " buildProofPaths leftPath " << node->getRight()->getName() << " " << "true" << std::endl;
}
buildProofPaths(node->getLeft(), leftPath, level + 1);
// 为右子节点构建路径
std::vector<std::pair<std::string, bool>> rightPath = path;
if (node->getLeft()) {
rightPath.push_back({node->getLeft()->getHash(), false});
std::cout << indent << " buildProofPaths rightPath " << node->getLeft()->getName() << " " << "false" << std::endl;
}
buildProofPaths(node->getRight(), rightPath, level + 1);
}
bool MerkleTree::verifyPath(const std::string& txHash, const std::vector<std::pair<std::string, bool>>& path) const {
std::string currentHash = txHash;
for (auto it = path.rbegin(); it != path.rend(); ++it) {
if (it->second) {
// 兄弟节点在左边,当前哈希在右边
currentHash = calculateHash(currentHash + it->first);
} else {
// 兄弟节点在右边,当前哈希在左边
currentHash = calculateHash(it->first + currentHash);
}
}
return currentHash == root_->getHash();
}
void MerkleTree::printTree() const {
std::cout << " MerkleTree::printTree " << std::endl;
if (!root_) {
std::cout << " Empty tree" << std::endl;
return;
}
printNode(root_);
}
void MerkleTree::printNode(std::shared_ptr<MerkleNode> node, int level) const {
if (!node) return;
std::string indent(level * 2, ' ');
std::cout << " " << indent << node->getName() << " Level " << node->getLevel() << ": " << node->getHash() << std::endl;
if (node->getLeft()) {
printNode(node->getLeft(), level + 1);
}
if (node->getRight()) {
printNode(node->getRight(), level + 1);
}
}
std::shared_ptr<MerkleNode> MerkleTree::buildTree(const std::vector<std::shared_ptr<MerkleNode>>& nodes, int level) {
std::cout << " MerkleTree::buildTree " << std::endl;
if (nodes.empty()) return nullptr;
std::vector<std::shared_ptr<MerkleNode>> newNodes;
for (size_t i = 0; i < nodes.size(); i += 2) {
auto left = nodes[i];
auto right = (i + 1 < nodes.size()) ? nodes[i + 1] : left;
auto parent = std::make_shared<MerkleNode>(left, right);
parent->setLevel(level + 1);
newNodes.push_back(parent);
}
if (newNodes.size() == 1) {
return newNodes[0];
}
return buildTree(newNodes, level + 1);
}
std::string MerkleTree::calculateHash(const std::string& data) const {
return ::calculateHash(data);
}