diff --git a/src/test/coins_tests.cpp b/src/test/coins_tests.cpp index ca2cbb0d4..2b26012ed 100644 --- a/src/test/coins_tests.cpp +++ b/src/test/coins_tests.cpp @@ -311,11 +311,27 @@ uint256 appendRandomSproutCommitment(SproutMerkleTree &tree) return cm; } +template 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 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 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 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(SAPLING); } + BOOST_TEST_CONTEXT("Orchard") { + anchorPopRegressionTestImpl(ORCHARD); + } } template void anchorRegressionTestImpl(ShieldedType type) @@ -514,7 +533,7 @@ template 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 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 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 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(SAPLING); } + BOOST_TEST_CONTEXT("Orchard") { + anchorRegressionTestImpl(ORCHARD); + } } BOOST_AUTO_TEST_CASE(nullifiers_test) @@ -628,7 +650,7 @@ template 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(SAPLING); } + BOOST_TEST_CONTEXT("Orchard") { + anchorsFlushImpl(ORCHARD); + } } BOOST_AUTO_TEST_CASE(chained_joinsplits) @@ -753,13 +778,13 @@ template 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 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(SAPLING); } + BOOST_TEST_CONTEXT("Orchard") { + anchorsTestImpl(ORCHARD); + } } static const unsigned int NUM_SIMULATION_ITERATIONS = 40000;