diff options
author | Carl Dong <contact@carldong.me> | 2022-07-01 00:08:14 -0400 |
---|---|---|
committer | Carl Dong <contact@carldong.me> | 2022-08-03 12:03:28 -0400 |
commit | 0f3a2532c38074dd9789d1c4c667db6ca46ff0ab (patch) | |
tree | e45ce50c4efda6ac38aa3dfeeb18dae4e2ecc8d0 | |
parent | 41c5201a90bbc2893333e334e8945759ef24e7dd (diff) | |
download | bitcoin-0f3a2532c38074dd9789d1c4c667db6ca46ff0ab.tar.xz |
validationcaches: Use size_t for sizes
...also move the 0-clamping logic to ApplyArgsManOptions, where it
belongs.
-rw-r--r-- | src/cuckoocache.h | 2 | ||||
-rw-r--r-- | src/kernel/validation_cache_sizes.h | 6 | ||||
-rw-r--r-- | src/node/validation_cache_args.cpp | 14 | ||||
-rw-r--r-- | src/script/sigcache.cpp | 8 | ||||
-rw-r--r-- | src/script/sigcache.h | 2 | ||||
-rw-r--r-- | src/validation.cpp | 7 | ||||
-rw-r--r-- | src/validation.h | 2 |
7 files changed, 20 insertions, 21 deletions
diff --git a/src/cuckoocache.h b/src/cuckoocache.h index b5da87136d..61f553806e 100644 --- a/src/cuckoocache.h +++ b/src/cuckoocache.h @@ -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. * diff --git a/src/kernel/validation_cache_sizes.h b/src/kernel/validation_cache_sizes.h index cf92cdbd64..72e4d1a52c 100644 --- a/src/kernel/validation_cache_sizes.h +++ b/src/kernel/validation_cache_sizes.h @@ -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}; }; } diff --git a/src/node/validation_cache_args.cpp b/src/node/validation_cache_args.cpp index ed183c24fe..5ea0a8ca0a 100644 --- a/src/node/validation_cache_args.cpp +++ b/src/node/validation_cache_args.cpp @@ -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, }; } } diff --git a/src/script/sigcache.cpp b/src/script/sigcache.cpp index 8e6971d3f3..e507ec7528 100644 --- a/src/script/sigcache.cpp +++ b/src/script/sigcache.cpp @@ -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; diff --git a/src/script/sigcache.h b/src/script/sigcache.h index 4c1cad38a7..290955090d 100644 --- a/src/script/sigcache.h +++ b/src/script/sigcache.h @@ -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 diff --git a/src/validation.cpp b/src/validation.cpp index 7a03adf558..c924bb3c34 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -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; diff --git a/src/validation.h b/src/validation.h index 6e072a49d9..ec9043ad7a 100644 --- a/src/validation.h +++ b/src/validation.h @@ -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 */ |