From f090c0e0d057b2bebea9918a883e9a25f97c5ffa Mon Sep 17 00:00:00 2001 From: svost Date: Fri, 11 Feb 2022 21:36:16 +0300 Subject: [PATCH] Wallet.h cleanup --- src/init.cpp | 5 ++- src/wallet.cpp | 43 +++++++++++++++++++++++++++---- src/wallet.h | 77 ++++++++++++------------------------------------------- src/walletdb.h | 1 - 4 files changed, 57 insertions(+), 69 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 9d42a55..9ada937 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -2,16 +2,17 @@ // Copyright (c) 2009-2012 The Bitcoin developers // Distributed under the MIT/X11 software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include "init.h" +#include "wallet.h" +#include "base58.h" #include "txdb-leveldb.h" #include "walletdb.h" #include "bitcoinrpc.h" #include "net.h" -#include "init.h" #include "util.h" #include "ipcollector.h" #include "interface.h" #include "checkpoints.h" -#include "wallet.h" #include #include diff --git a/src/wallet.cpp b/src/wallet.cpp index 3ed6c9e..75c9f31 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -3,15 +3,13 @@ // Distributed under the MIT/X11 software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#include "txdb-leveldb.h" #include "wallet.h" -#include "walletdb.h" -#include "crypter.h" -#include "interface.h" +#include "txdb-leveldb.h" #include "base58.h" -#include "kernel.h" #include "coincontrol.h" -#include "main.h" +#include "crypter.h" +#include "kernel.h" +#include "walletdb.h" #include @@ -136,6 +134,8 @@ bool CWallet::AddCryptedMalleableKey(const CMalleableKeyView& keyView, const std return true; } +bool CWallet::LoadCryptedKey(const CPubKey &vchPubKey, const std::vector &vchCryptedSecret) { SetMinVersion(FEATURE_WALLETCRYPT); return CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret); } + bool CWallet::AddCryptedKey(const CPubKey &vchPubKey, const vector &vchCryptedSecret) { if (!CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret)) @@ -178,6 +178,12 @@ bool CWallet::LoadKeyMetadata(const CMalleableKeyView &keyView, const CKeyMetada return true; } +bool CWallet::LoadKey(const CMalleableKeyView &keyView, const CSecret &vchSecretH) { return CCryptoKeyStore::AddMalleableKey(keyView, vchSecretH); } + +bool CWallet::LoadCryptedKey(const CMalleableKeyView &keyView, const std::vector &vchCryptedSecretH) { return CCryptoKeyStore::AddCryptedMalleableKey(keyView, vchCryptedSecretH); } + +bool CWallet::LoadMinVersion(int nVersion) { nWalletVersion = nVersion; nWalletMaxVersion = std::max(nWalletMaxVersion, nVersion); return true; } + bool CWallet::AddCScript(const CScript& redeemScript) { if (!CCryptoKeyStore::AddCScript(redeemScript)) @@ -1314,6 +1320,11 @@ void CWalletTx::GetAccountAmounts(const string& strAccount, int64_t& nGenerated, } } +bool CWalletTx::IsFromMe(const isminefilter &filter) const +{ + return (GetDebit(filter) > 0); +} + void CWalletTx::AddSupportingTransactions(CTxDB& txdb) { vtxPrev.clear(); @@ -3247,6 +3258,26 @@ void CWallet::ClearOrphans() } +CWalletTx::CWalletTx() +{ + Init(nullptr); +} + +CWalletTx::CWalletTx(const CWallet *pwalletIn) +{ + Init(pwalletIn); +} + +CWalletTx::CWalletTx(const CWallet *pwalletIn, const CMerkleTx &txIn) : CMerkleTx(txIn) +{ + Init(pwalletIn); +} + +CWalletTx::CWalletTx(const CWallet *pwalletIn, const CTransaction &txIn) : CMerkleTx(txIn) +{ + Init(pwalletIn); +} + void CWalletTx::Init(const CWallet *pwalletIn) { pwallet = pwalletIn; diff --git a/src/wallet.h b/src/wallet.h index 208e261..5ed8dfa 100644 --- a/src/wallet.h +++ b/src/wallet.h @@ -5,19 +5,9 @@ #ifndef BITCOIN_WALLET_H #define BITCOIN_WALLET_H -#include -#include - -#include - #include "main.h" -#include "key.h" -#include "keystore.h" -#include "script.h" #include "interface.h" -#include "util.h" #include "walletdb.h" -#include "base58.h" extern unsigned int nStakeMaxAge; extern bool fWalletUnlockMintOnly; @@ -143,16 +133,16 @@ public: bool LoadKeyMetadata(const CMalleableKeyView &keyView, const CKeyMetadata &metadata); // Load malleable key without saving it to disk (used by LoadWallet) - bool LoadKey(const CMalleableKeyView &keyView, const CSecret &vchSecretH) { return CCryptoKeyStore::AddMalleableKey(keyView, vchSecretH); } - bool LoadCryptedKey(const CMalleableKeyView &keyView, const std::vector &vchCryptedSecretH) { return CCryptoKeyStore::AddCryptedMalleableKey(keyView, vchCryptedSecretH); } + bool LoadKey(const CMalleableKeyView &keyView, const CSecret &vchSecretH); + bool LoadCryptedKey(const CMalleableKeyView &keyView, const std::vector &vchCryptedSecretH); - bool LoadMinVersion(int nVersion) { nWalletVersion = nVersion; nWalletMaxVersion = std::max(nWalletMaxVersion, nVersion); return true; } + bool LoadMinVersion(int nVersion); // Adds an encrypted key to the store, and saves it to disk. bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector &vchCryptedSecret); bool AddCryptedMalleableKey(const CMalleableKeyView& keyView, const std::vector &vchCryptedSecretH); // Adds an encrypted key to the store, without saving it to disk (used by LoadWallet) - bool LoadCryptedKey(const CPubKey &vchPubKey, const std::vector &vchCryptedSecret) { SetMinVersion(FEATURE_WALLETCRYPT); return CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret); } + bool LoadCryptedKey(const CPubKey &vchPubKey, const std::vector &vchCryptedSecret); bool AddCScript(const CScript& redeemScript); bool LoadCScript(const CScript& redeemScript); @@ -304,25 +294,6 @@ public: typedef std::map mapValue_t; -static void ReadOrderPos(int64_t& nOrderPos, mapValue_t& mapValue) -{ - if (!mapValue.count("n")) - { - nOrderPos = -1; // TODO: calculate elsewhere - return; - } - nOrderPos = atoi64(mapValue["n"].c_str()); -} - - -static void WriteOrderPos(const int64_t& nOrderPos, mapValue_t& mapValue) -{ - if (nOrderPos == -1) - return; - mapValue["n"] = i64tostr(nOrderPos); -} - - /** A transaction with a bunch of additional info that only the owner cares about. * It includes any unrecorded transactions needed to link it back to the block chain. */ @@ -363,25 +334,10 @@ public: mutable int64_t nAvailableWatchCreditCached; mutable int64_t nChangeCached; - CWalletTx() - { - Init(NULL); - } - - CWalletTx(const CWallet* pwalletIn) - { - Init(pwalletIn); - } - - CWalletTx(const CWallet* pwalletIn, const CMerkleTx& txIn) : CMerkleTx(txIn) - { - Init(pwalletIn); - } - - CWalletTx(const CWallet* pwalletIn, const CTransaction& txIn) : CMerkleTx(txIn) - { - Init(pwalletIn); - } + CWalletTx(); + CWalletTx(const CWallet* pwalletIn); + CWalletTx(const CWallet* pwalletIn, const CMerkleTx& txIn); + CWalletTx(const CWallet* pwalletIn, const CTransaction& txIn); void Init(const CWallet* pwalletIn); @@ -405,7 +361,8 @@ public: } pthis->mapValue["spent"] = str; - WriteOrderPos(pthis->nOrderPos, pthis->mapValue); + if (pthis->nOrderPos != -1) + pthis->mapValue["n"] = i64tostr(pthis->nOrderPos); if (nTimeSmart) pthis->mapValue["timesmart"] = strprintf("%u", nTimeSmart); @@ -430,7 +387,8 @@ public: else pthis->vfSpent.assign(vout.size(), fSpent); - ReadOrderPos(pthis->nOrderPos, pthis->mapValue); + const auto it_op = pthis->mapValue.find("n"); + pthis->nOrderPos = (it_op != pthis->mapValue.end()) ? atoi64(it_op->second.c_str()) : -1; pthis->nTimeSmart = mapValue.count("timesmart") ? (unsigned int)atoi64(pthis->mapValue["timesmart"]) : 0; } @@ -467,10 +425,7 @@ public: void GetAccountAmounts(const std::string& strAccount, int64_t& nGenerated, int64_t& nReceived, int64_t& nSent, int64_t& nFee, const isminefilter& filter) const; - bool IsFromMe(const isminefilter& filter) const - { - return (GetDebit(filter) > 0); - } + bool IsFromMe(const isminefilter& filter) const; bool InMempool() const; bool IsTrusted() const; @@ -590,7 +545,8 @@ public: if (!fRead) { - WriteOrderPos(nOrderPos, me.mapValue); + if (nOrderPos != -1) + me.mapValue["n"] = i64tostr(nOrderPos); if (!(mapValue.empty() && _ssExtra.empty())) { @@ -614,7 +570,8 @@ public: ss >> me.mapValue; me._ssExtra = std::vector(ss.begin(), ss.end()); } - ReadOrderPos(me.nOrderPos, me.mapValue); + const auto it_op = me.mapValue.find("n"); + me.nOrderPos = (it_op != me.mapValue.end()) ? atoi64(it_op->second.c_str()) : -1; } if (std::string::npos != nSepPos) me.strComment.erase(nSepPos); diff --git a/src/walletdb.h b/src/walletdb.h index 714b3cc..b8bf4c4 100644 --- a/src/walletdb.h +++ b/src/walletdb.h @@ -6,7 +6,6 @@ #define BITCOIN_WALLETDB_H #include "db.h" -//#include "base58.h" #include "keystore.h" class CKeyPool; -- 1.7.1