From 2ab0eaef2480ee8a7eac37024802ac53a46d6632 Mon Sep 17 00:00:00 2001 From: maslenitsa93 Date: Fri, 22 Feb 2019 14:22:59 +0300 Subject: [PATCH] Remove all authors and permlinks from worker objects #1129 --- libraries/chain/database_worker_objects.cpp | 38 +++---------- .../chain/include/golos/chain/database.hpp | 6 +- .../include/golos/chain/worker_objects.hpp | 28 +++------- libraries/chain/steem_evaluator.cpp | 2 +- libraries/chain/worker_evaluators.cpp | 56 ++++++++++--------- .../plugins/worker_api/worker_api_objects.hpp | 21 +++---- .../plugins/worker_api/worker_api_queries.hpp | 4 ++ plugins/worker_api/worker_api_plugin.cpp | 42 +++++++++++--- 8 files changed, 93 insertions(+), 104 deletions(-) diff --git a/libraries/chain/database_worker_objects.cpp b/libraries/chain/database_worker_objects.cpp index 4eef25e79d..7f515c9c50 100644 --- a/libraries/chain/database_worker_objects.cpp +++ b/libraries/chain/database_worker_objects.cpp @@ -28,36 +28,15 @@ namespace golos { namespace chain { return find(post); } - const worker_techspec_object& database::get_worker_result( - const account_name_type& author, - const std::string& permlink - ) const { try { - return get(std::make_tuple(author, permlink)); + const worker_techspec_object& database::get_worker_result(const comment_id_type& post) const { try { + return get(post); } catch (const std::out_of_range &e) { - GOLOS_THROW_MISSING_OBJECT("worker_techspec_object", fc::mutable_variant_object()("author",author)("worker_result_permlink",permlink)); - } FC_CAPTURE_AND_RETHROW((author)(permlink)) } - - const worker_techspec_object& database::get_worker_result( - const account_name_type& author, - const shared_string& permlink - ) const { try { - return get(std::make_tuple(author, permlink)); - } catch (const std::out_of_range &e) { - GOLOS_THROW_MISSING_OBJECT("worker_techspec_object", fc::mutable_variant_object()("author",author)("worker_result_permlink",permlink)); - } FC_CAPTURE_AND_RETHROW((author)(permlink)) } - - const worker_techspec_object* database::find_worker_result( - const account_name_type& author, - const std::string& permlink - ) const { - return find(std::make_tuple(author, permlink)); - } + const auto& comment = get_comment(post); + GOLOS_THROW_MISSING_OBJECT("worker_techspec_object", fc::mutable_variant_object()("author",comment.author)("worker_result_permlink",comment.permlink)); + } FC_CAPTURE_AND_RETHROW((post)) } - const worker_techspec_object* database::find_worker_result( - const account_name_type& author, - const shared_string& permlink - ) const { - return find(std::make_tuple(author, permlink)); + const worker_techspec_object* database::find_worker_result(const comment_id_type& post) const { + return find(post); } void database::process_worker_cashout() { @@ -87,8 +66,7 @@ namespace golos { namespace chain { } if (remaining_payments_count == 1) { - const auto& wpo_post = get_comment(wto_itr->worker_proposal_author, wto_itr->worker_proposal_permlink); - const auto& wpo = get_worker_proposal(wpo_post.id); + const auto& wpo = get_worker_proposal(wto_itr->worker_proposal_post); modify(wpo, [&](worker_proposal_object& wpo) { wpo.state = worker_proposal_state::closed; }); diff --git a/libraries/chain/include/golos/chain/database.hpp b/libraries/chain/include/golos/chain/database.hpp index aa3953e95f..d2c1924118 100644 --- a/libraries/chain/include/golos/chain/database.hpp +++ b/libraries/chain/include/golos/chain/database.hpp @@ -183,10 +183,8 @@ namespace golos { namespace chain { const worker_techspec_object& get_worker_techspec(const comment_id_type& post) const; const worker_techspec_object* find_worker_techspec(const comment_id_type& post) const; - const worker_techspec_object& get_worker_result(const account_name_type& author, const std::string& permlink) const; - const worker_techspec_object& get_worker_result(const account_name_type& author, const shared_string& permlink) const; - const worker_techspec_object* find_worker_result(const account_name_type& author, const std::string& permlink) const; - const worker_techspec_object* find_worker_result(const account_name_type& author, const shared_string& permlink) const; + const worker_techspec_object& get_worker_result(const comment_id_type& post) const; + const worker_techspec_object* find_worker_result(const comment_id_type& post) const; const comment_object &get_comment(const account_name_type &author, const shared_string &permlink) const; diff --git a/libraries/chain/include/golos/chain/worker_objects.hpp b/libraries/chain/include/golos/chain/worker_objects.hpp index 40ff216c68..a32da9a56c 100644 --- a/libraries/chain/include/golos/chain/worker_objects.hpp +++ b/libraries/chain/include/golos/chain/worker_objects.hpp @@ -18,8 +18,7 @@ namespace golos { namespace chain { worker_proposal_object() = delete; template - worker_proposal_object(Constructor&& c, allocator a) - : approved_techspec_permlink(a) { + worker_proposal_object(Constructor&& c, allocator a) { c(*this); }; @@ -29,8 +28,7 @@ namespace golos { namespace chain { comment_id_type post; worker_proposal_type type; worker_proposal_state state; - account_name_type approved_techspec_author; - shared_string approved_techspec_permlink; + comment_id_type approved_techspec_post; }; enum class worker_techspec_state { @@ -44,8 +42,7 @@ namespace golos { namespace chain { worker_techspec_object() = delete; template - worker_techspec_object(Constructor&& c, allocator a) - : worker_proposal_permlink(a), worker_result_permlink(a) { + worker_techspec_object(Constructor&& c, allocator a) { c(*this); }; @@ -53,8 +50,7 @@ namespace golos { namespace chain { account_name_type author; comment_id_type post; - account_name_type worker_proposal_author; - shared_string worker_proposal_permlink; + comment_id_type worker_proposal_post; worker_techspec_state state; time_point_sec created; time_point_sec modified; @@ -65,7 +61,7 @@ namespace golos { namespace chain { uint16_t disapproves = 0; account_name_type worker; time_point_sec work_beginning_time; - shared_string worker_result_permlink; + comment_id_type worker_result_post; time_point_sec completion_date; uint16_t payments_count; uint32_t payments_interval; @@ -141,22 +137,14 @@ namespace golos { namespace chain { tag, composite_key< worker_techspec_object, - member, - member, + member, member>, composite_key_compare< - std::less, - chainbase::strcmp_less, + std::less, std::less>>, ordered_unique< tag, - composite_key< - worker_techspec_object, - member, - member>, - composite_key_compare< - std::less, - chainbase::strcmp_less>>, + member>, ordered_unique< tag, composite_key< diff --git a/libraries/chain/steem_evaluator.cpp b/libraries/chain/steem_evaluator.cpp index 50900187fe..e4976c95a6 100644 --- a/libraries/chain/steem_evaluator.cpp +++ b/libraries/chain/steem_evaluator.cpp @@ -387,7 +387,7 @@ namespace golos { namespace chain { logic_exception::cannot_delete_post_with_worker_techspec, "Cannot delete a post with worker techspec."); - const auto* wto_result = _db.find_worker_result(comment.author, comment.permlink); + const auto* wto_result = _db.find_worker_result(comment.id); GOLOS_CHECK_LOGIC(!wto_result, logic_exception::cannot_delete_post_with_worker_result, "Cannot delete a post with worker result."); diff --git a/libraries/chain/worker_evaluators.cpp b/libraries/chain/worker_evaluators.cpp index eba6168eae..9e15f2f09a 100644 --- a/libraries/chain/worker_evaluators.cpp +++ b/libraries/chain/worker_evaluators.cpp @@ -49,7 +49,7 @@ namespace golos { namespace chain { "Cannot delete worker proposal with approved techspec"); const auto& wto_idx = _db.get_index(); - auto wto_itr = wto_idx.find(std::make_tuple(post.author, post.permlink)); + auto wto_itr = wto_idx.find(wpo.post); GOLOS_CHECK_LOGIC(wto_itr == wto_idx.end(), logic_exception::cannot_delete_worker_proposal_with_techspecs, "Cannot delete worker proposal with techspecs"); @@ -80,7 +80,7 @@ namespace golos { namespace chain { "This worker proposal already has approved techspec"); const auto& wto_idx = _db.get_index(); - auto wto_itr = wto_idx.find(std::make_tuple(wpo_post.author, wpo_post.permlink, o.author)); + auto wto_itr = wto_idx.find(std::make_tuple(wpo->post, o.author)); if (wto_itr != wto_idx.end()) { GOLOS_CHECK_LOGIC(o.permlink == to_string(post.permlink), logic_exception::there_already_is_your_techspec_with_another_permlink, @@ -107,8 +107,7 @@ namespace golos { namespace chain { _db.create([&](worker_techspec_object& wto) { wto.author = o.author; wto.post = post.id; - wto.worker_proposal_author = o.worker_proposal_author; - from_string(wto.worker_proposal_permlink, o.worker_proposal_permlink); + wto.worker_proposal_post = wpo->post; wto.state = worker_techspec_state::created; wto.created = now; wto.net_rshares = post.net_rshares; @@ -125,8 +124,7 @@ namespace golos { namespace chain { const auto& post = _db.get_comment(o.author, o.permlink); const auto& wto = _db.get_worker_techspec(post.id); - const auto& wpo_post = _db.get_comment(wto.worker_proposal_author, wto.worker_proposal_permlink); - const auto& wpo = _db.get_worker_proposal(wpo_post.id); + const auto& wpo = _db.get_worker_proposal(wto.worker_proposal_post); GOLOS_CHECK_LOGIC(wpo.state < worker_proposal_state::payment, logic_exception::cannot_delete_worker_techspec_for_paying_proposal, @@ -135,6 +133,7 @@ namespace golos { namespace chain { if (wto.state == worker_techspec_state::approved) { _db.modify(wpo, [&](worker_proposal_object& wpo) { wpo.state = worker_proposal_state::created; + wpo.approved_techspec_post = comment_id_type(); }); } @@ -206,12 +205,20 @@ namespace golos { namespace chain { _db.clear_worker_techspec_approves(wto); - const auto& wpo_post = _db.get_comment(wto.worker_proposal_author, wto.worker_proposal_permlink); - const auto& wpo = _db.get_worker_proposal(wpo_post.id); + _db.modify(wto, [&](worker_techspec_object& wto) { + wto.state = worker_techspec_state::closed; + }); + } else if (o.state == worker_techspec_approve_state::approve) { + auto approvers = count_approvers(worker_techspec_approve_state::approve); + + if (approvers < STEEMIT_MAJOR_VOTED_WITNESSES) { + return; + } + + const auto& wpo = _db.get_worker_proposal(wto.worker_proposal_post); _db.modify(wpo, [&](worker_proposal_object& wpo) { - wpo.approved_techspec_author = o.author; - from_string(wpo.approved_techspec_permlink, o.permlink); + wpo.approved_techspec_post = wto_post.id; wpo.state = worker_proposal_state::techspec; }); @@ -241,13 +248,12 @@ namespace golos { namespace chain { const auto& wto_post = _db.get_comment(o.author, o.worker_techspec_permlink); const auto& wto = _db.get_worker_techspec(wto_post.id); - const auto* wto_result = _db.find_worker_result(o.author, o.permlink); + const auto* wto_result = _db.find_worker_result(post.id); GOLOS_CHECK_LOGIC(!wto_result, logic_exception::this_post_already_used_as_worker_result, "This post already used as worker result"); - const auto& wpo_post = _db.get_comment(wto.worker_proposal_author, wto.worker_proposal_permlink); - const auto& wpo = _db.get_worker_proposal(wpo_post.id); + const auto& wpo = _db.get_worker_proposal(wto.worker_proposal_post); GOLOS_CHECK_LOGIC(wto.state == worker_techspec_state::approved, logic_exception::worker_result_can_be_created_only_for_techspec_in_work, @@ -263,7 +269,7 @@ namespace golos { namespace chain { } _db.modify(wto, [&](worker_techspec_object& wto) { - from_string(wto.worker_result_permlink, o.permlink); + wto.worker_result_post = post.id; if (o.completion_date != time_point_sec::min()) { wto.completion_date = o.completion_date; @@ -280,10 +286,10 @@ namespace golos { namespace chain { void worker_result_delete_evaluator::do_apply(const worker_result_delete_operation& o) { ASSERT_REQ_HF(STEEMIT_HARDFORK_0_21__1013, "worker_result_delete_operation"); - const auto& wto = _db.get_worker_result(o.author, o.permlink); + const auto& worker_result_post = _db.get_comment(o.author, o.permlink); + const auto& wto = _db.get_worker_result(worker_result_post.id); - const auto& wpo_post = _db.get_comment(wto.worker_proposal_author, wto.worker_proposal_permlink); - const auto& wpo = _db.get_worker_proposal(wpo_post.id); + const auto& wpo = _db.get_worker_proposal(wto.worker_proposal_post); GOLOS_CHECK_LOGIC(wpo.state < worker_proposal_state::payment, logic_exception::cannot_delete_worker_result_for_paying_proposal, @@ -294,7 +300,7 @@ namespace golos { namespace chain { }); _db.modify(wto, [&](worker_techspec_object& wto) { - wto.worker_result_permlink.clear(); + wto.worker_result_post = comment_id_type(); wto.completion_date = time_point::min(); }); } @@ -307,11 +313,10 @@ namespace golos { namespace chain { logic_exception::approver_of_result_should_be_in_top19_of_witnesses, "Approver of result should be in Top 19 of witnesses"); - const auto& wto_post = _db.get_comment(o.author, o.permlink); - const auto& wto = _db.get_worker_result(o.author, o.permlink); + const auto& worker_result_post = _db.get_comment(o.author, o.permlink); + const auto& wto = _db.get_worker_result(worker_result_post.id); - const auto& wpo_post = _db.get_comment(wto.worker_proposal_author, wto.worker_proposal_permlink); - const auto& wpo = _db.get_worker_proposal(wpo_post.id); + const auto& wpo = _db.get_worker_proposal(wto.worker_proposal_post); GOLOS_CHECK_LOGIC(wpo.state == worker_proposal_state::witnesses_review, logic_exception::worker_proposal_should_be_in_review_state_to_approve, @@ -323,7 +328,7 @@ namespace golos { namespace chain { "Approve term has expired"); const auto& wrao_idx = _db.get_index(); - auto wrao_itr = wrao_idx.find(std::make_tuple(wto_post.id, o.approver)); + auto wrao_itr = wrao_idx.find(std::make_tuple(worker_result_post.id, o.approver)); if (o.state == worker_techspec_approve_state::abstain) { if (wrao_itr != wrao_idx.end()) { @@ -340,7 +345,7 @@ namespace golos { namespace chain { } else { _db.create([&](worker_result_approve_object& wrao) { wrao.approver = o.approver; - wrao.post = wto.post; + wrao.post = worker_result_post.id; wrao.state = o.state; }); } @@ -425,8 +430,7 @@ namespace golos { namespace chain { const auto& wto_post = _db.get_comment(o.worker_techspec_author, o.worker_techspec_permlink); const auto& wto = _db.get_worker_techspec(wto_post.id); - const auto& wpo_post = _db.get_comment(wto.worker_proposal_author, wto.worker_proposal_permlink); - const auto& wpo = _db.get_worker_proposal(wpo_post.id); + const auto& wpo = _db.get_worker_proposal(wto.worker_proposal_post); if (!o.worker.size()) { // Unassign worker GOLOS_CHECK_LOGIC(wpo.state == worker_proposal_state::work, diff --git a/plugins/worker_api/include/golos/plugins/worker_api/worker_api_objects.hpp b/plugins/worker_api/include/golos/plugins/worker_api/worker_api_objects.hpp index 1e758233c4..90a012ca72 100644 --- a/plugins/worker_api/include/golos/plugins/worker_api/worker_api_objects.hpp +++ b/plugins/worker_api/include/golos/plugins/worker_api/worker_api_objects.hpp @@ -70,15 +70,12 @@ namespace golos { namespace plugins { namespace worker_api { void fill_worker_proposal(const worker_proposal_object& wpo) { type = wpo.type; state = wpo.state; - approved_techspec_author = wpo.approved_techspec_author; - approved_techspec_permlink = to_string(wpo.approved_techspec_permlink); } comment_api_object post; worker_proposal_type type; worker_proposal_state state; - account_name_type approved_techspec_author; - std::string approved_techspec_permlink; + comment_api_object approved_techspec_post; time_point_sec created; time_point_sec modified; share_type net_rshares; @@ -87,8 +84,6 @@ namespace golos { namespace plugins { namespace worker_api { struct worker_techspec_api_object { worker_techspec_api_object(const worker_techspec_object& o, const comment_api_object& p) : post(p), - worker_proposal_author(o.worker_proposal_author), - worker_proposal_permlink(to_string(o.worker_proposal_permlink)), state(o.state), created(o.created), modified(o.modified), @@ -99,7 +94,6 @@ namespace golos { namespace plugins { namespace worker_api { disapproves(o.disapproves), worker(o.worker), work_beginning_time(o.work_beginning_time), - worker_result_permlink(to_string(o.worker_result_permlink)), completion_date(o.completion_date), payments_count(o.payments_count), payments_interval(o.payments_interval), @@ -113,8 +107,7 @@ namespace golos { namespace plugins { namespace worker_api { } comment_api_object post; - account_name_type worker_proposal_author; - std::string worker_proposal_permlink; + comment_api_object worker_proposal_post; worker_techspec_state state; time_point_sec created; time_point_sec modified; @@ -125,7 +118,7 @@ namespace golos { namespace plugins { namespace worker_api { uint16_t disapproves = 0; account_name_type worker; time_point_sec work_beginning_time; - std::string worker_result_permlink; + comment_api_object worker_result_post; time_point_sec completion_date; uint16_t payments_count = 0; uint32_t payments_interval = 0; @@ -142,11 +135,11 @@ CHAINBASE_SET_INDEX_TYPE( golos::plugins::worker_api::worker_proposal_metadata_index) FC_REFLECT((golos::plugins::worker_api::worker_proposal_api_object), - (post)(type)(state)(approved_techspec_author)(approved_techspec_permlink)(created)(modified)(net_rshares) + (post)(type)(state)(approved_techspec_post)(created)(modified)(net_rshares) ) FC_REFLECT((golos::plugins::worker_api::worker_techspec_api_object), - (post)(worker_proposal_author)(worker_proposal_permlink)(state)(created)(modified)(net_rshares)(specification_cost) - (development_cost)(approves)(disapproves)(worker)(work_beginning_time)(worker_result_permlink)(completion_date)(payments_count) - (payments_interval)(month_consumption)(payment_beginning_time)(next_cashout_time)(finished_payments_count) + (post)(worker_proposal_post)(state)(created)(modified)(net_rshares)(specification_cost)(development_cost)(approves)(disapproves) + (worker)(work_beginning_time)(worker_result_post)(completion_date)(payments_count)(payments_interval)(month_consumption) + (payment_beginning_time)(next_cashout_time)(finished_payments_count) ) diff --git a/plugins/worker_api/include/golos/plugins/worker_api/worker_api_queries.hpp b/plugins/worker_api/include/golos/plugins/worker_api/worker_api_queries.hpp index b083bdeef2..d35d02a98f 100644 --- a/plugins/worker_api/include/golos/plugins/worker_api/worker_api_queries.hpp +++ b/plugins/worker_api/include/golos/plugins/worker_api/worker_api_queries.hpp @@ -58,6 +58,10 @@ namespace golos { namespace plugins { namespace worker_api { return select_states.empty() || select_states.count(state); } + bool has_worker_proposal() const { + return !!worker_proposal_author; + } + bool is_good_worker_proposal(const std::string& author, const std::string& permlink) const { return !worker_proposal_author || (worker_proposal_author == author && worker_proposal_permlink == permlink); diff --git a/plugins/worker_api/worker_api_plugin.cpp b/plugins/worker_api/worker_api_plugin.cpp index 35dabff301..ca4a45dae2 100644 --- a/plugins/worker_api/worker_api_plugin.cpp +++ b/plugins/worker_api/worker_api_plugin.cpp @@ -144,8 +144,6 @@ void worker_api_plugin::plugin_shutdown() { template void worker_api_plugin::worker_api_plugin_impl::select_postbased_results_ordered(const auto& query, std::vector& result, Selector&& select, FillWorkerFields&& fill_worker_fields, bool fill_posts) { - query.validate(); - if (!_db.has_index()) { return; } @@ -189,7 +187,7 @@ void worker_api_plugin::worker_api_plugin_impl::select_postbased_results_ordered } result.emplace_back(obj, ca); auto res_obj = result.back(); - if (!fill_worker_fields(_db, obj, res_obj, query)) { + if (!fill_worker_fields(this, obj, res_obj, query)) { result.pop_back(); } }; @@ -214,7 +212,10 @@ DEFINE_API(worker_api_plugin, get_worker_proposals) { (worker_proposal_query, query) (worker_proposal_sort, sort) (bool, fill_posts) + (bool, fill_approved_techspec_posts) ) + query.validate(); + std::vector result; auto wpo_selector = [&](const worker_proposal_query& query, const worker_proposal_metadata_object& wpmo) -> bool { @@ -224,8 +225,8 @@ DEFINE_API(worker_api_plugin, get_worker_proposals) { return true; }; - auto wpo_fill_worker_fields = [&](const golos::chain::database& _db, const worker_proposal_metadata_object& wpmo, worker_proposal_api_object& wpo_api, const worker_proposal_query& query) -> bool { - auto wpo = _db.get_worker_proposal(wpmo.post); + auto wpo_fill_worker_fields = [&](worker_api_plugin_impl* my, const worker_proposal_metadata_object& wpmo, worker_proposal_api_object& wpo_api, const worker_proposal_query& query) -> bool { + auto wpo = my->_db.get_worker_proposal(wpmo.post); if (!query.is_good_state(wpo.state)) { return false; } @@ -233,6 +234,10 @@ DEFINE_API(worker_api_plugin, get_worker_proposals) { return false; } wpo_api.fill_worker_proposal(wpo); + if (fill_approved_techspec_posts && wpo.approved_techspec_post != comment_id_type()) { + const auto post = my->_db.get_comment(wpo.approved_techspec_post); + wpo_api.approved_techspec_post = my->helper->create_comment_api_object(post); + } return true; }; @@ -250,7 +255,17 @@ DEFINE_API(worker_api_plugin, get_worker_techspecs) { (worker_techspec_query, query) (worker_techspec_sort, sort) (bool, fill_posts) + (bool, fill_worker_proposal_posts) + (bool, fill_worker_result_posts) ) + query.validate(); + + if (query.has_worker_proposal()) { + GOLOS_CHECK_PARAM(fill_worker_proposal_posts, { + GOLOS_CHECK_VALUE(fill_worker_proposal_posts, "Cannot filter by worker proposal without filling worker proposal posts"); + }); + } + std::vector result; auto wto_selector = [&](const worker_techspec_query& query, const worker_techspec_object& wto) -> bool { @@ -260,13 +275,22 @@ DEFINE_API(worker_api_plugin, get_worker_techspecs) { if (!query.is_good_state(wto.state)) { return false; } - if (!query.is_good_worker_proposal(wto.worker_proposal_author, to_string(wto.worker_proposal_permlink))) { - return false; - } return true; }; - auto wto_fill_worker_fields = [&](const golos::chain::database& _db, const worker_techspec_object& wto, worker_techspec_api_object& wto_api, const worker_techspec_query& query) -> bool { + auto wto_fill_worker_fields = [&](worker_api_plugin_impl* my, const worker_techspec_object& wto, worker_techspec_api_object& wto_api, const worker_techspec_query& query) -> bool { + if (fill_worker_proposal_posts) { + const auto post = my->_db.get_comment(wto.worker_proposal_post); + wto_api.worker_proposal_post = my->helper->create_comment_api_object(post); + + if (!query.is_good_worker_proposal(post.author, to_string(post.permlink))) { + return false; + } + } + if (fill_worker_result_posts && wto.worker_result_post != comment_id_type()) { + const auto post = my->_db.get_comment(wto.worker_result_post); + wto_api.worker_result_post = my->helper->create_comment_api_object(post); + } return true; };