Skip to content

Commit

Permalink
feat(contracts): RAM optimization (#815)
Browse files Browse the repository at this point in the history
* feat(contracts): add freeupram action

* feat(contracts): add freeupram action

* fix(contracts): fix missing stats updates for rateproducer scope

* fix(contracts): update wasm file

* fix(contracts): add missing emplace to update_stats_migration
  • Loading branch information
leisterfrancisco authored Oct 8, 2021
1 parent b892b97 commit 59db27e
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 33 deletions.
12 changes: 10 additions & 2 deletions contracts/rateproducer/include/rateproducer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -603,14 +603,21 @@ namespace eoscostarica {
* @param bp - Block Producer account name
*
*/
void update_stats_migration(name scope, name user, name bp);
void update_stats_migration(name bp);

/**
*
* Update the current logic to newest
*
*/
void migrate();

/**
*
* Liberate the ram used on ratings table under rateproducer and eden scope
*
*/
void freeupram();
};

EOSIO_ACTIONS(rateproducer,
Expand All @@ -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
10 changes: 10 additions & 0 deletions contracts/rateproducer/rateproducer.abi
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@
"base": "",
"fields": []
},
{
"name": "freeupram",
"base": "",
"fields": []
},
{
"name": "ratings_v2",
"base": "",
Expand Down Expand Up @@ -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": [
Expand Down
Binary file modified contracts/rateproducer/rateproducer.wasm
Binary file not shown.
6 changes: 6 additions & 0 deletions contracts/rateproducer/ricardian/rateproducer-ricardian.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.)";
Expand Down
100 changes: 69 additions & 31 deletions contracts/rateproducer/src/rateproducer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint128_t>(user.value) << 64) | bp.value;

auto uniq_rating_index = _ratings.get_index<name("uniqrating")>();
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,
Expand All @@ -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;
Expand All @@ -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


Expand Down

0 comments on commit 59db27e

Please sign in to comment.