Skip to content

Commit

Permalink
get_donates improvements #90
Browse files Browse the repository at this point in the history
  • Loading branch information
isteric-senior committed May 19, 2020
1 parent 7b0e575 commit 49b36b0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 33 deletions.
14 changes: 10 additions & 4 deletions libraries/chain/include/golos/chain/steem_objects.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ namespace golos {
donate_object() = delete;

template<typename Constructor, typename Allocator>
donate_object(Constructor&& c, allocator<Allocator> a) : comment(a) {
donate_object(Constructor&& c, allocator<Allocator> a) : target(a), comment(a) {
c(*this);
}

Expand All @@ -265,7 +265,8 @@ namespace golos {
asset amount;
account_name_type app;
uint16_t version;
fc::variant_object target;
shared_string target;
fc::sha256 target_id;
shared_string comment;
time_point_sec time;
};
Expand Down Expand Up @@ -496,8 +497,8 @@ namespace golos {

struct by_from_to;
struct by_to_from;
struct by_target_from_to;
struct by_app_version;
struct by_item_id;

using donate_index = multi_index_container<
donate_object,
Expand All @@ -511,6 +512,11 @@ namespace golos {
member<donate_object, account_name_type, &donate_object::to>,
member<donate_object, account_name_type, &donate_object::from>
>>,
ordered_non_unique<tag<by_target_from_to>, composite_key<donate_object,
member<donate_object, fc::sha256, &donate_object::target_id>,
member<donate_object, account_name_type, &donate_object::from>,
member<donate_object, account_name_type, &donate_object::to>
>>,
ordered_non_unique<tag<by_app_version>, composite_key<donate_object,
member<donate_object, account_name_type, &donate_object::app>,
member<donate_object, uint16_t, &donate_object::version>
Expand Down Expand Up @@ -559,5 +565,5 @@ FC_REFLECT((golos::chain::decline_voting_rights_request_object),
CHAINBASE_SET_INDEX_TYPE(golos::chain::decline_voting_rights_request_object, golos::chain::decline_voting_rights_request_index)

FC_REFLECT((golos::chain::donate_object),
(id)(from)(to)(amount)(app)(version)(target)(comment)(time))
(id)(from)(to)(amount)(app)(version)(target)(target_id)(comment)(time))
CHAINBASE_SET_INDEX_TYPE(golos::chain::donate_object, golos::chain::donate_index)
4 changes: 3 additions & 1 deletion libraries/chain/steem_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2722,6 +2722,7 @@ void delegate_vesting_shares(
auto itr = idx.find(std::make_tuple(op.memo.app, op.memo.version));
if (itr != idx.end()) {
std::function<bool(const fc::variant_object&,const fc::variant_object&)> same_schema = [&](auto& memo1, auto& memo2) {
if (memo1.size() != memo2.size()) return false;
for (auto& entry : memo1) {
if (!memo2.contains(entry.key().c_str())) return false;
auto e_obj = entry.value().is_object();
Expand All @@ -2735,7 +2736,8 @@ void delegate_vesting_shares(
}
return true;
};
GOLOS_CHECK_LOGIC(same_schema(itr->target, op.memo.target) && same_schema(op.memo.target, itr->target),
const auto& target = fc::json::from_string(to_string(itr->target)).get_object();
GOLOS_CHECK_LOGIC(same_schema(target, op.memo.target) && same_schema(op.memo.target, target),
logic_exception::wrong_donate_target_version,
"Donate target schema changed without changing API version.");
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/account_history/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ if (options.count(name)) { \
void operator()(const donate_operation& op) {
insert_sender(op.from);
insert_receiver(op.to);
const auto& to_account = myimpl->db.get_account(op.to);
const auto& to_account = myimpl->db.get_account(op.from.size() ? op.from : op.to);
insert_receiver(to_account.referrer_account);
}

Expand Down
57 changes: 30 additions & 27 deletions plugins/social_network/social_network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ namespace golos { namespace plugins { namespace social_network {
) const ;

std::vector<donate_object> select_donates (
const std::string& from, const std::string& to, uint32_t limit, uint32_t offset
const fc::variant_object& target, const std::string& from, const std::string& to, uint32_t limit, uint32_t offset
) const ;

std::vector<discussion> get_content_replies(
Expand Down Expand Up @@ -181,7 +181,7 @@ namespace golos { namespace plugins { namespace social_network {
}

std::vector<donate_object> social_network::impl::select_donates(
const std::string& from, const std::string& to, uint32_t limit, uint32_t offset
const fc::variant_object& target, const std::string& from, const std::string& to, uint32_t limit, uint32_t offset
) const {
if (limit == 0) {
return {};
Expand All @@ -198,26 +198,23 @@ namespace golos { namespace plugins { namespace social_network {
}
};

if (from.size()) {
const auto& from_to_idx = db.get_index<donate_index, by_from_to>();
if (to.size()) {
auto itr = from_to_idx.lower_bound(std::make_tuple(from, to));
fill_donates(from_to_idx, itr,
[&](auto& itr) {
return itr->from == from && itr->to == to;
});
} else {
auto itr = from_to_idx.lower_bound(std::make_tuple(from));
fill_donates(from_to_idx, itr,
[&](auto& itr) {
return itr->from == from;
});
}
if (target.size()) {
const auto target_id = fc::sha256::hash(fc::json::to_string(target));
const auto& idx = db.get_index<donate_index, by_target_from_to>();
auto itr = idx.lower_bound(std::make_tuple(target_id, from, to));
fill_donates(idx, itr, [&](auto& itr) {
return itr->target_id == target_id && (!from.size() || itr->from == from) && (!to.size() || itr->to == to);
});
} else if (from.size()) {
const auto& idx = db.get_index<donate_index, by_from_to>();
auto itr = idx.lower_bound(std::make_tuple(from, to));
fill_donates(idx, itr, [&](auto& itr) {
return itr->from == from && (!to.size() || itr->to == to);
});
} else if (to.size()) {
const auto& to_from_idx = db.get_index<donate_index, by_to_from>();
auto itr = to_from_idx.lower_bound(std::make_tuple(to));
fill_donates(to_from_idx, itr,
[&](auto& itr) {
const auto& idx = db.get_index<donate_index, by_to_from>();
auto itr = idx.lower_bound(std::make_tuple(to));
fill_donates(idx, itr, [&](auto& itr) {
return itr->to == to;
});
}
Expand Down Expand Up @@ -510,8 +507,13 @@ namespace golos { namespace plugins { namespace social_network {
don.amount = op.amount;
don.app = op.memo.app;
don.version = op.memo.version;
don.target = op.memo.target;

const auto& target = fc::json::to_string(op.memo.target);
from_string(don.target, target);
don.target_id = fc::sha256::hash(target);

if (!!op.memo.comment) from_string(don.comment, *op.memo.comment);

don.time = db.head_block_time();
});
}
Expand Down Expand Up @@ -846,13 +848,14 @@ namespace golos { namespace plugins { namespace social_network {

DEFINE_API(social_network, get_donates) {
PLUGIN_API_VALIDATE_ARGS(
(string, from)
(string, to)
(uint32_t, limit, DEFAULT_VOTE_LIMIT)
(uint32_t, offset, 0)
(fc::variant_object, target)
(string, from)
(string, to)
(uint32_t, limit, DEFAULT_VOTE_LIMIT)
(uint32_t, offset, 0)
);
return pimpl->db.with_weak_read_lock([&]() {
return pimpl->select_donates(from, to, limit, offset);
return pimpl->select_donates(target, from, to, limit, offset);
});
}

Expand Down

0 comments on commit 49b36b0

Please sign in to comment.