Skip to content

Commit

Permalink
Merge pull request steemit#705 from GolosChain/692-old_bandwidth
Browse files Browse the repository at this point in the history
Remove old account_bandwidth
  • Loading branch information
afalaleev committed Jun 5, 2018
2 parents 2f43ef7 + 8cb9690 commit 621bc7c
Show file tree
Hide file tree
Showing 20 changed files with 140 additions and 243 deletions.
34 changes: 10 additions & 24 deletions libraries/api/account_api_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,38 +49,24 @@ account_api_object::account_api_object(const account_object& a, const golos::cha
json_metadata = golos::chain::to_string(meta.json_metadata);
#endif

auto old_forum = db.find<account_bandwidth_object, by_account_bandwidth_type>(
std::make_tuple(name, bandwidth_type::old_forum));
if (old_forum != nullptr) {
average_bandwidth = old_forum->average_bandwidth;
lifetime_bandwidth = old_forum->lifetime_bandwidth;
last_bandwidth_update = old_forum->last_bandwidth_update;
}

auto old_market = db.find<account_bandwidth_object, by_account_bandwidth_type>(
std::make_tuple(name, bandwidth_type::old_market));
if (old_market != nullptr) {
average_market_bandwidth = old_market->average_bandwidth;
last_market_bandwidth_update = old_market->last_bandwidth_update;
}

auto post = db.find<account_bandwidth_object, by_account_bandwidth_type>(
std::make_tuple(name, bandwidth_type::post));
auto post = db.find<account_bandwidth_object, by_account_bandwidth_type>(std::make_tuple(name, bandwidth_type::post));
if (post != nullptr) {
last_root_post = post->last_bandwidth_update;
post_bandwidth = post->average_bandwidth;
last_root_post = post->last_bandwidth_update;
}

auto forum = db.find<account_bandwidth_object, by_account_bandwidth_type>(
std::make_tuple(name, bandwidth_type::forum));
auto forum = db.find<account_bandwidth_object, by_account_bandwidth_type>(std::make_tuple(name, bandwidth_type::forum));
if (forum != nullptr) {
new_average_bandwidth = forum->average_bandwidth;
average_bandwidth = forum->average_bandwidth;
lifetime_bandwidth = forum->lifetime_bandwidth;
last_bandwidth_update = forum->last_bandwidth_update;
}

auto market = db.find<account_bandwidth_object, by_account_bandwidth_type>(
std::make_tuple(name, bandwidth_type::market));
auto market = db.find<account_bandwidth_object, by_account_bandwidth_type>(std::make_tuple(name, bandwidth_type::market));
if (market != nullptr) {
new_average_market_bandwidth = market->average_bandwidth;
average_market_bandwidth = market->average_bandwidth;
lifetime_market_bandwidth = market->lifetime_bandwidth;
last_market_bandwidth_update = market->last_bandwidth_update;
}
}

