Skip to content

Commit c923872

Browse files
committed
refactor: Extract ParseOpCode from ParseScript
A second lookup in mapOpNames is also removed.
1 parent 89a8299 commit c923872

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
@@ -21,10 +21,10 @@
2121
#include <algorithm>
2222
#include <string>
2323

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

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

3030
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)