Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/walletdb.cpp
Show All 14 Lines | |||||
#include <util/system.h> | #include <util/system.h> | ||||
#include <util/time.h> | #include <util/time.h> | ||||
#include <wallet/wallet.h> | #include <wallet/wallet.h> | ||||
#include <atomic> | #include <atomic> | ||||
namespace DBKeys { | namespace DBKeys { | ||||
const std::string ACENTRY{"acentry"}; | const std::string ACENTRY{"acentry"}; | ||||
const std::string ACTIVEEXTERNALSPK{"activeexternalspk"}; | |||||
const std::string ACTIVEINTERNALSPK{"activeinternalspk"}; | |||||
const std::string BESTBLOCK_NOMERKLE{"bestblock_nomerkle"}; | const std::string BESTBLOCK_NOMERKLE{"bestblock_nomerkle"}; | ||||
const std::string BESTBLOCK{"bestblock"}; | const std::string BESTBLOCK{"bestblock"}; | ||||
const std::string CRYPTED_KEY{"ckey"}; | const std::string CRYPTED_KEY{"ckey"}; | ||||
const std::string CSCRIPT{"cscript"}; | const std::string CSCRIPT{"cscript"}; | ||||
const std::string DEFAULTKEY{"defaultkey"}; | const std::string DEFAULTKEY{"defaultkey"}; | ||||
const std::string DESTDATA{"destdata"}; | const std::string DESTDATA{"destdata"}; | ||||
const std::string FLAGS{"flags"}; | const std::string FLAGS{"flags"}; | ||||
const std::string HDCHAIN{"hdchain"}; | const std::string HDCHAIN{"hdchain"}; | ||||
const std::string KEYMETA{"keymeta"}; | const std::string KEYMETA{"keymeta"}; | ||||
const std::string KEY{"key"}; | const std::string KEY{"key"}; | ||||
const std::string MASTER_KEY{"mkey"}; | const std::string MASTER_KEY{"mkey"}; | ||||
const std::string MINVERSION{"minversion"}; | const std::string MINVERSION{"minversion"}; | ||||
const std::string NAME{"name"}; | const std::string NAME{"name"}; | ||||
const std::string OLD_KEY{"wkey"}; | const std::string OLD_KEY{"wkey"}; | ||||
const std::string ORDERPOSNEXT{"orderposnext"}; | const std::string ORDERPOSNEXT{"orderposnext"}; | ||||
const std::string POOL{"pool"}; | const std::string POOL{"pool"}; | ||||
const std::string PURPOSE{"purpose"}; | const std::string PURPOSE{"purpose"}; | ||||
const std::string SETTINGS{"settings"}; | const std::string SETTINGS{"settings"}; | ||||
const std::string TX{"tx"}; | const std::string TX{"tx"}; | ||||
const std::string VERSION{"version"}; | const std::string VERSION{"version"}; | ||||
const std::string WALLETDESCRIPTOR{"walletdescriptor"}; | |||||
const std::string WATCHMETA{"watchmeta"}; | const std::string WATCHMETA{"watchmeta"}; | ||||
const std::string WATCHS{"watchs"}; | const std::string WATCHS{"watchs"}; | ||||
} // namespace DBKeys | } // namespace DBKeys | ||||
// | // | ||||
// WalletBatch | // WalletBatch | ||||
// | // | ||||
▲ Show 20 Lines • Show All 136 Lines • ▼ Show 20 Lines | |||||
bool WalletBatch::ErasePool(int64_t nPool) { | bool WalletBatch::ErasePool(int64_t nPool) { | ||||
return EraseIC(std::make_pair(DBKeys::POOL, nPool)); | return EraseIC(std::make_pair(DBKeys::POOL, nPool)); | ||||
} | } | ||||
bool WalletBatch::WriteMinVersion(int nVersion) { | bool WalletBatch::WriteMinVersion(int nVersion) { | ||||
return WriteIC(DBKeys::MINVERSION, nVersion); | return WriteIC(DBKeys::MINVERSION, nVersion); | ||||
} | } | ||||
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); | |||||
} | |||||
class CWalletScanState { | class CWalletScanState { | ||||
public: | public: | ||||
unsigned int nKeys{0}; | unsigned int nKeys{0}; | ||||
unsigned int nCKeys{0}; | unsigned int nCKeys{0}; | ||||
unsigned int nWatchKeys{0}; | unsigned int nWatchKeys{0}; | ||||
unsigned int nKeyMeta{0}; | unsigned int nKeyMeta{0}; | ||||
unsigned int m_unknown_records{0}; | unsigned int m_unknown_records{0}; | ||||
bool fIsEncrypted{false}; | bool fIsEncrypted{false}; | ||||
bool fAnyUnordered{false}; | bool fAnyUnordered{false}; | ||||
std::vector<TxId> vWalletUpgrade; | std::vector<TxId> vWalletUpgrade; | ||||
std::map<OutputType, uint256> m_active_external_spks; | |||||
std::map<OutputType, uint256> m_active_internal_spks; | |||||
CWalletScanState() {} | CWalletScanState() {} | ||||
}; | }; | ||||
static bool ReadKeyValue(CWallet *pwallet, CDataStream &ssKey, | static bool ReadKeyValue(CWallet *pwallet, CDataStream &ssKey, | ||||
CDataStream &ssValue, CWalletScanState &wss, | CDataStream &ssValue, CWalletScanState &wss, | ||||
std::string &strType, std::string &strErr) | std::string &strType, std::string &strErr) | ||||
EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet) { | EXCLUSIVE_LOCKS_REQUIRED(pwallet->cs_wallet) { | ||||
▲ Show 20 Lines • Show All 218 Lines • ▼ Show 20 Lines | try { | ||||
strErr = "Error reading wallet database: Unknown non-tolerable " | strErr = "Error reading wallet database: Unknown non-tolerable " | ||||
"wallet flags found"; | "wallet flags found"; | ||||
return false; | return false; | ||||
} | } | ||||
} else if (strType == DBKeys::OLD_KEY) { | } else if (strType == DBKeys::OLD_KEY) { | ||||
strErr = "Found unsupported 'wkey' record, try loading with " | strErr = "Found unsupported 'wkey' record, try loading with " | ||||
"version 0.20"; | "version 0.20"; | ||||
return false; | return false; | ||||
} 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; | |||||
pwallet->LoadDescriptorScriptPubKeyMan(id, desc); | |||||
} else if (strType != DBKeys::BESTBLOCK && | } else if (strType != DBKeys::BESTBLOCK && | ||||
strType != DBKeys::BESTBLOCK_NOMERKLE && | strType != DBKeys::BESTBLOCK_NOMERKLE && | ||||
strType != DBKeys::MINVERSION && | strType != DBKeys::MINVERSION && | ||||
strType != DBKeys::ACENTRY && strType != DBKeys::VERSION && | strType != DBKeys::ACENTRY && strType != DBKeys::VERSION && | ||||
strType != DBKeys::SETTINGS) { | strType != DBKeys::SETTINGS) { | ||||
wss.m_unknown_records++; | wss.m_unknown_records++; | ||||
} | } | ||||
} catch (const std::exception &e) { | } catch (const std::exception &e) { | ||||
▲ Show 20 Lines • Show All 80 Lines • ▼ Show 20 Lines | try { | ||||
} | } | ||||
pcursor->close(); | pcursor->close(); | ||||
} catch (const boost::thread_interrupted &) { | } catch (const boost::thread_interrupted &) { | ||||
throw; | throw; | ||||
} catch (...) { | } catch (...) { | ||||
result = DBErrors::CORRUPT; | result = DBErrors::CORRUPT; | ||||
} | } | ||||
// Set the active ScriptPubKeyMans | |||||
for (auto spk_man_pair : wss.m_active_external_spks) { | |||||
pwallet->SetActiveScriptPubKeyMan( | |||||
spk_man_pair.second, spk_man_pair.first, /* internal */ false, | |||||
/* memonly */ true); | |||||
} | |||||
for (auto spk_man_pair : wss.m_active_internal_spks) { | |||||
pwallet->SetActiveScriptPubKeyMan( | |||||
spk_man_pair.second, spk_man_pair.first, /* internal */ true, | |||||
/* memonly */ true); | |||||
} | |||||
if (fNoncriticalErrors && result == DBErrors::LOAD_OK) { | if (fNoncriticalErrors && result == DBErrors::LOAD_OK) { | ||||
result = DBErrors::NONCRITICAL_ERROR; | result = DBErrors::NONCRITICAL_ERROR; | ||||
} | } | ||||
// Any wallet corruption at all: skip any rewriting or upgrading, we don't | // Any wallet corruption at all: skip any rewriting or upgrading, we don't | ||||
// want to make it worse. | // want to make it worse. | ||||
if (result != DBErrors::LOAD_OK) { | if (result != DBErrors::LOAD_OK) { | ||||
return result; | return result; | ||||
▲ Show 20 Lines • Show All 299 Lines • Show Last 20 Lines |