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.
This commit is contained in:
Gavin Andresen 2013-07-15 15:20:50 +10:00
parent b94595bb7f
commit 618855133d
9 changed files with 54 additions and 38 deletions

View File

@ -59,10 +59,10 @@ public:
cachedAddressTable.clear(); cachedAddressTable.clear();
{ {
LOCK(wallet->cs_wallet); 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 CBitcoinAddress& address = item.first;
const std::string& strName = item.second; const std::string& strName = item.second.name;
bool fMine = IsMine(*wallet, address.Get()); bool fMine = IsMine(*wallet, address.Get());
cachedAddressTable.append(AddressTableEntry(fMine ? AddressTableEntry::Receiving : AddressTableEntry::Sending, cachedAddressTable.append(AddressTableEntry(fMine ? AddressTableEntry::Receiving : AddressTableEntry::Sending,
QString::fromStdString(strName), QString::fromStdString(strName),
@ -397,10 +397,10 @@ QString AddressTableModel::labelForAddress(const QString &address) const
{ {
LOCK(wallet->cs_wallet); LOCK(wallet->cs_wallet);
CBitcoinAddress address_parsed(address.toStdString()); CBitcoinAddress address_parsed(address.toStdString());
std::map<CTxDestination, std::string>::iterator mi = wallet->mapAddressBook.find(address_parsed.Get()); std::map<CTxDestination, CAddressBookData>::iterator mi = wallet->mapAddressBook.find(address_parsed.Get());
if (mi != wallet->mapAddressBook.end()) if (mi != wallet->mapAddressBook.end())
{ {
return QString::fromStdString(mi->second); return QString::fromStdString(mi->second.name);
} }
} }
return QString(); return QString();

View File

@ -88,8 +88,8 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx)
strHTML += "<b>" + tr("From") + ":</b> " + tr("unknown") + "<br>"; strHTML += "<b>" + tr("From") + ":</b> " + tr("unknown") + "<br>";
strHTML += "<b>" + tr("To") + ":</b> "; strHTML += "<b>" + tr("To") + ":</b> ";
strHTML += GUIUtil::HtmlEscape(CBitcoinAddress(address).ToString()); strHTML += GUIUtil::HtmlEscape(CBitcoinAddress(address).ToString());
if (!wallet->mapAddressBook[address].empty()) if (!wallet->mapAddressBook[address].name.empty())
strHTML += " (" + tr("own address") + ", " + tr("label") + ": " + GUIUtil::HtmlEscape(wallet->mapAddressBook[address]) + ")"; strHTML += " (" + tr("own address") + ", " + tr("label") + ": " + GUIUtil::HtmlEscape(wallet->mapAddressBook[address].name) + ")";
else else
strHTML += " (" + tr("own address") + ")"; strHTML += " (" + tr("own address") + ")";
strHTML += "<br>"; strHTML += "<br>";
@ -110,8 +110,8 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx)
std::string strAddress = wtx.mapValue["to"]; std::string strAddress = wtx.mapValue["to"];
strHTML += "<b>" + tr("To") + ":</b> "; strHTML += "<b>" + tr("To") + ":</b> ";
CTxDestination dest = CBitcoinAddress(strAddress).Get(); CTxDestination dest = CBitcoinAddress(strAddress).Get();
if (wallet->mapAddressBook.count(dest) && !wallet->mapAddressBook[dest].empty()) if (wallet->mapAddressBook.count(dest) && !wallet->mapAddressBook[dest].name.empty())
strHTML += GUIUtil::HtmlEscape(wallet->mapAddressBook[dest]) + " "; strHTML += GUIUtil::HtmlEscape(wallet->mapAddressBook[dest].name) + " ";
strHTML += GUIUtil::HtmlEscape(strAddress) + "<br>"; strHTML += GUIUtil::HtmlEscape(strAddress) + "<br>";
} }
@ -167,8 +167,8 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx)
if (ExtractDestination(txout.scriptPubKey, address)) if (ExtractDestination(txout.scriptPubKey, address))
{ {
strHTML += "<b>" + tr("To") + ":</b> "; strHTML += "<b>" + tr("To") + ":</b> ";
if (wallet->mapAddressBook.count(address) && !wallet->mapAddressBook[address].empty()) if (wallet->mapAddressBook.count(address) && !wallet->mapAddressBook[address].name.empty())
strHTML += GUIUtil::HtmlEscape(wallet->mapAddressBook[address]) + " "; strHTML += GUIUtil::HtmlEscape(wallet->mapAddressBook[address].name) + " ";
strHTML += GUIUtil::HtmlEscape(CBitcoinAddress(address).ToString()); strHTML += GUIUtil::HtmlEscape(CBitcoinAddress(address).ToString());
strHTML += "<br>"; strHTML += "<br>";
} }
@ -254,8 +254,8 @@ QString TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx)
CTxDestination address; CTxDestination address;
if (ExtractDestination(vout.scriptPubKey, address)) if (ExtractDestination(vout.scriptPubKey, address))
{ {
if (wallet->mapAddressBook.count(address) && !wallet->mapAddressBook[address].empty()) if (wallet->mapAddressBook.count(address) && !wallet->mapAddressBook[address].name.empty())
strHTML += GUIUtil::HtmlEscape(wallet->mapAddressBook[address]) + " "; strHTML += GUIUtil::HtmlEscape(wallet->mapAddressBook[address].name) + " ";
strHTML += QString::fromStdString(CBitcoinAddress(address).ToString()); strHTML += QString::fromStdString(CBitcoinAddress(address).ToString());
} }
strHTML = strHTML + " " + tr("Amount") + "=" + BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, vout.nValue); strHTML = strHTML + " " + tr("Amount") + "=" + BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, vout.nValue);

