123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- // ECOin - Copyright (c) - 2014/2022 - GPLv3 - epsylon@riseup.net (https://03c8.net)
- #ifndef ECOIN_DB_H
- #define ECOIN_DB_H
- #include "main.h"
- #include <cstdlib>
- #include <cstring>
- #include <map>
- #include <string>
- #include <vector>
- #include <db_cxx.h>
- class CAddress;
- class CAddrMan;
- class CBlockLocator;
- class CDiskBlockIndex;
- class CDiskTxPos;
- class CMasterKey;
- class COutPoint;
- class CTxIndex;
- class CWallet;
- class CWalletTx;
- extern unsigned int nWalletDBUpdated;
- void ThreadFlushWalletDB(void* parg);
- bool BackupWallet(const CWallet& wallet, const std::string& strDest);
- class CDBEnv
- {
- private:
- bool fDetachDB;
- bool fDbEnvInit;
- bool fMockDb;
- boost::filesystem::path pathEnv;
- std::string strPath;
- void EnvShutdown();
- public:
- mutable CCriticalSection cs_db;
- DbEnv dbenv;
- std::map<std::string, int> mapFileUseCount;
- std::map<std::string, Db*> mapDb;
- CDBEnv();
- ~CDBEnv();
- void MakeMock();
- bool IsMock() { return fMockDb; };
- /*
- * Verify that database file strFile is OK. If it is not,
- * call the callback to try to recover.
- * This must be called BEFORE strFile is opened.
- * Returns true if strFile is OK.
- */
- enum VerifyResult { VERIFY_OK, RECOVER_OK, RECOVER_FAIL };
- VerifyResult Verify(std::string strFile, bool (*recoverFunc)(CDBEnv& dbenv, std::string strFile));
- /*
- * Salvage data from a file that Verify says is bad.
- * fAggressive sets the DB_AGGRESSIVE flag (see berkeley DB->verify() method documentation).
- * Appends binary key/value pairs to vResult, returns true if successful.
- * NOTE: reads the entire database into memory, so cannot be used
- * for huge databases.
- */
- typedef std::pair<std::vector<unsigned char>, std::vector<unsigned char> > KeyValPair;
- bool Salvage(std::string strFile, bool fAggressive, std::vector<KeyValPair>& vResult);
- bool Open(boost::filesystem::path pathEnv_);
- void Close();
- void Flush(bool fShutdown);
- void CheckpointLSN(std::string strFile);
- void SetDetach(bool fDetachDB_) { fDetachDB = fDetachDB_; }
- bool GetDetach() { return fDetachDB; }
- void CloseDb(const std::string& strFile);
- bool RemoveDb(const std::string& strFile);
- DbTxn *TxnBegin(int flags=DB_TXN_WRITE_NOSYNC)
- {
- DbTxn* ptxn = NULL;
- int ret = dbenv.txn_begin(NULL, &ptxn, flags);
- if (!ptxn || ret != 0)
- return NULL;
- return ptxn;
- }
- };
- extern CDBEnv bitdb;
- /** RAII class that provides access to a Berkeley database */
- class CDB
- {
- protected:
- Db* pdb;
- std::string strFile;
- DbTxn *activeTxn;
- bool fReadOnly;
- explicit CDB(const char* pszFile, const char* pszMode="r+");
- ~CDB() { Close(); }
- public:
- void Close();
- private:
- CDB(const CDB&);
- void operator=(const CDB&);
- protected:
- template<typename K, typename T>
- bool Read(const K& key, T& value)
- {
- if (!pdb)
- return false;
- // Key
- CDataStream ssKey(SER_DISK, CLIENT_VERSION);
- ssKey.reserve(1000);
- ssKey << key;
- Dbt datKey(&ssKey[0], ssKey.size());
- // Read
- Dbt datValue;
- datValue.set_flags(DB_DBT_MALLOC);
- int ret = pdb->get(activeTxn, &datKey, &datValue, 0);
- std::memset(datKey.get_data(), 0, datKey.get_size());
- if (datValue.get_data() == NULL)
- return false;
- // Unserialize value
- try {
- CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK, CLIENT_VERSION);
- ssValue >> value;
- }
- catch (std::exception &e) {
- return false;
- }
- // Clear and free memory
- std::memset(datValue.get_data(), 0, datValue.get_size());
- std::free(datValue.get_data());
- return (ret == 0);
- }
- template<typename K, typename T>
- bool Write(const K& key, const T& value, bool fOverwrite=true)
- {
- if (!pdb)
- return false;
- if (fReadOnly)
- assert(!"Write called on database in read-only mode");
- // Key
- CDataStream ssKey(SER_DISK, CLIENT_VERSION);
- ssKey.reserve(1000);
- ssKey << key;
- Dbt datKey(&ssKey[0], ssKey.size());
- // Value
- CDataStream ssValue(SER_DISK, CLIENT_VERSION);
- ssValue.reserve(10000);
- ssValue << value;
- Dbt datValue(&ssValue[0], ssValue.size());
- // Write
- int ret = pdb->put(activeTxn, &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));
- // Clear memory in case it was a private key
- std::memset(datKey.get_data(), 0, datKey.get_size());
- std::memset(datValue.get_data(), 0, datValue.get_size());
- return (ret == 0);
- }
- template<typename K>
- bool Erase(const K& key)
- {
- if (!pdb)
- return false;
- if (fReadOnly)
- assert(!"Erase called on database in read-only mode");
- // Key
- CDataStream ssKey(SER_DISK, CLIENT_VERSION);
- ssKey.reserve(1000);
- ssKey << key;
- Dbt datKey(&ssKey[0], ssKey.size());
- // Erase
- int ret = pdb->del(activeTxn, &datKey, 0);
- // Clear memory
- std::memset(datKey.get_data(), 0, datKey.get_size());
- return (ret == 0 || ret == DB_NOTFOUND);
- }
- template<typename K>
- bool Exists(const K& key)
- {
- if (!pdb)
- return false;
- // Key
- CDataStream ssKey(SER_DISK, CLIENT_VERSION);
- ssKey.reserve(1000);
- ssKey << key;
- Dbt datKey(&ssKey[0], ssKey.size());
- // Exists
- int ret = pdb->exists(activeTxn, &datKey, 0);
- // Clear memory
- std::memset(datKey.get_data(), 0, datKey.get_size());
- return (ret == 0);
- }
- Dbc* GetCursor()
- {
- if (!pdb)
- return NULL;
- Dbc* pcursor = NULL;
- int ret = pdb->cursor(NULL, &pcursor, 0);
- if (ret != 0)
- return NULL;
- return pcursor;
- }
- int ReadAtCursor(Dbc* pcursor, CDataStream& ssKey, CDataStream& ssValue, unsigned int fFlags=DB_NEXT)
- {
- // Read at cursor
- Dbt datKey;
- if (fFlags == DB_SET || fFlags == DB_SET_RANGE || fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
- {
- datKey.set_data(&ssKey[0]);
- datKey.set_size(ssKey.size());
- }
- Dbt datValue;
- if (fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)
- {
- datValue.set_data(&ssValue[0]);
- datValue.set_size(ssValue.size());
- }
- datKey.set_flags(DB_DBT_MALLOC);
- datValue.set_flags(DB_DBT_MALLOC);
- int ret = pcursor->get(&datKey, &datValue, fFlags);
- if (ret != 0)
- return ret;
- else if (datKey.get_data() == NULL || datValue.get_data() == NULL)
- return 99999;
- // Convert to streams
- ssKey.SetType(SER_DISK);
- ssKey.clear();
- ssKey.write((char*)datKey.get_data(), datKey.get_size());
- ssValue.SetType(SER_DISK);
- ssValue.clear();
- ssValue.write((char*)datValue.get_data(), datValue.get_size());
- // Clear and free memory
- std::memset(datKey.get_data(), 0, datKey.get_size());
- std::memset(datValue.get_data(), 0, datValue.get_size());
- std::free(datKey.get_data());
- std::free(datValue.get_data());
- return 0;
- }
- public:
- bool TxnBegin()
- {
- if (!pdb || activeTxn)
- return false;
- DbTxn* ptxn = bitdb.TxnBegin();
- if (!ptxn)
- return false;
- activeTxn = ptxn;
- return true;
- }
- bool TxnCommit()
- {
- if (!pdb || !activeTxn)
- return false;
- int ret = activeTxn->commit(0);
- activeTxn = NULL;
- return (ret == 0);
- }
- bool TxnAbort()
- {
- if (!pdb || !activeTxn)
- return false;
- int ret = activeTxn->abort();
- activeTxn = NULL;
- return (ret == 0);
- }
- bool ReadVersion(int& nVersion)
- {
- nVersion = 0;
- return Read(std::string("version"), nVersion);
- }
- bool WriteVersion(int nVersion)
- {
- return Write(std::string("version"), nVersion);
- }
- bool static Rewrite(const std::string& strFile, const char* pszSkip = NULL);
- };
- /** Access to the transaction database (blkindex.dat) */
- /**
- class CTxDB : public CDB
- {
- public:
- CTxDB(const char* pszMode="r+") : CDB("blkindex.dat", pszMode) { }
- private:
- CTxDB(const CTxDB&);
- void operator=(const CTxDB&);
- public:
- bool ReadTxIndex(uint256 hash, CTxIndex& txindex);
- bool UpdateTxIndex(uint256 hash, const CTxIndex& txindex);
- bool AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight);
- bool EraseTxIndex(const CTransaction& tx);
- bool ContainsTx(uint256 hash);
- bool ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex);
- bool ReadDiskTx(uint256 hash, CTransaction& tx);
- bool ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex);
- bool ReadDiskTx(COutPoint outpoint, CTransaction& tx);
- bool WriteBlockIndex(const CDiskBlockIndex& blockindex);
- bool ReadHashBestChain(uint256& hashBestChain);
- bool WriteHashBestChain(uint256 hashBestChain);
- bool ReadBestInvalidTrust(CBigNum& bnBestInvalidTrust);
- bool WriteBestInvalidTrust(CBigNum bnBestInvalidTrust);
- bool ReadSyncCheckpoint(uint256& hashCheckpoint);
- bool WriteSyncCheckpoint(uint256 hashCheckpoint);
- bool ReadCheckpointPubKey(std::string& strPubKey);
- bool WriteCheckpointPubKey(const std::string& strPubKey);
- bool LoadBlockIndex();
- private:
- bool LoadBlockIndexGuts();
- };
- */
- /** Access to the (IP) address database (peers.dat) */
- class CAddrDB
- {
- private:
- boost::filesystem::path pathAddr;
- public:
- CAddrDB();
- bool Write(const CAddrMan& addr);
- bool Read(CAddrMan& addr);
- };
- #endif // ECOIN_DB_H
|