diff --git a/src/script/interpreter.cpp b/src/script/interpreter.cpp --- a/src/script/interpreter.cpp +++ b/src/script/interpreter.cpp @@ -1635,9 +1635,10 @@ CScript pubKey2(pubKeySerialized.begin(), pubKeySerialized.end()); popstack(stack); - // Bail out early if ALLOW_SEGWIT_RECOVERY is set, the redeem script is - // a p2sh segwit program and it was the only item pushed into the stack - if ((flags & SCRIPT_ALLOW_SEGWIT_RECOVERY) != 0 && stack.empty() && + // Bail out early if SCRIPT_DISALLOW_SEGWIT_RECOVERY is not set, the + // redeem script is a p2sh segwit program, and it was the only item + // pushed onto the stack. + if ((flags & SCRIPT_DISALLOW_SEGWIT_RECOVERY) == 0 && stack.empty() && pubKey2.IsWitnessProgram()) { return set_success(serror); } diff --git a/src/script/script_flags.h b/src/script/script_flags.h --- a/src/script/script_flags.h +++ b/src/script/script_flags.h @@ -12,6 +12,7 @@ SCRIPT_VERIFY_NONE = 0, // Evaluate P2SH subscripts (softfork safe, BIP16). + // Note: The Segwit Recovery feature is an exception to P2SH SCRIPT_VERIFY_P2SH = (1U << 0), // Passing a non-strict-DER signature or one with undefined hashtype to a @@ -63,6 +64,7 @@ // be true". // (softfork safe, BIP62 rule 6) // Note: CLEANSTACK should never be used without P2SH or WITNESS. + // Note: The Segwit Recovery feature is an exception to CLEANSTACK SCRIPT_VERIFY_CLEANSTACK = (1U << 8), // Verify CHECKLOCKTIMEVERIFY @@ -106,8 +108,9 @@ // SCRIPT_ENABLE_SCHNORR = (1U << 19), - // Allows the recovery of coins sent to p2sh segwit addresses - SCRIPT_ALLOW_SEGWIT_RECOVERY = (1U << 20), + // The exception to CLEANSTACK and P2SH for the recovery of coins sent + // to p2sh segwit addresses is not allowed. + SCRIPT_DISALLOW_SEGWIT_RECOVERY = (1U << 20), }; #endif // BITCOIN_SCRIPT_SCRIPTFLAGS_H diff --git a/src/script/sigcache.cpp b/src/script/sigcache.cpp --- a/src/script/sigcache.cpp +++ b/src/script/sigcache.cpp @@ -29,7 +29,7 @@ SCRIPT_VERIFY_CHECKSEQUENCEVERIFY | SCRIPT_VERIFY_MINIMALIF | SCRIPT_VERIFY_NULLFAIL | SCRIPT_VERIFY_COMPRESSED_PUBKEYTYPE | SCRIPT_ENABLE_SIGHASH_FORKID | SCRIPT_ENABLE_REPLAY_PROTECTION | - SCRIPT_VERIFY_CHECKDATASIG_SIGOPS | SCRIPT_ALLOW_SEGWIT_RECOVERY; + SCRIPT_VERIFY_CHECKDATASIG_SIGOPS | SCRIPT_DISALLOW_SEGWIT_RECOVERY; /** * Valid signature cache, to avoid doing expensive ECDSA signature checking diff --git a/src/test/data/script_tests.json b/src/test/data/script_tests.json --- a/src/test/data/script_tests.json +++ b/src/test/data/script_tests.json @@ -3262,149 +3262,163 @@ [ "0x16 0x001491b24bf9f5288532960ac687abb035127b1d28a5", "HASH160 0x14 0x17743beb429c55c942d2ec703b98c4d57c2df5c6 EQUAL", - "CLEANSTACK,P2SH", + "CLEANSTACK,DISALLOW_SEGWIT_RECOVERY,P2SH", "CLEANSTACK", - "v0 P2SH-P2WPKH but no SCRIPT_ALLOW_SEGWIT_RECOVERY" + "v0 P2SH-P2WPKH with SCRIPT_DISALLOW_SEGWIT_RECOVERY" ], [ "0x16 0x001491b24bf9f5288532960ac687abb035127b1d28a5", "HASH160 0x14 0x17743beb429c55c942d2ec703b98c4d57c2df5c6 EQUAL", - "ALLOW_SEGWIT_RECOVERY,CLEANSTACK,P2SH", + "CLEANSTACK,P2SH", "OK", - "v0 P2SH-P2WPKH with SCRIPT_ALLOW_SEGWIT_RECOVERY" + "Valid Segwit recovery with v0 P2SH-P2WPKH" ], [ "0 0x16 0x001491b24bf9f5288532960ac687abb035127b1d28a5", "HASH160 0x14 0x17743beb429c55c942d2ec703b98c4d57c2df5c6 EQUAL", - "ALLOW_SEGWIT_RECOVERY,CLEANSTACK,P2SH", + "CLEANSTACK,P2SH", "CLEANSTACK", - "v0 P2SH-P2WPKH with extra stack item and SCRIPT_ALLOW_SEGWIT_RECOVERY" + "v0 P2SH-P2WPKH with extra stack item" ], [ "0x22 0x00205a0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", "HASH160 0x14 0x17a6be2f8fe8e94f033e53d17beefda0f3ac4409 EQUAL", - "CLEANSTACK,P2SH", + "CLEANSTACK,DISALLOW_SEGWIT_RECOVERY,P2SH", "CLEANSTACK", - "v0 P2SH-P2WSH but no SCRIPT_ALLOW_SEGWIT_RECOVERY" + "v0 P2SH-P2WSH with SCRIPT_DISALLOW_SEGWIT_RECOVERY" ], [ "0x22 0x00205a0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", "HASH160 0x14 0x17a6be2f8fe8e94f033e53d17beefda0f3ac4409 EQUAL", - "ALLOW_SEGWIT_RECOVERY,CLEANSTACK,P2SH", + "CLEANSTACK,P2SH", "OK", - "v0 P2SH-P2WSH with SCRIPT_ALLOW_SEGWIT_RECOVERY" + "Valid Segwit recovery with v0 P2SH-P2WSH" ], [ "0 0x22 0x00205a0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", "HASH160 0x14 0x17a6be2f8fe8e94f033e53d17beefda0f3ac4409 EQUAL", - "ALLOW_SEGWIT_RECOVERY,CLEANSTACK,P2SH", + "CLEANSTACK,P2SH", "CLEANSTACK", - "v0 P2SH-P2WSH with extra stack item and SCRIPT_ALLOW_SEGWIT_RECOVERY" + "v0 P2SH-P2WSH with extra stack item" ], [ "0x03 0x00015a", "HASH160 0x14 0x40b6941895022d458de8f4bbfe27f3aaa4fb9a74 EQUAL", - "ALLOW_SEGWIT_RECOVERY,CLEANSTACK,P2SH", + "CLEANSTACK,P2SH", "CLEANSTACK", - "Invalid witness program (too short) with SCRIPT_ALLOW_SEGWIT_RECOVERY" + "Invalid witness program (too short)" ], [ "0x04 0x00025a01", "HASH160 0x14 0x86123d8e050333a605e434ecf73128d83815b36f EQUAL", - "ALLOW_SEGWIT_RECOVERY,CLEANSTACK,P2SH", + "CLEANSTACK,P2SH", "OK", - "Valid witness program (min allowed length) with SCRIPT_ALLOW_SEGWIT_RECOVERY" + "Segwit recovery with valid witness program (min allowed length)" ], [ "0x2a 0x00285a0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f2021222324252627", "HASH160 0x14 0xdf7b93f88e83471b479fb219ae90e5b633d6b750 EQUAL", - "ALLOW_SEGWIT_RECOVERY,CLEANSTACK,P2SH", + "CLEANSTACK,P2SH", "OK", - "Valid witness program (max allowed length) with SCRIPT_ALLOW_SEGWIT_RECOVERY" + "Segwit recovery with valid witness program (max allowed length)" ], [ "0x2b 0x00295a0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728", "HASH160 0x14 0x13aa4fcfd630508e0794dca320cac172c5790aea EQUAL", - "ALLOW_SEGWIT_RECOVERY,CLEANSTACK,P2SH", + "CLEANSTACK,P2SH", "CLEANSTACK", - "Invalid witness program (too long) with SCRIPT_ALLOW_SEGWIT_RECOVERY" + "Invalid witness program (too long)" ], [ "0x22 0x60205a0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", "HASH160 0x14 0x9b0c7017004d3818b7c833ddb3cb5547a22034d0 EQUAL", - "ALLOW_SEGWIT_RECOVERY,CLEANSTACK,P2SH", + "CLEANSTACK,P2SH", "OK", - "Valid witness program (max allowed version) with SCRIPT_ALLOW_SEGWIT_RECOVERY" + "Segwit recovery with valid witness program (max allowed version)" ], [ "0x22 0x4f205a0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", "HASH160 0x14 0x97aa1e96e49ca6d744d7344f649dd9f94bcc35eb EQUAL", - "ALLOW_SEGWIT_RECOVERY,CLEANSTACK,P2SH", + "CLEANSTACK,P2SH", "CLEANSTACK", - "Invalid witness program (invalid version -1) with SCRIPT_ALLOW_SEGWIT_RECOVERY" + "Invalid witness program (invalid version -1)" ], [ "0x23 0x0111205a0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", "HASH160 0x14 0x4b5321beb1c09f593ff3c02be4af21c7f949e101 EQUAL", - "ALLOW_SEGWIT_RECOVERY,CLEANSTACK,P2SH", + "CLEANSTACK,P2SH", "CLEANSTACK", - "Invalid witness program (invalid version 17) with SCRIPT_ALLOW_SEGWIT_RECOVERY" + "Invalid witness program (invalid version 17)" ], [ "0x23 0x00205a0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f51", "HASH160 0x14 0x8eb812176c9e71732584123dd06d3246e659b199 EQUAL", - "ALLOW_SEGWIT_RECOVERY,CLEANSTACK,P2SH", + "CLEANSTACK,P2SH", "CLEANSTACK", - "Invalid witness program (more than 2 stack items) with SCRIPT_ALLOW_SEGWIT_RECOVERY" + "Invalid witness program (more than 2 stack items)" ], [ "0x04 0x00020000", "HASH160 0x14 0x0e01bcfe7c6f3fd2fd8f81092299369744684733 EQUAL", - "ALLOW_SEGWIT_RECOVERY,CLEANSTACK,P2SH", + "CLEANSTACK,P2SH", "OK", "Valid segwit recovery, in spite of false value being left on stack (0)" ], [ "0x04 0x00020080", "HASH160 0x14 0x10ddc638cb26615f867dad80efacced9e73766bc EQUAL", - "ALLOW_SEGWIT_RECOVERY,CLEANSTACK,P2SH", + "CLEANSTACK,P2SH", "OK", "Valid segwit recovery, in spite of false value being left on stack (minus 0)" ], [ + "0x04 0x00020000", + "HASH160 0x14 0x0e01bcfe7c6f3fd2fd8f81092299369744684733 EQUAL", + "CLEANSTACK,DISALLOW_SEGWIT_RECOVERY,P2SH", + "EVAL_FALSE", + "Otherwise valid segwit recovery, in spite of false value being left on stack (0), but with SCRIPT_DISALLOW_SEGWIT_RECOVERY" +], +[ + "0x04 0x00020080", + "HASH160 0x14 0x10ddc638cb26615f867dad80efacced9e73766bc EQUAL", + "CLEANSTACK,DISALLOW_SEGWIT_RECOVERY,P2SH", + "EVAL_FALSE", + "Otherwise valid segwit recovery, in spite of false value being left on stack (minus 0), but with SCRIPT_DISALLOW_SEGWIT_RECOVERY" +], +[ "0x22 0x50205a0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", "HASH160 0x14 0xbe02794ceede051da41b420e88a86fff2802af06 EQUAL", - "ALLOW_SEGWIT_RECOVERY,CLEANSTACK,P2SH", + "CLEANSTACK,P2SH", "BAD_OPCODE", - "Invalid witness program (OP_RESERVED in version field) with SCRIPT_ALLOW_SEGWIT_RECOVERY" + "Invalid witness program (OP_RESERVED in version field)" ], [ "0x17 0x01001491b24bf9f5288532960ac687abb035127b1d28a5", "HASH160 0x14 0x0718743e67c1ef4911e0421f206c5ff81755718e EQUAL", - "ALLOW_SEGWIT_RECOVERY,CLEANSTACK,P2SH", + "CLEANSTACK,P2SH", "CLEANSTACK", - "Invalid witness program (non-minimal push in version field) with SCRIPT_ALLOW_SEGWIT_RECOVERY" + "Invalid witness program (non-minimal push in version field)" ], [ "0x05 0x004c0245aa", "HASH160 0x14 0xd3ec673296c7fd7e1a9e53bfc36f414de303e905 EQUAL", - "ALLOW_SEGWIT_RECOVERY,CLEANSTACK,P2SH", + "CLEANSTACK,P2SH", "CLEANSTACK", - "Invalid witness program (non-minimal push in program field) with SCRIPT_ALLOW_SEGWIT_RECOVERY" + "Invalid witness program (non-minimal push in program field)" ], [ "0x16 0x001491b24bf9f5288532960ac687abb035127b1d28a5", "HASH160 0x14 0x17a6be2f8fe8e94f033e53d17beefda0f3ac4409 EQUAL", - "ALLOW_SEGWIT_RECOVERY,CLEANSTACK,P2SH", + "CLEANSTACK,P2SH", "EVAL_FALSE", - "v0 P2SH-P2WPKH whose redeem script hash does not match P2SH output and SCRIPT_ALLOW_SEGWIT_RECOVERY" + "v0 P2SH-P2WPKH whose redeem script hash does not match P2SH output" ], [ "0x16 0x001491b24bf9f5288532960ac687abb035127b1d28a5", "1", - "ALLOW_SEGWIT_RECOVERY,CLEANSTACK,P2SH", + "CLEANSTACK,P2SH", "CLEANSTACK", - "v0 P2SH-P2WPKH spending a non-P2SH output and SCRIPT_ALLOW_SEGWIT_RECOVERY" + "v0 P2SH-P2WPKH spending a non-P2SH output" ], ["CHECKSEQUENCEVERIFY tests"], diff --git a/src/test/script_tests.cpp b/src/test/script_tests.cpp --- a/src/test/script_tests.cpp +++ b/src/test/script_tests.cpp @@ -2080,25 +2080,25 @@ .EditPush(64, "01", "41") .ScriptError(SCRIPT_ERR_EVAL_FALSE)); - // Tests SCRIPT_ALLOW_SEGWIT_RECOVERY - const uint32_t allowSegwitRecoveryFlags = SCRIPT_VERIFY_CLEANSTACK | - SCRIPT_VERIFY_P2SH | - SCRIPT_ALLOW_SEGWIT_RECOVERY; + // Tests Segwit Recovery transactions and SCRIPT_DISALLOW_SEGWIT_RECOVERY + const uint32_t allowSegwitRecoveryFlags = + SCRIPT_VERIFY_CLEANSTACK | SCRIPT_VERIFY_P2SH; tests.push_back( TestBuilder(CScript() << OP_0 << ToByteVector(keys.pubkey0.GetID()), - "v0 P2SH-P2WPKH but no SCRIPT_ALLOW_SEGWIT_RECOVERY", - SCRIPT_VERIFY_CLEANSTACK | SCRIPT_VERIFY_P2SH, true) + "v0 P2SH-P2WPKH with SCRIPT_DISALLOW_SEGWIT_RECOVERY", + SCRIPT_VERIFY_CLEANSTACK | SCRIPT_VERIFY_P2SH | + SCRIPT_DISALLOW_SEGWIT_RECOVERY, + true) .PushRedeem() .ScriptError(SCRIPT_ERR_CLEANSTACK)); tests.push_back( TestBuilder(CScript() << OP_0 << ToByteVector(keys.pubkey0.GetID()), - "v0 P2SH-P2WPKH with SCRIPT_ALLOW_SEGWIT_RECOVERY", + "Valid Segwit recovery with v0 P2SH-P2WPKH", allowSegwitRecoveryFlags, true) .PushRedeem()); tests.push_back( TestBuilder(CScript() << OP_0 << ToByteVector(keys.pubkey0.GetID()), - "v0 P2SH-P2WPKH with extra stack item and " - "SCRIPT_ALLOW_SEGWIT_RECOVERY", + "v0 P2SH-P2WPKH with extra stack item", allowSegwitRecoveryFlags, true) .Num(0) .PushRedeem() @@ -2108,82 +2108,76 @@ 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31})); tests.push_back( TestBuilder(CScript() << OP_0 << ToByteVector(dummy256), - "v0 P2SH-P2WSH but no SCRIPT_ALLOW_SEGWIT_RECOVERY", - SCRIPT_VERIFY_CLEANSTACK | SCRIPT_VERIFY_P2SH, true) + "v0 P2SH-P2WSH with SCRIPT_DISALLOW_SEGWIT_RECOVERY", + SCRIPT_VERIFY_CLEANSTACK | SCRIPT_VERIFY_P2SH | + SCRIPT_DISALLOW_SEGWIT_RECOVERY, + true) .PushRedeem() .ScriptError(SCRIPT_ERR_CLEANSTACK)); - tests.push_back( - TestBuilder(CScript() << OP_0 << ToByteVector(dummy256), - "v0 P2SH-P2WSH with SCRIPT_ALLOW_SEGWIT_RECOVERY", - allowSegwitRecoveryFlags, true) - .PushRedeem()); tests.push_back(TestBuilder(CScript() << OP_0 << ToByteVector(dummy256), - "v0 P2SH-P2WSH with extra stack item and " - "SCRIPT_ALLOW_SEGWIT_RECOVERY", + "Valid Segwit recovery with v0 P2SH-P2WSH", + allowSegwitRecoveryFlags, true) + .PushRedeem()); + tests.push_back(TestBuilder(CScript() << OP_0 << ToByteVector(dummy256), + "v0 P2SH-P2WSH with extra stack item", allowSegwitRecoveryFlags, true) .Num(0) .PushRedeem() .ScriptError(SCRIPT_ERR_CLEANSTACK)); - // Tests the limits of IsWitnessProgram along with - // SCRIPT_ALLOW_SEGWIT_RECOVERY + // Tests the limits of IsWitnessProgram without + // SCRIPT_DISALLOW_SEGWIT_RECOVERY. std::vector shortprogram({90, 1}); tests.push_back( TestBuilder(CScript() << OP_0 << std::vector(shortprogram.begin(), shortprogram.end() - 1), - "Invalid witness program (too short) with " - "SCRIPT_ALLOW_SEGWIT_RECOVERY", + "Invalid witness program (too short)", allowSegwitRecoveryFlags, true) .PushRedeem() .ScriptError(SCRIPT_ERR_CLEANSTACK)); tests.push_back( - TestBuilder(CScript() << OP_0 << shortprogram, - "Valid witness program (min allowed length) with " - "SCRIPT_ALLOW_SEGWIT_RECOVERY", - allowSegwitRecoveryFlags, true) + TestBuilder( + CScript() << OP_0 << shortprogram, + "Segwit recovery with valid witness program (min allowed length)", + allowSegwitRecoveryFlags, true) .PushRedeem()); std::vector longprogram( {90, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40}); tests.push_back( - TestBuilder(CScript() << OP_0 - << std::vector(longprogram.begin(), - longprogram.end() - 1), - "Valid witness program (max allowed length) with " - "SCRIPT_ALLOW_SEGWIT_RECOVERY", - allowSegwitRecoveryFlags, true) + TestBuilder( + CScript() << OP_0 + << std::vector(longprogram.begin(), + longprogram.end() - 1), + "Segwit recovery with valid witness program (max allowed length)", + allowSegwitRecoveryFlags, true) .PushRedeem()); tests.push_back(TestBuilder(CScript() << OP_0 << longprogram, - "Invalid witness program (too long) with " - "SCRIPT_ALLOW_SEGWIT_RECOVERY", + "Invalid witness program (too long)", allowSegwitRecoveryFlags, true) .PushRedeem() .ScriptError(SCRIPT_ERR_CLEANSTACK)); tests.push_back( - TestBuilder(CScript() << OP_16 << ToByteVector(dummy256), - "Valid witness program (max allowed version) with " - "SCRIPT_ALLOW_SEGWIT_RECOVERY", - allowSegwitRecoveryFlags, true) + TestBuilder( + CScript() << OP_16 << ToByteVector(dummy256), + "Segwit recovery with valid witness program (max allowed version)", + allowSegwitRecoveryFlags, true) .PushRedeem()); tests.push_back( TestBuilder(CScript() << OP_1NEGATE << ToByteVector(dummy256), - "Invalid witness program (invalid version -1) with " - "SCRIPT_ALLOW_SEGWIT_RECOVERY", - allowSegwitRecoveryFlags, true) - .PushRedeem() - .ScriptError(SCRIPT_ERR_CLEANSTACK)); - tests.push_back( - TestBuilder(CScript() << 17 << ToByteVector(dummy256), - "Invalid witness program (invalid version 17) with " - "SCRIPT_ALLOW_SEGWIT_RECOVERY", + "Invalid witness program (invalid version -1)", allowSegwitRecoveryFlags, true) .PushRedeem() .ScriptError(SCRIPT_ERR_CLEANSTACK)); + tests.push_back(TestBuilder(CScript() << 17 << ToByteVector(dummy256), + "Invalid witness program (invalid version 17)", + allowSegwitRecoveryFlags, true) + .PushRedeem() + .ScriptError(SCRIPT_ERR_CLEANSTACK)); tests.push_back( TestBuilder(CScript() << OP_0 << ToByteVector(dummy256) << OP_1, - "Invalid witness program (more than 2 stack " - "items) with SCRIPT_ALLOW_SEGWIT_RECOVERY", + "Invalid witness program (more than 2 stack items)", allowSegwitRecoveryFlags, true) .PushRedeem() .ScriptError(SCRIPT_ERR_CLEANSTACK)); @@ -2200,11 +2194,27 @@ allowSegwitRecoveryFlags, true) .PushRedeem()); tests.push_back( + TestBuilder(CScript() << OP_0 << std::vector({0, 0}), + "Otherwise valid segwit recovery, in spite of false value " + "being left " + "on stack (0), but with SCRIPT_DISALLOW_SEGWIT_RECOVERY", + allowSegwitRecoveryFlags | SCRIPT_DISALLOW_SEGWIT_RECOVERY, + true) + .PushRedeem() + .ScriptError(SCRIPT_ERR_EVAL_FALSE)); + tests.push_back( TestBuilder( - CScript() << OP_RESERVED << ToByteVector(dummy256), - "Invalid witness program (OP_RESERVED in version field) with " - "SCRIPT_ALLOW_SEGWIT_RECOVERY", - allowSegwitRecoveryFlags, true) + CScript() << OP_0 << std::vector({0, 0x80}), + "Otherwise valid segwit recovery, in spite of false value being " + "left " + "on stack (minus 0), but with SCRIPT_DISALLOW_SEGWIT_RECOVERY", + allowSegwitRecoveryFlags | SCRIPT_DISALLOW_SEGWIT_RECOVERY, true) + .PushRedeem() + .ScriptError(SCRIPT_ERR_EVAL_FALSE)); + tests.push_back( + TestBuilder(CScript() << OP_RESERVED << ToByteVector(dummy256), + "Invalid witness program (OP_RESERVED in version field)", + allowSegwitRecoveryFlags, true) .PushRedeem() .ScriptError(SCRIPT_ERR_BAD_OPCODE)); const uint8_t nonmin_push_00[] = {1, 0}; @@ -2212,32 +2222,30 @@ TestBuilder( CScript(&nonmin_push_00[0], &nonmin_push_00[sizeof(nonmin_push_00)]) << ToByteVector(keys.pubkey0.GetID()), - "Invalid witness program (non-minimal push in version field) with " - "SCRIPT_ALLOW_SEGWIT_RECOVERY", + "Invalid witness program (non-minimal push in version field)", allowSegwitRecoveryFlags, true) .PushRedeem() .ScriptError(SCRIPT_ERR_CLEANSTACK)); const uint8_t nonmin_push_45aa[] = {OP_PUSHDATA1, 2, 0x45, 0xaa}; tests.push_back( - TestBuilder((CScript() << OP_0) + - CScript(&nonmin_push_45aa[0], - &nonmin_push_45aa[sizeof(nonmin_push_45aa)]), - "Invalid witness program (non-minimal push in program " - "field) with SCRIPT_ALLOW_SEGWIT_RECOVERY", - allowSegwitRecoveryFlags, true) + TestBuilder( + (CScript() << OP_0) + + CScript(&nonmin_push_45aa[0], + &nonmin_push_45aa[sizeof(nonmin_push_45aa)]), + "Invalid witness program (non-minimal push in program field)", + allowSegwitRecoveryFlags, true) .PushRedeem() .ScriptError(SCRIPT_ERR_CLEANSTACK)); tests.push_back( TestBuilder(CScript() << OP_0 << ToByteVector(dummy256), "v0 P2SH-P2WPKH whose redeem script hash does not match " - "P2SH output and SCRIPT_ALLOW_SEGWIT_RECOVERY", + "P2SH output", allowSegwitRecoveryFlags, true) .Push(CScript() << OP_0 << ToByteVector(keys.pubkey0.GetID())) .ScriptError(SCRIPT_ERR_EVAL_FALSE)); tests.push_back( TestBuilder(CScript() << OP_1, - "v0 P2SH-P2WPKH spending a non-P2SH output and " - "SCRIPT_ALLOW_SEGWIT_RECOVERY", + "v0 P2SH-P2WPKH spending a non-P2SH output", allowSegwitRecoveryFlags) .Push(CScript() << OP_0 << ToByteVector(keys.pubkey0.GetID())) .ScriptError(SCRIPT_ERR_CLEANSTACK)); diff --git a/src/test/scriptflags.cpp b/src/test/scriptflags.cpp --- a/src/test/scriptflags.cpp +++ b/src/test/scriptflags.cpp @@ -33,7 +33,7 @@ {"REPLAY_PROTECTION", SCRIPT_ENABLE_REPLAY_PROTECTION}, {"CHECKDATASIG", SCRIPT_VERIFY_CHECKDATASIG_SIGOPS}, {"SCHNORR", SCRIPT_ENABLE_SCHNORR}, - {"ALLOW_SEGWIT_RECOVERY", SCRIPT_ALLOW_SEGWIT_RECOVERY}, + {"DISALLOW_SEGWIT_RECOVERY", SCRIPT_DISALLOW_SEGWIT_RECOVERY}, }; uint32_t ParseScriptFlags(std::string strFlags) { diff --git a/src/test/sigcache_tests.cpp b/src/test/sigcache_tests.cpp --- a/src/test/sigcache_tests.cpp +++ b/src/test/sigcache_tests.cpp @@ -38,7 +38,7 @@ SCRIPT_VERIFY_CHECKSEQUENCEVERIFY | SCRIPT_VERIFY_MINIMALIF | SCRIPT_VERIFY_NULLFAIL | SCRIPT_VERIFY_COMPRESSED_PUBKEYTYPE | SCRIPT_ENABLE_SIGHASH_FORKID | SCRIPT_ENABLE_REPLAY_PROTECTION | - SCRIPT_VERIFY_CHECKDATASIG_SIGOPS | SCRIPT_ALLOW_SEGWIT_RECOVERY; + SCRIPT_VERIFY_CHECKDATASIG_SIGOPS | SCRIPT_DISALLOW_SEGWIT_RECOVERY; /* We will be testing that these flags DO affect the cache entry. The expected * behaviour is that flags which are not explicitly listed as invariant in * script/sigcache.cpp will affect the cache entry. Here we will thus enforce diff --git a/src/validation.cpp b/src/validation.cpp --- a/src/validation.cpp +++ b/src/validation.cpp @@ -719,10 +719,12 @@ } if (IsGreatWallEnabledForCurrentBlock(config)) { - if (!fRequireStandard) { - extraFlags |= SCRIPT_ALLOW_SEGWIT_RECOVERY; + if (fRequireStandard) { + extraFlags |= SCRIPT_DISALLOW_SEGWIT_RECOVERY; } extraFlags |= SCRIPT_ENABLE_SCHNORR; + } else { + extraFlags |= SCRIPT_DISALLOW_SEGWIT_RECOVERY; } // Make sure whatever we need to activate is actually activated. @@ -1238,14 +1240,14 @@ // This differs from MANDATORY_SCRIPT_VERIFY_FLAGS as it contains // additional upgrade flags (see AcceptToMemoryPoolWorker variable // extraFlags). - // Even though it is not a mandatory flag, - // SCRIPT_ALLOW_SEGWIT_RECOVERY is strictly more permissive than the - // set of standard flags. It therefore needs to be added in order to - // check if we need to penalize the peer that sent us the - // transaction or not. - uint32_t mandatoryFlags = - (flags & ~STANDARD_NOT_MANDATORY_VERIFY_FLAGS) | - SCRIPT_ALLOW_SEGWIT_RECOVERY; + // Although it is not (currently) a standard flag, + // SCRIPT_ALLOW_SEGWIT_RECOVERY is treated similarly to the + // set of standard-not-mandatory flags. It needs to be removed in + // order to check if we need to penalize, but not ban, the peer that + // sent us the transaction. + uint32_t mandatoryFlags = flags & + ~STANDARD_NOT_MANDATORY_VERIFY_FLAGS & + ~SCRIPT_DISALLOW_SEGWIT_RECOVERY; if (flags != mandatoryFlags) { // Check whether the failure was caused by a non-mandatory // script verification check. If so, don't trigger DoS @@ -1626,8 +1628,9 @@ // and their verify variants. We also stop accepting 65 byte signatures in // CHECKMULTISIG and its verify variant. if (IsGreatWallEnabled(config, pChainTip)) { - flags |= SCRIPT_ALLOW_SEGWIT_RECOVERY; flags |= SCRIPT_ENABLE_SCHNORR; + } else { + flags |= SCRIPT_DISALLOW_SEGWIT_RECOVERY; } // We make sure this node will have replay protection during the next hard diff --git a/test/functional/abc-segwit-recovery-activation.py b/test/functional/abc-segwit-recovery-activation.py --- a/test/functional/abc-segwit-recovery-activation.py +++ b/test/functional/abc-segwit-recovery-activation.py @@ -4,7 +4,7 @@ # Distributed under the MIT software license, see the accompanying # file COPYING or http://www.opensource.org/licenses/mit-license.php. """ -This test checks activation of the SCRIPT_ALLOW_SEGWIT_RECOVERY flag +This test checks activation of the Segwit Recovery feature. """ from test_framework.blocktools import (