Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deferred initialization of EmptyMerklePath structures till first use … #4131

Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/zcash/IncrementalMerkleTree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "uint256.h"
#include "serialize.h"
#include "sync.h"

#include "Zcash.h"
#include "zcash/util.h"
Expand Down Expand Up @@ -57,25 +58,34 @@ class MerklePath {
template<size_t Depth, typename Hash>
class EmptyMerkleRoots {
public:
EmptyMerkleRoots() {
empty_roots.at(0) = Hash::uncommitted();
for (size_t d = 1; d <= Depth; d++) {
empty_roots.at(d) = Hash::combine(empty_roots.at(d-1), empty_roots.at(d-1), d-1);
void initialize() {
zebambam marked this conversation as resolved.
Show resolved Hide resolved
LOCK(cs);
if (!initialized) {
empty_roots.at(0) = Hash::uncommitted();
zebambam marked this conversation as resolved.
Show resolved Hide resolved
for (size_t d = 1; d <= Depth; d++) {
empty_roots.at(d) = Hash::combine(empty_roots.at(d-1), empty_roots.at(d-1), d-1);
}
initialized = true;
}
}
EmptyMerkleRoots() { initialized = false; }
Hash empty_root(size_t depth) {
zebambam marked this conversation as resolved.
Show resolved Hide resolved
initialize();
return empty_roots.at(depth);
}
template <size_t D, typename H>
friend bool operator==(const EmptyMerkleRoots<D, H>& a,
const EmptyMerkleRoots<D, H>& b);
private:
std::array<Hash, Depth+1> empty_roots;
CCriticalSection cs;
mutable bool initialized;
mutable std::array<Hash, Depth+1> empty_roots;
};

template<size_t Depth, typename Hash>
bool operator==(const EmptyMerkleRoots<Depth, Hash>& a,
const EmptyMerkleRoots<Depth, Hash>& b) {
a.initialize(); b.initialize();
zebambam marked this conversation as resolved.
Show resolved Hide resolved
return a.empty_roots == b.empty_roots;
}

Expand Down