Skip to content

Commit

Permalink
Extend beacon registry db template to cover expired entries
Browse files Browse the repository at this point in the history
This also fixes the HandleCurrentHistoricalEntries specialization
in the beacon registry to properly deal with expired pending beacons.
  • Loading branch information
jamescowens committed Jan 15, 2024
1 parent abf5b65 commit 3f749b9
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 40 deletions.
19 changes: 17 additions & 2 deletions src/gridcoin/beacon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ void BeaconRegistry::Reset()
{
m_beacons.clear();
m_pending.clear();
m_expired_pending.clear();
m_beacon_db.clear();
}

Expand Down Expand Up @@ -1182,6 +1183,7 @@ void BeaconRegistry::Deactivate(const uint256 superblock_hash)
//!
template<> void BeaconRegistry::BeaconDB::HandleCurrentHistoricalEntries(GRC::BeaconRegistry::BeaconMap& entries,
GRC::BeaconRegistry::PendingBeaconMap& pending_entries,
std::set<Beacon_ptr>& expired_entries,
const Beacon& entry,
entry_ptr& historical_entry_ptr,
const uint64_t& recnum,
Expand Down Expand Up @@ -1216,7 +1218,7 @@ template<> void BeaconRegistry::BeaconDB::HandleCurrentHistoricalEntries(GRC::Be

if (entry.m_status == BeaconStatusForStorage::ACTIVE || entry.m_status == BeaconStatusForStorage::RENEWAL)
{
LogPrint(LogFlags::CONTRACT, "INFO: %s: %ss: entry insert: cpid %s, address %s, timestamp %" PRId64 ", "
LogPrint(LogFlags::CONTRACT, "INFO: %s: %s: entry insert: cpid %s, address %s, timestamp %" PRId64 ", "
"hash %s, previous_hash %s, status %s, recnum %" PRId64 ".",
__func__,
key_type,
Expand Down Expand Up @@ -1253,6 +1255,15 @@ template<> void BeaconRegistry::BeaconDB::HandleCurrentHistoricalEntries(GRC::Be
}
}

if (entry.m_status == BeaconStatusForStorage::ACTIVE) {
// Note that in the orginal activation, all the activations happen for a superblock, and then the expired_entry set is
// cleared and then new expired entries recorded from the just committed SB. This method operates at the record level, but
// clearing the expired_entries for each ACTIVE record posting will achieve the same effect, because the entries are ordered
// the proper way. It is a little bit of undesired work, but it is not worth the complexity of feeding the boundaries
// of the group of verified beacons to activate.
expired_entries.clear();
}

if (entry.m_status == BeaconStatusForStorage::EXPIRED_PENDING)
{
LogPrint(LogFlags::CONTRACT, "INFO: %s: %s: expired pending entry delete: cpid %s, address %s, timestamp %" PRId64 ", "
Expand All @@ -1268,6 +1279,9 @@ template<> void BeaconRegistry::BeaconDB::HandleCurrentHistoricalEntries(GRC::Be
recnum
);

// Insert the expired pending entry into the expired entries set.
expired_entries.insert(historical_entry_ptr);

// Delete any entry in the pending map that is marked expired.
pending_entries.erase(entry.GetId());
}
Expand All @@ -1294,7 +1308,7 @@ template<> void BeaconRegistry::BeaconDB::HandleCurrentHistoricalEntries(GRC::Be

int BeaconRegistry::Initialize()
{
int height = m_beacon_db.Initialize(m_beacons, m_pending);
int height = m_beacon_db.Initialize(m_beacons, m_pending, m_expired_pending);

LogPrint(LogFlags::BEACON, "INFO: %s: m_beacon_db size after load: %u", __func__, m_beacon_db.size());
LogPrint(LogFlags::BEACON, "INFO: %s: m_beacons size after load: %u", __func__, m_beacons.size());
Expand All @@ -1306,6 +1320,7 @@ void BeaconRegistry::ResetInMemoryOnly()
{
m_beacons.clear();
m_pending.clear();
m_expired_pending.clear();
m_beacon_db.clear_in_memory_only();
}

Expand Down
1 change: 1 addition & 0 deletions src/gridcoin/beacon.h
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,7 @@ class BeaconRegistry : public IContractHandler
BeaconStatusForStorage,
BeaconMap,
PendingBeaconMap,
std::set<Beacon_ptr>,
HistoricalBeaconMap> BeaconDB;

private:
Expand Down
12 changes: 8 additions & 4 deletions src/gridcoin/contract/registry_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ namespace GRC {
//! M: the map type for the entries
//! P: the map type for pending entries. This is really only used for beacons. In all other registries it is typedef'd to
//! the same as M.
//! X: the map type for expired pending entries. This is really only used for beacons. In all other registries it is typedef'd to
//! the same as M.
//! H: the historical map type for historical entries
//!
template<class E, class SE, class S, class M, class P, class H>
template<class E, class SE, class S, class M, class P, class X, class H>
class RegistryDB
{
public:
Expand Down Expand Up @@ -62,10 +64,12 @@ class RegistryDB
//! \param entries The map of current entries.
//! \param pending_entries. The map of pending entries. This is not used in the general template, only in the beacons
//! specialization.
//! \param expired_entries. The map of expired pending entries. This is not used in the geenral template, only in the
//! beacons specialization.
//!
//! \return block height up to and including which the entry records were stored.
//!
int Initialize(M& entries, P& pending_entries)
int Initialize(M& entries, P& pending_entries, X& expired_entries)
{
bool status = true;
int height = 0;
Expand Down Expand Up @@ -169,7 +173,7 @@ class RegistryDB
m_historical[iter.second.m_hash] = std::make_shared<E>(entry);
entry_ptr& historical_entry_ptr = m_historical[iter.second.m_hash];

HandleCurrentHistoricalEntries(entries, pending_entries, entry,
HandleCurrentHistoricalEntries(entries, pending_entries, expired_entries, entry,
historical_entry_ptr, recnum, key_type);

number_passivated += (uint64_t) HandlePreviousHistoricalEntries(historical_entry_ptr);
Expand Down Expand Up @@ -199,7 +203,7 @@ class RegistryDB
//! \param recnum
//! \param key_type
//!
void HandleCurrentHistoricalEntries(M& entries, P& pending_entries, const E& entry,
void HandleCurrentHistoricalEntries(M& entries, P& pending_entries, X& expired_entries, const E& entry,
entry_ptr& historical_entry_ptr, const uint64_t& recnum,
const std::string& key_type)
{
Expand Down
2 changes: 1 addition & 1 deletion src/gridcoin/project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ int Whitelist::Initialize()
{
LOCK(cs_lock);

int height = m_project_db.Initialize(m_project_entries, m_pending_project_entries);
int height = m_project_db.Initialize(m_project_entries, m_pending_project_entries, m_expired_project_entries);

LogPrint(LogFlags::CONTRACT, "INFO: %s: m_project_db size after load: %u", __func__, m_project_db.size());
LogPrint(LogFlags::CONTRACT, "INFO: %s: m_project_entries size after load: %u", __func__, m_project_entries.size());
Expand Down
6 changes: 5 additions & 1 deletion src/gridcoin/project.h
Original file line number Diff line number Diff line change
Expand Up @@ -618,13 +618,15 @@ class Whitelist : public IContractHandler
static void RunDBPassivation();

//!
//! \brief Specializes the template RegistryDB for the ScraperEntry class
//! \brief Specializes the template RegistryDB for the ScraperEntry class. Note that std::set<ProjectEntry> is not
//! actually used.
//!
typedef RegistryDB<ProjectEntry,
ProjectEntry,
ProjectEntryStatus,
ProjectEntryMap,
PendingProjectEntryMap,
std::set<ProjectEntry>,
HistoricalProjectEntryMap> ProjectEntryDB;

private:
Expand All @@ -644,6 +646,8 @@ class Whitelist : public IContractHandler
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.

ProjectEntryDB m_project_db; //!< The project db member
public:

Expand Down
2 changes: 1 addition & 1 deletion src/gridcoin/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ int ProtocolRegistry::Initialize()
{
LOCK(cs_lock);

int height = m_protocol_db.Initialize(m_protocol_entries, m_pending_protocol_entries);
int height = m_protocol_db.Initialize(m_protocol_entries, m_pending_protocol_entries, m_expired_protocol_entries);

LogPrint(LogFlags::CONTRACT, "INFO: %s: m_protocol_db size after load: %u", __func__, m_protocol_db.size());
LogPrint(LogFlags::CONTRACT, "INFO: %s: m_protocol_entries size after load: %u", __func__, m_protocol_entries.size());
Expand Down
6 changes: 5 additions & 1 deletion src/gridcoin/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -567,13 +567,15 @@ class ProtocolRegistry : public IContractHandler
static void RunDBPassivation();

//!
//! \brief Specializes the template RegistryDB for the ProtocolEntry class
//! \brief Specializes the template RegistryDB for the ProtocolEntry class. Note that std::set<ProtocolEntry>
//! is not actually used.
//!
typedef RegistryDB<ProtocolEntry,
ProtocolEntry,
ProtocolEntryStatus,
ProtocolEntryMap,
PendingProtocolEntryMap,
std::set<ProtocolEntry>,
HistoricalProtocolEntryMap> ProtocolEntryDB;

private:
Expand All @@ -593,6 +595,8 @@ class ProtocolRegistry : public IContractHandler
ProtocolEntryMap m_protocol_entries; //!< Contains the current protocol entries including entries marked DELETED.
PendingProtocolEntryMap m_pending_protocol_entries {}; //!< Not used. Only to satisfy the template.

std::set<ProtocolEntry> m_expired_protocol_entries {}; //!< Not used. Only to satisfy the template.

ProtocolEntryDB m_protocol_db;

public:
Expand Down
2 changes: 1 addition & 1 deletion src/gridcoin/scraper/scraper_registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ int ScraperRegistry::Initialize()
{
LOCK(cs_lock);

int height = m_scraper_db.Initialize(m_scrapers, m_pending_scrapers);
int height = m_scraper_db.Initialize(m_scrapers, m_pending_scrapers, m_expired_scraper_entries);

LogPrint(LogFlags::SCRAPER, "INFO: %s: m_scraper_db size after load: %u", __func__, m_scraper_db.size());
LogPrint(LogFlags::SCRAPER, "INFO: %s: m_scrapers size after load: %u", __func__, m_scrapers.size());
Expand Down
6 changes: 5 additions & 1 deletion src/gridcoin/scraper/scraper_registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -606,13 +606,15 @@ class ScraperRegistry : public IContractHandler
static void RunDBPassivation();

//!
//! \brief Specializes the template RegistryDB for the ScraperEntry class
//! \brief Specializes the template RegistryDB for the ScraperEntry class. Note that std::set<ScraperEntry> is
//! not actually used.
//!
typedef RegistryDB<ScraperEntry,
ScraperEntry,
ScraperEntryStatus,
ScraperMap,
PendingScraperMap,
std::set<ScraperEntry>,
HistoricalScraperMap> ScraperEntryDB;

private:
Expand All @@ -632,6 +634,8 @@ class ScraperRegistry : public IContractHandler
ScraperMap m_scrapers; //!< Contains the current scraper entries, including entries marked DELETED.
PendingScraperMap m_pending_scrapers {}; //!< Not actually used for scrapers. To satisfy the template only.

std::set<ScraperEntry> m_expired_scraper_entries {}; //!< Not actually used for scrapers. To satisfy the template only.

ScraperEntryDB m_scraper_db;

public:
Expand Down
2 changes: 1 addition & 1 deletion src/gridcoin/sidestake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ int SideStakeRegistry::Initialize()
{
LOCK(cs_lock);

int height = m_sidestake_db.Initialize(m_mandatory_sidestake_entries, m_pending_sidestake_entries);
int height = m_sidestake_db.Initialize(m_mandatory_sidestake_entries, m_pending_sidestake_entries, m_expired_sidestake_entries);

SubscribeToCoreSignals();

Expand Down
6 changes: 5 additions & 1 deletion src/gridcoin/sidestake.h
Original file line number Diff line number Diff line change
Expand Up @@ -739,13 +739,15 @@ class SideStakeRegistry : public IContractHandler
static void RunDBPassivation();

//!
//! \brief Specializes the template RegistryDB for the SideStake class
//! \brief Specializes the template RegistryDB for the SideStake class. Note that std::set<MandatorySideStake>
//! is not actually used.
//!
typedef RegistryDB<MandatorySideStake,
MandatorySideStake,
MandatorySideStake::MandatorySideStakeStatus,
MandatorySideStakeMap,
PendingSideStakeMap,
std::set<SideStake>,
HistoricalSideStakeMap> SideStakeDB;

private:
Expand Down Expand Up @@ -783,6 +785,8 @@ class SideStakeRegistry : public IContractHandler
MandatorySideStakeMap m_mandatory_sidestake_entries; //!< Contains the mandatory sidestake entries, including DELETED.
PendingSideStakeMap m_pending_sidestake_entries {}; //!< Not used. Only to satisfy the template.

std::set<SideStake> m_expired_sidestake_entries {}; //!< Not used. Only to satisfy the template.

SideStakeDB m_sidestake_db; //!< The internal sidestake db object for leveldb persistence.

bool m_local_entry_already_saved_to_config = false; //!< Flag to prevent reload on signal if individual entry saved already.
Expand Down
46 changes: 20 additions & 26 deletions src/test/gridcoin/beacon_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ class BeaconRegistryTest
if (beacon != nullptr) {
std::cout << "add beacon record: "
<< "blockheight = " << ctx.m_pindex->nHeight
<< ", hash = " << beacon->m_hash.GetHex()
<< ", cpid = " << beacon->m_cpid.ToString()
<< ", public key = " << HexStr(beacon->m_public_key)
<< ", address = " << beacon->GetAddress().ToString()
Expand All @@ -203,7 +202,6 @@ class BeaconRegistryTest
if (beacon != nullptr) {
std::cout << "delete beacon record: "
<< "blockheight = " << ctx.m_pindex->nHeight
<< ", hash = " << beacon->m_hash.GetHex()
<< ", cpid = " << beacon->m_cpid.ToString()
<< ", public key = " << HexStr(beacon->m_public_key)
<< ", address = " << beacon->GetAddress().ToString()
Expand Down Expand Up @@ -242,7 +240,6 @@ class BeaconRegistryTest
if (activated_beacon != nullptr) {
std::cout << "activated beacon record: "
<< "blockheight = " << pindex->nHeight
<< ", hash = " << activated_beacon->m_hash.GetHex()
<< ", cpid = " << activated_beacon->m_cpid.ToString()
<< ", public key = " << HexStr(activated_beacon->m_public_key)
<< ", address = " << activated_beacon->GetAddress().ToString()
Expand All @@ -258,7 +255,6 @@ class BeaconRegistryTest
if (iter != nullptr) {
std::cout << "expired beacon record: "
<< "blockheight = " << pindex->nHeight
<< ", hash = " << iter->m_hash.GetHex()
<< ", cpid = " << iter->m_cpid.ToString()
<< ", public key = " << HexStr(iter->m_public_key)
<< ", address = " << iter->GetAddress().ToString()
Expand All @@ -279,17 +275,6 @@ class BeaconRegistryTest
for (const auto& iter : registry.Beacons())
{
m_beacons_init[iter.first] = *iter.second;

std::cout << "init beacon record: "
<< "hash = " << iter.second->m_hash.GetHex()
<< ", cpid = " << iter.second->m_cpid.ToString()
<< ", public key = " << HexStr(iter.second->m_public_key)
<< ", address = " << iter.second->GetAddress().ToString()
<< ", timestamp = " << iter.second->m_timestamp
<< ", hash = " << iter.second->m_hash.GetHex()
<< ", prev beacon hash = " << iter.second->m_previous_hash.GetHex()
<< ", status = " << iter.second->StatusToString()
<< std::endl;
}

m_init_number_beacons = m_beacons_init.size();
Expand All @@ -314,6 +299,16 @@ class BeaconRegistryTest
// Create a copy of the referenced beacon object with a shared pointer to it and store.
m_local_historical_beacon_map_init[hash] = std::make_shared<GRC::Beacon>(*beacon_ptr);

std::cout << "init beacon db record: "
<< ", cpid = " << beacon_ptr->m_cpid.ToString()
<< ", public key = " << HexStr(beacon_ptr->m_public_key)
<< ", address = " << beacon_ptr->GetAddress().ToString()
<< ", timestamp = " << beacon_ptr->m_timestamp
<< ", hash = " << beacon_ptr->m_hash.GetHex()
<< ", prev beacon hash = " << beacon_ptr->m_previous_hash.GetHex()
<< ", status = " << beacon_ptr->StatusToString()
<< std::endl;

init_beacon_db_iter = init_beacon_db.advance(init_beacon_db_iter);
}

Expand All @@ -331,17 +326,6 @@ class BeaconRegistryTest
for (const auto& iter : registry.Beacons())
{
m_beacons_reinit[iter.first] = *iter.second;

std::cout << "reinit beacon record: "
<< "hash = " << iter.second->m_hash.GetHex()
<< ", cpid = " << iter.second->m_cpid.ToString()
<< ", public key = " << HexStr(iter.second->m_public_key)
<< ", address = " << iter.second->GetAddress().ToString()
<< ", timestamp = " << iter.second->m_timestamp
<< ", hash = " << iter.second->m_hash.GetHex()
<< ", prev beacon hash = " << iter.second->m_previous_hash.GetHex()
<< ", status = " << iter.second->StatusToString()
<< std::endl;
}

m_reinit_number_beacons = m_beacons_reinit.size();
Expand All @@ -366,6 +350,16 @@ class BeaconRegistryTest
// Create a copy of the referenced beacon object with a shared pointer to it and store.
m_local_historical_beacon_map_reinit[hash] = std::make_shared<GRC::Beacon>(*beacon_ptr);

std::cout << "init beacon db record: "
<< ", cpid = " << beacon_ptr->m_cpid.ToString()
<< ", public key = " << HexStr(beacon_ptr->m_public_key)
<< ", address = " << beacon_ptr->GetAddress().ToString()
<< ", timestamp = " << beacon_ptr->m_timestamp
<< ", hash = " << beacon_ptr->m_hash.GetHex()
<< ", prev beacon hash = " << beacon_ptr->m_previous_hash.GetHex()
<< ", status = " << beacon_ptr->StatusToString()
<< std::endl;

reinit_beacon_db_iter = reinit_beacon_db.advance(reinit_beacon_db_iter);
}
};
Expand Down

0 comments on commit 3f749b9

Please sign in to comment.