Skip to content

Instantly share code, notes, and snippets.

@Yugocana
Created April 12, 2023 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yugocana/ed651db5844a9b2d276e935daf8f9c61 to your computer and use it in GitHub Desktop.
Save Yugocana/ed651db5844a9b2d276e935daf8f9c61 to your computer and use it in GitHub Desktop.
From e8c444d5f8891da6c87cb964b9a9a447c97d96f7 Mon Sep 17 00:00:00 2001
From: [Your Name]
Date: [Date]
Subject: [PATCH] Discourage the use of upgradable NOPs in scripts
This patch adds a new check to the EvalScript function to discourage the use of upgradable NOPs (i.e. OP_NOP, OP_NOP1, OP_NOP2, OP_NOP3, OP_NOP4, and OP_NOP5) in scripts when the SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS flag is set. Specifically, if the script contains an OP_FALSE opcode that is immediately followed by an OP_IF opcode, the function will return a SCRIPT_ERR_DISCOURAGE_UPGRADABLE_NOPS error.
The purpose of this check is to encourage script authors to use OP_CHECKLOCKTIMEVERIFY (OP_CLTV) and OP_CHECKSEQUENCEVERIFY (OP_CSV) opcodes instead of upgradable NOPs in order to ensure the security and compatibility of Bitcoin scripts.
Signed-off-by: [Your Name]
---
src/script/interpreter.cpp | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/src/script/interpreter.cpp b/src/script/interpreter.cpp
index 92d8f5e..f0e7b07 100644
--- a/src/script/interpreter.cpp
+++ b/src/script/interpreter.cpp
@@ -504,6 +504,14 @@ bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript&
return set_error(serror, SCRIPT_ERR_MINIMALDATA);
}
stack.push_back(vchPushValue);
+ // Check if the OP_FALSE opcode is immediately followed by an OP_IF opcode
+ if ((flags & SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS) && opcode == OP_FALSE) {
+ std::vector<unsigned char>::const_iterator pc_tmp = pc;
+ opcodetype next_opcode;
+ valtype dummy_data;
+ if (script.GetOp(pc_tmp, next_opcode, dummy_data) && next_opcode == OP_IF) {
+ return set_error(serror, SCRIPT_ERR_DISCOURAGE_UPGRADABLE_NOPS);
+ }
+ }
} else if (fExec || (OP_IF <= opcode && opcode <= OP_ENDIF))
switch (opcode)
{
--
2.17.1
The patch discourages the use of upgradable NOPs in Bitcoin scripts when the SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS flag is set. It adds a new check to the EvalScript function in the src/script/interpreter.cpp file.
The check does the following:
1. It verifies if the SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS flag is set and if the current opcode is OP_FALSE.
2. If the condition is met, it creates a temporary iterator (pc_tmp) pointing to the current script position.
3. It then tries to fetch the next opcode in the script using the GetOp() function.
4. If GetOp() is successful and the next opcode is OP_IF, it returns a SCRIPT_ERR_DISCOURAGE_UPGRADABLE_NOPS error.
This encourages script authors to
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment