Skip to content

Commit ab14d1d

Browse files
TheCharlatanajtowns
andcommitted
validation: Don't error if maxsigcachesize exceeds uint32::max
Instead clamp it to uint32::max if it exceeds it. Co-authored-by: Anthony Towns <[email protected]>
1 parent d2c8d16 commit ab14d1d

File tree

5 files changed

+11
-24
lines changed

5 files changed

+11
-24
lines changed

src/cuckoocache.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include <cstring>
1515
#include <limits>
1616
#include <memory>
17-
#include <optional>
1817
#include <utility>
1918
#include <vector>
2019

@@ -360,16 +359,15 @@ class cache
360359
* structure
361360
* @returns A pair of the maximum number of elements storable (see setup()
362361
* documentation for more detail) and the approximate total size of these
363-
* elements in bytes or std::nullopt if the size requested is too large.
362+
* elements in bytes.
364363
*/
365-
std::optional<std::pair<uint32_t, size_t>> setup_bytes(size_t bytes)
364+
std::pair<uint32_t, size_t> setup_bytes(size_t bytes)
366365
{
367-
size_t requested_num_elems = bytes / sizeof(Element);
368-
if (std::numeric_limits<uint32_t>::max() < requested_num_elems) {
369-
return std::nullopt;
370-
}
366+
uint32_t requested_num_elems(std::min<size_t>(
367+
bytes / sizeof(Element),
368+
std::numeric_limits<uint32_t>::max()));
371369

372-
auto num_elems = setup(bytes/sizeof(Element));
370+
auto num_elems = setup(requested_num_elems);
373371

374372
size_t approx_size_bytes = num_elems * sizeof(Element);
375373
return std::make_pair(num_elems, approx_size_bytes);

src/init.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,11 +1156,8 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
11561156

11571157
ValidationCacheSizes validation_cache_sizes{};
11581158
ApplyArgsManOptions(args, validation_cache_sizes);
1159-
if (!InitSignatureCache(validation_cache_sizes.signature_cache_bytes)
1160-
|| !InitScriptExecutionCache(validation_cache_sizes.script_execution_cache_bytes))
1161-
{
1162-
return InitError(strprintf(_("Unable to allocate memory for -maxsigcachesize: '%s' MiB"), args.GetIntArg("-maxsigcachesize", DEFAULT_MAX_SIG_CACHE_BYTES >> 20)));
1163-
}
1159+
(void)InitSignatureCache(validation_cache_sizes.signature_cache_bytes);
1160+
(void)InitScriptExecutionCache(validation_cache_sizes.script_execution_cache_bytes);
11641161

11651162
assert(!node.scheduler);
11661163
node.scheduler = std::make_unique<CScheduler>();

src/script/sigcache.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
#include <algorithm>
1717
#include <mutex>
18-
#include <optional>
1918
#include <shared_mutex>
2019
#include <vector>
2120

@@ -77,7 +76,7 @@ class CSignatureCache
7776
std::unique_lock<std::shared_mutex> lock(cs_sigcache);
7877
setValid.insert(entry);
7978
}
80-
std::optional<std::pair<uint32_t, size_t>> setup_bytes(size_t n)
79+
std::pair<uint32_t, size_t> setup_bytes(size_t n)
8180
{
8281
return setValid.setup_bytes(n);
8382
}
@@ -96,10 +95,7 @@ static CSignatureCache signatureCache;
9695
// signatureCache.
9796
bool InitSignatureCache(size_t max_size_bytes)
9897
{
99-
auto setup_results = signatureCache.setup_bytes(max_size_bytes);
100-
if (!setup_results) return false;
101-
102-
const auto [num_elems, approx_size_bytes] = *setup_results;
98+
const auto [num_elems, approx_size_bytes] = signatureCache.setup_bytes(max_size_bytes);
10399
LogPrintf("Using %zu MiB out of %zu MiB requested for signature cache, able to store %zu elements\n",
104100
approx_size_bytes >> 20, max_size_bytes >> 20, num_elems);
105101
return true;

src/script/sigcache.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include <span.h>
1111
#include <util/hasher.h>
1212

13-
#include <optional>
1413
#include <vector>
1514

1615
// DoS prevention: limit cache size to 32MiB (over 1000000 entries on 64-bit

src/validation.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2100,10 +2100,7 @@ bool InitScriptExecutionCache(size_t max_size_bytes)
21002100
g_scriptExecutionCacheHasher.Write(nonce.begin(), 32);
21012101
g_scriptExecutionCacheHasher.Write(nonce.begin(), 32);
21022102

2103-
auto setup_results = g_scriptExecutionCache.setup_bytes(max_size_bytes);
2104-
if (!setup_results) return false;
2105-
2106-
const auto [num_elems, approx_size_bytes] = *setup_results;
2103+
const auto [num_elems, approx_size_bytes] = g_scriptExecutionCache.setup_bytes(max_size_bytes);
21072104
LogPrintf("Using %zu MiB out of %zu MiB requested for script execution cache, able to store %zu elements\n",
21082105
approx_size_bytes >> 20, max_size_bytes >> 20, num_elems);
21092106
return true;

0 commit comments

Comments
 (0)