From 618855133d4ae4ece130aec3b35c5a65bea95b8f Mon Sep 17 00:00:00 2001 From: Gavin Andresen Date: Mon, 15 Jul 2013 15:20:50 +1000 Subject: [PATCH] Refactor: CAddressBookData for mapAddressBook Straight refactor, so mapAddressBook stores a CAddressBookData (which just contains a std::string) instead of a std::string. Preparation for payment protocol work, which will add the notion of refund addresses to the address book. --- src/qt/addresstablemodel.cpp | 8 ++++---- src/qt/transactiondesc.cpp | 16 ++++++++-------- src/qt/walletmodel.cpp | 4 ++-- src/rpcdump.cpp | 2 +- src/rpcrawtransaction.cpp | 2 +- src/rpcwallet.cpp | 32 ++++++++++++++++---------------- src/wallet.cpp | 8 ++++---- src/wallet.h | 18 +++++++++++++++++- src/walletdb.cpp | 2 +- 9 files changed, 54 insertions(+), 38 deletions(-) diff --git a/src/qt/addresstablemodel.cpp b/src/qt/addresstablemodel.cpp index 180144452..823730c4c 100644 --- a/src/qt/addresstablemodel.cpp +++ b/src/qt/addresstablemodel.cpp @@ -59,10 +59,10 @@ public: cachedAddressTable.clear(); { LOCK(wallet->cs_wallet); - BOOST_FOREACH(const PAIRTYPE(CTxDestination, std::string)& item, wallet->mapAddressBook) + BOOST_FOREACH(const PAIRTYPE(CTxDestination, CAddressBookData)& item, wallet->mapAddressBook) { const CBitcoinAddress& address = item.first; - const std::string& strName = item.second; + const std::string& strName = item.second.name; bool fMine = IsMine(*wallet, address.Get()); cachedAddressTable.append(AddressTableEntry(fMine ? AddressTableEntry::Receiving : AddressTableEntry::Sending, QString::fromStdString(strName), @@ -397,10 +397,10 @@ QString AddressTableModel::labelForAddress(const QString &address) const { LOCK(wallet->cs_wallet); CBitcoinAddress address_parsed(address.toStdString()); - std::map::iterator mi = wallet->mapAddressBook.find(address_parsed.Get()); + std::map::iterator mi = wallet->mapAddressBook.find(address_parsed.Get()); if (mi != wallet->mapAddressBook.end()) { - return QString::fromStdString(mi->second); + return QString::fromStdString(mi->second.name); } } return QString(); diff --git a/src/qt/transactiondesc.cpp b/src/qt/transactiondesc.cpp index aeef721ce..e9f3eb478 100644 --- a/src/qt/transactiondesc.cpp +++ b/src/qt/transactiondesc.cpp @@ -88,8 +88,8 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx) strHTML += "" + tr("From") + ": " + tr("unknown") + "
"; strHTML += "" + tr("To") + ": "; strHTML += GUIUtil::HtmlEscape(CBitcoinAddress(address).ToString()); - if (!wallet->mapAddressBook[address].empty()) - strHTML += " (" + tr("own address") + ", " + tr("label") + ": " + GUIUtil::HtmlEscape(wallet->mapAddressBook[address]) + ")"; + if (!wallet->mapAddressBook[address].name.empty()) + strHTML += " (" + tr("own address") + ", " + tr("label") + ": " + GUIUtil::HtmlEscape(wallet->mapAddressBook[address].name) + ")"; else strHTML += " (" + tr("own address") + ")"; strHTML += "
"; @@ -110,8 +110,8 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx) std::string strAddress = wtx.mapValue["to"]; strHTML += "" + tr("To") + ": "; CTxDestination dest = CBitcoinAddress(strAddress).Get(); - if (wallet->mapAddressBook.count(dest) && !wallet->mapAddressBook[dest].empty()) - strHTML += GUIUtil::HtmlEscape(wallet->mapAddressBook[dest]) + " "; + if (wallet->mapAddressBook.count(dest) && !wallet->mapAddressBook[dest].name.empty()) + strHTML += GUIUtil::HtmlEscape(wallet->mapAddressBook[dest].name) + " "; strHTML += GUIUtil::HtmlEscape(strAddress) + "
"; } @@ -167,8 +167,8 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx) if (ExtractDestination(txout.scriptPubKey, address)) { strHTML += "" + tr("To") + ": "; - if (wallet->mapAddressBook.count(address) && !wallet->mapAddressBook[address].empty()) - strHTML += GUIUtil::HtmlEscape(wallet->mapAddressBook[address]) + " "; + if (wallet->mapAddressBook.count(address) && !wallet->mapAddressBook[address].name.empty()) + strHTML += GUIUtil::HtmlEscape(wallet->mapAddressBook[address].name) + " "; strHTML += GUIUtil::HtmlEscape(CBitcoinAddress(address).ToString()); strHTML += "
"; } @@ -254,8 +254,8 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx) CTxDestination address; if (ExtractDestination(vout.scriptPubKey, address)) { - if (wallet->mapAddressBook.count(address) && !wallet->mapAddressBook[address].empty()) - strHTML += GUIUtil::HtmlEscape(wallet->mapAddressBook[address]) + " "; + if (wallet->mapAddressBook.count(address) && !wallet->mapAddressBook[address].name.empty()) + strHTML += GUIUtil::HtmlEscape(wallet->mapAddressBook[address].name) + " "; strHTML += QString::fromStdString(CBitcoinAddress(address).ToString()); } strHTML = strHTML + " " + tr("Amount") + "=" + BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, vout.nValue); diff --git a/src/qt/walletmodel.cpp b/src/qt/walletmodel.cpp index fb3ffc5c9..d3c3ddcab 100644 --- a/src/qt/walletmodel.cpp +++ b/src/qt/walletmodel.cpp @@ -214,10 +214,10 @@ WalletModel::SendCoinsReturn WalletModel::sendCoins(const QListcs_wallet); - std::map::iterator mi = wallet->mapAddressBook.find(dest); + std::map::iterator mi = wallet->mapAddressBook.find(dest); // Check if we have a new address or an updated label - if (mi == wallet->mapAddressBook.end() || mi->second != strLabel) + if (mi == wallet->mapAddressBook.end() || mi->second.name != strLabel) { wallet->SetAddressBookName(dest, strLabel); } diff --git a/src/rpcdump.cpp b/src/rpcdump.cpp index dcfb023f3..fde9984b0 100644 --- a/src/rpcdump.cpp +++ b/src/rpcdump.cpp @@ -255,7 +255,7 @@ Value dumpwallet(const Array& params, bool fHelp) CKey key; if (pwalletMain->GetKey(keyid, key)) { if (pwalletMain->mapAddressBook.count(keyid)) { - file << strprintf("%s %s label=%s # addr=%s\n", CBitcoinSecret(key).ToString().c_str(), strTime.c_str(), EncodeDumpString(pwalletMain->mapAddressBook[keyid]).c_str(), strAddr.c_str()); + file << strprintf("%s %s label=%s # addr=%s\n", CBitcoinSecret(key).ToString().c_str(), strTime.c_str(), EncodeDumpString(pwalletMain->mapAddressBook[keyid].name).c_str(), strAddr.c_str()); } else if (setKeyPool.count(keyid)) { file << strprintf("%s %s reserve=1 # addr=%s\n", CBitcoinSecret(key).ToString().c_str(), strTime.c_str(), strAddr.c_str()); } else { diff --git a/src/rpcrawtransaction.cpp b/src/rpcrawtransaction.cpp index f08688d38..580120f2a 100644 --- a/src/rpcrawtransaction.cpp +++ b/src/rpcrawtransaction.cpp @@ -229,7 +229,7 @@ Value listunspent(const Array& params, bool fHelp) { entry.push_back(Pair("address", CBitcoinAddress(address).ToString())); if (pwalletMain->mapAddressBook.count(address)) - entry.push_back(Pair("account", pwalletMain->mapAddressBook[address])); + entry.push_back(Pair("account", pwalletMain->mapAddressBook[address].name)); } entry.push_back(Pair("scriptPubKey", HexStr(pk.begin(), pk.end()))); if (pk.IsPayToScriptHash()) diff --git a/src/rpcwallet.cpp b/src/rpcwallet.cpp index 83aa9471f..b233b654b 100644 --- a/src/rpcwallet.cpp +++ b/src/rpcwallet.cpp @@ -196,7 +196,7 @@ Value setaccount(const Array& params, bool fHelp) // Detect when changing the account of an address that is the 'unused current key' of another account: if (pwalletMain->mapAddressBook.count(address.Get())) { - string strOldAccount = pwalletMain->mapAddressBook[address.Get()]; + string strOldAccount = pwalletMain->mapAddressBook[address.Get()].name; if (address == GetAccountAddress(strOldAccount)) GetAccountAddress(strOldAccount, true); } @@ -219,9 +219,9 @@ Value getaccount(const Array& params, bool fHelp) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address"); string strAccount; - map::iterator mi = pwalletMain->mapAddressBook.find(address.Get()); - if (mi != pwalletMain->mapAddressBook.end() && !(*mi).second.empty()) - strAccount = (*mi).second; + map::iterator mi = pwalletMain->mapAddressBook.find(address.Get()); + if (mi != pwalletMain->mapAddressBook.end() && !(*mi).second.name.empty()) + strAccount = (*mi).second.name; return strAccount; } @@ -237,10 +237,10 @@ Value getaddressesbyaccount(const Array& params, bool fHelp) // Find all addresses that have the given account Array ret; - BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, string)& item, pwalletMain->mapAddressBook) + BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, CAddressBookData)& item, pwalletMain->mapAddressBook) { const CBitcoinAddress& address = item.first; - const string& strName = item.second; + const string& strName = item.second.name; if (strName == strAccount) ret.push_back(address.ToString()); } @@ -301,7 +301,7 @@ Value listaddressgroupings(const Array& params, bool fHelp) { LOCK(pwalletMain->cs_wallet); if (pwalletMain->mapAddressBook.find(CBitcoinAddress(address).Get()) != pwalletMain->mapAddressBook.end()) - addressInfo.push_back(pwalletMain->mapAddressBook.find(CBitcoinAddress(address).Get())->second); + addressInfo.push_back(pwalletMain->mapAddressBook.find(CBitcoinAddress(address).Get())->second.name); } jsonGrouping.push_back(addressInfo); } @@ -423,10 +423,10 @@ Value getreceivedbyaddress(const Array& params, bool fHelp) void GetAccountAddresses(string strAccount, set& setAddress) { - BOOST_FOREACH(const PAIRTYPE(CTxDestination, string)& item, pwalletMain->mapAddressBook) + BOOST_FOREACH(const PAIRTYPE(CTxDestination, CAddressBookData)& item, pwalletMain->mapAddressBook) { const CTxDestination& address = item.first; - const string& strName = item.second; + const string& strName = item.second.name; if (strName == strAccount) setAddress.insert(address); } @@ -862,10 +862,10 @@ Value ListReceived(const Array& params, bool fByAccounts) // Reply Array ret; map mapAccountTally; - BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, string)& item, pwalletMain->mapAddressBook) + BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, CAddressBookData)& item, pwalletMain->mapAddressBook) { const CBitcoinAddress& address = item.first; - const string& strAccount = item.second; + const string& strAccount = item.second.name; map::iterator it = mapTally.find(address); if (it == mapTally.end() && !fIncludeEmpty) continue; @@ -988,7 +988,7 @@ void ListTransactions(const CWalletTx& wtx, const string& strAccount, int nMinDe { string account; if (pwalletMain->mapAddressBook.count(r.first)) - account = pwalletMain->mapAddressBook[r.first]; + account = pwalletMain->mapAddressBook[r.first].name; if (fAllAccounts || (account == strAccount)) { Object entry; @@ -1101,9 +1101,9 @@ Value listaccounts(const Array& params, bool fHelp) nMinDepth = params[0].get_int(); map mapAccountBalances; - BOOST_FOREACH(const PAIRTYPE(CTxDestination, string)& entry, pwalletMain->mapAddressBook) { + BOOST_FOREACH(const PAIRTYPE(CTxDestination, CAddressBookData)& entry, pwalletMain->mapAddressBook) { if (IsMine(*pwalletMain, entry.first)) // This address belongs to me - mapAccountBalances[entry.second] = 0; + mapAccountBalances[entry.second.name] = 0; } for (map::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it) @@ -1121,7 +1121,7 @@ Value listaccounts(const Array& params, bool fHelp) { BOOST_FOREACH(const PAIRTYPE(CTxDestination, int64)& r, listReceived) if (pwalletMain->mapAddressBook.count(r.first)) - mapAccountBalances[pwalletMain->mapAddressBook[r.first]] += r.second; + mapAccountBalances[pwalletMain->mapAddressBook[r.first].name] += r.second; else mapAccountBalances[""] += r.second; } @@ -1470,7 +1470,7 @@ Value validateaddress(const Array& params, bool fHelp) ret.insert(ret.end(), detail.begin(), detail.end()); } if (pwalletMain->mapAddressBook.count(dest)) - ret.push_back(Pair("account", pwalletMain->mapAddressBook[dest])); + ret.push_back(Pair("account", pwalletMain->mapAddressBook[dest].name)); } return ret; } diff --git a/src/wallet.cpp b/src/wallet.cpp index 8b9f3d34e..4f3516953 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -717,8 +717,8 @@ void CWalletTx::GetAccountAmounts(const string& strAccount, int64& nReceived, { if (pwallet->mapAddressBook.count(r.first)) { - map::const_iterator mi = pwallet->mapAddressBook.find(r.first); - if (mi != pwallet->mapAddressBook.end() && (*mi).second == strAccount) + map::const_iterator mi = pwallet->mapAddressBook.find(r.first); + if (mi != pwallet->mapAddressBook.end() && (*mi).second.name == strAccount) nReceived += r.second; } else if (strAccount.empty()) @@ -1459,8 +1459,8 @@ DBErrors CWallet::LoadWallet(bool& fFirstRunRet) bool CWallet::SetAddressBookName(const CTxDestination& address, const string& strName) { - std::map::iterator mi = mapAddressBook.find(address); - mapAddressBook[address] = strName; + std::map::iterator mi = mapAddressBook.find(address); + mapAddressBook[address].name = strName; NotifyAddressBookChanged(this, address, strName, ::IsMine(*this, address), (mi == mapAddressBook.end()) ? CT_NEW : CT_UPDATED); if (!fFileBacked) return false; diff --git a/src/wallet.h b/src/wallet.h index 664a03291..a3bcc26c5 100644 --- a/src/wallet.h +++ b/src/wallet.h @@ -64,6 +64,22 @@ public: ) }; +/** Address book data */ +class CAddressBookData +{ +public: + std::string name; + + CAddressBookData() + { + } + + IMPLEMENT_SERIALIZE + ( + READWRITE(name); + ) +}; + /** A CWallet is an extension of a keystore, which also maintains a set of transactions and balances, * and provides the ability to create new transactions. */ @@ -124,7 +140,7 @@ public: int64 nOrderPosNext; std::map mapRequestCount; - std::map mapAddressBook; + std::map mapAddressBook; CPubKey vchDefaultKey; diff --git a/src/walletdb.cpp b/src/walletdb.cpp index 014d8cbe2..6cfd394bd 100644 --- a/src/walletdb.cpp +++ b/src/walletdb.cpp @@ -212,7 +212,7 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, { string strAddress; ssKey >> strAddress; - ssValue >> pwallet->mapAddressBook[CBitcoinAddress(strAddress).Get()]; + ssValue >> pwallet->mapAddressBook[CBitcoinAddress(strAddress).Get()].name; } else if (strType == "tx") {