From e4abf7e1dea104b44d0cd5c83707c758ea5d62b2 Mon Sep 17 00:00:00 2001 From: Peter Bushnell Date: Tue, 19 Jul 2022 09:28:29 +0100 Subject: [PATCH 1/2] Order getmasternodeblocks results --- src/masternodes/masternodes.h | 6 ++++-- src/masternodes/rpc_masternodes.cpp | 14 ++++++++++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/masternodes/masternodes.h b/src/masternodes/masternodes.h index 456d698bc3b..4122099f6af 100644 --- a/src/masternodes/masternodes.h +++ b/src/masternodes/masternodes.h @@ -210,13 +210,15 @@ class CMasternodesView : public virtual CStorageView void SetMasternodeLastBlockTime(const CKeyID & minter, const uint32_t &blockHeight, const int64_t &time); std::optional GetMasternodeLastBlockTime(const CKeyID & minter, const uint32_t height); void EraseMasternodeLastBlockTime(const uint256 &minter, const uint32_t& blockHeight); - void ForEachMinterNode(std::function)> callback, MNBlockTimeKey const & start = {}); + void ForEachMinterNode(std::function)> callback, + MNBlockTimeKey const & start = {uint256{}, std::numeric_limits::max()}); // Subnode block times void SetSubNodesBlockTime(const CKeyID & minter, const uint32_t &blockHeight, const uint8_t id, const int64_t& time); std::vector GetSubNodesBlockTime(const CKeyID & minter, const uint32_t height); void EraseSubNodesLastBlockTime(const uint256& nodeId, const uint32_t& blockHeight); - void ForEachSubNode(std::function)> callback, SubNodeBlockTimeKey const & start = {}); + void ForEachSubNode(std::function)> callback, + SubNodeBlockTimeKey const & start = {uint256{}, uint8_t{}, std::numeric_limits::max()}); uint16_t GetTimelock(const uint256& nodeId, const CMasternode& node, const uint64_t height) const; diff --git a/src/masternodes/rpc_masternodes.cpp b/src/masternodes/rpc_masternodes.cpp index 661113d9597..61fee71e131 100644 --- a/src/masternodes/rpc_masternodes.cpp +++ b/src/masternodes/rpc_masternodes.cpp @@ -538,7 +538,7 @@ UniValue getmasternodeblocks(const JSONRPCRequest& request) { UniValue identifier = request.params[0].get_obj(); int idCount{0}; - uint256 mn_id; + uint256 mn_id{}; if (!identifier["id"].isNull()) { mn_id = ParseHashV(identifier["id"], "id"); @@ -605,7 +605,8 @@ UniValue getmasternodeblocks(const JSONRPCRequest& request) { if (!request.params[1].isNull()) { depth = request.params[1].get_int(); } - UniValue ret(UniValue::VOBJ); + + std::map> mintedBlocks; auto currentHeight = ::ChainActive().Height(); depth = std::min(depth, currentHeight); auto startBlock = currentHeight - depth; @@ -624,7 +625,7 @@ UniValue getmasternodeblocks(const JSONRPCRequest& request) { auto tip = ::ChainActive()[blockHeight]; if (tip && depth > 0) { lastHeight = tip->nHeight; - ret.pushKV(std::to_string(lastHeight), tip->GetBlockHash().ToString()); + mintedBlocks.emplace(lastHeight, tip->GetBlockHash()); depth--; } @@ -644,10 +645,15 @@ UniValue getmasternodeblocks(const JSONRPCRequest& request) { for (; tip && tip->nHeight > creationHeight && depth > 0 && tip->nHeight > startBlock; tip = tip->pprev, --depth) { auto id = pcustomcsview->GetMasternodeIdByOperator(tip->minterKey()); if (id && *id == mn_id) { - ret.pushKV(std::to_string(tip->nHeight), tip->GetBlockHash().ToString()); + mintedBlocks.emplace(tip->nHeight, tip->GetBlockHash()); } } + UniValue ret(UniValue::VOBJ); + for (const auto& [height, hash] : mintedBlocks) { + ret.pushKV(std::to_string(height), hash.ToString()); + } + return GetRPCResultCache().Set(request, ret); } From 5766902d1d0f211c27d242675deb52aea2740978 Mon Sep 17 00:00:00 2001 From: Prasanna Loganathar Date: Tue, 2 Aug 2022 11:36:45 +0530 Subject: [PATCH 2/2] Make shutdown behavior safer --- src/init.cpp | 4 ++++ src/masternodes/anchors.h | 2 -- src/spv/spv_wrapper.h | 6 +----- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index cd568b4f05a..c835f3bc7db 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -795,6 +795,10 @@ static void ThreadImport(std::vector vImportFiles) if (file) { LogPrintf("Importing blocks file %s...\n", path.string()); LoadExternalBlockFile(chainparams, file); + if (ShutdownRequested()) { + LogPrintf("Shutdown requested. Exit %s\n", __func__); + return; + } } else { LogPrintf("Warning: Could not open blocks file %s\n", path.string()); } diff --git a/src/masternodes/anchors.h b/src/masternodes/anchors.h index d27b7a026bb..cdfa10e0475 100644 --- a/src/masternodes/anchors.h +++ b/src/masternodes/anchors.h @@ -276,8 +276,6 @@ class CAnchorIndex while (pcursor->Valid()) { - // ShutdownRequested replaces interruption_point - if (ShutdownRequested()) break; std::pair key; if (pcursor->GetKey(key) && key.first == prefix) { diff --git a/src/spv/spv_wrapper.h b/src/spv/spv_wrapper.h index 42b3d2f7f4e..87623f20633 100644 --- a/src/spv/spv_wrapper.h +++ b/src/spv/spv_wrapper.h @@ -202,8 +202,6 @@ class CSpvWrapper while (pcursor->Valid()) { - // ShutdownRequested replaces interruption_point - if (ShutdownRequested()) break; std::pair key; if (pcursor->GetKey(key) && key.first == prefix) { @@ -231,8 +229,6 @@ class CSpvWrapper while (pcursor->Valid()) { - // ShutdownRequested replaces interruption_point - if (ShutdownRequested()) break; std::pair key; if (pcursor->GetKey(key) && key.first == prefix) { @@ -261,7 +257,7 @@ class CFakeSpvWrapper : public CSpvWrapper CFakeSpvWrapper(); void Connect() override; - void Disconnect() override; + void Disconnect() override; bool IsConnected() const override; void CancelPendingTxs() override;