From b216738e41bd0e600d59e9b5e74cb2d842b5dd5b Mon Sep 17 00:00:00 2001 From: NIK Date: Wed, 24 Jan 2024 20:56:06 +0800 Subject: [PATCH 1/3] code cleanup --- include/dao.hpp | 40 ++++++++++++++++ src/dao.cpp | 81 +++++++++++++++++++++++++++++++++ src/proposals/proposal.cpp | 2 + src/recurring_activity.cpp | 2 + src/upvote_election/actions.cpp | 2 + 5 files changed, 127 insertions(+) diff --git a/include/dao.hpp b/include/dao.hpp index cc31de0..8a2d517 100644 --- a/include/dao.hpp +++ b/include/dao.hpp @@ -118,6 +118,36 @@ namespace pricing { eosio::indexed_by>, eosio::indexed_by>> payment_table; + + // deferred actions table + + TABLE deferred_actions_table { + uint64_t id; + eosio::time_point_sec execute_time; + std::vector auth; + name account; + name action_name; + std::vector data; + + uint64_t primary_key() const { return id; } + uint64_t by_execute_time() const { return execute_time.sec_since_epoch(); } + + }; + typedef multi_index<"defactions"_n, deferred_actions_table, + eosio::indexed_by<"bytime"_n, eosio::const_mem_fun> + > deferred_actions_tables; + + /* + // deferred actions test - remove + TABLE testdtrx_table { + uint64_t id; + uint64_t number; + std::string text; + + uint64_t primary_key() const { return id; } + }; + typedef multi_index<"testdtrx"_n, testdtrx_table> testdtrx_tables; + */ ACTION assigntokdao(asset token, uint64_t dao_id, bool force); @@ -203,6 +233,14 @@ namespace pricing { ACTION reset(); // debugging - maybe with the dev flags + ACTION executenext(); // execute stored deferred actions + + /* + // Actions for testing deferred transactions - only for unit tests + ACTION addtest(eosio::time_point_sec execute_time, uint64_t number, std::string text); + ACTION testdtrx(uint64_t number, std::string text); + */ + #ifdef DEVELOP_BUILD_HELPERS struct InputEdge { @@ -534,6 +572,8 @@ namespace pricing { void initSysBadges(); void createSystemBadge(name badge_edge, string label, string icon); + void schedule_deferred_action(eosio::time_point_sec execute_time, eosio::action action); + template std::optional getNameID(const name& n) const diff --git a/src/dao.cpp b/src/dao.cpp index 0047962..75dc2ed 100644 --- a/src/dao.cpp +++ b/src/dao.cpp @@ -2785,6 +2785,9 @@ void dao::activatebdg(uint64_t assign_badge_id) else { //Let's reschedule then //Schedule a trx to close the proposal + + // MARK DEFERRED + eosio::transaction trx; trx.actions.emplace_back(eosio::action( eosio::permission_level(get_self(), eosio::name("active")), @@ -3512,6 +3515,7 @@ void dao::reset() { require_auth(_self); +/* delete_table(get_self(), 0); delete_table(get_self(), 1); delete_table(get_self(), 2); @@ -3522,8 +3526,85 @@ void dao::reset() { delete_table(get_self(), get_self().value); delete_table(get_self(), get_self().value); delete_table(get_self(), get_self().value); + delete_table(get_self(), get_self().value); + delete_table(get_self(), get_self().value); +*/ + +} + +// Action to choose and execute the next action +// Note: anybody can call this - fails if there is no action to execute. +void dao::executenext() { + + deferred_actions_tables deftrx(get_self(), get_self().value); + + auto idx = deftrx.get_index<"bytime"_n>(); + auto itr = idx.begin(); + if (itr != idx.end() && itr->execute_time <= eosio::current_time_point()) { + + // Note: We can't use the public constructor because it will misinterpret the + // data and pack it again - data is already in packed format. + eosio::action act; + act.account = itr->account; + act.name = itr->action_name; + act.authorization = itr->auth; + act.data = itr->data; + + act.send(); + idx.erase(itr); + } + else { + eosio::check(false, "No deferred actions to execute at this time."); + } +} + +// Add a new deferred transaction +void dao::schedule_deferred_action(eosio::time_point_sec execute_time, eosio::action action) { + + deferred_actions_tables deftrx(get_self(), get_self().value); + + deftrx.emplace(get_self(), [&](auto& row) { + row.id = deftrx.available_primary_key(); + row.execute_time = execute_time; + row.auth = action.authorization; + row.account = action.account; + row.action_name = action.name; + row.data = action.data; + }); } +/* +// Test methods for deferred transactions - delete +void dao::addtest(eosio::time_point_sec execute_time, uint64_t number, std::string text) { + require_auth(get_self()); + + // 1 - Create an action object + eosio::action act( + eosio::permission_level(get_self(), eosio::name("active")), + get_self(), + eosio::name("testdtrx"), + std::make_tuple(number, text) + ); + + /// 2 - Schedule the action + schedule_deferred_action(execute_time, act); + +} + +void dao::testdtrx(uint64_t number, std::string text) { + require_auth(get_self()); + + testdtrx_tables testdtrx(get_self(), get_self().value); + + // Add the new entry to the testdtrx table + testdtrx.emplace(get_self(), [&](auto& row) { + row.id = testdtrx.available_primary_key(); + row.number = number; + row.text = text; + }); +} +*/ + } // namespace hypha diff --git a/src/proposals/proposal.cpp b/src/proposals/proposal.cpp index b940cf6..0d24775 100644 --- a/src/proposals/proposal.cpp +++ b/src/proposals/proposal.cpp @@ -522,6 +522,8 @@ namespace hypha proposal.update(); + // MARK DEFERRED + //Schedule a trx to close the proposal eosio::transaction trx; trx.actions.emplace_back(eosio::action( diff --git a/src/recurring_activity.cpp b/src/recurring_activity.cpp index e9fa398..85af15e 100644 --- a/src/recurring_activity.cpp +++ b/src/recurring_activity.cpp @@ -84,6 +84,8 @@ void RecurringActivity::scheduleArchive() return; } + // MARK DEFERRED + //Schedule a trx to close the proposal eosio::transaction trx; trx.actions.emplace_back(eosio::action( diff --git a/src/upvote_election/actions.cpp b/src/upvote_election/actions.cpp index b5fb30f..d6c8465 100644 --- a/src/upvote_election/actions.cpp +++ b/src/upvote_election/actions.cpp @@ -357,6 +357,8 @@ namespace hypha { { if (date < eosio::current_time_point()) return; + // MARK DEFERRED + //Schedule a trx to close the proposal eosio::transaction trx; trx.actions.emplace_back(eosio::action( From 3cafe5fd160ed2ecc8377832d683839da6f12c15 Mon Sep 17 00:00:00 2001 From: NIK Date: Wed, 24 Jan 2024 21:17:06 +0800 Subject: [PATCH 2/3] changing deferred actions to new system --- include/dao.hpp | 3 +-- src/dao.cpp | 21 ++++----------------- src/proposals/proposal.cpp | 15 +++------------ src/recurring_activity.cpp | 21 ++++----------------- src/upvote_election/actions.cpp | 21 +++++---------------- 5 files changed, 17 insertions(+), 64 deletions(-) diff --git a/include/dao.hpp b/include/dao.hpp index 8a2d517..4687a94 100644 --- a/include/dao.hpp +++ b/include/dao.hpp @@ -541,6 +541,7 @@ namespace pricing { } + void schedule_deferred_action(eosio::time_point_sec execute_time, eosio::action action); private: @@ -572,8 +573,6 @@ namespace pricing { void initSysBadges(); void createSystemBadge(name badge_edge, string label, string icon); - void schedule_deferred_action(eosio::time_point_sec execute_time, eosio::action action); - template std::optional getNameID(const name& n) const diff --git a/src/dao.cpp b/src/dao.cpp index 75dc2ed..ef33847 100644 --- a/src/dao.cpp +++ b/src/dao.cpp @@ -2786,28 +2786,15 @@ void dao::activatebdg(uint64_t assign_badge_id) //Let's reschedule then //Schedule a trx to close the proposal - // MARK DEFERRED - - eosio::transaction trx; - trx.actions.emplace_back(eosio::action( + eosio::action act( eosio::permission_level(get_self(), eosio::name("active")), get_self(), eosio::name("activatebdg"), std::make_tuple(badgeAssing.getID()) - )); - - auto activationTime = startTime.sec_since_epoch(); - - constexpr auto aditionalDelaySec = 60; - trx.delay_sec = (activationTime - now.sec_since_epoch()) + aditionalDelaySec; - - auto dhoSettings = getSettingsDocument(); - - auto nextID = dhoSettings->getSettingOrDefault("next_schedule_id", int64_t(0)); - - trx.send(nextID, get_self()); + ); + + schedule_deferred_action(startTime + eosio::seconds(4), act); - dhoSettings->setSetting(Content{"next_schedule_id", nextID + 1}); } } diff --git a/src/proposals/proposal.cpp b/src/proposals/proposal.cpp index 0d24775..c878e29 100644 --- a/src/proposals/proposal.cpp +++ b/src/proposals/proposal.cpp @@ -521,28 +521,19 @@ namespace hypha publishImpl(proposal); proposal.update(); - - // MARK DEFERRED //Schedule a trx to close the proposal - eosio::transaction trx; - trx.actions.emplace_back(eosio::action( + eosio::action act( permission_level(m_dao.get_self(), eosio::name("active")), m_dao.get_self(), eosio::name("closedocprop"), std::make_tuple(proposal.getID()) - )); + ); auto expiration = proposal.getContentWrapper().getOrFail(BALLOT, EXPIRATION_LABEL, "Proposal has no expiration")->getAs(); - constexpr auto aditionalDelaySec = 60; - trx.delay_sec = (expiration.sec_since_epoch() - eosio::current_time_point().sec_since_epoch()) + aditionalDelaySec; - - auto nextID = m_dhoSettings->getSettingOrDefault("next_schedule_id", int64_t(0)); - - trx.send(util::hashCombine(nextID, proposal.getID()), m_dao.get_self()); + m_dao.schedule_deferred_action(expiration + eosio::seconds(4), act); - m_dhoSettings->setSetting(Content{"next_schedule_id", nextID + 1}); } std::optional Proposal::getItemDocOpt(const char* docItem, const name& docType, ContentWrapper &contentWrapper) diff --git a/src/recurring_activity.cpp b/src/recurring_activity.cpp index 85af15e..17ed0f6 100644 --- a/src/recurring_activity.cpp +++ b/src/recurring_activity.cpp @@ -83,30 +83,17 @@ void RecurringActivity::scheduleArchive() if (isInfinite()) { return; } - - // MARK DEFERRED - + //Schedule a trx to close the proposal - eosio::transaction trx; - trx.actions.emplace_back(eosio::action( + eosio::action act( eosio::permission_level(m_dao->get_self(), eosio::name("active")), m_dao->get_self(), eosio::name("archiverecur"), std::make_tuple(getID()) - )); - - auto expiration = getEndDate().sec_since_epoch(); - - constexpr auto aditionalDelaySec = 60; - trx.delay_sec = (expiration - eosio::current_time_point().sec_since_epoch()) + aditionalDelaySec; - - auto dhoSettings = m_dao->getSettingsDocument(); - - auto nextID = dhoSettings->getSettingOrDefault("next_schedule_id", int64_t(0)); + ); - trx.send(nextID, m_dao->get_self()); + m_dao->schedule_deferred_action(getEndDate() + eosio::seconds(4), act); - dhoSettings->setSetting(Content{"next_schedule_id", nextID + 1}); } eosio::time_point RecurringActivity::getStartDate() diff --git a/src/upvote_election/actions.cpp b/src/upvote_election/actions.cpp index d6c8465..e8e350c 100644 --- a/src/upvote_election/actions.cpp +++ b/src/upvote_election/actions.cpp @@ -356,33 +356,22 @@ namespace hypha { static void scheduleElectionUpdate(dao& dao, UpvoteElection& election, time_point date) { if (date < eosio::current_time_point()) return; - - // MARK DEFERRED //Schedule a trx to close the proposal - eosio::transaction trx; - trx.actions.emplace_back(eosio::action( + eosio::action act( eosio::permission_level(dao.get_self(), eosio::name("active")), dao.get_self(), eosio::name("updateupvelc"), std::make_tuple(election.getId(), true, false) - )); + ); + + dao.schedule_deferred_action(date + eosio::seconds(4), act); EOS_CHECK( date > eosio::current_time_point(), "Can only schedule for dates in the future" ); - - constexpr auto aditionalDelaySec = 10; - trx.delay_sec = (date - eosio::current_time_point()).to_seconds() + aditionalDelaySec; - - auto dhoSettings = dao.getSettingsDocument(); - - auto nextID = dhoSettings->getSettingOrDefault("next_schedule_id", int64_t(0)); - - trx.send(nextID, dao.get_self()); - - dhoSettings->setSetting(Content{ "next_schedule_id", nextID + 1 }); + } static void assignDelegateBadges( From f0db0dfae3b2578e7394f9f619660ed8e3160535 Mon Sep 17 00:00:00 2001 From: NIK Date: Tue, 30 Jan 2024 20:48:02 +0800 Subject: [PATCH 3/3] adding test for deferred transactions back in for testnet deployment --- deploy_testnet.sh => deploy_telos_testnet.sh | 0 include/dao.hpp | 6 ++---- src/dao.cpp | 4 ++-- 3 files changed, 4 insertions(+), 6 deletions(-) rename deploy_testnet.sh => deploy_telos_testnet.sh (100%) diff --git a/deploy_testnet.sh b/deploy_telos_testnet.sh similarity index 100% rename from deploy_testnet.sh rename to deploy_telos_testnet.sh diff --git a/include/dao.hpp b/include/dao.hpp index 4687a94..63d8aa5 100644 --- a/include/dao.hpp +++ b/include/dao.hpp @@ -137,7 +137,7 @@ namespace pricing { eosio::indexed_by<"bytime"_n, eosio::const_mem_fun> > deferred_actions_tables; - /* + // deferred actions test - remove TABLE testdtrx_table { uint64_t id; @@ -147,7 +147,7 @@ namespace pricing { uint64_t primary_key() const { return id; } }; typedef multi_index<"testdtrx"_n, testdtrx_table> testdtrx_tables; - */ + ACTION assigntokdao(asset token, uint64_t dao_id, bool force); @@ -235,11 +235,9 @@ namespace pricing { ACTION executenext(); // execute stored deferred actions - /* // Actions for testing deferred transactions - only for unit tests ACTION addtest(eosio::time_point_sec execute_time, uint64_t number, std::string text); ACTION testdtrx(uint64_t number, std::string text); - */ #ifdef DEVELOP_BUILD_HELPERS diff --git a/src/dao.cpp b/src/dao.cpp index ef33847..f1b45b0 100644 --- a/src/dao.cpp +++ b/src/dao.cpp @@ -3561,7 +3561,7 @@ void dao::schedule_deferred_action(eosio::time_point_sec execute_time, eosio::ac }); } -/* + // Test methods for deferred transactions - delete void dao::addtest(eosio::time_point_sec execute_time, uint64_t number, std::string text) { require_auth(get_self()); @@ -3591,7 +3591,7 @@ void dao::testdtrx(uint64_t number, std::string text) { row.text = text; }); } -*/ + } // namespace hypha