Skip to content

Commit

Permalink
Update reserve ratio algorithm steemit#1257
Browse files Browse the repository at this point in the history
  • Loading branch information
mvandeberg committed Jul 18, 2017
1 parent 46f7276 commit e8fffaf
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 80 deletions.
2 changes: 1 addition & 1 deletion libraries/app/database_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ price database_api::get_current_median_history_price()const

dynamic_global_property_api_obj database_api_impl::get_dynamic_global_properties()const
{
return _db.get(dynamic_global_property_id_type());
return dynamic_global_property_api_obj( _db.get( dynamic_global_property_id_type() ), _db );
}

witness_schedule_api_obj database_api::get_witness_schedule()const
Expand Down
31 changes: 30 additions & 1 deletion libraries/app/include/steemit/app/steem_api_objects.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ using namespace steemit::chain;
typedef chain::change_recovery_account_request_object change_recovery_account_request_api_obj;
typedef chain::block_summary_object block_summary_api_obj;
typedef chain::comment_vote_object comment_vote_api_obj;
typedef chain::dynamic_global_property_object dynamic_global_property_api_obj;
typedef chain::convert_request_object convert_request_api_obj;
typedef chain::escrow_object escrow_api_obj;
typedef chain::liquidity_reward_balance_object liquidity_reward_balance_api_obj;
Expand Down Expand Up @@ -478,6 +477,30 @@ struct signed_block_api_obj : public signed_block
vector< transaction_id_type > transaction_ids;
};

struct dynamic_global_property_api_obj : public dynamic_global_property_object
{
dynamic_global_property_api_obj( const dynamic_global_property_object& gpo, const chain::database& db ) :
dynamic_global_property_object( gpo )
{
if( db.has_index< witness::reserve_ratio_index >() )
{
const auto& r = db.get( witness::reserve_ratio_id_type() );
current_reserve_ratio = r.current_reserve_ratio;
average_block_size = r.average_block_size;
max_virtual_bandwidth = r.max_virtual_bandwidth;
}
}

dynamic_global_property_api_obj( const dynamic_global_property_object& gpo ) :
dynamic_global_property_object( gpo ) {}

dynamic_global_property_api_obj() {}

uint32_t current_reserve_ratio = 1;
uint64_t average_block_size = 0;
uint64_t max_virtual_bandwidth = 0;
};

} } // steemit::app

