-
Notifications
You must be signed in to change notification settings - Fork 37.6k
Use boost::asio::deadline_timer for walletpassphrase timeout #2625
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,7 +84,7 @@ Value getinfo(const Array& params, bool fHelp) | |
obj.push_back(Pair("keypoolsize", pwalletMain->GetKeyPoolSize())); | ||
obj.push_back(Pair("paytxfee", ValueFromAmount(nTransactionFee))); | ||
if (pwalletMain->IsCrypted()) | ||
obj.push_back(Pair("unlocked_until", (boost::int64_t)nWalletUnlockTime / 1000)); | ||
obj.push_back(Pair("unlocked_until", (boost::int64_t)nWalletUnlockTime)); | ||
obj.push_back(Pair("errors", GetWarnings("statusbar"))); | ||
return obj; | ||
} | ||
|
@@ -1256,56 +1256,11 @@ Value keypoolrefill(const Array& params, bool fHelp) | |
} | ||
|
||
|
||
void ThreadTopUpKeyPool(void* parg) | ||
static void LockWallet(CWallet* pWallet) | ||
{ | ||
// Make this thread recognisable as the key-topping-up thread | ||
RenameThread("bitcoin-key-top"); | ||
|
||
pwalletMain->TopUpKeyPool(); | ||
} | ||
|
||
void ThreadCleanWalletPassphrase(void* parg) | ||
{ | ||
// 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; | ||
LOCK(cs_nWalletUnlockTime); | ||
nWalletUnlockTime = 0; | ||
pWallet->Lock(); | ||
} | ||
|
||
Value walletpassphrase(const Array& params, bool fHelp) | ||
|
@@ -1319,9 +1274,6 @@ Value walletpassphrase(const Array& params, bool fHelp) | |
if (!pwalletMain->IsCrypted()) | ||
throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: running with an unencrypted wallet, but walletpassphrase was called."); | ||
|
||
if (!pwalletMain->IsLocked()) | ||
throw JSONRPCError(RPC_WALLET_ALREADY_UNLOCKED, "Error: Wallet is already unlocked."); | ||
|
||
// Note that the walletpassphrase is stored in params[0] which is not mlock()ed | ||
SecureString strWalletPass; | ||
strWalletPass.reserve(100); | ||
|
@@ -1339,9 +1291,12 @@ Value walletpassphrase(const Array& params, bool fHelp) | |
"walletpassphrase <passphrase> <timeout>\n" | ||
"Stores the wallet decryption key in memory for <timeout> seconds."); | ||
|
||
NewThread(ThreadTopUpKeyPool, NULL); | ||
int64* pnSleepTime = new int64(params[1].get_int64()); | ||
NewThread(ThreadCleanWalletPassphrase, pnSleepTime); | ||
pwalletMain->TopUpKeyPool(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This used to be the source of quite some lag when calling unlock, not sure if it matters, but thats why it was in a separate thread. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Topping up the keypool in a separate thread adds indeterminism which scares me. I'd rather the state of the wallet be known when walletpassphrase returns ("full keypool, unlocked"). E.g. if the keypool is completely exhausted, and I do 'walletpassphrase' immediately followed by a 'send', will I always get a fresh 'change' key or not? Separate thread == maybe, maybe not.... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe using anything from keypool tries to top up keypool before it uses any keys anyway. Also, the topup keypool thread locks wallet first thing anyway, so unless your machine is under heavy load (or a shitty VPS), it should be pretty constant. Anyway, if you dont care about the performance, it doesnt matter much. |
||
|
||
int64 nSleepTime = params[1].get_int64(); | ||
LOCK(cs_nWalletUnlockTime); | ||
nWalletUnlockTime = GetTime() + nSleepTime; | ||
RPCRunLater("lockwallet", boost::bind(LockWallet, pwalletMain), nSleepTime); | ||
|
||
return Value::null; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add a comment that err will have a nonzero value when the timer was cancelled, I had to look this up as it was unclear to me why a timer handler would get an error passed in.