Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/wallet.cpp
Show First 20 Lines • Show All 1,310 Lines • ▼ Show 20 Lines | bool CWallet::IsChange(const CScript &script) const { | ||||
// change). | // change). | ||||
if (IsMine(script)) { | if (IsMine(script)) { | ||||
CTxDestination address; | CTxDestination address; | ||||
if (!ExtractDestination(script, address)) { | if (!ExtractDestination(script, address)) { | ||||
return true; | return true; | ||||
} | } | ||||
LOCK(cs_wallet); | LOCK(cs_wallet); | ||||
if (!mapAddressBook.count(address)) { | if (!FindAddressBookEntry(address)) { | ||||
return true; | return true; | ||||
} | } | ||||
} | } | ||||
return false; | return false; | ||||
} | } | ||||
Amount CWallet::GetChange(const CTxOut &txout) const { | Amount CWallet::GetChange(const CTxOut &txout) const { | ||||
▲ Show 20 Lines • Show All 2,096 Lines • ▼ Show 20 Lines | |||||
bool CWallet::SetAddressBookWithDB(WalletBatch &batch, | bool CWallet::SetAddressBookWithDB(WalletBatch &batch, | ||||
const CTxDestination &address, | const CTxDestination &address, | ||||
const std::string &strName, | const std::string &strName, | ||||
const std::string &strPurpose) { | const std::string &strPurpose) { | ||||
bool fUpdated = false; | bool fUpdated = false; | ||||
{ | { | ||||
LOCK(cs_wallet); | LOCK(cs_wallet); | ||||
std::map<CTxDestination, CAddressBookData>::iterator mi = | std::map<CTxDestination, CAddressBookData>::iterator mi = | ||||
mapAddressBook.find(address); | m_address_book.find(address); | ||||
fUpdated = mi != mapAddressBook.end(); | fUpdated = (mi != m_address_book.end() && !mi->second.IsChange()); | ||||
mapAddressBook[address].name = strName; | m_address_book[address].SetLabel(strName); | ||||
// Update purpose only if requested. | // Update purpose only if requested. | ||||
if (!strPurpose.empty()) { | if (!strPurpose.empty()) { | ||||
mapAddressBook[address].purpose = strPurpose; | m_address_book[address].purpose = strPurpose; | ||||
} | } | ||||
} | } | ||||
NotifyAddressBookChanged(this, address, strName, | NotifyAddressBookChanged(this, address, strName, | ||||
IsMine(address) != ISMINE_NO, strPurpose, | IsMine(address) != ISMINE_NO, strPurpose, | ||||
(fUpdated ? CT_UPDATED : CT_NEW)); | (fUpdated ? CT_UPDATED : CT_NEW)); | ||||
if (!strPurpose.empty() && !batch.WritePurpose(address, strPurpose)) { | if (!strPurpose.empty() && !batch.WritePurpose(address, strPurpose)) { | ||||
return false; | return false; | ||||
} | } | ||||
return batch.WriteName(address, strName); | return batch.WriteName(address, strName); | ||||
} | } | ||||
bool CWallet::SetAddressBook(const CTxDestination &address, | bool CWallet::SetAddressBook(const CTxDestination &address, | ||||
const std::string &strName, | const std::string &strName, | ||||
const std::string &strPurpose) { | const std::string &strPurpose) { | ||||
WalletBatch batch(*database); | WalletBatch batch(*database); | ||||
return SetAddressBookWithDB(batch, address, strName, strPurpose); | return SetAddressBookWithDB(batch, address, strName, strPurpose); | ||||
} | } | ||||
bool CWallet::DelAddressBook(const CTxDestination &address) { | bool CWallet::DelAddressBook(const CTxDestination &address) { | ||||
// If we want to delete receiving addresses, we need to take care that | |||||
// DestData "used" (and possibly newer DestData) gets preserved (and the | |||||
// "deleted" address transformed into a change entry instead of actually | |||||
// being deleted) | |||||
// NOTE: This isn't a problem for sending addresses because they never have | |||||
// any DestData yet! When adding new DestData, it should be considered here | |||||
// whether to retain or delete it (or move it?). | |||||
assert(!IsMine(address)); | |||||
{ | { | ||||
LOCK(cs_wallet); | LOCK(cs_wallet); | ||||
// Delete destdata tuples associated with address. | // Delete destdata tuples associated with address | ||||
for (const std::pair<const std::string, std::string> &item : | for (const std::pair<const std::string, std::string> &item : | ||||
mapAddressBook[address].destdata) { | m_address_book[address].destdata) { | ||||
WalletBatch(*database).EraseDestData(address, item.first); | WalletBatch(*database).EraseDestData(address, item.first); | ||||
} | } | ||||
m_address_book.erase(address); | |||||
mapAddressBook.erase(address); | |||||
} | } | ||||
NotifyAddressBookChanged(this, address, "", IsMine(address) != ISMINE_NO, | NotifyAddressBookChanged(this, address, "", IsMine(address) != ISMINE_NO, | ||||
"", CT_DELETED); | "", CT_DELETED); | ||||
WalletBatch(*database).ErasePurpose(address); | WalletBatch(*database).ErasePurpose(address); | ||||
return WalletBatch(*database).EraseName(address); | return WalletBatch(*database).EraseName(address); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 237 Lines • ▼ Show 20 Lines | std::set<std::set<CTxDestination>> CWallet::GetAddressGroupings() { | ||||
return ret; | return ret; | ||||
} | } | ||||
std::set<CTxDestination> | std::set<CTxDestination> | ||||
CWallet::GetLabelAddresses(const std::string &label) const { | CWallet::GetLabelAddresses(const std::string &label) const { | ||||
LOCK(cs_wallet); | LOCK(cs_wallet); | ||||
std::set<CTxDestination> result; | std::set<CTxDestination> result; | ||||
for (const std::pair<const CTxDestination, CAddressBookData> &item : | for (const std::pair<const CTxDestination, CAddressBookData> &item : | ||||
mapAddressBook) { | m_address_book) { | ||||
if (item.second.IsChange()) { | |||||
continue; | |||||
} | |||||
const CTxDestination &address = item.first; | const CTxDestination &address = item.first; | ||||
const std::string &strName = item.second.name; | const std::string &strName = item.second.name; | ||||
if (strName == label) { | if (strName == label) { | ||||
result.insert(address); | result.insert(address); | ||||
} | } | ||||
} | } | ||||
return result; | return result; | ||||
▲ Show 20 Lines • Show All 195 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
bool CWallet::AddDestData(WalletBatch &batch, const CTxDestination &dest, | bool CWallet::AddDestData(WalletBatch &batch, const CTxDestination &dest, | ||||
const std::string &key, const std::string &value) { | const std::string &key, const std::string &value) { | ||||
if (boost::get<CNoDestination>(&dest)) { | if (boost::get<CNoDestination>(&dest)) { | ||||
return false; | return false; | ||||
} | } | ||||
mapAddressBook[dest].destdata.insert(std::make_pair(key, value)); | m_address_book[dest].destdata.insert(std::make_pair(key, value)); | ||||
return batch.WriteDestData(dest, key, value); | return batch.WriteDestData(dest, key, value); | ||||
} | } | ||||
bool CWallet::EraseDestData(WalletBatch &batch, const CTxDestination &dest, | bool CWallet::EraseDestData(WalletBatch &batch, const CTxDestination &dest, | ||||
const std::string &key) { | const std::string &key) { | ||||
if (!mapAddressBook[dest].destdata.erase(key)) { | if (!m_address_book[dest].destdata.erase(key)) { | ||||
return false; | return false; | ||||
} | } | ||||
return batch.EraseDestData(dest, key); | return batch.EraseDestData(dest, key); | ||||
} | } | ||||
void CWallet::LoadDestData(const CTxDestination &dest, const std::string &key, | void CWallet::LoadDestData(const CTxDestination &dest, const std::string &key, | ||||
const std::string &value) { | const std::string &value) { | ||||
mapAddressBook[dest].destdata.insert(std::make_pair(key, value)); | m_address_book[dest].destdata.insert(std::make_pair(key, value)); | ||||
} | } | ||||
bool CWallet::GetDestData(const CTxDestination &dest, const std::string &key, | bool CWallet::GetDestData(const CTxDestination &dest, const std::string &key, | ||||
std::string *value) const { | std::string *value) const { | ||||
std::map<CTxDestination, CAddressBookData>::const_iterator i = | std::map<CTxDestination, CAddressBookData>::const_iterator i = | ||||
mapAddressBook.find(dest); | m_address_book.find(dest); | ||||
if (i != mapAddressBook.end()) { | if (i != m_address_book.end()) { | ||||
CAddressBookData::StringMap::const_iterator j = | CAddressBookData::StringMap::const_iterator j = | ||||
i->second.destdata.find(key); | i->second.destdata.find(key); | ||||
if (j != i->second.destdata.end()) { | if (j != i->second.destdata.end()) { | ||||
if (value) { | if (value) { | ||||
*value = j->second; | *value = j->second; | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
} | } | ||||
return false; | return false; | ||||
} | } | ||||
std::vector<std::string> | std::vector<std::string> | ||||
CWallet::GetDestValues(const std::string &prefix) const { | CWallet::GetDestValues(const std::string &prefix) const { | ||||
std::vector<std::string> values; | std::vector<std::string> values; | ||||
for (const auto &address : mapAddressBook) { | for (const auto &address : m_address_book) { | ||||
for (const auto &data : address.second.destdata) { | for (const auto &data : address.second.destdata) { | ||||
if (!data.first.compare(0, prefix.size(), prefix)) { | if (!data.first.compare(0, prefix.size(), prefix)) { | ||||
values.emplace_back(data.second); | values.emplace_back(data.second); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
return values; | return values; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 456 Lines • ▼ Show 20 Lines | std::shared_ptr<CWallet> CWallet::CreateWalletFromFile( | ||||
walletInstance->SetBroadcastTransactions( | walletInstance->SetBroadcastTransactions( | ||||
gArgs.GetBoolArg("-walletbroadcast", DEFAULT_WALLETBROADCAST)); | gArgs.GetBoolArg("-walletbroadcast", DEFAULT_WALLETBROADCAST)); | ||||
walletInstance->WalletLogPrintf("setKeyPool.size() = %u\n", | walletInstance->WalletLogPrintf("setKeyPool.size() = %u\n", | ||||
walletInstance->GetKeyPoolSize()); | walletInstance->GetKeyPoolSize()); | ||||
walletInstance->WalletLogPrintf("mapWallet.size() = %u\n", | walletInstance->WalletLogPrintf("mapWallet.size() = %u\n", | ||||
walletInstance->mapWallet.size()); | walletInstance->mapWallet.size()); | ||||
walletInstance->WalletLogPrintf("mapAddressBook.size() = %u\n", | walletInstance->WalletLogPrintf("m_address_book.size() = %u\n", | ||||
walletInstance->mapAddressBook.size()); | walletInstance->m_address_book.size()); | ||||
return walletInstance; | return walletInstance; | ||||
} | } | ||||
const CAddressBookData * | |||||
CWallet::FindAddressBookEntry(const CTxDestination &dest, | |||||
bool allow_change) const { | |||||
const auto &address_book_it = m_address_book.find(dest); | |||||
if (address_book_it == m_address_book.end()) { | |||||
return nullptr; | |||||
} | |||||
if ((!allow_change) && address_book_it->second.IsChange()) { | |||||
return nullptr; | |||||
} | |||||
return &address_book_it->second; | |||||
} | |||||
void CWallet::postInitProcess() { | void CWallet::postInitProcess() { | ||||
auto locked_chain = chain().lock(); | auto locked_chain = chain().lock(); | ||||
LOCK(cs_wallet); | LOCK(cs_wallet); | ||||
// Add wallet transactions that aren't already in a block to mempool. | // Add wallet transactions that aren't already in a block to mempool. | ||||
// Do this here as mempool requires genesis block to be loaded. | // Do this here as mempool requires genesis block to be loaded. | ||||
ReacceptWalletTransactions(); | ReacceptWalletTransactions(); | ||||
▲ Show 20 Lines • Show All 155 Lines • Show Last 20 Lines |