BitShares-Core  4.0.0
BitShares blockchain implementation and command-line interface 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 
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 timestamp
Definition: block.hpp:35
void modify(const T &obj, const Lambda &m)
uint32_t sec_since_epoch() const
Definition: time.hpp:90
Maintains global state information (committee_member list, current fees)This is an implementation det...
const witness_schedule_object & get_witness_schedule_object() const
Definition: db_getter.cpp:144
Definition: api.cpp:56
uint8_t maintenance_skip_slots
number of block_intervals to skip at maintenance time
const dynamic_global_property_object & get_dynamic_global_properties() const
Definition: db_getter.cpp:54
uint32_t head_block_num() const
Definition: db_getter.cpp:69
const global_property_object & get_global_properties() const
Definition: db_getter.cpp:44
uint32_t get_slot_at_time(fc::time_point_sec when) const
#define GRAPHENE_100_PERCENT
Definition: config.hpp:102
time_point_sec head_block_time() const
Definition: db_getter.cpp:64
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
uint32_t witness_participation_rate() const
Maintains global state information (committee_member list, current fees)This is an implementation det...
uint8_t popcount(uint64_t v)
Definition: popcount.cpp:31
witness_id_type get_scheduled_witness(uint32_t slot_num) const
Get the witness scheduled for block production in a slot.
fc::time_point_sec get_slot_time(uint32_t slot_num) const