Merge #8061: [Wallet] Improve Wallet encapsulation
380498a
Move BackupWallet to CWallet::BackupWallet (Patrick Strateman)ecb9741
Move GetAccountBalance from rpcwallet.cpp into CWallet::GetAccountBalance (Patrick Strateman)
This commit is contained in:
commit
f6b7df3155
6 changed files with 80 additions and 78 deletions
|
@ -447,7 +447,7 @@ bool WalletModel::changePassphrase(const SecureString &oldPass, const SecureStri
|
||||||
|
|
||||||
bool WalletModel::backupWallet(const QString &filename)
|
bool WalletModel::backupWallet(const QString &filename)
|
||||||
{
|
{
|
||||||
return BackupWallet(*wallet, filename.toLocal8Bit().data());
|
return wallet->BackupWallet(filename.toLocal8Bit().data());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handlers for core signals
|
// Handlers for core signals
|
||||||
|
|
|
@ -673,38 +673,6 @@ UniValue getreceivedbyaccount(const UniValue& params, bool fHelp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
CAmount GetAccountBalance(CWalletDB& walletdb, const string& strAccount, int nMinDepth, const isminefilter& filter)
|
|
||||||
{
|
|
||||||
CAmount nBalance = 0;
|
|
||||||
|
|
||||||
// Tally wallet transactions
|
|
||||||
for (map<uint256, CWalletTx>::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it)
|
|
||||||
{
|
|
||||||
const CWalletTx& wtx = (*it).second;
|
|
||||||
if (!CheckFinalTx(wtx) || wtx.GetBlocksToMaturity() > 0 || wtx.GetDepthInMainChain() < 0)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
CAmount nReceived, nSent, nFee;
|
|
||||||
wtx.GetAccountAmounts(strAccount, nReceived, nSent, nFee, filter);
|
|
||||||
|
|
||||||
if (nReceived != 0 && wtx.GetDepthInMainChain() >= nMinDepth)
|
|
||||||
nBalance += nReceived;
|
|
||||||
nBalance -= nSent + nFee;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Tally internal accounting entries
|
|
||||||
nBalance += walletdb.GetAccountCreditDebit(strAccount);
|
|
||||||
|
|
||||||
return nBalance;
|
|
||||||
}
|
|
||||||
|
|
||||||
CAmount GetAccountBalance(const string& strAccount, int nMinDepth, const isminefilter& filter)
|
|
||||||
{
|
|
||||||
CWalletDB walletdb(pwalletMain->strWalletFile);
|
|
||||||
return GetAccountBalance(walletdb, strAccount, nMinDepth, filter);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
UniValue getbalance(const UniValue& params, bool fHelp)
|
UniValue getbalance(const UniValue& params, bool fHelp)
|
||||||
{
|
{
|
||||||
if (!EnsureWalletIsAvailable(fHelp))
|
if (!EnsureWalletIsAvailable(fHelp))
|
||||||
|
@ -775,7 +743,7 @@ UniValue getbalance(const UniValue& params, bool fHelp)
|
||||||
|
|
||||||
string strAccount = AccountFromValue(params[0]);
|
string strAccount = AccountFromValue(params[0]);
|
||||||
|
|
||||||
CAmount nBalance = GetAccountBalance(strAccount, nMinDepth, filter);
|
CAmount nBalance = pwalletMain->GetAccountBalance(strAccount, nMinDepth, filter);
|
||||||
|
|
||||||
return ValueFromAmount(nBalance);
|
return ValueFromAmount(nBalance);
|
||||||
}
|
}
|
||||||
|
@ -923,7 +891,7 @@ UniValue sendfrom(const UniValue& params, bool fHelp)
|
||||||
EnsureWalletIsUnlocked();
|
EnsureWalletIsUnlocked();
|
||||||
|
|
||||||
// Check funds
|
// Check funds
|
||||||
CAmount nBalance = GetAccountBalance(strAccount, nMinDepth, ISMINE_SPENDABLE);
|
CAmount nBalance = pwalletMain->GetAccountBalance(strAccount, nMinDepth, ISMINE_SPENDABLE);
|
||||||
if (nAmount > nBalance)
|
if (nAmount > nBalance)
|
||||||
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Account has insufficient funds");
|
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Account has insufficient funds");
|
||||||
|
|
||||||
|
@ -1026,7 +994,7 @@ UniValue sendmany(const UniValue& params, bool fHelp)
|
||||||
EnsureWalletIsUnlocked();
|
EnsureWalletIsUnlocked();
|
||||||
|
|
||||||
// Check funds
|
// Check funds
|
||||||
CAmount nBalance = GetAccountBalance(strAccount, nMinDepth, ISMINE_SPENDABLE);
|
CAmount nBalance = pwalletMain->GetAccountBalance(strAccount, nMinDepth, ISMINE_SPENDABLE);
|
||||||
if (totalAmount > nBalance)
|
if (totalAmount > nBalance)
|
||||||
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Account has insufficient funds");
|
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Account has insufficient funds");
|
||||||
|
|
||||||
|
@ -1836,7 +1804,7 @@ UniValue backupwallet(const UniValue& params, bool fHelp)
|
||||||
LOCK2(cs_main, pwalletMain->cs_wallet);
|
LOCK2(cs_main, pwalletMain->cs_wallet);
|
||||||
|
|
||||||
string strDest = params[0].get_str();
|
string strDest = params[0].get_str();
|
||||||
if (!BackupWallet(*pwalletMain, strDest))
|
if (!pwalletMain->BackupWallet(strDest))
|
||||||
throw JSONRPCError(RPC_WALLET_ERROR, "Error: Wallet backup failed!");
|
throw JSONRPCError(RPC_WALLET_ERROR, "Error: Wallet backup failed!");
|
||||||
|
|
||||||
return NullUniValue;
|
return NullUniValue;
|
||||||
|
|
|
@ -2759,6 +2759,37 @@ set< set<CTxDestination> > CWallet::GetAddressGroupings()
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CAmount CWallet::GetAccountBalance(const std::string& strAccount, int nMinDepth, const isminefilter& filter)
|
||||||
|
{
|
||||||
|
CWalletDB walletdb(strWalletFile);
|
||||||
|
return GetAccountBalance(walletdb, strAccount, nMinDepth, filter);
|
||||||
|
}
|
||||||
|
|
||||||
|
CAmount CWallet::GetAccountBalance(CWalletDB& walletdb, const std::string& strAccount, int nMinDepth, const isminefilter& filter)
|
||||||
|
{
|
||||||
|
CAmount nBalance = 0;
|
||||||
|
|
||||||
|
// Tally wallet transactions
|
||||||
|
for (map<uint256, CWalletTx>::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
|
||||||
|
{
|
||||||
|
const CWalletTx& wtx = (*it).second;
|
||||||
|
if (!CheckFinalTx(wtx) || wtx.GetBlocksToMaturity() > 0 || wtx.GetDepthInMainChain() < 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
CAmount nReceived, nSent, nFee;
|
||||||
|
wtx.GetAccountAmounts(strAccount, nReceived, nSent, nFee, filter);
|
||||||
|
|
||||||
|
if (nReceived != 0 && wtx.GetDepthInMainChain() >= nMinDepth)
|
||||||
|
nBalance += nReceived;
|
||||||
|
nBalance -= nSent + nFee;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tally internal accounting entries
|
||||||
|
nBalance += walletdb.GetAccountCreditDebit(strAccount);
|
||||||
|
|
||||||
|
return nBalance;
|
||||||
|
}
|
||||||
|
|
||||||
std::set<CTxDestination> CWallet::GetAccountAddresses(const std::string& strAccount) const
|
std::set<CTxDestination> CWallet::GetAccountAddresses(const std::string& strAccount) const
|
||||||
{
|
{
|
||||||
LOCK(cs_wallet);
|
LOCK(cs_wallet);
|
||||||
|
@ -3252,6 +3283,46 @@ bool CWallet::ParameterInteraction()
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool CWallet::BackupWallet(const std::string& strDest)
|
||||||
|
{
|
||||||
|
if (!fFileBacked)
|
||||||
|
return false;
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
{
|
||||||
|
LOCK(bitdb.cs_db);
|
||||||
|
if (!bitdb.mapFileUseCount.count(strWalletFile) || bitdb.mapFileUseCount[strWalletFile] == 0)
|
||||||
|
{
|
||||||
|
// Flush log data to the dat file
|
||||||
|
bitdb.CloseDb(strWalletFile);
|
||||||
|
bitdb.CheckpointLSN(strWalletFile);
|
||||||
|
bitdb.mapFileUseCount.erase(strWalletFile);
|
||||||
|
|
||||||
|
// Copy wallet file
|
||||||
|
boost::filesystem::path pathSrc = GetDataDir() / strWalletFile;
|
||||||
|
boost::filesystem::path pathDest(strDest);
|
||||||
|
if (boost::filesystem::is_directory(pathDest))
|
||||||
|
pathDest /= strWalletFile;
|
||||||
|
|
||||||
|
try {
|
||||||
|
#if BOOST_VERSION >= 104000
|
||||||
|
boost::filesystem::copy_file(pathSrc, pathDest, boost::filesystem::copy_option::overwrite_if_exists);
|
||||||
|
#else
|
||||||
|
boost::filesystem::copy_file(pathSrc, pathDest);
|
||||||
|
#endif
|
||||||
|
LogPrintf("copied %s to %s\n", strWalletFile, pathDest.string());
|
||||||
|
return true;
|
||||||
|
} catch (const boost::filesystem::filesystem_error& e) {
|
||||||
|
LogPrintf("error copying %s to %s - %s\n", strWalletFile, pathDest.string(), e.what());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
MilliSleep(100);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
CKeyPool::CKeyPool()
|
CKeyPool::CKeyPool()
|
||||||
{
|
{
|
||||||
nTime = GetTime();
|
nTime = GetTime();
|
||||||
|
|
|
@ -776,6 +776,8 @@ public:
|
||||||
std::set< std::set<CTxDestination> > GetAddressGroupings();
|
std::set< std::set<CTxDestination> > GetAddressGroupings();
|
||||||
std::map<CTxDestination, CAmount> GetAddressBalances();
|
std::map<CTxDestination, CAmount> GetAddressBalances();
|
||||||
|
|
||||||
|
CAmount GetAccountBalance(const std::string& strAccount, int nMinDepth, const isminefilter& filter);
|
||||||
|
CAmount GetAccountBalance(CWalletDB& walletdb, const std::string& strAccount, int nMinDepth, const isminefilter& filter);
|
||||||
std::set<CTxDestination> GetAccountAddresses(const std::string& strAccount) const;
|
std::set<CTxDestination> GetAccountAddresses(const std::string& strAccount) const;
|
||||||
|
|
||||||
isminetype IsMine(const CTxIn& txin) const;
|
isminetype IsMine(const CTxIn& txin) const;
|
||||||
|
@ -883,6 +885,8 @@ public:
|
||||||
|
|
||||||
/* Wallets parameter interaction */
|
/* Wallets parameter interaction */
|
||||||
static bool ParameterInteraction();
|
static bool ParameterInteraction();
|
||||||
|
|
||||||
|
bool BackupWallet(const std::string& strDest);
|
||||||
};
|
};
|
||||||
|
|
||||||
/** A key allocated from the key pool. */
|
/** A key allocated from the key pool. */
|
||||||
|
|
|
@ -903,46 +903,6 @@ void ThreadFlushWalletDB(const string& strFile)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BackupWallet(const CWallet& wallet, const string& strDest)
|
|
||||||
{
|
|
||||||
if (!wallet.fFileBacked)
|
|
||||||
return false;
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
{
|
|
||||||
LOCK(bitdb.cs_db);
|
|
||||||
if (!bitdb.mapFileUseCount.count(wallet.strWalletFile) || bitdb.mapFileUseCount[wallet.strWalletFile] == 0)
|
|
||||||
{
|
|
||||||
// Flush log data to the dat file
|
|
||||||
bitdb.CloseDb(wallet.strWalletFile);
|
|
||||||
bitdb.CheckpointLSN(wallet.strWalletFile);
|
|
||||||
bitdb.mapFileUseCount.erase(wallet.strWalletFile);
|
|
||||||
|
|
||||||
// Copy wallet file
|
|
||||||
boost::filesystem::path pathSrc = GetDataDir() / wallet.strWalletFile;
|
|
||||||
boost::filesystem::path pathDest(strDest);
|
|
||||||
if (boost::filesystem::is_directory(pathDest))
|
|
||||||
pathDest /= wallet.strWalletFile;
|
|
||||||
|
|
||||||
try {
|
|
||||||
#if BOOST_VERSION >= 104000
|
|
||||||
boost::filesystem::copy_file(pathSrc, pathDest, boost::filesystem::copy_option::overwrite_if_exists);
|
|
||||||
#else
|
|
||||||
boost::filesystem::copy_file(pathSrc, pathDest);
|
|
||||||
#endif
|
|
||||||
LogPrintf("copied %s to %s\n", wallet.strWalletFile, pathDest.string());
|
|
||||||
return true;
|
|
||||||
} catch (const boost::filesystem::filesystem_error& e) {
|
|
||||||
LogPrintf("error copying %s to %s - %s\n", wallet.strWalletFile, pathDest.string(), e.what());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
MilliSleep(100);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// Try to (very carefully!) recover wallet file if there is a problem.
|
// Try to (very carefully!) recover wallet file if there is a problem.
|
||||||
//
|
//
|
||||||
|
|
|
@ -141,7 +141,6 @@ private:
|
||||||
bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry);
|
bool WriteAccountingEntry(const uint64_t nAccEntryNum, const CAccountingEntry& acentry);
|
||||||
};
|
};
|
||||||
|
|
||||||
bool BackupWallet(const CWallet& wallet, const std::string& strDest);
|
|
||||||
void ThreadFlushWalletDB(const std::string& strFile);
|
void ThreadFlushWalletDB(const std::string& strFile);
|
||||||
|
|
||||||
#endif // BITCOIN_WALLET_WALLETDB_H
|
#endif // BITCOIN_WALLET_WALLETDB_H
|
||||||
|
|
Loading…
Reference in a new issue