2012-09-03 21:14:03 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2022-12-24 23:49:50 +00:00
|
|
|
// Copyright (c) 2009-2022 The Bitcoin Core developers
|
2014-10-31 08:43:19 +08:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-09-03 21:14:03 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2013-04-13 00:13:08 -05:00
|
|
|
|
2014-11-03 16:16:40 +01:00
|
|
|
#ifndef BITCOIN_TXDB_H
|
|
|
|
#define BITCOIN_TXDB_H
|
2012-09-03 21:14:03 +02:00
|
|
|
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <coins.h>
|
|
|
|
#include <dbwrapper.h>
|
2022-09-15 10:45:07 +01:00
|
|
|
#include <kernel/cs_main.h>
|
2022-07-19 14:15:31 +02:00
|
|
|
#include <sync.h>
|
2023-03-15 11:18:06 +01:00
|
|
|
#include <util/fs.h>
|
2013-04-13 00:13:08 -05:00
|
|
|
|
2023-05-29 13:33:27 +02:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
2018-04-02 18:31:40 +00:00
|
|
|
#include <memory>
|
2021-08-04 20:55:31 +02:00
|
|
|
#include <optional>
|
2013-04-13 00:13:08 -05:00
|
|
|
#include <vector>
|
|
|
|
|
2023-05-29 13:33:27 +02:00
|
|
|
class COutPoint;
|
2013-04-13 00:13:08 -05:00
|
|
|
class uint256;
|
2012-09-03 21:14:03 +02:00
|
|
|
|
2014-10-31 08:43:19 +08:00
|
|
|
//! -dbcache default (MiB)
|
2017-03-31 10:25:39 -04:00
|
|
|
static const int64_t nDefaultDbCache = 450;
|
2017-04-19 09:34:30 -07:00
|
|
|
//! -dbbatchsize default (bytes)
|
|
|
|
static const int64_t nDefaultDbBatchSize = 16 << 20;
|
2016-06-27 15:39:28 +02:00
|
|
|
//! max. -dbcache (MiB)
|
2015-05-04 01:56:42 +02:00
|
|
|
static const int64_t nMaxDbCache = sizeof(void*) > 4 ? 16384 : 1024;
|
2016-06-27 15:39:28 +02:00
|
|
|
//! min. -dbcache (MiB)
|
2014-02-18 17:12:48 +01:00
|
|
|
static const int64_t nMinDbCache = 4;
|
2016-06-27 15:39:28 +02:00
|
|
|
//! Max memory allocated to block tree DB specific cache, if no -txindex (MiB)
|
|
|
|
static const int64_t nMaxBlockDBCache = 2;
|
|
|
|
//! Max memory allocated to block tree DB specific cache, if -txindex (MiB)
|
|
|
|
// Unlike for the UTXO database, for the txindex scenario the leveldb cache make
|
|
|
|
// a meaningful difference: https://github.com/bitcoin/bitcoin/pull/8273#issuecomment-229601991
|
2017-12-08 11:29:59 -08:00
|
|
|
static const int64_t nMaxTxIndexCache = 1024;
|
2018-08-29 22:15:50 -07:00
|
|
|
//! Max memory allocated to all block filter index caches combined in MiB.
|
|
|
|
static const int64_t max_filter_index_cache = 1024;
|
2016-06-27 15:39:28 +02:00
|
|
|
//! Max memory allocated to coin DB specific cache (MiB)
|
|
|
|
static const int64_t nMaxCoinsDBCache = 8;
|
2014-02-16 22:00:12 +01:00
|
|
|
|
2022-08-16 23:32:55 -04:00
|
|
|
//! User-controlled performance and debug options.
|
|
|
|
struct CoinsViewOptions {
|
|
|
|
//! Maximum database write batch size in bytes.
|
|
|
|
size_t batch_write_bytes = nDefaultDbBatchSize;
|
|
|
|
//! If non-zero, randomly exit when the database is flushed with (1/ratio)
|
|
|
|
//! probability.
|
|
|
|
int simulate_crash_ratio = 0;
|
|
|
|
};
|
|
|
|
|
2015-10-22 21:02:20 -04:00
|
|
|
/** CCoinsView backed by the coin database (chainstate/) */
|
2017-07-12 16:48:36 -04:00
|
|
|
class CCoinsViewDB final : public CCoinsView
|
2012-10-16 22:23:39 +02:00
|
|
|
{
|
|
|
|
protected:
|
2022-08-16 23:32:55 -04:00
|
|
|
DBParams m_db_params;
|
|
|
|
CoinsViewOptions m_options;
|
2019-04-17 10:07:15 -04:00
|
|
|
std::unique_ptr<CDBWrapper> m_db;
|
2012-10-16 22:23:39 +02:00
|
|
|
public:
|
2022-08-16 23:32:55 -04:00
|
|
|
explicit CCoinsViewDB(DBParams db_params, CoinsViewOptions options);
|
2012-10-16 22:23:39 +02:00
|
|
|
|
2017-05-30 17:58:54 -07:00
|
|
|
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
|
|
|
|
bool HaveCoin(const COutPoint &outpoint) const override;
|
2017-04-25 11:29:39 -07:00
|
|
|
uint256 GetBestBlock() const override;
|
2017-04-19 09:34:30 -07:00
|
|
|
std::vector<uint256> GetHeadBlocks() const override;
|
2019-12-03 13:25:35 -05:00
|
|
|
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock, bool erase = true) override;
|
2021-06-16 16:27:20 -04:00
|
|
|
std::unique_ptr<CCoinsViewCursor> Cursor() const override;
|
2017-05-12 15:19:19 -07:00
|
|
|
|
2022-02-02 13:23:02 +01:00
|
|
|
//! Whether an unsupported database format is used.
|
|
|
|
bool NeedsUpgrade();
|
2017-05-12 15:19:19 -07:00
|
|
|
size_t EstimateSize() const override;
|
2019-04-17 10:07:15 -04:00
|
|
|
|
|
|
|
//! Dynamically alter the underlying leveldb cache size.
|
|
|
|
void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
|
2022-02-01 16:22:00 -05:00
|
|
|
|
|
|
|
//! @returns filesystem path to on-disk storage or std::nullopt if in memory.
|
|
|
|
std::optional<fs::path> StoragePath() { return m_db->StoragePath(); }
|
2016-03-28 18:18:30 +02:00
|
|
|
};
|
|
|
|
|
2014-11-03 16:16:40 +01:00
|
|
|
#endif // BITCOIN_TXDB_H
|