Files
thehub/libs/server/BlocksDB.h
T

179 lines
6.1 KiB
C++
Raw Permalink Normal View History

/*
2017-11-13 23:09:33 +01:00
* This file is part of the Flowee project
* Copyright (c) 2009-2010 Satoshi Nakamoto
* Copyright (c) 2009-2015 The Bitcoin Core developers
2018-01-15 15:26:12 +00:00
* Copyright (c) 2017-2018 Tom Zander <tomz@freedommail.ch>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2017-02-14 11:17:46 +01:00
2018-01-16 10:47:52 +00:00
#ifndef BLOCKSDB_H
#define BLOCKSDB_H
2017-02-14 11:17:46 +01:00
#include "dbwrapper.h"
2018-01-15 15:26:12 +00:00
#include <primitives/FastUndoBlock.h>
#include <streaming/ConstBuffer.h>
2017-02-14 11:17:46 +01:00
#include <string>
#include <vector>
class CBlockFileInfo;
class CBlockIndex;
struct CDiskTxPos;
2017-04-22 00:57:16 +09:00
struct CDiskBlockPos;
2017-02-14 11:17:46 +01:00
class uint256;
class FastBlock;
2017-04-03 15:30:03 +02:00
class CChain;
2018-09-25 16:24:05 +02:00
class CScheduler;
2020-02-27 13:35:56 +01:00
class UnspentOutputDatabase;
2017-02-14 11:17:46 +01:00
namespace Blocks {
2018-01-15 15:26:12 +00:00
enum ReindexingState {
NoReindex,
ScanningFiles,
ParsingBlocks
};
2017-04-03 15:30:03 +02:00
class DBPrivate;
2017-02-14 11:17:46 +01:00
/** Access to the block database (blocks/index/) */
class DB : public CDBWrapper
2017-02-14 11:17:46 +01:00
{
public:
/**
* returns the singleton instance of the BlocksDB. Please be aware that
2017-02-18 15:49:04 +01:00
* this will return nullptr until you call createInstance() or createTestInstance();
2017-02-14 11:17:46 +01:00
*/
static DB *instance();
2017-02-14 11:17:46 +01:00
/**
* Deletes an old and creates a new instance of the BlocksDB singleton.
* @param[in] nCacheSize Configures various leveldb cache settings.
* @param[in] fWipe If true, remove all existing data.
* @see instance()
*/
2018-09-25 16:24:05 +02:00
static void createInstance(size_t nCacheSize, bool fWipe, CScheduler *scheduler = nullptr);
2017-02-14 11:17:46 +01:00
/// Deletes old singleton and creates a new one for unit testing.
static void createTestInstance(size_t nCacheSize);
2017-12-15 13:42:40 +01:00
static void shutdown();
2017-02-14 11:17:46 +01:00
2017-04-03 15:30:03 +02:00
/**
* @brief starts the blockImporter part of a 'reindex'.
* This kicks off a new thread that reads each file and schedules each block for
* validation.
*/
2017-02-21 13:44:48 +01:00
static void startBlockImporter();
2017-04-03 15:30:03 +02:00
protected:
DB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
2017-04-03 15:30:03 +02:00
DB(const Blocks::DB&) = delete;
void operator=(const DB&) = delete;
2017-02-14 11:17:46 +01:00
public:
bool WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo *> > &fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo);
2017-02-14 11:17:46 +01:00
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &fileinfo);
bool ReadLastBlockFile(int &nFile);
bool ReadTxIndex(const uint256 &txid, CDiskTxPos &pos);
bool WriteTxIndex(const std::vector<std::pair<uint256, CDiskTxPos> > &list);
bool WriteFlag(const std::string &name, bool fValue);
bool ReadFlag(const std::string &name, bool &fValue);
/// Reads and caches all info about blocks.
2020-02-27 13:35:56 +01:00
bool CacheAllBlockInfos(const UnspentOutputDatabase *utxo);
2017-02-14 11:17:46 +01:00
2018-01-15 15:26:12 +00:00
ReindexingState reindexing() const;
inline bool isReindexing() const {
return reindexing() != NoReindex;
}
void setReindexing(ReindexingState state);
2017-02-21 13:44:48 +01:00
FastBlock loadBlock(CDiskBlockPos pos);
FastUndoBlock loadUndoBlock(CDiskBlockPos pos);
Streaming::ConstBuffer loadBlockFile(int fileIndex);
2018-01-15 15:26:12 +00:00
FastBlock writeBlock(const FastBlock &block, CDiskBlockPos &pos);
2017-09-05 16:04:44 +02:00
/**
* @brief This method writes out the undo block to a specific file and belonging to a specific /a blockHash.
* @param block The actual undo block
* @param blockHash the hash of the parent block
* @param fileIndex the index the original block was written to, this determines which revert index this block goes to.
* @param posInFile a return value of the position this block ended up in.
*/
void writeUndoBlock(const UndoBlockBuilder &undoBlock, int fileIndex, uint32_t *posInFile = 0);
2017-04-03 15:30:03 +02:00
/**
* @brief make the blocks-DB aware of a new header-only tip.
2019-12-01 21:04:24 +01:00
* Add the partially validated block to the blocks database and import all parent
2017-04-03 15:30:03 +02:00
* blocks at the same time.
* This potentially updates the headerChain() and headerChainTips().
* @param block the index to the block object.
* @returns true if the header became the new main-chain tip.
*/
bool appendHeader(CBlockIndex *block);
2017-07-23 12:38:17 +02:00
/// allow adding one block, this API is primarily meant for unit tests.
bool appendBlock(CBlockIndex *block, int lastBlockFile);
2017-04-03 15:30:03 +02:00
const CChain &headerChain();
const std::list<CBlockIndex*> & headerChainTips();
2017-02-21 13:44:48 +01:00
2020-07-31 16:03:15 +02:00
/** Try to detect Partition (network isolation) attacks against us */
void partitionCheck(int64_t powTargetSpacing);
2017-08-01 12:14:54 +02:00
void loadConfig();
/// \internal
2017-12-15 13:42:40 +01:00
std::shared_ptr<DBPrivate> priv() {
2017-08-01 12:14:54 +02:00
return d;
}
2017-02-14 11:17:46 +01:00
private:
static DB *s_instance;
2017-12-15 13:42:40 +01:00
std::shared_ptr<DBPrivate> d;
2017-02-14 11:17:46 +01:00
};
/** Open a block file (blk?????.dat) */
FILE* openFile(const CDiskBlockPos &pos, bool fReadOnly);
/** Open an undo file (rev?????.dat) */
FILE* openUndoFile(const CDiskBlockPos &pos, bool fReadOnly);
2017-08-01 12:14:54 +02:00
/**
* Translation to a filesystem path.
* @param fileIndex the number. For instance blk12345.dat is 12345.
* @param prefix either "blk" or "rev"
* @param fFindHarder set this to true if you want a path outside our main data-directory
*/
boost::filesystem::path getFilepathForIndex(int fileIndex, const char *prefix, bool fFindHarder = false);
2018-01-15 15:26:12 +00:00
namespace Index {
const uint256 *insert(const uint256 &hash, CBlockIndex *index);
bool exists(const uint256 &hash);
CBlockIndex *get(const uint256 &hash);
bool empty();
int size();
bool reconsiderBlock(CBlockIndex *pindex);
/** Find the last common ancestor two blocks have.
* Both pa and pb must be non-NULL. */
CBlockIndex* lastCommonAncestor(CBlockIndex* pa, CBlockIndex* pb);
2018-01-15 15:26:12 +00:00
/**
* @brief fileIndexes loops over all blocks to find indexes.
* @return a set of file-indexes (blk[num].dat) that contain blocks.
*/
std::set<int> fileIndexes();
void unload();
}
}
2018-01-16 10:47:52 +00:00
#endif