0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-02 09:46:52 -05:00

validationcaches: Use size_t for sizes

...also move the 0-clamping logic to ApplyArgsManOptions, where it
   belongs.
This commit is contained in:
Carl Dong 2022-07-01 00:08:14 -04:00
parent 41c5201a90
commit 0f3a2532c3
7 changed files with 20 additions and 21 deletions

View file

@ -328,7 +328,7 @@ public:
}
/** setup initializes the container to store no more than new_size
* elements.
* elements and no less than 2 elements.
*
* setup should only be called once.
*

View file

@ -7,13 +7,13 @@
#include <script/sigcache.h>
#include <cstdint>
#include <cstddef>
#include <limits>
namespace kernel {
struct ValidationCacheSizes {
int64_t signature_cache_bytes{DEFAULT_MAX_SIG_CACHE_BYTES / 2};
int64_t script_execution_cache_bytes{DEFAULT_MAX_SIG_CACHE_BYTES / 2};
size_t signature_cache_bytes{DEFAULT_MAX_SIG_CACHE_BYTES / 2};
size_t script_execution_cache_bytes{DEFAULT_MAX_SIG_CACHE_BYTES / 2};
};
}

View file

@ -8,6 +8,9 @@
#include <util/system.h>
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <optional>
@ -17,11 +20,14 @@ namespace node {
void ApplyArgsManOptions(const ArgsManager& argsman, ValidationCacheSizes& cache_sizes)
{
if (auto max_size = argsman.GetIntArg("-maxsigcachesize")) {
// Multiply first, divide after to avoid integer truncation
int64_t size_each = *max_size * (1 << 20) / 2;
// 1. When supplied with a max_size of 0, both InitSignatureCache and
// InitScriptExecutionCache create the minimum possible cache (2
// elements). Therefore, we can use 0 as a floor here.
// 2. Multiply first, divide after to avoid integer truncation.
size_t clamped_size_each = std::max<int64_t>(*max_size, 0) * (1 << 20) / 2;
cache_sizes = {
.signature_cache_bytes = size_each,
.script_execution_cache_bytes = size_each,
.signature_cache_bytes = clamped_size_each,
.script_execution_cache_bytes = clamped_size_each,
};
}
}

View file

@ -93,13 +93,9 @@ static CSignatureCache signatureCache;
// To be called once in AppInitMain/BasicTestingSetup to initialize the
// signatureCache.
bool InitSignatureCache(int64_t max_size_bytes)
bool InitSignatureCache(size_t max_size_bytes)
{
// nMaxCacheSize is unsigned. If -maxsigcachesize is set to zero,
// setup_bytes creates the minimum possible cache (2 elements).
size_t nMaxCacheSize = std::max<int64_t>(max_size_bytes, 0);
auto setup_results = signatureCache.setup_bytes(nMaxCacheSize);
auto setup_results = signatureCache.setup_bytes(max_size_bytes);
if (!setup_results) return false;
const auto [num_elems, approx_size_bytes] = *setup_results;

View file

@ -32,6 +32,6 @@ public:
bool VerifySchnorrSignature(Span<const unsigned char> sig, const XOnlyPubKey& pubkey, const uint256& sighash) const override;
};
[[nodiscard]] bool InitSignatureCache(int64_t max_size_bytes);
[[nodiscard]] bool InitSignatureCache(size_t max_size_bytes);
#endif // BITCOIN_SCRIPT_SIGCACHE_H

View file

@ -1656,7 +1656,7 @@ bool CScriptCheck::operator()() {
static CuckooCache::cache<uint256, SignatureCacheHasher> g_scriptExecutionCache;
static CSHA256 g_scriptExecutionCacheHasher;
bool InitScriptExecutionCache(int64_t max_size_bytes)
bool InitScriptExecutionCache(size_t max_size_bytes)
{
// Setup the salted hasher
uint256 nonce = GetRandHash();
@ -1665,11 +1665,8 @@ bool InitScriptExecutionCache(int64_t max_size_bytes)
// just write our 32-byte entropy twice to fill the 64 bytes.
g_scriptExecutionCacheHasher.Write(nonce.begin(), 32);
g_scriptExecutionCacheHasher.Write(nonce.begin(), 32);
// nMaxCacheSize is unsigned. If -maxsigcachesize is set to zero,
// setup_bytes creates the minimum possible cache (2 elements).
size_t nMaxCacheSize = std::max<int64_t>(max_size_bytes, 0);
auto setup_results = g_scriptExecutionCache.setup_bytes(nMaxCacheSize);
auto setup_results = g_scriptExecutionCache.setup_bytes(max_size_bytes);
if (!setup_results) return false;
const auto [num_elems, approx_size_bytes] = *setup_results;

View file

@ -323,7 +323,7 @@ public:
};
/** Initializes the script-execution cache */
[[nodiscard]] bool InitScriptExecutionCache(int64_t max_size_bytes);
[[nodiscard]] bool InitScriptExecutionCache(size_t max_size_bytes);
/** Functions for validating blocks and updating the block tree */