/tmp/bitcoin/src/wallet/walletdb.h
Line | Count | Source |
1 | | // Copyright (c) 2009-2010 Satoshi Nakamoto |
2 | | // Copyright (c) 2009-present 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 <key.h> |
10 | | #include <primitives/transaction.h> |
11 | | #include <primitives/transaction_identifier.h> |
12 | | #include <script/sign.h> |
13 | | #include <wallet/db.h> |
14 | | #include <wallet/walletutil.h> |
15 | | |
16 | | #include <cstdint> |
17 | | #include <string> |
18 | | #include <unordered_set> |
19 | | #include <vector> |
20 | | |
21 | | class CScript; |
22 | | class uint160; |
23 | | class uint256; |
24 | | struct CBlockLocator; |
25 | | |
26 | | namespace wallet { |
27 | | class CMasterKey; |
28 | | class CWallet; |
29 | | class CWalletTx; |
30 | | struct WalletContext; |
31 | | |
32 | | // Logs information about the database, including available engines, features, and other capabilities |
33 | | void LogDBInfo(); |
34 | | |
35 | | /** |
36 | | * Overview of wallet database classes: |
37 | | * |
38 | | * - WalletBatch is an abstract modifier object for the wallet database, and encapsulates a database |
39 | | * batch update as well as methods to act on the database. It should be agnostic to the database implementation. |
40 | | */ |
41 | | |
42 | | /** Error statuses for the wallet database. |
43 | | * Values are in order of severity. When multiple errors occur, the most severe (highest value) will be returned. |
44 | | */ |
45 | | enum class DBErrors : int |
46 | | { |
47 | | LOAD_OK = 0, |
48 | | NEED_RESCAN = 1, |
49 | | EXTERNAL_SIGNER_SUPPORT_REQUIRED = 3, |
50 | | NONCRITICAL_ERROR = 4, |
51 | | TOO_NEW = 5, |
52 | | UNKNOWN_DESCRIPTOR = 6, |
53 | | LOAD_FAIL = 7, |
54 | | UNEXPECTED_LEGACY_ENTRY = 8, |
55 | | LEGACY_WALLET = 9, |
56 | | CORRUPT = 10, |
57 | | }; |
58 | | |
59 | | namespace DBKeys { |
60 | | extern const std::string ACENTRY; |
61 | | extern const std::string ACTIVEEXTERNALSPK; |
62 | | extern const std::string ACTIVEINTERNALSPK; |
63 | | extern const std::string BESTBLOCK; |
64 | | extern const std::string BESTBLOCK_NOMERKLE; |
65 | | extern const std::string CRYPTED_KEY; |
66 | | extern const std::string CSCRIPT; |
67 | | extern const std::string DEFAULTKEY; |
68 | | extern const std::string DESTDATA; |
69 | | extern const std::string FLAGS; |
70 | | extern const std::string HDCHAIN; |
71 | | extern const std::string KEY; |
72 | | extern const std::string KEYMETA; |
73 | | extern const std::string LOCKED_UTXO; |
74 | | extern const std::string MASTER_KEY; |
75 | | extern const std::string MINVERSION; |
76 | | extern const std::string NAME; |
77 | | extern const std::string OLD_KEY; |
78 | | extern const std::string ORDERPOSNEXT; |
79 | | extern const std::string POOL; |
80 | | extern const std::string PURPOSE; |
81 | | extern const std::string SETTINGS; |
82 | | extern const std::string TX; |
83 | | extern const std::string WTX_VARIANT; |
84 | | extern const std::string VERSION; |
85 | | extern const std::string WALLETDESCRIPTOR; |
86 | | extern const std::string WALLETDESCRIPTORCKEY; |
87 | | extern const std::string WALLETDESCRIPTORKEY; |
88 | | extern const std::string WATCHMETA; |
89 | | extern const std::string WATCHS; |
90 | | |
91 | | // Keys in this set pertain only to the legacy wallet (LegacyScriptPubKeyMan) and are removed during migration from legacy to descriptors. |
92 | | extern const std::unordered_set<std::string> LEGACY_TYPES; |
93 | | } // namespace DBKeys |
94 | | |
95 | | /* simple HD chain data model */ |
96 | | class CHDChain |
97 | | { |
98 | | public: |
99 | | uint32_t nExternalChainCounter; |
100 | | uint32_t nInternalChainCounter; |
101 | | CKeyID seed_id; //!< seed hash160 |
102 | | int64_t m_next_external_index{0}; // Next index in the keypool to be used. Memory only. |
103 | | int64_t m_next_internal_index{0}; // Next index in the keypool to be used. Memory only. |
104 | | |
105 | | static constexpr int VERSION_HD_BASE{1}; |
106 | | static constexpr int VERSION_HD_CHAIN_SPLIT{2}; |
107 | | static constexpr int CURRENT_VERSION{VERSION_HD_CHAIN_SPLIT}; |
108 | | int nVersion; |
109 | | |
110 | 305 | CHDChain() { SetNull(); } |
111 | | |
112 | | SERIALIZE_METHODS(CHDChain, obj) |
113 | 36 | { |
114 | 36 | READWRITE(obj.nVersion, obj.nExternalChainCounter, obj.seed_id); |
115 | 36 | if (obj.nVersion >= VERSION_HD_CHAIN_SPLIT) { |
116 | 36 | READWRITE(obj.nInternalChainCounter); |
117 | 36 | } |
118 | 36 | } |
119 | | |
120 | | void SetNull() |
121 | 305 | { |
122 | 305 | nVersion = CHDChain::CURRENT_VERSION; |
123 | 305 | nExternalChainCounter = 0; |
124 | 305 | nInternalChainCounter = 0; |
125 | 305 | seed_id.SetNull(); |
126 | 305 | } |
127 | | |
128 | | bool operator==(const CHDChain& chain) const |
129 | 0 | { |
130 | 0 | return seed_id == chain.seed_id; |
131 | 0 | } |
132 | | bool operator<(const CHDChain& chain) const |
133 | 7 | { |
134 | 7 | return seed_id < chain.seed_id; |
135 | 7 | } |
136 | | }; |
137 | | |
138 | | class CKeyMetadata |
139 | | { |
140 | | public: |
141 | | static constexpr int VERSION_BASIC{1}; |
142 | | static constexpr int VERSION_WITH_HDDATA{10}; |
143 | | static constexpr int VERSION_WITH_KEY_ORIGIN{12}; |
144 | | static constexpr int CURRENT_VERSION{VERSION_WITH_KEY_ORIGIN}; |
145 | | int nVersion; |
146 | | int64_t nCreateTime; // 0 means unknown |
147 | | std::string hdKeypath; //optional HD/bip32 keypath. Still used to determine whether a key is a seed. Also kept for backwards compatibility |
148 | | CKeyID hd_seed_id; //id of the HD seed used to derive this key |
149 | | KeyOriginInfo key_origin; // Key origin info with path and fingerprint |
150 | | bool has_key_origin = false; //!< Whether the key_origin is useful |
151 | | |
152 | | CKeyMetadata() |
153 | 1.22k | { |
154 | 1.22k | SetNull(); |
155 | 1.22k | } |
156 | | explicit CKeyMetadata(int64_t nCreateTime_) |
157 | 0 | { |
158 | 0 | SetNull(); |
159 | 0 | nCreateTime = nCreateTime_; |
160 | 0 | } |
161 | | |
162 | | SERIALIZE_METHODS(CKeyMetadata, obj) |
163 | 277 | { |
164 | 277 | READWRITE(obj.nVersion, obj.nCreateTime); |
165 | 277 | if (obj.nVersion >= VERSION_WITH_HDDATA) { |
166 | 277 | READWRITE(obj.hdKeypath, obj.hd_seed_id); |
167 | 277 | } |
168 | 277 | if (obj.nVersion >= VERSION_WITH_KEY_ORIGIN) |
169 | 277 | { |
170 | 277 | READWRITE(obj.key_origin); |
171 | 277 | READWRITE(obj.has_key_origin); |
172 | 277 | } |
173 | 277 | } void wallet::CKeyMetadata::SerializationOps<DataStream, wallet::CKeyMetadata, ActionUnserialize>(wallet::CKeyMetadata&, DataStream&, ActionUnserialize) Line | Count | Source | 163 | 277 | { | 164 | 277 | READWRITE(obj.nVersion, obj.nCreateTime); | 165 | 277 | if (obj.nVersion >= VERSION_WITH_HDDATA) { | 166 | 277 | READWRITE(obj.hdKeypath, obj.hd_seed_id); | 167 | 277 | } | 168 | 277 | if (obj.nVersion >= VERSION_WITH_KEY_ORIGIN) | 169 | 277 | { | 170 | 277 | READWRITE(obj.key_origin); | 171 | 277 | READWRITE(obj.has_key_origin); | 172 | 277 | } | 173 | 277 | } |
Unexecuted instantiation: void wallet::CKeyMetadata::SerializationOps<DataStream, wallet::CKeyMetadata const, ActionSerialize>(wallet::CKeyMetadata const&, DataStream&, ActionSerialize) |
174 | | |
175 | | void SetNull() |
176 | 1.22k | { |
177 | 1.22k | nVersion = CKeyMetadata::CURRENT_VERSION; |
178 | 1.22k | nCreateTime = 0; |
179 | 1.22k | hdKeypath.clear(); |
180 | 1.22k | hd_seed_id.SetNull(); |
181 | 1.22k | key_origin.clear(); |
182 | 1.22k | has_key_origin = false; |
183 | 1.22k | } |
184 | | }; |
185 | | |
186 | | struct DbTxnListener |
187 | | { |
188 | | std::function<void()> on_commit, on_abort; |
189 | | }; |
190 | | |
191 | | /** Access to the wallet database. |
192 | | * Opens the database and provides read and write access to it. Each read and write is its own transaction. |
193 | | * Multiple operation transactions can be started using TxnBegin() and committed using TxnCommit() |
194 | | * Otherwise the transaction will be committed when the object goes out of scope. |
195 | | * Optionally (on by default) it will flush to disk on close. |
196 | | * Every 1000 writes will automatically trigger a flush to disk. |
197 | | */ |
198 | | class WalletBatch |
199 | | { |
200 | | private: |
201 | | template <typename K, typename T> |
202 | | bool WriteIC(const K& key, const T& value, bool fOverwrite = true) |
203 | 293k | { |
204 | 293k | if (!m_batch->Write(key, value, fOverwrite)) { |
205 | 0 | return false; |
206 | 0 | } |
207 | 293k | return true; |
208 | 293k | } bool wallet::WalletBatch::WriteIC<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>(std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, bool) Line | Count | Source | 203 | 56.2k | { | 204 | 56.2k | if (!m_batch->Write(key, value, fOverwrite)) { | 205 | 0 | return false; | 206 | 0 | } | 207 | 56.2k | return true; | 208 | 56.2k | } |
bool wallet::WalletBatch::WriteIC<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, transaction_identifier<false>>, wallet::CWalletTx>(std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, transaction_identifier<false>> const&, wallet::CWalletTx const&, bool) Line | Count | Source | 203 | 23.0k | { | 204 | 23.0k | if (!m_batch->Write(key, value, fOverwrite)) { | 205 | 0 | return false; | 206 | 0 | } | 207 | 23.0k | return true; | 208 | 23.0k | } |
bool wallet::WalletBatch::WriteIC<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::pair<transaction_identifier<false>, transaction_identifier<true>>>, ParamsWrapper<TransactionSerParams, std::shared_ptr<CTransaction const> const>>(std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::pair<transaction_identifier<false>, transaction_identifier<true>>> const&, ParamsWrapper<TransactionSerParams, std::shared_ptr<CTransaction const> const> const&, bool) Line | Count | Source | 203 | 23.0k | { | 204 | 23.0k | if (!m_batch->Write(key, value, fOverwrite)) { | 205 | 0 | return false; | 206 | 0 | } | 207 | 23.0k | return true; | 208 | 23.0k | } |
Unexecuted instantiation: bool wallet::WalletBatch::WriteIC<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, CPubKey>, wallet::CKeyMetadata>(std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, CPubKey> const&, wallet::CKeyMetadata const&, bool) Unexecuted instantiation: bool wallet::WalletBatch::WriteIC<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, CPubKey>, std::pair<std::vector<unsigned char, secure_allocator<unsigned char>>, uint256>>(std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, CPubKey> const&, std::pair<std::vector<unsigned char, secure_allocator<unsigned char>>, uint256> const&, bool) Unexecuted instantiation: bool wallet::WalletBatch::WriteIC<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, CPubKey>, std::pair<std::vector<unsigned char, std::allocator<unsigned char>>, uint256>>(std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, CPubKey> const&, std::pair<std::vector<unsigned char, std::allocator<unsigned char>>, uint256> const&, bool) bool wallet::WalletBatch::WriteIC<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, unsigned int>, wallet::CMasterKey>(std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, unsigned int> const&, wallet::CMasterKey const&, bool) Line | Count | Source | 203 | 27 | { | 204 | 27 | if (!m_batch->Write(key, value, fOverwrite)) { | 205 | 0 | return false; | 206 | 0 | } | 207 | 27 | return true; | 208 | 27 | } |
Unexecuted instantiation: bool wallet::WalletBatch::WriteIC<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, CScript>, wallet::CKeyMetadata>(std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, CScript> const&, wallet::CKeyMetadata const&, bool) Unexecuted instantiation: bool wallet::WalletBatch::WriteIC<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, CScript>, unsigned char>(std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, CScript> const&, unsigned char const&, bool) bool wallet::WalletBatch::WriteIC<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, CBlockLocator>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, CBlockLocator const&, bool) Line | Count | Source | 203 | 25.1k | { | 204 | 25.1k | if (!m_batch->Write(key, value, fOverwrite)) { | 205 | 0 | return false; | 206 | 0 | } | 207 | 25.1k | return true; | 208 | 25.1k | } |
bool wallet::WalletBatch::WriteIC<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, long>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, long const&, bool) Line | Count | Source | 203 | 17.4k | { | 204 | 17.4k | if (!m_batch->Write(key, value, fOverwrite)) { | 205 | 0 | return false; | 206 | 0 | } | 207 | 17.4k | return true; | 208 | 17.4k | } |
bool wallet::WalletBatch::WriteIC<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, unsigned char>, uint256>(std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, unsigned char> const&, uint256 const&, bool) Line | Count | Source | 203 | 4.26k | { | 204 | 4.26k | if (!m_batch->Write(key, value, fOverwrite)) { | 205 | 0 | return false; | 206 | 0 | } | 207 | 4.26k | return true; | 208 | 4.26k | } |
bool wallet::WalletBatch::WriteIC<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::pair<uint256, CPubKey>>, std::pair<std::vector<unsigned char, secure_allocator<unsigned char>>, uint256>>(std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::pair<uint256, CPubKey>> const&, std::pair<std::vector<unsigned char, secure_allocator<unsigned char>>, uint256> const&, bool) Line | Count | Source | 203 | 4.42k | { | 204 | 4.42k | if (!m_batch->Write(key, value, fOverwrite)) { | 205 | 0 | return false; | 206 | 0 | } | 207 | 4.42k | return true; | 208 | 4.42k | } |
bool wallet::WalletBatch::WriteIC<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::pair<uint256, CPubKey>>, std::vector<unsigned char, std::allocator<unsigned char>>>(std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::pair<uint256, CPubKey>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, bool) Line | Count | Source | 203 | 273 | { | 204 | 273 | if (!m_batch->Write(key, value, fOverwrite)) { | 205 | 0 | return false; | 206 | 0 | } | 207 | 273 | return true; | 208 | 273 | } |
bool wallet::WalletBatch::WriteIC<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, uint256>, wallet::WalletDescriptor>(std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, uint256> const&, wallet::WalletDescriptor const&, bool) Line | Count | Source | 203 | 101k | { | 204 | 101k | if (!m_batch->Write(key, value, fOverwrite)) { | 205 | 0 | return false; | 206 | 0 | } | 207 | 101k | return true; | 208 | 101k | } |
bool wallet::WalletBatch::WriteIC<std::pair<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, uint256>, std::pair<unsigned int, unsigned int>>, std::vector<unsigned char, std::allocator<unsigned char>>>(std::pair<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, uint256>, std::pair<unsigned int, unsigned int>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, bool) Line | Count | Source | 203 | 24.0k | { | 204 | 24.0k | if (!m_batch->Write(key, value, fOverwrite)) { | 205 | 0 | return false; | 206 | 0 | } | 207 | 24.0k | return true; | 208 | 24.0k | } |
bool wallet::WalletBatch::WriteIC<std::pair<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, uint256>, unsigned int>, std::vector<unsigned char, std::allocator<unsigned char>>>(std::pair<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, uint256>, unsigned int> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, bool) Line | Count | Source | 203 | 9.29k | { | 204 | 9.29k | if (!m_batch->Write(key, value, fOverwrite)) { | 205 | 0 | return false; | 206 | 0 | } | 207 | 9.29k | return true; | 208 | 9.29k | } |
bool wallet::WalletBatch::WriteIC<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::pair<transaction_identifier<false>, unsigned int>>, unsigned char>(std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::pair<transaction_identifier<false>, unsigned int>> const&, unsigned char const&, bool) Line | Count | Source | 203 | 3 | { | 204 | 3 | if (!m_batch->Write(key, value, fOverwrite)) { | 205 | 0 | return false; | 206 | 0 | } | 207 | 3 | return true; | 208 | 3 | } |
bool wallet::WalletBatch::WriteIC<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>(std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, bool) Line | Count | Source | 203 | 29 | { | 204 | 29 | if (!m_batch->Write(key, value, fOverwrite)) { | 205 | 0 | return false; | 206 | 0 | } | 207 | 29 | return true; | 208 | 29 | } |
bool wallet::WalletBatch::WriteIC<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, unsigned long>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, unsigned long const&, bool) Line | Count | Source | 203 | 4.57k | { | 204 | 4.57k | if (!m_batch->Write(key, value, fOverwrite)) { | 205 | 0 | return false; | 206 | 0 | } | 207 | 4.57k | return true; | 208 | 4.57k | } |
|
209 | | |
210 | | template <typename K> |
211 | | bool EraseIC(const K& key) |
212 | 349 | { |
213 | 349 | if (!m_batch->Erase(key)) { |
214 | 0 | return false; |
215 | 0 | } |
216 | 349 | return true; |
217 | 349 | } bool wallet::WalletBatch::EraseIC<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>(std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>> const&) Line | Count | Source | 212 | 56 | { | 213 | 56 | if (!m_batch->Erase(key)) { | 214 | 0 | return false; | 215 | 0 | } | 216 | 56 | return true; | 217 | 56 | } |
bool wallet::WalletBatch::EraseIC<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, uint256>>(std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, uint256> const&) Line | Count | Source | 212 | 15 | { | 213 | 15 | if (!m_batch->Erase(key)) { | 214 | 0 | return false; | 215 | 0 | } | 216 | 15 | return true; | 217 | 15 | } |
Unexecuted instantiation: bool wallet::WalletBatch::EraseIC<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, CPubKey>>(std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, CPubKey> const&) bool wallet::WalletBatch::EraseIC<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, unsigned int>>(std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, unsigned int> const&) Line | Count | Source | 212 | 1 | { | 213 | 1 | if (!m_batch->Erase(key)) { | 214 | 0 | return false; | 215 | 0 | } | 216 | 1 | return true; | 217 | 1 | } |
Unexecuted instantiation: bool wallet::WalletBatch::EraseIC<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, CScript>>(std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, CScript> const&) bool wallet::WalletBatch::EraseIC<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, unsigned char>>(std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, unsigned char> const&) Line | Count | Source | 212 | 1 | { | 213 | 1 | if (!m_batch->Erase(key)) { | 214 | 0 | return false; | 215 | 0 | } | 216 | 1 | return true; | 217 | 1 | } |
bool wallet::WalletBatch::EraseIC<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::pair<uint256, CPubKey>>>(std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::pair<uint256, CPubKey>> const&) Line | Count | Source | 212 | 273 | { | 213 | 273 | if (!m_batch->Erase(key)) { | 214 | 0 | return false; | 215 | 0 | } | 216 | 273 | return true; | 217 | 273 | } |
bool wallet::WalletBatch::EraseIC<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::pair<transaction_identifier<false>, unsigned int>>>(std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::pair<transaction_identifier<false>, unsigned int>> const&) Line | Count | Source | 212 | 1 | { | 213 | 1 | if (!m_batch->Erase(key)) { | 214 | 0 | return false; | 215 | 0 | } | 216 | 1 | return true; | 217 | 1 | } |
bool wallet::WalletBatch::EraseIC<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>>(std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>> const&) Line | Count | Source | 212 | 2 | { | 213 | 2 | if (!m_batch->Erase(key)) { | 214 | 0 | return false; | 215 | 0 | } | 216 | 2 | return true; | 217 | 2 | } |
|
218 | | |
219 | | public: |
220 | | explicit WalletBatch(WalletDatabase &database) : |
221 | 167k | m_batch(database.MakeBatch()) |
222 | 167k | { |
223 | 167k | } |
224 | | WalletBatch(const WalletBatch&) = delete; |
225 | | WalletBatch& operator=(const WalletBatch&) = delete; |
226 | | |
227 | | bool WriteName(const std::string& strAddress, const std::string& strName); |
228 | | bool EraseName(const std::string& strAddress); |
229 | | |
230 | | bool WritePurpose(const std::string& strAddress, const std::string& purpose); |
231 | | bool ErasePurpose(const std::string& strAddress); |
232 | | |
233 | | bool WriteTx(const CWalletTx& wtx); |
234 | | bool EraseTx(Txid hash); |
235 | | bool WriteWtxVariant(const Txid& txid, const CTransactionRef& tx); |
236 | | |
237 | | bool WriteKeyMetadata(const CKeyMetadata& meta, const CPubKey& pubkey, bool overwrite); |
238 | | bool WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata &keyMeta); |
239 | | bool WriteCryptedKey(const CPubKey& vchPubKey, const std::vector<unsigned char>& vchCryptedSecret, const CKeyMetadata &keyMeta); |
240 | | bool WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey); |
241 | | bool EraseMasterKey(unsigned int id); |
242 | | |
243 | | bool WriteWatchOnly(const CScript &script, const CKeyMetadata &keymeta); |
244 | | bool EraseWatchOnly(const CScript &script); |
245 | | |
246 | | bool WriteBestBlock(const CBlockLocator& locator); |
247 | | bool ReadBestBlock(CBlockLocator& locator); |
248 | | |
249 | | // Returns true if wallet stores encryption keys |
250 | | bool IsEncrypted(); |
251 | | |
252 | | bool WriteOrderPosNext(int64_t nOrderPosNext); |
253 | | |
254 | | bool WriteDescriptorKey(const uint256& desc_id, const CPubKey& pubkey, const CPrivKey& privkey); |
255 | | bool WriteCryptedDescriptorKey(const uint256& desc_id, const CPubKey& pubkey, const std::vector<unsigned char>& secret); |
256 | | bool WriteDescriptor(const uint256& desc_id, const WalletDescriptor& descriptor); |
257 | | bool WriteDescriptorDerivedCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index, uint32_t der_index); |
258 | | bool WriteDescriptorParentCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index); |
259 | | bool WriteDescriptorLastHardenedCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index); |
260 | | bool WriteDescriptorCacheItems(const uint256& desc_id, const DescriptorCache& cache); |
261 | | |
262 | | bool WriteLockedUTXO(const COutPoint& output); |
263 | | bool EraseLockedUTXO(const COutPoint& output); |
264 | | |
265 | | bool WriteAddressPreviouslySpent(const CTxDestination& dest, bool previously_spent); |
266 | | bool WriteAddressReceiveRequest(const CTxDestination& dest, const std::string& id, const std::string& receive_request); |
267 | | bool EraseAddressReceiveRequest(const CTxDestination& dest, const std::string& id); |
268 | | bool EraseAddressData(const CTxDestination& dest); |
269 | | |
270 | | bool WriteActiveScriptPubKeyMan(uint8_t type, const uint256& id, bool internal); |
271 | | bool EraseActiveScriptPubKeyMan(uint8_t type, bool internal); |
272 | | |
273 | | DBErrors LoadWallet(CWallet* pwallet); |
274 | | |
275 | | //! Write the given client_version. |
276 | 734 | bool WriteVersion(int client_version) { return m_batch->Write(DBKeys::VERSION, CLIENT_VERSION); } |
277 | | |
278 | | //! Delete records of the given types |
279 | | bool EraseRecords(const std::unordered_set<std::string>& types); |
280 | | |
281 | | bool WriteWalletFlags(uint64_t flags); |
282 | | //! Begin a new transaction |
283 | | bool TxnBegin(); |
284 | | //! Commit current transaction |
285 | | bool TxnCommit(); |
286 | | //! Abort current transaction |
287 | | bool TxnAbort(); |
288 | 11 | bool HasActiveTxn() { return m_batch->HasActiveTxn(); } |
289 | | |
290 | | //! Registers db txn callback functions |
291 | | void RegisterTxnListener(const DbTxnListener& l); |
292 | | |
293 | | private: |
294 | | std::unique_ptr<DatabaseBatch> m_batch; |
295 | | |
296 | | // External functions listening to the current db txn outcome. |
297 | | // Listeners are cleared at the end of the transaction. |
298 | | std::vector<DbTxnListener> m_txn_listeners; |
299 | | }; |
300 | | |
301 | | /** |
302 | | * Executes the provided function 'func' within a database transaction context. |
303 | | * |
304 | | * This function ensures that all db modifications performed within 'func()' are |
305 | | * atomically committed to the db at the end of the process. And, in case of a |
306 | | * failure during execution, all performed changes are rolled back. |
307 | | * |
308 | | * @param database The db connection instance to perform the transaction on. |
309 | | * @param process_desc A description of the process being executed, used for logging purposes in the event of a failure. |
310 | | * @param func The function to be executed within the db txn context. It returns a boolean indicating whether to commit or roll back the txn. |
311 | | * @return true if the db txn executed successfully, false otherwise. |
312 | | */ |
313 | | bool RunWithinTxn(WalletDatabase& database, std::string_view process_desc, const std::function<bool(WalletBatch&)>& func); |
314 | | |
315 | | bool LoadKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr); |
316 | | bool LoadCryptedKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr); |
317 | | bool LoadEncryptionKey(CWallet* pwallet, DataStream& ssKey, DataStream& ssValue, std::string& strErr); |
318 | | bool LoadHDChain(CWallet* pwallet, DataStream& ssValue, std::string& strErr); |
319 | | |
320 | | //! Returns true if there are any DBKeys::LEGACY_TYPES record in the wallet db |
321 | | bool HasLegacyRecords(CWallet& wallet); |
322 | | bool HasLegacyRecords(CWallet& wallet, DatabaseBatch& batch); |
323 | | } // namespace wallet |
324 | | |
325 | | #endif // BITCOIN_WALLET_WALLETDB_H |