Skip to content

Commit 2687950

Browse files
committed
Add comments to descriptor tests
1 parent 82df4c6 commit 2687950

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/test/descriptor_tests.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,28 +79,42 @@ void Check(const std::string& prv, const std::string& pub, int flags, const std:
7979
BOOST_CHECK_EQUAL(parse_pub->IsRange(), (flags & RANGE) != 0);
8080
BOOST_CHECK_EQUAL(parse_priv->IsRange(), (flags & RANGE) != 0);
8181

82-
83-
// Is not ranged descriptor, only a single result is expected.
82+
// * For ranged descriptors, the `scripts` parameter is a list of expected result outputs, for subsequent
83+
// positions to evaluate the descriptors on (so the first element of `scripts` is for evaluating the
84+
// descriptor at 0; the second at 1; and so on). To verify this, we evaluate the descriptors once for
85+
// each element in `scripts`.
86+
// * For non-ranged descriptors, we evaluate the descriptors at positions 0, 1, and 2, but expect the
87+
// same result in each case, namely the first element of `scripts`. Because of that, the size of
88+
// `scripts` must be one in that case.
8489
if (!(flags & RANGE)) assert(scripts.size() == 1);
85-
8690
size_t max = (flags & RANGE) ? scripts.size() : 3;
91+
92+
// Iterate over the position we'll evaluate the descriptors in.
8793
for (size_t i = 0; i < max; ++i) {
94+
// Call the expected result scripts `ref`.
8895
const auto& ref = scripts[(flags & RANGE) ? i : 0];
96+
// When t=0, evaluate the `prv` descriptor; when t=1, evaluate the `pub` descriptor.
8997
for (int t = 0; t < 2; ++t) {
98+
// When the descriptor is hardened, evaluate with access to the private keys inside.
9099
const FlatSigningProvider& key_provider = (flags & HARDENED) ? keys_priv : keys_pub;
100+
101+
// Evaluate the descriptor selected by `t` in poisition `i`.
91102
FlatSigningProvider script_provider, script_provider_cached;
92103
std::vector<CScript> spks, spks_cached;
93104
std::vector<unsigned char> cache;
94105
BOOST_CHECK((t ? parse_priv : parse_pub)->Expand(i, key_provider, spks, script_provider, &cache));
95106

107+
// Compare the output with the expected result.
108+
BOOST_CHECK_EQUAL(spks.size(), ref.size());
109+
96110
// Try to expand again using cached data, and compare.
97111
BOOST_CHECK(parse_pub->ExpandFromCache(i, cache, spks_cached, script_provider_cached));
98112
BOOST_CHECK(spks == spks_cached);
99113
BOOST_CHECK(script_provider.pubkeys == script_provider_cached.pubkeys);
100114
BOOST_CHECK(script_provider.scripts == script_provider_cached.scripts);
101115
BOOST_CHECK(script_provider.origins == script_provider_cached.origins);
102116

103-
BOOST_CHECK_EQUAL(spks.size(), ref.size());
117+
// For each of the produced scripts, verify solvability, and when possible, try to sign a transaction spending it.
104118
for (size_t n = 0; n < spks.size(); ++n) {
105119
BOOST_CHECK_EQUAL(ref[n], HexStr(spks[n].begin(), spks[n].end()));
106120
BOOST_CHECK_EQUAL(IsSolvable(Merge(key_provider, script_provider), spks[n]), (flags & UNSOLVABLE) == 0);
@@ -132,6 +146,7 @@ void Check(const std::string& prv, const std::string& pub, int flags, const std:
132146
}
133147
}
134148
}
149+
135150
// Verify no expected paths remain that were not observed.
136151
BOOST_CHECK_MESSAGE(left_paths.empty(), "Not all expected key paths found: " + prv);
137152
}

0 commit comments

Comments
 (0)