Skip to content

Commit c29b18c

Browse files
authored
Fix the validation for existing checks (#27)
* Fix the validation for existing checks * adjust variable usage * Remove complexity out of the code * Remove 256 chars restriction for description fields * Adjust contract
1 parent 40401f6 commit c29b18c

File tree

2 files changed

+32
-62
lines changed

2 files changed

+32
-62
lines changed

community/community.cpp

Lines changed: 32 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ void cambiatus::create(eosio::asset cmm_asset, eosio::name creator, std::string
4141

4242
// Validates string fields
4343
eosio::check(name.size() <= 256, "name has more than 256 bytes");
44-
eosio::check(description.size() <= 256, "description has more than 256 bytes");
4544
eosio::check(logo.size() <= 256, "logo has more than 256 bytes");
4645

4746
// Check if community was created before
@@ -56,7 +55,7 @@ void cambiatus::create(eosio::asset cmm_asset, eosio::name creator, std::string
5655
c.creator = creator;
5756
c.logo = logo;
5857
c.name = name;
59-
c.description = description;
58+
c.description = description.substr(0, 255);
6059
c.inviter_reward = inviter_reward;
6160
c.invited_reward = invited_reward;
6261
c.has_objectives = has_objectives;
@@ -88,12 +87,11 @@ void cambiatus::update(eosio::asset cmm_asset, std::string logo, std::string nam
8887
// Validates string fields
8988
eosio::check(logo.size() <= 256, "logo has more than 256 bytes");
9089
eosio::check(name.size() <= 256, "name has more than 256 bytes");
91-
eosio::check(description.size() <= 256, "description has more than 256 bytes");
9290

9391
community.modify(cmm, _self, [&](auto &row) {
9492
row.logo = logo;
9593
row.name = name;
96-
row.description = description;
94+
row.description = description.substr(0, 255);
9795
row.inviter_reward = inviter_reward;
9896
row.invited_reward = invited_reward;
9997
row.has_objectives = has_objectives;
@@ -201,7 +199,6 @@ void cambiatus::newobjective(eosio::asset cmm_asset, std::string description, eo
201199

202200
eosio::symbol community_symbol = cmm_asset.symbol;
203201
eosio::check(community_symbol.is_valid(), "Invalid symbol name for community");
204-
eosio::check(description.length() <= 256, "Invalid length for description, must be less than 256 characters");
205202

206203
// Check if community exists
207204
communities community(_self, _self.value);
@@ -219,7 +216,7 @@ void cambiatus::newobjective(eosio::asset cmm_asset, std::string description, eo
219216
objectives objective(_self, _self.value);
220217
objective.emplace(_self, [&](auto &o) {
221218
o.id = get_available_id("objectives");
222-
o.description = description;
219+
o.description = description.substr(0, 255);
223220
o.community = community_symbol;
224221
o.creator = creator;
225222
});
@@ -229,8 +226,6 @@ void cambiatus::updobjective(std::uint64_t objective_id, std::string description
229226
{
230227
require_auth(editor);
231228

232-
eosio::check(description.length() <= 256, "Invalid length for description, must be less than 256 characters");
233-
234229
// Find objective
235230
objectives objective(_self, _self.value);
236231
const auto &found_objective = objective.get(objective_id, "Can't find objective with given ID");
@@ -251,7 +246,7 @@ void cambiatus::updobjective(std::uint64_t objective_id, std::string description
251246
eosio::check(found_objective.creator == editor || cmm.creator == editor, "You must be either the creator of the objective or the community creator to edit");
252247

253248
objective.modify(found_objective, _self, [&](auto &row) {
254-
row.description = description;
249+
row.description = description.substr(0, 255);
255250
});
256251
}
257252

@@ -298,9 +293,6 @@ void cambiatus::upsertaction(std::uint64_t action_id, std::uint64_t objective_id
298293
eosio::check(verifier_reward.amount >= 0, "verifier reward must be greater than or equal to 0");
299294
eosio::check(verifier_reward.symbol == obj.community, "verifier_reward must be a community token");
300295

301-
// Validate description
302-
eosio::check(description.length() <= 256, "Invalid length for description, must be less or equal than 256 chars");
303-
304296
// Validate deadline
305297
if (deadline > 0)
306298
{
@@ -322,10 +314,6 @@ void cambiatus::upsertaction(std::uint64_t action_id, std::uint64_t objective_id
322314
eosio::check(verifications >= 3 && ((verifications & 1) != 0), "You need at least three validators and it must be an odd number");
323315
}
324316

325-
// Validate proofs parameters
326-
eosio::check(photo_proof_instructions.length() <= 256,
327-
"Invalid length for photo proof instructions, must be less or equal than 256 chars");
328-
329317
// ========================================= End validation, start upsert
330318

331319
// Find action
@@ -340,7 +328,7 @@ void cambiatus::upsertaction(std::uint64_t action_id, std::uint64_t objective_id
340328
action.emplace(_self, [&](auto &a) {
341329
a.id = action_id;
342330
a.objective_id = objective_id;
343-
a.description = description;
331+
a.description = description.substr(0, 255);
344332
a.reward = reward;
345333
a.verifier_reward = verifier_reward;
346334
a.deadline = deadline;
@@ -352,13 +340,13 @@ void cambiatus::upsertaction(std::uint64_t action_id, std::uint64_t objective_id
352340
a.creator = creator;
353341
a.has_proof_photo = has_proof_photo;
354342
a.has_proof_code = has_proof_code;
355-
a.photo_proof_instructions = photo_proof_instructions;
343+
a.photo_proof_instructions = photo_proof_instructions.substr(0, 255);
356344
});
357345
}
358346
else
359347
{
360348
action.modify(itr_act, _self, [&](auto &a) {
361-
a.description = description;
349+
a.description = description.substr(0, 255);
362350
a.reward = reward;
363351
a.verifier_reward = verifier_reward;
364352
a.deadline = deadline;
@@ -369,7 +357,7 @@ void cambiatus::upsertaction(std::uint64_t action_id, std::uint64_t objective_id
369357
a.is_completed = is_completed;
370358
a.has_proof_photo = has_proof_photo;
371359
a.has_proof_code = has_proof_code;
372-
a.photo_proof_instructions = photo_proof_instructions;
360+
a.photo_proof_instructions = photo_proof_instructions.substr(0, 255);
373361
});
374362
}
375363

@@ -647,27 +635,15 @@ void cambiatus::verifyclaim(std::uint64_t claim_id, eosio::name verifier, std::u
647635

648636
// Get check index
649637
checks check(_self, _self.value);
650-
651-
// Assert that verifier hasn't done this previously
652638
auto check_by_claim = check.get_index<eosio::name{"byclaim"}>();
653-
auto itr_check_claim = check_by_claim.find(claim_id);
654-
uint64_t checks_count = 0;
655639

656-
// If has any checks
657-
if (itr_check_claim != check_by_claim.end())
640+
// Assert that verifier hasn't voted previously
641+
uint64_t checks_count = 0;
642+
for (auto itr_check_claim = check_by_claim.find(claim_id); itr_check_claim != check_by_claim.end(); itr_check_claim++)
658643
{
659-
for (; itr_check_claim != check_by_claim.end(); itr_check_claim++)
660-
{
661-
auto check_claim = *itr_check_claim;
662-
663-
// Increment counter if there is a vote already
664-
if (check_claim.validator == verifier)
665-
{
666-
checks_count++;
667-
}
668-
}
669-
670-
eosio::check(checks_count != 0, "The verifier cannot check the same claim more than once");
644+
auto check_claim = *itr_check_claim;
645+
bool existing_vote = check_claim.validator == verifier && check_claim.claim_id == claim_id;
646+
eosio::check(!existing_vote, "The verifier cannot check the same claim more than once");
671647
}
672648

673649
// Add new check
@@ -690,7 +666,6 @@ void cambiatus::verifyclaim(std::uint64_t claim_id, eosio::name verifier, std::u
690666
verification_reward.send();
691667
}
692668

693-
// We always update the claim status, at each vote
694669
// In order to know if its approved or rejected, we will have to count all existing checks, to see if we already have all needed
695670
// Just check `objact.verifications <= check counter`
696671

@@ -703,18 +678,18 @@ void cambiatus::verifyclaim(std::uint64_t claim_id, eosio::name verifier, std::u
703678
// At every vote we will have to update the claim status
704679
std::uint64_t positive_votes = 0;
705680
std::uint64_t negative_votes = 0;
706-
auto itr_check = check_by_claim.find(claim.id);
707-
for (; itr_check != check_by_claim.end();)
681+
for (auto itr_check = check_by_claim.find(claim_id); itr_check != check_by_claim.end(); itr_check++)
708682
{
709683
if ((*itr_check).is_verified == 1)
710684
{
711685
positive_votes++;
686+
eosio::print("\nFound a positive vote from: ", (*itr_check).validator);
712687
}
713688
else
714689
{
715690
negative_votes++;
691+
eosio::print("\nFound a negative vote from: ", (*itr_check).validator);
716692
}
717-
itr_check++;
718693
}
719694

720695
std::string status = "pending";
@@ -730,35 +705,30 @@ void cambiatus::verifyclaim(std::uint64_t claim_id, eosio::name verifier, std::u
730705
}
731706
}
732707

708+
eosio::print("\nFinal status of the claim is: ", status);
733709
claim_table.modify(itr_clm, _self, [&](auto &c) {
734710
c.status = status;
735711
});
736712

737-
if (status == "approved")
713+
if (status == "approved" && objact.reward.amount > 0)
738714
{
739-
if (objact.reward.amount > 0)
740-
{
741-
// Send reward
742-
std::string memo_action = "Thanks for doing an action for your community";
743-
eosio::action reward_action = eosio::action(eosio::permission_level{currency_account, eosio::name{"active"}}, // Permission
744-
currency_account, // Account
745-
eosio::name{"issue"}, // Action
746-
// to, quantity, memo
747-
std::make_tuple(claim.claimer, objact.reward, memo_action));
748-
reward_action.send();
749-
}
715+
// Send reward
716+
std::string memo_action = "Thanks for doing an action for your community";
717+
eosio::action reward_action = eosio::action(eosio::permission_level{currency_account, eosio::name{"active"}}, // Permission
718+
currency_account, // Account
719+
eosio::name{"issue"}, // Action
720+
// to, quantity, memo
721+
std::make_tuple(claim.claimer, objact.reward, memo_action));
722+
reward_action.send();
750723
}
751724

752725
// Check if action can be completed. Current claim must be either "approved" or "rejected"
753-
if (status != "pending")
726+
if (status != "pending" && !objact.is_completed && objact.usages > 0)
754727
{
755-
if (!objact.is_completed && objact.usages > 0)
756-
{
757-
action.modify(itr_objact, _self, [&](auto &a) {
758-
a.usages_left = objact.usages_left - 1;
759-
a.is_completed = (objact.usages_left - 1) == 0 ? 0 : 1;
760-
});
761-
}
728+
action.modify(itr_objact, _self, [&](auto &a) {
729+
a.usages_left = objact.usages_left - 1;
730+
a.is_completed = (objact.usages_left - 1) == 0 ? 0 : 1;
731+
});
762732
}
763733
}
764734

community/community.wasm

471 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)