coins_tests: Update tests to include Orchard case.

Co-authored-by: Jack Grigg <jack@electriccoin.co>
This commit is contained in:
therealyingtong 2022-03-09 22:52:39 +08:00
parent 663f8d434a
commit 26b8ec5562
1 changed files with 45 additions and 17 deletions

View File

@ -311,11 +311,27 @@ uint256 appendRandomSproutCommitment(SproutMerkleTree &tree)
return cm;
}
template<typename Tree> void AppendRandomLeaf(Tree &tree);
template<> void AppendRandomLeaf(SproutMerkleTree &tree) { tree.append(GetRandHash()); }
template<> void AppendRandomLeaf(SaplingMerkleTree &tree) { tree.append(GetRandHash()); }
template<> void AppendRandomLeaf(OrchardMerkleFrontier &tree) {
// OrchardMerkleFrontier only has APIs to append entire bundles, but
// fortunately the tests only require that the tree root change.
// TODO: Remove the need to create proofs by having a testing-only way to
// append a random leaf to OrchardMerkleFrontier.
uint256 orchardAnchor;
uint256 dataToBeSigned;
auto builder = orchard::Builder(true, true, orchardAnchor);
auto bundle = builder.Build().value().ProveAndSign(dataToBeSigned).value();
tree.AppendBundle(bundle);
}
template<typename Tree> bool GetAnchorAt(const CCoinsViewCacheTest &cache, const uint256 &rt, Tree &tree);
template<> bool GetAnchorAt(const CCoinsViewCacheTest &cache, const uint256 &rt, SproutMerkleTree &tree) { return cache.GetSproutAnchorAt(rt, tree); }
template<> bool GetAnchorAt(const CCoinsViewCacheTest &cache, const uint256 &rt, SaplingMerkleTree &tree) { return cache.GetSaplingAnchorAt(rt, tree); }
template<> bool GetAnchorAt(const CCoinsViewCacheTest &cache, const uint256 &rt, OrchardMerkleFrontier &tree) { return cache.GetOrchardAnchorAt(rt, tree); }
BOOST_FIXTURE_TEST_SUITE(coins_tests, BasicTestingSetup)
BOOST_FIXTURE_TEST_SUITE(coins_tests, JoinSplitTestingSetup)
void checkNullifierCache(const CCoinsViewCacheTest &cache, const TxWithNullifiers &txWithNullifiers, bool shouldBeInCache) {
// Make sure the nullifiers have not gotten mixed up
@ -431,7 +447,7 @@ template<typename Tree> void anchorPopRegressionTestImpl(ShieldedType type)
// Create dummy anchor/commitment
Tree tree;
tree.append(GetRandHash());
AppendRandomLeaf(tree);
// Add the anchor
cache1.PushAnchor(tree);
@ -460,7 +476,7 @@ template<typename Tree> void anchorPopRegressionTestImpl(ShieldedType type)
// Create dummy anchor/commitment
Tree tree;
tree.append(GetRandHash());
AppendRandomLeaf(tree);
// Add the anchor and flush to disk
cache1.PushAnchor(tree);
@ -503,6 +519,9 @@ BOOST_AUTO_TEST_CASE(anchor_pop_regression_test)
BOOST_TEST_CONTEXT("Sapling") {
anchorPopRegressionTestImpl<SaplingMerkleTree>(SAPLING);
}
BOOST_TEST_CONTEXT("Orchard") {
anchorPopRegressionTestImpl<OrchardMerkleFrontier>(ORCHARD);
}
}
template<typename Tree> void anchorRegressionTestImpl(ShieldedType type)
@ -514,7 +533,7 @@ template<typename Tree> void anchorRegressionTestImpl(ShieldedType type)
// Insert anchor into base.
Tree tree;
tree.append(GetRandHash());
AppendRandomLeaf(tree);
cache1.PushAnchor(tree);
cache1.Flush();
@ -531,7 +550,7 @@ template<typename Tree> void anchorRegressionTestImpl(ShieldedType type)
// Insert anchor into base.
Tree tree;
tree.append(GetRandHash());
AppendRandomLeaf(tree);
cache1.PushAnchor(tree);
cache1.Flush();
@ -548,7 +567,7 @@ template<typename Tree> void anchorRegressionTestImpl(ShieldedType type)
// Insert anchor into base.
Tree tree;
tree.append(GetRandHash());
AppendRandomLeaf(tree);
cache1.PushAnchor(tree);
cache1.Flush();
@ -571,7 +590,7 @@ template<typename Tree> void anchorRegressionTestImpl(ShieldedType type)
// Insert anchor into base.
Tree tree;
tree.append(GetRandHash());
AppendRandomLeaf(tree);
cache1.PushAnchor(tree);
cache1.Flush();
@ -595,6 +614,9 @@ BOOST_AUTO_TEST_CASE(anchor_regression_test)
BOOST_TEST_CONTEXT("Sapling") {
anchorRegressionTestImpl<SaplingMerkleTree>(SAPLING);
}
BOOST_TEST_CONTEXT("Orchard") {
anchorRegressionTestImpl<OrchardMerkleFrontier>(ORCHARD);
}
}
BOOST_AUTO_TEST_CASE(nullifiers_test)
@ -628,7 +650,7 @@ template<typename Tree> void anchorsFlushImpl(ShieldedType type)
CCoinsViewCacheTest cache(&base);
Tree tree;
BOOST_CHECK(GetAnchorAt(cache, cache.GetBestAnchor(type), tree));
tree.append(GetRandHash());
AppendRandomLeaf(tree);
newrt = tree.root();
@ -658,6 +680,9 @@ BOOST_AUTO_TEST_CASE(anchors_flush_test)
BOOST_TEST_CONTEXT("Sapling") {
anchorsFlushImpl<SaplingMerkleTree>(SAPLING);
}
BOOST_TEST_CONTEXT("Orchard") {
anchorsFlushImpl<OrchardMerkleFrontier>(ORCHARD);
}
}
BOOST_AUTO_TEST_CASE(chained_joinsplits)
@ -753,13 +778,13 @@ template<typename Tree> void anchorsTestImpl(ShieldedType type)
BOOST_CHECK(GetAnchorAt(cache, cache.GetBestAnchor(type), tree));
BOOST_CHECK(cache.GetBestAnchor(type) == tree.root());
tree.append(GetRandHash());
tree.append(GetRandHash());
tree.append(GetRandHash());
tree.append(GetRandHash());
tree.append(GetRandHash());
tree.append(GetRandHash());
tree.append(GetRandHash());
AppendRandomLeaf(tree);
AppendRandomLeaf(tree);
AppendRandomLeaf(tree);
AppendRandomLeaf(tree);
AppendRandomLeaf(tree);
AppendRandomLeaf(tree);
AppendRandomLeaf(tree);
Tree save_tree_for_later;
save_tree_for_later = tree;
@ -777,8 +802,8 @@ template<typename Tree> void anchorsTestImpl(ShieldedType type)
BOOST_CHECK(confirm_same.root() == newrt);
}
tree.append(GetRandHash());
tree.append(GetRandHash());
AppendRandomLeaf(tree);
AppendRandomLeaf(tree);
newrt2 = tree.root();
@ -816,6 +841,9 @@ BOOST_AUTO_TEST_CASE(anchors_test)
BOOST_TEST_CONTEXT("Sapling") {
anchorsTestImpl<SaplingMerkleTree>(SAPLING);
}
BOOST_TEST_CONTEXT("Orchard") {
anchorsTestImpl<OrchardMerkleFrontier>(ORCHARD);
}
}
static const unsigned int NUM_SIMULATION_ITERATIONS = 40000;