From 3827355cce44fbb7fdbc4171077d71b9bc332194 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Sun, 17 Mar 2024 14:22:36 +0000 Subject: [PATCH] refactor: move key initialization to InitKeys, define destructor --- src/init.cpp | 17 +---------------- src/masternode/node.cpp | 26 ++++++++++++++++++++++++++ src/masternode/node.h | 3 ++- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 78f6a56dfa386..a0cecbd5e8732 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -370,12 +370,6 @@ void PrepareShutdown(NodeContext& node) } if (fMasternodeMode) { UnregisterValidationInterface(::activeMasternodeManager.get()); - - LOCK(::activeMasternodeManager->cs); - // make sure to clean up BLS keys before global destructors are called (they have allocated from the secure memory pool) - ::activeMasternodeManager->m_info.blsKeyOperator.reset(); - ::activeMasternodeManager->m_info.blsPubKeyOperator.reset(); - ::activeMasternodeManager.reset(); } @@ -1860,16 +1854,7 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc { // Create and register activeMasternodeManager, will init later in ThreadImport ::activeMasternodeManager = std::make_unique(*node.connman, ::deterministicMNManager); - - LOCK(::activeMasternodeManager->cs); - assert(::activeMasternodeManager->m_info.blsKeyOperator == nullptr); - assert(::activeMasternodeManager->m_info.blsPubKeyOperator == nullptr); - ::activeMasternodeManager->m_info.blsKeyOperator = std::make_unique(keyOperator); - ::activeMasternodeManager->m_info.blsPubKeyOperator = std::make_unique(keyOperator.GetPublicKey()); - // We don't know the actual scheme at this point, print both - LogPrintf("MASTERNODE:\n blsPubKeyOperator legacy: %s\n blsPubKeyOperator basic: %s\n", - ::activeMasternodeManager->m_info.blsPubKeyOperator->ToString(true), - ::activeMasternodeManager->m_info.blsPubKeyOperator->ToString(false)); + ::activeMasternodeManager->InitKeys(keyOperator); RegisterValidationInterface(::activeMasternodeManager.get()); } diff --git a/src/masternode/node.cpp b/src/masternode/node.cpp index 047559e8f3ea2..de2aaf16852dc 100644 --- a/src/masternode/node.cpp +++ b/src/masternode/node.cpp @@ -17,6 +17,17 @@ // Keep track of the active Masternode std::unique_ptr activeMasternodeManager; +CActiveMasternodeManager::~CActiveMasternodeManager() +{ + // Make sure to clean up BLS keys before global destructors are called + // (they have been allocated from the secure memory pool) + { + LOCK(cs); + m_info.blsKeyOperator.reset(); + m_info.blsPubKeyOperator.reset(); + } +} + std::string CActiveMasternodeManager::GetStateString() const { switch (state) { @@ -134,6 +145,21 @@ void CActiveMasternodeManager::Init(const CBlockIndex* pindex) state = MASTERNODE_READY; } +void CActiveMasternodeManager::InitKeys(const CBLSSecretKey& sk) +{ + AssertLockNotHeld(cs); + + LOCK(cs); + assert(m_info.blsKeyOperator == nullptr); + assert(m_info.blsPubKeyOperator == nullptr); + m_info.blsKeyOperator = std::make_unique(sk); + m_info.blsPubKeyOperator = std::make_unique(sk.GetPublicKey()); + // We don't know the actual scheme at this point, print both + LogPrintf("MASTERNODE:\n blsPubKeyOperator legacy: %s\n blsPubKeyOperator basic: %s\n", + m_info.blsPubKeyOperator->ToString(/*specificLegacyScheme=*/ true), + m_info.blsPubKeyOperator->ToString(/*specificLegacyScheme=*/ false)); +} + void CActiveMasternodeManager::UpdatedBlockTip(const CBlockIndex* pindexNew, const CBlockIndex* pindexFork, bool fInitialDownload) { LOCK2(::cs_main, cs); diff --git a/src/masternode/node.h b/src/masternode/node.h index e3e25c66cec49..eba9eb36983b8 100644 --- a/src/masternode/node.h +++ b/src/masternode/node.h @@ -51,11 +51,12 @@ class CActiveMasternodeManager final : public CValidationInterface public: explicit CActiveMasternodeManager(CConnman& _connman, const std::unique_ptr& dmnman) : connman(_connman), m_dmnman(dmnman) {}; - ~CActiveMasternodeManager() = default; + ~CActiveMasternodeManager(); void UpdatedBlockTip(const CBlockIndex* pindexNew, const CBlockIndex* pindexFork, bool fInitialDownload) override; void Init(const CBlockIndex* pindex); + void InitKeys(const CBLSSecretKey& sk) LOCKS_EXCLUDED(cs); std::string GetStateString() const; std::string GetStatus() const;