Changeset View
Changeset View
Standalone View
Standalone View
src/test/txvalidationcache_tests.cpp
Show First 20 Lines • Show All 96 Lines • ▼ Show 20 Lines | |||||
// CHECKLOCKTIMEVERIFY and CHECKSEQUENCEVERIFY (and future NOP codes that may | // CHECKLOCKTIMEVERIFY and CHECKSEQUENCEVERIFY (and future NOP codes that may | ||||
// get reassigned) have an interaction with DISCOURAGE_UPGRADABLE_NOPS: if the | // get reassigned) have an interaction with DISCOURAGE_UPGRADABLE_NOPS: if the | ||||
// script flags used contain DISCOURAGE_UPGRADABLE_NOPS but don't contain | // script flags used contain DISCOURAGE_UPGRADABLE_NOPS but don't contain | ||||
// CHECKLOCKTIMEVERIFY (or CHECKSEQUENCEVERIFY), but the script does contain | // CHECKLOCKTIMEVERIFY (or CHECKSEQUENCEVERIFY), but the script does contain | ||||
// OP_CHECKLOCKTIMEVERIFY (or OP_CHECKSEQUENCEVERIFY), then script execution | // OP_CHECKLOCKTIMEVERIFY (or OP_CHECKSEQUENCEVERIFY), then script execution | ||||
// should fail. | // should fail. | ||||
// Capture this interaction with the upgraded_nop argument: set it when | // Capture this interaction with the upgraded_nop argument: set it when | ||||
// evaluating any script flag that is implemented as an upgraded NOP code. | // evaluating any script flag that is implemented as an upgraded NOP code. | ||||
void ValidateCheckInputsForAllFlags(const CMutableTransaction &mutableTx, | void ValidateCheckInputsForAllFlags(const CTransaction &tx, | ||||
uint32_t failing_flags, bool add_to_cache, | uint32_t failing_flags, bool add_to_cache, | ||||
bool upgraded_nop) { | bool upgraded_nop) { | ||||
const CTransaction tx(mutableTx); | |||||
PrecomputedTransactionData txdata(tx); | PrecomputedTransactionData txdata(tx); | ||||
// If we add many more flags, this loop can get too expensive, but we can | // If we add many more flags, this loop can get too expensive, but we can | ||||
// rewrite in the future to randomly pick a set of flags to evaluate. | // rewrite in the future to randomly pick a set of flags to evaluate. | ||||
for (uint32_t test_flags = 0; test_flags < (1U << 17); test_flags += 1) { | for (uint32_t test_flags = 0; test_flags < (1U << 17); test_flags += 1) { | ||||
CValidationState state; | CValidationState state; | ||||
// Make sure the mandatory flags are enabled. | // Make sure the mandatory flags are enabled. | ||||
test_flags |= MANDATORY_SCRIPT_VERIFY_FLAGS; | test_flags |= MANDATORY_SCRIPT_VERIFY_FLAGS; | ||||
▲ Show 20 Lines • Show All 179 Lines • ▼ Show 20 Lines | // test validity with P2SH. | ||||
invalid_under_p2sh_tx.vin[0].prevout = COutPoint(spend_tx.GetId(), 0); | invalid_under_p2sh_tx.vin[0].prevout = COutPoint(spend_tx.GetId(), 0); | ||||
invalid_under_p2sh_tx.vout.resize(1); | invalid_under_p2sh_tx.vout.resize(1); | ||||
invalid_under_p2sh_tx.vout[0].nValue = 11 * CENT; | invalid_under_p2sh_tx.vout[0].nValue = 11 * CENT; | ||||
invalid_under_p2sh_tx.vout[0].scriptPubKey = p2pk_scriptPubKey; | invalid_under_p2sh_tx.vout[0].scriptPubKey = p2pk_scriptPubKey; | ||||
std::vector<uint8_t> vchSig2(p2pk_scriptPubKey.begin(), | std::vector<uint8_t> vchSig2(p2pk_scriptPubKey.begin(), | ||||
p2pk_scriptPubKey.end()); | p2pk_scriptPubKey.end()); | ||||
invalid_under_p2sh_tx.vin[0].scriptSig << vchSig2; | invalid_under_p2sh_tx.vin[0].scriptSig << vchSig2; | ||||
ValidateCheckInputsForAllFlags(invalid_under_p2sh_tx, | ValidateCheckInputsForAllFlags(CTransaction(invalid_under_p2sh_tx), | ||||
SCRIPT_VERIFY_P2SH, true, false); | SCRIPT_VERIFY_P2SH, true, false); | ||||
} | } | ||||
// Test CHECKLOCKTIMEVERIFY | // Test CHECKLOCKTIMEVERIFY | ||||
{ | { | ||||
CMutableTransaction invalid_with_cltv_tx; | CMutableTransaction invalid_with_cltv_tx; | ||||
invalid_with_cltv_tx.nVersion = 1; | invalid_with_cltv_tx.nVersion = 1; | ||||
invalid_with_cltv_tx.nLockTime = 100; | invalid_with_cltv_tx.nLockTime = 100; | ||||
invalid_with_cltv_tx.vin.resize(1); | invalid_with_cltv_tx.vin.resize(1); | ||||
invalid_with_cltv_tx.vin[0].prevout = COutPoint(spend_tx.GetId(), 1); | invalid_with_cltv_tx.vin[0].prevout = COutPoint(spend_tx.GetId(), 1); | ||||
invalid_with_cltv_tx.vin[0].nSequence = 0; | invalid_with_cltv_tx.vin[0].nSequence = 0; | ||||
invalid_with_cltv_tx.vout.resize(1); | invalid_with_cltv_tx.vout.resize(1); | ||||
invalid_with_cltv_tx.vout[0].nValue = 11 * CENT; | invalid_with_cltv_tx.vout[0].nValue = 11 * CENT; | ||||
invalid_with_cltv_tx.vout[0].scriptPubKey = p2pk_scriptPubKey; | invalid_with_cltv_tx.vout[0].scriptPubKey = p2pk_scriptPubKey; | ||||
// Sign | // Sign | ||||
std::vector<uint8_t> vchSig; | std::vector<uint8_t> vchSig; | ||||
uint256 hash = SignatureHash( | uint256 hash = SignatureHash( | ||||
spend_tx.vout[1].scriptPubKey, CTransaction(invalid_with_cltv_tx), | spend_tx.vout[1].scriptPubKey, CTransaction(invalid_with_cltv_tx), | ||||
0, SigHashType().withForkId(), spend_tx.vout[1].nValue); | 0, SigHashType().withForkId(), spend_tx.vout[1].nValue); | ||||
BOOST_CHECK(coinbaseKey.SignECDSA(hash, vchSig)); | BOOST_CHECK(coinbaseKey.SignECDSA(hash, vchSig)); | ||||
vchSig.push_back(uint8_t(SIGHASH_ALL | SIGHASH_FORKID)); | vchSig.push_back(uint8_t(SIGHASH_ALL | SIGHASH_FORKID)); | ||||
invalid_with_cltv_tx.vin[0].scriptSig = CScript() << vchSig << 101; | invalid_with_cltv_tx.vin[0].scriptSig = CScript() << vchSig << 101; | ||||
ValidateCheckInputsForAllFlags(invalid_with_cltv_tx, | ValidateCheckInputsForAllFlags(CTransaction(invalid_with_cltv_tx), | ||||
SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY, true, | SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY, true, | ||||
true); | true); | ||||
// Make it valid, and check again | // Make it valid, and check again | ||||
invalid_with_cltv_tx.vin[0].scriptSig = CScript() << vchSig << 100; | invalid_with_cltv_tx.vin[0].scriptSig = CScript() << vchSig << 100; | ||||
CValidationState state; | CValidationState state; | ||||
CTransaction transaction(invalid_with_cltv_tx); | CTransaction transaction(invalid_with_cltv_tx); | ||||
Show All 20 Lines | // TEST CHECKSEQUENCEVERIFY | ||||
std::vector<uint8_t> vchSig; | std::vector<uint8_t> vchSig; | ||||
uint256 hash = SignatureHash( | uint256 hash = SignatureHash( | ||||
spend_tx.vout[2].scriptPubKey, CTransaction(invalid_with_csv_tx), 0, | spend_tx.vout[2].scriptPubKey, CTransaction(invalid_with_csv_tx), 0, | ||||
SigHashType().withForkId(), spend_tx.vout[2].nValue); | SigHashType().withForkId(), spend_tx.vout[2].nValue); | ||||
BOOST_CHECK(coinbaseKey.SignECDSA(hash, vchSig)); | BOOST_CHECK(coinbaseKey.SignECDSA(hash, vchSig)); | ||||
vchSig.push_back(uint8_t(SIGHASH_ALL | SIGHASH_FORKID)); | vchSig.push_back(uint8_t(SIGHASH_ALL | SIGHASH_FORKID)); | ||||
invalid_with_csv_tx.vin[0].scriptSig = CScript() << vchSig << 101; | invalid_with_csv_tx.vin[0].scriptSig = CScript() << vchSig << 101; | ||||
ValidateCheckInputsForAllFlags( | ValidateCheckInputsForAllFlags(CTransaction(invalid_with_csv_tx), | ||||
invalid_with_csv_tx, SCRIPT_VERIFY_CHECKSEQUENCEVERIFY, true, true); | SCRIPT_VERIFY_CHECKSEQUENCEVERIFY, true, | ||||
true); | |||||
// Make it valid, and check again | // Make it valid, and check again | ||||
invalid_with_csv_tx.vin[0].scriptSig = CScript() << vchSig << 100; | invalid_with_csv_tx.vin[0].scriptSig = CScript() << vchSig << 100; | ||||
CValidationState state; | CValidationState state; | ||||
CTransaction transaction(invalid_with_csv_tx); | CTransaction transaction(invalid_with_csv_tx); | ||||
PrecomputedTransactionData txdata(transaction); | PrecomputedTransactionData txdata(transaction); | ||||
Show All 26 Lines | // TODO: add tests for remaining script flags | ||||
UpdateTransaction(tx, 0, sigdata); | UpdateTransaction(tx, 0, sigdata); | ||||
ProduceSignature( | ProduceSignature( | ||||
MutableTransactionSignatureCreator(&keystore, &tx, 1, 11 * CENT, | MutableTransactionSignatureCreator(&keystore, &tx, 1, 11 * CENT, | ||||
SigHashType().withForkId()), | SigHashType().withForkId()), | ||||
spend_tx.vout[3].scriptPubKey, sigdata); | spend_tx.vout[3].scriptPubKey, sigdata); | ||||
UpdateTransaction(tx, 1, sigdata); | UpdateTransaction(tx, 1, sigdata); | ||||
// This should be valid under all script flags | // This should be valid under all script flags | ||||
ValidateCheckInputsForAllFlags(tx, 0, true, false); | ValidateCheckInputsForAllFlags(CTransaction(tx), 0, true, false); | ||||
// Check that if the second input is invalid, but the first input is | // Check that if the second input is invalid, but the first input is | ||||
// valid, the transaction is not cached. | // valid, the transaction is not cached. | ||||
// Invalidate vin[1] | // Invalidate vin[1] | ||||
tx.vin[1].scriptSig = CScript(); | tx.vin[1].scriptSig = CScript(); | ||||
CValidationState state; | CValidationState state; | ||||
CTransaction transaction(tx); | CTransaction transaction(tx); | ||||
Show All 21 Lines |