block_header_state.cpp 8.0 KB
Newer Older
D
Daniel Larimer 已提交
1 2
#include <eosio/chain/block_header_state.hpp>
#include <eosio/chain/exceptions.hpp>
3
#include <limits>
D
Daniel Larimer 已提交
4 5 6 7

namespace eosio { namespace chain {

  uint32_t block_header_state::calc_dpos_last_irreversible()const {
8
    if( producer_to_last_produced.size() == 0 )
D
Daniel Larimer 已提交
9 10 11 12
       return 0;

    vector<uint32_t> irb;
    irb.reserve( producer_to_last_produced.size() );
13
    for( const auto& item : producer_to_last_produced )
D
Daniel Larimer 已提交
14 15 16 17 18 19 20 21
       irb.push_back(item.second);

    size_t offset = EOS_PERCENT(irb.size(), config::percent_100- config::irreversible_threshold_percent);
    std::nth_element( irb.begin(), irb.begin() + offset, irb.end() );

    return irb[offset];
  }

22 23 24
   bool block_header_state::is_active_producer( account_name n )const {
      return producer_to_last_produced.find(n) != producer_to_last_produced.end();
   }
D
Daniel Larimer 已提交
25

26 27 28 29 30 31
   block_timestamp_type block_header_state::get_slot_time( uint32_t slot_num )const {
      auto t = header.timestamp;
      FC_ASSERT( std::numeric_limits<decltype(t.slot)>::max() - t.slot >= slot_num, "block timestamp overflow" );
      t.slot += slot_num;
      return t;
   }
D
Daniel Larimer 已提交
32

33 34 35 36 37 38 39 40
   uint32_t block_header_state::get_slot_at_time( block_timestamp_type t )const {
      auto first_slot_time = get_slot_time(1);
      if( t < first_slot_time )
         return 0;
      return (t.slot - first_slot_time.slot + 1);
   }

   producer_key block_header_state::get_scheduled_producer( uint32_t slot_num )const {
A
arhag 已提交
41 42 43 44 45
      return get_scheduled_producer( get_slot_time(slot_num) );
   }

   producer_key block_header_state::get_scheduled_producer( block_timestamp_type t )const {
      auto index = t.slot % (active_schedule.producers.size() * config::producer_repetitions);
46 47 48 49 50 51 52 53
      index /= config::producer_repetitions;
      return active_schedule.producers[index];
   }

   uint32_t block_header_state::producer_participation_rate()const
   {
      return static_cast<uint32_t>(config::percent_100); // Ignore participation rate for now until we construct a better metric.
   }
D
Daniel Larimer 已提交
54 55 56 57 58 59 60 61


  /**
   *  Generate a template block header state for a given block time, it will not
   *  contain a transaction mroot, action mroot, or new_producers as those components
   *  are derived from chain state.
   */
  block_header_state block_header_state::generate_next( block_timestamp_type when )const {
62
    block_header_state result;
D
Daniel Larimer 已提交
63

64 65 66 67 68 69 70 71
    if( when != block_timestamp_type() ) {
       FC_ASSERT( when > header.timestamp, "next block must be in the future" );
    } else {
       (when = header.timestamp).slot++;
    }
    result.header.timestamp                      = when;
    result.header.previous                       = id;
    result.header.schedule_version               = active_schedule.version;
D
Daniel Larimer 已提交
72

A
arhag 已提交
73
    auto prokey                                  = get_scheduled_producer(when);
D
Daniel Larimer 已提交
74 75
    result.block_signing_key                     = prokey.block_signing_key;
    result.header.producer                       = prokey.producer_name;
76

77
    result.pending_schedule_lib_num              = pending_schedule_lib_num;
D
Daniel Larimer 已提交
78 79 80
    result.pending_schedule_hash                 = pending_schedule_hash;
    result.block_num                             = block_num + 1;
    result.producer_to_last_produced             = producer_to_last_produced;
D
Daniel Larimer 已提交
81
    result.producer_to_last_produced[prokey.producer_name] = result.block_num;
82
    result.dpos_last_irreversible_blocknum       = result.calc_dpos_last_irreversible();
83 84
    result.bft_irreversible_blocknum             = 
                std::max(bft_irreversible_blocknum,result.dpos_last_irreversible_blocknum);
D
Daniel Larimer 已提交
85 86
    result.blockroot_merkle = blockroot_merkle;
    result.blockroot_merkle.append( id );
87 88

    auto block_mroot = result.blockroot_merkle.get_root();
D
Daniel Larimer 已提交
89

D
Daniel Larimer 已提交
90 91 92 93
    result.active_schedule  = active_schedule;
    result.pending_schedule = pending_schedule;


94
    if( result.pending_schedule.producers.size() &&
D
Daniel Larimer 已提交
95
        result.dpos_last_irreversible_blocknum >= pending_schedule_lib_num ) {
96
      result.active_schedule = move( result.pending_schedule );
D
Daniel Larimer 已提交
97 98 99 100 101 102 103 104 105 106 107 108

      flat_map<account_name,uint32_t> new_producer_to_last_produced;
      for( const auto& pro : result.active_schedule.producers ) {
        auto existing = producer_to_last_produced.find( pro.producer_name );
        if( existing != producer_to_last_produced.end() ) {
          new_producer_to_last_produced[pro.producer_name] = existing->second;
        } else {
          new_producer_to_last_produced[pro.producer_name] = result.dpos_last_irreversible_blocknum;
        }
      }
      result.producer_to_last_produced = move( new_producer_to_last_produced );
      result.producer_to_last_produced[prokey.producer_name] = result.block_num;
109
    }
D
Daniel Larimer 已提交
110 111 112 113 114

    return result;
  } /// generate_next


D
Daniel Larimer 已提交
115 116
  void block_header_state::set_new_producers( producer_schedule_type pending ) {
      FC_ASSERT( pending.version == active_schedule.version + 1, "wrong producer schedule version specified" );
117
      FC_ASSERT( pending_schedule.producers.size() == 0,
D
Daniel Larimer 已提交
118 119 120 121 122 123 124
                 "cannot set new pending producers until last pending is confirmed" );
      header.new_producers     = move(pending);
      pending_schedule_hash    = digest_type::hash( *header.new_producers );
      pending_schedule         = *header.new_producers;
      pending_schedule_lib_num = block_num;
  }

D
Daniel Larimer 已提交
125 126 127 128 129

  /**
   *  Transitions the current header state into the next header state given the supplied signed block header.
   *
   *  Given a signed block header, generate the expected template based upon the header time,
130
   *  then validate that the provided header matches the template.
D
Daniel Larimer 已提交
131
   *
132
   *  If the header specifies new_producers then apply them accordingly.
D
Daniel Larimer 已提交
133 134
   */
  block_header_state block_header_state::next( const signed_block_header& h )const {
D
Daniel Larimer 已提交
135
    FC_ASSERT( h.timestamp != block_timestamp_type(), "", ("h",h) );
D
Daniel Larimer 已提交
136 137 138 139 140 141

    auto result = generate_next( h.timestamp );
    FC_ASSERT( result.header.producer = h.producer, "wrong producer specified" );
    FC_ASSERT( h.previous == id, "block must link to current state" );
    FC_ASSERT( h.timestamp > header.timestamp, "block must be later in time" );

D
Daniel Larimer 已提交
142
    FC_ASSERT( result.header.producer   == h.producer, "producer is not scheduled for this time slot" );
D
Daniel Larimer 已提交
143

144 145

    // FC_ASSERT( result.header.block_mroot == h.block_mroot, "mistmatch block merkle root" );
D
Daniel Larimer 已提交
146

147
     /// below this point is state changes that cannot be validated with headers alone, but never-the-less,
D
Daniel Larimer 已提交
148
     /// must result in header state changes
D
Daniel Larimer 已提交
149
    if( h.new_producers ) {
D
Daniel Larimer 已提交
150
       result.set_new_producers( *h.new_producers );
151
    }
D
Daniel Larimer 已提交
152

D
Daniel Larimer 已提交
153 154 155 156
    result.header.action_mroot       = h.action_mroot;
    result.header.transaction_mroot  = h.transaction_mroot;
    result.header.producer_signature = h.producer_signature;
    result.id                        = h.id();
D
Daniel Larimer 已提交
157

158 159 160
    FC_ASSERT( result.block_signing_key == result.signee(), "block not signed by expected key",
               ("result.block_signing_key", result.block_signing_key)("signee", result.signee() ) );

D
Daniel Larimer 已提交
161 162 163
    return result;
  } /// next

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
  digest_type   block_header_state::sig_digest()const {
     auto header_bmroot = digest_type::hash( std::make_pair( header.digest(), blockroot_merkle.get_root() ) );
     return digest_type::hash( std::make_pair(header_bmroot, pending_schedule_hash) );
  }

  void block_header_state::sign( const std::function<signature_type(const digest_type&)>& signer ) {
     auto d = sig_digest();
     header.producer_signature = signer( d );
     FC_ASSERT( block_signing_key == fc::crypto::public_key( header.producer_signature, d ) );
  }

  public_key_type block_header_state::signee()const {
    return fc::crypto::public_key( header.producer_signature, sig_digest(), true );
  }

179 180 181 182 183 184 185 186 187 188 189
  void block_header_state::add_confirmation( const header_confirmation& conf ) {
     for( const auto& c : confirmations )
        FC_ASSERT( c.producer == conf.producer, "block already confirmed by this producer" );

     auto key = active_schedule.get_producer_key( conf.producer );
     FC_ASSERT( key != public_key_type(), "producer not in current schedule" );
     auto signer = fc::crypto::public_key( conf.producer_signature, sig_digest(), true );
     FC_ASSERT( signer == key, "confirmation not signed by expected key" );

     confirmations.emplace_back( conf );
  }
190 191


D
Daniel Larimer 已提交
192
} } /// namespace eosio::chain