Changeset View
Changeset View
Standalone View
Standalone View
src/test/script_tests.cpp
Show First 20 Lines • Show All 142 Lines • ▼ Show 20 Lines | BuildSpendingTransaction(const CScript &scriptSig, | ||||
txSpend.vin[0].nSequence = CTxIn::SEQUENCE_FINAL; | txSpend.vin[0].nSequence = CTxIn::SEQUENCE_FINAL; | ||||
txSpend.vout[0].scriptPubKey = CScript(); | txSpend.vout[0].scriptPubKey = CScript(); | ||||
txSpend.vout[0].nValue = txCredit.vout[0].nValue; | txSpend.vout[0].nValue = txCredit.vout[0].nValue; | ||||
return txSpend; | return txSpend; | ||||
} | } | ||||
static void DoTest(const CScript &scriptPubKey, const CScript &scriptSig, | static void DoTest(const CScript &scriptPubKey, const CScript &scriptSig, | ||||
int flags, const std::string &message, int scriptError, | uint32_t flags, const std::string &message, int scriptError, | ||||
const Amount nValue) { | const Amount nValue) { | ||||
bool expect = (scriptError == SCRIPT_ERR_OK); | bool expect = (scriptError == SCRIPT_ERR_OK); | ||||
if (flags & SCRIPT_VERIFY_CLEANSTACK) { | if (flags & SCRIPT_VERIFY_CLEANSTACK) { | ||||
flags |= SCRIPT_VERIFY_P2SH; | flags |= SCRIPT_VERIFY_P2SH; | ||||
} | } | ||||
ScriptError err; | ScriptError err; | ||||
CMutableTransaction txCredit = | CMutableTransaction txCredit = | ||||
BuildCreditingTransaction(scriptPubKey, nValue); | BuildCreditingTransaction(scriptPubKey, nValue); | ||||
CMutableTransaction tx = BuildSpendingTransaction(scriptSig, txCredit); | CMutableTransaction tx = BuildSpendingTransaction(scriptSig, txCredit); | ||||
CMutableTransaction tx2 = tx; | CMutableTransaction tx2 = tx; | ||||
BOOST_CHECK_MESSAGE(VerifyScript(scriptSig, scriptPubKey, flags, | BOOST_CHECK_MESSAGE(VerifyScript(scriptSig, scriptPubKey, flags, | ||||
MutableTransactionSignatureChecker( | MutableTransactionSignatureChecker( | ||||
&tx, 0, txCredit.vout[0].nValue), | &tx, 0, txCredit.vout[0].nValue), | ||||
&err) == expect, | &err) == expect, | ||||
message); | message); | ||||
BOOST_CHECK_MESSAGE( | BOOST_CHECK_MESSAGE( | ||||
err == scriptError, | err == scriptError, | ||||
std::string(FormatScriptError(err)) + " where " + | std::string(FormatScriptError(err)) + " where " + | ||||
std::string(FormatScriptError((ScriptError_t)scriptError)) + | std::string(FormatScriptError((ScriptError_t)scriptError)) + | ||||
" expected: " + message); | " expected: " + message); | ||||
#if defined(HAVE_CONSENSUS_LIB) | #if defined(HAVE_CONSENSUS_LIB) | ||||
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); | CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); | ||||
stream << tx2; | stream << tx2; | ||||
int libconsensus_flags = flags & bitcoinconsensus_SCRIPT_FLAGS_VERIFY_ALL; | uint32_t libconsensus_flags = | ||||
flags & bitcoinconsensus_SCRIPT_FLAGS_VERIFY_ALL; | |||||
if (libconsensus_flags == flags) { | if (libconsensus_flags == flags) { | ||||
if (flags & bitcoinconsensus_SCRIPT_ENABLE_SIGHASH_FORKID) { | if (flags & bitcoinconsensus_SCRIPT_ENABLE_SIGHASH_FORKID) { | ||||
BOOST_CHECK_MESSAGE(bitcoinconsensus_verify_script_with_amount( | BOOST_CHECK_MESSAGE(bitcoinconsensus_verify_script_with_amount( | ||||
scriptPubKey.data(), scriptPubKey.size(), | scriptPubKey.data(), scriptPubKey.size(), | ||||
txCredit.vout[0].nValue / SATOSHI, | txCredit.vout[0].nValue / SATOSHI, | ||||
(const uint8_t *)&stream[0], stream.size(), | (const uint8_t *)&stream[0], stream.size(), | ||||
0, libconsensus_flags, nullptr) == expect, | 0, libconsensus_flags, nullptr) == expect, | ||||
message); | message); | ||||
▲ Show 20 Lines • Show All 53 Lines • ▼ Show 20 Lines | private: | ||||
CScript script; | CScript script; | ||||
//! The P2SH redeemscript | //! The P2SH redeemscript | ||||
CScript redeemscript; | CScript redeemscript; | ||||
CTransactionRef creditTx; | CTransactionRef creditTx; | ||||
CMutableTransaction spendTx; | CMutableTransaction spendTx; | ||||
bool havePush; | bool havePush; | ||||
std::vector<uint8_t> push; | std::vector<uint8_t> push; | ||||
std::string comment; | std::string comment; | ||||
int flags; | uint32_t flags; | ||||
int scriptError; | int scriptError; | ||||
Amount nValue; | Amount nValue; | ||||
void DoPush() { | void DoPush() { | ||||
if (havePush) { | if (havePush) { | ||||
spendTx.vin[0].scriptSig << push; | spendTx.vin[0].scriptSig << push; | ||||
havePush = false; | havePush = false; | ||||
} | } | ||||
Show All 22 Lines | std::vector<uint8_t> DoSign(const CKey &key, const uint256 &hash, | ||||
vchSig.begin() + 6 + vchSig[3] + | vchSig.begin() + 6 + vchSig[3] + | ||||
vchSig[5 + vchSig[3]]); | vchSig[5 + vchSig[3]]); | ||||
} while (lenR != r.size() || lenS != s.size()); | } while (lenR != r.size() || lenS != s.size()); | ||||
return vchSig; | return vchSig; | ||||
} | } | ||||
public: | public: | ||||
TestBuilder(const CScript &script_, const std::string &comment_, int flags_, | TestBuilder(const CScript &script_, const std::string &comment_, | ||||
bool P2SH = false, Amount nValue_ = Amount::zero()) | uint32_t flags_, bool P2SH = false, | ||||
Amount nValue_ = Amount::zero()) | |||||
: script(script_), havePush(false), comment(comment_), flags(flags_), | : script(script_), havePush(false), comment(comment_), flags(flags_), | ||||
scriptError(SCRIPT_ERR_OK), nValue(nValue_) { | scriptError(SCRIPT_ERR_OK), nValue(nValue_) { | ||||
CScript scriptPubKey = script; | CScript scriptPubKey = script; | ||||
if (P2SH) { | if (P2SH) { | ||||
redeemscript = scriptPubKey; | redeemscript = scriptPubKey; | ||||
scriptPubKey = CScript() | scriptPubKey = CScript() | ||||
<< OP_HASH160 | << OP_HASH160 | ||||
<< ToByteVector(CScriptID(redeemscript)) << OP_EQUAL; | << ToByteVector(CScriptID(redeemscript)) << OP_EQUAL; | ||||
Show All 34 Lines | TestBuilder &Push(const CScript &_script) { | ||||
DoPush(std::vector<uint8_t>(_script.begin(), _script.end())); | DoPush(std::vector<uint8_t>(_script.begin(), _script.end())); | ||||
return *this; | return *this; | ||||
} | } | ||||
TestBuilder &PushSig(const CKey &key, | TestBuilder &PushSig(const CKey &key, | ||||
SigHashType sigHashType = SigHashType(), | SigHashType sigHashType = SigHashType(), | ||||
unsigned int lenR = 32, unsigned int lenS = 32, | unsigned int lenR = 32, unsigned int lenS = 32, | ||||
Amount amount = Amount::zero(), | Amount amount = Amount::zero(), | ||||
uint32_t flags = SCRIPT_ENABLE_SIGHASH_FORKID) { | uint32_t sigFlags = SCRIPT_ENABLE_SIGHASH_FORKID) { | ||||
uint256 hash = SignatureHash(script, CTransaction(spendTx), 0, | uint256 hash = SignatureHash(script, CTransaction(spendTx), 0, | ||||
sigHashType, amount, nullptr, flags); | sigHashType, amount, nullptr, sigFlags); | ||||
std::vector<uint8_t> vchSig = DoSign(key, hash, lenR, lenS); | std::vector<uint8_t> vchSig = DoSign(key, hash, lenR, lenS); | ||||
vchSig.push_back(static_cast<uint8_t>(sigHashType.getRawSigHashType())); | vchSig.push_back(static_cast<uint8_t>(sigHashType.getRawSigHashType())); | ||||
DoPush(vchSig); | DoPush(vchSig); | ||||
return *this; | return *this; | ||||
} | } | ||||
TestBuilder &PushDataSig(const CKey &key, const std::vector<uint8_t> &data, | TestBuilder &PushDataSig(const CKey &key, const std::vector<uint8_t> &data, | ||||
unsigned int lenR = 32, unsigned int lenS = 32) { | unsigned int lenR = 32, unsigned int lenS = 32) { | ||||
▲ Show 20 Lines • Show All 1,722 Lines • Show Last 20 Lines |