From 13c9aac6738166120ae9972c08316d0ba2fc46a9 Mon Sep 17 00:00:00 2001 From: Gerard097 Date: Wed, 30 Aug 2023 16:53:45 -0600 Subject: [PATCH] Mod genperiods to update all DAO's sharing calendar --- include/common.hpp | 3 + include/dao.hpp | 1 + include/period.hpp | 4 + src/dao.cpp | 91 ++++++++++++++++++--- src/period.cpp | 15 +++- src/proposals/assignment_proposal.cpp | 8 +- src/proposals/badge_assignment_proposal.cpp | 8 +- 7 files changed, 109 insertions(+), 21 deletions(-) diff --git a/include/common.hpp b/include/common.hpp index 7eef3155..5c0773e1 100644 --- a/include/common.hpp +++ b/include/common.hpp @@ -18,6 +18,9 @@ namespace hypha::common inline constexpr auto TESTNET_CONTRACT_NAME = name("mtdhoxhyphaa"); #endif + inline constexpr auto CALENDAR_WEEK = "weekly"; + inline constexpr auto CALENDAR_LUNAR = "lunar"; + inline constexpr auto PEG_TOKEN = "peg_token"; inline constexpr auto VOICE_TOKEN = "voice_token"; inline constexpr auto REWARD_TOKEN = "reward_token"; diff --git a/include/dao.hpp b/include/dao.hpp index 56921563..19da407b 100644 --- a/include/dao.hpp +++ b/include/dao.hpp @@ -160,6 +160,7 @@ namespace pricing { //ACTION remsetting(const string &key); ACTION genperiods(uint64_t dao_id, int64_t period_count/*, int64_t period_duration_sec*/); + ACTION initperiods(uint64_t dao_id, int64_t next_period); ACTION claimnextper(uint64_t assignment_id); // ACTION simclaimall(name account, uint64_t dao_id, bool only_ids); diff --git a/include/period.hpp b/include/period.hpp index 25a0605d..766b7729 100644 --- a/include/period.hpp +++ b/include/period.hpp @@ -4,6 +4,8 @@ #include +#include + namespace hypha { class dao; @@ -36,6 +38,8 @@ namespace hypha Period next(); + std::optional nextOpt(); + Period getNthPeriodAfter(int64_t count) const; int64_t getPeriodCountTo(Period& other); Period getPeriodUntil(eosio::time_point moment); diff --git a/src/dao.cpp b/src/dao.cpp index eef7f622..10eb3f97 100644 --- a/src/dao.cpp +++ b/src/dao.cpp @@ -1811,18 +1811,27 @@ void dao::createdao(ContentGroups& config) else { readDaoSettings(daoDoc.getID(), dao, configCW, false); } + + // Create start & end edges + // Just point to the initial period of the selected calendar + std::string_view calendar = common::CALENDAR_WEEK; + + if (auto [_, calendarType] = configCW.get(DETAILS, "calendary_type"); calendarType){ + calendar = calendarType->getAs(); + } - //Create start period - Period newPeriod(this, eosio::current_time_point(), to_str(dao, " start period")); - - //Create start & end edges - Edge(get_self(), get_self(), daoDoc.getID(), newPeriod.getID(), common::START); - Edge(get_self(), get_self(), daoDoc.getID(), newPeriod.getID(), common::END); - Edge(get_self(), get_self(), daoDoc.getID(), newPeriod.getID(), common::PERIOD); - Edge(get_self(), get_self(), newPeriod.getID(), daoDoc.getID(), common::DAO); + EOS_CHECK( + calendar == common::CALENDAR_WEEK || + calendar == common::CALENDAR_LUNAR, + to_str("Unkonw calendar type: ", calendar) + ); + + uint64_t startPeriodId = Edge::get(get_self(), getRootID(), name(calendar)).getToNode(); + + Edge(get_self(), get_self(), daoDoc.getID(), startPeriodId, common::START); #ifdef USE_TREASURY - //Create Treasury + // Create Treasury eosio::action( eosio::permission_level{ get_self(), name("active") }, get_self(), @@ -1842,6 +1851,15 @@ void dao::createdao(ContentGroups& config) ) ).send(); + eosio::action( + eosio::permission_level{ get_self(), name("active") }, + get_self(), + name("initperiods"), + std::make_tuple( + daoDoc.getID(), startPeriodId + ) + ).send(); + auto onboarder = getSettingsDocument(daoDoc.getID())->getOrFail(common::ONBOARDER_ACCOUNT); //Init core members should happen after scheduling setupdefs action @@ -2775,7 +2793,7 @@ void dao::checkAdminsAuth(uint64_t dao_id) void dao::genPeriods(uint64_t dao_id, int64_t period_count/*, int64_t period_duration_sec*/) { //Max number of periods that should be created in one call - const int64_t MAX_PERIODS_PER_CALL = 30; + const int64_t MAX_PERIODS_PER_CALL = 10; //Get last period //Document daoDoc(get_self(), dao_id); @@ -2787,6 +2805,9 @@ void dao::genPeriods(uint64_t dao_id, int64_t period_count/*, int64_t period_dur name daoName = settings->getOrFail(DAO_NAME); auto lastEdge = Edge::get(get_self(), dao_id, common::END); + + auto updateDaos = getGraph().getEdgesFrom(lastEdge.getToNode(), common::DAO); + lastEdge.erase(); uint64_t lastPeriodID = lastEdge.getToNode(); @@ -2805,7 +2826,11 @@ void dao::genPeriods(uint64_t dao_id, int64_t period_count/*, int64_t period_dur ); Edge(get_self(), get_self(), lastPeriodID, nextPeriod.getID(), common::NEXT); - Edge(get_self(), get_self(), dao_id, nextPeriod.getID(), common::PERIOD); + + for (auto& updateDao : updateDaos) { + Edge(get_self(), get_self(), updateDao.getToNode(), nextPeriod.getID(), common::PERIOD); + } + Edge(get_self(), get_self(), nextPeriod.getID(), dao_id, common::DAO); lastPeriodStartSecs = nextPeriodStart.sec_since_epoch(); @@ -2814,6 +2839,13 @@ void dao::genPeriods(uint64_t dao_id, int64_t period_count/*, int64_t period_dur Edge(get_self(), get_self(), dao_id, lastPeriodID, common::END); + for (auto& updateDao : updateDaos) { + if (updateDao.getToNode() != dao_id) { + Edge(get_self(), get_self(), lastPeriodID, updateDao.getToNode(), common::DAO); + updateDao.erase(); + } + } + //Check if there are more periods to created if (period_count > MAX_PERIODS_PER_CALL) { @@ -2849,6 +2881,43 @@ void dao::createVoiceToken(const eosio::name& daoName, ).send(); } +void dao::initperiods(uint64_t dao_id, int64_t next_period) +{ + verifyDaoType(dao_id); + + checkAdminsAuth(dao_id); + + auto period = std::make_optional(this, next_period); + + const size_t MAX_PERIODS_PER_CALL = 15; + + int it = 0; + + while (period && it++ < MAX_PERIODS_PER_CALL) { + Edge(get_self(), get_self(), dao_id, period->getID(), common::PERIOD); + + auto prev = std::move(period); + + //If next period is empty, then we reached the end of the period list + if (!(period = prev->next())) { + //Only the last period will create a reference to the DAO + Edge(get_self(), get_self(), prev->getID(), dao_id, common::DAO); + } + } + + //We didn't finish + if (period) { + eosio::action( + eosio::permission_level{ get_self(), name("active") }, + get_self(), + name("initperiods"), + std::make_tuple( + dao_id, period->getID() + ) + ).send(); + } +} + void dao::createToken(const std::string& contractType, name issuer, const asset& token) { auto dhoSettings = getSettingsDocument(); diff --git a/src/period.cpp b/src/period.cpp index d610fbc0..b0b5a710 100644 --- a/src/period.cpp +++ b/src/period.cpp @@ -137,9 +137,20 @@ namespace hypha Period Period::next() { TRACE_FUNCTION() + auto nextPeriod = nextOpt(); + EOS_CHECK(nextPeriod, "End of calendar has been reached. Contact administrator to add more time periods."); + return *nextPeriod; + } + + std::optional Period::nextOpt() + { auto [exists, period] = Edge::getIfExists(m_dao->get_self(), getID (), common::NEXT); - EOS_CHECK(exists, "End of calendar has been reached. Contact administrator to add more time periods."); - return Period(m_dao, period.getToNode()); + + if (exists) { + return std::make_optional(m_dao, period.getToNode()); + } + + return std::nullopt; } Period Period::getNthPeriodAfter(int64_t count) const diff --git a/src/proposals/assignment_proposal.cpp b/src/proposals/assignment_proposal.cpp index 6f198202..cae88ea3 100644 --- a/src/proposals/assignment_proposal.cpp +++ b/src/proposals/assignment_proposal.cpp @@ -126,10 +126,10 @@ namespace hypha // ) //TODO Period: Remove since period refactor will no longer point to DAO - EOS_CHECK( - Edge::exists(m_dao.get_self(), period.getID(), m_daoID, common::DAO), - "Period must belong to the DAO" - ); + // EOS_CHECK( + // Edge::exists(m_dao.get_self(), period.getID(), m_daoID, common::DAO), + // "Period must belong to the DAO" + // ); } else { // default START_PERIOD to next period ContentWrapper::insertOrReplace( diff --git a/src/proposals/badge_assignment_proposal.cpp b/src/proposals/badge_assignment_proposal.cpp index 0d33c667..bb4a13e4 100644 --- a/src/proposals/badge_assignment_proposal.cpp +++ b/src/proposals/badge_assignment_proposal.cpp @@ -195,10 +195,10 @@ namespace hypha ); //TODO Period: Remove since period refactor will no longer point to DAO - EOS_CHECK( - Edge::exists(m_dao.get_self(), period.getID(), m_daoID, common::DAO), - "Period must belong to the DAO" - ); + // EOS_CHECK( + // Edge::exists(m_dao.get_self(), period.getID(), m_daoID, common::DAO), + // "Period must belong to the DAO" + // ); } else { // default START_PERIOD to next period