Skip to content

Commit

Permalink
merge bitcoin#25157: Fix -rpcwait with -netinfo returning negative ti…
Browse files Browse the repository at this point in the history
…me durations
  • Loading branch information
kwvg committed Dec 8, 2024
1 parent 484447c commit ccde10b
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/bitcoin-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,6 @@ class NetinfoRequestHandler : public BaseRequestHandler
if (conn_type == "addr-fetch") return "addr";
return "";
}
const int64_t m_time_now{count_seconds(Now<CliSeconds>())};

public:
static constexpr int ID_PEERINFO = 0;
Expand Down Expand Up @@ -492,6 +491,7 @@ class NetinfoRequestHandler : public BaseRequestHandler
if (networkinfo["version"].get_int() < 200000) {
throw std::runtime_error("-netinfo requires dashd server to be running v20.0 and up");
}
const int64_t time_now{count_seconds(Now<CliSeconds>())};

// Count peer connection totals, and if DetailsRequested(), store peer data in a vector of structs.
for (const UniValue& peer : batch[ID_PEERINFO]["result"].getValues()) {
Expand Down Expand Up @@ -522,7 +522,7 @@ class NetinfoRequestHandler : public BaseRequestHandler
const double min_ping{peer["minping"].isNull() ? -1 : peer["minping"].get_real()};
const double ping{peer["pingtime"].isNull() ? -1 : peer["pingtime"].get_real()};
const std::string addr{peer["addr"].get_str()};
const std::string age{conn_time == 0 ? "" : ToString((m_time_now - conn_time) / 60)};
const std::string age{conn_time == 0 ? "" : ToString((time_now - conn_time) / 60)};
const std::string sub_version{peer["subver"].get_str()};
const std::string transport{peer["transport_protocol_type"].isNull() ? "v1" : peer["transport_protocol_type"].get_str()};
const bool is_addr_relay_enabled{peer["addr_relay_enabled"].isNull() ? false : peer["addr_relay_enabled"].get_bool()};
Expand Down Expand Up @@ -560,10 +560,10 @@ class NetinfoRequestHandler : public BaseRequestHandler
peer.transport_protocol_type.rfind('v', 0) == 0 ? peer.transport_protocol_type[1] : ' ',
PingTimeToString(peer.min_ping),
PingTimeToString(peer.ping),
peer.last_send ? ToString(m_time_now - peer.last_send) : "",
peer.last_recv ? ToString(m_time_now - peer.last_recv) : "",
peer.last_trxn ? ToString((m_time_now - peer.last_trxn) / 60) : peer.is_block_relay ? "*" : "",
peer.last_blck ? ToString((m_time_now - peer.last_blck) / 60) : "",
peer.last_send ? ToString(time_now - peer.last_send) : "",
peer.last_recv ? ToString(time_now - peer.last_recv) : "",
peer.last_trxn ? ToString((time_now - peer.last_trxn) / 60) : peer.is_block_relay ? "*" : "",
peer.last_blck ? ToString((time_now - peer.last_blck) / 60) : "",
strprintf("%s%s", peer.is_bip152_hb_to ? "." : " ", peer.is_bip152_hb_from ? "*" : " "),
m_max_addr_processed_length, // variable spacing
peer.addr_processed ? ToString(peer.addr_processed) : peer.is_addr_relay_enabled ? "" : ".",
Expand Down Expand Up @@ -874,7 +874,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 auto deadline{GetTime<std::chrono::microseconds>() + 1s * timeout};
const auto deadline{std::chrono::steady_clock::now() + 1s * timeout};

do {
try {
Expand All @@ -887,8 +887,7 @@ static UniValue ConnectAndCallRPC(BaseRequestHandler* rh, const std::string& str
}
break; // Connection succeeded, no need to retry.
} catch (const CConnectionFailed& e) {
const auto now{GetTime<std::chrono::microseconds>()};
if (fWait && (timeout <= 0 || now < deadline)) {
if (fWait && (timeout <= 0 || std::chrono::steady_clock::now() < deadline)) {
UninterruptibleSleep(1s);
} else {
throw CConnectionFailed(strprintf("timeout on transient error: %s", e.what()));
Expand Down

0 comments on commit ccde10b

Please sign in to comment.