Skip to content

Commit f9c8807

Browse files
committed
Deduplicate SignatureCacheHasher
This moves the SignatureCacheHasher to the sigcache header, out of the anonymous namespace, so that the tests can import it.
1 parent 471ed00 commit f9c8807

File tree

3 files changed

+27
-41
lines changed

3 files changed

+27
-41
lines changed

src/script/sigcache.cpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,6 @@
1515
#include <boost/thread.hpp>
1616

1717
namespace {
18-
19-
/**
20-
* We're hashing a nonce into the entries themselves, so we don't need extra
21-
* blinding in the set hash computation.
22-
*
23-
* This may exhibit platform endian dependent behavior but because these are
24-
* nonced hashes (random) and this state is only ever used locally it is safe.
25-
* All that matters is local consistency.
26-
*/
27-
class SignatureCacheHasher
28-
{
29-
public:
30-
template <uint8_t hash_select>
31-
uint32_t operator()(const uint256& key) const
32-
{
33-
static_assert(hash_select <8, "SignatureCacheHasher only has 8 hashes available.");
34-
uint32_t u;
35-
std::memcpy(&u, key.begin()+4*hash_select, 4);
36-
return u;
37-
}
38-
};
39-
4018
/**
4119
* Valid signature cache, to avoid doing expensive ECDSA signature checking
4220
* twice for every transaction (once when accepted into memory pool, and

src/script/sigcache.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,27 @@ static const int64_t MAX_MAX_SIG_CACHE_SIZE = 16384;
1919

2020
class CPubKey;
2121

22+
/**
23+
* We're hashing a nonce into the entries themselves, so we don't need extra
24+
* blinding in the set hash computation.
25+
*
26+
* This may exhibit platform endian dependent behavior but because these are
27+
* nonced hashes (random) and this state is only ever used locally it is safe.
28+
* All that matters is local consistency.
29+
*/
30+
class SignatureCacheHasher
31+
{
32+
public:
33+
template <uint8_t hash_select>
34+
uint32_t operator()(const uint256& key) const
35+
{
36+
static_assert(hash_select <8, "SignatureCacheHasher only has 8 hashes available.");
37+
uint32_t u;
38+
std::memcpy(&u, key.begin()+4*hash_select, 4);
39+
return u;
40+
}
41+
};
42+
2243
class CachingTransactionSignatureChecker : public TransactionSignatureChecker
2344
{
2445
private:

src/test/cuckoocache_tests.cpp

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44
#include <boost/test/unit_test.hpp>
55
#include "cuckoocache.h"
6+
#include "script/sigcache.h"
67
#include "test/test_bitcoin.h"
78
#include "random.h"
89
#include <thread>
@@ -36,20 +37,6 @@ void insecure_GetRandHash(uint256& t)
3637
*(ptr++) = insecure_rand.rand32();
3738
}
3839

39-
/** Definition copied from /src/script/sigcache.cpp
40-
*/
41-
class uint256Hasher
42-
{
43-
public:
44-
template <uint8_t hash_select>
45-
uint32_t operator()(const uint256& key) const
46-
{
47-
static_assert(hash_select <8, "SignatureCacheHasher only has 8 hashes available.");
48-
uint32_t u;
49-
std::memcpy(&u, key.begin() + 4 * hash_select, 4);
50-
return u;
51-
}
52-
};
5340

5441

5542
/* Test that no values not inserted into the cache are read out of it.
@@ -59,7 +46,7 @@ class uint256Hasher
5946
BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)
6047
{
6148
insecure_rand = FastRandomContext(true);
62-
CuckooCache::cache<uint256, uint256Hasher> cc{};
49+
CuckooCache::cache<uint256, SignatureCacheHasher> cc{};
6350
size_t megabytes = 4;
6451
cc.setup_bytes(megabytes << 20);
6552
uint256 v;
@@ -138,7 +125,7 @@ BOOST_AUTO_TEST_CASE(cuckoocache_hit_rate_ok)
138125
double HitRateThresh = 0.98;
139126
size_t megabytes = 4;
140127
for (double load = 0.1; load < 2; load *= 2) {
141-
double hits = test_cache<CuckooCache::cache<uint256, uint256Hasher>>(megabytes, load);
128+
double hits = test_cache<CuckooCache::cache<uint256, SignatureCacheHasher>>(megabytes, load);
142129
BOOST_CHECK(normalize_hit_rate(hits, load) > HitRateThresh);
143130
}
144131
}
@@ -206,7 +193,7 @@ void test_cache_erase(size_t megabytes)
206193
BOOST_AUTO_TEST_CASE(cuckoocache_erase_ok)
207194
{
208195
size_t megabytes = 4;
209-
test_cache_erase<CuckooCache::cache<uint256, uint256Hasher>>(megabytes);
196+
test_cache_erase<CuckooCache::cache<uint256, SignatureCacheHasher>>(megabytes);
210197
}
211198

212199
template <typename Cache>
@@ -293,7 +280,7 @@ void test_cache_erase_parallel(size_t megabytes)
293280
BOOST_AUTO_TEST_CASE(cuckoocache_erase_parallel_ok)
294281
{
295282
size_t megabytes = 4;
296-
test_cache_erase_parallel<CuckooCache::cache<uint256, uint256Hasher>>(megabytes);
283+
test_cache_erase_parallel<CuckooCache::cache<uint256, SignatureCacheHasher>>(megabytes);
297284
}
298285

299286

@@ -389,7 +376,7 @@ void test_cache_generations()
389376
}
390377
BOOST_AUTO_TEST_CASE(cuckoocache_generations)
391378
{
392-
test_cache_generations<CuckooCache::cache<uint256, uint256Hasher>>();
379+
test_cache_generations<CuckooCache::cache<uint256, SignatureCacheHasher>>();
393380
}
394381

395382
BOOST_AUTO_TEST_SUITE_END();

0 commit comments

Comments
 (0)