2012-04-15 18:39:49 -03:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2020-04-16 13:14:08 -04:00
|
|
|
// Copyright (c) 2009-2020 The Bitcoin Core developers
|
2014-12-13 01:09:33 -03:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-05-18 10:02:28 -04:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2012-04-15 18:39:49 -03:00
|
|
|
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <wallet/walletdb.h>
|
|
|
|
|
|
|
|
#include <fs.h>
|
2017-09-19 22:12:25 -03:00
|
|
|
#include <key_io.h>
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <protocol.h>
|
|
|
|
#include <serialize.h>
|
|
|
|
#include <sync.h>
|
2019-12-05 17:23:05 -03:00
|
|
|
#include <util/bip32.h>
|
2018-10-22 19:51:11 -03:00
|
|
|
#include <util/system.h>
|
|
|
|
#include <util/time.h>
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <wallet/wallet.h>
|
2013-04-13 02:13:08 -03:00
|
|
|
|
2016-11-27 00:32:30 -03:00
|
|
|
#include <atomic>
|
2018-07-31 12:23:26 -04:00
|
|
|
#include <string>
|
2016-11-27 00:32:30 -03:00
|
|
|
|
2019-07-27 15:50:36 -04:00
|
|
|
namespace DBKeys {
|
|
|
|
const std::string ACENTRY{"acentry"};
|
2019-07-08 15:41:31 -04:00
|
|
|
const std::string ACTIVEEXTERNALSPK{"activeexternalspk"};
|
|
|
|
const std::string ACTIVEINTERNALSPK{"activeinternalspk"};
|
2019-07-27 15:50:36 -04:00
|
|
|
const std::string BESTBLOCK_NOMERKLE{"bestblock_nomerkle"};
|
|
|
|
const std::string BESTBLOCK{"bestblock"};
|
|
|
|
const std::string CRYPTED_KEY{"ckey"};
|
|
|
|
const std::string CSCRIPT{"cscript"};
|
|
|
|
const std::string DEFAULTKEY{"defaultkey"};
|
|
|
|
const std::string DESTDATA{"destdata"};
|
|
|
|
const std::string FLAGS{"flags"};
|
|
|
|
const std::string HDCHAIN{"hdchain"};
|
|
|
|
const std::string KEYMETA{"keymeta"};
|
|
|
|
const std::string KEY{"key"};
|
|
|
|
const std::string MASTER_KEY{"mkey"};
|
|
|
|
const std::string MINVERSION{"minversion"};
|
|
|
|
const std::string NAME{"name"};
|
|
|
|
const std::string OLD_KEY{"wkey"};
|
|
|
|
const std::string ORDERPOSNEXT{"orderposnext"};
|
|
|
|
const std::string POOL{"pool"};
|
|
|
|
const std::string PURPOSE{"purpose"};
|
2019-07-27 08:07:30 -04:00
|
|
|
const std::string SETTINGS{"settings"};
|
2019-07-27 15:50:36 -04:00
|
|
|
const std::string TX{"tx"};
|
|
|
|
const std::string VERSION{"version"};
|
2019-07-08 15:41:31 -04:00
|
|
|
const std::string WALLETDESCRIPTOR{"walletdescriptor"};
|
2019-07-09 13:40:34 -04:00
|
|
|
const std::string WALLETDESCRIPTORCACHE{"walletdescriptorcache"};
|
2019-07-09 18:23:51 -04:00
|
|
|
const std::string WALLETDESCRIPTORCKEY{"walletdescriptorckey"};
|
|
|
|
const std::string WALLETDESCRIPTORKEY{"walletdescriptorkey"};
|
2019-07-27 15:50:36 -04:00
|
|
|
const std::string WATCHMETA{"watchmeta"};
|
|
|
|
const std::string WATCHS{"watchs"};
|
|
|
|
} // namespace DBKeys
|
|
|
|
|
2012-04-15 18:39:49 -03:00
|
|
|
//
|
2017-12-08 08:39:22 -03:00
|
|
|
// WalletBatch
|
2012-04-15 18:39:49 -03:00
|
|
|
//
|
|
|
|
|
2017-12-08 08:39:22 -03:00
|
|
|
bool WalletBatch::WriteName(const std::string& strAddress, const std::string& strName)
|
2012-04-15 18:39:49 -03:00
|
|
|
{
|
2019-07-27 15:50:36 -04:00
|
|
|
return WriteIC(std::make_pair(DBKeys::NAME, strAddress), strName);
|
2012-04-15 18:39:49 -03:00
|
|
|
}
|
|
|
|
|
2017-12-08 08:39:22 -03:00
|
|
|
bool WalletBatch::EraseName(const std::string& strAddress)
|
2012-04-15 18:39:49 -03:00
|
|
|
{
|
|
|
|
// This should only be used for sending addresses, never for receiving addresses,
|
|
|
|
// receiving addresses must always have an address book entry if they're not change return.
|
2019-07-27 15:50:36 -04:00
|
|
|
return EraseIC(std::make_pair(DBKeys::NAME, strAddress));
|
2012-04-15 18:39:49 -03:00
|
|
|
}
|
|
|
|
|
2017-12-08 08:39:22 -03:00
|
|
|
bool WalletBatch::WritePurpose(const std::string& strAddress, const std::string& strPurpose)
|
2013-07-22 02:50:39 -04:00
|
|
|
{
|
2019-07-27 15:50:36 -04:00
|
|
|
return WriteIC(std::make_pair(DBKeys::PURPOSE, strAddress), strPurpose);
|
2013-07-22 02:50:39 -04:00
|
|
|
}
|
|
|
|
|
2017-12-08 08:39:22 -03:00
|
|
|
bool WalletBatch::ErasePurpose(const std::string& strAddress)
|
2013-07-22 02:50:39 -04:00
|
|
|
{
|
2019-07-27 15:50:36 -04:00
|
|
|
return EraseIC(std::make_pair(DBKeys::PURPOSE, strAddress));
|
2013-07-22 02:50:39 -04:00
|
|
|
}
|
|
|
|
|
2017-12-08 08:39:22 -03:00
|
|
|
bool WalletBatch::WriteTx(const CWalletTx& wtx)
|
2013-04-13 02:13:08 -03:00
|
|
|
{
|
2019-07-27 15:50:36 -04:00
|
|
|
return WriteIC(std::make_pair(DBKeys::TX, wtx.GetHash()), wtx);
|
2013-04-13 02:13:08 -03:00
|
|
|
}
|
|
|
|
|
2017-12-08 08:39:22 -03:00
|
|
|
bool WalletBatch::EraseTx(uint256 hash)
|
2013-04-13 02:13:08 -03:00
|
|
|
{
|
2019-07-27 15:50:36 -04:00
|
|
|
return EraseIC(std::make_pair(DBKeys::TX, hash));
|
2013-04-13 02:13:08 -03:00
|
|
|
}
|
|
|
|
|
2018-11-06 11:23:28 -03:00
|
|
|
bool WalletBatch::WriteKeyMetadata(const CKeyMetadata& meta, const CPubKey& pubkey, const bool overwrite)
|
|
|
|
{
|
2019-07-27 15:50:36 -04:00
|
|
|
return WriteIC(std::make_pair(DBKeys::KEYMETA, pubkey), meta, overwrite);
|
2018-11-06 11:23:28 -03:00
|
|
|
}
|
|
|
|
|
2017-12-08 08:39:22 -03:00
|
|
|
bool WalletBatch::WriteKey(const CPubKey& vchPubKey, const CPrivKey& vchPrivKey, const CKeyMetadata& keyMeta)
|
2013-04-13 02:13:08 -03:00
|
|
|
{
|
2018-11-06 11:23:28 -03:00
|
|
|
if (!WriteKeyMetadata(keyMeta, vchPubKey, false)) {
|
2013-04-13 02:13:08 -03:00
|
|
|
return false;
|
2017-06-05 18:23:20 -04:00
|
|
|
}
|
2013-04-13 02:13:08 -03:00
|
|
|
|
|
|
|
// hash pubkey/privkey to accelerate wallet load
|
|
|
|
std::vector<unsigned char> vchKey;
|
|
|
|
vchKey.reserve(vchPubKey.size() + vchPrivKey.size());
|
|
|
|
vchKey.insert(vchKey.end(), vchPubKey.begin(), vchPubKey.end());
|
|
|
|
vchKey.insert(vchKey.end(), vchPrivKey.begin(), vchPrivKey.end());
|
|
|
|
|
2019-07-27 15:50:36 -04:00
|
|
|
return WriteIC(std::make_pair(DBKeys::KEY, vchPubKey), std::make_pair(vchPrivKey, Hash(vchKey.begin(), vchKey.end())), false);
|
2013-04-13 02:13:08 -03:00
|
|
|
}
|
|
|
|
|
2017-12-08 08:39:22 -03:00
|
|
|
bool WalletBatch::WriteCryptedKey(const CPubKey& vchPubKey,
|
2013-11-15 08:24:34 -03:00
|
|
|
const std::vector<unsigned char>& vchCryptedSecret,
|
2013-04-13 02:13:08 -03:00
|
|
|
const CKeyMetadata &keyMeta)
|
|
|
|
{
|
2018-11-06 11:23:28 -03:00
|
|
|
if (!WriteKeyMetadata(keyMeta, vchPubKey, true)) {
|
2013-04-13 02:13:08 -03:00
|
|
|
return false;
|
2017-06-05 18:23:20 -04:00
|
|
|
}
|
2013-04-13 02:13:08 -03:00
|
|
|
|
2019-09-23 19:10:13 -03:00
|
|
|
// Compute a checksum of the encrypted key
|
|
|
|
uint256 checksum = Hash(vchCryptedSecret.begin(), vchCryptedSecret.end());
|
|
|
|
|
|
|
|
const auto key = std::make_pair(DBKeys::CRYPTED_KEY, vchPubKey);
|
|
|
|
if (!WriteIC(key, std::make_pair(vchCryptedSecret, checksum), false)) {
|
2019-12-03 21:06:15 -03:00
|
|
|
// It may already exist, so try writing just the checksum
|
|
|
|
std::vector<unsigned char> val;
|
2020-06-15 16:54:58 -04:00
|
|
|
if (!m_batch->Read(key, val)) {
|
2019-12-03 21:06:15 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!WriteIC(key, std::make_pair(val, checksum), true)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-06-05 18:23:20 -04:00
|
|
|
}
|
2019-07-27 15:50:36 -04:00
|
|
|
EraseIC(std::make_pair(DBKeys::KEY, vchPubKey));
|
2013-04-13 02:13:08 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-08 08:39:22 -03:00
|
|
|
bool WalletBatch::WriteMasterKey(unsigned int nID, const CMasterKey& kMasterKey)
|
2013-04-13 02:13:08 -03:00
|
|
|
{
|
2019-07-27 15:50:36 -04:00
|
|
|
return WriteIC(std::make_pair(DBKeys::MASTER_KEY, nID), kMasterKey, true);
|
2013-04-13 02:13:08 -03:00
|
|
|
}
|
|
|
|
|
2017-12-08 08:39:22 -03:00
|
|
|
bool WalletBatch::WriteCScript(const uint160& hash, const CScript& redeemScript)
|
2013-04-13 02:13:08 -03:00
|
|
|
{
|
2019-07-27 15:50:36 -04:00
|
|
|
return WriteIC(std::make_pair(DBKeys::CSCRIPT, hash), redeemScript, false);
|
2013-04-13 02:13:08 -03:00
|
|
|
}
|
|
|
|
|
2017-12-08 08:39:22 -03:00
|
|
|
bool WalletBatch::WriteWatchOnly(const CScript &dest, const CKeyMetadata& keyMeta)
|
2013-07-25 19:06:01 -04:00
|
|
|
{
|
2019-07-27 15:50:36 -04:00
|
|
|
if (!WriteIC(std::make_pair(DBKeys::WATCHMETA, dest), keyMeta)) {
|
2016-11-08 18:55:02 -03:00
|
|
|
return false;
|
2017-06-05 18:23:20 -04:00
|
|
|
}
|
2019-07-27 15:50:36 -04:00
|
|
|
return WriteIC(std::make_pair(DBKeys::WATCHS, dest), '1');
|
2013-07-25 19:06:01 -04:00
|
|
|
}
|
|
|
|
|
2017-12-08 08:39:22 -03:00
|
|
|
bool WalletBatch::EraseWatchOnly(const CScript &dest)
|
2014-07-26 15:05:11 -04:00
|
|
|
{
|
2019-07-27 15:50:36 -04:00
|
|
|
if (!EraseIC(std::make_pair(DBKeys::WATCHMETA, dest))) {
|
2016-11-08 18:55:02 -03:00
|
|
|
return false;
|
2017-06-05 18:23:20 -04:00
|
|
|
}
|
2019-07-27 15:50:36 -04:00
|
|
|
return EraseIC(std::make_pair(DBKeys::WATCHS, dest));
|
2014-07-26 15:05:11 -04:00
|
|
|
}
|
|
|
|
|
2017-12-08 08:39:22 -03:00
|
|
|
bool WalletBatch::WriteBestBlock(const CBlockLocator& locator)
|
2013-04-13 02:13:08 -03:00
|
|
|
{
|
2019-07-27 15:50:36 -04:00
|
|
|
WriteIC(DBKeys::BESTBLOCK, CBlockLocator()); // Write empty block locator so versions that require a merkle branch automatically rescan
|
|
|
|
return WriteIC(DBKeys::BESTBLOCK_NOMERKLE, locator);
|
2013-04-13 02:13:08 -03:00
|
|
|
}
|
|
|
|
|
2017-12-08 08:39:22 -03:00
|
|
|
bool WalletBatch::ReadBestBlock(CBlockLocator& locator)
|
2013-04-13 02:13:08 -03:00
|
|
|
{
|
2020-06-15 16:54:58 -04:00
|
|
|
if (m_batch->Read(DBKeys::BESTBLOCK, locator) && !locator.vHave.empty()) return true;
|
|
|
|
return m_batch->Read(DBKeys::BESTBLOCK_NOMERKLE, locator);
|
2013-04-13 02:13:08 -03:00
|
|
|
}
|
|
|
|
|
2017-12-08 08:39:22 -03:00
|
|
|
bool WalletBatch::WriteOrderPosNext(int64_t nOrderPosNext)
|
2013-04-13 02:13:08 -03:00
|
|
|
{
|
2019-07-27 15:50:36 -04:00
|
|
|
return WriteIC(DBKeys::ORDERPOSNEXT, nOrderPosNext);
|
2013-04-13 02:13:08 -03:00
|
|
|
}
|
|
|
|
|
2017-12-08 08:39:22 -03:00
|
|
|
bool WalletBatch::ReadPool(int64_t nPool, CKeyPool& keypool)
|
2013-04-13 02:13:08 -03:00
|
|
|
{
|
2020-06-15 16:54:58 -04:00
|
|
|
return m_batch->Read(std::make_pair(DBKeys::POOL, nPool), keypool);
|
2013-04-13 02:13:08 -03:00
|
|
|
}
|
|
|
|
|
2017-12-08 08:39:22 -03:00
|
|
|
bool WalletBatch::WritePool(int64_t nPool, const CKeyPool& keypool)
|
2013-04-13 02:13:08 -03:00
|
|
|
{
|
2019-07-27 15:50:36 -04:00
|
|
|
return WriteIC(std::make_pair(DBKeys::POOL, nPool), keypool);
|
2013-04-13 02:13:08 -03:00
|
|
|
}
|
|
|
|
|
2017-12-08 08:39:22 -03:00
|
|
|
bool WalletBatch::ErasePool(int64_t nPool)
|
2013-04-13 02:13:08 -03:00
|
|
|
{
|
2019-07-27 15:50:36 -04:00
|
|
|
return EraseIC(std::make_pair(DBKeys::POOL, nPool));
|
2013-04-13 02:13:08 -03:00
|
|
|
}
|
|
|
|
|
2017-12-08 08:39:22 -03:00
|
|
|
bool WalletBatch::WriteMinVersion(int nVersion)
|
2013-04-13 02:13:08 -03:00
|
|
|
{
|
2019-07-27 15:50:36 -04:00
|
|
|
return WriteIC(DBKeys::MINVERSION, nVersion);
|
2013-04-13 02:13:08 -03:00
|
|
|
}
|
|
|
|
|
2019-07-08 15:41:31 -04:00
|
|
|
bool WalletBatch::WriteActiveScriptPubKeyMan(uint8_t type, const uint256& id, bool internal)
|
|
|
|
{
|
|
|
|
std::string key = internal ? DBKeys::ACTIVEINTERNALSPK : DBKeys::ACTIVEEXTERNALSPK;
|
|
|
|
return WriteIC(make_pair(key, type), id);
|
|
|
|
}
|
|
|
|
|
2019-07-10 16:02:43 -04:00
|
|
|
bool WalletBatch::WriteDescriptorKey(const uint256& desc_id, const CPubKey& pubkey, const CPrivKey& privkey)
|
|
|
|
{
|
|
|
|
// hash pubkey/privkey to accelerate wallet load
|
|
|
|
std::vector<unsigned char> key;
|
|
|
|
key.reserve(pubkey.size() + privkey.size());
|
|
|
|
key.insert(key.end(), pubkey.begin(), pubkey.end());
|
|
|
|
key.insert(key.end(), privkey.begin(), privkey.end());
|
|
|
|
|
|
|
|
return WriteIC(std::make_pair(DBKeys::WALLETDESCRIPTORKEY, std::make_pair(desc_id, pubkey)), std::make_pair(privkey, Hash(key.begin(), key.end())), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WalletBatch::WriteCryptedDescriptorKey(const uint256& desc_id, const CPubKey& pubkey, const std::vector<unsigned char>& secret)
|
|
|
|
{
|
|
|
|
if (!WriteIC(std::make_pair(DBKeys::WALLETDESCRIPTORCKEY, std::make_pair(desc_id, pubkey)), secret, false)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
EraseIC(std::make_pair(DBKeys::WALLETDESCRIPTORKEY, std::make_pair(desc_id, pubkey)));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WalletBatch::WriteDescriptor(const uint256& desc_id, const WalletDescriptor& descriptor)
|
|
|
|
{
|
|
|
|
return WriteIC(make_pair(DBKeys::WALLETDESCRIPTOR, desc_id), descriptor);
|
|
|
|
}
|
|
|
|
|
2019-07-10 16:38:12 -04:00
|
|
|
bool WalletBatch::WriteDescriptorDerivedCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index, uint32_t der_index)
|
|
|
|
{
|
|
|
|
std::vector<unsigned char> ser_xpub(BIP32_EXTKEY_SIZE);
|
|
|
|
xpub.Encode(ser_xpub.data());
|
|
|
|
return WriteIC(std::make_pair(std::make_pair(DBKeys::WALLETDESCRIPTORCACHE, desc_id), std::make_pair(key_exp_index, der_index)), ser_xpub);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WalletBatch::WriteDescriptorParentCache(const CExtPubKey& xpub, const uint256& desc_id, uint32_t key_exp_index)
|
|
|
|
{
|
|
|
|
std::vector<unsigned char> ser_xpub(BIP32_EXTKEY_SIZE);
|
|
|
|
xpub.Encode(ser_xpub.data());
|
|
|
|
return WriteIC(std::make_pair(std::make_pair(DBKeys::WALLETDESCRIPTORCACHE, desc_id), key_exp_index), ser_xpub);
|
|
|
|
}
|
|
|
|
|
2013-06-10 09:36:29 -04:00
|
|
|
class CWalletScanState {
|
|
|
|
public:
|
2019-01-05 08:02:12 -03:00
|
|
|
unsigned int nKeys{0};
|
|
|
|
unsigned int nCKeys{0};
|
|
|
|
unsigned int nWatchKeys{0};
|
|
|
|
unsigned int nKeyMeta{0};
|
|
|
|
unsigned int m_unknown_records{0};
|
|
|
|
bool fIsEncrypted{false};
|
|
|
|
bool fAnyUnordered{false};
|
2017-01-26 22:33:45 -03:00
|
|
|
std::vector<uint256> vWalletUpgrade;
|
2019-07-08 15:41:31 -04:00
|
|
|
std::map<OutputType, uint256> m_active_external_spks;
|
|
|
|
std::map<OutputType, uint256> m_active_internal_spks;
|
2019-07-09 13:40:34 -04:00
|
|
|
std::map<uint256, DescriptorCache> m_descriptor_caches;
|
2019-07-09 18:23:51 -04:00
|
|
|
std::map<std::pair<uint256, CKeyID>, CKey> m_descriptor_keys;
|
|
|
|
std::map<std::pair<uint256, CKeyID>, std::pair<CPubKey, std::vector<unsigned char>>> m_descriptor_crypt_keys;
|
2019-12-05 17:23:05 -03:00
|
|
|
std::map<uint160, CHDChain> m_hd_chains;
|
2013-06-10 09:36:29 -04:00
|
|
|
|
|
|
|
CWalletScanState() {
|
|
|
|
}
|
|
|
|
};
|
2012-05-27 19:06:09 -04:00
|
|
|
|
2018-05-02 12:14:48 -03:00
|
|
|
static bool
|
2012-09-18 15:30:47 -03:00
|
|
|
ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
|
2019-10-07 15:11:34 -03:00
|
|
|
CWalletScanState &wss, std::string& strType, std::string& strErr) EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet)
|
2012-09-18 15:30:47 -03:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
// Unserialize
|
|
|
|
// Taking advantage of the fact that pair serialization
|
|
|
|
// is just the two items serialized one after the other
|
|
|
|
ssKey >> strType;
|
2019-07-27 15:50:36 -04:00
|
|
|
if (strType == DBKeys::NAME) {
|
2017-01-26 22:33:45 -03:00
|
|
|
std::string strAddress;
|
2012-09-18 15:30:47 -03:00
|
|
|
ssKey >> strAddress;
|
2020-02-21 22:52:47 -03:00
|
|
|
std::string label;
|
|
|
|
ssValue >> label;
|
|
|
|
pwallet->m_address_book[DecodeDestination(strAddress)].SetLabel(label);
|
2019-07-27 15:50:36 -04:00
|
|
|
} else if (strType == DBKeys::PURPOSE) {
|
2017-01-26 22:33:45 -03:00
|
|
|
std::string strAddress;
|
2013-07-22 02:50:39 -04:00
|
|
|
ssKey >> strAddress;
|
2020-02-21 18:52:52 -03:00
|
|
|
ssValue >> pwallet->m_address_book[DecodeDestination(strAddress)].purpose;
|
2019-07-27 15:50:36 -04:00
|
|
|
} else if (strType == DBKeys::TX) {
|
2012-09-18 15:30:47 -03:00
|
|
|
uint256 hash;
|
|
|
|
ssKey >> hash;
|
2019-11-08 11:03:51 -03:00
|
|
|
// LoadToWallet call below creates a new CWalletTx that fill_wtx
|
|
|
|
// callback fills with transaction metadata.
|
|
|
|
auto fill_wtx = [&](CWalletTx& wtx, bool new_tx) {
|
|
|
|
assert(new_tx);
|
|
|
|
ssValue >> wtx;
|
|
|
|
if (wtx.GetHash() != hash)
|
|
|
|
return false;
|
2012-09-18 15:30:47 -03:00
|
|
|
|
2019-11-08 11:03:51 -03:00
|
|
|
// Undo serialize changes in 31600
|
|
|
|
if (31404 <= wtx.fTimeReceivedIsTxTime && wtx.fTimeReceivedIsTxTime <= 31703)
|
2012-09-18 15:30:47 -03:00
|
|
|
{
|
2019-11-08 11:03:51 -03:00
|
|
|
if (!ssValue.empty())
|
|
|
|
{
|
|
|
|
char fTmp;
|
|
|
|
char fUnused;
|
|
|
|
std::string unused_string;
|
|
|
|
ssValue >> fTmp >> fUnused >> unused_string;
|
|
|
|
strErr = strprintf("LoadWallet() upgrading tx ver=%d %d %s",
|
|
|
|
wtx.fTimeReceivedIsTxTime, fTmp, hash.ToString());
|
|
|
|
wtx.fTimeReceivedIsTxTime = fTmp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strErr = strprintf("LoadWallet() repairing tx ver=%d %s", wtx.fTimeReceivedIsTxTime, hash.ToString());
|
|
|
|
wtx.fTimeReceivedIsTxTime = 0;
|
|
|
|
}
|
|
|
|
wss.vWalletUpgrade.push_back(hash);
|
2012-09-18 15:30:47 -03:00
|
|
|
}
|
|
|
|
|
2019-11-08 11:03:51 -03:00
|
|
|
if (wtx.nOrderPos == -1)
|
|
|
|
wss.fAnyUnordered = true;
|
2012-09-18 15:30:47 -03:00
|
|
|
|
2019-11-08 11:03:51 -03:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
if (!pwallet->LoadToWallet(hash, fill_wtx)) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-07-27 15:50:36 -04:00
|
|
|
} else if (strType == DBKeys::WATCHS) {
|
2016-11-08 18:55:02 -03:00
|
|
|
wss.nWatchKeys++;
|
2014-06-09 15:11:59 -04:00
|
|
|
CScript script;
|
2017-07-07 03:54:42 -04:00
|
|
|
ssKey >> script;
|
2013-07-25 19:06:01 -04:00
|
|
|
char fYes;
|
|
|
|
ssValue >> fYes;
|
2019-10-07 15:11:34 -03:00
|
|
|
if (fYes == '1') {
|
2019-10-07 15:11:34 -03:00
|
|
|
pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadWatchOnly(script);
|
2019-10-07 15:11:34 -03:00
|
|
|
}
|
2019-07-30 17:43:15 -04:00
|
|
|
} else if (strType == DBKeys::KEY) {
|
2013-05-01 00:52:05 -04:00
|
|
|
CPubKey vchPubKey;
|
2012-09-18 15:30:47 -03:00
|
|
|
ssKey >> vchPubKey;
|
2013-05-01 00:52:05 -04:00
|
|
|
if (!vchPubKey.IsValid())
|
|
|
|
{
|
|
|
|
strErr = "Error reading wallet database: CPubKey corrupt";
|
|
|
|
return false;
|
|
|
|
}
|
2012-09-18 15:30:47 -03:00
|
|
|
CKey key;
|
2013-05-01 00:52:05 -04:00
|
|
|
CPrivKey pkey;
|
2014-12-15 05:11:16 -03:00
|
|
|
uint256 hash;
|
2013-11-15 08:24:34 -03:00
|
|
|
|
2019-07-30 17:43:15 -04:00
|
|
|
wss.nKeys++;
|
|
|
|
ssValue >> pkey;
|
2013-11-15 08:24:34 -03:00
|
|
|
|
2019-07-27 15:50:36 -04:00
|
|
|
// Old wallets store keys as DBKeys::KEY [pubkey] => [privkey]
|
2013-10-13 03:44:28 -03:00
|
|
|
// ... which was slow for wallets with lots of keys, because the public key is re-derived from the private key
|
|
|
|
// using EC operations as a checksum.
|
2019-07-27 15:50:36 -04:00
|
|
|
// Newer wallets store keys as DBKeys::KEY [pubkey] => [privkey][hash(pubkey,privkey)], which is much faster while
|
2013-10-13 03:44:28 -03:00
|
|
|
// remaining backwards-compatible.
|
2013-08-29 02:53:26 -04:00
|
|
|
try
|
2013-05-01 00:52:05 -04:00
|
|
|
{
|
2013-08-29 02:53:26 -04:00
|
|
|
ssValue >> hash;
|
2013-05-01 00:52:05 -04:00
|
|
|
}
|
2014-12-07 09:29:06 -03:00
|
|
|
catch (...) {}
|
2013-11-15 08:24:34 -03:00
|
|
|
|
2013-08-29 02:53:26 -04:00
|
|
|
bool fSkipCheck = false;
|
2013-11-15 08:24:34 -03:00
|
|
|
|
2014-12-15 05:11:16 -03:00
|
|
|
if (!hash.IsNull())
|
2013-05-01 00:52:05 -04:00
|
|
|
{
|
2013-08-29 02:53:26 -04:00
|
|
|
// hash pubkey/privkey to accelerate wallet load
|
|
|
|
std::vector<unsigned char> vchKey;
|
|
|
|
vchKey.reserve(vchPubKey.size() + pkey.size());
|
|
|
|
vchKey.insert(vchKey.end(), vchPubKey.begin(), vchPubKey.end());
|
|
|
|
vchKey.insert(vchKey.end(), pkey.begin(), pkey.end());
|
2013-11-15 08:24:34 -03:00
|
|
|
|
2013-08-29 02:53:26 -04:00
|
|
|
if (Hash(vchKey.begin(), vchKey.end()) != hash)
|
|
|
|
{
|
|
|
|
strErr = "Error reading wallet database: CPubKey/CPrivKey corrupt";
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-15 08:24:34 -03:00
|
|
|
|
2013-08-29 02:53:26 -04:00
|
|
|
fSkipCheck = true;
|
|
|
|
}
|
2013-11-15 08:24:34 -03:00
|
|
|
|
2013-08-29 02:53:26 -04:00
|
|
|
if (!key.Load(pkey, vchPubKey, fSkipCheck))
|
|
|
|
{
|
|
|
|
strErr = "Error reading wallet database: CPrivKey corrupt";
|
2013-05-01 00:52:05 -04:00
|
|
|
return false;
|
2012-09-18 15:30:47 -03:00
|
|
|
}
|
2019-10-07 15:11:34 -03:00
|
|
|
if (!pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadKey(key, vchPubKey))
|
2012-09-18 15:30:47 -03:00
|
|
|
{
|
2019-10-07 15:11:34 -03:00
|
|
|
strErr = "Error reading wallet database: LegacyScriptPubKeyMan::LoadKey failed";
|
2012-09-18 15:30:47 -03:00
|
|
|
return false;
|
|
|
|
}
|
2019-07-27 15:50:36 -04:00
|
|
|
} else if (strType == DBKeys::MASTER_KEY) {
|
2019-10-07 15:11:34 -03:00
|
|
|
// Master encryption key is loaded into only the wallet and not any of the ScriptPubKeyMans.
|
2012-09-18 15:30:47 -03:00
|
|
|
unsigned int nID;
|
|
|
|
ssKey >> nID;
|
|
|
|
CMasterKey kMasterKey;
|
|
|
|
ssValue >> kMasterKey;
|
|
|
|
if(pwallet->mapMasterKeys.count(nID) != 0)
|
|
|
|
{
|
|
|
|
strErr = strprintf("Error reading wallet database: duplicate CMasterKey id %u", nID);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
pwallet->mapMasterKeys[nID] = kMasterKey;
|
|
|
|
if (pwallet->nMasterKeyMaxID < nID)
|
|
|
|
pwallet->nMasterKeyMaxID = nID;
|
2019-07-27 15:50:36 -04:00
|
|
|
} else if (strType == DBKeys::CRYPTED_KEY) {
|
2015-10-29 15:24:49 -03:00
|
|
|
CPubKey vchPubKey;
|
2012-09-18 15:30:47 -03:00
|
|
|
ssKey >> vchPubKey;
|
2015-10-29 15:24:49 -03:00
|
|
|
if (!vchPubKey.IsValid())
|
|
|
|
{
|
|
|
|
strErr = "Error reading wallet database: CPubKey corrupt";
|
|
|
|
return false;
|
|
|
|
}
|
2017-01-26 22:33:45 -03:00
|
|
|
std::vector<unsigned char> vchPrivKey;
|
2012-09-18 15:30:47 -03:00
|
|
|
ssValue >> vchPrivKey;
|
2019-09-23 19:10:13 -03:00
|
|
|
|
|
|
|
// Get the checksum and check it
|
2019-12-03 20:57:51 -03:00
|
|
|
bool checksum_valid = false;
|
2019-09-23 19:10:13 -03:00
|
|
|
if (!ssValue.eof()) {
|
|
|
|
uint256 checksum;
|
|
|
|
ssValue >> checksum;
|
2019-12-03 20:57:51 -03:00
|
|
|
if ((checksum_valid = Hash(vchPrivKey.begin(), vchPrivKey.end()) != checksum)) {
|
2019-09-23 19:10:13 -03:00
|
|
|
strErr = "Error reading wallet database: Crypted key corrupt";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-10 09:36:29 -04:00
|
|
|
wss.nCKeys++;
|
|
|
|
|
2019-12-03 20:57:51 -03:00
|
|
|
if (!pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadCryptedKey(vchPubKey, vchPrivKey, checksum_valid))
|
2012-09-18 15:30:47 -03:00
|
|
|
{
|
2019-10-07 15:11:34 -03:00
|
|
|
strErr = "Error reading wallet database: LegacyScriptPubKeyMan::LoadCryptedKey failed";
|
2012-09-18 15:30:47 -03:00
|
|
|
return false;
|
|
|
|
}
|
2013-06-10 09:36:29 -04:00
|
|
|
wss.fIsEncrypted = true;
|
2019-07-27 15:50:36 -04:00
|
|
|
} else if (strType == DBKeys::KEYMETA) {
|
2017-12-08 14:50:46 -03:00
|
|
|
CPubKey vchPubKey;
|
|
|
|
ssKey >> vchPubKey;
|
2013-06-10 09:36:29 -04:00
|
|
|
CKeyMetadata keyMeta;
|
|
|
|
ssValue >> keyMeta;
|
|
|
|
wss.nKeyMeta++;
|
2019-10-07 15:11:34 -03:00
|
|
|
pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadKeyMetadata(vchPubKey.GetID(), keyMeta);
|
2019-12-05 17:23:05 -03:00
|
|
|
|
|
|
|
// Extract some CHDChain info from this metadata if it has any
|
|
|
|
if (keyMeta.nVersion >= CKeyMetadata::VERSION_WITH_HDDATA && !keyMeta.hd_seed_id.IsNull() && keyMeta.hdKeypath.size() > 0) {
|
|
|
|
// Get the path from the key origin or from the path string
|
2020-05-22 13:42:35 -04:00
|
|
|
// Not applicable when path is "s" or "m" as those indicate a seed
|
|
|
|
// See https://github.com/bitcoin/bitcoin/pull/12924
|
2019-12-05 17:23:05 -03:00
|
|
|
bool internal = false;
|
|
|
|
uint32_t index = 0;
|
2020-05-22 13:42:35 -04:00
|
|
|
if (keyMeta.hdKeypath != "s" && keyMeta.hdKeypath != "m") {
|
2019-12-05 17:23:05 -03:00
|
|
|
std::vector<uint32_t> path;
|
|
|
|
if (keyMeta.has_key_origin) {
|
|
|
|
// We have a key origin, so pull it from its path vector
|
|
|
|
path = keyMeta.key_origin.path;
|
|
|
|
} else {
|
|
|
|
// No key origin, have to parse the string
|
|
|
|
if (!ParseHDKeypath(keyMeta.hdKeypath, path)) {
|
|
|
|
strErr = "Error reading wallet database: keymeta with invalid HD keypath";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract the index and internal from the path
|
|
|
|
// Path string is m/0'/k'/i'
|
|
|
|
// Path vector is [0', k', i'] (but as ints OR'd with the hardened bit
|
|
|
|
// k == 0 for external, 1 for internal. i is the index
|
|
|
|
if (path.size() != 3) {
|
|
|
|
strErr = "Error reading wallet database: keymeta found with unexpected path";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (path[0] != 0x80000000) {
|
|
|
|
strErr = strprintf("Unexpected path index of 0x%08x (expected 0x80000000) for the element at index 0", path[0]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (path[1] != 0x80000000 && path[1] != (1 | 0x80000000)) {
|
|
|
|
strErr = strprintf("Unexpected path index of 0x%08x (expected 0x80000000 or 0x80000001) for the element at index 1", path[1]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ((path[2] & 0x80000000) == 0) {
|
|
|
|
strErr = strprintf("Unexpected path index of 0x%08x (expected to be greater than or equal to 0x80000000)", path[2]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
internal = path[1] == (1 | 0x80000000);
|
|
|
|
index = path[2] & ~0x80000000;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert a new CHDChain, or get the one that already exists
|
|
|
|
auto ins = wss.m_hd_chains.emplace(keyMeta.hd_seed_id, CHDChain());
|
|
|
|
CHDChain& chain = ins.first->second;
|
|
|
|
if (ins.second) {
|
|
|
|
// For new chains, we want to default to VERSION_HD_BASE until we see an internal
|
|
|
|
chain.nVersion = CHDChain::VERSION_HD_BASE;
|
|
|
|
chain.seed_id = keyMeta.hd_seed_id;
|
|
|
|
}
|
|
|
|
if (internal) {
|
|
|
|
chain.nVersion = CHDChain::VERSION_HD_CHAIN_SPLIT;
|
|
|
|
chain.nInternalChainCounter = std::max(chain.nInternalChainCounter, index);
|
|
|
|
} else {
|
|
|
|
chain.nExternalChainCounter = std::max(chain.nExternalChainCounter, index);
|
|
|
|
}
|
|
|
|
}
|
2019-07-27 15:50:36 -04:00
|
|
|
} else if (strType == DBKeys::WATCHMETA) {
|
2017-12-08 14:50:46 -03:00
|
|
|
CScript script;
|
|
|
|
ssKey >> script;
|
|
|
|
CKeyMetadata keyMeta;
|
|
|
|
ssValue >> keyMeta;
|
|
|
|
wss.nKeyMeta++;
|
2019-10-07 15:11:34 -03:00
|
|
|
pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadScriptMetadata(CScriptID(script), keyMeta);
|
2019-07-27 15:50:36 -04:00
|
|
|
} else if (strType == DBKeys::DEFAULTKEY) {
|
2017-07-28 20:00:49 -04:00
|
|
|
// We don't want or need the default key, but if there is one set,
|
|
|
|
// we want to make sure that it is valid so that we can detect corruption
|
|
|
|
CPubKey vchPubKey;
|
|
|
|
ssValue >> vchPubKey;
|
|
|
|
if (!vchPubKey.IsValid()) {
|
|
|
|
strErr = "Error reading wallet database: Default Key corrupt";
|
|
|
|
return false;
|
|
|
|
}
|
2019-07-27 15:50:36 -04:00
|
|
|
} else if (strType == DBKeys::POOL) {
|
2013-04-13 02:13:08 -03:00
|
|
|
int64_t nIndex;
|
2012-09-18 15:30:47 -03:00
|
|
|
ssKey >> nIndex;
|
2013-04-29 13:50:40 -04:00
|
|
|
CKeyPool keypool;
|
|
|
|
ssValue >> keypool;
|
2016-08-01 20:14:40 -04:00
|
|
|
|
2019-10-07 15:11:34 -03:00
|
|
|
pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadKeyPool(nIndex, keypool);
|
2019-07-27 15:50:36 -04:00
|
|
|
} else if (strType == DBKeys::CSCRIPT) {
|
2012-09-18 15:30:47 -03:00
|
|
|
uint160 hash;
|
|
|
|
ssKey >> hash;
|
|
|
|
CScript script;
|
2017-07-07 03:54:42 -04:00
|
|
|
ssValue >> script;
|
2019-10-07 15:11:34 -03:00
|
|
|
if (!pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadCScript(script))
|
2012-09-18 15:30:47 -03:00
|
|
|
{
|
2019-10-07 15:11:34 -03:00
|
|
|
strErr = "Error reading wallet database: LegacyScriptPubKeyMan::LoadCScript failed";
|
2012-09-18 15:30:47 -03:00
|
|
|
return false;
|
|
|
|
}
|
2019-07-27 15:50:36 -04:00
|
|
|
} else if (strType == DBKeys::ORDERPOSNEXT) {
|
2012-09-18 15:30:47 -03:00
|
|
|
ssValue >> pwallet->nOrderPosNext;
|
2019-07-27 15:50:36 -04:00
|
|
|
} else if (strType == DBKeys::DESTDATA) {
|
2013-11-18 12:55:54 -03:00
|
|
|
std::string strAddress, strKey, strValue;
|
|
|
|
ssKey >> strAddress;
|
|
|
|
ssKey >> strKey;
|
|
|
|
ssValue >> strValue;
|
2018-07-27 02:22:42 -04:00
|
|
|
pwallet->LoadDestData(DecodeDestination(strAddress), strKey, strValue);
|
2019-07-27 15:50:36 -04:00
|
|
|
} else if (strType == DBKeys::HDCHAIN) {
|
2016-01-02 08:34:08 -03:00
|
|
|
CHDChain chain;
|
|
|
|
ssValue >> chain;
|
2020-05-21 22:43:58 -04:00
|
|
|
pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadHDChain(chain);
|
2019-07-27 15:50:36 -04:00
|
|
|
} else if (strType == DBKeys::FLAGS) {
|
2017-02-16 10:22:18 -03:00
|
|
|
uint64_t flags;
|
|
|
|
ssValue >> flags;
|
2020-05-21 23:15:41 -04:00
|
|
|
if (!pwallet->LoadWalletFlags(flags)) {
|
2017-05-05 03:53:39 -03:00
|
|
|
strErr = "Error reading wallet database: Unknown non-tolerable wallet flags found";
|
|
|
|
return false;
|
|
|
|
}
|
2019-07-30 17:43:15 -04:00
|
|
|
} else if (strType == DBKeys::OLD_KEY) {
|
|
|
|
strErr = "Found unsupported 'wkey' record, try loading with version 0.18";
|
|
|
|
return false;
|
2019-07-08 15:41:31 -04:00
|
|
|
} else if (strType == DBKeys::ACTIVEEXTERNALSPK || strType == DBKeys::ACTIVEINTERNALSPK) {
|
|
|
|
uint8_t type;
|
|
|
|
ssKey >> type;
|
|
|
|
uint256 id;
|
|
|
|
ssValue >> id;
|
|
|
|
|
|
|
|
bool internal = strType == DBKeys::ACTIVEINTERNALSPK;
|
|
|
|
auto& spk_mans = internal ? wss.m_active_internal_spks : wss.m_active_external_spks;
|
|
|
|
if (spk_mans.count(static_cast<OutputType>(type)) > 0) {
|
|
|
|
strErr = "Multiple ScriptPubKeyMans specified for a single type";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
spk_mans[static_cast<OutputType>(type)] = id;
|
|
|
|
} else if (strType == DBKeys::WALLETDESCRIPTOR) {
|
|
|
|
uint256 id;
|
|
|
|
ssKey >> id;
|
|
|
|
WalletDescriptor desc;
|
|
|
|
ssValue >> desc;
|
2019-07-09 13:40:34 -04:00
|
|
|
if (wss.m_descriptor_caches.count(id) == 0) {
|
|
|
|
wss.m_descriptor_caches[id] = DescriptorCache();
|
|
|
|
}
|
2019-07-08 15:41:31 -04:00
|
|
|
pwallet->LoadDescriptorScriptPubKeyMan(id, desc);
|
2019-07-09 13:40:34 -04:00
|
|
|
} else if (strType == DBKeys::WALLETDESCRIPTORCACHE) {
|
|
|
|
bool parent = true;
|
|
|
|
uint256 desc_id;
|
|
|
|
uint32_t key_exp_index;
|
|
|
|
uint32_t der_index;
|
|
|
|
ssKey >> desc_id;
|
|
|
|
ssKey >> key_exp_index;
|
|
|
|
|
|
|
|
// if the der_index exists, it's a derived xpub
|
|
|
|
try
|
|
|
|
{
|
|
|
|
ssKey >> der_index;
|
|
|
|
parent = false;
|
|
|
|
}
|
|
|
|
catch (...) {}
|
|
|
|
|
|
|
|
std::vector<unsigned char> ser_xpub(BIP32_EXTKEY_SIZE);
|
|
|
|
ssValue >> ser_xpub;
|
|
|
|
CExtPubKey xpub;
|
|
|
|
xpub.Decode(ser_xpub.data());
|
|
|
|
if (parent) {
|
|
|
|
wss.m_descriptor_caches[desc_id].CacheParentExtPubKey(key_exp_index, xpub);
|
|
|
|
} else {
|
|
|
|
wss.m_descriptor_caches[desc_id].CacheDerivedExtPubKey(key_exp_index, der_index, xpub);
|
|
|
|
}
|
2019-07-09 18:23:51 -04:00
|
|
|
} else if (strType == DBKeys::WALLETDESCRIPTORKEY) {
|
|
|
|
uint256 desc_id;
|
|
|
|
CPubKey pubkey;
|
|
|
|
ssKey >> desc_id;
|
|
|
|
ssKey >> pubkey;
|
|
|
|
if (!pubkey.IsValid())
|
|
|
|
{
|
|
|
|
strErr = "Error reading wallet database: CPubKey corrupt";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
CKey key;
|
|
|
|
CPrivKey pkey;
|
|
|
|
uint256 hash;
|
|
|
|
|
|
|
|
wss.nKeys++;
|
|
|
|
ssValue >> pkey;
|
|
|
|
ssValue >> hash;
|
|
|
|
|
|
|
|
// hash pubkey/privkey to accelerate wallet load
|
|
|
|
std::vector<unsigned char> to_hash;
|
|
|
|
to_hash.reserve(pubkey.size() + pkey.size());
|
|
|
|
to_hash.insert(to_hash.end(), pubkey.begin(), pubkey.end());
|
|
|
|
to_hash.insert(to_hash.end(), pkey.begin(), pkey.end());
|
|
|
|
|
|
|
|
if (Hash(to_hash.begin(), to_hash.end()) != hash)
|
|
|
|
{
|
|
|
|
strErr = "Error reading wallet database: CPubKey/CPrivKey corrupt";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!key.Load(pkey, pubkey, true))
|
|
|
|
{
|
|
|
|
strErr = "Error reading wallet database: CPrivKey corrupt";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
wss.m_descriptor_keys.insert(std::make_pair(std::make_pair(desc_id, pubkey.GetID()), key));
|
|
|
|
} else if (strType == DBKeys::WALLETDESCRIPTORCKEY) {
|
|
|
|
uint256 desc_id;
|
|
|
|
CPubKey pubkey;
|
|
|
|
ssKey >> desc_id;
|
|
|
|
ssKey >> pubkey;
|
|
|
|
if (!pubkey.IsValid())
|
|
|
|
{
|
|
|
|
strErr = "Error reading wallet database: CPubKey corrupt";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::vector<unsigned char> privkey;
|
|
|
|
ssValue >> privkey;
|
|
|
|
wss.nCKeys++;
|
|
|
|
|
|
|
|
wss.m_descriptor_crypt_keys.insert(std::make_pair(std::make_pair(desc_id, pubkey.GetID()), std::make_pair(pubkey, privkey)));
|
|
|
|
wss.fIsEncrypted = true;
|
2019-07-27 15:50:36 -04:00
|
|
|
} else if (strType != DBKeys::BESTBLOCK && strType != DBKeys::BESTBLOCK_NOMERKLE &&
|
2019-07-27 08:07:30 -04:00
|
|
|
strType != DBKeys::MINVERSION && strType != DBKeys::ACENTRY &&
|
|
|
|
strType != DBKeys::VERSION && strType != DBKeys::SETTINGS) {
|
2018-04-04 21:46:40 -03:00
|
|
|
wss.m_unknown_records++;
|
2016-01-02 08:34:08 -03:00
|
|
|
}
|
2019-03-06 18:22:41 -03:00
|
|
|
} catch (const std::exception& e) {
|
|
|
|
if (strErr.empty()) {
|
|
|
|
strErr = e.what();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} catch (...) {
|
|
|
|
if (strErr.empty()) {
|
|
|
|
strErr = "Caught unknown exception in ReadKeyValue";
|
|
|
|
}
|
2012-09-18 15:30:47 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-15 19:15:50 -04:00
|
|
|
bool ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, std::string& strType, std::string& strErr)
|
|
|
|
{
|
|
|
|
CWalletScanState dummy_wss;
|
|
|
|
LOCK(pwallet->cs_wallet);
|
|
|
|
return ReadKeyValue(pwallet, ssKey, ssValue, dummy_wss, strType, strErr);
|
|
|
|
}
|
|
|
|
|
2017-12-08 08:39:22 -03:00
|
|
|
bool WalletBatch::IsKeyType(const std::string& strType)
|
2012-09-18 15:30:47 -03:00
|
|
|
{
|
2019-07-30 17:43:15 -04:00
|
|
|
return (strType == DBKeys::KEY ||
|
2019-07-27 15:50:36 -04:00
|
|
|
strType == DBKeys::MASTER_KEY || strType == DBKeys::CRYPTED_KEY);
|
2012-09-18 15:30:47 -03:00
|
|
|
}
|
|
|
|
|
2017-12-08 08:39:22 -03:00
|
|
|
DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
|
2012-04-15 18:39:49 -03:00
|
|
|
{
|
2013-06-10 09:36:29 -04:00
|
|
|
CWalletScanState wss;
|
2012-09-18 15:30:47 -03:00
|
|
|
bool fNoncriticalErrors = false;
|
2018-03-09 11:03:40 -03:00
|
|
|
DBErrors result = DBErrors::LOAD_OK;
|
2012-04-15 18:39:49 -03:00
|
|
|
|
2017-02-15 19:01:30 -03:00
|
|
|
LOCK(pwallet->cs_wallet);
|
2012-09-18 15:30:47 -03:00
|
|
|
try {
|
2012-04-15 18:39:49 -03:00
|
|
|
int nMinVersion = 0;
|
2020-06-15 16:54:58 -04:00
|
|
|
if (m_batch->Read(DBKeys::MINVERSION, nMinVersion)) {
|
2018-06-18 18:21:32 -04:00
|
|
|
if (nMinVersion > FEATURE_LATEST)
|
2018-03-09 11:03:40 -03:00
|
|
|
return DBErrors::TOO_NEW;
|
2012-04-15 18:39:49 -03:00
|
|
|
pwallet->LoadMinVersion(nMinVersion);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get cursor
|
2020-06-15 16:54:58 -04:00
|
|
|
if (!m_batch->StartCursor())
|
2012-04-15 18:39:49 -03:00
|
|
|
{
|
2018-06-15 19:02:52 -04:00
|
|
|
pwallet->WalletLogPrintf("Error getting wallet database cursor\n");
|
2018-03-09 11:03:40 -03:00
|
|
|
return DBErrors::CORRUPT;
|
2012-04-15 18:39:49 -03:00
|
|
|
}
|
|
|
|
|
2013-07-31 00:06:44 -04:00
|
|
|
while (true)
|
2012-04-15 18:39:49 -03:00
|
|
|
{
|
|
|
|
// Read next record
|
2012-04-16 09:56:45 -03:00
|
|
|
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
|
|
|
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
|
2020-05-14 21:17:01 -04:00
|
|
|
bool complete;
|
2020-06-15 16:54:58 -04:00
|
|
|
bool ret = m_batch->ReadAtCursor(ssKey, ssValue, complete);
|
2020-05-14 21:17:01 -04:00
|
|
|
if (complete) {
|
2012-04-15 18:39:49 -03:00
|
|
|
break;
|
2020-05-14 21:17:01 -04:00
|
|
|
}
|
|
|
|
else if (!ret)
|
2012-04-15 18:39:49 -03:00
|
|
|
{
|
2020-06-15 16:54:58 -04:00
|
|
|
m_batch->CloseCursor();
|
2018-06-15 19:02:52 -04:00
|
|
|
pwallet->WalletLogPrintf("Error reading next record from wallet database\n");
|
2018-03-09 11:03:40 -03:00
|
|
|
return DBErrors::CORRUPT;
|
2012-04-15 18:39:49 -03:00
|
|
|
}
|
|
|
|
|
2012-09-18 15:30:47 -03:00
|
|
|
// Try to be tolerant of single corrupt records:
|
2017-01-26 22:33:45 -03:00
|
|
|
std::string strType, strErr;
|
2013-06-10 09:36:29 -04:00
|
|
|
if (!ReadKeyValue(pwallet, ssKey, ssValue, wss, strType, strErr))
|
2012-04-15 18:39:49 -03:00
|
|
|
{
|
2012-09-18 15:30:47 -03:00
|
|
|
// losing keys is considered a catastrophic error, anything else
|
|
|
|
// we assume the user can live with:
|
2019-07-27 15:50:36 -04:00
|
|
|
if (IsKeyType(strType) || strType == DBKeys::DEFAULTKEY) {
|
2018-03-09 11:03:40 -03:00
|
|
|
result = DBErrors::CORRUPT;
|
2019-07-27 15:50:36 -04:00
|
|
|
} else if (strType == DBKeys::FLAGS) {
|
2017-05-05 03:53:39 -03:00
|
|
|
// reading the wallet flags can only fail if unknown flags are present
|
|
|
|
result = DBErrors::TOO_NEW;
|
|
|
|
} else {
|
2012-09-18 15:30:47 -03:00
|
|
|
// Leave other errors alone, if we try to fix them we might make things worse.
|
|
|
|
fNoncriticalErrors = true; // ... but do warn the user there is something wrong.
|
2019-07-27 15:50:36 -04:00
|
|
|
if (strType == DBKeys::TX)
|
2012-09-18 15:30:47 -03:00
|
|
|
// Rescan if there is a bad transaction record:
|
2017-08-01 15:17:40 -04:00
|
|
|
gArgs.SoftSetBoolArg("-rescan", true);
|
2012-04-15 18:39:49 -03:00
|
|
|
}
|
2012-09-08 01:55:36 -03:00
|
|
|
}
|
2012-09-18 15:30:47 -03:00
|
|
|
if (!strErr.empty())
|
2018-06-15 19:02:52 -04:00
|
|
|
pwallet->WalletLogPrintf("%s\n", strErr);
|
2012-04-15 18:39:49 -03:00
|
|
|
}
|
2020-04-24 11:23:40 -04:00
|
|
|
} catch (...) {
|
2018-03-09 11:03:40 -03:00
|
|
|
result = DBErrors::CORRUPT;
|
2012-09-18 15:30:47 -03:00
|
|
|
}
|
2020-06-15 16:54:58 -04:00
|
|
|
m_batch->CloseCursor();
|
2012-04-15 18:39:49 -03:00
|
|
|
|
2019-07-08 15:41:31 -04:00
|
|
|
// Set the active ScriptPubKeyMans
|
|
|
|
for (auto spk_man_pair : wss.m_active_external_spks) {
|
2020-05-21 23:01:24 -04:00
|
|
|
pwallet->LoadActiveScriptPubKeyMan(spk_man_pair.second, spk_man_pair.first, /* internal */ false);
|
2019-07-08 15:41:31 -04:00
|
|
|
}
|
|
|
|
for (auto spk_man_pair : wss.m_active_internal_spks) {
|
2020-05-21 23:01:24 -04:00
|
|
|
pwallet->LoadActiveScriptPubKeyMan(spk_man_pair.second, spk_man_pair.first, /* internal */ true);
|
2019-07-08 15:41:31 -04:00
|
|
|
}
|
|
|
|
|
2019-07-09 13:40:34 -04:00
|
|
|
// Set the descriptor caches
|
|
|
|
for (auto desc_cache_pair : wss.m_descriptor_caches) {
|
|
|
|
auto spk_man = pwallet->GetScriptPubKeyMan(desc_cache_pair.first);
|
|
|
|
assert(spk_man);
|
|
|
|
((DescriptorScriptPubKeyMan*)spk_man)->SetCache(desc_cache_pair.second);
|
|
|
|
}
|
|
|
|
|
2019-07-09 18:23:51 -04:00
|
|
|
// Set the descriptor keys
|
|
|
|
for (auto desc_key_pair : wss.m_descriptor_keys) {
|
|
|
|
auto spk_man = pwallet->GetScriptPubKeyMan(desc_key_pair.first.first);
|
|
|
|
((DescriptorScriptPubKeyMan*)spk_man)->AddKey(desc_key_pair.first.second, desc_key_pair.second);
|
|
|
|
}
|
|
|
|
for (auto desc_key_pair : wss.m_descriptor_crypt_keys) {
|
|
|
|
auto spk_man = pwallet->GetScriptPubKeyMan(desc_key_pair.first.first);
|
|
|
|
((DescriptorScriptPubKeyMan*)spk_man)->AddCryptedKey(desc_key_pair.first.second, desc_key_pair.second.first, desc_key_pair.second.second);
|
|
|
|
}
|
|
|
|
|
2018-03-09 11:03:40 -03:00
|
|
|
if (fNoncriticalErrors && result == DBErrors::LOAD_OK)
|
|
|
|
result = DBErrors::NONCRITICAL_ERROR;
|
2012-09-18 15:30:47 -03:00
|
|
|
|
|
|
|
// Any wallet corruption at all: skip any rewriting or
|
|
|
|
// upgrading, we don't want to make it worse.
|
2018-03-09 11:03:40 -03:00
|
|
|
if (result != DBErrors::LOAD_OK)
|
2012-09-18 15:30:47 -03:00
|
|
|
return result;
|
2012-04-15 18:39:49 -03:00
|
|
|
|
2019-03-13 01:37:08 -03:00
|
|
|
// Last client version to open this wallet, was previously the file version number
|
|
|
|
int last_client = CLIENT_VERSION;
|
2020-06-15 16:54:58 -04:00
|
|
|
m_batch->Read(DBKeys::VERSION, last_client);
|
2019-03-13 01:37:08 -03:00
|
|
|
|
2019-03-13 01:38:24 -03:00
|
|
|
int wallet_version = pwallet->GetVersion();
|
|
|
|
pwallet->WalletLogPrintf("Wallet File Version = %d\n", wallet_version > 0 ? wallet_version : last_client);
|
2012-04-15 18:39:49 -03:00
|
|
|
|
2018-06-15 19:02:52 -04:00
|
|
|
pwallet->WalletLogPrintf("Keys: %u plaintext, %u encrypted, %u w/ metadata, %u total. Unknown wallet records: %u\n",
|
2018-04-04 21:46:40 -03:00
|
|
|
wss.nKeys, wss.nCKeys, wss.nKeyMeta, wss.nKeys + wss.nCKeys, wss.m_unknown_records);
|
2013-06-10 09:36:29 -04:00
|
|
|
|
|
|
|
// nTimeFirstKey is only reliable if all keys have metadata
|
2019-08-14 14:25:53 -04:00
|
|
|
if (pwallet->IsLegacy() && (wss.nKeys + wss.nCKeys + wss.nWatchKeys) != wss.nKeyMeta) {
|
2019-10-07 15:11:34 -03:00
|
|
|
auto spk_man = pwallet->GetOrCreateLegacyScriptPubKeyMan();
|
2019-10-07 15:11:34 -03:00
|
|
|
if (spk_man) {
|
2019-10-07 15:11:34 -03:00
|
|
|
LOCK(spk_man->cs_KeyStore);
|
2019-10-07 15:11:34 -03:00
|
|
|
spk_man->UpdateTimeFirstKey(1);
|
|
|
|
}
|
|
|
|
}
|
2013-06-10 09:36:29 -04:00
|
|
|
|
2018-06-18 01:58:28 -04:00
|
|
|
for (const uint256& hash : wss.vWalletUpgrade)
|
2017-01-19 18:08:03 -03:00
|
|
|
WriteTx(pwallet->mapWallet.at(hash));
|
2012-04-15 18:39:49 -03:00
|
|
|
|
|
|
|
// Rewrite encrypted wallets of versions 0.4.0 and 0.5.0rc:
|
2019-03-13 01:37:08 -03:00
|
|
|
if (wss.fIsEncrypted && (last_client == 40000 || last_client == 50000))
|
2018-03-09 11:03:40 -03:00
|
|
|
return DBErrors::NEED_REWRITE;
|
2012-04-15 18:39:49 -03:00
|
|
|
|
2019-03-13 01:37:08 -03:00
|
|
|
if (last_client < CLIENT_VERSION) // Update
|
2020-06-15 16:54:58 -04:00
|
|
|
m_batch->Write(DBKeys::VERSION, CLIENT_VERSION);
|
2012-04-15 18:39:49 -03:00
|
|
|
|
2013-06-10 09:36:29 -04:00
|
|
|
if (wss.fAnyUnordered)
|
2016-09-28 12:57:25 -03:00
|
|
|
result = pwallet->ReorderTransactions();
|
2012-05-27 19:06:09 -04:00
|
|
|
|
2018-11-06 11:23:37 -03:00
|
|
|
// Upgrade all of the wallet keymetadata to have the hd master key id
|
|
|
|
// This operation is not atomic, but if it fails, updated entries are still backwards compatible with older software
|
|
|
|
try {
|
|
|
|
pwallet->UpgradeKeyMetadata();
|
|
|
|
} catch (...) {
|
|
|
|
result = DBErrors::CORRUPT;
|
|
|
|
}
|
|
|
|
|
2019-12-05 17:23:05 -03:00
|
|
|
// Set the inactive chain
|
|
|
|
if (wss.m_hd_chains.size() > 0) {
|
|
|
|
LegacyScriptPubKeyMan* legacy_spkm = pwallet->GetLegacyScriptPubKeyMan();
|
|
|
|
if (!legacy_spkm) {
|
|
|
|
pwallet->WalletLogPrintf("Inactive HD Chains found but no Legacy ScriptPubKeyMan\n");
|
|
|
|
return DBErrors::CORRUPT;
|
|
|
|
}
|
|
|
|
for (const auto& chain_pair : wss.m_hd_chains) {
|
|
|
|
if (chain_pair.first != pwallet->GetLegacyScriptPubKeyMan()->GetHDChain().seed_id) {
|
|
|
|
pwallet->GetLegacyScriptPubKeyMan()->AddInactiveHDChain(chain_pair.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-18 15:30:47 -03:00
|
|
|
return result;
|
2012-04-15 18:39:49 -03:00
|
|
|
}
|
|
|
|
|
2017-11-20 14:51:45 -03:00
|
|
|
DBErrors WalletBatch::FindWalletTx(std::vector<uint256>& vTxHash, std::list<CWalletTx>& vWtx)
|
2014-02-14 13:33:07 -03:00
|
|
|
{
|
2018-03-09 11:03:40 -03:00
|
|
|
DBErrors result = DBErrors::LOAD_OK;
|
2014-02-14 13:33:07 -03:00
|
|
|
|
|
|
|
try {
|
|
|
|
int nMinVersion = 0;
|
2020-06-15 16:54:58 -04:00
|
|
|
if (m_batch->Read(DBKeys::MINVERSION, nMinVersion)) {
|
2018-06-18 18:21:32 -04:00
|
|
|
if (nMinVersion > FEATURE_LATEST)
|
2018-03-09 11:03:40 -03:00
|
|
|
return DBErrors::TOO_NEW;
|
2014-02-14 13:33:07 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get cursor
|
2020-06-15 16:54:58 -04:00
|
|
|
if (!m_batch->StartCursor())
|
2014-02-14 13:33:07 -03:00
|
|
|
{
|
|
|
|
LogPrintf("Error getting wallet database cursor\n");
|
2018-03-09 11:03:40 -03:00
|
|
|
return DBErrors::CORRUPT;
|
2014-02-14 13:33:07 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
// Read next record
|
|
|
|
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
|
|
|
|
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
|
2020-05-14 21:17:01 -04:00
|
|
|
bool complete;
|
2020-06-15 16:54:58 -04:00
|
|
|
bool ret = m_batch->ReadAtCursor(ssKey, ssValue, complete);
|
2020-05-14 21:17:01 -04:00
|
|
|
if (complete) {
|
2014-02-14 13:33:07 -03:00
|
|
|
break;
|
2020-05-14 21:17:01 -04:00
|
|
|
} else if (!ret) {
|
2020-06-15 16:54:58 -04:00
|
|
|
m_batch->CloseCursor();
|
2014-02-14 13:33:07 -03:00
|
|
|
LogPrintf("Error reading next record from wallet database\n");
|
2018-03-09 11:03:40 -03:00
|
|
|
return DBErrors::CORRUPT;
|
2014-02-14 13:33:07 -03:00
|
|
|
}
|
|
|
|
|
2017-01-26 22:33:45 -03:00
|
|
|
std::string strType;
|
2014-02-14 13:33:07 -03:00
|
|
|
ssKey >> strType;
|
2019-07-27 15:50:36 -04:00
|
|
|
if (strType == DBKeys::TX) {
|
2014-02-14 13:33:07 -03:00
|
|
|
uint256 hash;
|
|
|
|
ssKey >> hash;
|
|
|
|
vTxHash.push_back(hash);
|
2017-11-20 14:51:45 -03:00
|
|
|
vWtx.emplace_back(nullptr /* wallet */, nullptr /* tx */);
|
|
|
|
ssValue >> vWtx.back();
|
2014-02-14 13:33:07 -03:00
|
|
|
}
|
|
|
|
}
|
2020-06-02 07:14:04 -04:00
|
|
|
} catch (...) {
|
2018-03-09 11:03:40 -03:00
|
|
|
result = DBErrors::CORRUPT;
|
2014-02-14 13:33:07 -03:00
|
|
|
}
|
2020-06-15 16:54:58 -04:00
|
|
|
m_batch->CloseCursor();
|
2014-02-14 13:33:07 -03:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-12-08 08:39:22 -03:00
|
|
|
DBErrors WalletBatch::ZapSelectTx(std::vector<uint256>& vTxHashIn, std::vector<uint256>& vTxHashOut)
|
2016-03-07 10:51:06 -03:00
|
|
|
{
|
|
|
|
// build list of wallet TXs and hashes
|
2017-01-26 22:33:45 -03:00
|
|
|
std::vector<uint256> vTxHash;
|
2017-11-20 14:51:45 -03:00
|
|
|
std::list<CWalletTx> vWtx;
|
2016-11-12 06:53:18 -03:00
|
|
|
DBErrors err = FindWalletTx(vTxHash, vWtx);
|
2018-03-09 11:03:40 -03:00
|
|
|
if (err != DBErrors::LOAD_OK) {
|
2016-03-07 10:51:06 -03:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::sort(vTxHash.begin(), vTxHash.end());
|
|
|
|
std::sort(vTxHashIn.begin(), vTxHashIn.end());
|
|
|
|
|
|
|
|
// erase each matching wallet TX
|
|
|
|
bool delerror = false;
|
2017-01-26 22:33:45 -03:00
|
|
|
std::vector<uint256>::iterator it = vTxHashIn.begin();
|
2018-06-18 01:58:28 -04:00
|
|
|
for (const uint256& hash : vTxHash) {
|
2016-03-07 10:51:06 -03:00
|
|
|
while (it < vTxHashIn.end() && (*it) < hash) {
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
if (it == vTxHashIn.end()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if ((*it) == hash) {
|
|
|
|
if(!EraseTx(hash)) {
|
2019-11-08 04:15:37 -03:00
|
|
|
LogPrint(BCLog::WALLETDB, "Transaction was found for deletion but returned database error: %s\n", hash.GetHex());
|
2016-03-07 10:51:06 -03:00
|
|
|
delerror = true;
|
|
|
|
}
|
|
|
|
vTxHashOut.push_back(hash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (delerror) {
|
2018-03-09 11:03:40 -03:00
|
|
|
return DBErrors::CORRUPT;
|
2016-03-07 10:51:06 -03:00
|
|
|
}
|
2018-03-09 11:03:40 -03:00
|
|
|
return DBErrors::LOAD_OK;
|
2016-03-07 10:51:06 -03:00
|
|
|
}
|
|
|
|
|
2017-11-20 14:51:45 -03:00
|
|
|
DBErrors WalletBatch::ZapWalletTx(std::list<CWalletTx>& vWtx)
|
2014-02-14 13:33:07 -03:00
|
|
|
{
|
|
|
|
// build list of wallet TXs
|
2017-01-26 22:33:45 -03:00
|
|
|
std::vector<uint256> vTxHash;
|
2016-11-12 06:53:18 -03:00
|
|
|
DBErrors err = FindWalletTx(vTxHash, vWtx);
|
2018-03-09 11:03:40 -03:00
|
|
|
if (err != DBErrors::LOAD_OK)
|
2014-02-14 13:33:07 -03:00
|
|
|
return err;
|
|
|
|
|
|
|
|
// erase each wallet TX
|
2018-06-18 01:58:28 -04:00
|
|
|
for (const uint256& hash : vTxHash) {
|
2014-02-14 13:33:07 -03:00
|
|
|
if (!EraseTx(hash))
|
2018-03-09 11:03:40 -03:00
|
|
|
return DBErrors::CORRUPT;
|
2014-02-14 13:33:07 -03:00
|
|
|
}
|
|
|
|
|
2018-03-09 11:03:40 -03:00
|
|
|
return DBErrors::LOAD_OK;
|
2014-02-14 13:33:07 -03:00
|
|
|
}
|
|
|
|
|
2017-01-23 11:27:59 -03:00
|
|
|
void MaybeCompactWalletDB()
|
2012-04-15 18:39:49 -03:00
|
|
|
{
|
2017-07-13 18:25:56 -04:00
|
|
|
static std::atomic<bool> fOneThread(false);
|
2017-02-08 15:19:18 -03:00
|
|
|
if (fOneThread.exchange(true)) {
|
2012-04-15 18:39:49 -03:00
|
|
|
return;
|
2017-02-08 15:19:18 -03:00
|
|
|
}
|
2012-04-15 18:39:49 -03:00
|
|
|
|
2018-05-22 11:18:07 -04:00
|
|
|
for (const std::shared_ptr<CWallet>& pwallet : GetWallets()) {
|
2017-12-08 08:39:22 -03:00
|
|
|
WalletDatabase& dbh = pwallet->GetDBHandle();
|
2012-04-15 18:39:49 -03:00
|
|
|
|
2016-09-09 05:42:30 -03:00
|
|
|
unsigned int nUpdateCounter = dbh.nUpdateCounter;
|
2012-04-15 18:39:49 -03:00
|
|
|
|
2016-09-09 05:42:30 -03:00
|
|
|
if (dbh.nLastSeen != nUpdateCounter) {
|
|
|
|
dbh.nLastSeen = nUpdateCounter;
|
|
|
|
dbh.nLastWalletUpdate = GetTime();
|
|
|
|
}
|
2012-04-15 18:39:49 -03:00
|
|
|
|
2016-09-09 05:42:30 -03:00
|
|
|
if (dbh.nLastFlushed != nUpdateCounter && GetTime() - dbh.nLastWalletUpdate >= 2) {
|
2020-06-15 14:39:26 -04:00
|
|
|
if (dbh.PeriodicFlush()) {
|
2016-09-09 05:42:30 -03:00
|
|
|
dbh.nLastFlushed = nUpdateCounter;
|
|
|
|
}
|
2017-03-08 07:48:58 -03:00
|
|
|
}
|
2012-04-15 18:39:49 -03:00
|
|
|
}
|
2016-09-09 05:42:30 -03:00
|
|
|
|
2017-02-08 15:19:18 -03:00
|
|
|
fOneThread = false;
|
2012-04-15 18:39:49 -03:00
|
|
|
}
|
|
|
|
|
2017-12-08 08:39:22 -03:00
|
|
|
bool WalletBatch::WriteDestData(const std::string &address, const std::string &key, const std::string &value)
|
2013-11-18 12:55:54 -03:00
|
|
|
{
|
2019-07-27 15:50:36 -04:00
|
|
|
return WriteIC(std::make_pair(DBKeys::DESTDATA, std::make_pair(address, key)), value);
|
2013-11-18 12:55:54 -03:00
|
|
|
}
|
|
|
|
|
2017-12-08 08:39:22 -03:00
|
|
|
bool WalletBatch::EraseDestData(const std::string &address, const std::string &key)
|
2013-11-18 12:55:54 -03:00
|
|
|
{
|
2019-07-27 15:50:36 -04:00
|
|
|
return EraseIC(std::make_pair(DBKeys::DESTDATA, std::make_pair(address, key)));
|
2013-11-18 12:55:54 -03:00
|
|
|
}
|
2016-01-02 08:34:08 -03:00
|
|
|
|
|
|
|
|
2017-12-08 08:39:22 -03:00
|
|
|
bool WalletBatch::WriteHDChain(const CHDChain& chain)
|
2016-01-02 08:34:08 -03:00
|
|
|
{
|
2019-07-27 15:50:36 -04:00
|
|
|
return WriteIC(DBKeys::HDCHAIN, chain);
|
2016-11-27 00:32:30 -03:00
|
|
|
}
|
2017-03-08 07:48:58 -03:00
|
|
|
|
2017-02-16 10:22:18 -03:00
|
|
|
bool WalletBatch::WriteWalletFlags(const uint64_t flags)
|
|
|
|
{
|
2019-07-27 15:50:36 -04:00
|
|
|
return WriteIC(DBKeys::FLAGS, flags);
|
2017-02-16 10:22:18 -03:00
|
|
|
}
|
|
|
|
|
2017-12-08 08:39:22 -03:00
|
|
|
bool WalletBatch::TxnBegin()
|
2017-03-08 13:20:08 -03:00
|
|
|
{
|
2020-06-15 16:54:58 -04:00
|
|
|
return m_batch->TxnBegin();
|
2017-03-08 13:20:08 -03:00
|
|
|
}
|
|
|
|
|
2017-12-08 08:39:22 -03:00
|
|
|
bool WalletBatch::TxnCommit()
|
2017-03-08 13:20:08 -03:00
|
|
|
{
|
2020-06-15 16:54:58 -04:00
|
|
|
return m_batch->TxnCommit();
|
2017-03-08 13:20:08 -03:00
|
|
|
}
|
|
|
|
|
2017-12-08 08:39:22 -03:00
|
|
|
bool WalletBatch::TxnAbort()
|
2017-03-08 13:20:08 -03:00
|
|
|
{
|
2020-06-15 16:54:58 -04:00
|
|
|
return m_batch->TxnAbort();
|
2017-03-08 13:20:08 -03:00
|
|
|
}
|
2020-06-15 16:10:08 -04:00
|
|
|
|
|
|
|
bool IsWalletLoaded(const fs::path& wallet_path)
|
|
|
|
{
|
|
|
|
return IsBDBWalletLoaded(wallet_path);
|
|
|
|
}
|
2020-06-17 12:25:22 -04:00
|
|
|
|
|
|
|
/** Return object for accessing database at specified path. */
|
2020-06-15 16:24:00 -04:00
|
|
|
std::unique_ptr<WalletDatabase> CreateWalletDatabase(const fs::path& path)
|
2020-06-17 12:25:22 -04:00
|
|
|
{
|
|
|
|
std::string filename;
|
|
|
|
return MakeUnique<BerkeleyDatabase>(GetWalletEnv(path, filename), std::move(filename));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return object for accessing dummy database with no read/write capabilities. */
|
2020-06-15 16:24:00 -04:00
|
|
|
std::unique_ptr<WalletDatabase> CreateDummyWalletDatabase()
|
2020-06-17 12:25:22 -04:00
|
|
|
{
|
|
|
|
return MakeUnique<BerkeleyDatabase>();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Return object for accessing temporary in-memory database. */
|
2020-06-15 16:24:00 -04:00
|
|
|
std::unique_ptr<WalletDatabase> CreateMockWalletDatabase()
|
2020-06-17 12:25:22 -04:00
|
|
|
{
|
|
|
|
return MakeUnique<BerkeleyDatabase>(std::make_shared<BerkeleyEnvironment>(), "");
|
|
|
|
}
|