Skip to content

Commit c090262

Browse files
committed
Merge #10699: Make all script validation flags backward compatible
01013f5 Simplify tx validation tests (Pieter Wuille) 2dd6f80 Add a test that all flags are softforks (Pieter Wuille) 2851b77 Make all script verification flags softforks (Pieter Wuille) Pull request description: This change makes `SCRIPT_VERIFY_UPGRADABLE_NOPS` not apply to `OP_CHECKLOCKTIMEVERIFY` and `OP_CHECKSEQUENCEVERIFY`. This is a no-op as `UPGRADABLE_NOPS` is only set for mempool transactions, and those always have `SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY` and `SCRIPT_VERIFY_CHECKSEQUENCEVERIFY` set as well. The advantage is that setting more flags now always results in a reduction in acceptable scripts (=softfork). This results in a nice and testable property for validation, for which a new test is added. This also means that the introduction of a new definition for a NOP or witness version will likely need the following procedure (example OP_NOP8 here) * Remove OP_NOP8 from being affected by `SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS`. * Add a `SCRIPT_VERIFY_DISCOURAGE_NOP8`, which only applies to `OP_NOP8`. * Add a `SCRIPT_VERIFY_NOP8` which implements the new consensus logic. * Before activation, add `SCRIPT_VERIFY_DISCOURAGE_NOP8` to the mempool flags. * After activation, add `SCRIPT_VERIFY_NOP8` to both the mempool and consensus flags. Tree-SHA512: d3b4538986ecf646aac9dba13a8d89318baf9e308e258547ca3b99e7c0509747f323edac6b1fea4e87e7d3c01b71193794b41679ae4f86f6e11ed6be3fd62c72
2 parents 0e722e8 + 01013f5 commit c090262

File tree

5 files changed

+33
-31
lines changed

5 files changed

+33
-31
lines changed