View File

@ -214,10 +214,10 @@ WalletModel::SendCoinsReturn WalletModel::sendCoins(const QList<SendCoinsRecipie
{ {
LOCK(wallet->cs_wallet); LOCK(wallet->cs_wallet);
std::map<CTxDestination, std::string>::iterator mi = wallet->mapAddressBook.find(dest); std::map<CTxDestination, CAddressBookData>::iterator mi = wallet->mapAddressBook.find(dest);
// Check if we have a new address or an updated label // 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); wallet->SetAddressBookName(dest, strLabel);
} }

View File

@ -255,7 +255,7 @@ Value dumpwallet(const Array& params, bool fHelp)
CKey key; CKey key;
if (pwalletMain->GetKey(keyid, key)) { if (pwalletMain->GetKey(keyid, key)) {
if (pwalletMain->mapAddressBook.count(keyid)) { 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)) { } 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()); file << strprintf("%s %s reserve=1 # addr=%s\n", CBitcoinSecret(key).ToString().c_str(), strTime.c_str(), strAddr.c_str());
} else { } else {

View File

@ -229,7 +229,7 @@ Value listunspent(const Array& params, bool fHelp)
{ {
entry.push_back(Pair("address", CBitcoinAddress(address).ToString())); entry.push_back(Pair("address", CBitcoinAddress(address).ToString()));
if (pwalletMain->mapAddressBook.count(address)) 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()))); entry.push_back(Pair("scriptPubKey", HexStr(pk.begin(), pk.end())));
if (pk.IsPayToScriptHash()) if (pk.IsPayToScriptHash())

View File

@ -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: // Detect when changing the account of an address that is the 'unused current key' of another account:
if (pwalletMain->mapAddressBook.count(address.Get())) if (pwalletMain->mapAddressBook.count(address.Get()))
{ {
string strOldAccount = pwalletMain->mapAddressBook[address.Get()]; string strOldAccount = pwalletMain->mapAddressBook[address.Get()].name;
if (address == GetAccountAddress(strOldAccount)) if (address == GetAccountAddress(strOldAccount))
GetAccountAddress(strOldAccount, true); GetAccountAddress(strOldAccount, true);
} }
@ -219,9 +219,9 @@ Value getaccount(const Array& params, bool fHelp)
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address"); throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
string strAccount; string strAccount;
map<CTxDestination, string>::iterator mi = pwalletMain->mapAddressBook.find(address.Get()); map<CTxDestination, CAddressBookData>::iterator mi = pwalletMain->mapAddressBook.find(address.Get());
if (mi != pwalletMain->mapAddressBook.end() && !(*mi).second.empty()) if (mi != pwalletMain->mapAddressBook.end() && !(*mi).second.name.empty())
strAccount = (*mi).second; strAccount = (*mi).second.name;
return strAccount; return strAccount;
} }
@ -237,10 +237,10 @@ Value getaddressesbyaccount(const Array& params, bool fHelp)
// Find all addresses that have the given account // Find all addresses that have the given account
Array ret; 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 CBitcoinAddress& address = item.first;
const string& strName = item.second; const string& strName = item.second.name;
if (strName == strAccount) if (strName == strAccount)
ret.push_back(address.ToString()); ret.push_back(address.ToString());
} }
@ -301,7 +301,7 @@ Value listaddressgroupings(const Array& params, bool fHelp)
{ {
LOCK(pwalletMain->cs_wallet); LOCK(pwalletMain->cs_wallet);
if (pwalletMain->mapAddressBook.find(CBitcoinAddress(address).Get()) != pwalletMain->mapAddressBook.end()) 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); jsonGrouping.push_back(addressInfo);
} }
@ -423,10 +423,10 @@ Value getreceivedbyaddress(const Array& params, bool fHelp)
void GetAccountAddresses(string strAccount, set<CTxDestination>& setAddress) void GetAccountAddresses(string strAccount, set<CTxDestination>& 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 CTxDestination& address = item.first;
const string& strName = item.second; const string& strName = item.second.name;
if (strName == strAccount) if (strName == strAccount)
setAddress.insert(address); setAddress.insert(address);
} }
@ -862,10 +862,10 @@ Value ListReceived(const Array& params, bool fByAccounts)
// Reply // Reply
Array ret; Array ret;
map<string, tallyitem> mapAccountTally; map<string, tallyitem> 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 CBitcoinAddress& address = item.first;
const string& strAccount = item.second; const string& strAccount = item.second.name;
map<CBitcoinAddress, tallyitem>::iterator it = mapTally.find(address); map<CBitcoinAddress, tallyitem>::iterator it = mapTally.find(address);
if (it == mapTally.end() && !fIncludeEmpty) if (it == mapTally.end() && !fIncludeEmpty)
continue; continue;
@ -988,7 +988,7 @@ void ListTransactions(const CWalletTx& wtx, const string& strAccount, int nMinDe
{ {
string account; string account;
if (pwalletMain->mapAddressBook.count(r.first)) if (pwalletMain->mapAddressBook.count(r.first))
account = pwalletMain->mapAddressBook[r.first]; account = pwalletMain->mapAddressBook[r.first].name;
if (fAllAccounts || (account == strAccount)) if (fAllAccounts || (account == strAccount))
{ {
Object entry; Object entry;
@ -1101,9 +1101,9 @@ Value listaccounts(const Array& params, bool fHelp)
nMinDepth = params[0].get_int(); nMinDepth = params[0].get_int();
map<string, int64> mapAccountBalances; map<string, int64> 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 if (IsMine(*pwalletMain, entry.first)) // This address belongs to me
mapAccountBalances[entry.second] = 0; mapAccountBalances[entry.second.name] = 0;
} }
for (map<uint256, CWalletTx>::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it) for (map<uint256, CWalletTx>::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) BOOST_FOREACH(const PAIRTYPE(CTxDestination, int64)& r, listReceived)
if (pwalletMain->mapAddressBook.count(r.first)) if (pwalletMain->mapAddressBook.count(r.first))
mapAccountBalances[pwalletMain->mapAddressBook[r.first]] += r.second; mapAccountBalances[pwalletMain->mapAddressBook[r.first].name] += r.second;
else else
mapAccountBalances[""] += r.second; mapAccountBalances[""] += r.second;
} }
@ -1470,7 +1470,7 @@ Value validateaddress(const Array& params, bool fHelp)
ret.insert(ret.end(), detail.begin(), detail.end()); ret.insert(ret.end(), detail.begin(), detail.end());
} }
if (pwalletMain->mapAddressBook.count(dest)) if (pwalletMain->mapAddressBook.count(dest))
ret.push_back(Pair("account", pwalletMain->mapAddressBook[dest])); ret.push_back(Pair("account", pwalletMain->mapAddressBook[dest].name));
} }
return ret; return ret;
} }

