Skip to content

Commit 0f3a253

Browse files
committed
validationcaches: Use size_t for sizes
...also move the 0-clamping logic to ApplyArgsManOptions, where it belongs.
1 parent 41c5201 commit 0f3a253

File tree

7 files changed

+20
-21
lines changed

7 files changed

+20
-21
lines changed

src/cuckoocache.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ class cache
328328
}
329329

330330
/** setup initializes the container to store no more than new_size
331-
* elements.
331+
* elements and no less than 2 elements.
332332
*
333333
* setup should only be called once.
334334
*

src/kernel/validation_cache_sizes.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77

88
#include <script/sigcache.h>
99

10-
#include <cstdint>
10+
#include <cstddef>
1111
#include <limits>
1212

1313
namespace kernel {
1414
struct ValidationCacheSizes {
15-
int64_t signature_cache_bytes{DEFAULT_MAX_SIG_CACHE_BYTES / 2};
16-
int64_t script_execution_cache_bytes{DEFAULT_MAX_SIG_CACHE_BYTES / 2};
15+
size_t signature_cache_bytes{DEFAULT_MAX_SIG_CACHE_BYTES / 2};
16+
size_t script_execution_cache_bytes{DEFAULT_MAX_SIG_CACHE_BYTES / 2};
1717
};
1818
}
1919

src/node/validation_cache_args.cpp

+10-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
#include <util/system.h>
1010

11+
#include <algorithm>
12+
#include <cstddef>
13+
#include <cstdint>
1114
#include <memory>
1215
#include <optional>
1316

@@ -17,11 +20,14 @@ namespace node {
1720
void ApplyArgsManOptions(const ArgsManager& argsman, ValidationCacheSizes& cache_sizes)
1821
{
1922
if (auto max_size = argsman.GetIntArg("-maxsigcachesize")) {
20-
// Multiply first, divide after to avoid integer truncation
21-
int64_t size_each = *max_size * (1 << 20) / 2;
23+
// 1. When supplied with a max_size of 0, both InitSignatureCache and
24+
// InitScriptExecutionCache create the minimum possible cache (2
25+
// elements). Therefore, we can use 0 as a floor here.
26+
// 2. Multiply first, divide after to avoid integer truncation.
27+
size_t clamped_size_each = std::max<int64_t>(*max_size, 0) * (1 << 20) / 2;
2228
cache_sizes = {
23-
.signature_cache_bytes = size_each,
24-
.script_execution_cache_bytes = size_each,
29+
.signature_cache_bytes = clamped_size_each,
30+
.script_execution_cache_bytes = clamped_size_each,
2531
};
2632
}
2733
}

src/script/sigcache.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,9 @@ static CSignatureCache signatureCache;
9393

9494
// To be called once in AppInitMain/BasicTestingSetup to initialize the
9595
// signatureCache.
96-
bool InitSignatureCache(int64_t max_size_bytes)
96+
bool InitSignatureCache(size_t max_size_bytes)
9797
{
98-
// nMaxCacheSize is unsigned. If -maxsigcachesize is set to zero,
99-
// setup_bytes creates the minimum possible cache (2 elements).
100-
size_t nMaxCacheSize = std::max<int64_t>(max_size_bytes, 0);
101-
102-
auto setup_results = signatureCache.setup_bytes(nMaxCacheSize);
98+
auto setup_results = signatureCache.setup_bytes(max_size_bytes);
10399
if (!setup_results) return false;
104100

105101
const auto [num_elems, approx_size_bytes] = *setup_results;

src/script/sigcache.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ class CachingTransactionSignatureChecker : public TransactionSignatureChecker
3232
bool VerifySchnorrSignature(Span<const unsigned char> sig, const XOnlyPubKey& pubkey, const uint256& sighash) const override;
3333
};
3434

35-
[[nodiscard]] bool InitSignatureCache(int64_t max_size_bytes);
35+
[[nodiscard]] bool InitSignatureCache(size_t max_size_bytes);
3636

3737
#endif // BITCOIN_SCRIPT_SIGCACHE_H

src/validation.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -1656,7 +1656,7 @@ bool CScriptCheck::operator()() {
16561656
static CuckooCache::cache<uint256, SignatureCacheHasher> g_scriptExecutionCache;
16571657
static CSHA256 g_scriptExecutionCacheHasher;
16581658

1659-
bool InitScriptExecutionCache(int64_t max_size_bytes)
1659+
bool InitScriptExecutionCache(size_t max_size_bytes)
16601660
{
16611661
// Setup the salted hasher
16621662
uint256 nonce = GetRandHash();
@@ -1665,11 +1665,8 @@ bool InitScriptExecutionCache(int64_t max_size_bytes)
16651665
// just write our 32-byte entropy twice to fill the 64 bytes.
16661666
g_scriptExecutionCacheHasher.Write(nonce.begin(), 32);
16671667
g_scriptExecutionCacheHasher.Write(nonce.begin(), 32);
1668-
// nMaxCacheSize is unsigned. If -maxsigcachesize is set to zero,
1669-
// setup_bytes creates the minimum possible cache (2 elements).
1670-
size_t nMaxCacheSize = std::max<int64_t>(max_size_bytes, 0);
16711668

1672-
auto setup_results = g_scriptExecutionCache.setup_bytes(nMaxCacheSize);
1669+
auto setup_results = g_scriptExecutionCache.setup_bytes(max_size_bytes);
16731670
if (!setup_results) return false;
16741671

16751672
const auto [num_elems, approx_size_bytes] = *setup_results;

src/validation.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ class CScriptCheck
323323
};
324324

325325
/** Initializes the script-execution cache */
326-
[[nodiscard]] bool InitScriptExecutionCache(int64_t max_size_bytes);
326+
[[nodiscard]] bool InitScriptExecutionCache(size_t max_size_bytes);
327327

328328
/** Functions for validating blocks and updating the block tree */
329329

0 commit comments

Comments
 (0)