Port anchor_pop_regression_test to gtest suite CoinsTests

This commit is contained in:
sasha 2022-04-26 15:07:41 -07:00
parent 6cd441accf
commit 8fa95217f6
2 changed files with 94 additions and 86 deletions

View File

@ -445,3 +445,97 @@ TEST(CoinsTests, NullifierRegression)
checkNullifierCache(cache1, txWithNullifiers, false);
}
}
template<typename Tree> void anchorPopRegressionTestImpl(ShieldedType type)
{
// Correct behavior:
{
CCoinsViewTest base;
CCoinsViewCacheTest cache1(&base);
// Create dummy anchor/commitment
Tree tree;
AppendRandomLeaf(tree);
// Add the anchor
cache1.PushAnchor(tree);
cache1.Flush();
// Remove the anchor
cache1.PopAnchor(Tree::empty_root(), type);
cache1.Flush();
// Add the anchor back
cache1.PushAnchor(tree);
cache1.Flush();
// The base contains the anchor, of course!
{
Tree checkTree;
EXPECT_TRUE(GetAnchorAt(cache1, tree.root(), checkTree));
EXPECT_TRUE(checkTree.root() == tree.root());
}
}
// Previously incorrect behavior
{
CCoinsViewTest base;
CCoinsViewCacheTest cache1(&base);
// Create dummy anchor/commitment
Tree tree;
AppendRandomLeaf(tree);
// Add the anchor and flush to disk
cache1.PushAnchor(tree);
cache1.Flush();
// Remove the anchor, but don't flush yet!
cache1.PopAnchor(Tree::empty_root(), type);
{
CCoinsViewCacheTest cache2(&cache1); // Build cache on top
cache2.PushAnchor(tree); // Put the same anchor back!
cache2.Flush(); // Flush to cache1
}
// cache2's flush kinda worked, i.e. cache1 thinks the
// tree is there, but it didn't bring down the correct
// treestate...
{
Tree checktree;
EXPECT_TRUE(GetAnchorAt(cache1, tree.root(), checktree));
EXPECT_TRUE(checktree.root() == tree.root()); // Oh, shucks.
}
// Flushing cache won't help either, just makes the inconsistency
// permanent.
cache1.Flush();
{
Tree checktree;
EXPECT_TRUE(GetAnchorAt(cache1, tree.root(), checktree));
EXPECT_TRUE(checktree.root() == tree.root()); // Oh, shucks.
}
}
}
TEST(CoinsTests, AnchorPopRegression)
{
LoadProofParameters();
{
SCOPED_TRACE("Sprout");
anchorPopRegressionTestImpl<SproutMerkleTree>(SPROUT);
}
{
SCOPED_TRACE("Sapling");
anchorPopRegressionTestImpl<SaplingMerkleTree>(SAPLING);
}
{
SCOPED_TRACE("Orchard");
anchorPopRegressionTestImpl<OrchardMerkleFrontier>(ORCHARD);
}
}

View File

@ -350,92 +350,6 @@ void checkNullifierCache(const CCoinsViewCacheTest &cache, const TxWithNullifier
BOOST_CHECK(containsOrchardNullifier == shouldBeInCache);
}
template<typename Tree> void anchorPopRegressionTestImpl(ShieldedType type)
{
// Correct behavior:
{
CCoinsViewTest base;
CCoinsViewCacheTest cache1(&base);
// Create dummy anchor/commitment
Tree tree;
AppendRandomLeaf(tree);
// Add the anchor
cache1.PushAnchor(tree);
cache1.Flush();
// Remove the anchor
cache1.PopAnchor(Tree::empty_root(), type);
cache1.Flush();
// Add the anchor back
cache1.PushAnchor(tree);
cache1.Flush();
// The base contains the anchor, of course!
{
Tree checkTree;
BOOST_CHECK(GetAnchorAt(cache1, tree.root(), checkTree));
BOOST_CHECK(checkTree.root() == tree.root());
}
}
// Previously incorrect behavior
{
CCoinsViewTest base;
CCoinsViewCacheTest cache1(&base);
// Create dummy anchor/commitment
Tree tree;
AppendRandomLeaf(tree);
// Add the anchor and flush to disk
cache1.PushAnchor(tree);
cache1.Flush();
// Remove the anchor, but don't flush yet!
cache1.PopAnchor(Tree::empty_root(), type);
{
CCoinsViewCacheTest cache2(&cache1); // Build cache on top
cache2.PushAnchor(tree); // Put the same anchor back!
cache2.Flush(); // Flush to cache1
}
// cache2's flush kinda worked, i.e. cache1 thinks the
// tree is there, but it didn't bring down the correct
// treestate...
{
Tree checktree;
BOOST_CHECK(GetAnchorAt(cache1, tree.root(), checktree));
BOOST_CHECK(checktree.root() == tree.root()); // Oh, shucks.
}
// Flushing cache won't help either, just makes the inconsistency
// permanent.
cache1.Flush();
{
Tree checktree;
BOOST_CHECK(GetAnchorAt(cache1, tree.root(), checktree));
BOOST_CHECK(checktree.root() == tree.root()); // Oh, shucks.
}
}
}
BOOST_AUTO_TEST_CASE(anchor_pop_regression_test)
{
BOOST_TEST_CONTEXT("Sprout") {
anchorPopRegressionTestImpl<SproutMerkleTree>(SPROUT);
}
BOOST_TEST_CONTEXT("Sapling") {
anchorPopRegressionTestImpl<SaplingMerkleTree>(SAPLING);
}
BOOST_TEST_CONTEXT("Orchard") {
anchorPopRegressionTestImpl<OrchardMerkleFrontier>(ORCHARD);
}
}
template<typename Tree> void anchorRegressionTestImpl(ShieldedType type)
{
// Correct behavior: