Skip to content

Commit

Permalink
Merge pull request #446 from GolosChain/428-remove-hash-usage
Browse files Browse the repository at this point in the history
remove hash usage #428
  • Loading branch information
s-medvedev committed Feb 8, 2019
2 parents 461ffa6 + 51b0572 commit 562a5cc
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 47 deletions.
7 changes: 7 additions & 0 deletions golos.publication/golos.publication.abi
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,13 @@
"orders": [
{"field": "id", "order": "asc"}
]
},
{
"name": "bypermlink",
"unique": "false",
"orders": [
{"field": "permlink", "order": "asc"}
]
}
]
},
Expand Down
56 changes: 35 additions & 21 deletions golos.publication/golos.publication.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "golos.publication.hpp"
#include <common/hash64.hpp>
#include <eosiolib/transaction.hpp>
#include <eosiolib/event.hpp>
#include <cyber.token/cyber.token.hpp>
Expand Down Expand Up @@ -118,7 +117,10 @@ void publication::create_message(name account, std::string permlink,
use_charge(lims, parentacc ? structures::limitparams::COMM : structures::limitparams::POST, issuer, account,
golos::vesting::get_account_effective_vesting(config::vesting_name, account, token_code).amount, token_code, vestpayment);

auto message_id = hash64(permlink);
tables::message_table message_table(_self, account.value);
uint64_t message_id = message_table.available_primary_key();
if (message_id == 0)
message_id = 1;
if(!parentacc)
use_postbw_charge(lims, issuer, account, token_code, message_id);

Expand Down Expand Up @@ -168,11 +170,18 @@ void publication::create_message(name account, std::string permlink,
eosio_assert(pool->state.msgs < std::numeric_limits<structures::counter_t>::max(), "publication::create_message: pool->msgs == max_counter_val");
pools.modify(*pool, _self, [&](auto &item){ item.state.msgs++; });

tables::message_table message_table(_self, account.value);
eosio_assert(message_table.find(message_id) == message_table.end(), "This message already exists.");
auto message_index = message_table.get_index<"bypermlink"_n>();
auto message_itr = message_index.find(permlink);
eosio_assert(message_itr == message_index.end(), "This message already exists.");

tables::content_table content_table(_self, account.value);
auto parent_id = parentacc ? hash64(parentprmlnk) : 0;
uint64_t parent_id = 0;
if (parentacc) {
tables::message_table message_table(_self, parentacc.value);
auto message_index = message_table.get_index<"bypermlink"_n>();
auto message_itr = message_index.find(parentprmlnk);
parent_id = message_itr->id;
}

uint8_t level = 0;
if(parentacc)
Expand Down Expand Up @@ -226,8 +235,12 @@ void publication::update_message(name account, std::string permlink,
std::string languagemssg, std::vector<structures::tag> tags,
std::string jsonmetadata) {
require_auth(account);
tables::message_table message_table(_self, account.value);
auto message_index = message_table.get_index<"bypermlink"_n>();
auto message_itr = message_index.find(permlink);
eosio_assert(message_itr != message_index.end(), "Message doesn't exist.");
tables::content_table content_table(_self, account.value);
auto cont_itr = content_table.find(hash64(permlink));
auto cont_itr = content_table.find(message_itr->id);
eosio_assert(cont_itr != content_table.end(), "Content doesn't exist.");

content_table.modify(cont_itr, account, [&]( auto &item ) {
Expand Down Expand Up @@ -255,12 +268,12 @@ void publication::delete_message(name account, std::string permlink) {
tables::content_table content_table(_self, account.value);
tables::vote_table vote_table(_self, account.value);

auto message_id = hash64(permlink);
auto mssg_itr = message_table.find(message_id);
eosio_assert(mssg_itr != message_table.end(), "Message doesn't exist.");
auto message_index = message_table.get_index<"bypermlink"_n>();
auto mssg_itr = message_index.find(permlink);
eosio_assert(mssg_itr != message_index.end(), "Message doesn't exist.");
eosio_assert((mssg_itr->childcount) == 0, "You can't delete comment with child comments.");
eosio_assert(FP(mssg_itr->state.netshares) <= 0, "Cannot delete a comment with net positive votes.");
auto cont_itr = content_table.find(message_id);
auto cont_itr = content_table.find(mssg_itr->id);
eosio_assert(cont_itr != content_table.end(), "Content doesn't exist.");

if(mssg_itr->parentacc)
Expand All @@ -270,12 +283,12 @@ void publication::delete_message(name account, std::string permlink) {
remove_postbw_charge(account, get_pool(pools, mssg_itr->date)->state.funds.symbol.code(), mssg_itr->id);
}

message_table.erase(mssg_itr);
message_index.erase(mssg_itr);
content_table.erase(cont_itr);

auto votetable_index = vote_table.get_index<"messageid"_n>();
auto vote_itr = votetable_index.lower_bound(message_id);
while ((vote_itr != votetable_index.end()) && (vote_itr->message_id == message_id))
auto vote_itr = votetable_index.lower_bound(mssg_itr->id);
while ((vote_itr != votetable_index.end()) && (vote_itr->message_id == mssg_itr->id))
vote_itr = votetable_index.erase(vote_itr);
}

Expand Down Expand Up @@ -519,10 +532,10 @@ void publication::set_vote(name voter, name author, string permlink, int16_t wei
const auto &max_vote_changes_param = cfg.get().max_vote_changes_param;
const auto &social_acc_param = cfg.get().social_acc_param;

uint64_t id = hash64(permlink);
tables::message_table message_table(_self, author.value);
auto mssg_itr = message_table.find(id);
eosio_assert(mssg_itr != message_table.end(), "Message doesn't exist.");
auto message_index = message_table.get_index<"bypermlink"_n>();
auto mssg_itr = message_index.find(permlink);
eosio_assert(mssg_itr != message_index.end(), "Message doesn't exist.");
tables::reward_pools pools(_self, _self.value);
auto pool = get_pool(pools, mssg_itr->date);
check_account(voter, pool->state.funds.symbol);
Expand All @@ -531,7 +544,7 @@ void publication::set_vote(name voter, name author, string permlink, int16_t wei
auto cur_time = current_time();

auto votetable_index = vote_table.get_index<"byvoter"_n>();
auto vote_itr = votetable_index.find(std::make_tuple(hash64(permlink), voter));
auto vote_itr = votetable_index.find(std::make_tuple(mssg_itr->id, voter));
if (vote_itr != votetable_index.end()) {
// it's not consensus part and can be moved to storage in future
if (mssg_itr->closed) {
Expand All @@ -558,7 +571,7 @@ void publication::set_vote(name voter, name author, string permlink, int16_t wei
send_poolstate_event(item);
});

message_table.modify(mssg_itr, name(), [&]( auto &item ) {
message_index.modify(mssg_itr, name(), [&]( auto &item ) {
item.state.netshares = new_mssg_rshares.data();
item.state.sumcuratorsw = (FP(item.state.sumcuratorsw) - FP(vote_itr->curatorsw)).data();
send_poststate_event(author, item);
Expand Down Expand Up @@ -600,7 +613,7 @@ void publication::set_vote(name voter, name author, string permlink, int16_t wei

auto sumcuratorsw_delta = get_delta(machine, FP(mssg_itr->state.voteshares), FP(msg_new_state.voteshares), pool->rules.curationfunc);
msg_new_state.sumcuratorsw = (FP(mssg_itr->state.sumcuratorsw) + sumcuratorsw_delta).data();
message_table.modify(mssg_itr, _self, [&](auto &item) {
message_index.modify(mssg_itr, _self, [&](auto &item) {
item.state = msg_new_state;
send_poststate_event(author, item);
});
Expand Down Expand Up @@ -813,8 +826,9 @@ void publication::set_params(std::vector<posting_params> params) {

void publication::reblog(name rebloger, name author, std::string permlink) {
tables::message_table message_table(_self, author.value);
auto message_id = hash64(permlink);
eosio_assert(message_table.find(message_id) != message_table.end(),
auto message_index = message_table.get_index<"bypermlink"_n>();
auto mssg_itr = message_index.find(permlink);
eosio_assert(mssg_itr != message_index.end(),
"You can't reblog, because this message doesn't exist.");
}

Expand Down
8 changes: 7 additions & 1 deletion golos.publication/objects.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ struct message {
uint64_t primary_key() const {
return id;
}

std::string secondary_key() const {
return permlink;
}

};

struct delegate_voter {
Expand Down Expand Up @@ -199,7 +204,8 @@ namespace tables {
using namespace eosio;

using id_index = indexed_by<N(id), const_mem_fun<structures::message, uint64_t, &structures::message::primary_key>>;
using message_table = multi_index<N(message), structures::message, id_index>;
using permlink_index = indexed_by<N(bypermlink), const_mem_fun<structures::message, std::string, &structures::message::secondary_key>>;
using message_table = multi_index<N(message), structures::message, id_index, permlink_index>;

using content_id_index = indexed_by<N(id), const_mem_fun<structures::content, uint64_t, &structures::content::primary_key>>;
using content_table = multi_index<N(content), structures::content, content_id_index>;
Expand Down
18 changes: 18 additions & 0 deletions tests/golos.posting_test_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,24 @@ struct golos_posting_api: base_contract_api {
return _tester->get_chaindb_struct(_code, acc, N(content), id, "content");
}

variant get_message(account_name acc, const std::string& permlink) {
variant obj = _tester->get_chaindb_lower_bound_struct(_code, acc, N(message), N(bypermlink), permlink, "message");
if (!obj.is_null()) {
if(obj["permlink"].as<std::string>() != permlink) {
return variant();
}
}
return obj;
}

variant get_content(account_name acc, const std::string& permlink) {
variant obj = get_message(acc, permlink);
if(!obj.is_null()) {
return get_content(acc, obj["id"].as<uint64_t>());
}
return variant();
}

variant get_vote(account_name acc, uint64_t id) {
return _tester->get_chaindb_struct(_code, acc, N(vote), id, "voteinfo");
}
Expand Down
8 changes: 4 additions & 4 deletions tests/golos.publication_rewards_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ class reward_calcs_tester : public extended_tester {
for (auto itr = msgs.begin(); itr != msgs.end(); ++itr) {
auto cur = *itr;
if (!cur["closed"].as<bool>()) {
s.set_message(message_key{user, cur["id"].as<uint64_t>()}, {
s.set_message(message_key{user, cur["permlink"].as<std::string>()}, {
static_cast<double>(FP(cur["state"]["netshares"].as<base_t>())),
static_cast<double>(FP(cur["state"]["voteshares"].as<base_t>())),
static_cast<double>(FP(cur["state"]["sumcuratorsw"].as<base_t>()))
Expand Down Expand Up @@ -455,7 +455,7 @@ class reward_calcs_tester : public extended_tester {
bool vestpayment = false
) {
auto ret = post.create_msg(author, permlink, parentacc, parentprmlnk, beneficiaries, tokenprop, vestpayment);
message_key key{author, hash64(permlink)};
message_key key{author, permlink};

auto reward_weight = 0.0;
string ret_str = ret;
Expand All @@ -478,7 +478,7 @@ class reward_calcs_tester : public extended_tester {
static_cast<double>(cur_time().to_seconds()),
beneficiaries, reward_weight));
} else {
key.id = 0;
key.permlink = std::string();
}
return ret;
}
Expand All @@ -494,7 +494,7 @@ class reward_calcs_tester : public extended_tester {
? post.upvote(voter, author, permlink, weight)
: post.downvote(voter, author, permlink, -weight);

message_key msg_key{author, hash64(permlink)};
message_key msg_key{author, permlink};

string ret_str = ret;
if ((ret == success()) || (ret_str.find("forum::apply_limits:") != string::npos)) {
Expand Down
8 changes: 4 additions & 4 deletions tests/golos.publication_rewards_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ inline double get_prop(int64_t arg) {

struct message_key {
account_name author;
uint64_t id;
std::string permlink;
bool operator ==(const message_key& rhs) const {
return author == rhs.author && id == rhs.id;
return author == rhs.author && permlink == rhs.permlink;
}
};

Expand Down Expand Up @@ -94,10 +94,10 @@ struct statemap : public std::map<std::string, aprox_val_t> {
return "balance of " + acc.to_string() + ": ";
}
static std::string get_message_str(const message_key& msg) {
return "message of " + msg.author.to_string() + " #" + std::to_string(msg.id) + ": ";
return "message of " + msg.author.to_string() + " #" + msg.permlink + ": ";
}
static std::string get_vote_str(account_name voter, const message_key& msg) {
return "vote of " + voter.to_string() + " for message of " + msg.author.to_string() + " #" + std::to_string(msg.id) + ": ";
return "vote of " + voter.to_string() + " for message of " + msg.author.to_string() + " #" + msg.permlink + ": ";
}
void set_pool(uint64_t id, const pool_data& data = {}) {
auto prefix = get_pool_str(id);
Expand Down
25 changes: 11 additions & 14 deletions tests/golos.publication_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ class golos_publication_tester : public golos_tester {

void check_equal_content(const variant& a, const variant& b) {
BOOST_CHECK_EQUAL(true, a.is_object() && b.is_object());
BOOST_CHECK_EQUAL(a["id"].as<uint64_t>(), b["id"].as<uint64_t>());
BOOST_CHECK_EQUAL(a["headermssg"].as<std::string>(), b["headermssg"].as<std::string>());
BOOST_CHECK_EQUAL(a["bodymssg"].as<std::string>(), b["bodymssg"].as<std::string>());
BOOST_CHECK_EQUAL(a["languagemssg"].as<std::string>(), b["languagemssg"].as<std::string>());
Expand All @@ -114,7 +113,7 @@ class golos_publication_tester : public golos_tester {

protected:
const mvo _test_msg = mvo()
("id", hash64("permlink"))
("id", 1)
("parentacc", "")
("parent_id", 0)
("tokenprop", 0)
Expand All @@ -130,7 +129,6 @@ class golos_publication_tester : public golos_tester {
("level", 0);

const mvo _test_content = mvo()
("id", hash64("permlink"))
("headermssg", "headermssg")
("bodymssg", "bodymssg")
("languagemssg", "languagemssg")
Expand Down Expand Up @@ -206,7 +204,7 @@ BOOST_FIXTURE_TEST_CASE(create_message, golos_publication_tester) try {
BOOST_TEST_MESSAGE("--- checking that another user can create a message with the same permlink.");
BOOST_CHECK_EQUAL(success(), post.create_msg(N(chucknorris), "permlink"));

auto id = hash64("permlink");
auto id = 1;
check_equal_post(post.get_message(N(brucelee), id), _test_msg);
check_equal_content(post.get_content(N(brucelee), id), _test_content);

Expand Down Expand Up @@ -235,14 +233,13 @@ BOOST_FIXTURE_TEST_CASE(update_message, golos_publication_tester) try {
BOOST_CHECK_EQUAL(success(), post.update_msg(N(brucelee), "permlink",
"headermssgnew", "bodymssgnew", "languagemssgnew", {{"tagnew"}}, "jsonmetadatanew"));

check_equal_content(post.get_content(N(brucelee), hash64("permlink")), mvo()
("id", hash64("permlink"))
check_equal_content(post.get_content(N(brucelee), 1), mvo()
("headermssg", "headermssgnew")
("bodymssg", "bodymssgnew")
("languagemssg", "languagemssgnew")
("tags", variants({mvo()("tag", "tagnew")}))
("jsonmetadata", "jsonmetadatanew"));
BOOST_CHECK_EQUAL(err.no_content, post.update_msg(N(brucelee), "permlinknew",
BOOST_CHECK_EQUAL(err.no_message, post.update_msg(N(brucelee), "permlinknew",
"headermssgnew", "bodymssgnew", "languagemssgnew", {{"tagnew"}}, "jsonmetadatanew"));
} FC_LOG_AND_RETHROW()

Expand Down Expand Up @@ -277,7 +274,7 @@ BOOST_FIXTURE_TEST_CASE(upvote, golos_publication_tester) try {
BOOST_TEST_MESSAGE("--- succeed on initial upvote");
BOOST_CHECK_EQUAL(success(), post.create_msg(N(brucelee), permlink));
BOOST_CHECK_EQUAL(success(), vote_brucelee(123));
auto _vote = mvo()("id",0)("message_id",hash64(permlink))("voter","brucelee")("count",1); // TODO: time
auto _vote = mvo()("id",0)("message_id",1)("voter","brucelee")("count",1); // TODO: time
CHECK_MATCHING_OBJECT(post.get_vote(N(brucelee), 0), _vote);
produce_block();

Expand Down Expand Up @@ -326,7 +323,7 @@ BOOST_FIXTURE_TEST_CASE(downvote, golos_publication_tester) try {
BOOST_TEST_MESSAGE("--- succeed on initial downvote");
BOOST_CHECK_EQUAL(success(), post.create_msg(N(brucelee), permlink));
BOOST_CHECK_EQUAL(success(), vote_brucelee(123));
auto _vote = mvo()("id",0)("message_id",hash64(permlink))("voter","brucelee")("count",1); // TODO: time
auto _vote = mvo()("id",0)("message_id",1)("voter","brucelee")("count",1); // TODO: time
CHECK_MATCHING_OBJECT(post.get_vote(N(brucelee), 0), _vote);
produce_block();

Expand Down Expand Up @@ -431,20 +428,20 @@ BOOST_FIXTURE_TEST_CASE(comments_cashout_time_test, golos_publication_tester) tr
produce_blocks(need_blocks);

BOOST_TEST_MESSAGE("--- checking that messages wasn't closed.");
BOOST_CHECK_EQUAL(post.get_message(N(brucelee), hash64("permlink"))["closed"].as<bool>(), false);
BOOST_CHECK_EQUAL(post.get_message(N(chucknorris), hash64("comment_permlink"))["closed"].as<bool>(), false);
BOOST_CHECK_EQUAL(post.get_message(N(brucelee), 1)["closed"].as<bool>(), false);
BOOST_CHECK_EQUAL(post.get_message(N(chucknorris), 2)["closed"].as<bool>(), false);

produce_block();

BOOST_TEST_MESSAGE("--- checking that messages was closed.");
BOOST_CHECK_EQUAL(post.get_message(N(brucelee), hash64("permlink"))["closed"].as<bool>(), true);
BOOST_CHECK_EQUAL(post.get_message(N(chucknorris), hash64("comment_permlink"))["closed"].as<bool>(), true);
BOOST_CHECK_EQUAL(post.get_message(N(brucelee), 1)["closed"].as<bool>(), true);
BOOST_CHECK_EQUAL(post.get_message(N(chucknorris), 2)["closed"].as<bool>(), true);

BOOST_CHECK_EQUAL(success(), post.create_msg(N(jackiechan), "sorry guys i'm late", N(brucelee), "permlink"));
produce_block();

BOOST_TEST_MESSAGE("--- checking that closed message comment was closed.");
BOOST_CHECK_EQUAL(post.get_message(N(jackiechan), hash64("sorry guys i'm late"))["closed"].as<bool>(), true);
BOOST_CHECK_EQUAL(post.get_message(N(jackiechan), 3)["closed"].as<bool>(), true);

} FC_LOG_AND_RETHROW()

Expand Down
5 changes: 2 additions & 3 deletions tests/golos.referral_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,7 @@ BOOST_FIXTURE_TEST_CASE(create_referral_message_tests, golos_referral_tester) tr
BOOST_CHECK_EQUAL(success(), referral.create_referral(N(issuer), N(sania), 500, cur_time().to_seconds() + expire, token.make_asset(50)));
BOOST_CHECK_EQUAL(success(), post.create_msg(N(sania), "permlink"));

auto id = hash64("permlink");
auto post_sania = post.get_message(N(sania), id);
auto post_sania = post.get_message(N(sania), "permlink");
BOOST_CHECK_EQUAL (post_sania["beneficiaries"].size(), 1);
BOOST_CHECK_EQUAL( post_sania["beneficiaries"][uint8_t(0)].as<beneficiary>().account, N(issuer) );

Expand All @@ -250,7 +249,7 @@ BOOST_FIXTURE_TEST_CASE(create_referral_message_tests, golos_referral_tester) tr
BOOST_CHECK_EQUAL(err.referrer_benif, post.create_msg(N(pasha), "permlink", N(), "parentprmlnk", { beneficiary{N(issuer), 2000} }));
BOOST_CHECK_EQUAL(success(), post.create_msg(N(pasha), "permlink", N(), "parentprmlnk", { beneficiary{N(tania), 2000} }));

auto post_pasha = post.get_message(N(pasha), id);
auto post_pasha = post.get_message(N(pasha), "permlink");
BOOST_CHECK_EQUAL (post_pasha["beneficiaries"].size(), 2);
} FC_LOG_AND_RETHROW()

Expand Down
13 changes: 13 additions & 0 deletions tests/golos_tester.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ class golos_tester : public tester {
fc::variant get_tbl_struct(name code, uint64_t scope, name tbl, uint64_t id, const std::string& n) const;
fc::variant get_tbl_singleton(name code, uint64_t scope, name tbl, const string &n) const;

template<typename Key>
fc::variant get_chaindb_lower_bound_struct(name code, uint64_t scope, name tbl, name indx, const Key& key, const std::string& n) const {
variant r;
try {
bytes data = fc::raw::pack(key);
const auto& finfo = _chaindb.lower_bound({code, scope, tbl, indx}, data.data(), data.size());
r = _chaindb.value_at_cursor({code, finfo.cursor});
} catch (...) {
// key not found
}
return r;
}

fc::variant get_chaindb_struct(name code, uint64_t scope, name tbl, uint64_t id, const std::string& n) const;
fc::variant get_chaindb_singleton(name code, uint64_t scope, name tbl, const std::string& n) const;
std::vector<fc::variant> get_all_chaindb_rows(name code, uint64_t scope, name tbl, bool strict) const;
Expand Down

0 comments on commit 562a5cc

Please sign in to comment.