Skip to content

Commit 17b1142

Browse files
committed
Cache transaction validation successes
1 parent ca37e0f commit 17b1142

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed

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/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()

0 commit comments

Comments
 (0)