9 #include <scorum/rewards_math/formulas.hpp>
11 #include <boost/lambda/lambda.hpp>
12 #include <boost/multi_index/detail/unbounded.hpp>
20 , _witness_svc(witness_svc)
28 return get_by<by_id>(account_id);
30 FC_CAPTURE_AND_RETHROW((account_id))
37 return get_by<by_name>(name);
39 FC_CAPTURE_AND_RETHROW((name))
44 return find_by<by_name>(name) !=
nullptr;
51 return db_impl().get<account_authority_object, by_account>(name);
53 FC_CAPTURE_AND_RETHROW((name))
57 const optional<const char*>& context_type_name)
const
59 auto acc = find_by<by_name>(name);
60 if (context_type_name.valid())
62 FC_ASSERT(acc !=
nullptr,
"\"${1}\" \"${2}\" must exist.", (
"1", *context_type_name)(
"2", name));
66 FC_ASSERT(acc !=
nullptr,
"Account \"${1}\" must exist.", (
"1", name));
71 const optional<const char*>& context_type_name)
const
73 for (
const auto& a : names)
82 const std::string& json_metadata)
84 FC_ASSERT(new_account_name.size() > 0,
"Account 'name' should not be empty.");
90 owner.add_authority(memo_key, 1);
91 owner.weight_threshold = 1;
93 const auto& new_account
96 update(new_account, [&](account_object& acc) {
97 acc.created_by_genesis =
true;
98 acc.balance = balance;
107 const std::string& json_metadata,
112 const auto& new_account =
create([&](account_object& acc) {
113 acc.name = new_account_name;
114 acc.recovery_account = creator_name;
115 acc.memo_key = memo_key;
118 fc::from_string(acc.json_metadata, json_metadata);
124 db_impl().create<account_authority_object>([&](account_authority_object& auth) {
125 auth.account = new_account_name;
129 auth.last_owner_update = fc::time_point_sec::min();
139 const std::string& json_metadata,
150 const auto& new_account
162 const std::string& json_metadata,
167 const asset& delegation)
170 FC_ASSERT(delegation.
symbol() ==
SP_SYMBOL,
"invalid asset type (symbol)");
174 update(creator, [&](account_object& c) { c.delegated_scorumpower += delegation; });
178 const auto& new_account
181 update(new_account, [&](account_object& acc) { acc.received_scorumpower = delegation; });
183 if (delegation.
amount > 0)
187 db_impl().create<scorumpower_delegation_object>([&](scorumpower_delegation_object& vdo) {
188 vdo.delegator = creator_name;
189 vdo.delegatee = new_account_name;
190 vdo.scorumpower = delegation;
204 const std::string& json_metadata,
212 const auto& new_account
224 const account_authority_object& account_authority,
226 const std::string& json_metadata,
227 const optional<authority>&
owner,
228 const optional<authority>&
active,
229 const optional<authority>&
posting)
233 update(account, [&](account_object& acc) {
235 acc.memo_key = memo_key;
239 acc.active_challenged = false;
240 acc.last_active_proved = t;
243 acc.last_account_update = t;
246 if (json_metadata.size() > 0)
247 fc::from_string(acc.json_metadata, json_metadata);
253 db_impl().modify(account_authority, [&](account_authority_object& auth) {
266 db_impl().create<owner_authority_history_object>([&](owner_authority_history_object& hist) {
267 hist.account = account.name;
268 hist.previous_owner_authority =
db_impl().get<account_authority_object, by_account>(account.name).
owner;
269 hist.last_valid_time = t;
272 db_impl().modify(
db_impl().get<account_authority_object, by_account>(account.name),
273 [&](account_authority_object& auth) {
274 auth.owner = owner_authority;
275 auth.last_owner_update = t;
283 update(account, [&](account_object& acnt) { acnt.balance += amount; });
305 update(account, [&](account_object& acnt) { acnt.active_sp_holders_pending_scr_reward += amount; });
317 FC_ASSERT(amount.
symbol() ==
SP_SYMBOL,
"invalid asset type (symbol)");
318 update(account, [&](account_object& a) { a.scorumpower += amount; });
335 FC_ASSERT(amount.
symbol() ==
SP_SYMBOL,
"invalid asset type (symbol)");
336 update(account, [&](account_object& acnt) { acnt.active_sp_holders_pending_sp_reward += amount; });
354 return new_scorumpower;
356 FC_CAPTURE_AND_RETHROW((to_account.name)(
scorum))
361 FC_ASSERT(amount.
symbol() ==
SP_SYMBOL,
"invalid asset type (symbol)");
362 update(account, [&](account_object& a) { a.delegated_scorumpower += amount; });
373 FC_ASSERT(amount.
symbol() ==
SP_SYMBOL,
"invalid asset type (symbol)");
374 update(account, [&](account_object& a) { a.received_scorumpower += amount; });
392 if (account.active_challenged)
394 update(account, [&](account_object& a) {
395 a.active_challenged =
false;
396 a.last_active_proved = t;
405 update(account, [&](account_object& a) {
406 a.active_challenged =
false;
407 a.last_active_proved = t;
410 a.owner_challenged =
false;
411 a.last_owner_proved = t;
418 update(account, [&](account_object& a) { a.witnesses_voted_for++; });
423 update(account, [&](account_object& a) { a.witnesses_voted_for--; });
430 update(author_account, [&](account_object& a) {
431 if (parent_author_name == SCORUM_ROOT_POST_PARENT_ACCOUNT)
433 a.last_root_post = t;
441 if (voting_power < account.voting_power)
448 update(account, [&](account_object& a) {
449 a.voting_power = voting_power;
450 a.last_vote_time = t;
451 a.voting_power_restoring_time = scorum::rewards_math::calculate_expected_restoring_time(
453 a.vote_reward_competitive_sp = a.effective_scorumpower();
459 if (account.active_sp_holders_cashout_time == fc::time_point_sec::maximum())
461 update(account, [&](account_object& a) {
463 a.active_sp_holders_cashout_time = cashout_time;
473 const auto& recovery_request_idx
474 =
db_impl().get_index<account_recovery_request_index>().indices().get<by_account>();
475 auto request = recovery_request_idx.find(account_to_recover);
477 if (request == recovery_request_idx.end())
479 FC_ASSERT(!new_owner_authority.
is_impossible(),
"Cannot recover using an impossible authority.");
480 FC_ASSERT(new_owner_authority.
weight_threshold,
"Cannot recover using an open authority.");
484 db_impl().create<account_recovery_request_object>([&](account_recovery_request_object& req) {
485 req.account_to_recover = account_to_recover;
486 req.new_owner_authority = new_owner_authority;
496 FC_ASSERT(!new_owner_authority.
is_impossible(),
"Cannot recover using an impossible authority.");
500 db_impl().modify(*request, [&](account_recovery_request_object& req) {
501 req.new_owner_authority = new_owner_authority;
513 const auto& recovery_request_idx
514 =
db_impl().get_index<account_recovery_request_index>().indices().get<by_account>();
515 auto request = recovery_request_idx.find(account_to_recover.name);
517 FC_ASSERT(request != recovery_request_idx.end(),
"There are no active recovery requests for this account.");
518 FC_ASSERT(request->new_owner_authority == new_owner_authority,
519 "New owner authority does not match recovery request.");
521 const auto& recent_auth_idx =
db_impl().get_index<owner_authority_history_index>().indices().get<by_account>();
522 auto hist = recent_auth_idx.lower_bound(account_to_recover.name);
525 while (hist != recent_auth_idx.end() && hist->account == account_to_recover.name && !found)
527 found = hist->previous_owner_authority == recent_owner_authority;
533 FC_ASSERT(found,
"Recent authority not found in authority history.");
537 update(account_to_recover, [&](account_object& a) { a.last_account_recovery = t; });
545 const auto& change_recovery_idx
546 =
db_impl().get_index<change_recovery_account_request_index>().indices().get<by_account>();
547 auto request = change_recovery_idx.find(account_to_recover.name);
549 if (request == change_recovery_idx.end())
551 db_impl().create<change_recovery_account_request_object>([&](change_recovery_account_request_object& req) {
552 req.account_to_recover = account_to_recover.name;
553 req.recovery_account = new_recovery_account_name;
557 else if (account_to_recover.recovery_account != new_recovery_account_name)
559 db_impl().modify(*request, [&](change_recovery_account_request_object& req) {
560 req.recovery_account = new_recovery_account_name;
573 std::array<share_type, SCORUM_MAX_PROXY_RECURSION_DEPTH + 1> delta;
574 delta[0] = -account.scorumpower.amount;
576 delta[i + 1] = -account.proxied_vsf_votes[i];
580 if (proxy_account.valid())
582 flat_set<account_id_type> proxy_chain({ account.id, (*proxy_account).id });
586 auto cprox = &(*proxy_account);
587 while (cprox->proxy.size() != 0)
590 FC_ASSERT(proxy_chain.insert(next_proxy.id).second,
"This proxy would create a proxy loop.");
598 update(account, [&](account_object& a) { a.proxy = (*proxy_account).name; });
602 delta[i] = -delta[i];
614 auto itr = vidx.lower_bound(boost::make_tuple(account.id,
witness_id_type()));
615 while (itr != vidx.end() && itr->account == account.id)
617 const auto& current = *itr;
622 update(account, [&](account_object& acc) { acc.witnesses_voted_for = 0; });
626 const account_object& account,
const std::array<share_type, SCORUM_MAX_PROXY_RECURSION_DEPTH + 1>& delta,
int depth)
628 if (account.proxy != SCORUM_PROXY_TO_SELF_ACCOUNT)
636 update(proxy, [&](account_object& a) {
639 a.proxied_vsf_votes[i + depth] += delta[i];
649 total_delta += delta[i];
657 if (account.proxy != SCORUM_PROXY_TO_SELF_ACCOUNT)
665 update(proxy, [&](account_object& a) { a.proxied_vsf_votes[depth] += delta; });
677 fc::time_point_sec min_vote_time_for_cashout = _dgp_svc.
head_block_time();
679 return get_range_by<by_voting_power_restoring_time>(min_vote_time_for_cashout < boost::lambda::_1,
685 foreach_by<by_id>(call);
692 ::boost::lambda::_1 <= std::make_tuple(until,
ALL_IDS));
694 FC_CAPTURE_AND_RETHROW((until))
701 const auto& account_idx =
db_impl().get_index<account_index>().indices().get<by_name>();
702 for (
auto itr = account_idx.begin(); itr != account_idx.end(); ++itr)
704 totals.
scr += itr->balance;
705 totals.
sp += itr->scorumpower;
706 totals.
pending_scr += itr->active_sp_holders_pending_scr_reward;
707 totals.
pending_sp += itr->active_sp_holders_pending_sp_reward;
709 totals.
vsf_votes += (itr->proxy == SCORUM_PROXY_TO_SELF_ACCOUNT
710 ? itr->witness_vote_weight()
713 : itr->scorumpower.amount));
virtual account_refs_type get_by_cashout_time(const fc::time_point_sec &until) const override
virtual void add_post(const account_object &author_account, const account_name_type &parent_author_name) override
virtual void increase_delegated_scorumpower(const account_object &account, const asset &amount) override
virtual void increase_pending_scorumpower(const account_object &account, const asset &amount) override
virtual void update_owner_authority(const account_object &account, const authority &owner_authority) override
virtual void decrease_scorumpower(const account_object &account, const asset &amount) override
virtual const account_object & create_account_with_delegation(const account_name_type &new_account_name, const account_name_type &creator_name, const public_key_type &memo_key, const std::string &json_metadata, const authority &owner, const authority &active, const authority &posting, const asset &fee_in_scorums, const asset &delegation_in_scorumpower) override
virtual void increase_received_scorumpower(account_name_type account_name, const asset &amount) override
virtual const account_authority_object & get_account_authority(const account_name_type &) const override
virtual void foreach_account(account_call_type &&) const override
virtual void update_voting_power(const account_object &account, uint16_t voting_power) override
virtual const account_object & create_account_with_bonus(const account_name_type &new_account_name, const account_name_type &creator_name, const public_key_type &memo_key, const std::string &json_metadata, const authority &owner, const authority &active, const authority &posting, const asset &bonus) override
virtual void increase_balance(const account_object &account, const asset &amount) override
virtual account_refs_type get_active_sp_holders() const override
virtual void check_account_existence(const account_name_type &, const optional< const char * > &context_type_name=optional< const char * >()) const override
virtual void decrease_balance(const account_object &account, const asset &amount) override
virtual void decrease_pending_scorumpower(const account_object &account, const asset &amount) override
virtual void decrease_pending_balance(const account_object &account, const asset &amount) override
virtual void drop_challenged(const account_object &account) override
virtual void clear_witness_votes(const account_object &account) override
virtual void update_active_sp_holders_cashout_time(const account_object &account) override
virtual void update_acount(const account_object &account, const account_authority_object &account_authority, const public_key_type &memo_key, const std::string &json_metadata, const optional< authority > &owner, const optional< authority > &active, const optional< authority > &posting) override
virtual void increase_scorumpower(const account_object &account, const asset &amount) override
virtual void decrease_witnesses_voted_for(const account_object &account) override
virtual const account_object & create_initial_account(const account_name_type &new_account_name, const public_key_type &memo_key, const asset &balance_in_scorums, const std::string &json_metadata) override
virtual void change_recovery_account(const account_object &account_to_recover, const account_name_type &new_recovery_account) override
virtual void decrease_received_scorumpower(account_name_type account_name, const asset &amount) override
virtual void create_account_recovery(const account_name_type &account_to_recover_name, const authority &new_owner_authority) override
virtual const asset create_scorumpower(const account_object &to_account, const asset &scorum) override
dbs_account(dba::db_index &, dynamic_global_property_service_i &, witness_service_i &)
const account_object & create_account(const account_name_type &new_account_name, const account_name_type &creator_name, const public_key_type &memo_key, const std::string &json_metadata, const authority &owner, const authority &active, const authority &posting) override
virtual void submit_account_recovery(const account_object &account_to_recover, const authority &new_owner_authority, const authority &recent_owner_authority) override
virtual void increase_pending_balance(const account_object &account, const asset &amount) override
virtual void increase_witnesses_voted_for(const account_object &account) override
virtual void adjust_proxied_witness_votes(const account_object &account, const std::array< share_type, SCORUM_MAX_PROXY_RECURSION_DEPTH+1 > &delta, int depth=0) override
virtual void prove_authority(const account_object &account, bool require_owner) override
virtual const account_object & get_account(const account_name_type &) const override
virtual accounts_total accounts_circulating_capital() const override
virtual void update_voting_proxy(const account_object &account, const optional< account_object > &proxy_account) override
dba::db_index & db_impl()
virtual const object_type & create(const modifier_type &modifier) override
virtual void update(const modifier_type &modifier) override
virtual const object_type & get() const override
virtual bool is_exists() const override
Maintains global state information.
asset total_scorumpower
Total SP on accounts scorumpower.
asset circulating_capital
Total SCR and SP on circulating (on account balances). circulating_capital <= total_supply.
asset total_pending_scr
Total amount of pending SCR.
asset total_pending_sp
Total amount of pending SP.
#define SCORUM_OWNER_AUTH_RECOVERY_PERIOD
#define SCORUM_ACTIVE_SP_HOLDERS_REWARD_PERIOD
#define SCORUM_VOTE_REGENERATION_SECONDS
#define SCORUM_MAX_PROXY_RECURSION_DEPTH
#define SCORUM_ACCOUNT_RECOVERY_REQUEST_EXPIRATION_PERIOD
#define SCORUM_CREATE_ACCOUNT_DELEGATION_TIME
const unbounded_placeholder unbounded
shared_multi_index_container< witness_vote_object, indexed_by< ordered_unique< tag< by_id >, member< witness_vote_object, witness_vote_id_type, &witness_vote_object::id > >, ordered_unique< tag< by_account_witness >, composite_key< witness_vote_object, member< witness_vote_object, account_id_type, &witness_vote_object::account >, member< witness_vote_object, witness_id_type, &witness_vote_object::witness > >, composite_key_compare< std::less< account_id_type >, std::less< witness_id_type > > >, ordered_unique< tag< by_witness_account >, composite_key< witness_vote_object, member< witness_vote_object, witness_id_type, &witness_vote_object::witness >, member< witness_vote_object, account_id_type, &witness_vote_object::account > >, composite_key_compare< std::less< witness_id_type >, std::less< account_id_type > > > > > witness_vote_index
fc::safe< share_value_type > share_type
flat_map< account_name_type, authority_weight_type > account_authority_map
fc::fixed_string_16 account_name_type
std::vector< cref_type > account_refs_type
typename base_service_i::call_type account_call_type
asset scr
sum of all SCR balances
asset pending_scr
sum of all pending SCR balances
asset sp
sum of all SP balances
asset pending_sp
sum of all pending SP balances
virtual void update(const modifier_type &modifier)=0
virtual fc::time_point_sec head_block_time() const =0
virtual void adjust_witness_votes(const account_object &account, const share_type &delta)=0
asset_symbol_type symbol() const
account_authority_map account_auths
uint32_t weight_threshold
bool is_impossible() const