mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-02 14:37:42 -03:00
refactor: Extract ParseOpCode from ParseScript
A second lookup in mapOpNames is also removed.
This commit is contained in:
parent
89a8299a14
commit
c92387232f
2 changed files with 17 additions and 10 deletions
|
@ -21,10 +21,10 @@
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
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())
|
||||||
|
@ -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<std::string> words;
|
std::vector<std::string> words;
|
||||||
boost::algorithm::split(words, s, boost::algorithm::is_any_of(" \t\n"), boost::algorithm::token_compress_on);
|
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<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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -221,7 +221,7 @@
|
||||||
{ "exec": "./bitcoin-tx",
|
{ "exec": "./bitcoin-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": "./bitcoin-tx",
|
{ "exec": "./bitcoin-tx",
|
||||||
|
|
Loading…
Add table
Reference in a new issue