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 1 commit
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
22 changes: 17 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,36 @@ 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
if (!initialized) {
LOCK(cs_EmptyMerkleRoots_private_variables_write);
zebambam marked this conversation as resolved.
Show resolved Hide resolved
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:
CCriticalSection cs_EmptyMerkleRoots_private_variables_write;
zebambam marked this conversation as resolved.
Show resolved Hide resolved
bool initialized;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be better as a std::atomic<bool>. We could then replace the locking with initialized.compare_exchange_strong(...).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

template<size_t Depth, typename Hash>
class EmptyMerkleRoots {
public:
    EmptyMerkleRoots() : initialized(false) { }
    void initialize() const {
        bool unitialized = false;
        if (initialized.compare_exchange_strong(unitialized, true)) {
            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);
            }
        }
    }
    Hash empty_root(size_t depth) const {
        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:
    mutable std::atomic<bool> initialized;
    mutable std::array<Hash, Depth+1> empty_roots;
};

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a neat way to do it I guess. I'm not sure that it's so much better than what's proposed that we should hold up the PR though. Maybe someone else disagrees.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. The two ways should both be fine.

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