BitShares-Core  4.0.0
BitShares blockchain implementation and command-line interface software
db_maint.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cryptonomex, Inc., and contributors.
3  *
4  * The MIT License
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #include <fc/uint128.hpp>
26 
28 
31 #include <graphene/chain/hardfork.hpp>
32 
49 
50 namespace graphene { namespace chain {
51 
52 template<class Index>
53 vector<std::reference_wrapper<const typename Index::object_type>> database::sort_votable_objects(size_t count) const
54 {
55  using ObjectType = typename Index::object_type;
56  const auto& all_objects = get_index_type<Index>().indices();
57  count = std::min(count, all_objects.size());
58  vector<std::reference_wrapper<const ObjectType>> refs;
59  refs.reserve(all_objects.size());
60  std::transform(all_objects.begin(), all_objects.end(),
61  std::back_inserter(refs),
62  [](const ObjectType& o) { return std::cref(o); });
63  std::partial_sort(refs.begin(), refs.begin() + count, refs.end(),
64  [this](const ObjectType& a, const ObjectType& b)->bool {
65  share_type oa_vote = _vote_tally_buffer[a.vote_id];
66  share_type ob_vote = _vote_tally_buffer[b.vote_id];
67  if( oa_vote != ob_vote )
68  return oa_vote > ob_vote;
69  return a.vote_id < b.vote_id;
70  });
71 
72  refs.resize(count, refs.front());
73  return refs;
74 }
75 
76 template<class Type>
77 void database::perform_account_maintenance(Type tally_helper)
78 {
79  const auto& bal_idx = get_index_type< account_balance_index >().indices().get< by_maintenance_flag >();
80  if( bal_idx.begin() != bal_idx.end() )
81  {
82  auto bal_itr = bal_idx.rbegin();
83  while( bal_itr->maintenance_flag )
84  {
85  const account_balance_object& bal_obj = *bal_itr;
86 
87  modify( get_account_stats_by_owner( bal_obj.owner ), [&bal_obj](account_statistics_object& aso) {
88  aso.core_in_balance = bal_obj.balance;
89  });
90 
91  modify( bal_obj, []( account_balance_object& abo ) {
92  abo.maintenance_flag = false;
93  });
94 
95  bal_itr = bal_idx.rbegin();
96  }
97  }
98 
99  const auto& stats_idx = get_index_type< account_stats_index >().indices().get< by_maintenance_seq >();
100  auto stats_itr = stats_idx.lower_bound( true );
101 
102  while( stats_itr != stats_idx.end() )
103  {
104  const account_statistics_object& acc_stat = *stats_itr;
105  const account_object& acc_obj = acc_stat.owner( *this );
106  ++stats_itr;
107 
108  if( acc_stat.has_some_core_voting() )
109  tally_helper( acc_obj, acc_stat );
110 
111  if( acc_stat.has_pending_fees() )
112  acc_stat.process_fees( acc_obj, *this );
113  }
114 
115 }
116 
119 {
120  private:
121  share_type pay;
122  database& db;
123 
124  public:
126  : pay(pay), db(db) {}
127 
128  typedef void result_type;
129  template<typename W>
130  void operator()(W& worker)const
131  {
132  worker.pay_worker(pay, db);
133  }
134 };
135 
136 void database::update_worker_votes()
137 {
138  const auto& idx = get_index_type<worker_index>().indices().get<by_account>();
139  auto itr = idx.begin();
140  auto itr_end = idx.end();
141  bool allow_negative_votes = (head_block_time() < HARDFORK_607_TIME);
142  while( itr != itr_end )
143  {
144  modify( *itr, [this,allow_negative_votes]( worker_object& obj )
145  {
146  obj.total_votes_for = _vote_tally_buffer[obj.vote_for];
147  obj.total_votes_against = allow_negative_votes ? _vote_tally_buffer[obj.vote_against] : 0;
148  });
149  ++itr;
150  }
151 }
152 
153 void database::pay_workers( share_type& budget )
154 {
155  const auto head_time = head_block_time();
156 // ilog("Processing payroll! Available budget is ${b}", ("b", budget));
157  vector<std::reference_wrapper<const worker_object>> active_workers;
158  // TODO optimization: add by_expiration index to avoid iterating through all objects
159  get_index_type<worker_index>().inspect_all_objects([head_time, &active_workers](const object& o) {
160  const worker_object& w = static_cast<const worker_object&>(o);
161  if( w.is_active(head_time) && w.approving_stake() > 0 )
162  active_workers.emplace_back(w);
163  });
164 
165  // worker with more votes is preferred
166  // if two workers exactly tie for votes, worker with lower ID is preferred
167  std::sort(active_workers.begin(), active_workers.end(), [](const worker_object& wa, const worker_object& wb) {
168  share_type wa_vote = wa.approving_stake();
169  share_type wb_vote = wb.approving_stake();
170  if( wa_vote != wb_vote )
171  return wa_vote > wb_vote;
172  return wa.id < wb.id;
173  });
174 
175  const auto last_budget_time = get_dynamic_global_properties().last_budget_time;
176  const auto passed_time_ms = head_time - last_budget_time;
177  const auto passed_time_count = passed_time_ms.count();
178  const auto day_count = fc::days(1).count();
179  for( uint32_t i = 0; i < active_workers.size() && budget > 0; ++i )
180  {
181  const worker_object& active_worker = active_workers[i];
182  share_type requested_pay = active_worker.daily_pay;
183 
184  // Note: if there is a good chance that passed_time_count == day_count,
185  // for better performance, can avoid the 128 bit calculation by adding a check.
186  // Since it's not the case on BitShares mainnet, we're not using a check here.
187  fc::uint128_t pay = requested_pay.value;
188  pay *= passed_time_count;
189  pay /= day_count;
190  requested_pay = static_cast<uint64_t>(pay);
191 
192  share_type actual_pay = std::min(budget, requested_pay);
193  //ilog(" ==> Paying ${a} to worker ${w}", ("w", active_worker.id)("a", actual_pay));
194  modify(active_worker, [&](worker_object& w) {
195  w.worker.visit(worker_pay_visitor(actual_pay, *this));
196  });
197 
198  budget -= actual_pay;
199  }
200 }
201 
202 void database::update_active_witnesses()
203 { try {
204  assert( _witness_count_histogram_buffer.size() > 0 );
205  share_type stake_target = (_total_voting_stake[1]-_witness_count_histogram_buffer[0]) / 2;
206 
209 
210  share_type stake_tally = 0;
211 
212  size_t witness_count = 0;
213  if( stake_target > 0 )
214  {
215  while( (witness_count < _witness_count_histogram_buffer.size() - 1)
216  && (stake_tally <= stake_target) )
217  {
218  stake_tally += _witness_count_histogram_buffer[++witness_count];
219  }
220  }
221 
223 
224  witness_count = std::max( witness_count*2+1, (size_t)cpo.immutable_parameters.min_witness_count );
225  auto wits = sort_votable_objects<witness_index>( witness_count );
226 
228 
229  auto update_witness_total_votes = [this]( const witness_object& wit ) {
230  modify( wit, [this]( witness_object& obj )
231  {
232  obj.total_votes = _vote_tally_buffer[obj.vote_id];
233  });
234  };
235 
236  if( _track_standby_votes )
237  {
238  const auto& all_witnesses = get_index_type<witness_index>().indices();
239  for( const witness_object& wit : all_witnesses )
240  {
241  update_witness_total_votes( wit );
242  }
243  }
244  else
245  {
246  for( const witness_object& wit : wits )
247  {
248  update_witness_total_votes( wit );
249  }
250  }
251 
252  // Update witness authority
253  modify( get(GRAPHENE_WITNESS_ACCOUNT), [this,&wits]( account_object& a )
254  {
255  if( head_block_time() < HARDFORK_533_TIME )
256  {
257  uint64_t total_votes = 0;
258  map<account_id_type, uint64_t> weights;
259  a.active.weight_threshold = 0;
260  a.active.clear();
261 
262  for( const witness_object& wit : wits )
263  {
264  weights.emplace(wit.witness_account, _vote_tally_buffer[wit.vote_id]);
265  total_votes += _vote_tally_buffer[wit.vote_id];
266  }
267 
268  // total_votes is 64 bits. Subtract the number of leading low bits from 64 to get the number of useful bits,
269  // then I want to keep the most significant 16 bits of what's left.
270  int8_t bits_to_drop = std::max(int(boost::multiprecision::detail::find_msb(total_votes)) - 15, 0);
271  for( const auto& weight : weights )
272  {
273  // Ensure that everyone has at least one vote. Zero weights aren't allowed.
274  uint16_t votes = std::max((weight.second >> bits_to_drop), uint64_t(1) );
275  a.active.account_auths[weight.first] += votes;
276  a.active.weight_threshold += votes;
277  }
278 
279  a.active.weight_threshold /= 2;
280  a.active.weight_threshold += 1;
281  }
282  else
283  {
284  vote_counter vc;
285  for( const witness_object& wit : wits )
286  vc.add( wit.witness_account, _vote_tally_buffer[wit.vote_id] );
287  vc.finish( a.active );
288  }
289  } );
290 
291  modify( gpo, [&wits]( global_property_object& gp )
292  {
293  gp.active_witnesses.clear();
294  gp.active_witnesses.reserve(wits.size());
295  std::transform(wits.begin(), wits.end(),
296  std::inserter(gp.active_witnesses, gp.active_witnesses.end()),
297  [](const witness_object& w) {
298  return w.id;
299  });
300  });
301 
303 
304 void database::update_active_committee_members()
305 { try {
306  assert( _committee_count_histogram_buffer.size() > 0 );
307  share_type stake_target = (_total_voting_stake[0]-_committee_count_histogram_buffer[0]) / 2;
308 
311  share_type stake_tally = 0;
312  size_t committee_member_count = 0;
313  if( stake_target > 0 )
314  {
315  while( (committee_member_count < _committee_count_histogram_buffer.size() - 1)
316  && (stake_tally <= stake_target.value) )
317  {
318  stake_tally += _committee_count_histogram_buffer[++committee_member_count];
319  }
320  }
321 
323 
324  committee_member_count = std::max( committee_member_count*2+1, (size_t)cpo.immutable_parameters.min_committee_member_count );
325  auto committee_members = sort_votable_objects<committee_member_index>( committee_member_count );
326 
327  auto update_committee_member_total_votes = [this]( const committee_member_object& cm ) {
328  modify( cm, [this]( committee_member_object& obj )
329  {
330  obj.total_votes = _vote_tally_buffer[obj.vote_id];
331  });
332  };
333 
334  if( _track_standby_votes )
335  {
336  const auto& all_committee_members = get_index_type<committee_member_index>().indices();
337  for( const committee_member_object& cm : all_committee_members )
338  {
339  update_committee_member_total_votes( cm );
340  }
341  }
342  else
343  {
344  for( const committee_member_object& cm : committee_members )
345  {
346  update_committee_member_total_votes( cm );
347  }
348  }
349 
350  // Update committee authorities
351  if( !committee_members.empty() )
352  {
353  const account_object& committee_account = get(GRAPHENE_COMMITTEE_ACCOUNT);
354  modify( committee_account, [this,&committee_members](account_object& a)
355  {
356  if( head_block_time() < HARDFORK_533_TIME )
357  {
358  uint64_t total_votes = 0;
359  map<account_id_type, uint64_t> weights;
360  a.active.weight_threshold = 0;
361  a.active.clear();
362 
363  for( const committee_member_object& cm : committee_members )
364  {
365  weights.emplace( cm.committee_member_account, _vote_tally_buffer[cm.vote_id] );
366  total_votes += _vote_tally_buffer[cm.vote_id];
367  }
368 
369  // total_votes is 64 bits. Subtract the number of leading low bits from 64 to get the number of useful bits,
370  // then I want to keep the most significant 16 bits of what's left.
371  int8_t bits_to_drop = std::max(int(boost::multiprecision::detail::find_msb(total_votes)) - 15, 0);
372  for( const auto& weight : weights )
373  {
374  // Ensure that everyone has at least one vote. Zero weights aren't allowed.
375  uint16_t votes = std::max((weight.second >> bits_to_drop), uint64_t(1) );
376  a.active.account_auths[weight.first] += votes;
377  a.active.weight_threshold += votes;
378  }
379 
380  a.active.weight_threshold /= 2;
381  a.active.weight_threshold += 1;
382  }
383  else
384  {
385  vote_counter vc;
386  for( const committee_member_object& cm : committee_members )
387  vc.add( cm.committee_member_account, _vote_tally_buffer[cm.vote_id] );
388  vc.finish( a.active );
389  }
390  });
391  modify( get(GRAPHENE_RELAXED_COMMITTEE_ACCOUNT), [&committee_account](account_object& a)
392  {
393  a.active = committee_account.active;
394  });
395  }
396  modify( get_global_properties(), [&committee_members](global_property_object& gp)
397  {
398  gp.active_committee_members.clear();
399  std::transform(committee_members.begin(), committee_members.end(),
400  std::inserter(gp.active_committee_members, gp.active_committee_members.begin()),
401  [](const committee_member_object& d) { return d.id; });
402  });
404 
405 void database::initialize_budget_record( fc::time_point_sec now, budget_record& rec )const
406 {
408  const asset_object& core = get_core_asset();
410 
411  rec.from_initial_reserve = core.reserved(*this);
414 
415  if( (dpo.last_budget_time == fc::time_point_sec())
416  || (now <= dpo.last_budget_time) )
417  {
418  rec.time_since_last_budget = 0;
419  return;
420  }
421 
422  int64_t dt = (now - dpo.last_budget_time).to_seconds();
423  rec.time_since_last_budget = uint64_t( dt );
424 
425  // We'll consider accumulated_fees to be reserved at the BEGINNING
426  // of the maintenance interval. However, for speed we only
427  // call modify() on the asset_dynamic_data_object once at the
428  // end of the maintenance interval. Thus the accumulated_fees
429  // are available for the budget at this point, but not included
430  // in core.reserved().
431  share_type reserve = rec.from_initial_reserve + core_dd.accumulated_fees;
432  // Similarly, we consider leftover witness_budget to be burned
433  // at the BEGINNING of the maintenance interval.
434  reserve += dpo.witness_budget;
435 
436  fc::uint128_t budget_u128 = reserve.value;
437  budget_u128 *= uint64_t(dt);
438  budget_u128 *= GRAPHENE_CORE_ASSET_CYCLE_RATE;
439  //round up to the nearest satoshi -- this is necessary to ensure
440  // there isn't an "untouchable" reserve, and we will eventually
441  // be able to use the entire reserve
442  budget_u128 += ((uint64_t(1) << GRAPHENE_CORE_ASSET_CYCLE_RATE_BITS) - 1);
443  budget_u128 >>= GRAPHENE_CORE_ASSET_CYCLE_RATE_BITS;
444  if( budget_u128 < static_cast<fc::uint128_t>(reserve.value) )
445  rec.total_budget = share_type(static_cast<uint64_t>(budget_u128));
446  else
447  rec.total_budget = reserve;
448 
449  return;
450 }
451 
455 void database::process_budget()
456 {
457  try
458  {
463 
464  int64_t time_to_maint = (dpo.next_maintenance_time - now).to_seconds();
465  //
466  // The code that generates the next maintenance time should
467  // only produce a result in the future. If this assert
468  // fails, then the next maintenance time algorithm is buggy.
469  //
470  assert( time_to_maint > 0 );
471  //
472  // Code for setting chain parameters should validate
473  // block_interval > 0 (as well as the humans proposing /
474  // voting on changes to block interval).
475  //
476  assert( gpo.parameters.block_interval > 0 );
477  uint64_t blocks_to_maint = (uint64_t(time_to_maint) + gpo.parameters.block_interval - 1) / gpo.parameters.block_interval;
478 
479  // blocks_to_maint > 0 because time_to_maint > 0,
480  // which means numerator is at least equal to block_interval
481 
482  budget_record rec;
483  initialize_budget_record( now, rec );
484  share_type available_funds = rec.total_budget;
485 
486  share_type witness_budget = gpo.parameters.witness_pay_per_block.value * blocks_to_maint;
487  rec.requested_witness_budget = witness_budget;
488  witness_budget = std::min(witness_budget, available_funds);
489  rec.witness_budget = witness_budget;
490  available_funds -= witness_budget;
491 
492  fc::uint128_t worker_budget_u128 = gpo.parameters.worker_budget_per_day.value;
493  worker_budget_u128 *= uint64_t(time_to_maint);
494  worker_budget_u128 /= 60*60*24;
495 
496  share_type worker_budget;
497  if( worker_budget_u128 >= static_cast<fc::uint128_t>(available_funds.value) )
498  worker_budget = available_funds;
499  else
500  worker_budget = static_cast<uint64_t>(worker_budget_u128);
501  rec.worker_budget = worker_budget;
502  available_funds -= worker_budget;
503 
504  share_type leftover_worker_funds = worker_budget;
505  pay_workers(leftover_worker_funds);
506  rec.leftover_worker_funds = leftover_worker_funds;
507  available_funds += leftover_worker_funds;
508 
509  rec.supply_delta = rec.witness_budget
510  + rec.worker_budget
514 
515  modify(core, [&]( asset_dynamic_data_object& _core )
516  {
517  _core.current_supply = (_core.current_supply + rec.supply_delta );
518 
519  assert( rec.supply_delta ==
520  witness_budget
521  + worker_budget
522  - leftover_worker_funds
523  - _core.accumulated_fees
524  - dpo.witness_budget
525  );
526  _core.accumulated_fees = 0;
527  });
528 
529  modify(dpo, [&]( dynamic_global_property_object& _dpo )
530  {
531  // Since initial witness_budget was rolled into
532  // available_funds, we replace it with witness_budget
533  // instead of adding it.
534  _dpo.witness_budget = witness_budget;
535  _dpo.last_budget_time = now;
536  });
537 
538  create< budget_record_object >( [&]( budget_record_object& _rec )
539  {
540  _rec.time = head_block_time();
541  _rec.record = rec;
542  });
543 
544  // available_funds is money we could spend, but don't want to.
545  // we simply let it evaporate back into the reserve.
546  }
548 }
549 
550 template< typename Visitor >
551 void visit_special_authorities( const database& db, Visitor visit )
552 {
553  const auto& sa_idx = db.get_index_type< special_authority_index >().indices().get<by_id>();
554 
555  for( const special_authority_object& sao : sa_idx )
556  {
557  const account_object& acct = sao.account(db);
559  {
560  visit( acct, true, acct.owner_special_authority );
561  }
563  {
564  visit( acct, false, acct.active_special_authority );
565  }
566  }
567 }
568 
570 {
572  [&]( const account_object& acct, bool is_owner, const special_authority& auth )
573  {
574  if( auth.is_type< top_holders_special_authority >() )
575  {
576  // use index to grab the top N holders of the asset and vote_counter to obtain the weights
577 
578  const top_holders_special_authority& tha = auth.get< top_holders_special_authority >();
579  vote_counter vc;
580  const auto& bal_idx = db.get_index_type< account_balance_index >().indices().get< by_asset_balance >();
581  uint8_t num_needed = tha.num_top_holders;
582  if( num_needed == 0 )
583  return;
584 
585  // find accounts
586  const auto range = bal_idx.equal_range( boost::make_tuple( tha.asset ) );
587  for( const account_balance_object& bal : boost::make_iterator_range( range.first, range.second ) )
588  {
589  assert( bal.asset_type == tha.asset );
590  if( bal.owner == acct.id )
591  continue;
592  vc.add( bal.owner, bal.balance.value );
593  --num_needed;
594  if( num_needed == 0 )
595  break;
596  }
597 
598  db.modify( acct, [&]( account_object& a )
599  {
600  vc.finish( is_owner ? a.owner : a.active );
601  if( !vc.is_empty() )
602  a.top_n_control_flags |= (is_owner ? account_object::top_n_control_owner : account_object::top_n_control_active);
603  } );
604  }
605  } );
606 }
607 
609  database& db,
610  uint64_t fba_id,
611  uint16_t network_pct,
612  uint16_t designated_asset_buyback_pct,
613  uint16_t designated_asset_issuer_pct
614 )
615 {
616  FC_ASSERT( uint32_t(network_pct) + uint32_t(designated_asset_buyback_pct) + uint32_t(designated_asset_issuer_pct) == GRAPHENE_100_PERCENT );
617  const fba_accumulator_object& fba = fba_accumulator_id_type( fba_id )(db);
618  if( fba.accumulated_fba_fees == 0 )
619  return;
620 
621  const asset_dynamic_data_object& core_dd = db.get_core_dynamic_data();
622 
623  if( !fba.is_configured(db) )
624  {
625  ilog( "${n} core given to network at block ${b} due to non-configured FBA", ("n", fba.accumulated_fba_fees)("b", db.head_block_time()) );
626  db.modify( core_dd, [&]( asset_dynamic_data_object& _core_dd )
627  {
628  _core_dd.current_supply -= fba.accumulated_fba_fees;
629  } );
630  db.modify( fba, [&]( fba_accumulator_object& _fba )
631  {
632  _fba.accumulated_fba_fees = 0;
633  } );
634  return;
635  }
636 
637  fc::uint128_t buyback_amount_128 = fba.accumulated_fba_fees.value;
638  buyback_amount_128 *= designated_asset_buyback_pct;
639  buyback_amount_128 /= GRAPHENE_100_PERCENT;
640  share_type buyback_amount = static_cast<uint64_t>(buyback_amount_128);
641 
642  fc::uint128_t issuer_amount_128 = fba.accumulated_fba_fees.value;
643  issuer_amount_128 *= designated_asset_issuer_pct;
644  issuer_amount_128 /= GRAPHENE_100_PERCENT;
645  share_type issuer_amount = static_cast<uint64_t>(issuer_amount_128);
646 
647  // this assert should never fail
648  FC_ASSERT( buyback_amount + issuer_amount <= fba.accumulated_fba_fees );
649 
650  share_type network_amount = fba.accumulated_fba_fees - (buyback_amount + issuer_amount);
651 
652  const asset_object& designated_asset = (*fba.designated_asset)(db);
653 
654  if( network_amount != 0 )
655  {
656  db.modify( core_dd, [&]( asset_dynamic_data_object& _core_dd )
657  {
658  _core_dd.current_supply -= network_amount;
659  } );
660  }
661 
663  vop.account_id = *designated_asset.buyback_account;
664  vop.fba_id = fba.id;
665  vop.amount = buyback_amount;
666  if( vop.amount != 0 )
667  {
668  db.adjust_balance( *designated_asset.buyback_account, asset(buyback_amount) );
669  db.push_applied_operation(vop);
670  }
671 
672  vop.account_id = designated_asset.issuer;
673  vop.fba_id = fba.id;
674  vop.amount = issuer_amount;
675  if( vop.amount != 0 )
676  {
677  db.adjust_balance( designated_asset.issuer, asset(issuer_amount) );
678  db.push_applied_operation(vop);
679  }
680 
681  db.modify( fba, [&]( fba_accumulator_object& _fba )
682  {
683  _fba.accumulated_fba_fees = 0;
684  } );
685 }
686 
688 {
692 }
693 
695 {
696  const auto& bbo_idx = db.get_index_type< buyback_index >().indices().get<by_id>();
697  const auto& bal_idx = db.get_index_type< primary_index< account_balance_index > >().get_secondary_index< balances_by_account_index >();
698 
699  for( const buyback_object& bbo : bbo_idx )
700  {
701  const asset_object& asset_to_buy = bbo.asset_to_buy(db);
702  assert( asset_to_buy.buyback_account.valid() );
703 
704  const account_object& buyback_account = (*(asset_to_buy.buyback_account))(db);
705 
706  if( !buyback_account.allowed_assets.valid() )
707  {
708  wlog( "skipping buyback account ${b} at block ${n} because allowed_assets does not exist", ("b", buyback_account)("n", db.head_block_num()) );
709  continue;
710  }
711 
712  for( const auto& entry : bal_idx.get_account_balances( buyback_account.id ) )
713  {
714  const auto* it = entry.second;
715  asset_id_type asset_to_sell = it->asset_type;
716  share_type amount_to_sell = it->balance;
717  if( asset_to_sell == asset_to_buy.id )
718  continue;
719  if( amount_to_sell == 0 )
720  continue;
721  if( buyback_account.allowed_assets->find( asset_to_sell ) == buyback_account.allowed_assets->end() )
722  {
723  wlog( "buyback account ${b} not selling disallowed holdings of asset ${a} at block ${n}", ("b", buyback_account)("a", asset_to_sell)("n", db.head_block_num()) );
724  continue;
725  }
726 
727  try
728  {
729  transaction_evaluation_state buyback_context(&db);
730  buyback_context.skip_fee_schedule_check = true;
731 
732  limit_order_create_operation create_vop;
733  create_vop.fee = asset( 0, asset_id_type() );
734  create_vop.seller = buyback_account.id;
735  create_vop.amount_to_sell = asset( amount_to_sell, asset_to_sell );
736  create_vop.min_to_receive = asset( 1, asset_to_buy.id );
737  create_vop.expiration = time_point_sec::maximum();
738  create_vop.fill_or_kill = false;
739 
740  limit_order_id_type order_id = db.apply_operation( buyback_context, create_vop ).get< object_id_type >();
741 
742  if( db.find( order_id ) != nullptr )
743  {
744  limit_order_cancel_operation cancel_vop;
745  cancel_vop.fee = asset( 0, asset_id_type() );
746  cancel_vop.order = order_id;
747  cancel_vop.fee_paying_account = buyback_account.id;
748 
749  db.apply_operation( buyback_context, cancel_vop );
750  }
751  }
752  catch( const fc::exception& e )
753  {
754  // we can in fact get here, e.g. if asset issuer of buy/sell asset blacklists/whitelists the buyback account
755  wlog( "Skipping buyback processing selling ${as} for ${ab} for buyback account ${b} at block ${n}; exception was ${e}",
756  ("as", asset_to_sell)("ab", asset_to_buy)("b", buyback_account)("n", db.head_block_num())("e", e.to_detail_string()) );
757  continue;
758  }
759  }
760  }
761  return;
762 }
763 
765 {
766  const auto& account_idx = db.get_index_type<account_index>().indices().get<by_id>();
768  for( const account_object& acct : account_idx )
769  {
770  try
771  {
772  transaction_evaluation_state upgrade_context(&db);
773  upgrade_context.skip_fee_schedule_check = true;
774 
775  if( acct.is_annual_member( now ) )
776  {
777  account_upgrade_operation upgrade_vop;
778  upgrade_vop.fee = asset( 0, asset_id_type() );
779  upgrade_vop.account_to_upgrade = acct.id;
780  upgrade_vop.upgrade_to_lifetime_member = true;
781  db.apply_operation( upgrade_context, upgrade_vop );
782  }
783  }
784  catch( const fc::exception& e )
785  {
786  // we can in fact get here, e.g. if asset issuer of buy/sell asset blacklists/whitelists the buyback account
787  wlog( "Skipping annual member deprecate processing for account ${a} (${an}) at block ${n}; exception was ${e}",
788  ("a", acct.id)("an", acct.name)("n", db.head_block_num())("e", e.to_detail_string()) );
789  continue;
790  }
791  }
792  return;
793 }
794 
795 void database::process_bids( const asset_bitasset_data_object& bad )
796 {
797  if( bad.is_prediction_market ) return;
798  if( bad.current_feed.settlement_price.is_null() ) return;
799 
800  asset_id_type to_revive_id = (asset( 0, bad.options.short_backing_asset ) * bad.settlement_price).asset_id;
801  const asset_object& to_revive = to_revive_id( *this );
802  const asset_dynamic_data_object& bdd = to_revive.dynamic_data( *this );
803 
804  const auto& bid_idx = get_index_type< collateral_bid_index >().indices().get<by_price>();
805  const auto start = bid_idx.lower_bound( boost::make_tuple( to_revive_id, price::max( bad.options.short_backing_asset, to_revive_id ), collateral_bid_id_type() ) );
806 
807  share_type covered = 0;
808  auto itr = start;
809  while( covered < bdd.current_supply && itr != bid_idx.end() && itr->inv_swan_price.quote.asset_id == to_revive_id )
810  {
811  const collateral_bid_object& bid = *itr;
812  asset debt_in_bid = bid.inv_swan_price.quote;
813  if( debt_in_bid.amount > bdd.current_supply )
814  debt_in_bid.amount = bdd.current_supply;
815  asset total_collateral = debt_in_bid * bad.settlement_price;
816  total_collateral += bid.inv_swan_price.base;
817  price call_price = price::call_price( debt_in_bid, total_collateral, bad.current_feed.maintenance_collateral_ratio );
818  if( ~call_price >= bad.current_feed.settlement_price ) break;
819  covered += debt_in_bid.amount;
820  ++itr;
821  }
822  if( covered < bdd.current_supply ) return;
823 
824  const auto end = itr;
825  share_type to_cover = bdd.current_supply;
826  share_type remaining_fund = bad.settlement_fund;
827  for( itr = start; itr != end; )
828  {
829  const collateral_bid_object& bid = *itr;
830  ++itr;
831  asset debt_in_bid = bid.inv_swan_price.quote;
832  if( debt_in_bid.amount > bdd.current_supply )
833  debt_in_bid.amount = bdd.current_supply;
834  share_type debt = debt_in_bid.amount;
835  share_type collateral = (debt_in_bid * bad.settlement_price).amount;
836  if( debt >= to_cover )
837  {
838  debt = to_cover;
839  collateral = remaining_fund;
840  }
841  to_cover -= debt;
842  remaining_fund -= collateral;
843  execute_bid( bid, debt, collateral, bad.current_feed );
844  }
845  FC_ASSERT( remaining_fund == 0 );
846  FC_ASSERT( to_cover == 0 );
847 
848  _cancel_bids_and_revive_mpa( to_revive, bad );
849 }
850 
854 {
855  // Update call_price
856  wlog( "Updating all call orders for hardfork core-343 at block ${n}", ("n",db.head_block_num()) );
857  asset_id_type current_asset;
858  const asset_bitasset_data_object* abd = nullptr;
859  // by_collateral index won't change after call_price updated, so it's safe to iterate
860  for( const auto& call_obj : db.get_index_type<call_order_index>().indices().get<by_collateral>() )
861  {
862  if( current_asset != call_obj.debt_type() ) // debt type won't be asset_id_type(), abd will always get initialized
863  {
864  current_asset = call_obj.debt_type();
865  abd = &current_asset(db).bitasset_data(db);
866  }
867  if( !abd || abd->is_prediction_market ) // nothing to do with PM's; check !abd just to be safe
868  continue;
869  db.modify( call_obj, [abd]( call_order_object& call ) {
870  call.call_price = price::call_price( call.get_debt(), call.get_collateral(),
872  });
873  }
874  wlog( "Done updating all call orders for hardfork core-343 at block ${n}", ("n",db.head_block_num()) );
875 }
876 
880 {
881  // Update call_price
882  for( const auto& call_obj : db.get_index_type<call_order_index>().indices().get<by_id>() )
883  {
884  db.modify( call_obj, []( call_order_object& call ) {
885  call.call_price.base.amount = 1;
886  call.call_price.quote.amount = 1;
887  });
888  }
889 }
890 
893 {
894  // Match call orders
895  wlog( "Matching call orders at block ${n}", ("n",db.head_block_num()) );
896  const auto& asset_idx = db.get_index_type<asset_index>().indices().get<by_type>();
897  auto itr = asset_idx.lower_bound( true );
898  while( itr != asset_idx.end() )
899  {
900  const asset_object& a = *itr;
901  ++itr;
902  // be here, next_maintenance_time should have been updated already
903  db.check_call_orders( a, true, false ); // allow black swan, and call orders are taker
904  }
905  wlog( "Done matching call orders at block ${n}", ("n",db.head_block_num()) );
906 }
907 
908 void database::process_bitassets()
909 {
910  time_point_sec head_time = head_block_time();
911  uint32_t head_epoch_seconds = head_time.sec_since_epoch();
912  bool after_hf_core_518 = ( head_time >= HARDFORK_CORE_518_TIME ); // clear expired feeds
913 
914  const auto update_bitasset = [this,head_time,head_epoch_seconds,after_hf_core_518]( asset_bitasset_data_object &o )
915  {
916  o.force_settled_volume = 0; // Reset all BitAsset force settlement volumes to zero
917 
918  // clear expired feeds
919  if( after_hf_core_518 )
920  {
921  const auto &asset = get( o.asset_id );
922  auto flags = asset.options.flags;
923  if ( ( flags & ( witness_fed_asset | committee_fed_asset ) ) &&
924  o.options.feed_lifetime_sec < head_epoch_seconds ) // if smartcoin && check overflow
925  {
926  fc::time_point_sec calculated = head_time - o.options.feed_lifetime_sec;
927  for( auto itr = o.feeds.rbegin(); itr != o.feeds.rend(); ) // loop feeds
928  {
929  auto feed_time = itr->second.first;
930  std::advance( itr, 1 );
931  if( feed_time < calculated )
932  o.feeds.erase( itr.base() ); // delete expired feed
933  }
934  }
935  }
936  };
937 
938  for( const auto& d : get_index_type<asset_bitasset_data_index>().indices() )
939  {
940  modify( d, update_bitasset );
941  if( d.has_settlement() )
942  process_bids(d);
943  }
944 }
945 
946 /****
947  * @brief a one-time data process to correct max_supply
948  *
949  * NOTE: while exceeding max_supply happened in mainnet, it seemed to have corrected
950  * itself before HF 1465. But this method must remain to correct some assets in testnet
951  */
953 {
954  // for each market issued asset
955  const auto& asset_idx = db.get_index_type<asset_index>().indices().get<by_type>();
956  for( auto asset_itr = asset_idx.lower_bound(true); asset_itr != asset_idx.end(); ++asset_itr )
957  {
958  const auto& current_asset = *asset_itr;
959  graphene::chain::share_type current_supply = current_asset.dynamic_data(db).current_supply;
960  graphene::chain::share_type max_supply = current_asset.options.max_supply;
961  if (current_supply > max_supply && max_supply != GRAPHENE_MAX_SHARE_SUPPLY)
962  {
963  wlog( "Adjusting max_supply of ${asset} because current_supply (${current_supply}) is greater than ${old}.",
964  ("asset", current_asset.symbol)
965  ("current_supply", current_supply.value)
966  ("old", max_supply));
967  db.modify<asset_object>( current_asset, [current_supply](asset_object& obj) {
968  obj.options.max_supply = graphene::chain::share_type(std::min(current_supply.value, GRAPHENE_MAX_SHARE_SUPPLY));
969  });
970  }
971  }
972 }
973 
974 /****
975  * @brief a one-time data process to correct current_supply of BTS token in the BitShares mainnet
976  */
978 {
979  const balance_object* bal = db.find( balance_id_type( HARDFORK_CORE_2103_BALANCE_ID ) );
980  if( bal != nullptr && bal->balance.amount < 0 )
981  {
982  const asset_dynamic_data_object& ddo = bal->balance.asset_id(db).dynamic_data(db);
984  obj.current_supply -= bal->balance.amount;
985  });
986  db.remove( *bal );
987  }
988 }
989 
991 {
992  time_point_sec head_time = db.head_block_time();
994 
995  const auto update_bitasset = [head_time, next_maint_time]( asset_bitasset_data_object &o )
996  {
997  o.update_median_feeds( head_time, next_maint_time );
998  };
999 
1000  for( const auto& d : db.get_index_type<asset_bitasset_data_index>().indices() )
1001  {
1002  db.modify( d, update_bitasset );
1003  }
1004 }
1005 
1006 /******
1007  * @brief one-time data process for hard fork core-868-890
1008  *
1009  * Prior to hardfork 868, switching a bitasset's shorting asset would not reset its
1010  * feeds. This method will run at the hardfork time, and erase (or nullify) feeds
1011  * that have incorrect backing assets.
1012  * https://github.com/bitshares/bitshares-core/issues/868
1013  *
1014  * Prior to hardfork 890, changing a bitasset's feed expiration time would not
1015  * trigger a median feed update. This method will run at the hardfork time, and
1016  * correct all median feed data.
1017  * https://github.com/bitshares/bitshares-core/issues/890
1018  *
1019  * @param db the database
1020  * @param skip_check_call_orders true if check_call_orders() should not be called
1021  */
1022 // NOTE: Unable to remove this function for testnet nor mainnet. Unfortunately, bad
1023 // feeds were found.
1024 void process_hf_868_890( database& db, bool skip_check_call_orders )
1025 {
1026  const auto next_maint_time = db.get_dynamic_global_properties().next_maintenance_time;
1027  const auto head_time = db.head_block_time();
1028  // for each market issued asset
1029  const auto& asset_idx = db.get_index_type<asset_index>().indices().get<by_type>();
1030  for( auto asset_itr = asset_idx.lower_bound(true); asset_itr != asset_idx.end(); ++asset_itr )
1031  {
1032  const auto& current_asset = *asset_itr;
1033  // Incorrect witness & committee feeds can simply be removed.
1034  // For non-witness-fed and non-committee-fed assets, set incorrect
1035  // feeds to price(), since we can't simply remove them. For more information:
1036  // https://github.com/bitshares/bitshares-core/pull/832#issuecomment-384112633
1037  bool is_witness_or_committee_fed = false;
1038  if ( current_asset.options.flags & ( witness_fed_asset | committee_fed_asset ) )
1039  is_witness_or_committee_fed = true;
1040 
1041  // for each feed
1042  const asset_bitasset_data_object& bitasset_data = current_asset.bitasset_data(db);
1043  auto itr = bitasset_data.feeds.begin();
1044  while( itr != bitasset_data.feeds.end() )
1045  {
1046  // If the feed is invalid
1047  if ( itr->second.second.settlement_price.quote.asset_id != bitasset_data.options.short_backing_asset
1048  && ( is_witness_or_committee_fed || itr->second.second.settlement_price != price() ) )
1049  {
1050  db.modify( bitasset_data, [&itr, is_witness_or_committee_fed]( asset_bitasset_data_object& obj )
1051  {
1052  if( is_witness_or_committee_fed )
1053  {
1054  // erase the invalid feed
1055  itr = obj.feeds.erase(itr);
1056  }
1057  else
1058  {
1059  // nullify the invalid feed
1060  obj.feeds[itr->first].second.settlement_price = price();
1061  ++itr;
1062  }
1063  });
1064  }
1065  else
1066  {
1067  // Feed is valid. Skip it.
1068  ++itr;
1069  }
1070  } // end loop of each feed
1071 
1072  // always update the median feed due to https://github.com/bitshares/bitshares-core/issues/890
1073  db.modify( bitasset_data, [head_time,next_maint_time]( asset_bitasset_data_object &obj ) {
1074  obj.update_median_feeds( head_time, next_maint_time );
1075  // NOTE: Normally we should call check_call_orders() after called update_median_feeds(), but for
1076  // mainnet actually check_call_orders() would do nothing, so we skipped it for better performance.
1077  });
1078 
1079  } // for each market issued asset
1080 }
1081 
1082 
1088 {
1089  const auto& index = db.get_index_type<custom_authority_index>().indices().get<by_expiration>();
1090  while (!index.empty() && index.begin()->valid_to < db.head_block_time())
1091  db.remove(*index.begin());
1092 }
1093 
1094 namespace detail {
1095 
1097  {
1098  time_point_sec full_power_time;
1099  time_point_sec zero_power_time;
1100  };
1101 
1103  {
1104  vote_recalc_options( uint32_t f, uint32_t d, uint32_t s )
1105  : full_power_seconds(f), recalc_steps(d), seconds_per_step(s)
1106  {
1107  total_recalc_seconds = ( recalc_steps - 1 ) * seconds_per_step; // should not overflow
1108  power_percents_to_subtract.reserve( recalc_steps - 1 );
1109  for( uint32_t i = 1; i < recalc_steps; ++i )
1110  power_percents_to_subtract.push_back( GRAPHENE_100_PERCENT * i / recalc_steps ); // should not overflow
1111  }
1112 
1114  {
1115  return { now - full_power_seconds, now - full_power_seconds - total_recalc_seconds };
1116  }
1117 
1119  uint32_t recalc_steps; // >= 1
1122  vector<uint16_t> power_percents_to_subtract;
1123 
1124  static const vote_recalc_options witness();
1125  static const vote_recalc_options committee();
1126  static const vote_recalc_options worker();
1127  static const vote_recalc_options delegator();
1128 
1129  // return the stake that is "recalced to X"
1130  uint64_t get_recalced_voting_stake( const uint64_t stake, const time_point_sec last_vote_time,
1131  const vote_recalc_times& recalc_times ) const
1132  {
1133  if( last_vote_time > recalc_times.full_power_time )
1134  return stake;
1135  if( last_vote_time <= recalc_times.zero_power_time )
1136  return 0;
1137  uint32_t diff = recalc_times.full_power_time.sec_since_epoch() - last_vote_time.sec_since_epoch();
1138  uint32_t steps_to_subtract_minus_1 = diff / seconds_per_step;
1139  fc::uint128_t stake_to_subtract( stake );
1140  stake_to_subtract *= power_percents_to_subtract[steps_to_subtract_minus_1];
1141  stake_to_subtract /= GRAPHENE_100_PERCENT;
1142  return stake - static_cast<uint64_t>(stake_to_subtract);
1143  }
1144  };
1145 
1147  {
1148  static const vote_recalc_options o( 360*86400, 8, 45*86400 );
1149  return o;
1150  }
1151  const vote_recalc_options vote_recalc_options::committee()
1152  {
1153  static const vote_recalc_options o( 360*86400, 8, 45*86400 );
1154  return o;
1155  }
1157  {
1158  static const vote_recalc_options o( 360*86400, 8, 45*86400 );
1159  return o;
1160  }
1161  const vote_recalc_options vote_recalc_options::delegator()
1162  {
1163  static const vote_recalc_options o( 360*86400, 8, 45*86400 );
1164  return o;
1165  }
1166 }
1167 
1168 void database::perform_chain_maintenance(const signed_block& next_block, const global_property_object& global_props)
1169 {
1170  const auto& gpo = get_global_properties();
1171  const auto& dgpo = get_dynamic_global_properties();
1172 
1173  distribute_fba_balances(*this);
1174  create_buyback_orders(*this);
1175 
1176  struct vote_tally_helper {
1177  database& d;
1178  const global_property_object& props;
1179  const dynamic_global_property_object& dprops;
1180  const time_point_sec now;
1181  const bool hf2103_passed;
1182  const bool pob_activated;
1183 
1184  optional<detail::vote_recalc_times> witness_recalc_times;
1185  optional<detail::vote_recalc_times> committee_recalc_times;
1186  optional<detail::vote_recalc_times> worker_recalc_times;
1187  optional<detail::vote_recalc_times> delegator_recalc_times;
1188 
1189  vote_tally_helper( database& db )
1190  : d(db), props( d.get_global_properties() ), dprops( d.get_dynamic_global_properties() ),
1191  now( d.head_block_time() ), hf2103_passed( HARDFORK_CORE_2103_PASSED( now ) ),
1192  pob_activated( dprops.total_pob > 0 || dprops.total_inactive > 0 )
1193  {
1194  d._vote_tally_buffer.resize( props.next_available_vote_id, 0 );
1195  d._witness_count_histogram_buffer.resize( props.parameters.maximum_witness_count / 2 + 1, 0 );
1196  d._committee_count_histogram_buffer.resize( props.parameters.maximum_committee_count / 2 + 1, 0 );
1197  d._total_voting_stake[0] = 0;
1198  d._total_voting_stake[1] = 0;
1199  if( hf2103_passed )
1200  {
1201  witness_recalc_times = detail::vote_recalc_options::witness().get_vote_recalc_times( now );
1202  committee_recalc_times = detail::vote_recalc_options::committee().get_vote_recalc_times( now );
1203  worker_recalc_times = detail::vote_recalc_options::worker().get_vote_recalc_times( now );
1204  delegator_recalc_times = detail::vote_recalc_options::delegator().get_vote_recalc_times( now );
1205  }
1206  }
1207 
1208  void operator()( const account_object& stake_account, const account_statistics_object& stats )
1209  {
1210  // PoB activation
1211  if( pob_activated && stats.total_core_pob == 0 && stats.total_core_inactive == 0 )
1212  return;
1213 
1214  if( props.parameters.count_non_member_votes || stake_account.is_member( now ) )
1215  {
1216  // There may be a difference between the account whose stake is voting and the one specifying opinions.
1217  // Usually they're the same, but if the stake account has specified a voting_account, that account is the
1218  // one specifying the opinions.
1219  bool directly_voting = ( stake_account.options.voting_account == GRAPHENE_PROXY_TO_SELF_ACCOUNT );
1220  const account_object& opinion_account = ( directly_voting ? stake_account
1221  : d.get(stake_account.options.voting_account) );
1222 
1223  uint64_t voting_stake[3]; // 0=committee, 1=witness, 2=worker, as in vote_id_type::vote_type
1224  uint64_t num_committee_voting_stake; // number of committee members
1225  voting_stake[2] = ( pob_activated ? 0 : stats.total_core_in_orders.value )
1226  + (stake_account.cashback_vb.valid() ? (*stake_account.cashback_vb)(d).balance.amount.value: 0)
1227  + stats.core_in_balance.value;
1228 
1229  //PoB
1230  const uint64_t pol_amount = stats.total_core_pol.value;
1231  const uint64_t pol_value = stats.total_pol_value.value;
1232  const uint64_t pob_amount = stats.total_core_pob.value;
1233  const uint64_t pob_value = stats.total_pob_value.value;
1234  if( pob_amount == 0 )
1235  {
1236  voting_stake[2] += pol_value;
1237  }
1238  else if( pol_amount == 0 ) // and pob_amount > 0
1239  {
1240  if( pob_amount <= voting_stake[2] )
1241  {
1242  voting_stake[2] += ( pob_value - pob_amount );
1243  }
1244  else
1245  {
1246  auto base_value = static_cast<fc::uint128_t>( voting_stake[2] ) * pob_value / pob_amount;
1247  voting_stake[2] = static_cast<uint64_t>( base_value );
1248  }
1249  }
1250  else if( pob_amount <= pol_amount ) // pob_amount > 0 && pol_amount > 0
1251  {
1252  auto base_value = static_cast<fc::uint128_t>( pob_value ) * pol_value / pol_amount;
1253  auto diff_value = static_cast<fc::uint128_t>( pob_amount ) * pol_value / pol_amount;
1254  base_value += ( pol_value - diff_value );
1255  voting_stake[2] += static_cast<uint64_t>( base_value );
1256  }
1257  else // pob_amount > pol_amount > 0
1258  {
1259  auto base_value = static_cast<fc::uint128_t>( pol_value ) * pob_value / pob_amount;
1260  fc::uint128_t diff_amount = pob_amount - pol_amount;
1261  if( diff_amount <= voting_stake[2] )
1262  {
1263  auto diff_value = static_cast<fc::uint128_t>( pol_amount ) * pob_value / pob_amount;
1264  base_value += ( pob_value - diff_value );
1265  voting_stake[2] += static_cast<uint64_t>( base_value - diff_amount );
1266  }
1267  else // diff_amount > voting_stake[2]
1268  {
1269  base_value += static_cast<fc::uint128_t>( voting_stake[2] ) * pob_value / pob_amount;
1270  voting_stake[2] = static_cast<uint64_t>( base_value );
1271  }
1272  }
1273 
1274  // Shortcut
1275  if( voting_stake[2] == 0 )
1276  return;
1277 
1278  // Recalculate votes
1279  if( !hf2103_passed )
1280  {
1281  voting_stake[0] = voting_stake[2];
1282  voting_stake[1] = voting_stake[2];
1283  num_committee_voting_stake = voting_stake[2];
1284  }
1285  else
1286  {
1287  if( !directly_voting )
1288  {
1290  voting_stake[2], stats.last_vote_time, *delegator_recalc_times );
1291  }
1292  const account_statistics_object& opinion_account_stats = ( directly_voting ? stats
1293  : opinion_account.statistics( d ) );
1295  voting_stake[2], opinion_account_stats.last_vote_time, *witness_recalc_times );
1297  voting_stake[2], opinion_account_stats.last_vote_time, *committee_recalc_times );
1298  num_committee_voting_stake = voting_stake[0];
1299  if( opinion_account.num_committee_voted > 1 )
1300  voting_stake[0] /= opinion_account.num_committee_voted;
1302  voting_stake[2], opinion_account_stats.last_vote_time, *worker_recalc_times );
1303  }
1304 
1305  for( vote_id_type id : opinion_account.options.votes )
1306  {
1307  uint32_t offset = id.instance();
1308  uint32_t type = std::min( id.type(), vote_id_type::vote_type::worker ); // cap the data
1309  // if they somehow managed to specify an illegal offset, ignore it.
1310  if( offset < d._vote_tally_buffer.size() )
1311  d._vote_tally_buffer[offset] += voting_stake[type];
1312  }
1313 
1314  // votes for a number greater than maximum_witness_count are skipped here
1315  if( voting_stake[1] > 0
1316  && opinion_account.options.num_witness <= props.parameters.maximum_witness_count )
1317  {
1318  uint16_t offset = opinion_account.options.num_witness / 2;
1319  d._witness_count_histogram_buffer[offset] += voting_stake[1];
1320  }
1321  // votes for a number greater than maximum_committee_count are skipped here
1322  if( num_committee_voting_stake > 0
1323  && opinion_account.options.num_committee <= props.parameters.maximum_committee_count )
1324  {
1325  uint16_t offset = opinion_account.options.num_committee / 2;
1326  d._committee_count_histogram_buffer[offset] += num_committee_voting_stake;
1327  }
1328 
1329  d._total_voting_stake[0] += num_committee_voting_stake;
1330  d._total_voting_stake[1] += voting_stake[1];
1331  }
1332  }
1333  } tally_helper(*this);
1334 
1335  perform_account_maintenance( tally_helper );
1336 
1337  struct clear_canary {
1338  clear_canary(vector<uint64_t>& target): target(target){}
1339  ~clear_canary() { target.clear(); }
1340  private:
1341  vector<uint64_t>& target;
1342  };
1343  clear_canary a(_witness_count_histogram_buffer),
1344  b(_committee_count_histogram_buffer),
1345  c(_vote_tally_buffer);
1346 
1347  update_top_n_authorities(*this);
1348  update_active_witnesses();
1349  update_active_committee_members();
1350  update_worker_votes();
1351 
1352  modify(gpo, [&dgpo](global_property_object& p) {
1353  // Remove scaling of account registration fee
1355  (dgpo.accounts_registered_this_interval / p.parameters.accounts_per_fee_scale);
1356 
1357  if( p.pending_parameters )
1358  {
1359  p.parameters = std::move(*p.pending_parameters);
1360  p.pending_parameters.reset();
1361  }
1362  });
1363 
1364  auto next_maintenance_time = dgpo.next_maintenance_time;
1365  auto maintenance_interval = gpo.parameters.maintenance_interval;
1366 
1367  if( next_maintenance_time <= next_block.timestamp )
1368  {
1369  if( next_block.block_num() == 1 )
1370  next_maintenance_time = time_point_sec() +
1371  (((next_block.timestamp.sec_since_epoch() / maintenance_interval) + 1) * maintenance_interval);
1372  else
1373  {
1374  // We want to find the smallest k such that next_maintenance_time + k * maintenance_interval > head_block_time()
1375  // This implies k > ( head_block_time() - next_maintenance_time ) / maintenance_interval
1376  //
1377  // Let y be the right-hand side of this inequality, i.e.
1378  // y = ( head_block_time() - next_maintenance_time ) / maintenance_interval
1379  //
1380  // and let the fractional part f be y-floor(y). Clearly 0 <= f < 1.
1381  // We can rewrite f = y-floor(y) as floor(y) = y-f.
1382  //
1383  // Clearly k = floor(y)+1 has k > y as desired. Now we must
1384  // show that this is the least such k, i.e. k-1 <= y.
1385  //
1386  // But k-1 = floor(y)+1-1 = floor(y) = y-f <= y.
1387  // So this k suffices.
1388  //
1389  auto y = (head_block_time() - next_maintenance_time).to_seconds() / maintenance_interval;
1390  next_maintenance_time += (y+1) * maintenance_interval;
1391  }
1392  }
1393 
1394  if( (dgpo.next_maintenance_time < HARDFORK_613_TIME) && (next_maintenance_time >= HARDFORK_613_TIME) )
1395  deprecate_annual_members(*this);
1396 
1397  // To reset call_price of all call orders, then match by new rule, for hard fork core-343
1398  bool to_update_and_match_call_orders_for_hf_343 = false;
1399  if( (dgpo.next_maintenance_time <= HARDFORK_CORE_343_TIME) && (next_maintenance_time > HARDFORK_CORE_343_TIME) )
1400  to_update_and_match_call_orders_for_hf_343 = true;
1401 
1402  // Process inconsistent price feeds
1403  if( (dgpo.next_maintenance_time <= HARDFORK_CORE_868_890_TIME) && (next_maintenance_time > HARDFORK_CORE_868_890_TIME) )
1404  process_hf_868_890( *this, to_update_and_match_call_orders_for_hf_343 );
1405 
1406  // To reset call_price of all call orders, then match by new rule, for hard fork core-1270
1407  bool to_update_and_match_call_orders_for_hf_1270 = false;
1408  if( (dgpo.next_maintenance_time <= HARDFORK_CORE_1270_TIME) && (next_maintenance_time > HARDFORK_CORE_1270_TIME) )
1409  to_update_and_match_call_orders_for_hf_1270 = true;
1410 
1411  // make sure current_supply is less than or equal to max_supply
1412  if ( dgpo.next_maintenance_time <= HARDFORK_CORE_1465_TIME && next_maintenance_time > HARDFORK_CORE_1465_TIME )
1413  process_hf_1465(*this);
1414 
1415  // Fix supply issue
1416  if ( dgpo.next_maintenance_time <= HARDFORK_CORE_2103_TIME && next_maintenance_time > HARDFORK_CORE_2103_TIME )
1417  process_hf_2103(*this);
1418 
1419  modify(dgpo, [next_maintenance_time](dynamic_global_property_object& d) {
1420  d.next_maintenance_time = next_maintenance_time;
1422  });
1423 
1424  // We need to do it after updated next_maintenance_time, to apply new rules here, for hard fork core-343
1425  if( to_update_and_match_call_orders_for_hf_343 )
1426  {
1428  match_call_orders(*this);
1429  }
1430 
1431  // We need to do it after updated next_maintenance_time, to apply new rules here, for hard fork core-1270.
1432  if( to_update_and_match_call_orders_for_hf_1270 )
1433  {
1435  update_median_feeds(*this);
1436  match_call_orders(*this);
1437  }
1438 
1439  process_bitassets();
1441 
1442  // process_budget needs to run at the bottom because
1443  // it needs to know the next_maintenance_time
1444  process_budget();
1445 }
1446 
1447 } }
bool is_type() const
share_type approving_stake() const
share_type total_core_pol
Total amount of core token in other tickets.
bool is_active(fc::time_point_sec now) const
fc::time_point_sec timestamp
Definition: block.hpp:35
static const vote_recalc_options worker()
Definition: db_maint.cpp:1156
void modify(const T &obj, const Lambda &m)
void update_call_orders_hf_343(database &db)
Definition: db_maint.cpp:853
Wraps a derived index to intercept calls to create, modify, and remove so that callbacks may be fired...
Definition: index.hpp:309
bool upgrade_to_lifetime_member
If true, the account will be upgraded to a lifetime member; otherwise, it will add a year to the subs...
Definition: account.hpp:245
const T * find(object_id_type id) const
void adjust_balance(account_id_type account, asset delta)
Adjust a particular account&#39;s balance in a given asset by a delta.
Definition: db_balance.cpp:54
Manage an account&#39;s membership statusThis operation is used to upgrade an account to a member...
Definition: account.hpp:234
bool fill_or_kill
If this flag is set the entire order must be filled or the operation is rejected. ...
Definition: market.hpp:62
void finish(authority &out_auth)
Definition: vote_count.hpp:55
contains properties that only apply to bitassets (market issued assets)
void process_hf_868_890(database &db, bool skip_check_call_orders)
Definition: db_maint.cpp:1024
uint32_t sec_since_epoch() const
Definition: time.hpp:90
bool is_member(time_point_sec now) const
special_authority active_special_authority
uint16_t accounts_per_fee_scale
number of accounts between fee scalings
This class represents an account on the object graphAccounts are the primary unit of authority on the...
Maintains global state information (committee_member list, current fees)This is an implementation det...
tracks the blockchain state in an extensible manner
Definition: database.hpp:70
Definition: api.cpp:56
void process_hf_1465(database &db)
Definition: db_maint.cpp:952
account_id_type account_to_upgrade
The account to upgrade; must not already be a lifetime member.
Definition: account.hpp:243
#define GRAPHENE_PROXY_TO_SELF_ACCOUNT
Represents the canonical account for specifying you will vote directly (as opposed to a proxy) ...
Definition: config.hpp:135
tracks debt and call price information
Used to generate a useful error report when an exception is thrown.At each level in the stack where t...
Definition: exception.hpp:56
std::string to_detail_string(log_level ll=log_level::all) const
Definition: exception.cpp:183
const dynamic_global_property_object & get_dynamic_global_properties() const
Definition: db_getter.cpp:54
bool valid() const
Definition: optional.hpp:186
void add(Component who, uint64_t votes)
Definition: vote_count.hpp:37
uint32_t head_block_num() const
Definition: db_getter.cpp:69
optional< account_id_type > buyback_account
#define GRAPHENE_CORE_ASSET_CYCLE_RATE
Definition: config.hpp:51
immutable_chain_parameters immutable_parameters
share_type total_pol_value
Total value of tickets whose current type is not lock_forever.
uint8_t account_fee_scale_bitshifts
number of times to left bitshift account registration fee at each scaling
price call_price
Collateral / Debt.
visitor::result_type visit(visitor &v)
account_id_type issuer
ID of the account which issued this asset.
#define wlog(FORMAT,...)
Definition: logger.hpp:123
vote_recalc_options(uint32_t f, uint32_t d, uint32_t s)
Definition: db_maint.cpp:1104
bool is_null() const
Definition: asset.cpp:225
const global_property_object & get_global_properties() const
Definition: db_getter.cpp:44
#define GRAPHENE_MAX_SHARE_SUPPLY
Definition: config.hpp:38
const T & get(object_id_type id) const
uint64_t get_recalced_voting_stake(const uint64_t stake, const time_point_sec last_vote_time, const vote_recalc_times &recalc_times) const
Definition: db_maint.cpp:1130
void update_call_orders_hf_1270(database &db)
Definition: db_maint.cpp:879
void create_buyback_orders(database &db)
Definition: db_maint.cpp:694
provides stack-based nullable value similar to boost::optional
Definition: optional.hpp:20
void execute_bid(const collateral_bid_object &bid, share_type debt_covered, share_type collateral_from_fund, const price_feed &current_feed)
Definition: db_market.cpp:193
void distribute_fba_balances(database &db)
Definition: db_maint.cpp:687
worker_pay_visitor(share_type pay, database &db)
Definition: db_maint.cpp:125
void update_median_feeds(time_point_sec current_time, time_point_sec next_maintenance_time)
bool is_prediction_market
True if this asset implements a Prediction Market.
microseconds days(int64_t d)
Definition: time.hpp:38
operation_result apply_operation(transaction_evaluation_state &eval_state, const operation &op)
Definition: db_block.cpp:754
static time_point_sec maximum()
Definition: time.hpp:86
object_id_type id
Definition: object.hpp:73
uint16_t maintenance_collateral_ratio
Definition: asset.hpp:190
share_type total_pob_value
Total value of tickets whose current type is lock_forever.
void delete_expired_custom_authorities(database &db)
Remove any custom active authorities whose expiration dates are in the past.
Definition: db_maint.cpp:1087
flat_set< vote_id_type > votes
Definition: account.hpp:58
uint32_t push_applied_operation(const operation &op)
Definition: db_block.cpp:527
The price struct stores asset prices in the BitShares system.
Definition: asset.hpp:114
uint16_t maximum_witness_count
maximum number of active witnesses
int64_t count() const
Definition: time.hpp:28
#define GRAPHENE_100_PERCENT
Definition: config.hpp:102
optional< vesting_balance_id_type > cashback_vb
time_point_sec head_block_time() const
Definition: db_getter.cpp:64
flat_map< account_id_type, weight_type > account_auths
Definition: authority.hpp:120
#define ilog(FORMAT,...)
Definition: logger.hpp:117
#define GRAPHENE_COMMITTEE_ACCOUNT
Definition: config.hpp:125
share_type total_core_inactive
Total amount of core token in inactive lock_forever tickets.
worker_type worker
ID of this worker&#39;s pay balance.
void update_median_feeds(database &db)
Definition: db_maint.cpp:990
#define FC_CAPTURE_AND_RETHROW(...)
Definition: exception.hpp:478
instructs the blockchain to attempt to sell one asset for anotherThe blockchain will atempt to sell a...
Definition: market.hpp:48
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
bool is_configured(const database &db) const
Definition: fba_object.cpp:31
const chain_property_object & get_chain_properties() const
Definition: db_getter.cpp:49
tracks the asset information that changes frequentlyBecause the asset_object is very large it doesn&#39;t...
time_point_sec last_vote_time
last time voted
bids of collateral for debt after a black swan
optional< asset_id_type > designated_asset
Definition: fba_object.hpp:44
An ID for some votable object.
Definition: vote.hpp:51
void match_call_orders(database &db)
Match call orders for all bitAssets, including PMs.
Definition: db_maint.cpp:892
static const vote_recalc_options witness()
Definition: db_maint.cpp:1146
#define GRAPHENE_RELAXED_COMMITTEE_ACCOUNT
Represents the current committee members.
Definition: config.hpp:129
const Operation::fee_parameters_type & get() const
const account_statistics_object & get_account_stats_by_owner(account_id_type owner) const
Definition: db_getter.cpp:139
uint16_t maximum_committee_count
maximum number of active committee_members
share_type accumulated_fees
fees accumulate to be paid out over time
const asset_dynamic_data_object & get_core_dynamic_data() const
Definition: db_getter.cpp:39
tracks the parameters of an assetAll assets have a globally unique symbol name that controls how they...
void deprecate_annual_members(database &db)
Definition: db_maint.cpp:764
typename impl::transform< List, Transformer >::type transform
Transform elements of a typelist.
Definition: typelist.hpp:170
share_type daily_pay
Amount in CORE this worker will be paid each day.
void operator()(W &worker) const
Definition: db_maint.cpp:130
safe< int64_t > share_type
Definition: types.hpp:247
vector< committee_member_id_type > active_committee_members
share_type witness_pay_per_block
CORE to be allocated to witnesses (per block)
the bitasset is to be fed by witnesses
Definition: types.hpp:147
const object & get(object_id_type id) const
Definition: index.hpp:111
asset_id_type asset_id
Definition: asset.hpp:39
vote_id_type vote_against
Voting ID which represents disapproval of this worker.
bitasset_options options
The tunable options for BitAssets are stored in this field.
Worker object contains the details of a blockchain worker. See The Blockchain Worker System for detai...
vote_id_type vote_for
Voting ID which represents approval of this worker.
static const vote_recalc_options committee()
Definition: db_maint.cpp:1151
A visitor for worker_type which calls pay_worker on the worker within.
Definition: db_maint.cpp:118
account_statistics_id_type statistics
uint32_t block_num() const
Definition: block.hpp:34
void update_top_n_authorities(database &db)
Definition: db_maint.cpp:569
abstract base class for accessing objects indexed in various ways.
Definition: index.hpp:71
void remove(const object &obj)
Maintains global state information (committee_member list, current fees)This is an implementation det...
bool count_non_member_votes
set to false to restrict voting privlegages to member accounts
#define GRAPHENE_CORE_ASSET_CYCLE_RATE_BITS
Definition: config.hpp:52
share_type worker_budget_per_day
CORE to be allocated to workers (per day)
share_type current_supply
The number of shares currently in existence.
flat_map< account_id_type, pair< time_point_sec, price_feed_with_icr > > feeds
void visit_special_authorities(const database &db, Visitor visit)
Definition: db_maint.cpp:551
vote_recalc_times get_vote_recalc_times(const time_point_sec now) const
Definition: db_maint.cpp:1113
tracks information about a committee_member account.A committee_member is responsible for setting blo...
#define GRAPHENE_1_PERCENT
Definition: config.hpp:103
price max() const
Definition: asset.hpp:130
static price call_price(const asset &debt, const asset &collateral, uint16_t collateral_ratio)
Definition: asset.cpp:212
uint16_t num_committee_voted
Pre-calculated for better performance on chain maintenance.
share_type total_core_pob
Total amount of core token in active lock_forever tickets.
share_type settlement_fund
Amount of collateral which is available for force settlement.
uint8_t block_interval
interval in seconds between blocks
const asset_dynamic_data_object & dynamic_data(const DB &db) const
static const vote_recalc_options delegator()
Definition: db_maint.cpp:1161
const IndexType & get_index_type() const
T value
Definition: safe.hpp:22
special_authority owner_special_authority
optional< chain_parameters > pending_parameters
void process_hf_2103(database &db)
Definition: db_maint.cpp:977
bool check_call_orders(const asset_object &mia, bool enable_black_swan=true, bool for_new_limit_order=false, const asset_bitasset_data_object *bitasset_ptr=nullptr)
Definition: db_market.cpp:1100
void split_fba_balance(database &db, uint64_t fba_id, uint16_t network_pct, uint16_t designated_asset_buyback_pct, uint16_t designated_asset_issuer_pct)
Definition: db_maint.cpp:608
const asset_object & get_core_asset() const
Definition: db_getter.cpp:34
#define GRAPHENE_WITNESS_ACCOUNT
Represents the current witnesses.
Definition: config.hpp:127
share_type reserved(const DB &db) const
const index_type & indices() const