Skip to content

Commit

Permalink
WIP, SON related operations, cli_wallet commands and RPC
Browse files Browse the repository at this point in the history
- Updating global_property_object
  • Loading branch information
obucina committed Oct 2, 2019
1 parent 44f185e commit 457c8b9
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 2 deletions.
3 changes: 1 addition & 2 deletions libraries/chain/db_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ void database::initialize_indexes()
acnt_index->add_secondary_index<account_referrer_index>();

add_index< primary_index<committee_member_index> >();
add_index< primary_index<son_index> >();
add_index< primary_index<witness_index> >();
add_index< primary_index<limit_order_index > >();
add_index< primary_index<call_order_index > >();
Expand Down Expand Up @@ -304,8 +305,6 @@ void database::initialize_indexes()
//add_index< primary_index<distributed_dividend_balance_object_index > >();
add_index< primary_index<pending_dividend_payout_balance_for_holder_object_index > >();
add_index< primary_index<total_distributed_dividend_balance_object_index > >();

add_index< primary_index<son_index> >();
}

void database::init_genesis(const genesis_state_type& genesis_state)
Expand Down
105 changes: 105 additions & 0 deletions libraries/chain/db_maint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,96 @@ void database::update_active_committee_members()
});
} FC_CAPTURE_AND_RETHROW() }

void database::update_active_sons()
{ try {
assert( _sons_count_histogram_buffer.size() > 0 );
share_type stake_target = (_total_voting_stake-_witness_count_histogram_buffer[0]) / 2;

/// accounts that vote for 0 or 1 son do not get to express an opinion on
/// the number of sons to have (they abstain and are non-voting accounts)

share_type stake_tally = 0;

size_t son_count = 0;
if( stake_target > 0 )
{
while( (son_count < _sons_count_histogram_buffer.size() - 1)
&& (stake_tally <= stake_target) )
{
stake_tally += _sons_count_histogram_buffer[++son_count];
}
}

const chain_property_object& cpo = get_chain_properties();
auto sons = sort_votable_objects<son_index>(std::max(son_count*2+1, (size_t)cpo.immutable_parameters.min_son_count));

const global_property_object& gpo = get_global_properties();

const auto& all_sons = get_index_type<son_index>().indices();

for( const son_object& son : all_sons )
{
modify( son, [&]( son_object& obj ){
obj.total_votes = _vote_tally_buffer[son.vote_id];
});
}

// Update SON authority
modify( get(GRAPHENE_SON_ACCOUNT_ID), [&]( account_object& a )
{
if( head_block_time() < HARDFORK_533_TIME )
{
uint64_t total_votes = 0;
map<account_id_type, uint64_t> weights;
a.active.weight_threshold = 0;
a.active.clear();

for( const son_object& son : sons )
{
weights.emplace(son.son_member_account, _vote_tally_buffer[son.vote_id]);
total_votes += _vote_tally_buffer[son.vote_id];
}

// total_votes is 64 bits. Subtract the number of leading low bits from 64 to get the number of useful bits,
// then I want to keep the most significant 16 bits of what's left.
int8_t bits_to_drop = std::max(int(boost::multiprecision::detail::find_msb(total_votes)) - 15, 0);
for( const auto& weight : weights )
{
// Ensure that everyone has at least one vote. Zero weights aren't allowed.
uint16_t votes = std::max((weight.second >> bits_to_drop), uint64_t(1) );
a.active.account_auths[weight.first] += votes;
a.active.weight_threshold += votes;
}

a.active.weight_threshold /= 2;
a.active.weight_threshold += 1;
}
else
{
vote_counter vc;
for( const son_object& son : sons )
vc.add( son.son_member_account, std::max(_vote_tally_buffer[son.vote_id], UINT64_C(1)) );
vc.finish( a.active );
}
} );

modify(gpo, [&]( global_property_object& gp ){
gp.active_sons.clear();
gp.active_sons.reserve(sons.size());
std::transform(sons.begin(), sons.end(),
std::inserter(gp.active_sons, gp.active_sons.end()),
[](const son_object& s) {
return s.id;
});
});

//const witness_schedule_object& wso = witness_schedule_id_type()(*this);
//modify(wso, [&](witness_schedule_object& _wso)
//{
// _wso.scheduler.update(gpo.active_witnesses);
//});
} FC_CAPTURE_AND_RETHROW() }

