Skip to content

Commit 46f0b2f

Browse files
committed
Merge #19851: refactor: Extract ParseOpCode from ParseScript
c923872 refactor: Extract ParseOpCode from ParseScript (João Barbosa) Pull request description: Seems more natural to have `mapOpNames` "hidden" in `ParseOpCode` than in `ParseScript`. A second lookup in `mapOpNames` is also removed. ACKs for top commit: laanwj: ACK c923872 theStack: re-ACK c923872 Tree-SHA512: d59d1964760622cf365479d44e3e676aa0bf46b60e77160140d967e012042df92121d3224c7551dc96eff5ff3294598cc6bade82adb3f60d28810e18e60e1257
2 parents 2878167 + c923872 commit 46f0b2f

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/core_read.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
#include <algorithm>
2121
#include <string>
2222

23-
CScript ParseScript(const std::string& s)
24-
{
25-
CScript result;
23+
namespace {
2624

25+
opcodetype ParseOpCode(const std::string& s)
26+
{
2727
static std::map<std::string, opcodetype> mapOpNames;
2828

2929
if (mapOpNames.empty())
@@ -45,6 +45,17 @@ CScript ParseScript(const std::string& s)
4545
}
4646
}
4747

48+
auto it = mapOpNames.find(s);
49+
if (it == mapOpNames.end()) throw std::runtime_error("script parse error: unknown opcode");
50+
return it->second;
51+
}
52+
53+
} // namespace
54+
55+
CScript ParseScript(const std::string& s)
56+
{
57+
CScript result;
58+
4859
std::vector<std::string> words;
4960
boost::algorithm::split(words, s, boost::algorithm::is_any_of(" \t\n"), boost::algorithm::token_compress_on);
5061

@@ -82,14 +93,10 @@ CScript ParseScript(const std::string& s)
8293
std::vector<unsigned char> value(w->begin()+1, w->end()-1);
8394
result << value;
8495
}
85-
else if (mapOpNames.count(*w))
86-
{
87-
// opcode, e.g. OP_ADD or ADD:
88-
result << mapOpNames[*w];
89-
}
9096
else
9197
{
92-
throw std::runtime_error("script parse error");
98+
// opcode, e.g. OP_ADD or ADD:
99+
result << ParseOpCode(*w);
93100
}
94101
}
95102

test/util/data/bitcoin-util-test.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@
221221
{ "exec": "./bitcoin-tx",
222222
"args": ["-create", "outscript=0:123badscript"],
223223
"return_code": 1,
224-
"error_txt": "error: script parse error",
224+
"error_txt": "error: script parse error: unknown opcode",
225225
"description": "Create a new transaction with an invalid output script"
226226
},
227227
{ "exec": "./bitcoin-tx",

0 commit comments

Comments
 (0)