From 07b3316d6d1fc47995fb48ace50f9439c85ae824 Mon Sep 17 00:00:00 2001 From: Jon Layton Date: Wed, 24 Oct 2018 16:58:23 -0500 Subject: [PATCH] Fix segfaults by using chain tip header --- src/rpc/blockchain.cpp | 5 +++-- src/rpc/blockchain.h | 2 +- src/rpc/mining.cpp | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index e74af89a2..c14a6c4e5 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -71,8 +71,9 @@ double GetDifficultyINTERNAL(const CBlockIndex* blockindex, bool networkDifficul } uint32_t bits; + auto tipblock = chainActive.Tip()->GetBlockHeader(); if (networkDifficulty) { - bits = GetNextWorkRequired(blockindex, nullptr, Params()); + bits = GetNextWorkRequired(blockindex, &tipblock, Params()); } else { bits = blockindex->nBits; } @@ -1281,7 +1282,7 @@ UniValue getblockchaininfo(const JSONRPCRequest& request) obj.pushKV("blocks", (int)chainActive.Height()); obj.pushKV("headers", pindexBestHeader ? pindexBestHeader->nHeight : -1); obj.pushKV("bestblockhash", chainActive.Tip()->GetBlockHash().GetHex()); - obj.pushKV("difficulty", (double)GetNetworkDifficulty()); + obj.pushKV("difficulty", (double)GetDifficulty(chainActive.Tip())); obj.pushKV("mediantime", (int64_t)chainActive.Tip()->GetMedianTimePast()); obj.pushKV("verificationprogress", GuessVerificationProgress(Params().TxData(), chainActive.Tip())); obj.pushKV("initialblockdownload", IsInitialBlockDownload()); diff --git a/src/rpc/blockchain.h b/src/rpc/blockchain.h index b29da8130..0772cf7f2 100644 --- a/src/rpc/blockchain.h +++ b/src/rpc/blockchain.h @@ -23,7 +23,7 @@ static constexpr int NUM_GETBLOCKSTATS_PERCENTILES = 5; */ double GetDifficulty(const CBlockIndex* blockindex = nullptr); -double GetNetworkDifficulty(const CBlockIndex* blockindex = nullptr); +double GetNetworkDifficulty(const CBlockIndex* blockindex); /** Callback for when block tip changed. */ void RPCNotifyBlockChange(bool ibd, const CBlockIndex *); diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp index 1cd9ff9da..e123a8d6a 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -254,7 +254,7 @@ static UniValue getmininginfo(const JSONRPCRequest& request) obj.pushKV("blocks", (int)chainActive.Height()); obj.pushKV("currentblockweight", (uint64_t)nLastBlockWeight); obj.pushKV("currentblocktx", (uint64_t)nLastBlockTx); - obj.pushKV("difficulty", (double)GetNetworkDifficulty()); + obj.pushKV("difficulty", (double)GetNetworkDifficulty(chainActive.Tip())); obj.pushKV("networkhashps", getnetworkhashps(request)); obj.pushKV("pooledtx", (uint64_t)mempool.size()); obj.pushKV("chain", Params().NetworkIDString());