Research
Timechain
Search Donate
Sponsors
Cake Wallet, private Bitcoin and Monero walletSideShift.ai, no-signup crypto exchangeOrangeRock, non-custodial bitcoin spot and perps tradingBraiins Hashpower, buy and sell Bitcoin hashrateeCash.com
Bitcoin v0.3.0 · 2010-07-06

db.h

404 lines · plain text · GitHub · MIT license (license.txt)
1// Copyright (c) 2009-2010 Satoshi Nakamoto2// Distributed under the MIT/X11 software license, see the accompanying3// file license.txt or http://www.opensource.org/licenses/mit-license.php.45class CTransaction;6class CTxIndex;7class CDiskBlockIndex;8class CDiskTxPos;9class COutPoint;10class CUser;11class CReview;12class CAddress;13class CWalletTx;1415extern map<string, string> mapAddressBook;16extern CCriticalSection cs_mapAddressBook;17extern vector<unsigned char> vchDefaultKey;18extern bool fClient;19202122extern unsigned int nWalletDBUpdated;23extern DbEnv dbenv;242526extern void DBFlush(bool fShutdown);2728293031class CDB32{33protected:34    Db* pdb;35    string strFile;36    vector<DbTxn*> vTxn;37    bool fReadOnly;3839    explicit CDB(const char* pszFile, const char* pszMode="r+");40    ~CDB() { Close(); }41public:42    void Close();43private:44    CDB(const CDB&);45    void operator=(const CDB&);4647protected:48    template<typename K, typename T>49    bool Read(const K& key, T& value)50    {51        if (!pdb)52            return false;5354        // Key55        CDataStream ssKey(SER_DISK);56        ssKey.reserve(1000);57        ssKey << key;58        Dbt datKey(&ssKey[0], ssKey.size());5960        // Read61        Dbt datValue;62        datValue.set_flags(DB_DBT_MALLOC);63        int ret = pdb->get(GetTxn(), &datKey, &datValue, 0);64        memset(datKey.get_data(), 0, datKey.get_size());65        if (datValue.get_data() == NULL)66            return false;6768        // Unserialize value69        CDataStream ssValue((char*)datValue.get_data(), (char*)datValue.get_data() + datValue.get_size(), SER_DISK);70        ssValue >> value;7172        // Clear and free memory73        memset(datValue.get_data(), 0, datValue.get_size());74        free(datValue.get_data());75        return (ret == 0);76    }7778    template<typename K, typename T>79    bool Write(const K& key, const T& value, bool fOverwrite=true)80    {81        if (!pdb)82            return false;83        if (fReadOnly)84            assert(("Write called on database in read-only mode", false));8586        // Key87        CDataStream ssKey(SER_DISK);88        ssKey.reserve(1000);89        ssKey << key;90        Dbt datKey(&ssKey[0], ssKey.size());9192        // Value93        CDataStream ssValue(SER_DISK);94        ssValue.reserve(10000);95        ssValue << value;96        Dbt datValue(&ssValue[0], ssValue.size());9798        // Write99        int ret = pdb->put(GetTxn(), &datKey, &datValue, (fOverwrite ? 0 : DB_NOOVERWRITE));100101        // Clear memory in case it was a private key102        memset(datKey.get_data(), 0, datKey.get_size());103        memset(datValue.get_data(), 0, datValue.get_size());104        return (ret == 0);105    }106107    template<typename K>108    bool Erase(const K& key)109    {110        if (!pdb)111            return false;112        if (fReadOnly)113            assert(("Erase called on database in read-only mode", false));114115        // Key116        CDataStream ssKey(SER_DISK);117        ssKey.reserve(1000);118        ssKey << key;119        Dbt datKey(&ssKey[0], ssKey.size());120121        // Erase122        int ret = pdb->del(GetTxn(), &datKey, 0);123124        // Clear memory125        memset(datKey.get_data(), 0, datKey.get_size());126        return (ret == 0 || ret == DB_NOTFOUND);127    }128129    template<typename K>130    bool Exists(const K& key)131    {132        if (!pdb)133            return false;134135        // Key136        CDataStream ssKey(SER_DISK);137        ssKey.reserve(1000);138        ssKey << key;139        Dbt datKey(&ssKey[0], ssKey.size());140141        // Exists142        int ret = pdb->exists(GetTxn(), &datKey, 0);143144        // Clear memory145        memset(datKey.get_data(), 0, datKey.get_size());146        return (ret == 0);147    }148149    Dbc* GetCursor()150    {151        if (!pdb)152            return NULL;153        Dbc* pcursor = NULL;154        int ret = pdb->cursor(NULL, &pcursor, 0);155        if (ret != 0)156            return NULL;157        return pcursor;158    }159160    int ReadAtCursor(Dbc* pcursor, CDataStream& ssKey, CDataStream& ssValue, unsigned int fFlags=DB_NEXT)161    {162        // Read at cursor163        Dbt datKey;164        if (fFlags == DB_SET || fFlags == DB_SET_RANGE || fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)165        {166            datKey.set_data(&ssKey[0]);167            datKey.set_size(ssKey.size());168        }169        Dbt datValue;170        if (fFlags == DB_GET_BOTH || fFlags == DB_GET_BOTH_RANGE)171        {172            datValue.set_data(&ssValue[0]);173            datValue.set_size(ssValue.size());174        }175        datKey.set_flags(DB_DBT_MALLOC);176        datValue.set_flags(DB_DBT_MALLOC);177        int ret = pcursor->get(&datKey, &datValue, fFlags);178        if (ret != 0)179            return ret;180        else if (datKey.get_data() == NULL || datValue.get_data() == NULL)181            return 99999;182183        // Convert to streams184        ssKey.SetType(SER_DISK);185        ssKey.clear();186        ssKey.write((char*)datKey.get_data(), datKey.get_size());187        ssValue.SetType(SER_DISK);188        ssValue.clear();189        ssValue.write((char*)datValue.get_data(), datValue.get_size());190191        // Clear and free memory192        memset(datKey.get_data(), 0, datKey.get_size());193        memset(datValue.get_data(), 0, datValue.get_size());194        free(datKey.get_data());195        free(datValue.get_data());196        return 0;197    }198199    DbTxn* GetTxn()200    {201        if (!vTxn.empty())202            return vTxn.back();203        else204            return NULL;205    }206207public:208    bool TxnBegin()209    {210        if (!pdb)211            return false;212        DbTxn* ptxn = NULL;213        int ret = dbenv.txn_begin(GetTxn(), &ptxn, 0);214        if (!ptxn || ret != 0)215            return false;216        vTxn.push_back(ptxn);217        return true;218    }219220    bool TxnCommit()221    {222        if (!pdb)223            return false;224        if (vTxn.empty())225            return false;226        int ret = vTxn.back()->commit(0);227        vTxn.pop_back();228        return (ret == 0);229    }230231    bool TxnAbort()232    {233        if (!pdb)234            return false;235        if (vTxn.empty())236            return false;237        int ret = vTxn.back()->abort();238        vTxn.pop_back();239        return (ret == 0);240    }241242    bool ReadVersion(int& nVersion)243    {244        nVersion = 0;245        return Read(string("version"), nVersion);246    }247248    bool WriteVersion(int nVersion)249    {250        return Write(string("version"), nVersion);251    }252};253254255256257258259260261class CTxDB : public CDB262{263public:264    CTxDB(const char* pszMode="r+") : CDB(!fClient ? "blkindex.dat" : NULL, pszMode) { }265private:266    CTxDB(const CTxDB&);267    void operator=(const CTxDB&);268public:269    bool ReadTxIndex(uint256 hash, CTxIndex& txindex);270    bool UpdateTxIndex(uint256 hash, const CTxIndex& txindex);271    bool AddTxIndex(const CTransaction& tx, const CDiskTxPos& pos, int nHeight);272    bool EraseTxIndex(const CTransaction& tx);273    bool ContainsTx(uint256 hash);274    bool ReadOwnerTxes(uint160 hash160, int nHeight, vector<CTransaction>& vtx);275    bool ReadDiskTx(uint256 hash, CTransaction& tx, CTxIndex& txindex);276    bool ReadDiskTx(uint256 hash, CTransaction& tx);277    bool ReadDiskTx(COutPoint outpoint, CTransaction& tx, CTxIndex& txindex);278    bool ReadDiskTx(COutPoint outpoint, CTransaction& tx);279    bool WriteBlockIndex(const CDiskBlockIndex& blockindex);280    bool EraseBlockIndex(uint256 hash);281    bool ReadHashBestChain(uint256& hashBestChain);282    bool WriteHashBestChain(uint256 hashBestChain);283    bool LoadBlockIndex();284};285286287288289290class CAddrDB : public CDB291{292public:293    CAddrDB(const char* pszMode="r+") : CDB("addr.dat", pszMode) { }294private:295    CAddrDB(const CAddrDB&);296    void operator=(const CAddrDB&);297public:298    bool WriteAddress(const CAddress& addr);299    bool LoadAddresses();300};301302bool LoadAddresses();303304305306307308309class CWalletDB : public CDB310{311public:312    CWalletDB(const char* pszMode="r+") : CDB("wallet.dat", pszMode) { }313private:314    CWalletDB(const CWalletDB&);315    void operator=(const CWalletDB&);316public:317    bool ReadName(const string& strAddress, string& strName)318    {319        strName = "";320        return Read(make_pair(string("name"), strAddress), strName);321    }322323    bool WriteName(const string& strAddress, const string& strName)324    {325        CRITICAL_BLOCK(cs_mapAddressBook)326            mapAddressBook[strAddress] = strName;327        nWalletDBUpdated++;328        return Write(make_pair(string("name"), strAddress), strName);329    }330331    bool EraseName(const string& strAddress)332    {333        // This should only be used for sending addresses, never for receiving addresses,334        // receiving addresses must always have an address book entry if they're not change return.335        CRITICAL_BLOCK(cs_mapAddressBook)336            mapAddressBook.erase(strAddress);337        nWalletDBUpdated++;338        return Erase(make_pair(string("name"), strAddress));339    }340341    bool ReadTx(uint256 hash, CWalletTx& wtx)342    {343        return Read(make_pair(string("tx"), hash), wtx);344    }345346    bool WriteTx(uint256 hash, const CWalletTx& wtx)347    {348        nWalletDBUpdated++;349        return Write(make_pair(string("tx"), hash), wtx);350    }351352    bool EraseTx(uint256 hash)353    {354        nWalletDBUpdated++;355        return Erase(make_pair(string("tx"), hash));356    }357358    bool ReadKey(const vector<unsigned char>& vchPubKey, CPrivKey& vchPrivKey)359    {360        vchPrivKey.clear();361        return Read(make_pair(string("key"), vchPubKey), vchPrivKey);362    }363364    bool WriteKey(const vector<unsigned char>& vchPubKey, const CPrivKey& vchPrivKey)365    {366        nWalletDBUpdated++;367        return Write(make_pair(string("key"), vchPubKey), vchPrivKey, false);368    }369370    bool ReadDefaultKey(vector<unsigned char>& vchPubKey)371    {372        vchPubKey.clear();373        return Read(string("defaultkey"), vchPubKey);374    }375376    bool WriteDefaultKey(const vector<unsigned char>& vchPubKey)377    {378        vchDefaultKey = vchPubKey;379        nWalletDBUpdated++;380        return Write(string("defaultkey"), vchPubKey);381    }382383    template<typename T>384    bool ReadSetting(const string& strKey, T& value)385    {386        return Read(make_pair(string("setting"), strKey), value);387    }388389    template<typename T>390    bool WriteSetting(const string& strKey, const T& value)391    {392        nWalletDBUpdated++;393        return Write(make_pair(string("setting"), strKey), value);394    }395396    bool LoadWallet();397};398399bool LoadWallet(bool& fFirstRunRet);400401inline bool SetAddressBookName(const string& strAddress, const string& strName)402{403    return CWalletDB().WriteName(strAddress, strName);404}

Click a line number to link to it; use #L10-L20 for a range. "=" next to a version means the file is identical there.

0:000:00