Skip to content

Commit

Permalink
Change around the AutoGreylist initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescowens committed Jan 19, 2025
1 parent a7305d6 commit 03a5b9d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 26 deletions.
22 changes: 11 additions & 11 deletions src/gridcoin/project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<AutoGreylist> g_autogreylist_ptr = std::make_shared<AutoGreylist>();
} // anonymous namespace

// -----------------------------------------------------------------------------
Expand All @@ -33,9 +30,9 @@ Whitelist& GRC::GetWhitelist()
return g_whitelist;
}

std::shared_ptr<AutoGreylist> AutoGreylist::GetAutoGreylistCache()
std::shared_ptr<AutoGreylist> GRC::GetAutoGreylistCache()
{
return g_autogreylist_ptr;
return GRC::GetWhitelist().GetAutoGreylist();
}


Expand Down Expand Up @@ -589,10 +586,8 @@ WhitelistSnapshot Whitelist::Snapshot(const ProjectEntry::ProjectFilterFlag& fil
return WhitelistSnapshot(std::make_shared<ProjectList>(projects), filter);
}

std::shared_ptr<GRC::AutoGreylist> 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
Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -1000,6 +995,11 @@ const Whitelist::ProjectEntryMap Whitelist::GetProjectsFirstActive() const
return m_project_first_actives;
}

std::shared_ptr<AutoGreylist> Whitelist::GetAutoGreylist()
{
return m_auto_greylist;
}

Whitelist::ProjectEntryDB &Whitelist::GetProjectDB()
{
return m_project_db;
Expand Down
42 changes: 29 additions & 13 deletions src/gridcoin/project.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<AutoGreylist> GetAutoGreylistCache();
//static std::shared_ptr<AutoGreylist> GetAutoGreylistCache();

private:
mutable CCriticalSection autogreylist_lock;
Expand All @@ -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<AutoGreylist>())
{}

//!
//! \brief The type that keys project entries by their key strings. Note that the entries
Expand Down Expand Up @@ -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<AutoGreylist> GetAutoGreylist();

//!
//! \brief Specializes the template RegistryDB for the ScraperEntry class. Note that std::set<ProjectEntry> is not
//! actually used.
Expand Down Expand Up @@ -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<ProjectEntry> 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<ProjectEntry> 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<AutoGreylist> m_auto_greylist; //!< Smart shared pointer to the AutoGreylist cache object
public:

ProjectEntryDB& GetProjectDB();
Expand All @@ -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<AutoGreylist> GetAutoGreylistCache();

} // namespace GRC

#endif // GRIDCOIN_PROJECT_H
2 changes: 1 addition & 1 deletion src/gridcoin/superblock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<GRC::AutoGreylist> greylist_ptr = GRC::AutoGreylist::GetAutoGreylistCache();
std::shared_ptr<GRC::AutoGreylist> greylist_ptr = GRC::GetAutoGreylistCache();

greylist_ptr->RefreshWithSuperblock(superblock);
}
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2704,7 +2704,7 @@ UniValue getautogreylist(const UniValue& params, bool fHelp)

UniValue res(UniValue::VOBJ);

std::shared_ptr<GRC::AutoGreylist> greylist_ptr = GRC::AutoGreylist::GetAutoGreylistCache();
std::shared_ptr<GRC::AutoGreylist> greylist_ptr = GRC::GetAutoGreylistCache();

greylist_ptr->Refresh();

Expand Down

0 comments on commit 03a5b9d

Please sign in to comment.