Fixed a couple incremental merkle tree bugs breaking consistency checks.
This commit is contained in:
parent
2c2dd2305e
commit
cf47198370
|
@ -113,14 +113,13 @@ bool CCoinsViewCache::GetAnchorAt(const uint256 &rt, libzerocash::IncrementalMer
|
|||
}
|
||||
}
|
||||
|
||||
CAnchorsCacheEntry entry;
|
||||
if (!base->GetAnchorAt(rt, tree)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
entry.entered = true;
|
||||
entry.tree.setTo(tree);
|
||||
cacheAnchors.insert(std::make_pair(rt, entry));
|
||||
CAnchorsMap::iterator ret = cacheAnchors.insert(std::make_pair(rt, CAnchorsCacheEntry())).first;
|
||||
ret->second.entered = true;
|
||||
ret->second.tree.setTo(tree);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -93,7 +93,10 @@ public:
|
|||
}
|
||||
for (CAnchorsMap::iterator it = mapAnchors.begin(); it != mapAnchors.end(); ) {
|
||||
if (it->second.entered) {
|
||||
mapAnchors_[it->first] = it->second.tree;
|
||||
std::map<uint256, libzerocash::IncrementalMerkleTree>::iterator ret =
|
||||
mapAnchors_.insert(std::make_pair(it->first, IncrementalMerkleTree(INCREMENTAL_MERKLE_TREE_DEPTH))).first;
|
||||
|
||||
ret->second.setTo(it->second.tree);
|
||||
} else {
|
||||
mapAnchors_.erase(it->first);
|
||||
}
|
||||
|
@ -173,6 +176,45 @@ void appendRandomCommitment(IncrementalMerkleTree &tree)
|
|||
tree.insertElement(commitment, index);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(anchors_flush_test)
|
||||
{
|
||||
CCoinsViewTest base;
|
||||
uint256 newrt;
|
||||
{
|
||||
CCoinsViewCacheTest cache(&base);
|
||||
IncrementalMerkleTree tree(INCREMENTAL_MERKLE_TREE_DEPTH);
|
||||
BOOST_CHECK(cache.GetAnchorAt(cache.GetBestAnchor(), tree));
|
||||
appendRandomCommitment(tree);
|
||||
|
||||
{
|
||||
std::vector<unsigned char> newrt_v(32);
|
||||
tree.getRootValue(newrt_v);
|
||||
newrt = uint256(newrt_v);
|
||||
}
|
||||
|
||||
cache.PushAnchor(tree);
|
||||
cache.Flush();
|
||||
}
|
||||
|
||||
{
|
||||
CCoinsViewCacheTest cache(&base);
|
||||
IncrementalMerkleTree tree(INCREMENTAL_MERKLE_TREE_DEPTH);
|
||||
BOOST_CHECK(cache.GetAnchorAt(cache.GetBestAnchor(), tree));
|
||||
|
||||
// Get the cached entry.
|
||||
BOOST_CHECK(cache.GetAnchorAt(cache.GetBestAnchor(), tree));
|
||||
|
||||
uint256 check_rt;
|
||||
{
|
||||
std::vector<unsigned char> newrt_v(32);
|
||||
tree.getRootValue(newrt_v);
|
||||
check_rt = uint256(newrt_v);
|
||||
}
|
||||
|
||||
BOOST_CHECK(check_rt == newrt);
|
||||
}
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(anchors_test)
|
||||
{
|
||||
// TODO: These tests should be more methodical.
|
||||
|
|
|
@ -1077,12 +1077,11 @@ bool CWallet::WitnessBucketCommitment(uint256 &commitment,
|
|||
std::vector<bool> index;
|
||||
std::vector<unsigned char> commitment_value(bucket_commitment.begin(), bucket_commitment.end());
|
||||
libzerocash::convertBytesVectorToVector(commitment_value, commitment_bv);
|
||||
tree.insertElement(commitment_bv, index);
|
||||
assert(tree.insertElement(commitment_bv, index));
|
||||
|
||||
if (bucket_commitment == commitment) {
|
||||
// We've found it! Now, we construct a witness.
|
||||
res = true;
|
||||
tree.prune();
|
||||
commitment_index = index;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue