Skip to content

Commit 5786a81

Browse files
glozowjl2012
andcommitted
Verify that all validation flags are backward compatible
See #10699, i.e. adding a flag should always reduce the number of acceptable scripts. Co-authored-by: Johnson Lau <[email protected]>
1 parent b10ce9a commit 5786a81

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/test/transaction_tests.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <streams.h>
2323
#include <test/util/transaction_utils.h>
2424
#include <util/strencodings.h>
25+
#include <util/string.h>
2526
#include <validation.h>
2627

2728
#include <functional>
@@ -230,6 +231,21 @@ BOOST_AUTO_TEST_CASE(tx_valid)
230231
if (!CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, ~verify_flags, txdata, strTest, /* expect_valid */ true)) {
231232
BOOST_ERROR("Tx unexpectedly failed: " << strTest);
232233
}
234+
235+
// Backwards compatibility of script verification flags: Removing any flag(s) should not invalidate a valid transaction
236+
for (size_t i = 0; i < mapFlagNames.size(); ++i) {
237+
// Removing individual flags
238+
unsigned int flags = TrimFlags(~(verify_flags | (1U << i)));
239+
if (!CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, flags, txdata, strTest, /* expect_valid */ true)) {
240+
BOOST_ERROR("Tx unexpectedly failed with flag " << ToString(i) << " unset: " << strTest);
241+
}
242+
// Removing random combinations of flags
243+
flags = TrimFlags(~(verify_flags | (unsigned int)InsecureRandBits(mapFlagNames.size())));
244+
if (!CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, flags, txdata, strTest, /* expect_valid */ true)) {
245+
BOOST_ERROR("Tx unexpectedly failed with random flags " << ToString(flags) << ": " << strTest);
246+
}
247+
}
248+
233249
// Check that flags are maximal: transaction should fail if any unset flags are set.
234250
for (auto flags_excluding_one: ExcludeIndividualFlags(verify_flags)) {
235251
if (!CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, ~flags_excluding_one, txdata, strTest, /* expect_valid */ false)) {
@@ -302,6 +318,21 @@ BOOST_AUTO_TEST_CASE(tx_invalid)
302318
if (!CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, verify_flags, txdata, strTest, /* expect_valid */ false)) {
303319
BOOST_ERROR("Tx unexpectedly passed: " << strTest);
304320
}
321+
322+
// Backwards compatibility of script verification flags: Adding any flag(s) should not validate an invalid transaction
323+
for (size_t i = 0; i < mapFlagNames.size(); i++) {
324+
unsigned int flags = FillFlags(verify_flags | (1U << i));
325+
// Adding individual flags
326+
if (!CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, flags, txdata, strTest, /* expect_valid */ false)) {
327+
BOOST_ERROR("Tx unexpectedly passed with flag " << ToString(i) << " set: " << strTest);
328+
}
329+
// Adding random combinations of flags
330+
flags = FillFlags(verify_flags | (unsigned int)InsecureRandBits(mapFlagNames.size()));
331+
if (!CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, flags, txdata, strTest, /* expect_valid */ false)) {
332+
BOOST_ERROR("Tx unexpectedly passed with random flags " << ToString(flags) << ": " << strTest);
333+
}
334+
}
335+
305336
// Check that flags are minimal: transaction should succeed if any set flags are unset.
306337
for (auto flags_excluding_one: ExcludeIndividualFlags(verify_flags)) {
307338
if (!CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, flags_excluding_one, txdata, strTest, /* expect_valid */ true)) {

0 commit comments

Comments
 (0)