block_header_state.cpp 9.3 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 {


8 9 10
   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 已提交
11

A
arhag 已提交
12 13
   producer_key block_header_state::get_scheduled_producer( block_timestamp_type t )const {
      auto index = t.slot % (active_schedule.producers.size() * config::producer_repetitions);
14 15 16 17
      index /= config::producer_repetitions;
      return active_schedule.producers[index];
   }

D
Daniel Larimer 已提交
18 19 20 21 22 23 24

  /**
   *  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 {
25
    block_header_state result;
D
Daniel Larimer 已提交
26

27 28 29 30 31 32 33 34
    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 已提交
35

A
arhag 已提交
36
    auto prokey                                  = get_scheduled_producer(when);
D
Daniel Larimer 已提交
37 38
    result.block_signing_key                     = prokey.block_signing_key;
    result.header.producer                       = prokey.producer_name;
39

40
    result.pending_schedule_lib_num              = pending_schedule_lib_num;
D
Daniel Larimer 已提交
41 42 43
    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 已提交
44
    result.producer_to_last_produced[prokey.producer_name] = result.block_num;
D
Daniel Larimer 已提交
45 46
    result.blockroot_merkle = blockroot_merkle;
    result.blockroot_merkle.append( id );
47 48

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

50 51 52 53
    result.active_schedule                   = active_schedule;
    result.pending_schedule                  = pending_schedule;
    result.dpos_irreversible_blocknum        = dpos_irreversible_blocknum;
    result.bft_irreversible_blocknum         = bft_irreversible_blocknum;
54

55
    if( result.pending_schedule.producers.size() &&
56
        result.dpos_irreversible_blocknum >= pending_schedule_lib_num ) {
57
      result.active_schedule = move( result.pending_schedule );
D
Daniel Larimer 已提交
58 59 60 61 62 63 64

      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 {
65
          new_producer_to_last_produced[pro.producer_name] = result.dpos_irreversible_blocknum;
D
Daniel Larimer 已提交
66 67 68 69
        }
      }
      result.producer_to_last_produced = move( new_producer_to_last_produced );
      result.producer_to_last_produced[prokey.producer_name] = result.block_num;
70
    }
D
Daniel Larimer 已提交
71

72
    /// grow the confirmed count
B
Bart Wyatt 已提交
73
    static_assert(std::numeric_limits<uint8_t>::max() >= (config::max_producers * 2 / 3) + 1, "8bit confirmations may not be able to hold all of the needed confirmations");
74 75 76

    // This uses the previous block active_schedule because thats the "schedule" that signs and therefore confirms _this_ block
    auto num_active_producers = active_schedule.producers.size();
B
Bart Wyatt 已提交
77 78 79
    uint32_t required_confs = (uint32_t)(num_active_producers * 2 / 3) + 1;

    if( confirm_count.size() < config::maximum_tracked_dpos_confirmations ) {
80 81 82
       result.confirm_count.reserve( confirm_count.size() + 1 );
       result.confirm_count  = confirm_count;
       result.confirm_count.resize( confirm_count.size() + 1 );
B
Bart Wyatt 已提交
83
       result.confirm_count.back() = (uint8_t)required_confs;
84 85 86
    } else {
       result.confirm_count.resize( confirm_count.size() );
       memcpy( &result.confirm_count[0], &confirm_count[1], confirm_count.size() - 1 );
B
Bart Wyatt 已提交
87
       result.confirm_count.back() = (uint8_t)required_confs;
88 89
    }

D
Daniel Larimer 已提交
90 91 92 93
    return result;
  } /// generate_next


D
Daniel Larimer 已提交
94 95
  void block_header_state::set_new_producers( producer_schedule_type pending ) {
      FC_ASSERT( pending.version == active_schedule.version + 1, "wrong producer schedule version specified" );
96
      FC_ASSERT( pending_schedule.producers.size() == 0,
D
Daniel Larimer 已提交
97 98 99 100 101 102 103
                 "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 已提交
104 105 106 107 108

  /**
   *  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,
109
   *  then validate that the provided header matches the template.
D
Daniel Larimer 已提交
110
   *
111
   *  If the header specifies new_producers then apply them accordingly.
D
Daniel Larimer 已提交
112
   */
113
  block_header_state block_header_state::next( const signed_block_header& h, bool trust )const {
D
Daniel Larimer 已提交
114
    FC_ASSERT( h.timestamp != block_timestamp_type(), "", ("h",h) );
115
    FC_ASSERT( h.header_extensions.size() == 0, "no supported extensions" );
D
Daniel Larimer 已提交
116 117

    FC_ASSERT( h.timestamp > header.timestamp, "block must be later in time" );
A
arhag 已提交
118 119 120 121
    FC_ASSERT( h.previous == id, "block must link to current state" );
    auto result = generate_next( h.timestamp );
    FC_ASSERT( result.header.producer == h.producer, "wrong producer specified" );
    FC_ASSERT( result.header.schedule_version == h.schedule_version, "schedule_version in signed block is corrupted" );
122

123 124 125 126 127
    auto itr = producer_to_last_produced.find(h.producer);
    if( itr != producer_to_last_produced.end() ) {
       FC_ASSERT( itr->second <= result.block_num - h.confirmed, "producer double-confirming known range" );
    }

A
arhag 已提交
128
    // FC_ASSERT( result.header.block_mroot == h.block_mroot, "mismatch block merkle root" );
D
Daniel Larimer 已提交
129

130
     /// below this point is state changes that cannot be validated with headers alone, but never-the-less,
D
Daniel Larimer 已提交
131
     /// must result in header state changes
D
Daniel Larimer 已提交
132
    if( h.new_producers ) {
D
Daniel Larimer 已提交
133
       result.set_new_producers( *h.new_producers );
134
    }
D
Daniel Larimer 已提交
135

136 137 138
    result.set_confirmed( h.confirmed );


D
Daniel Larimer 已提交
139 140 141
    result.header.action_mroot       = h.action_mroot;
    result.header.transaction_mroot  = h.transaction_mroot;
    result.header.producer_signature = h.producer_signature;
A
arhag 已提交
142
    result.id                        = result.header.id();
D
Daniel Larimer 已提交
143

144 145 146 147
    if( !trust ) {
       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() ) );
    }
