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