Skip to content

Commit 3ba5ebc

Browse files
committed
Merge pull request #5000
0391423 Discourage NOPs reserved for soft-fork upgrades (Peter Todd)
2 parents cf9c488 + 0391423 commit 3ba5ebc

File tree

8 files changed

+49
-3
lines changed

8 files changed

+49
-3
lines changed

src/script/interpreter.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,14 @@ bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, un
329329
// Control
330330
//
331331
case OP_NOP:
332+
break;
333+
332334
case OP_NOP1: case OP_NOP2: case OP_NOP3: case OP_NOP4: case OP_NOP5:
333335
case OP_NOP6: case OP_NOP7: case OP_NOP8: case OP_NOP9: case OP_NOP10:
336+
{
337+
if (flags & SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS)
338+
return set_error(serror, SCRIPT_ERR_DISCOURAGE_UPGRADABLE_NOPS);
339+
}
334340
break;
335341

336342
case OP_IF:

src/script/interpreter.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,18 @@ enum
5757
// any other push causes the script to fail (BIP62 rule 3).
5858
// In addition, whenever a stack element is interpreted as a number, it must be of minimal length (BIP62 rule 4).
5959
// (softfork safe)
60-
SCRIPT_VERIFY_MINIMALDATA = (1U << 6)
60+
SCRIPT_VERIFY_MINIMALDATA = (1U << 6),
61+
62+
// Discourage use of NOPs reserved for upgrades (NOP1-10)
63+
//
64+
// Provided so that nodes can avoid accepting or mining transactions
65+
// containing executed NOP's whose meaning may change after a soft-fork,
66+
// thus rendering the script invalid; with this flag set executing
67+
// discouraged NOPs fails the script. This verification flag will never be
68+
// a mandatory flag applied to scripts in a block. NOPs that are not
69+
// executed, e.g. within an unexecuted IF ENDIF block, are *not* rejected.
70+
SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS = (1U << 7)
71+
6172
};
6273

6374
uint256 SignatureHash(const CScript &scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType);

src/script/script_error.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ const char* ScriptErrorString(const ScriptError serror)
5959
return "Non-canonical signature: S value is unnecessarily high";
6060
case SCRIPT_ERR_SIG_NULLDUMMY:
6161
return "Dummy CHECKMULTISIG argument must be zero";
62+
case SCRIPT_ERR_DISCOURAGE_UPGRADABLE_NOPS:
63+
return "NOPx reserved for soft-fork upgrades";
6264
case SCRIPT_ERR_UNKNOWN_ERROR:
6365
case SCRIPT_ERR_ERROR_COUNT:
6466
default: break;

src/script/script_error.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ typedef enum ScriptError_t
4343
SCRIPT_ERR_SIG_HIGH_S,
4444
SCRIPT_ERR_SIG_NULLDUMMY,
4545

46+
/* softfork safeness */
47+
SCRIPT_ERR_DISCOURAGE_UPGRADABLE_NOPS,
48+
4649
SCRIPT_ERR_ERROR_COUNT
4750
} ScriptError;
4851

src/script/standard.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ static const unsigned int MANDATORY_SCRIPT_VERIFY_FLAGS = SCRIPT_VERIFY_P2SH;
4747
static const unsigned int STANDARD_SCRIPT_VERIFY_FLAGS = MANDATORY_SCRIPT_VERIFY_FLAGS |
4848
SCRIPT_VERIFY_STRICTENC |
4949
SCRIPT_VERIFY_MINIMALDATA |
50-
SCRIPT_VERIFY_NULLDUMMY;
50+
SCRIPT_VERIFY_NULLDUMMY |
51+
SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS;
5152

5253
/** For convenience, standard but not mandatory verify flags. */
5354
static const unsigned int STANDARD_NOT_MANDATORY_VERIFY_FLAGS = STANDARD_SCRIPT_VERIFY_FLAGS & ~MANDATORY_SCRIPT_VERIFY_FLAGS;

src/test/data/script_invalid.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,23 @@ nSequences are max.
163163
["1","NOP1 NOP2 NOP3 NOP4 NOP5 NOP6 NOP7 NOP8 NOP9 NOP10 2 EQUAL", "P2SH,STRICTENC"],
164164
["'NOP_1_to_10' NOP1 NOP2 NOP3 NOP4 NOP5 NOP6 NOP7 NOP8 NOP9 NOP10","'NOP_1_to_11' EQUAL", "P2SH,STRICTENC"],
165165