148

D
Daniel Larimer 已提交
149 150 151
    return result;
  } /// next

152 153 154 155 156 157 158 159 160 161
  void block_header_state::set_confirmed( uint16_t num_prev_blocks ) {
     /*
     idump((num_prev_blocks)(confirm_count.size()));

     for( uint32_t i = 0; i < confirm_count.size(); ++i ) {
        std::cerr << "confirm_count["<<i<<"] = " << int(confirm_count[i]) << "\n";
     }
     */
     header.confirmed = num_prev_blocks;

162
     int32_t i = (int32_t)(confirm_count.size() - 1);
163 164
     uint32_t blocks_to_confirm = num_prev_blocks + 1; /// confirm the head block too
     while( i >= 0 && blocks_to_confirm ) {
165
        --confirm_count[i];
166
        //idump((confirm_count[i]));
167
        if( confirm_count[i] == 0 )
168
        {
169 170 171 172 173 174 175 176 177 178
           uint32_t block_num_for_i = block_num - (uint32_t)(confirm_count.size() - 1 - i);
           dpos_irreversible_blocknum = block_num_for_i;
           //idump((dpos2_lib)(block_num)(dpos_irreversible_blocknum));

           if (i == confirm_count.size() - 1) {
              confirm_count.resize(0);
           } else {
              memmove( &confirm_count[0], &confirm_count[i + 1], confirm_count.size() - i  - 1);
              confirm_count.resize( confirm_count.size() - i - 1 );
           }
179 180 181 182

           return;
        }
        --i;
183
        --blocks_to_confirm;
184 185 186
     }
  }

187 188 189 190 191
  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) );
  }

192
  void block_header_state::sign( const std::function<signature_type(const digest_type&)>& signer, bool trust ) {
193 194
     auto d = sig_digest();
     header.producer_signature = signer( d );
195 196 197
     if( !trust ) {
        FC_ASSERT( block_signing_key == fc::crypto::public_key( header.producer_signature, d ) );
     }
198 199 200 201 202 203
  }

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

204 205
  void block_header_state::add_confirmation( const header_confirmation& conf ) {
     for( const auto& c : confirmations )
206
        FC_ASSERT( c.producer != conf.producer, "block already confirmed by this producer" );
207 208 209 210 211 212 213 214

     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 );
  }
215 216


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