void database::initialize_budget_record( fc::time_point_sec now, budget_record& rec )const
{
const dynamic_global_property_object& dpo = get_dynamic_global_properties();
Expand Down Expand Up @@ -1401,6 +1491,7 @@ void database::perform_chain_maintenance(const signed_block& next_block, const g
d._vote_tally_buffer.resize(props.next_available_vote_id);
d._witness_count_histogram_buffer.resize(props.parameters.maximum_witness_count / 2 + 1);
d._committee_count_histogram_buffer.resize(props.parameters.maximum_committee_count / 2 + 1);
d._sons_count_histogram_buffer.resize(props.parameters.maximum_son s committee_count / 2 + 1);
d._total_voting_stake = 0;

auto balance_type = vesting_balance_type::unspecified;
Expand Down Expand Up @@ -1503,6 +1594,18 @@ void database::perform_chain_maintenance(const signed_block& next_block, const g
// same rationale as for witnesses
d._committee_count_histogram_buffer[offset] += voting_stake;
}
if( opinion_account.options.num_son <= props.parameters.maximum_son_count )
{
uint16_t offset = std::min(size_t(opinion_account.options.num_son/2),
d._son_count_histogram_buffer.size() - 1);
// votes for a number greater than maximum_son_count
// are turned into votes for maximum_son_count.
//
// in particular, this takes care of the case where a
// member was voting for a high number, then the
// parameter was lowered.
d._son_count_histogram_buffer[offset] += voting_stake;
}

d._total_voting_stake += voting_stake;
}
Expand Down Expand Up @@ -1533,11 +1636,13 @@ void database::perform_chain_maintenance(const signed_block& next_block, const g
};
clear_canary a(_witness_count_histogram_buffer),
b(_committee_count_histogram_buffer),
d(_son_count_histogram_buffer),
c(_vote_tally_buffer);

update_top_n_authorities(*this);
update_active_witnesses();
update_active_committee_members();
update_active_sons();
update_worker_votes();

modify(gpo, [this](global_property_object& p) {
Expand Down
4 changes: 4 additions & 0 deletions libraries/chain/include/graphene/chain/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@

#define GRAPHENE_DEFAULT_MIN_WITNESS_COUNT (11)
#define GRAPHENE_DEFAULT_MIN_COMMITTEE_MEMBER_COUNT (11)
#define GRAPHENE_DEFAULT_MIN_SON_COUNT (5)
#define GRAPHENE_DEFAULT_MAX_WITNESSES (1001) // SHOULD BE ODD
#define GRAPHENE_DEFAULT_MAX_COMMITTEE (1001) // SHOULD BE ODD
#define GRAPHENE_DEFAULT_MAX_SONS (15)
#define GRAPHENE_DEFAULT_MAX_PROPOSAL_LIFETIME_SEC (60*60*24*7*4) // Four weeks
#define GRAPHENE_DEFAULT_COMMITTEE_PROPOSAL_REVIEW_PERIOD_SEC (60*60*24*7*2) // Two weeks
#define GRAPHENE_DEFAULT_NETWORK_PERCENT_OF_FEE (20*GRAPHENE_1_PERCENT)
Expand Down Expand Up @@ -173,6 +175,8 @@
#define GRAPHENE_PROXY_TO_SELF_ACCOUNT (graphene::chain::account_id_type(5))
///
#define GRAPHENE_RAKE_FEE_ACCOUNT_ID (graphene::chain::account_id_type(6))
///
#define GRAPHENE_SON_ACCOUNT_ID (graphene::chain::account_id_type(7))
/// Sentinel value used in the scheduler.
#define GRAPHENE_NULL_WITNESS (graphene::chain::witness_id_type(0))
///@}
Expand Down
2 changes: 2 additions & 0 deletions libraries/chain/include/graphene/chain/database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ namespace graphene { namespace chain {
void perform_chain_maintenance(const signed_block& next_block, const global_property_object& global_props);
void update_active_witnesses();
void update_active_committee_members();
void update_active_sons();
void update_worker_votes();
public:
double calculate_vesting_factor(const account_object& stake_account);
Expand Down Expand Up @@ -537,6 +538,7 @@ namespace graphene { namespace chain {
vector<uint64_t> _vote_tally_buffer;
vector<uint64_t> _witness_count_histogram_buffer;
vector<uint64_t> _committee_count_histogram_buffer;
vector<uint64_t> _sons_count_histogram_buffer;
uint64_t _total_voting_stake;

flat_map<uint32_t,block_id_type> _checkpoints;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,5 @@ FC_REFLECT_DERIVED( graphene::chain::global_property_object, (graphene::db::obje
(next_available_vote_id)
(active_committee_members)
(active_witnesses)
(active_sons)
)
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct immutable_chain_parameters
{
uint16_t min_committee_member_count = GRAPHENE_DEFAULT_MIN_COMMITTEE_MEMBER_COUNT;
uint16_t min_witness_count = GRAPHENE_DEFAULT_MIN_WITNESS_COUNT;
uint16_t min_son_count = GRAPHENE_DEFAULT_MIN_SON_COUNT;
uint32_t num_special_accounts = 0;
uint32_t num_special_assets = 0;
};
Expand All @@ -44,6 +45,7 @@ struct immutable_chain_parameters
FC_REFLECT( graphene::chain::immutable_chain_parameters,
(min_committee_member_count)
(min_witness_count)
(min_son_count)
(num_special_accounts)
(num_special_assets)
)
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ namespace graphene { namespace chain {
uint8_t maximum_asset_feed_publishers = GRAPHENE_DEFAULT_MAX_ASSET_FEED_PUBLISHERS; ///< the maximum number of feed publishers for a given asset
uint16_t maximum_witness_count = GRAPHENE_DEFAULT_MAX_WITNESSES; ///< maximum number of active witnesses
uint16_t maximum_committee_count = GRAPHENE_DEFAULT_MAX_COMMITTEE; ///< maximum number of active committee_members
uint16_t maximum_son_count = GRAPHENE_DEFAULT_MAX_SONS; ///< maximum number of active SONS
uint16_t maximum_authority_membership = GRAPHENE_DEFAULT_MAX_AUTHORITY_MEMBERSHIP; ///< largest number of keys/accounts an authority can have
uint16_t reserve_percent_of_fee = GRAPHENE_DEFAULT_BURN_PERCENT_OF_FEE; ///< the percentage of the network's allocation of a fee that is taken out of circulation
uint16_t network_percent_of_fee = GRAPHENE_DEFAULT_NETWORK_PERCENT_OF_FEE; ///< percent of transaction fees paid to network
Expand Down Expand Up @@ -150,6 +151,7 @@ FC_REFLECT( graphene::chain::chain_parameters,
(maximum_asset_feed_publishers)
(maximum_witness_count)
(maximum_committee_count)
(maximum_son_count)
(maximum_authority_membership)
(reserve_percent_of_fee)
(network_percent_of_fee)
Expand Down

0 comments on commit 457c8b9

Please sign in to comment.