Expand Down
10 changes: 5 additions & 5 deletions libraries/api/discussion_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ namespace golos { namespace api {
std::function<void(const golos::chain::database&, discussion&)> fill_promoted_;
};

// get_discussion
// get_discussion
discussion discussion_helper::impl::get_discussion(const comment_object& c, uint32_t vote_limit) const {
discussion d = create_discussion(c);
set_url(d);
Expand All @@ -100,9 +100,9 @@ namespace golos { namespace api {
return d;
}

discussion discussion_helper::get_discussion(const comment_object& c, uint32_t vote_limit) const {
discussion discussion_helper::get_discussion(const comment_object& c, uint32_t vote_limit) const {
return pimpl->get_discussion(c, vote_limit);
}
}
//

// select_active_votes
Expand Down Expand Up @@ -202,7 +202,7 @@ namespace golos { namespace api {
void discussion_helper::set_url(discussion& d) const {
pimpl->set_url(d);
}
//
//
// create_discussion
discussion discussion_helper::impl::create_discussion(const comment_object& o) const {
return discussion(o, database_);
Expand All @@ -218,7 +218,7 @@ namespace golos { namespace api {
std::function<void(const golos::chain::database&, discussion&)> fill_promoted
) {
pimpl = std::make_unique<impl>(db, fill_reputation, fill_promoted);
}
}

discussion_helper::~discussion_helper() = default;

Expand Down
19 changes: 9 additions & 10 deletions libraries/api/include/golos/api/account_api_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,18 @@ struct account_api_object {

uint16_t witnesses_voted_for;

share_type average_bandwidth = 0;
share_type lifetime_bandwidth = 0;
share_type average_bandwidth;
share_type average_market_bandwidth;
share_type lifetime_bandwidth;
share_type lifetime_market_bandwidth;
time_point_sec last_bandwidth_update;

share_type average_market_bandwidth = 0;
time_point_sec last_market_bandwidth_update;
time_point_sec last_post;
time_point_sec last_root_post;
share_type post_bandwidth = STEEMIT_100_PERCENT;

share_type new_average_bandwidth;
share_type new_average_market_bandwidth;
set<string> witness_votes;

fc::optional<share_type> reputation;
};

Expand All @@ -110,9 +108,10 @@ FC_REFLECT((golos::api::account_api_object),
(savings_sbd_seconds)(savings_sbd_seconds_last_update)(savings_sbd_last_interest_payment)
(savings_withdraw_requests)(vesting_shares)(delegated_vesting_shares)(received_vesting_shares)
(vesting_withdraw_rate)(next_vesting_withdrawal)(withdrawn)(to_withdraw)(withdraw_routes)
(curation_rewards)(posting_rewards)(proxied_vsf_votes)(witnesses_voted_for)(average_bandwidth)
(lifetime_bandwidth)(last_bandwidth_update)(average_market_bandwidth)(last_market_bandwidth_update)
(last_post)(last_root_post)(post_bandwidth)(new_average_bandwidth)(new_average_market_bandwidth)
(curation_rewards)(posting_rewards)(proxied_vsf_votes)(witnesses_voted_for)
(average_bandwidth)(average_market_bandwidth)(lifetime_bandwidth)(lifetime_market_bandwidth)
(last_bandwidth_update)(last_market_bandwidth_update)
(last_post)(last_root_post)(post_bandwidth)
(witness_votes)(reputation))

#endif //GOLOS_ACCOUNT_API_OBJ_HPP
6 changes: 3 additions & 3 deletions libraries/api/include/golos/api/discussion_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ namespace golos { namespace api {
golos::chain::database& db,
std::function<void(const golos::chain::database&, const account_name_type&, fc::optional<share_type>&)> fill_reputation,
std::function<void(const golos::chain::database&, discussion&)> fill_promoted);
~discussion_helper() ;
~discussion_helper();


void set_pending_payout(discussion& d) const;

void set_url(discussion& d) const;

void select_active_votes(
std::vector<vote_state>& result, uint32_t& total_count,
const std::string& author, const std::string& permlink, uint32_t limit
) const ;
) const;

discussion create_discussion(const comment_object& o) const;

Expand Down
111 changes: 23 additions & 88 deletions libraries/chain/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ namespace golos { namespace chain {
return block_id_type();
}

// Reversible blocks are *usually* in the TAPOS buffer. Since this
// Reversible blocks are *usually* in the TAPOS buffer. Since this
// is the fastest check, we do it first.
block_summary_id_type bsid = block_num & 0xFFFF;
const block_summary_object *bs = find<block_summary_object, by_id>(bsid);
Expand All @@ -355,7 +355,7 @@ namespace golos { namespace chain {
}
}

// Next we query the block log. Irreversible blocks are here.
// Next we query the block log. Irreversible blocks are here.

auto b = _block_log.read_block_by_num(block_num);
if (b.valid()) {
Expand Down Expand Up @@ -581,69 +581,12 @@ namespace golos { namespace chain {
adjust_supply(-fee);
}

void database::old_update_account_bandwidth(const account_object &a, uint32_t trx_size, const bandwidth_type type) {
try {
const auto &props = get_dynamic_global_properties();
if (props.total_vesting_shares.amount > 0) {
FC_ASSERT(a.vesting_shares.amount >
0, "Only accounts with a postive vesting balance may transact.");

auto band = find<account_bandwidth_object, by_account_bandwidth_type>(boost::make_tuple(a.name, type));

if (band == nullptr) {
band = &create<account_bandwidth_object>([&](account_bandwidth_object &b) {
b.account = a.name;
b.type = type;
});
}

modify(*band, [&](account_bandwidth_object &b) {
b.lifetime_bandwidth +=
trx_size * STEEMIT_BANDWIDTH_PRECISION;

auto now = head_block_time();
auto delta_time = (now -
b.last_bandwidth_update).to_seconds();
uint64_t N = trx_size * STEEMIT_BANDWIDTH_PRECISION;
if (delta_time >=
STEEMIT_BANDWIDTH_AVERAGE_WINDOW_SECONDS) {
b.average_bandwidth = N;
} else {
auto old_weight = b.average_bandwidth *
(STEEMIT_BANDWIDTH_AVERAGE_WINDOW_SECONDS -
delta_time);
auto new_weight = delta_time * N;
b.average_bandwidth = (old_weight + new_weight) /
STEEMIT_BANDWIDTH_AVERAGE_WINDOW_SECONDS;
}

b.last_bandwidth_update = now;
});

fc::uint128_t account_vshares(a.vesting_shares.amount.value);
fc::uint128_t total_vshares(props.total_vesting_shares.amount.value);

fc::uint128_t account_average_bandwidth(band->average_bandwidth.value);
fc::uint128_t max_virtual_bandwidth(props.max_virtual_bandwidth);

FC_ASSERT((account_vshares * max_virtual_bandwidth) >
(account_average_bandwidth * total_vshares),
"Account exceeded maximum allowed bandwidth per vesting share.",
("account_vshares", account_vshares)
("account_average_bandwidth", account_average_bandwidth)
("max_virtual_bandwidth", max_virtual_bandwidth)
("total_vesting_shares", total_vshares));
}
} FC_CAPTURE_AND_RETHROW()
}

bool database::update_account_bandwidth(const account_object &a, uint32_t trx_size, const bandwidth_type type) {
const auto &props = get_dynamic_global_properties();
bool has_bandwidth = true;

if (props.total_vesting_shares.amount > 0) {
auto band = find<account_bandwidth_object, by_account_bandwidth_type>(boost::make_tuple(a.name, type));

if (band == nullptr) {
band = &create<account_bandwidth_object>([&](account_bandwidth_object &b) {
b.account = a.name;
Expand All @@ -652,11 +595,8 @@ namespace golos { namespace chain {
}

share_type new_bandwidth;
share_type trx_bandwidth =
trx_size * STEEMIT_BANDWIDTH_PRECISION;
auto delta_time = (head_block_time() -
band->last_bandwidth_update).to_seconds();

share_type trx_bandwidth = trx_size * STEEMIT_BANDWIDTH_PRECISION;
auto delta_time = (head_block_time() - band->last_bandwidth_update).to_seconds();
if (delta_time > STEEMIT_BANDWIDTH_AVERAGE_WINDOW_SECONDS) {
new_bandwidth = 0;
} else {
Expand All @@ -681,16 +621,14 @@ namespace golos { namespace chain {
fc::uint128_t account_average_bandwidth(band->average_bandwidth.value);
fc::uint128_t max_virtual_bandwidth(props.max_virtual_bandwidth);

has_bandwidth = (account_vshares * max_virtual_bandwidth) >
(account_average_bandwidth * total_vshares);
has_bandwidth = (account_vshares * max_virtual_bandwidth) > (account_average_bandwidth * total_vshares);

if (is_producing())
FC_ASSERT(has_bandwidth,
"Account exceeded maximum allowed bandwidth per vesting share.",
("account_vshares", account_vshares)
("account_average_bandwidth", account_average_bandwidth)
("max_virtual_bandwidth", max_virtual_bandwidth)
("total_vesting_shares", total_vshares));
FC_ASSERT(has_bandwidth, "Account exceeded maximum allowed bandwidth per vesting share.",
("account_vshares", account_vshares)
("account_average_bandwidth", account_average_bandwidth)
("max_virtual_bandwidth", max_virtual_bandwidth)
("total_vesting_shares", total_vshares));
}

return has_bandwidth;
Expand Down Expand Up @@ -772,7 +710,7 @@ namespace golos { namespace chain {
}

/**
* Push block "may fail" in which case every partial change is unwound. After
* Push block "may fail" in which case every partial change is unwound. After
* push block is successful the block is appended to the chain database on disk.
*
* @return true if we switched forks as a result of this push.
Expand Down Expand Up @@ -934,7 +872,7 @@ namespace golos { namespace chain {

// Create a temporary undo session as a child of _pending_tx_session.
// The temporary session will be discarded by the destructor if
// _apply_transaction fails. If we make it to merge(), we
// _apply_transaction fails. If we make it to merge(), we
// apply the changes.

auto temp_session = start_undo_session();
Expand Down Expand Up @@ -996,7 +934,7 @@ namespace golos { namespace chain {
// This rebuild is necessary because pending transactions' validity
// and semantics may have changed since they were received, because
// time-based semantics are evaluated based on the current block
// time. These changes can only be reflected in the database when
// time. These changes can only be reflected in the database when
// the value of the "when" variable is known, which means we need to
// re-apply pending transactions in this method.
//
Expand Down Expand Up @@ -1088,7 +1026,7 @@ namespace golos { namespace chain {
pending_block.sign(block_signing_private_key);
}

// TODO: Move this to _push_block() so session is restored.
// TODO: Move this to _push_block() so session is restored.
if (!(skip & skip_block_size_check)) {
FC_ASSERT(fc::raw::pack_size(pending_block) <= STEEMIT_MAX_BLOCK_SIZE);
}
Expand Down Expand Up @@ -1267,7 +1205,7 @@ namespace golos { namespace chain {
* The ratio of total_vesting_shares / total_vesting_fund_steem should not
* change as the result of the user adding funds
*
* V / C = (V+Vn) / (C+Cn)
* V / C = (V+Vn) / (C+Cn)
*
* Simplifies to Vn = (V * Cn ) / C
*
Expand Down Expand Up @@ -1963,7 +1901,7 @@ namespace golos { namespace chain {
* Let V = total vesting shares
* Let v = total vesting shares being cashed out
*
* The user may withdraw vT / V tokens
* The user may withdraw vT / V tokens
*/
share_type to_withdraw;
if (from_account.to_withdraw - from_account.withdrawn <
Expand Down Expand Up @@ -2269,7 +2207,7 @@ namespace golos { namespace chain {

void database::process_comment_cashout() {
/// don't allow any content to get paid out until the website is ready to launch
/// and people have had a week to start posting. The first cashout will be the biggest because it
/// and people have had a week to start posting. The first cashout will be the biggest because it
/// will represent 2+ months of rewards.
// if (!has_hardfork(STEEMIT_FIRST_CASHOUT_TIME)) {
// return;
Expand Down Expand Up @@ -3448,7 +3386,7 @@ namespace golos { namespace chain {
}
}
else {
if ( wit.last_sbd_exchange_update < now + STEEMIT_MAX_FEED_AGE && !wit.sbd_exchange_rate.is_null() ) {
if ( wit.last_sbd_exchange_update < now + STEEMIT_MAX_FEED_AGE && !wit.sbd_exchange_rate.is_null() ) {

feeds.push_back(wit.sbd_exchange_rate);
}
Expand Down Expand Up @@ -3525,14 +3463,11 @@ namespace golos { namespace chain {

auto trx_size = fc::raw::pack_size(trx);

for (const auto &auth : required) {
const auto &acnt = get_account(auth);

old_update_account_bandwidth(acnt, trx_size, bandwidth_type::old_forum);
for (const auto& auth : required) {
const auto& acnt = get_account(auth);
update_account_bandwidth(acnt, trx_size, bandwidth_type::forum);
for (const auto &op : trx.operations) {
for (const auto& op : trx.operations) {
if (is_market_operation(op)) {
old_update_account_bandwidth(acnt, trx_size, bandwidth_type::old_market);
update_account_bandwidth(acnt, trx_size * 10, bandwidth_type::market);
break;
}
Expand Down Expand Up @@ -3663,7 +3598,7 @@ namespace golos { namespace chain {
* their capacity.
*
* When the reserve ratio is at its max (check STEEMIT_MAX_RESERVE_RATIO) a 50%
* reduction will take 3 to 4 days to return back to maximum. When it is at its
* reduction will take 3 to 4 days to return back to maximum. When it is at its
* minimum it will return back to its prior level in just a few minutes.
*
* If the network reserve ratio falls under 100 then it is probably time to
Expand Down Expand Up @@ -3974,7 +3909,7 @@ namespace golos { namespace chain {
});
/**
* There are times when the AMOUNT_FOR_SALE * SALE_PRICE == 0 which means that we
* have hit the limit where the seller is asking for nothing in return. When this
* have hit the limit where the seller is asking for nothing in return. When this
* happens we must refund any balance back to the seller, it is too small to be
* sold at the sale price.
*/
Expand Down
Loading

0 comments on commit 621bc7c

Please sign in to comment.