diff --git a/contracts/rateproducer/include/rateproducer.hpp b/contracts/rateproducer/include/rateproducer.hpp index 62d54b3c..b2d4a2bf 100644 --- a/contracts/rateproducer/include/rateproducer.hpp +++ b/contracts/rateproducer/include/rateproducer.hpp @@ -603,7 +603,7 @@ namespace eoscostarica { * @param bp - Block Producer account name * */ - void update_stats_migration(name scope, name user, name bp); + void update_stats_migration(name bp); /** * @@ -611,6 +611,13 @@ namespace eoscostarica { * */ void migrate(); + + /** + * + * Liberate the ram used on ratings table under rateproducer and eden scope + * + */ + void freeupram(); }; EOSIO_ACTIONS(rateproducer, @@ -620,6 +627,7 @@ namespace eoscostarica { action(wipe, ricardian_contract(wipe_ricardian)), action(rminactive, ricardian_contract(rminactive_ricardian)), action(rmrate, user, bp, ricardian_contract(rmrate_ricardian)), - action(migrate, ricardian_contract(migrate_ricardian))) + action(migrate, ricardian_contract(migrate_ricardian)), + action(freeupram, ricardian_contract(freeupram_ricardian))) } // namespace eoscostarica \ No newline at end of file diff --git a/contracts/rateproducer/rateproducer.abi b/contracts/rateproducer/rateproducer.abi index bb2a492e..cafb1af8 100644 --- a/contracts/rateproducer/rateproducer.abi +++ b/contracts/rateproducer/rateproducer.abi @@ -75,6 +75,11 @@ "base": "", "fields": [] }, + { + "name": "freeupram", + "base": "", + "fields": [] + }, { "name": "ratings_v2", "base": "", @@ -182,6 +187,11 @@ "name": "migrate", "type": "migrate", "ricardian_contract": "---\nspec_version: 0.1.0\ntitle: Update the current logic to newest\nsummary: The intent of the `{{ migrate }}` action is to update the current logic to newest.\n---" + }, + { + "name": "freeupram", + "type": "freeupram", + "ricardian_contract": "---\nspec_version: 0.1.0\ntitle: Free up RAM\nsummary: The intent of the `{{ freeupram }}` action is to remove all records from old ratings table including the rateproducer and eden scope.\n---" } ], "tables": [ diff --git a/contracts/rateproducer/rateproducer.wasm b/contracts/rateproducer/rateproducer.wasm index 58d82d75..f65a49c3 100755 Binary files a/contracts/rateproducer/rateproducer.wasm and b/contracts/rateproducer/rateproducer.wasm differ diff --git a/contracts/rateproducer/ricardian/rateproducer-ricardian.cpp b/contracts/rateproducer/ricardian/rateproducer-ricardian.cpp index a70f98d7..57b94d96 100644 --- a/contracts/rateproducer/ricardian/rateproducer-ricardian.cpp +++ b/contracts/rateproducer/ricardian/rateproducer-ricardian.cpp @@ -36,6 +36,12 @@ title: Update the current logic to newest summary: The intent of the `{{ migrate }}` action is to update the current logic to newest. ---)"; +const char* freeupram_ricardian = R"(--- +spec_version: 0.1.0 +title: Free up RAM +summary: The intent of the `{{ freeupram }}` action is to remove all records from old ratings table including the rateproducer and eden scope. +---)"; + // CLAUSES const char* datastorage_clause = R"(The rateproducer application values the security of personal data. We may process only minimal user data as much as it is necessary to maintain the rateproducer application running. We only receive and store any information you knowingly provide to us when you create an account, or fill using the app.)"; diff --git a/contracts/rateproducer/src/rateproducer.cpp b/contracts/rateproducer/src/rateproducer.cpp index dc119e5f..5466a683 100644 --- a/contracts/rateproducer/src/rateproducer.cpp +++ b/contracts/rateproducer/src/rateproducer.cpp @@ -481,26 +481,21 @@ namespace eoscostarica { &bp_average); } - void rateproducer::update_stats_migration(name scope, name user, name bp) { - ratings_table_v2 _ratings(_self, scope.value); - auto uniq_rating = (static_cast(user.value) << 64) | bp.value; - - auto uniq_rating_index = _ratings.get_index(); - auto existing_rating = uniq_rating_index.find(uniq_rating); + void rateproducer::update_stats_migration(name bp) { + name default_scope = _self; + name ram_payer = _self; - check( existing_rating != uniq_rating_index.end(), "Rating does not exist" ); - //update bp stats float bp_transparency = 0; float bp_infrastructure = 0; float bp_trustiness = 0; float bp_community = 0; float bp_development = 0; - uint32_t bp_ratings_cntr = 0; + uint32_t bp_ratings_cntr = 0; float bp_average = 0; //re-calculate stats for the bp - calculate_bp_stats (scope, + calculate_bp_stats (default_scope, bp, &bp_transparency, &bp_infrastructure, @@ -509,35 +504,56 @@ namespace eoscostarica { &bp_development, &bp_ratings_cntr, &bp_average); + + float totalStats = + bp_transparency + + bp_infrastructure + + bp_trustiness + + bp_community + + bp_development; - //save the re-calcualtes stats - name ram_payer = _self; - update_bp_stats (scope, - &ram_payer, - &bp, - &bp_transparency, - &bp_infrastructure, - &bp_trustiness, - &bp_community, - &bp_development, - &bp_ratings_cntr, - &bp_average); + name stats_ram_payer =_self; + stats_table _stats(_self, default_scope.value); + auto itr = _stats.find(bp.value); + if(itr != _stats.end()) { + if(totalStats) { + _stats.modify(itr, stats_ram_payer, [&]( auto& row ) { + row.transparency = bp_transparency; + row.infrastructure = bp_infrastructure; + row.trustiness = bp_trustiness; + row.development = bp_community; + row.community = bp_development; + row.ratings_cntr = bp_ratings_cntr; + row.average = bp_average; + }); + } else _stats.erase(itr); + } else { + if(totalStats) { + _stats.emplace(stats_ram_payer, [&]( auto& row ) { + row.bp = bp; + row.transparency = bp_transparency; + row.infrastructure = bp_infrastructure; + row.trustiness = bp_trustiness; + row.development = bp_community; + row.community = bp_development; + row.ratings_cntr = bp_ratings_cntr; + row.average = bp_average; + }); + } + } } void rateproducer::migrate() { config c = cfg.get_or_create(_self, config{.owner = _self, .version = 0}); require_auth(c.owner); - // assert we only run once // the comparison value needs to be hard-coded with each new migration eosio::check(c.version < 2, "Migration already ran"); - ratings_table _ratings_self(_self, _self.value); ratings_table_v2 _ratings_self_v2(_self, _self.value); ratings_table_v2 _ratings_eden_v2(_self, eden_scope.value); - for(auto itr = _ratings_self.begin(); itr != _ratings_self.end(); itr++) { - auto modify_rating = [&]( auto& row ) -> auto { + auto emplace_rating = [&]( auto& row ) -> auto { row.id = itr->id; row.user = itr->user; row.bp = itr->bp; @@ -547,16 +563,38 @@ namespace eoscostarica { row.community = itr->community; row.development = itr->development; }; - - name tempScope = is_eden(itr->user) ? eden_scope : _self; - if(tempScope.value == eden_scope.value) _ratings_eden_v2.emplace(_self, modify_rating); - else _ratings_self_v2.emplace(_self, modify_rating); - update_stats_migration(tempScope, itr->user, itr->bp); + + name temp_scope = is_eden(itr->user) ? eden_scope : _self; + if(temp_scope.value == eden_scope.value) _ratings_eden_v2.emplace(_self, emplace_rating); + else _ratings_self_v2.emplace(_self, emplace_rating); + update_stats_migration(itr->bp); } c.version++; cfg.set(c, c.owner); } + + void rateproducer::freeupram() { + config c = cfg.get_or_create(_self, config{.owner = _self, .version = 0}); + require_auth(_self); + + eosio::check(c.version < 3, "Make sure to run `migrate` action before run this action"); + + ratings_table _ratings_general(_self, _self.value); + auto general_itr = _ratings_general.begin(); + while (general_itr != _ratings_general.end()) { + general_itr = _ratings_general.erase(general_itr); + } + + ratings_table _ratings_eden(_self, eden_scope.value); + auto eden_itr = _ratings_eden.begin(); + while (eden_itr != _ratings_eden.end()) { + eden_itr = _ratings_eden.erase(eden_itr); + } + + c.version++; + cfg.set(c, c.owner); + } } // namespace eoscostarica