LCOV - code coverage report
Current view: top level - src/wallet - walletdb.h (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 62 64 96.9 %
Date: 2022-04-21 14:51:19 Functions: 42 43 97.7 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 84 140 60.0 %

           Branch data     Line data    Source code
#       1                 :            : // Copyright (c) 2009-2010 Satoshi Nakamoto
#       2                 :            : // Copyright (c) 2009-2021 The Bitcoin Core developers
#       3                 :            : // Distributed under the MIT software license, see the accompanying
#       4                 :            : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
#       5                 :            : 
#       6                 :            : #ifndef BITCOIN_WALLET_WALLETDB_H
#       7                 :            : #define BITCOIN_WALLET_WALLETDB_H
#       8                 :            : 
#       9                 :            : #include <script/sign.h>
#      10                 :            : #include <wallet/db.h>
#      11                 :            : #include <wallet/walletutil.h>
#      12                 :            : #include <key.h>
#      13                 :            : 
#      14                 :            : #include <stdint.h>
#      15                 :            : #include <string>
#      16                 :            : #include <vector>
#      17                 :            : 
#      18                 :            : class CScript;
#      19                 :            : class uint160;
#      20                 :            : class uint256;
#      21                 :            : struct CBlockLocator;
#      22                 :            : 
#      23                 :            : namespace wallet {
#      24                 :            : class CKeyPool;
#      25                 :            : class CMasterKey;
#      26                 :            : class CWallet;
#      27                 :            : class CWalletTx;
#      28                 :            : struct WalletContext;
#      29                 :            : 
#      30                 :            : /**
#      31                 :            :  * Overview of wallet database classes:
#      32                 :            :  *
#      33                 :            :  * - WalletBatch is an abstract modifier object for the wallet database, and encapsulates a database
#      34                 :            :  *   batch update as well as methods to act on the database. It should be agnostic to the database implementation.
#      35                 :            :  *
#      36                 :            :  * The following classes are implementation specific:
#      37                 :            :  * - BerkeleyEnvironment is an environment in which the database exists.
#      38                 :            :  * - BerkeleyDatabase represents a wallet database.
#      39                 :            :  * - BerkeleyBatch is a low-level database batch update.
#      40                 :            :  */
#      41                 :            : 
#      42                 :            : static const bool DEFAULT_FLUSHWALLET = true;
#      43                 :            : 
#      44                 :            : /** Error statuses for the wallet database */
#      45                 :            : enum class DBErrors
#      46                 :            : {
#      47                 :            :     LOAD_OK,
#      48                 :            :     CORRUPT,
#      49                 :            :     NONCRITICAL_ERROR,
#      50                 :            :     TOO_NEW,
#      51                 :            :     EXTERNAL_SIGNER_SUPPORT_REQUIRED,
#      52                 :            :     LOAD_FAIL,
#      53                 :            :     NEED_REWRITE,
#      54                 :            :     NEED_RESCAN
#      55                 :            : };
#      56                 :            : 
#      57                 :            : namespace DBKeys {
#      58                 :            : extern const std::string ACENTRY;
#      59                 :            : extern const std::string ACTIVEEXTERNALSPK;
#      60                 :            : extern const std::string ACTIVEINTERNALSPK;
#      61                 :            : extern const std::string BESTBLOCK;
#      62                 :            : extern const std::string BESTBLOCK_NOMERKLE;
#      63                 :            : extern const std::string CRYPTED_KEY;
#      64                 :            : extern const std::string CSCRIPT;
#      65                 :            : extern const std::string DEFAULTKEY;
#      66                 :            : extern const std::string DESTDATA;
#      67                 :            : extern const std::string FLAGS;
#      68                 :            : extern const std::string HDCHAIN;
#      69                 :            : extern const std::string KEY;
#      70                 :            : extern const std::string KEYMETA;
#      71                 :            : extern const std::string LOCKED_UTXO;
#      72                 :            : extern const std::string MASTER_KEY;
#      73                 :            : extern const std::string MINVERSION;
#      74                 :            : extern const std::string NAME;
#      75                 :            : extern const std::string OLD_KEY;
#      76                 :            : extern const std::string ORDERPOSNEXT;
#      77                 :            : extern const std::string POOL;
#      78                 :            : extern const std::string PURPOSE;
#      79                 :            : extern const std::string SETTINGS;
#      80                 :            : extern const std::string TX;
#      81                 :            : extern const std::string VERSION;
#      82                 :            : extern const std::string WALLETDESCRIPTOR;
#      83                 :            : extern const std::string WALLETDESCRIPTORCKEY;
#      84                 :            : extern const std::string WALLETDESCRIPTORKEY;
#      85                 :            : extern const std::string WATCHMETA;
#      86                 :            : extern const std::string WATCHS;
#      87                 :            : } // namespace DBKeys
#      88                 :            : 
#      89                 :            : /* simple HD chain data model */
#      90                 :            : class CHDChain
#      91                 :            : {
#      92                 :            : public:
#      93                 :            :     uint32_t nExternalChainCounter;
#      94                 :            :     uint32_t nInternalChainCounter;
#      95                 :            :     CKeyID seed_id; //!< seed hash160
#      96                 :            :     int64_t m_next_external_index{0}; // Next index in the keypool to be used. Memory only.
#      97                 :            :     int64_t m_next_internal_index{0}; // Next index in the keypool to be used. Memory only.
#      98                 :            : 
#      99                 :            :     static const int VERSION_HD_BASE        = 1;
#     100                 :            :     static const int VERSION_HD_CHAIN_SPLIT = 2;
#     101                 :            :     static const int CURRENT_VERSION        = VERSION_HD_CHAIN_SPLIT;
#     102                 :            :     int nVersion;
#     103                 :            : 
#     104                 :      12004 :     CHDChain() { SetNull(); }
#     105                 :            : 
#     106                 :            :     SERIALIZE_METHODS(CHDChain, obj)
#     107                 :      20077 :     {
#     108                 :      20077 :         READWRITE(obj.nVersion, obj.nExternalChainCounter, obj.seed_id);
#     109 [ +  - ][ +  - ]:      20077 :         if (obj.nVersion >= VERSION_HD_CHAIN_SPLIT) {
#     110                 :      20077 :             READWRITE(obj.nInternalChainCounter);
#     111                 :      20077 :         }
#     112                 :      20077 :     }
#     113                 :            : 
#     114                 :            :     void SetNull()
#     115                 :      12004 :     {
#     116                 :      12004 :         nVersion = CHDChain::CURRENT_VERSION;
#     117                 :      12004 :         nExternalChainCounter = 0;
#     118                 :      12004 :         nInternalChainCounter = 0;
#     119                 :      12004 :         seed_id.SetNull();
#     120                 :      12004 :     }
#     121                 :            : 
#     122                 :            :     bool operator==(const CHDChain& chain) const
#     123                 :      45005 :     {
#     124                 :      45005 :         return seed_id == chain.seed_id;
#     125                 :      45005 :     }
#     126                 :            : };
#     127                 :            : 
#     128                 :            : class CKeyMetadata
#     129                 :            : {
#     130                 :            : public:
#     131                 :            :     static const int VERSION_BASIC=1;
#     132                 :            :     static const int VERSION_WITH_HDDATA=10;
#     133                 :            :     static const int VERSION_WITH_KEY_ORIGIN = 12;
#     134                 :            :     static const int CURRENT_VERSION=VERSION_WITH_KEY_ORIGIN;
#     135                 :            :     int nVersion;
#     136                 :            :     int64_t nCreateTime; // 0 means unknown
#     137                 :            :     std::string hdKeypath; //optional HD/bip32 keypath. Still used to determine whether a key is a seed. Also kept for backwards compatibility
#     138                 :            :     CKeyID hd_seed_id; //id of the HD seed used to derive this key
#     139                 :            :     KeyOriginInfo key_origin; // Key origin info with path and fingerprint
#     140                 :            :     bool has_key_origin = false; //!< Whether the key_origin is useful
#     141                 :            : 
#     142                 :            :     CKeyMetadata()
#     143                 :     655440 :     {
#     144                 :     655440 :         SetNull();
#     145                 :     655440 :     }
#     146                 :            :     explicit CKeyMetadata(int64_t nCreateTime_)
#     147                 :      28096 :     {
#     148                 :      28096 :         SetNull();
#     149                 :      28096 :         nCreateTime = nCreateTime_;
#     150                 :      28096 :     }
#     151                 :            : 
#     152                 :            :     SERIALIZE_METHODS(CKeyMetadata, obj)
#     153                 :      33903 :     {
#     154                 :      33903 :         READWRITE(obj.nVersion, obj.nCreateTime);
#     155 [ +  - ][ +  - ]:      33903 :         if (obj.nVersion >= VERSION_WITH_HDDATA) {
#     156                 :      33903 :             READWRITE(obj.hdKeypath, obj.hd_seed_id);
#     157                 :      33903 :         }
#     158 [ +  - ][ +  - ]:      33903 :         if (obj.nVersion >= VERSION_WITH_KEY_ORIGIN)
#     159                 :      33903 :         {
#     160                 :      33903 :             READWRITE(obj.key_origin);
#     161                 :      33903 :             READWRITE(obj.has_key_origin);
#     162                 :      33903 :         }
#     163                 :      33903 :     }
#     164                 :            : 
#     165                 :            :     void SetNull()
#     166                 :     683536 :     {
#     167                 :     683536 :         nVersion = CKeyMetadata::CURRENT_VERSION;
#     168                 :     683536 :         nCreateTime = 0;
#     169                 :     683536 :         hdKeypath.clear();
#     170                 :     683536 :         hd_seed_id.SetNull();
#     171                 :     683536 :         key_origin.clear();
#     172                 :     683536 :         has_key_origin = false;
#     173                 :     683536 :     }
#     174                 :            : };
#     175                 :            : 
#     176                 :            : /** Access to the wallet database.
#     177                 :            :  * Opens the database and provides read and write access to it. Each read and write is its own transaction.
#     178                 :            :  * Multiple operation transactions can be started using TxnBegin() and committed using TxnCommit()
#     179                 :            :  * Otherwise the transaction will be committed when the object goes out of scope.
#     180                 :            :  * Optionally (on by default) it will flush to disk on close.
#     181                 :            :  * Every 1000 writes will automatically trigger a flush to disk.
#     182                 :            :  */
#     183                 :            : class WalletBatch
#     184                 :            : {
#     185                 :            : private:
#     186                 :            :     template <typename K, typename T>
#     187                 :            :     bool WriteIC(const K& key, const T& value, bool fOverwrite = true)
#     188                 :     273893 :     {
#     189 [ -  + ][ -  + ]:     273893 :         if (!m_batch->Write(key, value, fOverwrite)) {
#         [ -  + ][ -  + ]
#         [ -  + ][ -  + ]
#         [ -  + ][ -  + ]
#         [ -  + ][ -  + ]
#         [ -  + ][ +  + ]
#         [ -  + ][ -  + ]
#         [ -  + ][ -  + ]
#         [ -  + ][ +  + ]
#         [ -  + ][ -  + ]
#         [ -  + ][ -  + ]
#                 [ -  + ]
#     190                 :       8918 :             return false;
#     191                 :       8918 :         }
#     192                 :     264975 :         m_database.IncrementUpdateCounter();
#     193 [ +  + ][ +  + ]:     264975 :         if (m_database.nUpdateCounter % 1000 == 0) {
#         [ +  + ][ -  + ]
#         [ -  + ][ +  + ]
#         [ -  + ][ +  + ]
#         [ -  + ][ +  + ]
#         [ +  + ][ -  + ]
#         [ -  + ][ -  + ]
#         [ +  + ][ +  + ]
#         [ +  + ][ -  + ]
#         [ +  + ][ -  + ]
#         [ +  + ][ -  + ]
#                 [ +  + ]
#     194                 :        162 :             m_batch->Flush();
#     195                 :        162 :         }
#     196                 :     264975 :         return true;
#     197                 :     273893 :     }
#     198                 :            : 
#     199                 :            :     template <typename K>
#     200                 :            :     bool EraseIC(const K& key)
#     201                 :      14136 :     {
#     202 [ -  + ][ -  + ]:      14136 :         if (!m_batch->Erase(key)) {
#         [ -  + ][ -  + ]
#         [ -  + ][ #  # ]
#         [ -  + ][ -  + ]
#                 [ -  + ]
#     203                 :          0 :             return false;
#     204                 :          0 :         }
#     205                 :      14136 :         m_database.IncrementUpdateCounter();
#     206 [ -  + ][ -  + ]:      14136 :         if (m_database.nUpdateCounter % 1000 == 0) {
#         [ -  + ][ -  + ]
#         [ -  + ][ -  + ]
#         [ #  # ][ -  + ]
#                 [ +  + ]
#     207                 :         10 :             m_batch->Flush();
#     208                 :         10 :         }
#     209                 :      14136 :         return true;
#     210                 :      14136 :     }
#     211                 :            : 
#     212                 :            : public:
#     213                 :            :     explicit WalletBatch(WalletDatabase &database, bool _fFlushOnClose = true) :
#     214                 :            :         m_batch(database.MakeBatch(_fFlushOnClose)),
#     215                 :            :         m_database(database)
#     216                 :     196319 :     {
#     217                 :     196319 :     }
#     218                 :            :     WalletBatch(const WalletBatch&) = delete;
#     219                 :            :     WalletBatch& operator=(const WalletBatch&) = delete;
#     220                 :            : 
#     221                 :            :     bool WriteName(const std::string& strAddress, const std::string& strName);
#     222                 :            :     bool EraseName(const std::string& strAddress);
#     223                 :            : 
#     224                 :            :     bool WritePurpose(const std::string& strAddress, const std::string& purpose);
#     225                 :            :     bool ErasePurpose(const std::string& strAddress);
#     226                 :            : 
#     227                 :            :     bool WriteTx(const CWalletTx& wtx);
#     228                 :            :     bool EraseTx(uint256 hash);
#     229                 :            : 
#     230                 :            :     bool WriteKeyMetadata(const CKeyMetadata& meta, const CPubKey& pubkey, const bool overwrite);
#     231                 :            :     bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata &keyMeta);
#     232                 :            :     bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta);
#     233                 :            :     bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey);
#     234                 :            : 
#     235                 :            :     bool WriteCScript(const uint160& hash, const CScript& redeemScript);
#     236                 :            : 
#     237                 :            :     bool WriteWatchOnly(const CScript &script, const CKeyMetadata &keymeta);
#     238                 :            :     bool EraseWatchOnly(const CScript &script);
#     239                 :            : 
#     240                 :            :     bool WriteBestBlock(const CBlockLocator& locator);
#     241                 :            :     bool ReadBestBlock(CBlockLocator& locator);
#     242                 :            : 
#     243                 :            :     bool WriteOrderPosNext(int64_t nOrderPosNext);
#     244                 :            : 
#     245                 :            :     bool ReadPool(int64_t nPool, CKeyPool& keypool);
#     246                 :            :     bool WritePool(int64_t nPool, const CKeyPool& keypool);
#     247                 :            :     bool ErasePool(int64_t nPool);
#     248                 :            : 
#     249                 :            :     bool WriteMinVersion(int nVersion);
#     250                 :            : 
#     251                 :            :     bool WriteDescriptorKey(const uint256& desc_id, const CPubKey& pubkey, const CPrivKey& privkey);
#     252                 :            :     bool WriteCryptedDescriptorKey(const uint256& desc_id, const CPubKey& pubkey, const std::vector<unsigned char>& secret);
#     253                 :            :     bool WriteDescriptor(const uint256& desc_id, const WalletDescriptor& descriptor);
#     254                 :            :     bool WriteDescriptorDerivedCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index, uint32_t der_index);
#     255                 :            :     bool WriteDescriptorParentCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index);
#     256                 :            :     bool WriteDescriptorLastHardenedCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index);
#     257                 :            :     bool WriteDescriptorCacheItems(const uint256& desc_id, const DescriptorCache& cache);
#     258                 :            : 
#     259                 :            :     bool WriteLockedUTXO(const COutPoint& output);
#     260                 :            :     bool EraseLockedUTXO(const COutPoint& output);
#     261                 :            : 
#     262                 :            :     /// Write destination data key,value tuple to database
#     263                 :            :     bool WriteDestData(const std::string &address, const std::string &key, const std::string &value);
#     264                 :            :     /// Erase destination data tuple from wallet database
#     265                 :            :     bool EraseDestData(const std::string &address, const std::string &key);
#     266                 :            : 
#     267                 :            :     bool WriteActiveScriptPubKeyMan(uint8_t type, const uint256& id, bool internal);
#     268                 :            :     bool EraseActiveScriptPubKeyMan(uint8_t type, bool internal);
#     269                 :            : 
#     270                 :            :     DBErrors LoadWallet(CWallet* pwallet);
#     271                 :            :     DBErrors FindWalletTx(std::vector<uint256>& vTxHash, std::list<CWalletTx>& vWtx);
#     272                 :            :     DBErrors ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256>& vHashOut);
#     273                 :            :     /* Function to determine if a certain KV/key-type is a key (cryptographical key) type */
#     274                 :            :     static bool IsKeyType(const std::string& strType);
#     275                 :            : 
#     276                 :            :     //! write the hdchain model (external chain child index counter)
#     277                 :            :     bool WriteHDChain(const CHDChain& chain);
#     278                 :            : 
#     279                 :            :     bool WriteWalletFlags(const uint64_t flags);
#     280                 :            :     //! Begin a new transaction
#     281                 :            :     bool TxnBegin();
#     282                 :            :     //! Commit current transaction
#     283                 :            :     bool TxnCommit();
#     284                 :            :     //! Abort current transaction
#     285                 :            :     bool TxnAbort();
#     286                 :            : private:
#     287                 :            :     std::unique_ptr<DatabaseBatch> m_batch;
#     288                 :            :     WalletDatabase& m_database;
#     289                 :            : };
#     290                 :            : 
#     291                 :            : //! Compacts BDB state so that wallet.dat is self-contained (if there are changes)
#     292                 :            : void MaybeCompactWalletDB(WalletContext& context);
#     293                 :            : 
#     294                 :            : //! Callback for filtering key types to deserialize in ReadKeyValue
#     295                 :            : using KeyFilterFn = std::function<bool(const std::string&)>;
#     296                 :            : 
#     297                 :            : //! Unserialize a given Key-Value pair and load it into the wallet
#     298                 :            : bool ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, std::string& strType, std::string& strErr, const KeyFilterFn& filter_fn = nullptr);
#     299                 :            : 
#     300                 :            : /** Return object for accessing dummy database with no read/write capabilities. */
#     301                 :            : std::unique_ptr<WalletDatabase> CreateDummyWalletDatabase();
#     302                 :            : 
#     303                 :            : /** Return object for accessing temporary in-memory database. */
#     304                 :            : std::unique_ptr<WalletDatabase> CreateMockWalletDatabase();
#     305                 :            : } // namespace wallet
#     306                 :            : 
#     307                 :            : #endif // BITCOIN_WALLET_WALLETDB_H

Generated by: LCOV version 0-eol-96201-ge66f56f4af6a