Scorum
witness.cpp
Go to the documentation of this file.
6 
10 
12 
13 namespace scorum {
14 namespace chain {
15 
17  witness_schedule_service_i& witness_schedule_svc,
20  : base_service_type(db)
21  , _dgp_svc(dgp_svc)
22  , _witness_schedule_svc(witness_schedule_svc)
23  , _chain_dba(chain_dba)
24 {
25 }
26 
28 {
29  try
30  {
31  return get_by<by_name>(name);
32  }
33  FC_CAPTURE_AND_RETHROW((name))
34 }
35 
37 {
38  return find_by<by_name>(name) != nullptr;
39 }
40 
42 {
43  const auto& idx = db_impl().get_index<witness_index>().indices().get<by_vote_name>();
44  FC_ASSERT(idx.begin() != idx.end(), "Empty witness_index by_vote_name.");
45  return (*idx.begin());
46 }
47 
49  const std::string& url,
50  const public_key_type& block_signing_key,
51  const chain_properties& props)
52 {
53  FC_ASSERT(owner.size(), "Witness 'owner_name' should not be empty.");
54 
55  const auto& new_witness = create_internal(owner, block_signing_key);
56 
57  update(new_witness, [&](witness_object& w) {
58  fc::from_string(w.url, url);
59  w.created = _dgp_svc.head_block_time();
60  w.proposed_chain_props = props;
61  });
62 
63  return new_witness;
64 }
65 
67  const public_key_type& block_signing_key)
68 {
69  const auto& new_witness = create_internal(owner, block_signing_key);
70 
71  update(new_witness, [&](witness_object& w) { w.schedule = witness_object::top20; });
72 
73  return new_witness;
74 }
75 
76 const witness_object& dbs_witness::create_internal(const account_name_type& owner,
77  const public_key_type& block_signing_key)
78 {
79  return create([&](witness_object& w) {
80  w.owner = owner;
81  w.signing_key = block_signing_key;
82  w.hardfork_time_vote = _chain_dba.get().genesis_time;
83  });
84 }
85 
87  const std::string& url,
88  const public_key_type& block_signing_key,
89  const chain_properties& props)
90 {
91  update(witness, [&](witness_object& w) {
92  fc::from_string(w.url, url);
93  w.signing_key = block_signing_key;
94  w.proposed_chain_props = props;
95  });
96 }
97 
98 void dbs_witness::adjust_witness_votes(const account_object& account, const share_type& delta)
99 {
100  const auto& vidx = db_impl().get_index<witness_vote_index>().indices().get<by_account_witness>();
101  auto itr = vidx.lower_bound(boost::make_tuple(account.id, witness_id_type()));
102  while (itr != vidx.end() && itr->account == account.id)
103  {
104  adjust_witness_vote(db_impl().get(itr->witness), delta);
105  ++itr;
106  }
107 }
108 
110 {
111  block_info ctx = get_head_block_context();
112 
113  const auto& props = _dgp_svc.get();
114  const auto& wso = _witness_schedule_svc.get();
115 
116  update(witness, [&](witness_object& w) {
117  debug_log(ctx, "updating votes for witness=${w}", ("w", w.owner));
118 
119  auto delta_pos = w.votes.value * (wso.current_virtual_time - w.virtual_last_update);
120  w.virtual_position += delta_pos;
121 
122  w.virtual_last_update = wso.current_virtual_time;
123 
124  debug_log(ctx, "old_votes=${v}", ("v", w.votes));
125 
126  w.votes += delta;
127  FC_ASSERT(w.votes <= props.total_scorumpower.amount, "",
128  ("w.votes", w.votes)("props", props.total_scorumpower));
129 
131  = w.virtual_last_update + (VIRTUAL_SCHEDULE_LAP_LENGTH - w.virtual_position) / (w.votes.value + 1);
132 
135  if (w.virtual_scheduled_time < wso.current_virtual_time)
136  w.virtual_scheduled_time = fc::uint128::max_value();
137 
138  debug_log(ctx, "new_votes=${v}", ("v", w.votes));
139  });
140 }
141 
142 block_info dbs_witness::get_head_block_context()
143 {
144  const auto& dprop = _dgp_svc.get();
145  block_info ctx(dprop.head_block_number, dprop.head_block_id.str(), dprop.time, dprop.current_witness);
146 
147  return ctx;
148 }
149 } // namespace chain
150 } // namespace scorum
dba::db_index & db_impl()
Definition: dbs_base.cpp:22
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
const witness_object & create_witness(const account_name_type &owner, const std::string &url, const public_key_type &block_signing_key, const chain_properties &props) override
Definition: witness.cpp:48
void update_witness(const witness_object &witness, const std::string &url, const public_key_type &block_signing_key, const chain_properties &props) override
Definition: witness.cpp:86
void adjust_witness_votes(const account_object &account, const share_type &delta) override
Definition: witness.cpp:98
dbs_witness(dba::db_index &db, witness_schedule_service_i &, dynamic_global_property_service_i &, dba::db_accessor< chain_property_object > &)
Definition: witness.cpp:16
const witness_object & get_top_witness() const override
Definition: witness.cpp:41
const witness_object & create_initial_witness(const account_name_type &owner, const public_key_type &block_signing_key) override
Definition: witness.cpp:66
void adjust_witness_vote(const witness_object &witness, const share_type &delta) override
Definition: witness.cpp:109
chain_properties proposed_chain_props
witness_schedule_type schedule
#define debug_log(CTX, FORMAT,...)
Definition: debug_log.hpp:3
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
shared_multi_index_container< witness_object, indexed_by< ordered_unique< tag< by_id >, member< witness_object, witness_id_type, &witness_object::id > >, ordered_unique< tag< by_name >, member< witness_object, account_name_type, &witness_object::owner > >, ordered_unique< tag< by_vote_name >, composite_key< witness_object, member< witness_object, share_type, &witness_object::votes >, member< witness_object, account_name_type, &witness_object::owner > >, composite_key_compare< std::greater< share_type >, std::less< account_name_type > > >, ordered_unique< tag< by_schedule_time >, composite_key< witness_object, member< witness_object, fc::uint128, &witness_object::virtual_scheduled_time >, member< witness_object, witness_id_type, &witness_object::id > > > > > witness_index
fc::safe< share_value_type > share_type
Definition: types.hpp:73
fc::fixed_string_16 account_name_type
Definition: types.hpp:62
Definition: asset.cpp:15
virtual const object_type & get() const =0
virtual fc::time_point_sec head_block_time() const =0