Fix AddressBook syncrhonization between a CWallet and CWalletDB

This problem was reported independently by laanwj in Issue #350.
This commit is contained in:
Stéphane Gimenez 2011-06-27 23:22:30 +02:00
parent d0d80170a2
commit 4d410cfce9
4 changed files with 45 additions and 16 deletions

View file

@ -332,12 +332,15 @@ Value getnewaddress(const Array& params, bool fHelp)
// Generate a new key that is added to wallet // Generate a new key that is added to wallet
string strAddress = PubKeyToAddress(pwalletMain->GetKeyFromKeyPool()); string strAddress = PubKeyToAddress(pwalletMain->GetKeyFromKeyPool());
// This could be done in the same main CS as GetKeyFromKeyPool.
CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
pwalletMain->SetAddressBookName(strAddress, strAccount); pwalletMain->SetAddressBookName(strAddress, strAccount);
return strAddress; return strAddress;
} }
// requires cs_main, cs_mapWallet locks // requires cs_main, cs_mapWallet, cs_mapAddressBook locks
string GetAccountAddress(string strAccount, bool bForceNew=false) string GetAccountAddress(string strAccount, bool bForceNew=false)
{ {
string strAddress; string strAddress;
@ -393,6 +396,7 @@ Value getaccountaddress(const Array& params, bool fHelp)
CRITICAL_BLOCK(cs_main) CRITICAL_BLOCK(cs_main)
CRITICAL_BLOCK(pwalletMain->cs_mapWallet) CRITICAL_BLOCK(pwalletMain->cs_mapWallet)
CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
{ {
ret = GetAccountAddress(strAccount); ret = GetAccountAddress(strAccount);
} }
@ -431,9 +435,10 @@ Value setaccount(const Array& params, bool fHelp)
if (strAddress == GetAccountAddress(strOldAccount)) if (strAddress == GetAccountAddress(strOldAccount))
GetAccountAddress(strOldAccount, true); GetAccountAddress(strOldAccount, true);
} }
}
pwalletMain->SetAddressBookName(strAddress, strAccount); pwalletMain->SetAddressBookName(strAddress, strAccount);
}
return Value::null; return Value::null;
} }

View file

@ -1186,6 +1186,7 @@ void CMainFrame::OnButtonNew(wxCommandEvent& event)
string strAddress = PubKeyToAddress(pwalletMain->GetKeyFromKeyPool()); string strAddress = PubKeyToAddress(pwalletMain->GetKeyFromKeyPool());
// Save // Save
CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
pwalletMain->SetAddressBookName(strAddress, strName); pwalletMain->SetAddressBookName(strAddress, strName);
SetDefaultReceivingAddress(strAddress); SetDefaultReceivingAddress(strAddress);
} }
@ -2444,6 +2445,7 @@ void CAddressBookDialog::OnListEndLabelEdit(wxListEvent& event)
if (event.IsEditCancelled()) if (event.IsEditCancelled())
return; return;
string strAddress = (string)GetItemText(m_listCtrl, event.GetIndex(), 1); string strAddress = (string)GetItemText(m_listCtrl, event.GetIndex(), 1);
CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
pwalletMain->SetAddressBookName(strAddress, string(event.GetText())); pwalletMain->SetAddressBookName(strAddress, string(event.GetText()));
pframeMain->RefreshListCtrl(); pframeMain->RefreshListCtrl();
} }
@ -2479,7 +2481,8 @@ void CAddressBookDialog::OnButtonDelete(wxCommandEvent& event)
if (m_listCtrl->GetItemState(nIndex, wxLIST_STATE_SELECTED)) if (m_listCtrl->GetItemState(nIndex, wxLIST_STATE_SELECTED))
{ {
string strAddress = (string)GetItemText(m_listCtrl, nIndex, 1); string strAddress = (string)GetItemText(m_listCtrl, nIndex, 1);
CWalletDB(pwalletMain->strWalletFile).EraseName(strAddress); CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
pwalletMain->DelAddressBookName(strAddress);
m_listCtrl->DeleteItem(nIndex); m_listCtrl->DeleteItem(nIndex);
} }
} }
@ -2538,9 +2541,12 @@ void CAddressBookDialog::OnButtonEdit(wxCommandEvent& event)
} }
// Write back // Write back
CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
{
if (strAddress != strAddressOrg) if (strAddress != strAddressOrg)
CWalletDB(pwalletMain->strWalletFile).EraseName(strAddressOrg); pwalletMain->DelAddressBookName(strAddressOrg);
pwalletMain->SetAddressBookName(strAddress, strName); pwalletMain->SetAddressBookName(strAddress, strName);
}
m_listCtrl->SetItem(nIndex, 1, strAddress); m_listCtrl->SetItem(nIndex, 1, strAddress);
m_listCtrl->SetItemText(nIndex, strName); m_listCtrl->SetItemText(nIndex, strName);
pframeMain->RefreshListCtrl(); pframeMain->RefreshListCtrl();
@ -2580,6 +2586,7 @@ void CAddressBookDialog::OnButtonNew(wxCommandEvent& event)
} }
// Add to list and select it // Add to list and select it
CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
pwalletMain->SetAddressBookName(strAddress, strName); pwalletMain->SetAddressBookName(strAddress, strName);
int nIndex = InsertLine(m_listCtrl, strName, strAddress); int nIndex = InsertLine(m_listCtrl, strName, strAddress);
SetSelection(m_listCtrl, nIndex); SetSelection(m_listCtrl, nIndex);

View file

@ -977,6 +977,24 @@ bool CWallet::LoadWallet(bool& fFirstRunRet)
return true; return true;
} }
bool CWallet::SetAddressBookName(const string& strAddress, const string& strName)
{
mapAddressBook[strAddress] = strName;
if (!fFileBacked)
return false;
return CWalletDB(strWalletFile).WriteName(strAddress, strName);
}
bool CWallet::DelAddressBookName(const string& strAddress)
{
mapAddressBook.erase(strAddress);
if (!fFileBacked)
return false;
return CWalletDB(strWalletFile).EraseName(strAddress);
}
void CWallet::PrintWallet(const CBlock& block) void CWallet::PrintWallet(const CBlock& block)
{ {
CRITICAL_BLOCK(cs_mapWallet) CRITICAL_BLOCK(cs_mapWallet)

View file

@ -150,12 +150,11 @@ public:
bool LoadWallet(bool& fFirstRunRet); bool LoadWallet(bool& fFirstRunRet);
// bool BackupWallet(const std::string& strDest); // bool BackupWallet(const std::string& strDest);
bool SetAddressBookName(const std::string& strAddress, const std::string& strName) // requires cs_mapAddressBook lock
{ bool SetAddressBookName(const std::string& strAddress, const std::string& strName);
if (!fFileBacked)
return false; // requires cs_mapAddressBook lock
return CWalletDB(strWalletFile).WriteName(strAddress, strName); bool DelAddressBookName(const std::string& strAddress);
}
void UpdatedTransaction(const uint256 &hashTx) void UpdatedTransaction(const uint256 &hashTx)
{ {