Changeset View
Changeset View
Standalone View
Standalone View
src/test/txvalidationcache_tests.cpp
// Copyright (c) 2011-2016 The Bitcoin Core developers | // Copyright (c) 2011-2016 The Bitcoin Core 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 <chain.h> | #include <chain.h> | ||||
#include <config.h> | #include <config.h> | ||||
#include <consensus/validation.h> | #include <consensus/validation.h> | ||||
#include <key.h> | #include <key.h> | ||||
#include <keystore.h> | #include <keystore.h> | ||||
#include <miner.h> | #include <miner.h> | ||||
#include <policy/policy.h> | |||||
#include <pubkey.h> | #include <pubkey.h> | ||||
#include <random.h> | #include <random.h> | ||||
#include <script/scriptcache.h> | #include <script/scriptcache.h> | ||||
#include <script/sighashtype.h> | #include <script/sighashtype.h> | ||||
#include <script/sign.h> | #include <script/sign.h> | ||||
#include <script/standard.h> | |||||
#include <txmempool.h> | #include <txmempool.h> | ||||
#include <util/time.h> | #include <util/time.h> | ||||
#include <validation.h> | #include <validation.h> | ||||
#include <test/lcg.h> | #include <test/lcg.h> | ||||
#include <test/sigutil.h> | #include <test/sigutil.h> | ||||
#include <test/test_bitcoin.h> | #include <test/test_bitcoin.h> | ||||
▲ Show 20 Lines • Show All 220 Lines • ▼ Show 20 Lines | // spend_tx is invalid according to DISCOURAGE_UPGRADABLE_NOPS | ||||
const CTransaction tx(spend_tx); | const CTransaction tx(spend_tx); | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
CValidationState state; | CValidationState state; | ||||
PrecomputedTransactionData ptd_spend_tx(tx); | PrecomputedTransactionData ptd_spend_tx(tx); | ||||
BOOST_CHECK(!CheckInputs(tx, state, pcoinsTip.get(), true, | BOOST_CHECK(!CheckInputs(tx, state, pcoinsTip.get(), true, | ||||
MANDATORY_SCRIPT_VERIFY_FLAGS | | STANDARD_SCRIPT_VERIFY_FLAGS, true, true, | ||||
SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS, | ptd_spend_tx, nullptr)); | ||||
true, true, ptd_spend_tx, nullptr)); | |||||
// If we call again asking for scriptchecks (as happens in | // If we call again asking for scriptchecks (as happens in | ||||
// ConnectBlock), we should add a script check object for this -- we're | // ConnectBlock), we should add a script check object for this -- we're | ||||
// not caching invalidity (if that changes, delete this test case). | // not caching invalidity (if that changes, delete this test case). | ||||
std::vector<CScriptCheck> scriptchecks; | std::vector<CScriptCheck> scriptchecks; | ||||
BOOST_CHECK(CheckInputs(tx, state, pcoinsTip.get(), true, | BOOST_CHECK(CheckInputs(tx, state, pcoinsTip.get(), true, | ||||
MANDATORY_SCRIPT_VERIFY_FLAGS | | STANDARD_SCRIPT_VERIFY_FLAGS, true, true, | ||||
SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS, | ptd_spend_tx, &scriptchecks)); | ||||
true, true, ptd_spend_tx, &scriptchecks)); | |||||
BOOST_CHECK_EQUAL(scriptchecks.size(), 1U); | BOOST_CHECK_EQUAL(scriptchecks.size(), 1U); | ||||
// Test that CheckInputs returns true iff cleanstack-enforcing flags are | // Test that CheckInputs returns true iff cleanstack-enforcing flags are | ||||
// not present. Don't add these checks to the cache, so that we can test | // not present. Don't add these checks to the cache, so that we can test | ||||
// later that block validation works fine in the absence of cached | // later that block validation works fine in the absence of cached | ||||
// successes. | // successes. | ||||
ValidateCheckInputsForAllFlags( | ValidateCheckInputsForAllFlags( | ||||
tx, SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS, 0, false, false); | tx, SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS, 0, false, false); | ||||
▲ Show 20 Lines • Show All 55 Lines • ▼ Show 20 Lines | // Test CHECKLOCKTIMEVERIFY | ||||
// 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); | ||||
PrecomputedTransactionData txdata(transaction); | PrecomputedTransactionData txdata(transaction); | ||||
BOOST_CHECK(CheckInputs(transaction, state, pcoinsTip.get(), true, | BOOST_CHECK(CheckInputs(transaction, state, pcoinsTip.get(), true, | ||||
MANDATORY_SCRIPT_VERIFY_FLAGS | | STANDARD_SCRIPT_VERIFY_FLAGS, true, true, | ||||
SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY, | txdata, nullptr)); | ||||
true, true, txdata, nullptr)); | |||||
} | } | ||||
// TEST CHECKSEQUENCEVERIFY | // TEST CHECKSEQUENCEVERIFY | ||||
{ | { | ||||
CMutableTransaction invalid_with_csv_tx; | CMutableTransaction invalid_with_csv_tx; | ||||
invalid_with_csv_tx.nVersion = 2; | invalid_with_csv_tx.nVersion = 2; | ||||
invalid_with_csv_tx.vin.resize(1); | invalid_with_csv_tx.vin.resize(1); | ||||
invalid_with_csv_tx.vin[0].prevout = COutPoint(spend_tx.GetId(), 2); | invalid_with_csv_tx.vin[0].prevout = COutPoint(spend_tx.GetId(), 2); | ||||
Show All 19 Lines | // TEST CHECKSEQUENCEVERIFY | ||||
// 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); | ||||
BOOST_CHECK(CheckInputs(transaction, state, pcoinsTip.get(), true, | BOOST_CHECK(CheckInputs(transaction, state, pcoinsTip.get(), true, | ||||
MANDATORY_SCRIPT_VERIFY_FLAGS | | STANDARD_SCRIPT_VERIFY_FLAGS, true, true, | ||||
SCRIPT_VERIFY_CHECKSEQUENCEVERIFY, | txdata, nullptr)); | ||||
true, true, txdata, nullptr)); | |||||
} | } | ||||
// TODO: add tests for remaining script flags | // TODO: add tests for remaining script flags | ||||
{ | { | ||||
// Test a transaction with multiple inputs. | // Test a transaction with multiple inputs. | ||||
CMutableTransaction tx; | CMutableTransaction tx; | ||||
Show All 38 Lines | // TODO: add tests for remaining script flags | ||||
CValidationState state; | CValidationState state; | ||||
CTransaction transaction(tx); | CTransaction transaction(tx); | ||||
PrecomputedTransactionData txdata(transaction); | PrecomputedTransactionData txdata(transaction); | ||||
// This transaction is now invalid because the second signature is | // This transaction is now invalid because the second signature is | ||||
// missing. | // missing. | ||||
BOOST_CHECK(!CheckInputs(transaction, state, pcoinsTip.get(), true, | BOOST_CHECK(!CheckInputs(transaction, state, pcoinsTip.get(), true, | ||||
MANDATORY_SCRIPT_VERIFY_FLAGS, true, true, | STANDARD_SCRIPT_VERIFY_FLAGS, true, true, | ||||
txdata, nullptr)); | txdata, nullptr)); | ||||
// Make sure this transaction was not cached (ie becausethe first input | // Make sure this transaction was not cached (ie becausethe first input | ||||
// was valid) | // was valid) | ||||
std::vector<CScriptCheck> scriptchecks; | std::vector<CScriptCheck> scriptchecks; | ||||
BOOST_CHECK(CheckInputs(transaction, state, pcoinsTip.get(), true, | BOOST_CHECK(CheckInputs(transaction, state, pcoinsTip.get(), true, | ||||
MANDATORY_SCRIPT_VERIFY_FLAGS, true, true, | STANDARD_SCRIPT_VERIFY_FLAGS, true, true, | ||||
txdata, &scriptchecks)); | txdata, &scriptchecks)); | ||||
// Should get 2 script checks back -- caching is on a whole-transaction | // Should get 2 script checks back -- caching is on a whole-transaction | ||||
// basis. | // basis. | ||||
BOOST_CHECK_EQUAL(scriptchecks.size(), 2U); | BOOST_CHECK_EQUAL(scriptchecks.size(), 2U); | ||||
} | } | ||||
} | } | ||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |