Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/coins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,11 +352,13 @@ const Coin& AccessByTxid(const CCoinsViewCache& view, const uint256& txid)
return coinEmpty;
}

bool CCoinsViewErrorCatcher::GetCoin(const COutPoint &outpoint, Coin &coin) const {
template <typename Func>
static bool ExecuteBackedWrapper(Func func, const std::vector<std::function<void()>>& err_callbacks)
{
try {
return CCoinsViewBacked::GetCoin(outpoint, coin);
return func();
} catch(const std::runtime_error& e) {
for (const auto& f : m_err_callbacks) {
for (const auto& f : err_callbacks) {
f();
}
LogPrintf("Error reading from database: %s\n", e.what());
Expand All @@ -367,3 +369,11 @@ bool CCoinsViewErrorCatcher::GetCoin(const COutPoint &outpoint, Coin &coin) cons
std::abort();
}
}

bool CCoinsViewErrorCatcher::GetCoin(const COutPoint &outpoint, Coin &coin) const {
return ExecuteBackedWrapper([&]() { return CCoinsViewBacked::GetCoin(outpoint, coin); }, m_err_callbacks);
}

bool CCoinsViewErrorCatcher::HaveCoin(const COutPoint &outpoint) const {
return ExecuteBackedWrapper([&]() { return CCoinsViewBacked::HaveCoin(outpoint); }, m_err_callbacks);
}
1 change: 1 addition & 0 deletions src/coins.h
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ class CCoinsViewErrorCatcher final : public CCoinsViewBacked
}

bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
bool HaveCoin(const COutPoint &outpoint) const override;

private:
/** A list of callbacks to execute upon leveldb read error. */
Expand Down
9 changes: 9 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1549,6 +1549,15 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
RandAddPeriodic();
}, std::chrono::minutes{1});

// Check disk space every 5 minutes to avoid db corruption.
node.scheduler->scheduleEvery([&args]{
constexpr uint64_t min_disk_space = 50 << 20; // 50 MB
if (!CheckDiskSpace(args.GetBlocksDirPath(), min_disk_space)) {
Comment on lines +1554 to +1555
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Disk-space threshold is effectively doubled.

At Line 1555, CheckDiskSpace() already applies a built-in 50 MiB minimum. Passing min_disk_space from Line 1554 as additional_bytes raises the effective shutdown threshold to ~100 MiB.

💡 Proposed fix
-        constexpr uint64_t min_disk_space = 50 << 20; // 50 MB
-        if (!CheckDiskSpace(args.GetBlocksDirPath(), min_disk_space)) {
+        if (!CheckDiskSpace(args.GetBlocksDirPath())) {
             LogPrintf("Shutting down due to lack of disk space!\n");
             StartShutdown();
         }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/init.cpp` around lines 1554 - 1555, The code sets min_disk_space = 50 <<
20 and then calls CheckDiskSpace(args.GetBlocksDirPath(), min_disk_space), but
CheckDiskSpace already enforces a 50 MiB minimum, effectively doubling the
threshold; fix by calling CheckDiskSpace with zero additional bytes (or remove
the extra min_disk_space) so only the built-in 50 MiB minimum applies—update the
call site that uses CheckDiskSpace(args.GetBlocksDirPath(), ...) and remove or
replace the min_disk_space argument accordingly.

LogPrintf("Shutting down due to lack of disk space!\n");
StartShutdown();
}
}, std::chrono::minutes{5});

GetMainSignals().RegisterBackgroundSignalScheduler(*node.scheduler);

// Create client interfaces for wallets that are supposed to be loaded
Expand Down
16 changes: 10 additions & 6 deletions src/test/fuzz/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,14 @@ FUZZ_TARGET(transaction, .init = initialize_transaction)
const CCoinsViewCache coins_view_cache(&coins_view);
(void)AreInputsStandard(tx, coins_view_cache);

UniValue u(UniValue::VOBJ);
TxToUniv(tx, /*block_hash=*/uint256::ZERO, /*entry=*/u);
TxToUniv(tx, /*block_hash=*/uint256::ONE, /*entry=*/u);

// TxToUniv(tx, /*block_hash=*/uint256::ZERO, /*include_addresses=*/true, u);
// TxToUniv(tx, /*block_hash=*/uint256::ONE, /*include_addresses=*/false, u);
if (tx.GetTotalSize() < 250'000) { // Avoid high memory usage (with msan) due to json encoding
{
UniValue u{UniValue::VOBJ};
TxToUniv(tx, /*block_hash=*/uint256::ZERO, /*entry=*/u);
}
{
UniValue u{UniValue::VOBJ};
TxToUniv(tx, /*block_hash=*/uint256::ONE, /*entry=*/u);
}
}
}
11 changes: 10 additions & 1 deletion src/test/util/setup_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <test/util/net.h>
#include <test/util/txmempool.h>
#include <txdb.h>
#include <util/check.h>
#include <util/strencodings.h>
#include <util/string.h>
#include <util/thread.h>
Expand Down Expand Up @@ -156,6 +157,15 @@ void DashChainstateSetupClose(NodeContext& node)
Assert(node.mempool.get()));
}

struct NetworkSetup
{
NetworkSetup()
{
Assert(SetupNetworking());
}
};
static NetworkSetup g_networksetup_instance;

BasicTestingSetup::BasicTestingSetup(const std::string& chainName, const std::vector<const char*>& extra_args)
: m_path_root{fs::temp_directory_path() / "test_common_" PACKAGE_NAME / g_insecure_rand_ctx_temp_path.rand256().ToString()},
m_args{}
Expand Down Expand Up @@ -201,7 +211,6 @@ BasicTestingSetup::BasicTestingSetup(const std::string& chainName, const std::ve
ECC_Start();
BLSInit();
SetupEnvironment();
SetupNetworking();
InitSignatureCache();
InitScriptExecutionCache();

Expand Down
3 changes: 3 additions & 0 deletions src/test/util_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,9 @@ BOOST_AUTO_TEST_CASE(test_LockDirectory)
// has released the lock as we would expect by probing it.
int processstatus;
BOOST_CHECK_EQUAL(write(fd[1], &LockCommand, 1), 1);
// The following line invokes the ~CNetCleanup dtor without
// a paired SetupNetworking call. This is acceptable as long as
// ~CNetCleanup is a no-op for non-Windows platforms.
BOOST_CHECK_EQUAL(write(fd[1], &ExitCommand, 1), 1);
BOOST_CHECK_EQUAL(waitpid(pid, &processstatus, 0), pid);
BOOST_CHECK_EQUAL(processstatus, 0);
Expand Down
2 changes: 1 addition & 1 deletion src/util/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ extern const char * const BITCOIN_CONF_FILENAME;
extern const char * const BITCOIN_SETTINGS_FILENAME;

void SetupEnvironment();
bool SetupNetworking();
[[nodiscard]] bool SetupNetworking();

template<typename... Args>
bool error(const char* fmt, const Args&... args)
Expand Down
4 changes: 2 additions & 2 deletions src/wallet/test/fuzz/notifications.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 The Bitcoin Core developers
// Copyright (c) 2021-present The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

Expand Down Expand Up @@ -84,7 +84,7 @@ FUZZ_TARGET(wallet_notifications, .init = initialize_setup)
// The total amount, to be distributed to the wallets a and b in txs
// without fee. Thus, the balance of the wallets should always equal the
// total amount.
const auto total_amount{ConsumeMoney(fuzzed_data_provider)};
const auto total_amount{ConsumeMoney(fuzzed_data_provider, /*max=*/MAX_MONEY / 100000)};
FuzzedWallet a{"fuzzed_wallet_a"};
FuzzedWallet b{"fuzzed_wallet_b"};

Expand Down
3 changes: 1 addition & 2 deletions test/functional/mempool_package_limits.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ def set_test_params(self):
def run_test(self):
self.wallet = MiniWallet(self.nodes[0])
# Add enough mature utxos to the wallet so that all txs spend confirmed coins.
self.generate(self.wallet, 35)
self.generate(self.nodes[0], COINBASE_MATURITY)
self.generate(self.wallet, COINBASE_MATURITY + 35)

self.test_chain_limits()
self.test_desc_count_limits()
Expand Down
Loading