From 03a5b9d249514664f084a3377387e6d71586fe94 Mon Sep 17 00:00:00 2001 From: "James C. Owens" Date: Sun, 19 Jan 2025 14:53:28 -0500 Subject: [PATCH] Change around the AutoGreylist initialization --- src/gridcoin/project.cpp | 22 +++++++++---------- src/gridcoin/project.h | 42 +++++++++++++++++++++++++------------ src/gridcoin/superblock.cpp | 2 +- src/rpc/blockchain.cpp | 2 +- 4 files changed, 42 insertions(+), 26 deletions(-) diff --git a/src/gridcoin/project.cpp b/src/gridcoin/project.cpp index 46db399a90..0bffc6bf9c 100644 --- a/src/gridcoin/project.cpp +++ b/src/gridcoin/project.cpp @@ -17,11 +17,8 @@ using namespace GRC; using LogFlags = BCLog::LogFlags; namespace { -// This is the global singleton for the whitelist. +// This is the global singleton for the whitelist. It also contains a smart pointer to the AutoGreylist cache singleton object. Whitelist g_whitelist; - -// This is the global cached (singleton) for the auto greylist. -std::shared_ptr g_autogreylist_ptr = std::make_shared(); } // anonymous namespace // ----------------------------------------------------------------------------- @@ -33,9 +30,9 @@ Whitelist& GRC::GetWhitelist() return g_whitelist; } -std::shared_ptr AutoGreylist::GetAutoGreylistCache() +std::shared_ptr GRC::GetAutoGreylistCache() { - return g_autogreylist_ptr; + return GRC::GetWhitelist().GetAutoGreylist(); } @@ -589,10 +586,8 @@ WhitelistSnapshot Whitelist::Snapshot(const ProjectEntry::ProjectFilterFlag& fil return WhitelistSnapshot(std::make_shared(projects), filter); } - std::shared_ptr greylist_ptr = GRC::AutoGreylist::GetAutoGreylistCache(); - if (refresh_greylist) { - greylist_ptr->Refresh(); + m_auto_greylist->Refresh(); } // This contains the override for automatic greylisting integrated with the switch. If the AutoGreylist class refresh @@ -615,7 +610,7 @@ WhitelistSnapshot Whitelist::Snapshot(const ProjectEntry::ProjectFilterFlag& fil // applies the current state of the greylist at the time of the construction of the whitelist snapshot, without // disturbing the underlying projects registry. - bool in_greylist = greylist_ptr->Contains(iter.first); + bool in_greylist = m_auto_greylist->Contains(iter.first); // If the project does NOT have a status of auto greylist override, and it is either active or already manually // greylisted, then if it is in the greylist, mark with the status auto greylisted. @@ -701,7 +696,7 @@ void Whitelist::Reset() m_project_db.clear(); // If the whitelist registry is reset, the auto greylist cache should be reset as well. - AutoGreylist::GetAutoGreylistCache()->Reset(); + m_auto_greylist->Reset(); } void Whitelist::AddDelete(const ContractContext& ctx) @@ -1000,6 +995,11 @@ const Whitelist::ProjectEntryMap Whitelist::GetProjectsFirstActive() const return m_project_first_actives; } +std::shared_ptr Whitelist::GetAutoGreylist() +{ + return m_auto_greylist; +} + Whitelist::ProjectEntryDB &Whitelist::GetProjectDB() { return m_project_db; diff --git a/src/gridcoin/project.h b/src/gridcoin/project.h index 94bfc23358..a5cab7c63d 100644 --- a/src/gridcoin/project.h +++ b/src/gridcoin/project.h @@ -832,14 +832,7 @@ class AutoGreylist //! void Reset(); - //! - //! \brief Static method to provide the shared pointer to the global auto greylist cache object. The actual - //! global is in an anonymous namespace to ensure the access is only through this method and also prevents having - //! to have extern statements all over. - //! - //! \return shared pointer to the auto greylist global cache object. - //! - static std::shared_ptr GetAutoGreylistCache(); + //static std::shared_ptr GetAutoGreylistCache(); private: mutable CCriticalSection autogreylist_lock; @@ -862,9 +855,13 @@ class Whitelist : public IContractHandler //! Version 1: >= 5.4.6.0. //! Whitelist() - : m_project_db(1) - { - }; + : m_project_entries() + , m_pending_project_entries() + , m_expired_project_entries() + , m_project_first_actives() + , m_project_db(1) + , m_auto_greylist(std::make_shared()) + {} //! //! \brief The type that keys project entries by their key strings. Note that the entries @@ -995,6 +992,13 @@ class Whitelist : public IContractHandler //! const ProjectEntryMap GetProjectsFirstActive() const; + //! + //! \brief Method to provide the shared pointer to the global auto greylist cache object. + //! + //! \return shared pointer to the auto greylist global cache object. + //! + std::shared_ptr GetAutoGreylist(); + //! //! \brief Specializes the template RegistryDB for the ScraperEntry class. Note that std::set is not //! actually used. @@ -1027,13 +1031,16 @@ class Whitelist : public IContractHandler void AddDelete(const ContractContext& ctx); ProjectEntryMap m_project_entries; //!< The set of whitelisted projects. - PendingProjectEntryMap m_pending_project_entries {}; //!< Not actually used. Only to satisfy the template. - std::set m_expired_project_entries {}; //!< Not actually used. Only to satisfy the template. + PendingProjectEntryMap m_pending_project_entries; //!< Not actually used. Only to satisfy the template. + + std::set m_expired_project_entries; //!< Not actually used. Only to satisfy the template. ProjectEntryMap m_project_first_actives; //!< Tracks when projects were first activated for auto greylisting purposes. ProjectEntryDB m_project_db; //!< The project db member + + std::shared_ptr m_auto_greylist; //!< Smart shared pointer to the AutoGreylist cache object public: ProjectEntryDB& GetProjectDB(); @@ -1045,6 +1052,15 @@ class Whitelist : public IContractHandler //! \return Current global whitelist registry instance. //! Whitelist& GetWhitelist(); + +//! +//! \brief Global scope function to provide the shared pointer to the global auto greylist cache object. The AutoGreylist +//! cache is originally constructed and a pointer held in m_auto_greylist in the Whitelist class global singleton, g_whitelist. +//! +//! \return shared pointer to the auto greylist global cache object. +//! +std::shared_ptr GetAutoGreylistCache(); + } // namespace GRC #endif // GRIDCOIN_PROJECT_H diff --git a/src/gridcoin/superblock.cpp b/src/gridcoin/superblock.cpp index 018315c7dc..e14cb25bbb 100644 --- a/src/gridcoin/superblock.cpp +++ b/src/gridcoin/superblock.cpp @@ -628,7 +628,7 @@ Superblock Superblock::FromConvergence( superblock.m_projects_all_cpids_total_credits.Reset(stats.mScraperConvergedStats.m_total_credit_map); // Refresh the auto greylist and refresh this superblock with the greylist status. - std::shared_ptr greylist_ptr = GRC::AutoGreylist::GetAutoGreylistCache(); + std::shared_ptr greylist_ptr = GRC::GetAutoGreylistCache(); greylist_ptr->RefreshWithSuperblock(superblock); } diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index dc2fe6e6e4..9ff5094726 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -2704,7 +2704,7 @@ UniValue getautogreylist(const UniValue& params, bool fHelp) UniValue res(UniValue::VOBJ); - std::shared_ptr greylist_ptr = GRC::AutoGreylist::GetAutoGreylistCache(); + std::shared_ptr greylist_ptr = GRC::GetAutoGreylistCache(); greylist_ptr->Refresh();