View File

@ -717,8 +717,8 @@ void CWalletTx::GetAccountAmounts(const string& strAccount, int64& nReceived,
{ {
if (pwallet->mapAddressBook.count(r.first)) if (pwallet->mapAddressBook.count(r.first))
{ {
map<CTxDestination, string>::const_iterator mi = pwallet->mapAddressBook.find(r.first); map<CTxDestination, CAddressBookData>::const_iterator mi = pwallet->mapAddressBook.find(r.first);
if (mi != pwallet->mapAddressBook.end() && (*mi).second == strAccount) if (mi != pwallet->mapAddressBook.end() && (*mi).second.name == strAccount)
nReceived += r.second; nReceived += r.second;
} }
else if (strAccount.empty()) else if (strAccount.empty())
@ -1459,8 +1459,8 @@ DBErrors CWallet::LoadWallet(bool& fFirstRunRet)
bool CWallet::SetAddressBookName(const CTxDestination& address, const string& strName) bool CWallet::SetAddressBookName(const CTxDestination& address, const string& strName)
{ {
std::map<CTxDestination, std::string>::iterator mi = mapAddressBook.find(address); std::map<CTxDestination, CAddressBookData>::iterator mi = mapAddressBook.find(address);
mapAddressBook[address] = strName; mapAddressBook[address].name = strName;
NotifyAddressBookChanged(this, address, strName, ::IsMine(*this, address), (mi == mapAddressBook.end()) ? CT_NEW : CT_UPDATED); NotifyAddressBookChanged(this, address, strName, ::IsMine(*this, address), (mi == mapAddressBook.end()) ? CT_NEW : CT_UPDATED);
if (!fFileBacked) if (!fFileBacked)
return false; return false;

View File

@ -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, /** 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. * and provides the ability to create new transactions.
*/ */
@ -124,7 +140,7 @@ public:
int64 nOrderPosNext; int64 nOrderPosNext;
std::map<uint256, int> mapRequestCount; std::map<uint256, int> mapRequestCount;
std::map<CTxDestination, std::string> mapAddressBook; std::map<CTxDestination, CAddressBookData> mapAddressBook;
CPubKey vchDefaultKey; CPubKey vchDefaultKey;

View File

@ -212,7 +212,7 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
{ {
string strAddress; string strAddress;
ssKey >> strAddress; ssKey >> strAddress;
ssValue >> pwallet->mapAddressBook[CBitcoinAddress(strAddress).Get()]; ssValue >> pwallet->mapAddressBook[CBitcoinAddress(strAddress).Get()].name;
} }
else if (strType == "tx") else if (strType == "tx")
{ {