Changeset View
Changeset View
Standalone View
Standalone View
src/test/scriptflags.cpp
// Copyright (c) 2017 The Bitcoin developers | // Copyright (c) 2017 The Bitcoin developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include "test/scriptflags.h" | #include "test/scriptflags.h" | ||||
#include "script/interpreter.h" | #include "script/interpreter.h" | ||||
#include <boost/algorithm/string/classification.hpp> | #include <boost/algorithm/string/classification.hpp> | ||||
#include <boost/algorithm/string/split.hpp> | #include <boost/algorithm/string/split.hpp> | ||||
#include <boost/test/unit_test.hpp> | #include <boost/test/unit_test.hpp> | ||||
#include <map> | #include <map> | ||||
#include <vector> | #include <vector> | ||||
static std::map<std::string, unsigned int> mapFlagNames = { | static std::map<std::string, uint32_t> mapFlagNames = { | ||||
{"NONE", SCRIPT_VERIFY_NONE}, | {"NONE", SCRIPT_VERIFY_NONE}, | ||||
{"P2SH", SCRIPT_VERIFY_P2SH}, | {"P2SH", SCRIPT_VERIFY_P2SH}, | ||||
{"STRICTENC", SCRIPT_VERIFY_STRICTENC}, | {"STRICTENC", SCRIPT_VERIFY_STRICTENC}, | ||||
{"DERSIG", SCRIPT_VERIFY_DERSIG}, | {"DERSIG", SCRIPT_VERIFY_DERSIG}, | ||||
{"LOW_S", SCRIPT_VERIFY_LOW_S}, | {"LOW_S", SCRIPT_VERIFY_LOW_S}, | ||||
{"SIGPUSHONLY", SCRIPT_VERIFY_SIGPUSHONLY}, | {"SIGPUSHONLY", SCRIPT_VERIFY_SIGPUSHONLY}, | ||||
{"MINIMALDATA", SCRIPT_VERIFY_MINIMALDATA}, | {"MINIMALDATA", SCRIPT_VERIFY_MINIMALDATA}, | ||||
{"NULLDUMMY", SCRIPT_VERIFY_NULLDUMMY}, | {"NULLDUMMY", SCRIPT_VERIFY_NULLDUMMY}, | ||||
{"DISCOURAGE_UPGRADABLE_NOPS", SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS}, | {"DISCOURAGE_UPGRADABLE_NOPS", SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS}, | ||||
{"CLEANSTACK", SCRIPT_VERIFY_CLEANSTACK}, | {"CLEANSTACK", SCRIPT_VERIFY_CLEANSTACK}, | ||||
{"MINIMALIF", SCRIPT_VERIFY_MINIMALIF}, | {"MINIMALIF", SCRIPT_VERIFY_MINIMALIF}, | ||||
{"NULLFAIL", SCRIPT_VERIFY_NULLFAIL}, | {"NULLFAIL", SCRIPT_VERIFY_NULLFAIL}, | ||||
{"CHECKLOCKTIMEVERIFY", SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY}, | {"CHECKLOCKTIMEVERIFY", SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY}, | ||||
{"CHECKSEQUENCEVERIFY", SCRIPT_VERIFY_CHECKSEQUENCEVERIFY}, | {"CHECKSEQUENCEVERIFY", SCRIPT_VERIFY_CHECKSEQUENCEVERIFY}, | ||||
{"DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM", | {"DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM", | ||||
SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM}, | SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM}, | ||||
{"COMPRESSED_PUBKEYTYPE", SCRIPT_VERIFY_COMPRESSED_PUBKEYTYPE}, | {"COMPRESSED_PUBKEYTYPE", SCRIPT_VERIFY_COMPRESSED_PUBKEYTYPE}, | ||||
{"SIGHASH_FORKID", SCRIPT_ENABLE_SIGHASH_FORKID}, | {"SIGHASH_FORKID", SCRIPT_ENABLE_SIGHASH_FORKID}, | ||||
}; | }; | ||||
unsigned int ParseScriptFlags(std::string strFlags) { | uint32_t ParseScriptFlags(std::string strFlags) { | ||||
if (strFlags.empty()) { | if (strFlags.empty()) { | ||||
return 0; | return 0; | ||||
} | } | ||||
unsigned int flags = 0; | |||||
uint32_t flags = 0; | |||||
std::vector<std::string> words; | std::vector<std::string> words; | ||||
boost::algorithm::split(words, strFlags, boost::algorithm::is_any_of(",")); | boost::algorithm::split(words, strFlags, boost::algorithm::is_any_of(",")); | ||||
for (std::string &word : words) { | for (std::string &word : words) { | ||||
if (!mapFlagNames.count(word)) | if (!mapFlagNames.count(word)) | ||||
BOOST_ERROR("Bad test: unknown verification flag '" << word << "'"); | BOOST_ERROR("Bad test: unknown verification flag '" << word << "'"); | ||||
flags |= mapFlagNames[word]; | flags |= mapFlagNames[word]; | ||||
} | } | ||||
return flags; | return flags; | ||||
} | } | ||||
std::string FormatScriptFlags(unsigned int flags) { | std::string FormatScriptFlags(uint32_t flags) { | ||||
if (flags == 0) { | if (flags == 0) { | ||||
return ""; | return ""; | ||||
} | } | ||||
std::string ret; | std::string ret; | ||||
std::map<std::string, unsigned int>::const_iterator it = | std::map<std::string, uint32_t>::const_iterator it = mapFlagNames.begin(); | ||||
mapFlagNames.begin(); | |||||
while (it != mapFlagNames.end()) { | while (it != mapFlagNames.end()) { | ||||
if (flags & it->second) { | if (flags & it->second) { | ||||
ret += it->first + ","; | ret += it->first + ","; | ||||
} | } | ||||
it++; | it++; | ||||
} | } | ||||
return ret.substr(0, ret.size() - 1); | return ret.substr(0, ret.size() - 1); | ||||
} | } |