Skip to content

Commit

Permalink
Adjust locking and other minor changes (tbs)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescowens committed Jan 19, 2025
1 parent 64cc435 commit 3783458
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
54 changes: 34 additions & 20 deletions src/gridcoin/project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,28 +324,35 @@ WhitelistSnapshot WhitelistSnapshot::Sorted() const

AutoGreylist::AutoGreylist()
: m_greylist_ptr(std::make_shared<Greylist>())
, m_superblock_hash(uint256 {})
, m_superblock_hash(Superblock().GetHash(true))
{
//Refresh();
}

AutoGreylist::Greylist::const_iterator AutoGreylist::begin() const
{
LOCK(autogreylist_lock);

return m_greylist_ptr->begin();
}

AutoGreylist::Greylist::const_iterator AutoGreylist::end() const
{
LOCK(autogreylist_lock);

return m_greylist_ptr->end();
}

AutoGreylist::Greylist::size_type AutoGreylist::size() const
{
LOCK(autogreylist_lock);

return m_greylist_ptr->size();
}

bool AutoGreylist::Contains(const std::string& name, const bool& only_auto_greylisted) const
{
LOCK(autogreylist_lock);

if (m_greylist_ptr == nullptr) {
return false;
}
Expand All @@ -367,26 +374,24 @@ bool AutoGreylist::Contains(const std::string& name, const bool& only_auto_greyl
}
}

void AutoGreylist::Refresh()
void AutoGreylist::Refresh() EXCLUSIVE_LOCKS_REQUIRED (cs_main)
{
LOCK(cs_main);
SuperblockPtr superblock_ptr = Quorum::CurrentSuperblock();

// If the current superblock has not changed, then no need to do anything.
if (!m_superblock_hash.IsNull() && Quorum::CurrentSuperblock()->GetHash() == m_superblock_hash) {
if (superblock_ptr.IsEmpty()) {
return;
}

SuperblockPtr superblock_ptr = Quorum::CurrentSuperblock();

if (!superblock_ptr.IsEmpty()) {
RefreshWithSuperblock(superblock_ptr);
// If the m_superblock_hash has been populated and the current superblock has not changed, then no need to do anything.
if (m_superblock_hash != Superblock().GetHash() && superblock_ptr->GetHash() == m_superblock_hash) {
return;
}

RefreshWithSuperblock(superblock_ptr);
}

void AutoGreylist::RefreshWithSuperblock(SuperblockPtr superblock_ptr_in)
void AutoGreylist::RefreshWithSuperblock(SuperblockPtr superblock_ptr_in) EXCLUSIVE_LOCKS_REQUIRED (cs_main)
{
LOCK(lock);

if (superblock_ptr_in.IsEmpty()) {
return;
}
Expand All @@ -397,6 +402,8 @@ void AutoGreylist::RefreshWithSuperblock(SuperblockPtr superblock_ptr_in)
// with the underlying whitelist state.
const WhitelistSnapshot whitelist = GetWhitelist().Snapshot(GRC::ProjectEntry::ProjectFilterFlag::ALL_BUT_DELETED, false, false);

LOCK(autogreylist_lock);

m_greylist_ptr->clear();

// No need to go further if the whitelist is empty (ignoring deleted records).
Expand Down Expand Up @@ -514,9 +521,11 @@ void AutoGreylist::RefreshWithSuperblock(SuperblockPtr superblock_ptr_in)
&& (iter->second.GetZCD() > 7
|| iter->second.GetWAS() < Fraction(1, 10));
}

m_superblock_hash = superblock_ptr_in->GetHash();
}

void AutoGreylist::RefreshWithSuperblock(Superblock& superblock)
void AutoGreylist::RefreshWithSuperblock(Superblock& superblock) EXCLUSIVE_LOCKS_REQUIRED (cs_main)
{
SuperblockPtr superblock_ptr;

Expand All @@ -526,11 +535,7 @@ void AutoGreylist::RefreshWithSuperblock(Superblock& superblock)
// overloaded version which takes the superblock_ptr and follows the chain backwards to do the greylist calculations.
superblock_ptr.Replace(superblock);

{
LOCK(cs_main);

superblock_ptr.Rebind(pindexBest);
}
superblock_ptr.Rebind(pindexBest);

RefreshWithSuperblock(superblock_ptr);

Expand All @@ -546,13 +551,22 @@ void AutoGreylist::RefreshWithSuperblock(Superblock& superblock)
}
}

void AutoGreylist::Reset()
{
if (m_greylist_ptr != nullptr) {
m_greylist_ptr->clear();
}

m_superblock_hash = Superblock().GetHash(true);
}

// -----------------------------------------------------------------------------
// Class: Whitelist (Registry)
// -----------------------------------------------------------------------------

WhitelistSnapshot Whitelist::Snapshot(const ProjectEntry::ProjectFilterFlag& filter,
const bool& refresh_greylist,
const bool& include_override) const
const bool& include_override) const EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
ProjectList projects;

Expand Down
6 changes: 4 additions & 2 deletions src/gridcoin/project.h
Original file line number Diff line number Diff line change
Expand Up @@ -689,13 +689,15 @@ class AutoGreylist
//!
void RefreshWithSuperblock(Superblock& superblock);

void Reset();

static std::shared_ptr<AutoGreylist> GetAutoGreylistCache();

private:
CCriticalSection lock;
mutable CCriticalSection autogreylist_lock;

GreylistPtr m_greylist_ptr;
uint256 m_superblock_hash;
QuorumHash m_superblock_hash;
};

//!
Expand Down

0 comments on commit 3783458

Please sign in to comment.