-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodes.cpp
43 lines (36 loc) · 1.61 KB
/
nodes.cpp
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
#include "nodes.hpp"
namespace merkle {
void HashOfLeaf::updateHash(const ByteSequence& key, const ByteSequence& value) {
size_t size = key.size();
auto* size_p = reinterpret_cast<char*>(&size);
ByteSequence to_hash;
to_hash.reserve(sizeof(size) + key.size() + value.size());
std::copy(size_p, size_p + sizeof(size), std::back_insert_iterator(to_hash));
std::copy(key.cbegin(), key.cend(), std::back_insert_iterator(to_hash));
std::copy(value.cbegin(), value.cend(), std::back_insert_iterator(to_hash));
computeSHA256<ByteSequence>(to_hash, getMutableHash());
}
HashOfLeaf::HashOfLeaf(const ByteSequence& key, const ByteSequence& value) {
updateHash(key, value);
}
const ByteSequence BranchNode::kNullNodeToHash = {0};
unsigned char BranchNode::kNullNodeHash[SHA256_DIGEST_LENGTH] = {};
void BranchNode::computeHash() {
ByteSequence to_hash;
for (const auto& child : children_) {
if (child == nullptr) {
to_hash.insert(to_hash.end(), kNullNodeHash, kNullNodeHash + SHA256_DIGEST_LENGTH);
} else {
to_hash.insert(to_hash.end(), child->hash(), child->hash() + SHA256_DIGEST_LENGTH);
}
}
computeSHA256<ByteSequence>(to_hash, getMutableHash());
}
void BranchNode::updateHashOfLeafChild(Byte child, const ByteSequence& key,
const ByteSequence& value) {
assert(children_[child] != nullptr);
assert(children_[child]->getType() == Node::Type::HashOfLeaf);
auto* hashOfLeaf = static_cast<merkle::HashOfLeaf*>(children_[child].get());
hashOfLeaf->updateHash(key, value);
}
} // namespace merkle