aboutsummaryrefslogtreecommitdiff
path: root/rpc.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'rpc.cpp')
-rw-r--r--rpc.cpp92
1 files changed, 55 insertions, 37 deletions
diff --git a/rpc.cpp b/rpc.cpp
index 97710ff6b8..d527f9b35a 100644
--- a/rpc.cpp
+++ b/rpc.cpp
@@ -315,46 +315,45 @@ Value getnewaddress(const Array& params, bool fHelp)
}
+// requires cs_main, cs_mapWallet locks
string GetAccountAddress(string strAccount, bool bForceNew=false)
{
string strAddress;
- CRITICAL_BLOCK(cs_mapWallet)
- {
- CWalletDB walletdb;
- walletdb.TxnBegin();
-
- CAccount account;
- walletdb.ReadAccount(strAccount, account);
+ CWalletDB walletdb;
+ walletdb.TxnBegin();
- // Check if the current key has been used
- if (!account.vchPubKey.empty())
- {
- CScript scriptPubKey;
- scriptPubKey.SetBitcoinAddress(account.vchPubKey);
- for (map<uint256, CWalletTx>::iterator it = mapWallet.begin();
- it != mapWallet.end() && !account.vchPubKey.empty();
- ++it)
- {
- const CWalletTx& wtx = (*it).second;
- foreach(const CTxOut& txout, wtx.vout)
- if (txout.scriptPubKey == scriptPubKey)
- account.vchPubKey.clear();
- }
- }
+ CAccount account;
+ walletdb.ReadAccount(strAccount, account);
- // Generate a new key
- if (account.vchPubKey.empty() || bForceNew)
+ // Check if the current key has been used
+ if (!account.vchPubKey.empty())
+ {
+ CScript scriptPubKey;
+ scriptPubKey.SetBitcoinAddress(account.vchPubKey);
+ for (map<uint256, CWalletTx>::iterator it = mapWallet.begin();
+ it != mapWallet.end() && !account.vchPubKey.empty();
+ ++it)
{
- account.vchPubKey = GetKeyFromKeyPool();
- string strAddress = PubKeyToAddress(account.vchPubKey);
- SetAddressBookName(strAddress, strAccount);
- walletdb.WriteAccount(strAccount, account);
+ const CWalletTx& wtx = (*it).second;
+ foreach(const CTxOut& txout, wtx.vout)
+ if (txout.scriptPubKey == scriptPubKey)
+ account.vchPubKey.clear();
}
+ }
- walletdb.TxnCommit();
- strAddress = PubKeyToAddress(account.vchPubKey);
+ // Generate a new key
+ if (account.vchPubKey.empty() || bForceNew)
+ {
+ account.vchPubKey = GetKeyFromKeyPool();
+ string strAddress = PubKeyToAddress(account.vchPubKey);
+ SetAddressBookName(strAddress, strAccount);
+ walletdb.WriteAccount(strAccount, account);
}
+
+ walletdb.TxnCommit();
+ strAddress = PubKeyToAddress(account.vchPubKey);
+
return strAddress;
}
@@ -368,7 +367,15 @@ Value getaccountaddress(const Array& params, bool fHelp)
// Parse the account first so we don't generate a key if there's an error
string strAccount = AccountFromValue(params[0]);
- return GetAccountAddress(strAccount);
+ Value ret;
+
+ CRITICAL_BLOCK(cs_main)
+ CRITICAL_BLOCK(cs_mapWallet)
+ {
+ ret = GetAccountAddress(strAccount);
+ }
+
+ return ret;
}
@@ -392,6 +399,8 @@ Value setaccount(const Array& params, bool fHelp)
strAccount = AccountFromValue(params[1]);
// Detect when changing the account of an address that is the 'unused current key' of another account:
+ CRITICAL_BLOCK(cs_main)
+ CRITICAL_BLOCK(cs_mapWallet)
CRITICAL_BLOCK(cs_mapAddressBook)
{
if (mapAddressBook.count(strAddress))
@@ -475,9 +484,13 @@ Value sendtoaddress(const Array& params, bool fHelp)
if (params.size() > 3 && params[3].type() != null_type && !params[3].get_str().empty())
wtx.mapValue["to"] = params[3].get_str();
- string strError = SendMoneyToBitcoinAddress(strAddress, nAmount, wtx);
- if (strError != "")
- throw JSONRPCError(-4, strError);
+ CRITICAL_BLOCK(cs_main)
+ {
+ string strError = SendMoneyToBitcoinAddress(strAddress, nAmount, wtx);
+ if (strError != "")
+ throw JSONRPCError(-4, strError);
+ }
+
return wtx.GetHash().GetHex();
}
@@ -752,6 +765,7 @@ Value sendfrom(const Array& params, bool fHelp)
if (params.size() > 5 && params[5].type() != null_type && !params[5].get_str().empty())
wtx.mapValue["to"] = params[5].get_str();
+ CRITICAL_BLOCK(cs_main)
CRITICAL_BLOCK(cs_mapWallet)
{
// Check funds
@@ -808,6 +822,7 @@ Value sendmany(const Array& params, bool fHelp)
vecSend.push_back(make_pair(scriptPubKey, nAmount));
}
+ CRITICAL_BLOCK(cs_main)
CRITICAL_BLOCK(cs_mapWallet)
{
// Check funds
@@ -1135,8 +1150,11 @@ Value listaccounts(const Array& params, bool fHelp)
CRITICAL_BLOCK(cs_mapWallet)
CRITICAL_BLOCK(cs_mapAddressBook)
{
- foreach(const PAIRTYPE(string, string)& entry, mapAddressBook)
- mapAccountBalances[entry.second] = 0;
+ foreach(const PAIRTYPE(string, string)& entry, mapAddressBook) {
+ uint160 hash160;
+ if(AddressToHash160(entry.first, hash160) && mapPubKeys.count(hash160)) // This address belongs to me
+ mapAccountBalances[entry.second] = 0;
+ }
for (map<uint256, CWalletTx>::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
{
@@ -1468,7 +1486,7 @@ string HTTPPost(const string& strMsg, const map<string,string>& mapRequestHeader
string rfc1123Time()
{
- char buffer[32];
+ char buffer[64];
time_t now;
time(&now);
struct tm* now_gmt = gmtime(&now);