refactor: move key initialization to InitKeys, define destructor

This commit is contained in:
Kittywhiskers Van Gogh 2024-03-17 14:22:36 +00:00
parent e5295dec1f
commit 3827355cce
No known key found for this signature in database
GPG Key ID: 30CD0C065E5C4AAD
3 changed files with 29 additions and 17 deletions

View File

@ -370,12 +370,6 @@ void PrepareShutdown(NodeContext& node)
} }
if (fMasternodeMode) { if (fMasternodeMode) {
UnregisterValidationInterface(::activeMasternodeManager.get()); 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(); ::activeMasternodeManager.reset();
} }
@ -1860,16 +1854,7 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc
{ {
// Create and register activeMasternodeManager, will init later in ThreadImport // Create and register activeMasternodeManager, will init later in ThreadImport
::activeMasternodeManager = std::make_unique<CActiveMasternodeManager>(*node.connman, ::deterministicMNManager); ::activeMasternodeManager = std::make_unique<CActiveMasternodeManager>(*node.connman, ::deterministicMNManager);
::activeMasternodeManager->InitKeys(keyOperator);
LOCK(::activeMasternodeManager->cs);
assert(::activeMasternodeManager->m_info.blsKeyOperator == nullptr);
assert(::activeMasternodeManager->m_info.blsPubKeyOperator == nullptr);
::activeMasternodeManager->m_info.blsKeyOperator = std::make_unique<CBLSSecretKey>(keyOperator);
::activeMasternodeManager->m_info.blsPubKeyOperator = std::make_unique<CBLSPublicKey>(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));
RegisterValidationInterface(::activeMasternodeManager.get()); RegisterValidationInterface(::activeMasternodeManager.get());
} }

View File

@ -17,6 +17,17 @@
// Keep track of the active Masternode // Keep track of the active Masternode
std::unique_ptr<CActiveMasternodeManager> activeMasternodeManager; std::unique_ptr<CActiveMasternodeManager> 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 std::string CActiveMasternodeManager::GetStateString() const
{ {
switch (state) { switch (state) {
@ -134,6 +145,21 @@ void CActiveMasternodeManager::Init(const CBlockIndex* pindex)
state = MASTERNODE_READY; 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<CBLSSecretKey>(sk);
m_info.blsPubKeyOperator = std::make_unique<CBLSPublicKey>(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) void CActiveMasternodeManager::UpdatedBlockTip(const CBlockIndex* pindexNew, const CBlockIndex* pindexFork, bool fInitialDownload)
{ {
LOCK2(::cs_main, cs); LOCK2(::cs_main, cs);

View File

@ -51,11 +51,12 @@ private:
public: public:
explicit CActiveMasternodeManager(CConnman& _connman, const std::unique_ptr<CDeterministicMNManager>& dmnman) : explicit CActiveMasternodeManager(CConnman& _connman, const std::unique_ptr<CDeterministicMNManager>& dmnman) :
connman(_connman), m_dmnman(dmnman) {}; connman(_connman), m_dmnman(dmnman) {};
~CActiveMasternodeManager() = default; ~CActiveMasternodeManager();
void UpdatedBlockTip(const CBlockIndex* pindexNew, const CBlockIndex* pindexFork, bool fInitialDownload) override; void UpdatedBlockTip(const CBlockIndex* pindexNew, const CBlockIndex* pindexFork, bool fInitialDownload) override;
void Init(const CBlockIndex* pindex); void Init(const CBlockIndex* pindex);
void InitKeys(const CBLSSecretKey& sk) LOCKS_EXCLUDED(cs);
std::string GetStateString() const; std::string GetStateString() const;
std::string GetStatus() const; std::string GetStatus() const;