Skip to content

Commit

Permalink
refactor: make bls{Pub}KeyOperator member variables instead of pointers
Browse files Browse the repository at this point in the history
Using unique_ptr is a relic of activeMasternodeInfo, as it was accessible
independent of CActiveMasternodeManager. As it is now only accessible
if CActiveMasternodeManager is initialized, we can make it part of its
constructor and make them const.

Removes the assertion and sanity checks to see if key pair is valid.
  • Loading branch information
kwvg committed Mar 24, 2024
1 parent fbc7836 commit 73cef4f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 41 deletions.
4 changes: 1 addition & 3 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1853,9 +1853,7 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc
fMasternodeMode = true;
{
// Create and register activeMasternodeManager, will init later in ThreadImport
::activeMasternodeManager = std::make_unique<CActiveMasternodeManager>(*node.connman, ::deterministicMNManager);
::activeMasternodeManager->InitKeys(keyOperator);

::activeMasternodeManager = std::make_unique<CActiveMasternodeManager>(keyOperator, *node.connman, ::deterministicMNManager);
RegisterValidationInterface(::activeMasternodeManager.get());
}
}
Expand Down
41 changes: 13 additions & 28 deletions src/masternode/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
// Keep track of the active Masternode
std::unique_ptr<CActiveMasternodeManager> activeMasternodeManager;

CActiveMasternodeManager::~CActiveMasternodeManager()
CActiveMasternodeManager::CActiveMasternodeManager(const CBLSSecretKey& sk, CConnman& _connman, const std::unique_ptr<CDeterministicMNManager>& dmnman) :
m_info(sk, sk.GetPublicKey()),
connman(_connman),
m_dmnman(dmnman)
{
// 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();
}
assert(sk.IsValid()); /* We can assume pk is valid if sk is valid */
LogPrintf("MASTERNODE:\n blsPubKeyOperator legacy: %s\n blsPubKeyOperator basic: %s\n",
m_info.blsPubKeyOperator.ToString(/*specificLegacyScheme=*/ true),
m_info.blsPubKeyOperator.ToString(/*specificLegacyScheme=*/ false));
}

std::string CActiveMasternodeManager::GetStateString() const
Expand Down Expand Up @@ -97,7 +97,7 @@ void CActiveMasternodeManager::Init(const CBlockIndex* pindex)

CDeterministicMNList mnList = Assert(m_dmnman)->GetListForBlock(pindex);

CDeterministicMNCPtr dmn = mnList.GetMNByOperatorKey(*m_info.blsPubKeyOperator);
CDeterministicMNCPtr dmn = mnList.GetMNByOperatorKey(m_info.blsPubKeyOperator);
if (!dmn) {
// MN not appeared on the chain yet
return;
Expand Down Expand Up @@ -146,21 +146,6 @@ 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<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)
{
LOCK2(::cs_main, cs);
Expand Down Expand Up @@ -258,7 +243,7 @@ template <template <typename> class EncryptedObj, typename Obj>
int version) const
{
AssertLockNotHeld(cs);
return WITH_LOCK(cs, return obj.Decrypt(idx, *Assert(m_info.blsKeyOperator), ret_obj, version));
return WITH_LOCK(cs, return obj.Decrypt(idx, m_info.blsKeyOperator, ret_obj, version));
}
template bool CActiveMasternodeManager::Decrypt(const CBLSIESEncryptedObject<CBLSSecretKey>& obj, size_t idx,
CBLSSecretKey& ret_obj, int version) const;
Expand All @@ -268,18 +253,18 @@ template bool CActiveMasternodeManager::Decrypt(const CBLSIESMultiRecipientObjec
[[nodiscard]] CBLSSignature CActiveMasternodeManager::Sign(const uint256& hash) const
{
AssertLockNotHeld(cs);
return WITH_LOCK(cs, return Assert(m_info.blsKeyOperator)->Sign(hash));
return WITH_LOCK(cs, return m_info.blsKeyOperator.Sign(hash));
}

[[nodiscard]] CBLSSignature CActiveMasternodeManager::Sign(const uint256& hash, const bool is_legacy) const
{
AssertLockNotHeld(cs);
return WITH_LOCK(cs, return Assert(m_info.blsKeyOperator)->Sign(hash, is_legacy));
return WITH_LOCK(cs, return m_info.blsKeyOperator.Sign(hash, is_legacy));
}

// We need to pass a copy as opposed to a const ref because CBLSPublicKeyVersionWrapper
// does not accept a const ref in its construction args
[[nodiscard]] CBLSPublicKey CActiveMasternodeManager::GetPubKey() const
{
return *Assert(m_info.blsPubKeyOperator);
return m_info.blsPubKeyOperator;
}
16 changes: 7 additions & 9 deletions src/masternode/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,27 @@
#ifndef BITCOIN_MASTERNODE_NODE_H
#define BITCOIN_MASTERNODE_NODE_H

#include <bls/bls.h>
#include <netaddress.h>
#include <primitives/transaction.h>
#include <threadsafety.h>
#include <validationinterface.h>

class CBLSPublicKey;
class CBLSSecretKey;
class CBLSSignature;
class CDeterministicMNManager;

struct CActiveMasternodeInfo {
// Keys for the active Masternode
std::unique_ptr<CBLSPublicKey> blsPubKeyOperator;
std::unique_ptr<CBLSSecretKey> blsKeyOperator;
const CBLSSecretKey blsKeyOperator;
const CBLSPublicKey blsPubKeyOperator;

// Initialized while registering Masternode
uint256 proTxHash;
COutPoint outpoint;
CService service;
bool legacy{true};

CActiveMasternodeInfo(const CBLSSecretKey& blsKeyOperator, const CBLSPublicKey& blsPubKeyOperator) :
blsKeyOperator(blsKeyOperator), blsPubKeyOperator(blsPubKeyOperator) {};
};

class CActiveMasternodeManager final : public CValidationInterface
Expand All @@ -51,14 +52,11 @@ class CActiveMasternodeManager final : public CValidationInterface
const std::unique_ptr<CDeterministicMNManager>& m_dmnman;

public:
explicit CActiveMasternodeManager(CConnman& _connman, const std::unique_ptr<CDeterministicMNManager>& dmnman) :
connman(_connman), m_dmnman(dmnman) {};
~CActiveMasternodeManager();
explicit CActiveMasternodeManager(const CBLSSecretKey& sk, CConnman& _connman, const std::unique_ptr<CDeterministicMNManager>& dmnman);

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;
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/governance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ static UniValue gobject_submit(const JSONRPCRequest& request)
fMnFound = mnList.HasValidMNByCollateral(::activeMasternodeManager->GetOutPoint());

LogPrint(BCLog::GOBJECT, "gobject_submit -- pubKeyOperator = %s, outpoint = %s, params.size() = %lld, fMnFound = %d\n",
(::activeMasternodeManager->GetPubKey().IsValid() ? ::activeMasternodeManager->GetPubKey().ToString(::activeMasternodeManager->IsLegacy()) : "N/A"),
::activeMasternodeManager->GetPubKey().ToString(::activeMasternodeManager->IsLegacy()),
::activeMasternodeManager->GetOutPoint().ToStringShort(), request.params.size(), fMnFound);
} else {
LogPrint(BCLog::GOBJECT, "gobject_submit -- pubKeyOperator = N/A, outpoint = N/A, params.size() = %lld, fMnFound = %d\n",
Expand Down

0 comments on commit 73cef4f

Please sign in to comment.