FC_REFLECT( steemit::app::comment_api_obj,
Expand Down Expand Up @@ -568,3 +591,9 @@ FC_REFLECT_DERIVED( steemit::app::signed_block_api_obj, (steemit::protocol::sign
(signing_key)
(transaction_ids)
)

FC_REFLECT_DERIVED( steemit::app::dynamic_global_property_api_obj, (steemit::chain::dynamic_global_property_object),
(current_reserve_ratio)
(average_block_size)
(max_virtual_bandwidth)
)
38 changes: 1 addition & 37 deletions libraries/chain/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2929,7 +2929,6 @@ void database::create_block_summary(const signed_block& next_block)

void database::update_global_dynamic_data( const signed_block& b )
{ try {
auto block_size = fc::raw::pack_size(b);
const dynamic_global_property_object& _dgp =
get_dynamic_global_properties();

Expand Down Expand Up @@ -2975,41 +2974,6 @@ void database::update_global_dynamic_data( const signed_block& b )
dgp.head_block_id = b.id();
dgp.time = b.timestamp;
dgp.current_aslot += missed_blocks+1;
dgp.average_block_size = (99 * dgp.average_block_size + block_size)/100;

/**
* About once per minute the average network use is consulted and used to
* adjust the reserve ratio. Anything above 50% usage reduces the ratio by
* half which should instantly bring the network from 50% to 25% use unless
* the demand comes from users who have surplus capacity. In other words,
* a 50% reduction in reserve ratio does not result in a 50% reduction in usage,
* it will only impact users who where attempting to use more than 50% of their
* capacity.
*
* When the reserve ratio is at its max (10,000) a 50% 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
* increase the capacity of the network.
*/
if( dgp.head_block_number % 20 == 0 )
{
if( ( !has_hardfork( STEEMIT_HARDFORK_0_12__179 ) && dgp.average_block_size > dgp.maximum_block_size / 2 ) ||
( has_hardfork( STEEMIT_HARDFORK_0_12__179 ) && dgp.average_block_size > dgp.maximum_block_size / 4 ) )
{
dgp.current_reserve_ratio /= 2; /// exponential back up
}
else
{ /// linear growth... not much fine grain control near full capacity
dgp.current_reserve_ratio++;
}

if( has_hardfork( STEEMIT_HARDFORK_0_2 ) && dgp.current_reserve_ratio > STEEMIT_MAX_RESERVE_RATIO )
dgp.current_reserve_ratio = STEEMIT_MAX_RESERVE_RATIO;
}
dgp.max_virtual_bandwidth = (dgp.maximum_block_size * dgp.current_reserve_ratio *
STEEMIT_BANDWIDTH_PRECISION * STEEMIT_BANDWIDTH_AVERAGE_WINDOW_SECONDS) / STEEMIT_BLOCK_INTERVAL;
} );

if( !(get_node_properties().skip_flags & skip_undo_history_check) )
Expand Down Expand Up @@ -3373,7 +3337,7 @@ void database::adjust_balance( const account_object& a, const asset& delta )
acnt.sbd_balance += interest_paid;
acnt.sbd_seconds = 0;
acnt.sbd_last_interest_payment = head_block_time();

if(interest > 0)
push_virtual_operation( interest_operation( a.name, interest_paid ) );

Expand Down
32 changes: 0 additions & 32 deletions libraries/chain/include/steemit/chain/global_property_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,6 @@ namespace steemit { namespace chain {

uint16_t sbd_print_rate = STEEMIT_100_PERCENT;

/**
* Average block size is updated every block to be:
*
* average_block_size = (99 * average_block_size + new_block_size) / 100
*
* This property is used to update the current_reserve_ratio to maintain approximately
* 50% or less utilization of network capacity.
*/
uint32_t average_block_size = 0;

/**
* Maximum block size is decided by the set of active witnesses which change every round.
* Each witness posts what they think the maximum size should be as part of their witness
Expand All @@ -117,25 +107,6 @@ namespace steemit { namespace chain {

uint32_t last_irreversible_block_num = 0;

/**
* The maximum bandwidth the blockchain can support is:
*
* max_bandwidth = maximum_block_size * STEEMIT_BANDWIDTH_AVERAGE_WINDOW_SECONDS / STEEMIT_BLOCK_INTERVAL
*
* The maximum virtual bandwidth is:
*
* max_bandwidth * current_reserve_ratio
*/
uint64_t max_virtual_bandwidth = 0;

/**
* Any time average_block_size <= 50% maximum_block_size this value grows by 1 until it
* reaches STEEMIT_MAX_RESERVE_RATIO. Any time average_block_size is greater than
* 50% it falls by 1%. Upward adjustments happen once per round, downward adjustments
* happen every block.
*/
uint64_t current_reserve_ratio = 1;

/**
* The number of votes regenerated per day. Any user voting slower than this rate will be
* "wasting" voting power through spillover; any user voting faster than this rate will have
Expand Down Expand Up @@ -176,14 +147,11 @@ FC_REFLECT( steemit::chain::dynamic_global_property_object,
(pending_rewarded_vesting_steem)
(sbd_interest_rate)
(sbd_print_rate)
(average_block_size)
(maximum_block_size)
(current_aslot)
(recent_slots_filled)
(participation_count)
(last_irreversible_block_num)
(max_virtual_bandwidth)
(current_reserve_ratio)
(vote_power_reserve_rate)
)
CHAINBASE_SET_INDEX_TYPE( steemit::chain::dynamic_global_property_object, steemit::chain::dynamic_global_property_index )
1 change: 0 additions & 1 deletion libraries/plugins/block_info/block_info_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ void block_info_plugin::on_applied_block( const chain::signed_block& b )

info.block_id = b.id();
info.block_size = fc::raw::pack_size( b );
info.average_block_size = dgpo.average_block_size;
info.aslot = dgpo.current_aslot;
info.last_irreversible_block_num = dgpo.last_irreversible_block_num;
info.num_pow_witnesses = dgpo.num_pow_witnesses;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ struct block_info
{
chain::block_id_type block_id;
uint32_t block_size = 0;
uint32_t average_block_size = 0;
uint64_t aslot = 0;
uint32_t last_irreversible_block_num = 0;
uint32_t num_pow_witnesses = 0;
Expand All @@ -26,7 +25,6 @@ struct block_with_info
FC_REFLECT( steemit::plugin::block_info::block_info,
(block_id)
(block_size)
(average_block_size)
(aslot)
(last_irreversible_block_num)
(num_pow_witnesses)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ using namespace steemit::chain;
enum witness_plugin_object_type
{
account_bandwidth_object_type = ( WITNESS_SPACE_ID << 8 ),
content_edit_lock_object_type = ( WITNESS_SPACE_ID << 8 ) + 1
content_edit_lock_object_type = ( WITNESS_SPACE_ID << 8 ) + 1,
reserve_ratio_object_type = ( WITNESS_SPACE_ID << 8 ) + 2
};

enum bandwidth_type
Expand Down Expand Up @@ -48,6 +49,7 @@ class account_bandwidth_object : public object< account_bandwidth_object_type, a

typedef oid< account_bandwidth_object > account_bandwidth_id_type;


class content_edit_lock_object : public object< content_edit_lock_object_type, content_edit_lock_object >
{
public:
Expand All @@ -66,6 +68,53 @@ class content_edit_lock_object : public object< content_edit_lock_object_type, c

typedef oid< content_edit_lock_object > content_edit_lock_id_type;


class reserve_ratio_object : public object< reserve_ratio_object_type, reserve_ratio_object >
{
public:
template< typename Constructor, typename Allocator >
reserve_ratio_object( Constructor&& c, allocator< Allocator > a )
{
c( *this );
}

reserve_ratio_object() {}

id_type id;

/**
* Average block size is updated every block to be:
*
* average_block_size = (99 * average_block_size + new_block_size) / 100
*
* This property is used to update the current_reserve_ratio to maintain approximately
* 50% or less utilization of network capacity.
*/
int32_t average_block_size = 0;

/**
* Any time average_block_size <= 50% maximum_block_size this value grows by 1 until it
* reaches STEEMIT_MAX_RESERVE_RATIO. Any time average_block_size is greater than
* 50% it falls by 1%. Upward adjustments happen once per round, downward adjustments
* happen every block.
*/
int64_t current_reserve_ratio = 1;

/**
* The maximum bandwidth the blockchain can support is:
*
* max_bandwidth = maximum_block_size * STEEMIT_BANDWIDTH_AVERAGE_WINDOW_SECONDS / STEEMIT_BLOCK_INTERVAL
*
* The maximum virtual bandwidth is:
*
* max_bandwidth * current_reserve_ratio
*/
uint64_t max_virtual_bandwidth = 0;
};

typedef oid< reserve_ratio_object > reserve_ratio_id_type;


struct by_account_bandwidth_type;

typedef multi_index_container <
Expand Down Expand Up @@ -96,6 +145,15 @@ typedef multi_index_container <
allocator< content_edit_lock_object >
> content_edit_lock_index;

typedef multi_index_container <
reserve_ratio_object,
indexed_by <
ordered_unique< tag< by_id >,
member< reserve_ratio_object, reserve_ratio_id_type, &reserve_ratio_object::id > >
>,
allocator< reserve_ratio_object >
> reserve_ratio_index;

} } // steemit::witness

FC_REFLECT_ENUM( steemit::witness::bandwidth_type, (post)(forum)(market) )
Expand All @@ -107,3 +165,7 @@ CHAINBASE_SET_INDEX_TYPE( steemit::witness::account_bandwidth_object, steemit::w
FC_REFLECT( steemit::witness::content_edit_lock_object,
(id)(account)(lock_time) )
CHAINBASE_SET_INDEX_TYPE( steemit::witness::content_edit_lock_object, steemit::witness::content_edit_lock_index )

FC_REFLECT( steemit::witness::reserve_ratio_object,
(id)(average_block_size)(current_reserve_ratio)(max_virtual_bandwidth) )
CHAINBASE_SET_INDEX_TYPE( steemit::witness::reserve_ratio_object, steemit::witness::reserve_ratio_index )
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@

#include <fc/thread/future.hpp>

#define RESERVE_RATIO_PRECISION (10000ll)
#define RESERVE_RATIO_MIN_INCREMENT (5000ll)

namespace steemit { namespace witness {

using std::string;
Expand Down
Loading

0 comments on commit e8fffaf

Please sign in to comment.