Merge #19851: refactor: Extract ParseOpCode from ParseScript

c92387232f750397da7d131f262c150a608408c2 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 c92387232f750397da7d131f262c150a608408c2
  theStack:
    re-ACK c92387232f750397da7d131f262c150a608408c2

Tree-SHA512: d59d1964760622cf365479d44e3e676aa0bf46b60e77160140d967e012042df92121d3224c7551dc96eff5ff3294598cc6bade82adb3f60d28810e18e60e1257
This commit is contained in:
Wladimir J. van der Laan 2020-11-20 05:34:17 +01:00 committed by PastaPastaPasta
parent afb8f35b51
commit 5e20e69415
2 changed files with 17 additions and 10 deletions

View File

@ -17,10 +17,10 @@
#include <algorithm> #include <algorithm>
CScript ParseScript(const std::string& s) namespace {
{
CScript result;
opcodetype ParseOpCode(const std::string& s)
{
static std::map<std::string, opcodetype> mapOpNames; static std::map<std::string, opcodetype> mapOpNames;
if (mapOpNames.empty()) if (mapOpNames.empty())
@ -42,6 +42,17 @@ CScript ParseScript(const std::string& s)
} }
} }
} }
auto it = mapOpNames.find(s);
if (it == mapOpNames.end()) throw std::runtime_error("script parse error: unknown opcode");
return it->second;
}
} // namespace
CScript ParseScript(const std::string& s)
{
CScript result;
std::vector<std::string> words = SplitString(s, " \t\n"); std::vector<std::string> words = SplitString(s, " \t\n");
@ -79,14 +90,10 @@ CScript ParseScript(const std::string& s)
std::vector<unsigned char> value(w->begin()+1, w->end()-1); std::vector<unsigned char> value(w->begin()+1, w->end()-1);
result << value; result << value;
} }
else if (mapOpNames.count(*w))
{
// opcode, e.g. OP_ADD or ADD:
result << mapOpNames[*w];
}
else else
{ {
throw std::runtime_error("script parse error"); // opcode, e.g. OP_ADD or ADD:
result << ParseOpCode(*w);
} }
} }

View File

@ -202,7 +202,7 @@
{ "exec": "./dash-tx", { "exec": "./dash-tx",
"args": ["-create", "outscript=0:123badscript"], "args": ["-create", "outscript=0:123badscript"],
"return_code": 1, "return_code": 1,
"error_txt": "error: script parse error", "error_txt": "error: script parse error: unknown opcode",
"description": "Create a new transaction with an invalid output script" "description": "Create a new transaction with an invalid output script"
}, },
{ "exec": "./dash-tx", { "exec": "./dash-tx",