From d70127e8c70adfeff8e830829a758259be1a14f0 Mon Sep 17 00:00:00 2001 From: myriadcoin Date: Wed, 19 Mar 2014 23:15:53 +0200 Subject: [PATCH] Fix walletpassphrase rpc --- src/rpcdump.cpp | 3 ++ src/rpcwallet.cpp | 76 ++++++++++++++++++++++++++++++++++++++++------- src/util.cpp | 12 ++++++++ src/util.h | 2 ++ 4 files changed, 83 insertions(+), 10 deletions(-) diff --git a/src/rpcdump.cpp b/src/rpcdump.cpp index 2d4a750b..86d5f2a3 100644 --- a/src/rpcdump.cpp +++ b/src/rpcdump.cpp @@ -72,6 +72,8 @@ Value importprivkey(const Array& params, bool fHelp) "importprivkey [label] [rescan=true]\n" "Adds a private key (as returned by dumpprivkey) to your wallet."); + EnsureWalletIsUnlocked(); + string strSecret = params[0].get_str(); string strLabel = ""; if (params.size() > 1) @@ -88,6 +90,7 @@ Value importprivkey(const Array& params, bool fHelp) if (!fGood) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key"); CKey key = vchSecret.GetKey(); + if (!key.IsValid()) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Private key outside allowed range"); CPubKey pubkey = key.GetPubKey(); CKeyID vchAddress = pubkey.GetID(); { diff --git a/src/rpcwallet.cpp b/src/rpcwallet.cpp index 8580b08c..857451d1 100644 --- a/src/rpcwallet.cpp +++ b/src/rpcwallet.cpp @@ -1277,11 +1277,63 @@ Value keypoolrefill(const Array& params, bool fHelp) } -static void LockWallet(CWallet* pWallet) +//static void LockWallet(CWallet* pWallet) +//{ +// LOCK(cs_nWalletUnlockTime); +// nWalletUnlockTime = 0; +// pWallet->Lock(); +//} + +void ThreadCleanWalletPassphrase(void* parg) { - LOCK(cs_nWalletUnlockTime); - nWalletUnlockTime = 0; - pWallet->Lock(); + // Make this thread recognisable as the wallet relocking thread + RenameThread("bitcoin-lock-wa"); + + int64 nMyWakeTime = GetTimeMillis() + *((int64*)parg) * 1000; + + ENTER_CRITICAL_SECTION(cs_nWalletUnlockTime); + + if (nWalletUnlockTime == 0) + { + nWalletUnlockTime = nMyWakeTime; + + do + { + if (nWalletUnlockTime==0) + break; + int64 nToSleep = nWalletUnlockTime - GetTimeMillis(); + if (nToSleep <= 0) + break; + + LEAVE_CRITICAL_SECTION(cs_nWalletUnlockTime); + MilliSleep(nToSleep); + ENTER_CRITICAL_SECTION(cs_nWalletUnlockTime); + + } while(1); + + if (nWalletUnlockTime) + { + nWalletUnlockTime = 0; + pwalletMain->Lock(); + } + } + else + { + if (nWalletUnlockTime < nMyWakeTime) + nWalletUnlockTime = nMyWakeTime; + } + + LEAVE_CRITICAL_SECTION(cs_nWalletUnlockTime); + + delete (int64*)parg; +} + +void ThreadTopUpKeyPool(void* parg) +{ + // Make this thread recognisable as the key-topping-up thread + RenameThread("bitcoin-key-top"); + + pwalletMain->TopUpKeyPool(); } Value walletpassphrase(const Array& params, bool fHelp) @@ -1312,13 +1364,17 @@ Value walletpassphrase(const Array& params, bool fHelp) "walletpassphrase \n" "Stores the wallet decryption key in memory for seconds."); - pwalletMain->TopUpKeyPool(); - - int64 nSleepTime = params[1].get_int64(); - LOCK(cs_nWalletUnlockTime); - nWalletUnlockTime = GetTime() + nSleepTime; - RPCRunLater("lockwallet", boost::bind(LockWallet, pwalletMain), nSleepTime); + //pwalletMain->TopUpKeyPool(); + //int64 nSleepTime = params[1].get_int64(); + //LOCK(cs_nWalletUnlockTime); + //nWalletUnlockTime = GetTime() + nSleepTime; + //RPCRunLater("lockwallet", boost::bind(LockWallet, pwalletMain), nSleepTime); + + NewThread(ThreadTopUpKeyPool, NULL); + int64* pnSleepTime = new int64(params[1].get_int64()); + NewThread(ThreadCleanWalletPassphrase, pnSleepTime); + return Value::null; } diff --git a/src/util.cpp b/src/util.cpp index e393f691..90746f4a 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -1468,3 +1468,15 @@ void RenameThread(const char* name) (void)name; #endif } + +bool NewThread(void(*pfn)(void*), void* parg) +{ + try + { + boost::thread(pfn, parg); // thread detaches when out of scope + } catch(boost::thread_resource_error &e) { + printf("Error creating thread: %s\n", e.what()); + return false; + } + return true; +} diff --git a/src/util.h b/src/util.h index bee2749c..48da12c4 100644 --- a/src/util.h +++ b/src/util.h @@ -580,4 +580,6 @@ template void TraceThread(const char* name, Callable func) } } +bool NewThread(void(*pfn)(void*), void* parg); + #endif