Skip to content

Commit

Permalink
force: Add cleartasks action to clear RAM for expired tasks
Browse files Browse the repository at this point in the history
Submissions can be deleted if the batch has been deleted. This code
still needs testing. See issue #94.
  • Loading branch information
jeisses committed Jan 8, 2023
1 parent fd8ca09 commit 640f720
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
23 changes: 23 additions & 0 deletions contracts/force/force.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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");
Expand Down
21 changes: 15 additions & 6 deletions contracts/force/force.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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, uint128_t, &batchjoin::by_account_batch>>> batchjoin_table;

typedef multi_index<"submission"_n, submission,
indexed_by<"leaf"_n, const_mem_fun<submission, checksum256, &submission::by_leaf>>,
indexed_by<"batch"_n, const_mem_fun<submission, uint64_t, &submission::by_batch>>>
typedef multi_index<
"batchjoin"_n,
batchjoin,
indexed_by<"accbatch"_n,
const_mem_fun<batchjoin, uint128_t, &batchjoin::by_account_batch>>>
batchjoin_table;

typedef multi_index<
"submission"_n, submission,
indexed_by<"leaf"_n, const_mem_fun<submission, checksum256, &submission::by_leaf>>,
indexed_by<"batch"_n, const_mem_fun<submission, uint64_t, &submission::by_batch>>>
submission_table;

typedef multi_index<"payment"_n, payment,
Expand Down

0 comments on commit 640f720

Please sign in to comment.