Skip to content

Commit 08e9c57

Browse files
committed
Merge pull request #6077
517e6dd Unit test doublespends in new blocks (Gavin Andresen) 17b1142 Cache transaction validation successes (Pieter Wuille)
2 parents ca37e0f + 517e6dd commit 08e9c57

File tree

8 files changed

+330
-6
lines changed

8 files changed

+330
-6
lines changed

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ BITCOIN_TESTS =\
7373
test/test_bitcoin.h \
7474
test/timedata_tests.cpp \
7575
test/transaction_tests.cpp \
76+
test/txvalidationcache_tests.cpp \
7677
test/uint256_tests.cpp \
7778
test/univalue_tests.cpp \
7879
test/util_tests.cpp

src/main.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,9 @@ int GetSpendHeight(const CCoinsViewCache& inputs)
12771277
return pindexPrev->nHeight + 1;
12781278
}
12791279

1280+
static mrumap<uint256, unsigned int> cacheCheck(2 * MAX_BLOCK_SIZE / CTransaction().GetSerializeSize(SER_NETWORK, PROTOCOL_VERSION));
1281+
static boost::mutex cs_cacheCheck;
1282+
12801283
namespace Consensus {
12811284
bool CheckTxInputs(const CTransaction& tx, CValidationState& state, const CCoinsViewCache& inputs, int nSpendHeight)
12821285
{
@@ -1331,6 +1334,17 @@ bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsVi
13311334
{
13321335
if (!tx.IsCoinBase())
13331336
{
1337+
if (fScriptChecks) {
1338+
boost::unique_lock<boost::mutex> lock(cs_cacheCheck);
1339+
mrumap<uint256, unsigned int>::const_iterator iter = cacheCheck.find(tx.GetHash());
1340+
if (iter != cacheCheck.end()) {
1341+
// The following test relies on the fact that all script validation flags are softforks (i.e. an extra bit set cannot cause a false result to become true).
1342+
if ((iter->second & flags) == flags) {
1343+
return true;
1344+
}
1345+
}
1346+
}
1347+
13341348
if (!Consensus::CheckTxInputs(tx, state, inputs, GetSpendHeight(inputs)))
13351349
return false;
13361350

@@ -1381,6 +1395,11 @@ bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsVi
13811395
}
13821396
}
13831397

1398+
if (cacheStore && fScriptChecks && pvChecks == NULL) {
1399+
boost::unique_lock<boost::mutex> lock(cs_cacheCheck);
1400+
cacheCheck.insert(tx.GetHash(), flags);
1401+
}
1402+
13841403
return true;
13851404
}
13861405

@@ -2101,6 +2120,13 @@ bool static ConnectTip(CValidationState &state, CBlockIndex *pindexNew, CBlock *
21012120
BOOST_FOREACH(const CTransaction &tx, pblock->vtx) {
21022121
SyncWithWallets(tx, pblock);
21032122
}
2123+
// Erase block's transactions from the validation cache
2124+
{
2125+
boost::unique_lock<boost::mutex> lock(cs_cacheCheck);
2126+
BOOST_FOREACH(const CTransaction &tx, pblock->vtx) {
2127+
cacheCheck.erase(tx.GetHash());
2128+
}
2129+
}
21042130

21052131
int64_t nTime6 = GetTimeMicros(); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1;
21062132
LogPrint("bench", " - Connect postprocess: %.2fms [%.2fs]\n", (nTime6 - nTime5) * 0.001, nTimePostConnect * 0.000001);

src/mruset.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
#include <vector>
1010
#include <utility>
1111

12+
#include <boost/multi_index_container.hpp>
13+
#include <boost/multi_index/ordered_index.hpp>
14+
#include <boost/multi_index/sequenced_index.hpp>
15+
1216
/** STL-like set container that only keeps the most recent N elements. */
1317
template <typename T>
1418
class mruset
@@ -62,4 +66,60 @@ class mruset
6266
size_type max_size() const { return nMaxSize; }
6367
};
6468

69+
/** STL-like map container that only keeps the most recent N elements. */
70+
template <typename K, typename V>
71+
class mrumap
72+
{
73+
private:
74+
struct key_extractor {
75+
typedef K result_type;
76+
const result_type& operator()(const std::pair<K, V>& e) const { return e.first; }
77+
result_type& operator()(std::pair<K, V>* e) const { return e->first; }
78+
};
79+
80+
typedef boost::multi_index_container<
81+
std::pair<K, V>,
82+
boost::multi_index::indexed_by<
83+
boost::multi_index::sequenced<>,
84+
boost::multi_index::ordered_unique<key_extractor>
85+
>
86+
> map_type;
87+
88+
public:
89+
typedef K key_type;
90+
typedef std::pair<K, V> value_type;
91+
typedef typename map_type::iterator iterator;
92+
typedef typename map_type::const_iterator const_iterator;
93+
typedef typename map_type::size_type size_type;
94+
95+
protected:
96+
map_type m_;
97+
size_type max_size_;
98+
99+
public:
100+
mrumap(size_type max_size_in = 1) { clear(max_size_in); }
101+
iterator begin() { return m_.begin(); }
102+
iterator end() { return m_.end(); }
103+
const_iterator begin() const { return m_.begin(); }
104+
const_iterator end() const { return m_.end(); }
105+
size_type size() const { return m_.size(); }
106+
bool empty() const { return m_.empty(); }
107+
iterator find(const key_type& key) { return m_.template project<0>(boost::get<1>(m_).find(key)); }
108+
const_iterator find(const key_type& key) const { return m_.template project<0>(boost::get<1>(m_).find(key)); }
109+
size_type count(const key_type& key) const { return boost::get<1>(m_).count(key); }
110+
void clear(size_type max_size_in) { m_.clear(); max_size_ = max_size_in; }
111+
std::pair<iterator, bool> insert(const K& key, const V& value)
112+
{
113+
std::pair<K, V> elem(key, value);
114+
std::pair<iterator, bool> p = m_.push_front(elem);
115+
if (p.second && m_.size() > max_size_) {
116+
m_.pop_back();
117+
}
118+
return p;
119+
}
120+
void erase(iterator it) { m_.erase(it); }
121+
void erase(const key_type& k) { boost::get<1>(m_).erase(k); }
122+
size_type max_size() const { return max_size_; }
123+
};
124+
65125
#endif // BITCOIN_MRUSET_H

src/test/README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,16 @@ uint256_tests.cpp.
1818

1919
For further reading, I found the following website to be helpful in
2020
explaining how the boost unit test framework works:
21-
[http://www.alittlemadness.com/2009/03/31/c-unit-testing-with-boosttest/](http://www.alittlemadness.com/2009/03/31/c-unit-testing-with-boosttest/).
21+
[http://www.alittlemadness.com/2009/03/31/c-unit-testing-with-boosttest/](http://www.alittlemadness.com/2009/03/31/c-unit-testing-with-boosttest/).
22+
23+
test_bitcoin has some built-in command-line arguments; for
24+
example, to run just the getarg_tests verbosely:
25+
26+
test_bitcoin --log_level=all --run_test=getarg_tests
27+
28+
... or to run just the doubledash test:
29+
30+
test_bitcoin --run_test=getarg_tests/doubledash
31+
32+
Run test_bitcoin --help for the full list.
33+

src/test/mruset_tests.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,68 @@ BOOST_AUTO_TEST_CASE(mruset_test)
7878
}
7979
}
8080

81+
BOOST_AUTO_TEST_CASE(mrumap_test)
82+
{
83+
// The mrumap being tested.
84+
mrumap<int, char> mru(5000);
85+
86+
// Run the test 10 times.
87+
for (int test = 0; test < 10; test++) {
88+
// Reset mru.
89+
mru.clear(5000);
90+
91+
// A deque + set to simulate the mruset.
92+
std::deque<int> rep;
93+
std::map<int, char> all;
94+
95+
// Insert 10000 random integers below 15000.
96+
for (int j=0; j<10000; j++) {
97+
int add = GetRandInt(15000);
98+
char val = (char)GetRandInt(256);
99+
mru.insert(add, val);
100+
101+
// Add the number to rep/all as well.
102+
if (all.count(add) == 0) {
103+
all.insert(std::make_pair<int, char>(add, val));
104+
rep.push_back(add);
105+
if (all.size() == 5001) {
106+
all.erase(rep.front());
107+
rep.pop_front();
108+
}
109+
}
110+
111+
if (GetRandInt(5) == 0) {
112+
// With 20% chance: remove an item
113+
int pos = GetRandInt(rep.size());
114+
std::deque<int>::iterator it = rep.begin();
115+
while (pos--) { it++; }
116+
int delval = *it;
117+
mru.erase(delval);
118+
all.erase(delval);
119+
rep.erase(it);
120+
}
121+
122+
// Do a full comparison between mru and the simulated mru every 1000 and every 5001 elements.
123+
if (j % 1000 == 0 || j % 5001 == 0) {
124+
// Check that all elements that should be in there, are in there.
125+
BOOST_FOREACH(int x, rep) {
126+
BOOST_CHECK(mru.count(x));
127+
BOOST_CHECK(mru.find(x)->second == all[x]);
128+
}
129+
130+
// Check that all elements that are in there, should be in there.
131+
for (mrumap<int, char>::iterator it = mru.begin(); it != mru.end(); it++) {
132+
BOOST_CHECK(all.count(it->first));
133+
BOOST_CHECK(all[it->first] == it->second);
134+
}
135+
136+
for (int t = 0; t < 10; t++) {
137+
int r = GetRandInt(15000);
138+
BOOST_CHECK(all.count(r) == mru.count(r));
139+
}
140+
}
141+
}
142+
}
143+
}
144+
81145
BOOST_AUTO_TEST_SUITE_END()

src/test/test_bitcoin.cpp

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
#include "test_bitcoin.h"
88

99
#include "chainparams.h"
10+
#include "consensus/consensus.h"
11+
#include "consensus/validation.h"
1012
#include "key.h"
1113
#include "main.h"
14+
#include "miner.h"
15+
#include "pubkey.h"
1216
#include "random.h"
1317
#include "txdb.h"
1418
#include "ui_interface.h"
@@ -28,20 +32,22 @@ CWallet* pwalletMain;
2832
extern bool fPrintToConsole;
2933
extern void noui_connect();
3034

31-
BasicTestingSetup::BasicTestingSetup()
35+
BasicTestingSetup::BasicTestingSetup(CBaseChainParams::Network network)
3236
{
3337
ECC_Start();
3438
SetupEnvironment();
3539
fPrintToDebugLog = false; // don't want to write to debug.log file
3640
fCheckBlockIndex = true;
37-
SelectParams(CBaseChainParams::MAIN);
41+
SelectParams(network);
42+
noui_connect();
3843
}
44+
3945
BasicTestingSetup::~BasicTestingSetup()
4046
{
4147
ECC_Stop();
4248
}
4349

44-
TestingSetup::TestingSetup()
50+
TestingSetup::TestingSetup(CBaseChainParams::Network network) : BasicTestingSetup(network)
4551
{
4652
#ifdef ENABLE_WALLET
4753
bitdb.MakeMock();
@@ -87,6 +93,51 @@ TestingSetup::~TestingSetup()
8793
boost::filesystem::remove_all(pathTemp);
8894
}
8995

96+
TestChain100Setup::TestChain100Setup() : TestingSetup(CBaseChainParams::REGTEST)
97+
{
98+
// Generate a 100-block chain:
99+
coinbaseKey.MakeNewKey(true);
100+
CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
101+
for (int i = 0; i < COINBASE_MATURITY; i++)
102+
{
103+
std::vector<CMutableTransaction> noTxns;
104+
CBlock b = CreateAndProcessBlock(noTxns, scriptPubKey);
105+
coinbaseTxns.push_back(b.vtx[0]);
106+
}
107+
}
108+
109+
//
110+
// Create a new block with just given transactions, coinbase paying to
111+
// scriptPubKey, and try to add it to the current chain.
112+
//
113+
CBlock
114+
TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey)
115+
{
116+
CBlockTemplate *pblocktemplate = CreateNewBlock(scriptPubKey);
117+
CBlock& block = pblocktemplate->block;
118+
119+
// Replace mempool-selected txns with just coinbase plus passed-in txns:
120+
block.vtx.resize(1);
121+
BOOST_FOREACH(const CMutableTransaction& tx, txns)
122+
block.vtx.push_back(tx);
123+
// IncrementExtraNonce creates a valid coinbase and merkleRoot
124+
unsigned int extraNonce = 0;
125+
IncrementExtraNonce(&block, chainActive.Tip(), extraNonce);
126+
127+
while (!CheckProofOfWork(block.GetHash(), block.nBits, Params(CBaseChainParams::REGTEST).GetConsensus())) ++block.nNonce;
128+
129+
CValidationState state;
130+
ProcessNewBlock(state, NULL, &block, true, NULL);
131+
132+
CBlock result = block;
133+
delete pblocktemplate;
134+
return result;
135+
}
136+
137+
TestChain100Setup::~TestChain100Setup()
138+
{
139+
}
140+
90141
void Shutdown(void* parg)
91142
{
92143
exit(0);

src/test/test_bitcoin.h

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef BITCOIN_TEST_TEST_BITCOIN_H
22
#define BITCOIN_TEST_TEST_BITCOIN_H
33

4+
#include "chainparamsbase.h"
5+
#include "key.h"
46
#include "txdb.h"
57

68
#include <boost/filesystem.hpp>
@@ -10,7 +12,7 @@
1012
* This just configures logging and chain parameters.
1113
*/
1214
struct BasicTestingSetup {
13-
BasicTestingSetup();
15+
BasicTestingSetup(CBaseChainParams::Network network = CBaseChainParams::MAIN);
1416
~BasicTestingSetup();
1517
};
1618

@@ -23,8 +25,30 @@ struct TestingSetup: public BasicTestingSetup {
2325
boost::filesystem::path pathTemp;
2426
boost::thread_group threadGroup;
2527

26-
TestingSetup();
28+
TestingSetup(CBaseChainParams::Network network = CBaseChainParams::MAIN);
2729
~TestingSetup();
2830
};
2931

32+
class CBlock;
33+
struct CMutableTransaction;
34+
class CScript;
35+
36+
//
37+
// Testing fixture that pre-creates a
38+
// 100-block REGTEST-mode block chain
39+
//
40+
struct TestChain100Setup : public TestingSetup {
41+
TestChain100Setup();
42+
43+
// Create a new block with just given transactions, coinbase paying to
44+
// scriptPubKey, and try to add it to the current chain.
45+
CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
46+
const CScript& scriptPubKey);
47+
48+
~TestChain100Setup();
49+
50+
std::vector<CTransaction> coinbaseTxns; // For convenience, coinbase transactions
51+
CKey coinbaseKey; // private/public key needed to spend coinbase transactions
52+
};
53+
3054
#endif

0 commit comments

Comments
 (0)