Skip to content

Commit

Permalink
Merge pull request steemit#713 from steemit/563-bandwidth-algorithm
Browse files Browse the repository at this point in the history
563 bandwidth algorithm
  • Loading branch information
bytemaster authored Jan 5, 2017
2 parents ab67737 + a23e51a commit 9ce63c6
Show file tree
Hide file tree
Showing 12 changed files with 261 additions and 137 deletions.
18 changes: 14 additions & 4 deletions libraries/app/database_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ vector< extended_account > database_api_impl::get_accounts( vector< string > nam
auto itr = idx.find( name );
if ( itr != idx.end() )
{
results.push_back( extended_account( *itr, _db.get< account_authority_object, by_account >( itr->name ) ) );
results.push_back( extended_account( *itr, _db ) );

if( _follow_api )
{
Expand Down Expand Up @@ -470,7 +470,7 @@ vector<optional<account_api_obj>> database_api_impl::lookup_account_names(const

if( itr )
{
result.push_back( account_api_obj( *itr, _db.get< account_authority_object, by_account >( name ) ) );
result.push_back( account_api_obj( *itr, _db ) );
}
else
{
Expand Down Expand Up @@ -619,6 +619,16 @@ vector< withdraw_route > database_api::get_withdraw_routes( string account, with
});
}

optional< account_bandwidth_api_obj > database_api::get_account_bandwidth( string account, bandwidth_type type )const
{
optional< account_bandwidth_api_obj > result;
auto band = my->_db.find< account_bandwidth_object, by_account_bandwidth_type >( boost::make_tuple( account, type ) );
if( band != nullptr )
result = *band;

return result;
}

//////////////////////////////////////////////////////////////////////
// //
// Witnesses //
Expand Down Expand Up @@ -1889,7 +1899,7 @@ state database_api::get_state( string path )const

if( part[0].size() && part[0][0] == '@' ) {
auto acnt = part[0].substr(1);
_state.accounts[acnt] = extended_account( my->_db.get_account(acnt), my->_db.get< account_authority_object, by_account >(acnt) );
_state.accounts[acnt] = extended_account( my->_db.get_account(acnt), my->_db );
if( my->_follow_api )
{
_state.accounts[acnt].reputation = my->_follow_api->get_account_reputations( acnt, 1 )[0].reputation;
Expand Down Expand Up @@ -2203,7 +2213,7 @@ state database_api::get_state( string path )const
for( const auto& a : accounts )
{
_state.accounts.erase("");
_state.accounts[a] = extended_account( my->_db.get_account( a ), my->_db.get< account_authority_object, by_account >( a ) );
_state.accounts[a] = extended_account( my->_db.get_account( a ), my->_db );
if( my->_follow_api )
{
_state.accounts[a].reputation = my->_follow_api->get_account_reputations( a, 1 )[0].reputation;
Expand Down
3 changes: 3 additions & 0 deletions libraries/app/include/steemit/app/database_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ class database_api

vector< withdraw_route > get_withdraw_routes( string account, withdraw_route_type type = outgoing )const;

optional< account_bandwidth_api_obj > get_account_bandwidth( string account, bandwidth_type type )const;

vector< savings_withdraw_api_obj > get_savings_withdraw_from( string account )const;
vector< savings_withdraw_api_obj > get_savings_withdraw_to( string account )const;

Expand Down Expand Up @@ -502,6 +504,7 @@ FC_API(steemit::app::database_api,
(get_recovery_request)
(get_escrow)
(get_withdraw_routes)
(get_account_bandwidth)
(get_savings_withdraw_from)
(get_savings_withdraw_to)

Expand Down
2 changes: 1 addition & 1 deletion libraries/app/include/steemit/app/state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ namespace steemit { namespace app {
struct extended_account : public account_api_obj
{
extended_account(){}
extended_account( const account_object& a, const account_authority_object& auth ):account_api_obj( a, auth ){}
extended_account( const account_object& a, const database& db ):account_api_obj( a, db ){}

asset vesting_balance; /// convert vesting_shares to vesting steem
share_type reputation = 0;
Expand Down
68 changes: 51 additions & 17 deletions libraries/app/include/steemit/app/steem_api_objects.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ typedef chain::withdraw_vesting_route_object withdraw_vesting_route_ap
typedef chain::decline_voting_rights_request_object decline_voting_rights_request_api_obj;
typedef chain::witness_vote_object witness_vote_api_obj;
typedef chain::witness_schedule_object witness_schedule_api_obj;
typedef chain::account_bandwidth_object account_bandwidth_api_obj;

struct comment_api_obj
{
Expand Down Expand Up @@ -184,16 +185,12 @@ struct tag_api_obj

struct account_api_obj
{
account_api_obj( const chain::account_object& a, const chain::account_authority_object& auth ) :
account_api_obj( const chain::account_object& a, const chain::database& db ) :
id( a.id ),
name( a.name ),
owner( authority( auth.owner ) ),
active( authority( auth.active ) ),
posting( authority( auth.posting ) ),
memo_key( a.memo_key ),
json_metadata( to_string( a.json_metadata ) ),
proxy( a.proxy ),
last_owner_update( auth.last_owner_update ),
last_account_update( a.last_account_update ),
created( a.created ),
mined( a.mined ),
Expand Down Expand Up @@ -231,18 +228,51 @@ struct account_api_obj
withdraw_routes( a.withdraw_routes ),
proxied_vsf_votes( a.proxied_vsf_votes.size() ),
witnesses_voted_for( a.witnesses_voted_for ),
average_bandwidth( a.average_bandwidth ),
lifetime_bandwidth( a.lifetime_bandwidth ),
last_bandwidth_update( a.last_bandwidth_update ),
average_market_bandwidth( a.average_market_bandwidth ),
last_market_bandwidth_update( a.last_market_bandwidth_update ),
last_post( a.last_post ),
last_root_post( a.last_root_post ),
post_bandwidth( a.post_bandwidth )
last_post( a.last_post )
{
size_t n = a.proxied_vsf_votes.size();
for( size_t i=0; i<n; i++ )
proxied_vsf_votes.push_back( a.proxied_vsf_votes[i] );

const auto& auth = db.get< account_authority_object, by_account >( name );
owner = authority( auth.owner );
active = authority( auth.active );
posting = authority( auth.posting );
last_owner_update = auth.last_owner_update;

auto old_forum = db.find< account_bandwidth_object, by_account_bandwidth_type >( boost::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 >( boost::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 >( boost::make_tuple( name, bandwidth_type::post ) );
if( post != nullptr )
{
last_root_post = post->last_bandwidth_update;
post_bandwidth = post->average_bandwidth;
}

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

auto market = db.find< account_bandwidth_object, by_account_bandwidth_type >( boost::make_tuple( name, bandwidth_type::market ) );
if( market != nullptr )
{
new_average_market_bandwidth = market->average_bandwidth;
}
}


Expand Down Expand Up @@ -307,15 +337,18 @@ struct account_api_obj

uint16_t witnesses_voted_for;

uint64_t average_bandwidth;
uint64_t lifetime_bandwidth;
share_type average_bandwidth = 0;
share_type lifetime_bandwidth = 0;
time_point_sec last_bandwidth_update;

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

share_type new_average_bandwidth;
share_type new_average_market_bandwidth;
};

struct owner_authority_history_api_obj
Expand Down Expand Up @@ -478,6 +511,7 @@ FC_REFLECT( steemit::app::account_api_obj,
(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)
)

FC_REFLECT( steemit::app::owner_authority_history_api_obj,
Expand Down
Loading

0 comments on commit 9ce63c6

Please sign in to comment.