From 640f720fad6de43b24e4f6e542de4d3d07049163 Mon Sep 17 00:00:00 2001 From: Jesse Eisses Date: Sun, 8 Jan 2023 15:17:36 +0100 Subject: [PATCH] force: Add `cleartasks` action to clear RAM for expired tasks Submissions can be deleted if the batch has been deleted. This code still needs testing. See issue #94. --- contracts/force/force.cpp | 23 +++++++++++++++++++++++ contracts/force/force.hpp | 21 +++++++++++++++------ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/contracts/force/force.cpp b/contracts/force/force.cpp index 73f7db1..7ec35e9 100644 --- a/contracts/force/force.cpp +++ b/contracts/force/force.cpp @@ -96,6 +96,7 @@ void force::rmbatch(uint32_t id, uint32_t campaign_id, vaccount::sig sig) { uint64_t batch_pk =(uint64_t{campaign_id} << 32) | id; auto& camp = camp_tbl.get(campaign_id, "campaign not found"); + auto batch_itr = batch_tbl.find(batch_pk); eosio::check(batch_itr != batch_tbl.end(), "batch does not exist"); @@ -108,6 +109,28 @@ void force::rmbatch(uint32_t id, uint32_t campaign_id, vaccount::sig sig) { batch_tbl.erase(batch_itr); } +void force::cleartasks(uint64_t batch_pk, vaccount::sig sig) { + // tasks can only be cleared if the batch is removed + batch_table batch_tbl(_self, _self.value); + auto batch_itr = batch_tbl.find(batch_pk); + eosio::check(batch_itr == batch_tbl.end(), "batch still exists"); + + // remove the submissoins in this batch + submission_table submission_tbl(_self, _self.value); + auto by_batch = submission_tbl.get_index<"batch"_n>(); + auto itr_start = by_batch.lower_bound(batch_pk); + auto itr_end = by_batch.upper_bound(batch_pk); + int erased = 0; + + for (; itr_start != itr_end; itr_start++) { + erased++; + auto& subm = *itr_start; + submission_tbl.erase(subm); + if (erased >= 100) + break; + } +} + void force::publishbatch(uint64_t batch_id, uint32_t num_tasks, vaccount::sig sig) { batch_table batch_tbl(_self, _self.value); auto& batch = batch_tbl.get(batch_id, "batch not found"); diff --git a/contracts/force/force.hpp b/contracts/force/force.hpp index 3fb1be3..335118b 100644 --- a/contracts/force/force.hpp +++ b/contracts/force/force.hpp @@ -94,6 +94,10 @@ class [[eosio::contract("force")]] force : public eosio::contract { uint32_t campaign_id, vaccount::sig sig); + [[eosio::action]] + void cleartasks(uint64_t batch_pk, + vaccount::sig sig); + [[eosio::action]] void closebatch(uint64_t batch_id, vaccount::vaddress owner, @@ -425,12 +429,17 @@ class [[eosio::contract("force")]] force : public eosio::contract { typedef multi_index<"campaign"_n, campaign> campaign_table; typedef multi_index<"batch"_n, batch> batch_table; - typedef multi_index<"batchjoin"_n, batchjoin, - indexed_by<"accbatch"_n, const_mem_fun>> batchjoin_table; - - typedef multi_index<"submission"_n, submission, - indexed_by<"leaf"_n, const_mem_fun>, - indexed_by<"batch"_n, const_mem_fun>> + typedef multi_index< + "batchjoin"_n, + batchjoin, + indexed_by<"accbatch"_n, + const_mem_fun>> + batchjoin_table; + + typedef multi_index< + "submission"_n, submission, + indexed_by<"leaf"_n, const_mem_fun>, + indexed_by<"batch"_n, const_mem_fun>> submission_table; typedef multi_index<"payment"_n, payment,