166+
["Ensure 100% coverage of discouraged NOPS"],
167+
["1", "NOP1", "P2SH,DISCOURAGE_UPGRADABLE_NOPS"],
168+
["1", "NOP2", "P2SH,DISCOURAGE_UPGRADABLE_NOPS"],
169+
["1", "NOP3", "P2SH,DISCOURAGE_UPGRADABLE_NOPS"],
170+
["1", "NOP4", "P2SH,DISCOURAGE_UPGRADABLE_NOPS"],
171+
["1", "NOP5", "P2SH,DISCOURAGE_UPGRADABLE_NOPS"],
172+
["1", "NOP6", "P2SH,DISCOURAGE_UPGRADABLE_NOPS"],
173+
["1", "NOP7", "P2SH,DISCOURAGE_UPGRADABLE_NOPS"],
174+
["1", "NOP8", "P2SH,DISCOURAGE_UPGRADABLE_NOPS"],
175+
["1", "NOP9", "P2SH,DISCOURAGE_UPGRADABLE_NOPS"],
176+
["1", "NOP10", "P2SH,DISCOURAGE_UPGRADABLE_NOPS"],
177+
178+
["NOP10", "1", "P2SH,DISCOURAGE_UPGRADABLE_NOPS", "Discouraged NOP10 in scriptSig"],
179+
180+
["1 0x01 0xb9", "HASH160 0x14 0x15727299b05b45fdaf9ac9ecf7565cfe27c3e567 EQUAL",
181+
"P2SH,DISCOURAGE_UPGRADABLE_NOPS", "Discouraged NOP10 in redeemScript"],
182+
166183
["0x50","1", "P2SH,STRICTENC", "opcode 0x50 is reserved"],
167184
["1", "IF 0xba ELSE 1 ENDIF", "P2SH,STRICTENC", "opcodes above NOP10 invalid if executed"],
168185
["1", "IF 0xbb ELSE 1 ENDIF", "P2SH,STRICTENC"],

src/test/data/script_valid.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@ nSequences are max.
235235
["1","NOP1 NOP2 NOP3 NOP4 NOP5 NOP6 NOP7 NOP8 NOP9 NOP10 1 EQUAL", "P2SH,STRICTENC"],
236236
["'NOP_1_to_10' NOP1 NOP2 NOP3 NOP4 NOP5 NOP6 NOP7 NOP8 NOP9 NOP10","'NOP_1_to_10' EQUAL", "P2SH,STRICTENC"],
237237

238+
["1", "NOP", "P2SH,STRICTENC,DISCOURAGE_UPGRADABLE_NOPS", "Discourage NOPx flag allows OP_NOP"],
239+
240+
["0", "IF NOP10 ENDIF 1", "P2SH,STRICTENC,DISCOURAGE_UPGRADABLE_NOPS",
241+
"Discouraged NOPs are allowed if not executed"],
242+
238243
["0", "IF 0xba ELSE 1 ENDIF", "P2SH,STRICTENC", "opcodes above NOP10 invalid if executed"],
239244
["0", "IF 0xbb ELSE 1 ENDIF", "P2SH,STRICTENC"],
240245
["0", "IF 0xbc ELSE 1 ENDIF", "P2SH,STRICTENC"],

src/test/transaction_tests.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ static std::map<string, unsigned int> mapFlagNames = boost::assign::map_list_of
3737
(string("LOW_S"), (unsigned int)SCRIPT_VERIFY_LOW_S)
3838
(string("SIGPUSHONLY"), (unsigned int)SCRIPT_VERIFY_SIGPUSHONLY)
3939
(string("MINIMALDATA"), (unsigned int)SCRIPT_VERIFY_MINIMALDATA)
40-
(string("NULLDUMMY"), (unsigned int)SCRIPT_VERIFY_NULLDUMMY);
40+
(string("NULLDUMMY"), (unsigned int)SCRIPT_VERIFY_NULLDUMMY)
41+
(string("DISCOURAGE_UPGRADABLE_NOPS"), (unsigned int)SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS);
4142

4243
unsigned int ParseScriptFlags(string strFlags)
4344
{

0 commit comments

Comments
 (0)