Changeset View
Changeset View
Standalone View
Standalone View
src/test/script_tests.cpp
Show First 20 Lines • Show All 87 Lines • ▼ Show 20 Lines | static ScriptErrorDesc script_errors[] = { | ||||
{SCRIPT_ERR_DISCOURAGE_UPGRADABLE_NOPS, "DISCOURAGE_UPGRADABLE_NOPS"}, | {SCRIPT_ERR_DISCOURAGE_UPGRADABLE_NOPS, "DISCOURAGE_UPGRADABLE_NOPS"}, | ||||
{SCRIPT_ERR_NONCOMPRESSED_PUBKEY, "NONCOMPRESSED_PUBKEY"}, | {SCRIPT_ERR_NONCOMPRESSED_PUBKEY, "NONCOMPRESSED_PUBKEY"}, | ||||
{SCRIPT_ERR_ILLEGAL_FORKID, "ILLEGAL_FORKID"}, | {SCRIPT_ERR_ILLEGAL_FORKID, "ILLEGAL_FORKID"}, | ||||
{SCRIPT_ERR_MUST_USE_FORKID, "MISSING_FORKID"}, | {SCRIPT_ERR_MUST_USE_FORKID, "MISSING_FORKID"}, | ||||
{SCRIPT_ERR_DIV_BY_ZERO, "DIV_BY_ZERO"}, | {SCRIPT_ERR_DIV_BY_ZERO, "DIV_BY_ZERO"}, | ||||
{SCRIPT_ERR_MOD_BY_ZERO, "MOD_BY_ZERO"}, | {SCRIPT_ERR_MOD_BY_ZERO, "MOD_BY_ZERO"}, | ||||
}; | }; | ||||
const char *FormatScriptError(ScriptError_t err) { | static const char *FormatScriptError(ScriptError_t err) { | ||||
for (size_t i = 0; i < ARRAYLEN(script_errors); ++i) { | for (size_t i = 0; i < ARRAYLEN(script_errors); ++i) { | ||||
if (script_errors[i].err == err) { | if (script_errors[i].err == err) { | ||||
return script_errors[i].name; | return script_errors[i].name; | ||||
} | } | ||||
} | } | ||||
BOOST_ERROR("Unknown scripterror enumeration value, update script_errors " | BOOST_ERROR("Unknown scripterror enumeration value, update script_errors " | ||||
"in script_tests.cpp."); | "in script_tests.cpp."); | ||||
return ""; | return ""; | ||||
} | } | ||||
ScriptError_t ParseScriptError(const std::string &name) { | static ScriptError_t ParseScriptError(const std::string &name) { | ||||
for (size_t i = 0; i < ARRAYLEN(script_errors); ++i) { | for (size_t i = 0; i < ARRAYLEN(script_errors); ++i) { | ||||
if (script_errors[i].name == name) { | if (script_errors[i].name == name) { | ||||
return script_errors[i].err; | return script_errors[i].err; | ||||
} | } | ||||
} | } | ||||
BOOST_ERROR("Unknown scripterror \"" << name << "\" in test description"); | BOOST_ERROR("Unknown scripterror \"" << name << "\" in test description"); | ||||
return SCRIPT_ERR_UNKNOWN_ERROR; | return SCRIPT_ERR_UNKNOWN_ERROR; | ||||
▲ Show 20 Lines • Show All 1,761 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(script_PushData) { | ||||
std::vector<std::vector<uint8_t>> pushdata4Stack; | std::vector<std::vector<uint8_t>> pushdata4Stack; | ||||
BOOST_CHECK(EvalScript( | BOOST_CHECK(EvalScript( | ||||
pushdata4Stack, CScript(&pushdata4[0], &pushdata4[sizeof(pushdata4)]), | pushdata4Stack, CScript(&pushdata4[0], &pushdata4[sizeof(pushdata4)]), | ||||
SCRIPT_VERIFY_P2SH, BaseSignatureChecker(), &err)); | SCRIPT_VERIFY_P2SH, BaseSignatureChecker(), &err)); | ||||
BOOST_CHECK(pushdata4Stack == directStack); | BOOST_CHECK(pushdata4Stack == directStack); | ||||
BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err)); | BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err)); | ||||
} | } | ||||
CScript sign_multisig(const CScript &scriptPubKey, | static CScript sign_multisig(const CScript &scriptPubKey, | ||||
const std::vector<CKey> &keys, | const std::vector<CKey> &keys, | ||||
const CTransaction &transaction) { | const CTransaction &transaction) { | ||||
uint256 hash = SignatureHash(scriptPubKey, transaction, 0, SigHashType(), | uint256 hash = SignatureHash(scriptPubKey, transaction, 0, SigHashType(), | ||||
Amount::zero()); | Amount::zero()); | ||||
CScript result; | CScript result; | ||||
// | // | ||||
// NOTE: CHECKMULTISIG has an unfortunate bug; it requires one extra item on | // NOTE: CHECKMULTISIG has an unfortunate bug; it requires one extra item on | ||||
// the stack, before the signatures. Putting OP_0 on the stack is the | // the stack, before the signatures. Putting OP_0 on the stack is the | ||||
// workaround; fixing the bug would mean splitting the block chain (old | // workaround; fixing the bug would mean splitting the block chain (old | ||||
// clients would not accept new CHECKMULTISIG transactions, and vice-versa) | // clients would not accept new CHECKMULTISIG transactions, and vice-versa) | ||||
// | // | ||||
result << OP_0; | result << OP_0; | ||||
for (const CKey &key : keys) { | for (const CKey &key : keys) { | ||||
std::vector<uint8_t> vchSig; | std::vector<uint8_t> vchSig; | ||||
BOOST_CHECK(key.SignECDSA(hash, vchSig)); | BOOST_CHECK(key.SignECDSA(hash, vchSig)); | ||||
vchSig.push_back(uint8_t(SIGHASH_ALL)); | vchSig.push_back(uint8_t(SIGHASH_ALL)); | ||||
result << vchSig; | result << vchSig; | ||||
} | } | ||||
return result; | return result; | ||||
} | } | ||||
CScript sign_multisig(const CScript &scriptPubKey, const CKey &key, | static CScript sign_multisig(const CScript &scriptPubKey, const CKey &key, | ||||
const CTransaction &transaction) { | const CTransaction &transaction) { | ||||
std::vector<CKey> keys; | std::vector<CKey> keys; | ||||
keys.push_back(key); | keys.push_back(key); | ||||
return sign_multisig(scriptPubKey, keys, transaction); | return sign_multisig(scriptPubKey, keys, transaction); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG12) { | BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG12) { | ||||
ScriptError err; | ScriptError err; | ||||
CKey key1, key2, key3; | CKey key1, key2, key3; | ||||
▲ Show 20 Lines • Show All 657 Lines • Show Last 20 Lines |