BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
db_witness_schedule.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 
29 
30 #include <fc/popcount.hpp>
31 
32 namespace graphene { namespace chain {
33 
34 using boost::container::flat_set;
35 
36 witness_id_type database::get_scheduled_witness( uint32_t slot_num )const
37 {
40  uint64_t current_aslot = dpo.current_aslot + slot_num;
41  return wso.current_shuffled_witnesses[ current_aslot % wso.current_shuffled_witnesses.size() ];
42 }
43 
45 {
46  if( slot_num == 0 )
47  return fc::time_point_sec();
48 
49  auto interval = block_interval();
51 
52  if( head_block_num() == 0 )
53  {
54  // n.b. first block is at genesis_time plus one block interval
55  fc::time_point_sec genesis_time = dpo.time;
56  return genesis_time + slot_num * interval;
57  }
58 
59  int64_t head_block_abs_slot = head_block_time().sec_since_epoch() / interval;
60  fc::time_point_sec head_slot_time(head_block_abs_slot * interval);
61 
63 
65  slot_num += gpo.parameters.maintenance_skip_slots;
66 
67  // "slot 0" is head_slot_time
68  // "slot 1" is head_slot_time,
69  // plus maint interval if head block is a maint block
70  // plus block interval if head block is not a maint block
71  return head_slot_time + (slot_num * interval);
72 }
73 
75 {
76  fc::time_point_sec first_slot_time = get_slot_time( 1 );
77  if( when < first_slot_time )
78  return 0;
79  return (when - first_slot_time).to_seconds() / block_interval() + 1;
80 }
81 
82 uint32_t database::update_witness_missed_blocks( const signed_block& b )
83 {
84  uint32_t missed_blocks = get_slot_at_time( b.timestamp );
85  FC_ASSERT( missed_blocks != 0, "Trying to push double-produced block onto current block?!" );
86  missed_blocks--;
87  const auto& witnesses = witness_schedule_id_type()(*this).current_shuffled_witnesses;
88  if( missed_blocks < witnesses.size() )
89  for( uint32_t i = 0; i < missed_blocks; ++i ) {
90  const auto& witness_missed = get_scheduled_witness( i+1 )(*this);
91  modify( witness_missed, []( witness_object& w ) {
92  w.total_missed++;
93  });
94  }
95  return missed_blocks;
96 }
97 
99 {
101  return uint64_t(GRAPHENE_100_PERCENT) * fc::popcount(dpo.recent_slots_filled) / 128;
102 }
103 
104 void database::update_witness_schedule()
105 {
108 
109  if( head_block_num() % gpo.active_witnesses.size() == 0 )
110  {
111  modify( wso, [&]( witness_schedule_object& _wso )
112  {
113  _wso.current_shuffled_witnesses.clear();
114  _wso.current_shuffled_witnesses.reserve( gpo.active_witnesses.size() );
115 
116  for( const witness_id_type& w : gpo.active_witnesses )
117  _wso.current_shuffled_witnesses.push_back( w );
118 
119  auto now_hi = uint64_t(head_block_time().sec_since_epoch()) << 32;
120  for( uint32_t i = 0; i < _wso.current_shuffled_witnesses.size(); ++i )
121  {
124  uint64_t k = now_hi + uint64_t(i)*2685821657736338717ULL;
125  k ^= (k >> 12);
126  k ^= (k << 25);
127  k ^= (k >> 27);
128  k *= 2685821657736338717ULL;
129 
130  uint32_t jmax = _wso.current_shuffled_witnesses.size() - i;
131  uint32_t j = i + k%jmax;
132  std::swap( _wso.current_shuffled_witnesses[i],
133  _wso.current_shuffled_witnesses[j] );
134  }
135  });
136  }
137 }
138 
139 } }
fc::time_point_sec::sec_since_epoch
uint32_t sec_since_epoch() const
Definition: time.hpp:90
graphene::chain::dynamic_global_property_object
Maintains global state information (committee_member list, current fees)
Definition: global_property_object.hpp:62
graphene::chain::dynamic_global_property_object::current_aslot
uint64_t current_aslot
Definition: global_property_object.hpp:91
graphene::chain::witness_object::total_missed
int64_t total_missed
Definition: witness_object.hpp:42
graphene::chain::dynamic_global_property_object::time
time_point_sec time
Definition: global_property_object.hpp:68
graphene::chain::database::head_block_time
time_point_sec head_block_time() const
Definition: db_getter.cpp:67
database.hpp
graphene::chain::global_property_object::parameters
chain_parameters parameters
Definition: global_property_object.hpp:44
graphene::protocol::chain_parameters::maintenance_skip_slots
uint8_t maintenance_skip_slots
number of block_intervals to skip at maintenance time
Definition: chain_parameters.hpp:55
graphene::chain::dynamic_global_property_object::recent_slots_filled
fc::uint128_t recent_slots_filled
Definition: global_property_object.hpp:96
graphene::chain::database::get_slot_time
fc::time_point_sec get_slot_time(uint32_t slot_num) const
Definition: db_witness_schedule.cpp:44
fc::popcount
uint8_t popcount(uint64_t v)
Definition: popcount.cpp:31
graphene::chain::database::witness_participation_rate
uint32_t witness_participation_rate() const
Definition: db_witness_schedule.cpp:98
fc::time_point_sec
Definition: time.hpp:74
graphene::protocol::block_header::timestamp
fc::time_point_sec timestamp
Definition: block.hpp:35
graphene::chain::database::head_block_num
uint32_t head_block_num() const
Definition: db_getter.cpp:72
graphene::chain::database::get_slot_at_time
uint32_t get_slot_at_time(fc::time_point_sec when) const
Definition: db_witness_schedule.cpp:74
graphene::chain::dynamic_global_property_object::dynamic_flags
uint32_t dynamic_flags
Definition: global_property_object.hpp:102
FC_ASSERT
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
graphene::chain::witness_schedule_object::current_shuffled_witnesses
vector< witness_id_type > current_shuffled_witnesses
Definition: witness_schedule_object.hpp:34
graphene::chain::global_property_object::active_witnesses
flat_set< witness_id_type > active_witnesses
Definition: global_property_object.hpp:49
graphene::chain::global_property_object
Maintains global state information (committee_member list, current fees)
Definition: global_property_object.hpp:40
popcount.hpp
graphene::chain::witness_object
Definition: witness_object.hpp:32
graphene::chain::witness_schedule_object
Definition: witness_schedule_object.hpp:30
graphene::chain::database::get_witness_schedule_object
const witness_schedule_object & get_witness_schedule_object() const
Definition: db_getter.cpp:147
graphene::chain::database::get_global_properties
const global_property_object & get_global_properties() const
Definition: db_getter.cpp:47
witness_schedule_object.hpp
graphene::chain::dynamic_global_property_object::maintenance_flag
@ maintenance_flag
Definition: global_property_object.hpp:117
graphene::protocol::signed_block
Definition: block.hpp:64
witness_object.hpp
graphene::chain::database::get_dynamic_global_properties
const dynamic_global_property_object & get_dynamic_global_properties() const
Definition: db_getter.cpp:57
graphene::chain::database::get_scheduled_witness
witness_id_type get_scheduled_witness(uint32_t slot_num) const
Get the witness scheduled for block production in a slot.
Definition: db_witness_schedule.cpp:36
global_property_object.hpp
GRAPHENE_100_PERCENT
#define GRAPHENE_100_PERCENT
Definition: config.hpp:102
graphene
Definition: api.cpp:48
graphene::db::object_database::modify
void modify(const T &obj, const Lambda &m)
Definition: object_database.hpp:99