Merge bitcoin/bitcoin#22327: cli: Avoid truncating -rpcwaittimeout

fa34cb8024 cli: Avoid truncating -rpcwaittimeout (MarcoFalke)

Pull request description:

  `seconds` is not enough precision to "exactly" store a timestamp n seconds into the future. Improve the precision by using `microseconds`. Fixes #22325

  Also, use chrono literals.

ACKs for top commit:
  jonatack:
    ACK fa34cb8024 review, debug-built, tested
  theStack:
    Tested ACK fa34cb8024

Tree-SHA512: 7158da8545f9998a82bcc8636e04564efdb1e1be43b4288298c151b4df29ad47a2760259eefadd4a01db92ea18a1e017f3febc1cd8c69a4b28c86180229d8c90
This commit is contained in:
MarcoFalke 2021-06-24 16:01:18 +02:00
commit b2f5c38333
No known key found for this signature in database
GPG key ID: CE2B75697E69A548

View file

@ -797,7 +797,7 @@ static UniValue ConnectAndCallRPC(BaseRequestHandler* rh, const std::string& str
// Execute and handle connection failures with -rpcwait.
const bool fWait = gArgs.GetBoolArg("-rpcwait", false);
const int timeout = gArgs.GetArg("-rpcwaittimeout", DEFAULT_WAIT_CLIENT_TIMEOUT);
const int64_t deadline = GetTime<std::chrono::seconds>().count() + timeout;
const auto deadline{GetTime<std::chrono::microseconds>() + 1s * timeout};
do {
try {
@ -810,9 +810,9 @@ static UniValue ConnectAndCallRPC(BaseRequestHandler* rh, const std::string& str
}
break; // Connection succeeded, no need to retry.
} catch (const CConnectionFailed& e) {
const int64_t now = GetTime<std::chrono::seconds>().count();
const auto now{GetTime<std::chrono::microseconds>()};
if (fWait && (timeout <= 0 || now < deadline)) {
UninterruptibleSleep(std::chrono::seconds{1});
UninterruptibleSleep(1s);
} else {
throw CConnectionFailed(strprintf("timeout on transient error: %s", e.what()));
}