Skip to content

Commit 5aee73d

Browse files
committed
[test] minor improvements / followups
Add missing script verify flags to mapFlagNames. iterate through mapFlagNames values instead of bits. BOOST_CHECK_MESSAGE better reports which test failed exactly, whereas BOOST_ERROR was just incrementing the error counter.
1 parent 8a365df commit 5aee73d

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

src/test/transaction_tests.cpp

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <script/signingprovider.h>
2121
#include <script/standard.h>
2222
#include <streams.h>
23+
#include <test/util/script.h>
2324
#include <test/util/transaction_utils.h>
2425
#include <util/strencodings.h>
2526
#include <util/string.h>
@@ -59,6 +60,9 @@ static std::map<std::string, unsigned int> mapFlagNames = {
5960
{std::string("WITNESS_PUBKEYTYPE"), (unsigned int)SCRIPT_VERIFY_WITNESS_PUBKEYTYPE},
6061
{std::string("CONST_SCRIPTCODE"), (unsigned int)SCRIPT_VERIFY_CONST_SCRIPTCODE},
6162
{std::string("TAPROOT"), (unsigned int)SCRIPT_VERIFY_TAPROOT},
63+
{std::string("DISCOURAGE_UPGRADABLE_PUBKEYTYPE"), (unsigned int)SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_PUBKEYTYPE},
64+
{std::string("DISCOURAGE_OP_SUCCESS"), (unsigned int)SCRIPT_VERIFY_DISCOURAGE_OP_SUCCESS},
65+
{std::string("DISCOURAGE_UPGRADABLE_TAPROOT_VERSION"), (unsigned int)SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_TAPROOT_VERSION},
6266
};
6367

6468
unsigned int ParseScriptFlags(std::string strFlags)
@@ -139,6 +143,7 @@ unsigned int TrimFlags(unsigned int flags)
139143

140144
// CLEANSTACK requires WITNESS (and transitively CLEANSTACK requires P2SH)
141145
if (!(flags & SCRIPT_VERIFY_WITNESS)) flags &= ~(unsigned int)SCRIPT_VERIFY_CLEANSTACK;
146+
Assert(IsValidFlagCombination(flags));
142147
return flags;
143148
}
144149

@@ -149,6 +154,7 @@ unsigned int FillFlags(unsigned int flags)
149154

150155
// WITNESS implies P2SH (and transitively CLEANSTACK implies P2SH)
151156
if (flags & SCRIPT_VERIFY_WITNESS) flags |= SCRIPT_VERIFY_P2SH;
157+
Assert(IsValidFlagCombination(flags));
152158
return flags;
153159
}
154160

@@ -231,16 +237,15 @@ BOOST_AUTO_TEST_CASE(tx_valid)
231237
BOOST_ERROR("Bad test flags: " << strTest);
232238
}
233239

234-
if (!CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, ~verify_flags, txdata, strTest, /* expect_valid */ true)) {
235-
BOOST_ERROR("Tx unexpectedly failed: " << strTest);
236-
}
240+
BOOST_CHECK_MESSAGE(CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, ~verify_flags, txdata, strTest, /* expect_valid */ true),
241+
"Tx unexpectedly failed: " << strTest);
237242

238243
// Backwards compatibility of script verification flags: Removing any flag(s) should not invalidate a valid transaction
239-
for (size_t i = 0; i < mapFlagNames.size(); ++i) {
244+
for (const auto& [name, flag] : mapFlagNames) {
240245
// Removing individual flags
241-
unsigned int flags = TrimFlags(~(verify_flags | (1U << i)));
246+
unsigned int flags = TrimFlags(~(verify_flags | flag));
242247
if (!CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, flags, txdata, strTest, /* expect_valid */ true)) {
243-
BOOST_ERROR("Tx unexpectedly failed with flag " << ToString(i) << " unset: " << strTest);
248+
BOOST_ERROR("Tx unexpectedly failed with flag " << name << " unset: " << strTest);
244249
}
245250
// Removing random combinations of flags
246251
flags = TrimFlags(~(verify_flags | (unsigned int)InsecureRandBits(mapFlagNames.size())));
@@ -250,7 +255,7 @@ BOOST_AUTO_TEST_CASE(tx_valid)
250255
}
251256

252257
// Check that flags are maximal: transaction should fail if any unset flags are set.
253-
for (auto flags_excluding_one: ExcludeIndividualFlags(verify_flags)) {
258+
for (auto flags_excluding_one : ExcludeIndividualFlags(verify_flags)) {
254259
if (!CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, ~flags_excluding_one, txdata, strTest, /* expect_valid */ false)) {
255260
BOOST_ERROR("Too many flags unset: " << strTest);
256261
}
@@ -317,27 +322,31 @@ BOOST_AUTO_TEST_CASE(tx_invalid)
317322
PrecomputedTransactionData txdata(tx);
318323
unsigned int verify_flags = ParseScriptFlags(test[2].get_str());
319324

320-
// Not using FillFlags() in the main test, in order to detect invalid verifyFlags combination
321-
if (!CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, verify_flags, txdata, strTest, /* expect_valid */ false)) {
322-
BOOST_ERROR("Tx unexpectedly passed: " << strTest);
325+
// Check that the test gives a valid combination of flags (otherwise VerifyScript will throw). Don't edit the flags.
326+
if (verify_flags != FillFlags(verify_flags)) {
327+
BOOST_ERROR("Bad test flags: " << strTest);
323328
}
324329

330+
// Not using FillFlags() in the main test, in order to detect invalid verifyFlags combination
331+
BOOST_CHECK_MESSAGE(CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, verify_flags, txdata, strTest, /* expect_valid */ false),
332+
"Tx unexpectedly passed: " << strTest);
333+
325334
// Backwards compatibility of script verification flags: Adding any flag(s) should not validate an invalid transaction
326-
for (size_t i = 0; i < mapFlagNames.size(); i++) {
327-
unsigned int flags = FillFlags(verify_flags | (1U << i));
335+
for (const auto& [name, flag] : mapFlagNames) {
336+
unsigned int flags = FillFlags(verify_flags | flag);
328337
// Adding individual flags
329338
if (!CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, flags, txdata, strTest, /* expect_valid */ false)) {
330-
BOOST_ERROR("Tx unexpectedly passed with flag " << ToString(i) << " set: " << strTest);
339+
BOOST_ERROR("Tx unexpectedly passed with flag " << name << " set: " << strTest);
331340
}
332341
// Adding random combinations of flags
333342
flags = FillFlags(verify_flags | (unsigned int)InsecureRandBits(mapFlagNames.size()));
334343
if (!CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, flags, txdata, strTest, /* expect_valid */ false)) {
335-
BOOST_ERROR("Tx unexpectedly passed with random flags " << ToString(flags) << ": " << strTest);
344+
BOOST_ERROR("Tx unexpectedly passed with random flags " << name << ": " << strTest);
336345
}
337346
}
338347

339348
// Check that flags are minimal: transaction should succeed if any set flags are unset.
340-
for (auto flags_excluding_one: ExcludeIndividualFlags(verify_flags)) {
349+
for (auto flags_excluding_one : ExcludeIndividualFlags(verify_flags)) {
341350
if (!CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, flags_excluding_one, txdata, strTest, /* expect_valid */ true)) {
342351
BOOST_ERROR("Too many flags set: " << strTest);
343352
}

0 commit comments

Comments
 (0)