2017-11-09 19:34:51 +01:00
|
|
|
/*
|
|
|
|
|
* This file is part of the Flowee project
|
|
|
|
|
* Copyright (C) 2011-2015 The Bitcoin Core developers
|
2021-06-20 22:44:44 +02:00
|
|
|
* Copyright (C) 2017 Tom Zander <tom@flowee.org>
|
2017-11-09 19:34:51 +01:00
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
2015-03-03 09:59:32 -05:00
|
|
|
|
2018-01-15 15:26:12 +00:00
|
|
|
#include "test_bitcoin.h"
|
2015-03-03 09:59:32 -05:00
|
|
|
#include "consensus/validation.h"
|
|
|
|
|
#include "main.h"
|
|
|
|
|
#include "script/standard.h"
|
2021-11-02 10:23:33 +01:00
|
|
|
#include <primitives/Block.h>
|
2023-11-24 18:16:32 +01:00
|
|
|
#include <primitives/PrivateKey.h>
|
2018-01-15 15:26:12 +00:00
|
|
|
#include <consensus/consensus.h>
|
2015-03-03 09:59:32 -05:00
|
|
|
|
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE(tx_validationcache_tests)
|
|
|
|
|
|
2017-08-24 10:07:20 +02:00
|
|
|
#ifndef WIN32 // Avoid irrelevant fail due to database handles still being open at exit
|
2018-01-15 15:26:12 +00:00
|
|
|
//
|
|
|
|
|
// Testing fixture that pre-creates a
|
|
|
|
|
// 100-block REGTEST-mode block chain
|
|
|
|
|
//
|
|
|
|
|
class TestChain100Setup : public TestingSetup {
|
|
|
|
|
public:
|
|
|
|
|
TestChain100Setup() {
|
|
|
|
|
// Generate a 100-block chain:
|
2022-05-11 13:46:15 +02:00
|
|
|
coinbaseKey.makeNewKey();
|
|
|
|
|
CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.getPubKey()) << OP_CHECKSIG;
|
2018-01-15 15:26:12 +00:00
|
|
|
CBlockIndex *parent = bv.blockchain()->Tip();
|
|
|
|
|
CBlockIndex dummy;
|
|
|
|
|
dummy.nTime = parent->nTime;
|
|
|
|
|
dummy.phashBlock = parent->phashBlock;
|
|
|
|
|
uint256 dummySha;
|
2018-01-25 23:40:50 +00:00
|
|
|
uint32_t bits = parent->nBits;
|
2018-01-15 15:26:12 +00:00
|
|
|
|
|
|
|
|
for (int i = 0; i < COINBASE_MATURITY; ++i) {
|
|
|
|
|
dummy.nHeight = parent->nHeight + i;
|
|
|
|
|
dummy.nTime += 10;
|
2018-01-25 23:40:50 +00:00
|
|
|
dummy.nBits = bits;
|
2021-11-02 10:18:24 +01:00
|
|
|
Block block = bv.createBlock(&dummy, scriptPubKey);
|
2018-01-25 23:40:50 +00:00
|
|
|
bits = block.bits();
|
2018-01-15 15:26:12 +00:00
|
|
|
bv.addBlock(block, Validation::SaveGoodToDisk, 0);
|
|
|
|
|
coinbaseTxns.push_back(block.createOldBlock().vtx[0]);
|
|
|
|
|
dummySha = block.createHash();
|
|
|
|
|
dummy.phashBlock = &dummySha;
|
|
|
|
|
}
|
|
|
|
|
bv.waitValidationFinished();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create a new block with just given transactions, coinbase paying to
|
|
|
|
|
// scriptPubKey, and try to add it to the current chain.
|
2021-11-02 09:36:09 +01:00
|
|
|
MutableBlock createAndProcessBlock(const std::vector<CMutableTransaction>& txns,
|
2018-01-15 15:26:12 +00:00
|
|
|
const CScript& scriptPubKey) {
|
|
|
|
|
std::vector<CTransaction> tx;
|
|
|
|
|
for (auto t : txns) { tx.push_back(t); }
|
|
|
|
|
bv.waitValidationFinished(); // make sure that Tip really is Tip
|
2021-11-02 10:18:24 +01:00
|
|
|
Block block = bv.createBlock(bv.blockchain()->Tip(), scriptPubKey, tx);
|
2018-01-15 15:26:12 +00:00
|
|
|
auto future = bv.addBlock(block, Validation::SaveGoodToDisk, 0).start();
|
|
|
|
|
future.waitUntilFinished();
|
|
|
|
|
return block.createOldBlock();
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-22 13:08:35 +02:00
|
|
|
void ToMemPool(CMutableTransaction& tx, bool expectPass)
|
2018-01-15 15:26:12 +00:00
|
|
|
{
|
|
|
|
|
auto future = bv.addTransaction(Tx::fromOldTransaction(tx));
|
|
|
|
|
std::string result = future.get();
|
2018-10-22 13:08:35 +02:00
|
|
|
if (expectPass != result.empty()) {
|
|
|
|
|
logCritical() << "ToMemPool" << result;
|
|
|
|
|
throw std::runtime_error(expectPass ? result : "ToMemPool gave no error");
|
|
|
|
|
}
|
2018-01-15 15:26:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<CTransaction> coinbaseTxns; // For convenience, coinbase transactions
|
2022-07-06 22:12:33 +02:00
|
|
|
PrivateKey coinbaseKey; // private/public key needed to spend coinbase transactions
|
2018-01-15 15:26:12 +00:00
|
|
|
};
|
|
|
|
|
|
2015-03-03 09:59:32 -05:00
|
|
|
BOOST_FIXTURE_TEST_CASE(tx_mempool_block_doublespend, TestChain100Setup)
|
|
|
|
|
{
|
2018-01-15 15:26:12 +00:00
|
|
|
BOOST_CHECK(true);
|
2015-03-03 09:59:32 -05:00
|
|
|
// Make sure skipping validation of transctions that were
|
|
|
|
|
// validated going into the memory pool does not allow
|
|
|
|
|
// double-spends in blocks to pass validation when they should not.
|
|
|
|
|
|
2022-05-11 13:46:15 +02:00
|
|
|
CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.getPubKey()) << OP_CHECKSIG;
|
2015-03-03 09:59:32 -05:00
|
|
|
|
|
|
|
|
// Create a double-spend of mature coinbase txn:
|
|
|
|
|
std::vector<CMutableTransaction> spends;
|
|
|
|
|
spends.resize(2);
|
|
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
|
{
|
|
|
|
|
spends[i].vin.resize(1);
|
|
|
|
|
spends[i].vin[0].prevout.hash = coinbaseTxns[0].GetHash();
|
|
|
|
|
spends[i].vin[0].prevout.n = 0;
|
|
|
|
|
spends[i].vout.resize(1);
|
|
|
|
|
spends[i].vout[0].nValue = 11*CENT;
|
|
|
|
|
spends[i].vout[0].scriptPubKey = scriptPubKey;
|
|
|
|
|
|
|
|
|
|
// Sign:
|
|
|
|
|
std::vector<unsigned char> vchSig;
|
2017-07-28 16:56:37 +02:00
|
|
|
uint256 hash = SignatureHash(scriptPubKey, spends[i], 0, 50 * COIN, SIGHASH_ALL | SIGHASH_FORKID, SCRIPT_ENABLE_SIGHASH_FORKID);
|
2021-04-19 15:45:02 +02:00
|
|
|
BOOST_CHECK(coinbaseKey.signECDSA(hash, vchSig));
|
2017-07-28 16:56:37 +02:00
|
|
|
vchSig.push_back((unsigned char)SIGHASH_ALL + SIGHASH_FORKID);
|
2015-03-03 09:59:32 -05:00
|
|
|
spends[i].vin[0].scriptSig << vchSig;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-02 09:36:09 +01:00
|
|
|
MutableBlock block;
|
2018-01-15 15:26:12 +00:00
|
|
|
// block with both of those transactions should be rejected.
|
|
|
|
|
block = createAndProcessBlock(spends, scriptPubKey);
|
2021-11-02 09:28:35 +01:00
|
|
|
BOOST_CHECK(chainActive.Tip()->GetBlockHash() != block.createHash());
|
2015-03-03 09:59:32 -05:00
|
|
|
|
2018-01-15 15:26:12 +00:00
|
|
|
// Sanity test: first spend in mempool, second in block, that's OK:
|
2015-03-03 09:59:32 -05:00
|
|
|
std::vector<CMutableTransaction> oneSpend;
|
|
|
|
|
oneSpend.push_back(spends[0]);
|
2018-01-15 15:26:12 +00:00
|
|
|
bv.mp.clear();
|
2018-10-22 13:08:35 +02:00
|
|
|
ToMemPool(spends[1], /* expectPass = */ true);
|
2018-01-15 15:26:12 +00:00
|
|
|
block = createAndProcessBlock(oneSpend, scriptPubKey);
|
2021-11-02 09:28:35 +01:00
|
|
|
BOOST_CHECK(chainActive.Tip()->GetBlockHash() == block.createHash());
|
2015-03-03 09:59:32 -05:00
|
|
|
// spends[1] should have been removed from the mempool when the
|
|
|
|
|
// block with spends[0] is accepted:
|
2018-01-15 15:26:12 +00:00
|
|
|
BOOST_CHECK_EQUAL(bv.mp.size(), 0);
|
2015-03-03 09:59:32 -05:00
|
|
|
}
|
2017-08-24 10:07:20 +02:00
|
|
|
#endif
|
2015-03-03 09:59:32 -05:00
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|