Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement tests for delegators #705 #768

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
227 changes: 143 additions & 84 deletions tests/golos.publication_rewards_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ class reward_calcs_tester : public golos_tester {
, _users{
N(alice), N(alice1), N(alice2), N(alice3), N(alice4), N(alice5),
N(bob), N(bob1), N(bob2), N(bob3), N(bob4), N(bob5),
N(why), N(has), N(my), N(imagination), N(become), N(poor),
N(voter), N(voter1), N(voter2),
N(voter3), N(voter4), N(voter5), N(votersix)}
N(why), N(has), N(my), N(imagination), N(become), N(poor)}
, _stranger(N(dan.larimer)) {

produce_blocks(2); // why 2?
Expand Down Expand Up @@ -1082,6 +1080,7 @@ BOOST_FIXTURE_TEST_CASE(golos_delegators_test, reward_calcs_tester) try {
BOOST_TEST_MESSAGE("golos_delegators_test");
int64_t maxfp = std::numeric_limits<fixp_t>::max();
auto bignum = 500000000000;
auto vest_delta = 0.01;
init(bignum, 500000);
produce_blocks();
BOOST_TEST_MESSAGE("--- setrules");
Expand Down Expand Up @@ -1115,20 +1114,39 @@ BOOST_FIXTURE_TEST_CASE(golos_delegators_test, reward_calcs_tester) try {
auto vest_amount = vest.make_asset(150000000);
uint16_t interest_rate = 3000;

BOOST_TEST_MESSAGE("--- create_voters_and_add_vesting");
std::vector<account_name> voters = {N(voter), N(voter1), N(voter2),
N(voter3), N(voter4), N(voter5), N(votersix)};
create_accounts(voters);
add_vesting(voters, 500000);

BOOST_TEST_MESSAGE("--- create_delegators_and_add_vesting");
std::vector<account_name> delegators = {N(dlg), N(dlg1), N(dlg2),
N(dlg3), N(dlg4), N(dlg5), N(dlgsix), N(dlgseven), N(dlgeight),
N(dlgnine), N(dlgten), N(dlgeleven), N(dlgtwelve)};
create_accounts(delegators);
add_vesting(delegators, 500000);

BOOST_TEST_MESSAGE("--- one delegator interest_rate > 0");
BOOST_TEST_MESSAGE("--- create_message: " << name{_users[0]}.to_string());
BOOST_CHECK_EQUAL(success(), create_message({_users[0], "permlink"}));
check();
BOOST_CHECK_EQUAL(success(), delegate_vest(_users[2], _users[18], amount, interest_rate));
check();
BOOST_TEST_MESSAGE("--- " << name{_users[18]}.to_string() << " voted");
BOOST_CHECK_EQUAL(success(), addvote(_users[18], {_users[0], "permlink"}, cfg::_100percent));
BOOST_CHECK_EQUAL(success(), delegate_vest(delegators[0], voters[0], amount, interest_rate));
check();
auto voter_amount = vest.get_balance_raw(_users[18])["vesting"].as<asset>();
auto delegator_amount = vest.get_balance_raw(_users[2])["vesting"].as<asset>();
BOOST_TEST_MESSAGE("--- " << name{voters[0]}.to_string() << " voted");
BOOST_CHECK_EQUAL(success(), addvote(voters[0], {_users[0], "permlink"}, cfg::_100percent));
produce_blocks(golos::seconds_to_blocks(post.window));
BOOST_CHECK_GT(vest.get_balance_raw(_users[18])["vesting"].as<asset>(), voter_amount);
BOOST_CHECK_GT(vest.get_balance_raw(_users[2])["vesting"].as<asset>(), delegator_amount);
check();
CHECK_EQUAL_WITH_DELTA(
vest.get_balance_raw(voters[0])["vesting"].as<asset>().to_real(),
_state.balances[voters[0]].vestamount,
vest_delta
);
CHECK_EQUAL_WITH_DELTA(
vest.get_balance_raw(delegators[0])["vesting"].as<asset>().to_real(),
_state.balances[delegators[0]].vestamount,
vest_delta
);
check();

BOOST_TEST_MESSAGE("--- add_funds_to_forum");
Expand All @@ -1139,16 +1157,22 @@ BOOST_FIXTURE_TEST_CASE(golos_delegators_test, reward_calcs_tester) try {
BOOST_TEST_MESSAGE("--- create_message: " << name{_users[0]}.to_string());
BOOST_CHECK_EQUAL(success(), create_message({_users[0], "permlink1"}));
check();
BOOST_CHECK_EQUAL(success(), delegate_vest(_users[3], _users[19], amount, cfg::_100percent));
check();
BOOST_TEST_MESSAGE("--- " << name{_users[19]}.to_string() << " voted");
BOOST_CHECK_EQUAL(success(), addvote(_users[19], {_users[0], "permlink1"}, cfg::_100percent));
BOOST_CHECK_EQUAL(success(), delegate_vest(delegators[1], voters[1], amount, cfg::_100percent));
check();
voter_amount = vest.get_balance_raw(_users[19])["vesting"].as<asset>();
delegator_amount = vest.get_balance_raw(_users[3])["vesting"].as<asset>();
BOOST_TEST_MESSAGE("--- " << name{voters[1]}.to_string() << " voted");
BOOST_CHECK_EQUAL(success(), addvote(voters[1], {_users[0], "permlink1"}, cfg::_100percent));
produce_blocks(golos::seconds_to_blocks(post.window));
BOOST_CHECK_GT(vest.get_balance_raw(_users[19])["vesting"].as<asset>(), voter_amount);
BOOST_CHECK_GT(vest.get_balance_raw(_users[3])["vesting"].as<asset>(), delegator_amount);
check();
CHECK_EQUAL_WITH_DELTA(
vest.get_balance_raw(voters[1])["vesting"].as<asset>().to_real(),
_state.balances[voters[1]].vestamount,
vest_delta
);
CHECK_EQUAL_WITH_DELTA(
vest.get_balance_raw(delegators[1])["vesting"].as<asset>().to_real(),
_state.balances[delegators[1]].vestamount,
vest_delta
);
check();

BOOST_TEST_MESSAGE("--- add_funds_to_forum");
Expand All @@ -1159,16 +1183,20 @@ BOOST_FIXTURE_TEST_CASE(golos_delegators_test, reward_calcs_tester) try {
BOOST_TEST_MESSAGE("--- create_message: " << name{_users[0]}.to_string());
BOOST_CHECK_EQUAL(success(), create_message({_users[0], "permlink2"}));
check();
BOOST_CHECK_EQUAL(success(), delegate_vest(_users[4], _users[20], amount, 0));
BOOST_CHECK_EQUAL(success(), delegate_vest(delegators[2], voters[2], amount, 0));
check();
BOOST_TEST_MESSAGE("--- " << name{_users[20]}.to_string() << " voted");
BOOST_CHECK_EQUAL(success(), addvote(_users[20], {_users[0], "permlink2"}, cfg::_100percent));
BOOST_TEST_MESSAGE("--- " << name{voters[2]}.to_string() << " voted");
BOOST_CHECK_EQUAL(success(), addvote(voters[2], {_users[0], "permlink2"}, cfg::_100percent));
check();
voter_amount = vest.get_balance_raw(_users[20])["vesting"].as<asset>();
delegator_amount = vest.get_balance_raw(_users[4])["vesting"].as<asset>();
auto delegator2_amount = vest.get_balance_raw(delegators[2])["vesting"].as<asset>();
produce_blocks(golos::seconds_to_blocks(post.window));
BOOST_CHECK_GT(vest.get_balance_raw(_users[20])["vesting"].as<asset>(), voter_amount);
BOOST_CHECK_EQUAL(vest.get_balance_raw(_users[4])["vesting"].as<asset>(), delegator_amount);
check();
CHECK_EQUAL_WITH_DELTA(
vest.get_balance_raw(voters[2])["vesting"].as<asset>().to_real(),
_state.balances[voters[2]].vestamount,
vest_delta
);
BOOST_CHECK_EQUAL(vest.get_balance_raw(delegators[2])["vesting"].as<asset>(), delegator2_amount);
check();

BOOST_TEST_MESSAGE("--- add_funds_to_forum");
Expand All @@ -1180,22 +1208,34 @@ BOOST_FIXTURE_TEST_CASE(golos_delegators_test, reward_calcs_tester) try {
BOOST_CHECK_EQUAL(success(), create_message({_users[0], "permlink3"}));
check();
interest_rate = 4500;
BOOST_CHECK_EQUAL(success(), delegate_vest(_users[5], _users[21], amount, interest_rate));
BOOST_CHECK_EQUAL(success(), delegate_vest(_users[6], _users[21], amount, interest_rate));
BOOST_CHECK_EQUAL(success(), delegate_vest(_users[7], _users[21], amount, interest_rate));
BOOST_CHECK_EQUAL(success(), delegate_vest(delegators[3], voters[3], amount, interest_rate));
BOOST_CHECK_EQUAL(success(), delegate_vest(delegators[4], voters[3], amount, interest_rate));
BOOST_CHECK_EQUAL(success(), delegate_vest(delegators[5], voters[3], amount, interest_rate));
check();
BOOST_TEST_MESSAGE("--- " << name{_users[21]}.to_string() << " voted");
BOOST_CHECK_EQUAL(success(), addvote(_users[21], {_users[0], "permlink3"}, cfg::_100percent));
check();
voter_amount = vest.get_balance_raw(_users[21])["vesting"].as<asset>();
auto delegator1_amount = vest.get_balance_raw(_users[5])["vesting"].as<asset>();
auto delegator2_amount = vest.get_balance_raw(_users[6])["vesting"].as<asset>();
auto delegator3_amount = vest.get_balance_raw(_users[7])["vesting"].as<asset>();
BOOST_TEST_MESSAGE("--- " << name{voters[3]}.to_string() << " voted");
BOOST_CHECK_EQUAL(success(), addvote(voters[3], {_users[0], "permlink3"}, cfg::_100percent));
produce_blocks(golos::seconds_to_blocks(post.window));
BOOST_CHECK_GT(vest.get_balance_raw(_users[21])["vesting"].as<asset>(), voter_amount);
BOOST_CHECK_GT(vest.get_balance_raw(_users[5])["vesting"].as<asset>(), delegator1_amount);
BOOST_CHECK_GT(vest.get_balance_raw(_users[6])["vesting"].as<asset>(), delegator2_amount);
BOOST_CHECK_GT(vest.get_balance_raw(_users[7])["vesting"].as<asset>(), delegator3_amount);
check();
CHECK_EQUAL_WITH_DELTA(
vest.get_balance_raw(voters[3])["vesting"].as<asset>().to_real(),
_state.balances[voters[3]].vestamount,
vest_delta
);
CHECK_EQUAL_WITH_DELTA(
vest.get_balance_raw(delegators[3])["vesting"].as<asset>().to_real(),
_state.balances[delegators[3]].vestamount,
vest_delta
);
CHECK_EQUAL_WITH_DELTA(
vest.get_balance_raw(delegators[4])["vesting"].as<asset>().to_real(),
_state.balances[delegators[4]].vestamount,
vest_delta
);
CHECK_EQUAL_WITH_DELTA(
vest.get_balance_raw(delegators[5])["vesting"].as<asset>().to_real(),
_state.balances[delegators[5]].vestamount,
vest_delta
);
check();

BOOST_TEST_MESSAGE("--- add_funds_to_forum");
Expand All @@ -1206,22 +1246,31 @@ BOOST_FIXTURE_TEST_CASE(golos_delegators_test, reward_calcs_tester) try {
BOOST_TEST_MESSAGE("--- create_message: " << name{_users[0]}.to_string());
BOOST_CHECK_EQUAL(success(), create_message({_users[0], "permlink4"}));
check();
BOOST_CHECK_EQUAL(success(), delegate_vest(_users[8], _users[22], amount, interest_rate));
BOOST_CHECK_EQUAL(success(), delegate_vest(_users[9], _users[22], amount, interest_rate));
BOOST_CHECK_EQUAL(success(), delegate_vest(_users[10], _users[22], amount, 0));
check();
BOOST_TEST_MESSAGE("--- " << name{_users[22]}.to_string() << " voted");
BOOST_CHECK_EQUAL(success(), addvote(_users[22], {_users[0], "permlink4"}, cfg::_100percent));
BOOST_CHECK_EQUAL(success(), delegate_vest(delegators[6], voters[4], amount, interest_rate));
BOOST_CHECK_EQUAL(success(), delegate_vest(delegators[7], voters[4], amount, interest_rate));
BOOST_CHECK_EQUAL(success(), delegate_vest(delegators[8], voters[4], amount, 0));
check();
voter_amount = vest.get_balance_raw(_users[22])["vesting"].as<asset>();
delegator1_amount = vest.get_balance_raw(_users[8])["vesting"].as<asset>();
delegator2_amount = vest.get_balance_raw(_users[9])["vesting"].as<asset>();
delegator3_amount = vest.get_balance_raw(_users[10])["vesting"].as<asset>();
BOOST_TEST_MESSAGE("--- " << name{voters[4]}.to_string() << " voted");
BOOST_CHECK_EQUAL(success(), addvote(voters[4], {_users[0], "permlink4"}, cfg::_100percent));
auto delegator8_amount = vest.get_balance_raw(delegators[8])["vesting"].as<asset>();
produce_blocks(golos::seconds_to_blocks(post.window));
BOOST_CHECK_GT(vest.get_balance_raw(_users[22])["vesting"].as<asset>(), voter_amount);
BOOST_CHECK_GT(vest.get_balance_raw(_users[8])["vesting"].as<asset>(), delegator1_amount);
BOOST_CHECK_GT(vest.get_balance_raw(_users[9])["vesting"].as<asset>(), delegator2_amount);
BOOST_CHECK_EQUAL(vest.get_balance_raw(_users[10])["vesting"].as<asset>(), delegator3_amount);
check();
CHECK_EQUAL_WITH_DELTA(
vest.get_balance_raw(voters[4])["vesting"].as<asset>().to_real(),
_state.balances[voters[4]].vestamount,
vest_delta
);
CHECK_EQUAL_WITH_DELTA(
vest.get_balance_raw(delegators[6])["vesting"].as<asset>().to_real(),
_state.balances[delegators[6]].vestamount,
vest_delta
);
CHECK_EQUAL_WITH_DELTA(
vest.get_balance_raw(delegators[7])["vesting"].as<asset>().to_real(),
_state.balances[delegators[7]].vestamount,
vest_delta
);
BOOST_CHECK_EQUAL(vest.get_balance_raw(delegators[8])["vesting"].as<asset>(), delegator8_amount);
check();

BOOST_TEST_MESSAGE("--- add_funds_to_forum");
Expand All @@ -1232,23 +1281,22 @@ BOOST_FIXTURE_TEST_CASE(golos_delegators_test, reward_calcs_tester) try {
BOOST_TEST_MESSAGE("--- create_message: " << name{_users[0]}.to_string());
BOOST_CHECK_EQUAL(success(), create_message({_users[0], "permlink5"}));
check();
BOOST_CHECK_EQUAL(success(), delegate_vest(_users[11], _users[23], amount, cfg::_1percent));
BOOST_CHECK_EQUAL(success(), delegate_vest(_users[12], _users[23], amount, cfg::_1percent));
BOOST_CHECK_EQUAL(success(), delegate_vest(_users[13], _users[23], amount, interest_rate));
BOOST_CHECK_EQUAL(success(), delegate_vest(delegators[9], voters[5], amount, cfg::_1percent));
BOOST_CHECK_EQUAL(success(), delegate_vest(delegators[10], voters[5], amount, cfg::_1percent));
BOOST_CHECK_EQUAL(success(), delegate_vest(delegators[11], voters[5], amount, interest_rate));
check();
BOOST_TEST_MESSAGE("--- " << name{_users[23]}.to_string() << " voted");
BOOST_CHECK_EQUAL(success(), addvote(_users[23], {_users[0], "permlink5"}, cfg::_100percent));
BOOST_TEST_MESSAGE("--- " << name{voters[5]}.to_string() << " voted");
BOOST_CHECK_EQUAL(success(), addvote(voters[5], {_users[0], "permlink5"}, cfg::_100percent));
check();
voter_amount = vest.get_balance_raw(_users[23])["vesting"].as<asset>();
delegator1_amount = vest.get_balance_raw(_users[11])["vesting"].as<asset>();
delegator2_amount = vest.get_balance_raw(_users[12])["vesting"].as<asset>();
delegator3_amount = vest.get_balance_raw(_users[13])["vesting"].as<asset>();
auto delegator9_amount = vest.get_balance_raw(delegators[9])["vesting"].as<asset>();
auto delegator10_amount = vest.get_balance_raw(delegators[10])["vesting"].as<asset>();
BOOST_CHECK_EQUAL(post.get_vote(_users[0], 0)["delegators"].size(), 1);
produce_blocks(golos::seconds_to_blocks(post.window));
check();
std::vector<account_name> dlg_vec_db;
std::vector<account_name> dlg_vec_st{_users[11], _users[12], _users[13]};
std::vector<account_name> dlg_vec_st{delegators[9], delegators[10], delegators[11]};
for (auto dlg_obj : vest.get_delegators())
if (dlg_obj["delegatee"].as<account_name>() == _users[23])
if (dlg_obj["delegatee"].as<account_name>() == voters[5])
dlg_vec_db.emplace_back(dlg_obj["delegator"].as<account_name>());
BOOST_CHECK_EQUAL_COLLECTIONS(dlg_vec_st.begin(), dlg_vec_st.end(), dlg_vec_db.begin(), dlg_vec_db.end());
/*
Expand All @@ -1257,51 +1305,62 @@ BOOST_FIXTURE_TEST_CASE(golos_delegators_test, reward_calcs_tester) try {
* voter_vest_amount = 500
* effective_vesting = 500+5+5+5 = 515
*
* dlg_reward for _users[11] and _users[12]
* dlg_reward for delegators[9] and delegators[10]
* interest_rate = 100
* reward = 5 * 100 / 515 = 0.97
*
* dlg_reward for _users[13]
* dlg_reward for delegators[11]
* interest_rate = 4500
* reward = 5 * 4500 / 515 = 43.689
*
* dlg_reward will be payed only for _users[13],
* dlg_reward will be payed only for delegators[11],
* because 0.97 will be rounded to 0
*/
BOOST_CHECK_GT(vest.get_balance_raw(_users[23])["vesting"].as<asset>(), voter_amount);
BOOST_CHECK_EQUAL(vest.get_balance_raw(_users[11])["vesting"].as<asset>(), delegator1_amount);
BOOST_CHECK_EQUAL(vest.get_balance_raw(_users[12])["vesting"].as<asset>(), delegator2_amount);
BOOST_CHECK_GT(vest.get_balance_raw(_users[13])["vesting"].as<asset>(), delegator3_amount);
CHECK_EQUAL_WITH_DELTA(
vest.get_balance_raw(voters[5])["vesting"].as<asset>().to_real(),
_state.balances[voters[5]].vestamount,
vest_delta
);
BOOST_CHECK_EQUAL(vest.get_balance_raw(delegators[9])["vesting"].as<asset>(), delegator9_amount);
BOOST_CHECK_EQUAL(vest.get_balance_raw(delegators[10])["vesting"].as<asset>(), delegator10_amount);
CHECK_EQUAL_WITH_DELTA(
vest.get_balance_raw(delegators[11])["vesting"].as<asset>().to_real(),
_state.balances[delegators[11]].vestamount,
vest_delta
);
check();

BOOST_TEST_MESSAGE("--- add_funds_to_forum");
BOOST_CHECK_EQUAL(success(), add_funds_to_forum(50000));
check();

BOOST_TEST_MESSAGE("--- increase vesting amount for " << name{_users[15]}.to_string());
add_vesting({_users[15]}, vest_amount.get_amount());
BOOST_TEST_MESSAGE("--- increase vesting amount for " << name{delegators[12]}.to_string());
add_vesting({delegators[12]}, vest_amount.get_amount());

BOOST_TEST_MESSAGE("--- checking that interest_rate doesn't overflow and curator has no reward");
BOOST_TEST_MESSAGE("--- create_message: " << name{_users[0]}.to_string());
BOOST_CHECK_EQUAL(success(), create_message({_users[0], "permlink6"}));
check();
BOOST_TEST_MESSAGE("--- retire vesting for curator");
BOOST_CHECK_EQUAL(success(), retire_vest(vest.get_balance_raw(_users[24])["vesting"].as<asset>(), _users[24], _issuer));
BOOST_CHECK_EQUAL(success(), delegate_vest(_users[15], _users[24], vest_amount, cfg::_100percent));
BOOST_CHECK_EQUAL(success(), retire_vest(vest.get_balance_raw(voters[6])["vesting"].as<asset>(), voters[6], _issuer));
BOOST_CHECK_EQUAL(success(), delegate_vest(delegators[12], voters[6], vest_amount, cfg::_100percent));
check();
BOOST_TEST_MESSAGE("--- " << name{_users[24]}.to_string() << " voted");
BOOST_CHECK_EQUAL(success(), addvote(_users[24], {_users[0], "permlink6"}, cfg::_100percent));
BOOST_TEST_MESSAGE("--- " << name{voters[6]}.to_string() << " voted");
BOOST_CHECK_EQUAL(success(), addvote(voters[6], {_users[0], "permlink6"}, cfg::_100percent));
check();
voter_amount = vest.get_balance_raw(_users[24])["vesting"].as<asset>();
delegator_amount = vest.get_balance_raw(_users[15])["vesting"].as<asset>();
auto voter6_amount = vest.get_balance_raw(voters[6])["vesting"].as<asset>();
auto vote_dlg = post.get_vote(_users[0], 0)["delegators"];
BOOST_CHECK_EQUAL(vote_dlg.size(), 1);
BOOST_CHECK_EQUAL(vote_dlg[(size_t)0]["interest_rate"].as<uint16_t>(), cfg::_100percent);
produce_blocks(golos::seconds_to_blocks(post.window));

BOOST_CHECK_EQUAL(voter_amount.get_amount(), 0);
BOOST_CHECK_EQUAL(vest.get_balance_raw(_users[24])["vesting"].as<asset>(), voter_amount);
BOOST_CHECK_GT(vest.get_balance_raw(_users[15])["vesting"].as<asset>(), delegator_amount);
check();
BOOST_CHECK_EQUAL(voter6_amount.get_amount(), 0);
BOOST_CHECK_EQUAL(vest.get_balance_raw(voters[6])["vesting"].as<asset>(), voter6_amount);
CHECK_EQUAL_WITH_DELTA(
vest.get_balance_raw(delegators[12])["vesting"].as<asset>().to_real(),
_state.balances[delegators[12]].vestamount,
vest_delta
);
check();
} FC_LOG_AND_RETHROW()

Expand Down