Skip to content

Commit 5e20e69

Browse files
laanwjPastaPastaPasta
authored andcommitted
Merge bitcoin#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
1 parent afb8f35 commit 5e20e69

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
@@ -17,10 +17,10 @@
1717

1818
#include <algorithm>
1919

20-
CScript ParseScript(const std::string& s)
21-
{
22-
CScript result;
20+
namespace {
2321

22+
opcodetype ParseOpCode(const std::string& s)
23+
{
2424
static std::map<std::string, opcodetype> mapOpNames;
2525

2626
if (mapOpNames.empty())
@@ -42,6 +42,17 @@ CScript ParseScript(const std::string& s)
4242
}
4343
}
4444
}
45+
auto it = mapOpNames.find(s);
46+
if (it == mapOpNames.end()) throw std::runtime_error("script parse error: unknown opcode");
47+
return it->second;
48+
}
49+
50+
} // namespace
51+
52+
CScript ParseScript(const std::string& s)
53+
{
54+
CScript result;
55+
4556

4657
std::vector<std::string> words = SplitString(s, " \t\n");
4758

@@ -79,14 +90,10 @@ CScript ParseScript(const std::string& s)
7990
std::vector<unsigned char> value(w->begin()+1, w->end()-1);
8091
result << value;
8192
}
82-
else if (mapOpNames.count(*w))
83-
{
84-
// opcode, e.g. OP_ADD or ADD:
85-
result << mapOpNames[*w];
86-
}
8793
else
8894
{
89-
throw std::runtime_error("script parse error");
95+
// opcode, e.g. OP_ADD or ADD:
96+
result << ParseOpCode(*w);
9097
}
9198
}
9299

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@
202202
{ "exec": "./dash-tx",
203203
"args": ["-create", "outscript=0:123badscript"],
204204
"return_code": 1,
205-
"error_txt": "error: script parse error",
205+
"error_txt": "error: script parse error: unknown opcode",
206206
"description": "Create a new transaction with an invalid output script"
207207
},
208208
{ "exec": "./dash-tx",

0 commit comments

Comments
 (0)