From c92387232f750397da7d131f262c150a608408c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Barbosa?= Date: Mon, 31 Aug 2020 19:29:22 +0100 Subject: [PATCH] refactor: Extract ParseOpCode from ParseScript A second lookup in mapOpNames is also removed. --- src/core_read.cpp | 25 ++++++++++++++++--------- test/util/data/bitcoin-util-test.json | 2 +- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/core_read.cpp b/src/core_read.cpp index 1c0a8a096d..fbcb0d5e3b 100644 --- a/src/core_read.cpp +++ b/src/core_read.cpp @@ -21,10 +21,10 @@ #include #include -CScript ParseScript(const std::string& s) -{ - CScript result; +namespace { +opcodetype ParseOpCode(const std::string& s) +{ static std::map mapOpNames; if (mapOpNames.empty()) @@ -45,6 +45,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 words; boost::algorithm::split(words, s, boost::algorithm::is_any_of(" \t\n"), boost::algorithm::token_compress_on); @@ -82,14 +93,10 @@ CScript ParseScript(const std::string& s) std::vector value(w->begin()+1, w->end()-1); result << value; } - else if (mapOpNames.count(*w)) - { - // opcode, e.g. OP_ADD or ADD: - result << mapOpNames[*w]; - } else { - throw std::runtime_error("script parse error"); + // opcode, e.g. OP_ADD or ADD: + result << ParseOpCode(*w); } } diff --git a/test/util/data/bitcoin-util-test.json b/test/util/data/bitcoin-util-test.json index 99cd4ab695..0a9846b4be 100644 --- a/test/util/data/bitcoin-util-test.json +++ b/test/util/data/bitcoin-util-test.json @@ -221,7 +221,7 @@ { "exec": "./bitcoin-tx", "args": ["-create", "outscript=0:123badscript"], "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" }, { "exec": "./bitcoin-tx",