src/script/interpreter.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,6 @@ bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript&
349349
{
350350
if (!(flags & SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY)) {
351351
// not enabled; treat as a NOP2
352-
if (flags & SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS) {
353-
return set_error(serror, SCRIPT_ERR_DISCOURAGE_UPGRADABLE_NOPS);
354-
}
355352
break;
356353
}
357354

@@ -391,9 +388,6 @@ bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript&
391388
{
392389
if (!(flags & SCRIPT_VERIFY_CHECKSEQUENCEVERIFY)) {
393390
// not enabled; treat as a NOP3
394-
if (flags & SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS) {
395-
return set_error(serror, SCRIPT_ERR_DISCOURAGE_UPGRADABLE_NOPS);
396-
}
397391
break;
398392
}
399393

src/script/interpreter.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,37 +27,40 @@ enum
2727
SIGHASH_ANYONECANPAY = 0x80,
2828
};
2929

30-
/** Script verification flags */
30+
/** Script verification flags.
31+
*
32+
* All flags are intended to be soft forks: the set of acceptable scripts under
33+
* flags (A | B) is a subset of the acceptable scripts under flag (A).
34+
*/
3135
enum
3236
{
3337
SCRIPT_VERIFY_NONE = 0,
3438

35-
// Evaluate P2SH subscripts (softfork safe, BIP16).
39+
// Evaluate P2SH subscripts (BIP16).
3640
SCRIPT_VERIFY_P2SH = (1U << 0),
3741

3842
// Passing a non-strict-DER signature or one with undefined hashtype to a checksig operation causes script failure.
3943
// Evaluating a pubkey that is not (0x04 + 64 bytes) or (0x02 or 0x03 + 32 bytes) by checksig causes script failure.
40-
// (softfork safe, but not used or intended as a consensus rule).
44+
// (not used or intended as a consensus rule).
4145
SCRIPT_VERIFY_STRICTENC = (1U << 1),
4246

43-
// Passing a non-strict-DER signature to a checksig operation causes script failure (softfork safe, BIP62 rule 1)
47+
// Passing a non-strict-DER signature to a checksig operation causes script failure (BIP62 rule 1)
4448
SCRIPT_VERIFY_DERSIG = (1U << 2),
4549

4650
// Passing a non-strict-DER signature or one with S > order/2 to a checksig operation causes script failure
47-
// (softfork safe, BIP62 rule 5).
51+
// (BIP62 rule 5).
4852
SCRIPT_VERIFY_LOW_S = (1U << 3),
4953

50-
// verify dummy stack item consumed by CHECKMULTISIG is of zero-length (softfork safe, BIP62 rule 7).
54+
// verify dummy stack item consumed by CHECKMULTISIG is of zero-length (BIP62 rule 7).
5155
SCRIPT_VERIFY_NULLDUMMY = (1U << 4),
5256

53-
// Using a non-push operator in the scriptSig causes script failure (softfork safe, BIP62 rule 2).
57+
// Using a non-push operator in the scriptSig causes script failure (BIP62 rule 2).
5458
SCRIPT_VERIFY_SIGPUSHONLY = (1U << 5),
5559

5660
// Require minimal encodings for all push operations (OP_0... OP_16, OP_1NEGATE where possible, direct
5761
// pushes up to 75 bytes, OP_PUSHDATA up to 255 bytes, OP_PUSHDATA2 for anything larger). Evaluating
5862
// any other push causes the script to fail (BIP62 rule 3).
5963
// In addition, whenever a stack element is interpreted as a number, it must be of minimal length (BIP62 rule 4).
60-
// (softfork safe)
6164
SCRIPT_VERIFY_MINIMALDATA = (1U << 6),
6265

6366
// Discourage use of NOPs reserved for upgrades (NOP1-10)
@@ -68,12 +71,14 @@ enum
6871
// discouraged NOPs fails the script. This verification flag will never be
6972
// a mandatory flag applied to scripts in a block. NOPs that are not
7073
// executed, e.g. within an unexecuted IF ENDIF block, are *not* rejected.
74+
// NOPs that have associated forks to give them new meaning (CLTV, CSV)
75+
// are not subject to this rule.
7176
SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS = (1U << 7),
7277

7378
// Require that only a single stack element remains after evaluation. This changes the success criterion from
7479
// "At least one stack element must remain, and when interpreted as a boolean, it must be true" to
7580
// "Exactly one stack element must remain, and when interpreted as a boolean, it must be true".
76-
// (softfork safe, BIP62 rule 6)
81+
// (BIP62 rule 6)
7782
// Note: CLEANSTACK should never be used without P2SH or WITNESS.
7883
SCRIPT_VERIFY_CLEANSTACK = (1U << 8),
7984

src/test/data/script_tests.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -862,8 +862,6 @@
862862

863863
["Ensure 100% coverage of discouraged NOPS"],
864864
["1", "NOP1", "P2SH,DISCOURAGE_UPGRADABLE_NOPS", "DISCOURAGE_UPGRADABLE_NOPS"],
865-
["1", "CHECKLOCKTIMEVERIFY", "P2SH,DISCOURAGE_UPGRADABLE_NOPS", "DISCOURAGE_UPGRADABLE_NOPS"],
866-
["1", "CHECKSEQUENCEVERIFY", "P2SH,DISCOURAGE_UPGRADABLE_NOPS", "DISCOURAGE_UPGRADABLE_NOPS"],
867865
["1", "NOP4", "P2SH,DISCOURAGE_UPGRADABLE_NOPS", "DISCOURAGE_UPGRADABLE_NOPS"],
868866
["1", "NOP5", "P2SH,DISCOURAGE_UPGRADABLE_NOPS", "DISCOURAGE_UPGRADABLE_NOPS"],
869867
["1", "NOP6", "P2SH,DISCOURAGE_UPGRADABLE_NOPS", "DISCOURAGE_UPGRADABLE_NOPS"],

src/test/script_tests.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,17 @@ void DoTest(const CScript& scriptPubKey, const CScript& scriptSig, const CScript
166166
CMutableTransaction tx2 = tx;
167167
BOOST_CHECK_MESSAGE(VerifyScript(scriptSig, scriptPubKey, &scriptWitness, flags, MutableTransactionSignatureChecker(&tx, 0, txCredit.vout[0].nValue), &err) == expect, message);
168168
BOOST_CHECK_MESSAGE(err == scriptError, std::string(FormatScriptError(err)) + " where " + std::string(FormatScriptError((ScriptError_t)scriptError)) + " expected: " + message);
169+
170+
// Verify that removing flags from a passing test or adding flags to a failing test does not change the result.
171+
for (int i = 0; i < 16; ++i) {
172+
int extra_flags = InsecureRandBits(16);
173+
int combined_flags = expect ? (flags & ~extra_flags) : (flags | extra_flags);
174+
// Weed out some invalid flag combinations.
175+
if (combined_flags & SCRIPT_VERIFY_CLEANSTACK && ~combined_flags & (SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS)) continue;
176+
if (combined_flags & SCRIPT_VERIFY_WITNESS && ~combined_flags & SCRIPT_VERIFY_P2SH) continue;
177+
BOOST_CHECK_MESSAGE(VerifyScript(scriptSig, scriptPubKey, &scriptWitness, combined_flags, MutableTransactionSignatureChecker(&tx, 0, txCredit.vout[0].nValue), &err) == expect, message + strprintf(" (with flags %x)", combined_flags));
178+
}
179+
169180
#if defined(HAVE_CONSENSUS_LIB)
170181
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
171182
stream << tx2;

src/test/txvalidationcache_tests.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ BOOST_FIXTURE_TEST_CASE(tx_mempool_block_doublespend, TestChain100Setup)
103103
// should fail.
104104
// Capture this interaction with the upgraded_nop argument: set it when evaluating
105105
// any script flag that is implemented as an upgraded NOP code.
106-
void ValidateCheckInputsForAllFlags(CMutableTransaction &tx, uint32_t failing_flags, bool add_to_cache, bool upgraded_nop)
106+
void ValidateCheckInputsForAllFlags(CMutableTransaction &tx, uint32_t failing_flags, bool add_to_cache)
107107
{
108108
PrecomputedTransactionData txdata(tx);
109109
// If we add many more flags, this loop can get too expensive, but we can
@@ -124,12 +124,6 @@ void ValidateCheckInputsForAllFlags(CMutableTransaction &tx, uint32_t failing_fl
124124
// CheckInputs should succeed iff test_flags doesn't intersect with
125125
// failing_flags
126126
bool expected_return_value = !(test_flags & failing_flags);
127-
if (expected_return_value && upgraded_nop) {
128-
// If the script flag being tested corresponds to an upgraded NOP,
129-
// then script execution should fail if DISCOURAGE_UPGRADABLE_NOPS
130-
// is set.
131-
expected_return_value = !(test_flags & SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS);
132-
}
133127
BOOST_CHECK_EQUAL(ret, expected_return_value);
134128

135129
// Test the caching
@@ -218,7 +212,7 @@ BOOST_FIXTURE_TEST_CASE(checkinputs_test, TestChain100Setup)
218212
// not present. Don't add these checks to the cache, so that we can
219213
// test later that block validation works fine in the absence of cached
220214
// successes.
221-
ValidateCheckInputsForAllFlags(spend_tx, SCRIPT_VERIFY_DERSIG | SCRIPT_VERIFY_LOW_S | SCRIPT_VERIFY_STRICTENC, false, false);
215+
ValidateCheckInputsForAllFlags(spend_tx, SCRIPT_VERIFY_DERSIG | SCRIPT_VERIFY_LOW_S | SCRIPT_VERIFY_STRICTENC, false);
222216

223217
// And if we produce a block with this tx, it should be valid (DERSIG not
224218
// enabled yet), even though there's no cache entry.
@@ -243,7 +237,7 @@ BOOST_FIXTURE_TEST_CASE(checkinputs_test, TestChain100Setup)
243237
std::vector<unsigned char> vchSig2(p2pk_scriptPubKey.begin(), p2pk_scriptPubKey.end());
244238
invalid_under_p2sh_tx.vin[0].scriptSig << vchSig2;
245239

246-
ValidateCheckInputsForAllFlags(invalid_under_p2sh_tx, SCRIPT_VERIFY_P2SH, true, false);
240+
ValidateCheckInputsForAllFlags(invalid_under_p2sh_tx, SCRIPT_VERIFY_P2SH, true);
247241
}
248242

249243
// Test CHECKLOCKTIMEVERIFY
@@ -266,7 +260,7 @@ BOOST_FIXTURE_TEST_CASE(checkinputs_test, TestChain100Setup)
266260
vchSig.push_back((unsigned char)SIGHASH_ALL);
267261
invalid_with_cltv_tx.vin[0].scriptSig = CScript() << vchSig << 101;
268262

269-
ValidateCheckInputsForAllFlags(invalid_with_cltv_tx, SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY, true, true);
263+
ValidateCheckInputsForAllFlags(invalid_with_cltv_tx, SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY, true);
270264

271265
// Make it valid, and check again
272266
invalid_with_cltv_tx.vin[0].scriptSig = CScript() << vchSig << 100;
@@ -294,7 +288,7 @@ BOOST_FIXTURE_TEST_CASE(checkinputs_test, TestChain100Setup)
294288
vchSig.push_back((unsigned char)SIGHASH_ALL);
295289
invalid_with_csv_tx.vin[0].scriptSig = CScript() << vchSig << 101;
296290

297-
ValidateCheckInputsForAllFlags(invalid_with_csv_tx, SCRIPT_VERIFY_CHECKSEQUENCEVERIFY, true, true);
291+
ValidateCheckInputsForAllFlags(invalid_with_csv_tx, SCRIPT_VERIFY_CHECKSEQUENCEVERIFY, true);
298292

299293
// Make it valid, and check again
300294
invalid_with_csv_tx.vin[0].scriptSig = CScript() << vchSig << 100;
@@ -323,11 +317,11 @@ BOOST_FIXTURE_TEST_CASE(checkinputs_test, TestChain100Setup)
323317
UpdateTransaction(valid_with_witness_tx, 0, sigdata);
324318

325319
// This should be valid under all script flags.
326-
ValidateCheckInputsForAllFlags(valid_with_witness_tx, 0, true, false);
320+
ValidateCheckInputsForAllFlags(valid_with_witness_tx, 0, true);
327321

328322
// Remove the witness, and check that it is now invalid.
329323
valid_with_witness_tx.vin[0].scriptWitness.SetNull();
330-
ValidateCheckInputsForAllFlags(valid_with_witness_tx, SCRIPT_VERIFY_WITNESS, true, false);
324+
ValidateCheckInputsForAllFlags(valid_with_witness_tx, SCRIPT_VERIFY_WITNESS, true);
331325
}
332326

333327
{
@@ -352,7 +346,7 @@ BOOST_FIXTURE_TEST_CASE(checkinputs_test, TestChain100Setup)
352346
}
353347

354348
// This should be valid under all script flags
355-
ValidateCheckInputsForAllFlags(tx, 0, true, false);
349+
ValidateCheckInputsForAllFlags(tx, 0, true);
356350

357351
// Check that if the second input is invalid, but the first input is
358352
// valid, the transaction is not cached.

0 commit comments

Comments
 (0)