chain_controller.cpp 43.1 KB
Newer Older
N
Nathan Hourt 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright (c) 2017, Respective Authors.
 *
 * The MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
N
Nathan Hourt 已提交
24

25
#include <eos/chain/chain_controller.hpp>
N
Nathan Hourt 已提交
26 27 28 29
#include <eos/chain/exceptions.hpp>

#include <eos/chain/block_summary_object.hpp>
#include <eos/chain/global_property_object.hpp>
N
Nathan Hourt 已提交
30 31
#include <eos/chain/key_value_object.hpp>
#include <eos/chain/action_objects.hpp>
N
Nathan Hourt 已提交
32 33 34
#include <eos/chain/transaction_object.hpp>
#include <eos/chain/producer_object.hpp>

35 36 37
#include <eos/types/native.hpp>
#include <eos/types/generated.hpp>

38 39 40
#include <eos/utilities/randutils.hpp>
#include <eos/utilities/pcg-random/pcg_random.hpp>

N
Nathan Hourt 已提交
41
#include <fc/smart_ref_impl.hpp>
N
Nathan Hourt 已提交
42 43 44
#include <fc/uint128.hpp>
#include <fc/crypto/digest.hpp>

45
#include <boost/range/algorithm/copy.hpp>
46
#include <boost/range/adaptor/transformed.hpp>
N
Nathan Hourt 已提交
47 48 49 50 51

#include <fstream>
#include <functional>
#include <iostream>

52 53
#include <Wren++.h>

N
Nathan Hourt 已提交
54 55
namespace eos { namespace chain {

56 57

String apply_context::get( String key )const {
58
   const auto& obj = db.get<key_value_object,by_scope_key>( boost::make_tuple(scope, key) );
59 60 61
   return String(obj.value.begin(),obj.value.end());
}
void apply_context::set( String key, String value ) {
62
   const auto* obj = db.find<key_value_object,by_scope_key>( boost::make_tuple(scope, key) );
63
   if( obj ) {
64
      mutable_db.modify( *obj, [&]( auto& o ) {
65 66 67 68
         o.value.resize( value.size() );
        // memcpy( o.value.data(), value.data(), value.size() );
      });
   } else {
69
      mutable_db.create<key_value_object>( [&](auto& o) {
70
         o.scope = scope;
71 72 73 74 75 76
         o.key.insert( 0, key.data(), key.size() );
         o.value.insert( 0, value.data(), value.size() );
      });
   }
}
void apply_context::remove( String key ) {
77
   const auto* obj = db.find<key_value_object,by_scope_key>( boost::make_tuple(scope, key) );
78
   if( obj ) {
79
      mutable_db.remove( *obj );
80 81 82 83 84
   }
}



85
bool chain_controller::is_known_block(const block_id_type& id)const
N
Nathan Hourt 已提交
86
{
87
   return _fork_db.is_known_block(id) || _block_log.read_block_by_id(id);
N
Nathan Hourt 已提交
88 89 90 91 92 93
}
/**
 * Only return true *if* the transaction has not expired or been invalidated. If this
 * method is called with a VERY old transaction we will return false, they should
 * query things by blocks if they are that old.
 */
94
bool chain_controller::is_known_transaction(const transaction_id_type& id)const
N
Nathan Hourt 已提交
95
{
96
   const auto& trx_idx = _db.get_index<transaction_multi_index, by_trx_id>();
N
Nathan Hourt 已提交
97 98 99
   return trx_idx.find( id ) != trx_idx.end();
}

100
block_id_type chain_controller::get_block_id_for_num(uint32_t block_num)const
N
Nathan Hourt 已提交
101
{ try {
102 103
   if (const auto& block = fetch_block_by_number(block_num))
      return block->id();
N
Nathan Hourt 已提交
104

105 106 107
   FC_THROW_EXCEPTION(unknown_block_exception, "Could not find block");
} FC_CAPTURE_AND_RETHROW((block_num)) }

108
optional<signed_block> chain_controller::fetch_block_by_id(const block_id_type& id)const
N
Nathan Hourt 已提交
109
{
110 111
   auto b = _fork_db.fetch_block(id);
   if(b) return b->data;
112
   return _block_log.read_block_by_id(id);
N
Nathan Hourt 已提交
113 114
}

115
optional<signed_block> chain_controller::fetch_block_by_number(uint32_t num)const
N
Nathan Hourt 已提交
116
{
117
   if (const auto& block = _block_log.read_block_by_num(num))
118 119
      return *block;

N
Nathan Hourt 已提交
120
   // Not in _block_log, so it must be since the last irreversible block. Grab it from _fork_db instead
121 122 123 124 125 126 127 128
   if (num <= head_block_num()) {
      auto block = _fork_db.head();
      while (block && block->num > num)
         block = block->prev.lock();
      if (block && block->num == num)
         return block->data;
   }

N
Nathan Hourt 已提交
129 130 131
   return optional<signed_block>();
}

132
const SignedTransaction& chain_controller::get_recent_transaction(const transaction_id_type& trx_id) const
N
Nathan Hourt 已提交
133
{
134
   auto& index = _db.get_index<transaction_multi_index, by_trx_id>();
N
Nathan Hourt 已提交
135 136 137 138 139
   auto itr = index.find(trx_id);
   FC_ASSERT(itr != index.end());
   return itr->trx;
}

140
std::vector<block_id_type> chain_controller::get_block_ids_on_fork(block_id_type head_of_fork) const
N
Nathan Hourt 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
{
  pair<fork_database::branch_type, fork_database::branch_type> branches = _fork_db.fetch_branch_from(head_block_id(), head_of_fork);
  if( !((branches.first.back()->previous_id() == branches.second.back()->previous_id())) )
  {
     edump( (head_of_fork)
            (head_block_id())
            (branches.first.size())
            (branches.second.size()) );
     assert(branches.first.back()->previous_id() == branches.second.back()->previous_id());
  }
  std::vector<block_id_type> result;
  for (const item_ptr& fork_block : branches.second)
    result.emplace_back(fork_block->id);
  result.emplace_back(branches.first.back()->previous_id());
  return result;
}

/**
 * Push block "may fail" in which case every partial change is unwound.  After
 * push block is successful the block is appended to the chain database on disk.
 *
 * @return true if we switched forks as a result of this push.
 */
164
bool chain_controller::push_block(const signed_block& new_block, uint32_t skip)
D
Daniel Larimer 已提交
165 166 167
{ try {
   return with_skip_flags( skip, [&](){ 
      return without_pending_transactions( [&]() {
168
         return _db.with_write_lock( [&]() {
169 170
            return _push_block(new_block);
         } );
N
Nathan Hourt 已提交
171 172
      });
   });
173
} FC_CAPTURE_AND_RETHROW((new_block)) }
N
Nathan Hourt 已提交
174

175
bool chain_controller::_push_block(const signed_block& new_block)
N
Nathan Hourt 已提交
176 177
{ try {
   uint32_t skip = get_node_properties().skip_flags;
178
   if (!(skip&skip_fork_db)) {
N
Nathan Hourt 已提交
179 180 181 182 183
      /// TODO: if the block is greater than the head block and before the next maitenance interval
      // verify that the block signer is in the current set of active producers.

      shared_ptr<fork_item> new_head = _fork_db.push_block(new_block);
      //If the head block from the longest chain does not build off of the current head, we need to switch forks.
184
      if (new_head->data.previous != head_block_id()) {
N
Nathan Hourt 已提交
185 186
         //If the newly pushed block is the same height as head, we get head back in new_head
         //Only switch forks if new_head is actually higher than head
187 188
         if (new_head->data.block_num() > head_block_num()) {
            wlog("Switching to fork: ${id}", ("id",new_head->data.id()));
N
Nathan Hourt 已提交
189 190 191
            auto branches = _fork_db.fetch_branch_from(new_head->data.id(), head_block_id());

            // pop blocks until we hit the forked block
192
            while (head_block_id() != branches.second.back()->data.previous)
N
Nathan Hourt 已提交
193 194 195
               pop_block();

            // push all blocks on the new fork
196 197
            for (auto ritr = branches.first.rbegin(); ritr != branches.first.rend(); ++ritr) {
                ilog("pushing blocks from fork ${n} ${id}", ("n",(*ritr)->data.block_num())("id",(*ritr)->data.id()));
N
Nathan Hourt 已提交
198 199
                optional<fc::exception> except;
                try {
200
                   auto session = _db.start_undo_session(true);
201
                   apply_block((*ritr)->data, skip);
N
Nathan Hourt 已提交
202 203
                   session.push();
                }
204 205 206
                catch (const fc::exception& e) { except = e; }
                if (except) {
                   wlog("exception thrown while switching forks ${e}", ("e",except->to_detail_string()));
N
Nathan Hourt 已提交
207
                   // remove the rest of branches.first from the fork_db, those blocks are invalid
208 209
                   while (ritr != branches.first.rend()) {
                      _fork_db.remove((*ritr)->data.id());
N
Nathan Hourt 已提交
210 211
                      ++ritr;
                   }
212
                   _fork_db.set_head(branches.second.front());
N
Nathan Hourt 已提交
213 214

                   // pop all blocks from the bad fork
215
                   while (head_block_id() != branches.second.back()->data.previous)
N
Nathan Hourt 已提交
216 217 218
                      pop_block();

                   // restore all blocks from the good fork
219
                   for (auto ritr = branches.second.rbegin(); ritr != branches.second.rend(); ++ritr) {
220
                      auto session = _db.start_undo_session(true);
221
                      apply_block((*ritr)->data, skip);
N
Nathan Hourt 已提交
222 223 224 225 226 227 228 229 230 231 232 233
                      session.push();
                   }
                   throw *except;
                }
            }
            return true;
         }
         else return false;
      }
   }

   try {
234
      auto session = _db.start_undo_session(true);
N
Nathan Hourt 已提交
235 236 237 238 239 240 241 242 243
      apply_block(new_block, skip);
      session.push();
   } catch ( const fc::exception& e ) {
      elog("Failed to push new block:\n${e}", ("e", e.to_detail_string()));
      _fork_db.remove(new_block.id());
      throw;
   }

   return false;
244
} FC_CAPTURE_AND_RETHROW((new_block)) }
N
Nathan Hourt 已提交
245 246 247 248 249 250 251 252 253 254

/**
 * Attempts to push the transaction into the pending queue
 *
 * When called to push a locally generated transaction, set the skip_block_size_check bit on the skip argument. This
 * will allow the transaction to be pushed even if it causes the pending block size to exceed the maximum block size.
 * Although the transaction will probably not propagate further now, as the peers are likely to have their pending
 * queues full as well, it will be kept in the queue to be propagated later when a new block flushes out the pending
 * queues.
 */
255
void chain_controller::push_transaction(const SignedTransaction& trx, uint32_t skip)
N
Nathan Hourt 已提交
256
{ try {
257 258
   with_skip_flags(skip, [&]() {
      with_producing([&](){
259
         _db.with_write_lock([&]() {
D
Daniel Larimer 已提交
260 261 262
            _push_transaction(trx);
         });
      });
N
Nathan Hourt 已提交
263 264
   });
} FC_CAPTURE_AND_RETHROW((trx)) }
N
Nathan Hourt 已提交
265

266
void chain_controller::_push_transaction(const SignedTransaction& trx) {
267 268
   // If this is the first transaction pushed after applying a block, start a new undo session.
   // This allows us to quickly rewind to the clean state of the head block, in case a new block arrives.
269
   if (!_pending_tx_session.valid())
270
      _pending_tx_session = _db.start_undo_session(true);
271

272
   auto temp_session = _db.start_undo_session(true);
N
Nathan Hourt 已提交
273
   _apply_transaction(trx);
D
Daniel Larimer 已提交
274
   _pending_transactions.push_back(trx);
N
Nathan Hourt 已提交
275 276 277 278 279 280

   // notify_changed_objects();
   // The transaction applied successfully. Merge its changes into the pending block session.
   temp_session.squash();

   // notify anyone listening to pending transactions
N
Nathan Hourt 已提交
281
   on_pending_transaction(trx);
N
Nathan Hourt 已提交
282 283 284
}


285
signed_block chain_controller::generate_block(
N
Nathan Hourt 已提交
286
   fc::time_point_sec when,
N
Nathan Hourt 已提交
287
   const AccountName& producer,
N
Nathan Hourt 已提交
288 289 290 291
   const fc::ecc::private_key& block_signing_private_key,
   uint32_t skip /* = 0 */
   )
{ try {
D
Daniel Larimer 已提交
292 293
   return with_producing( [&]() {
      return with_skip_flags( skip, [&](){
294
         auto b = _db.with_write_lock( [&](){
N
Nathan Hourt 已提交
295
           return _generate_block( when, producer, block_signing_private_key );
D
Daniel Larimer 已提交
296
         });
297 298
         push_block(b);
         return b;
D
Daniel Larimer 已提交
299 300 301
      });
    });
} FC_CAPTURE_AND_RETHROW( (when) ) }
N
Nathan Hourt 已提交
302

303
signed_block chain_controller::_generate_block(
N
Nathan Hourt 已提交
304
   fc::time_point_sec when,
N
Nathan Hourt 已提交
305
   const AccountName& producer,
N
Nathan Hourt 已提交
306 307 308 309 310 311 312
   const fc::ecc::private_key& block_signing_private_key
   )
{
   try {
   uint32_t skip = get_node_properties().skip_flags;
   uint32_t slot_num = get_slot_at_time( when );
   FC_ASSERT( slot_num > 0 );
N
Nathan Hourt 已提交
313 314
   AccountName scheduled_producer = get_scheduled_producer( slot_num );
   FC_ASSERT( scheduled_producer == producer );
N
Nathan Hourt 已提交
315

N
Nathan Hourt 已提交
316
   const auto& producer_obj = get_producer(scheduled_producer);
N
Nathan Hourt 已提交
317 318 319 320 321

   if( !(skip & skip_producer_signature) )
      FC_ASSERT( producer_obj.signing_key == block_signing_private_key.get_public_key() );

   static const size_t max_block_header_size = fc::raw::pack_size( signed_block_header() ) + 4;
322
   auto maximum_block_size = get_global_properties().configuration.maxBlockSize;
N
Nathan Hourt 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
   size_t total_block_size = max_block_header_size;

   signed_block pending_block;

   //
   // The following code throws away existing pending_tx_session and
   // rebuilds it by re-applying pending transactions.
   //
   // This rebuild is necessary because pending transactions' validity
   // and semantics may have changed since they were received, because
   // time-based semantics are evaluated based on the current block
   // time.  These changes can only be reflected in the database when
   // the value of the "when" variable is known, which means we need to
   // re-apply pending transactions in this method.
   //
   _pending_tx_session.reset();
339
   _pending_tx_session = _db.start_undo_session(true);
N
Nathan Hourt 已提交
340 341 342

   uint64_t postponed_tx_count = 0;
   // pop pending state (reset to head block state)
N
Nathan Hourt 已提交
343
   for( const SignedTransaction& tx : _pending_transactions )
N
Nathan Hourt 已提交
344 345 346 347 348 349 350 351 352 353 354 355
   {
      size_t new_total_size = total_block_size + fc::raw::pack_size( tx );

      // postpone transaction if it would make block too big
      if( new_total_size >= maximum_block_size )
      {
         postponed_tx_count++;
         continue;
      }

      try
      {
356
         auto temp_session = _db.start_undo_session(true);
N
Nathan Hourt 已提交
357
         _apply_transaction(tx);
N
Nathan Hourt 已提交
358 359
         temp_session.squash();

N
Nathan Hourt 已提交
360
         total_block_size += fc::raw::pack_size(tx);
N
Nathan Hourt 已提交
361 362 363 364 365 366
         if (pending_block.cycles.empty()) {
            pending_block.cycles.resize(1);
            pending_block.cycles.back().resize(1);
         }
         pending_block.cycles.back().back().user_input.emplace_back(tx);
#warning TODO: Populate generated blocks with generated transactions
N
Nathan Hourt 已提交
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
      }
      catch ( const fc::exception& e )
      {
         // Do nothing, transaction will not be re-applied
         wlog( "Transaction was not processed while generating block due to ${e}", ("e", e) );
         wlog( "The transaction was ${t}", ("t", tx) );
      }
   }
   if( postponed_tx_count > 0 )
   {
      wlog( "Postponed ${n} transactions due to block size limit", ("n", postponed_tx_count) );
   }

   _pending_tx_session.reset();

   // We have temporarily broken the invariant that
   // _pending_tx_session is the result of applying _pending_tx, as
D
Daniel Larimer 已提交
384
   // _pending_transactions now consists of the set of postponed transactions.
N
Nathan Hourt 已提交
385 386 387 388 389 390
   // However, the push_block() call below will re-create the
   // _pending_tx_session.

   pending_block.previous = head_block_id();
   pending_block.timestamp = when;
   pending_block.transaction_merkle_root = pending_block.calculate_merkle_root();
D
Daniel Larimer 已提交
391

392
   pending_block.producer = producer_obj.owner;
N
Nathan Hourt 已提交
393 394 395 396 397

   if( !(skip & skip_producer_signature) )
      pending_block.sign( block_signing_private_key );

   // TODO:  Move this to _push_block() so session is restored.
398
   /*
N
Nathan Hourt 已提交
399 400 401 402
   if( !(skip & skip_block_size_check) )
   {
      FC_ASSERT( fc::raw::pack_size(pending_block) <= get_global_properties().parameters.maximum_block_size );
   }
403
   */
N
Nathan Hourt 已提交
404

405
   // push_block( pending_block, skip );
N
Nathan Hourt 已提交
406 407

   return pending_block;
N
Nathan Hourt 已提交
408
} FC_CAPTURE_AND_RETHROW( (producer) ) }
N
Nathan Hourt 已提交
409 410

/**
N
Nathan Hourt 已提交
411
 * Removes the most recent block from the database and undoes any changes it made.
N
Nathan Hourt 已提交
412
 */
413
void chain_controller::pop_block()
N
Nathan Hourt 已提交
414 415 416 417 418 419 420
{ try {
   _pending_tx_session.reset();
   auto head_id = head_block_id();
   optional<signed_block> head_block = fetch_block_by_id( head_id );
   EOS_ASSERT( head_block.valid(), pop_empty_chain, "there are no blocks to pop" );

   _fork_db.pop_block();
421
   _db.undo();
N
Nathan Hourt 已提交
422 423
} FC_CAPTURE_AND_RETHROW() }

424
void chain_controller::clear_pending()
N
Nathan Hourt 已提交
425
{ try {
D
Daniel Larimer 已提交
426
   _pending_transactions.clear();
N
Nathan Hourt 已提交
427 428 429 430 431
   _pending_tx_session.reset();
} FC_CAPTURE_AND_RETHROW() }

//////////////////// private methods ////////////////////

432
void chain_controller::apply_block(const signed_block& next_block, uint32_t skip)
N
Nathan Hourt 已提交
433 434
{
   auto block_num = next_block.block_num();
435 436 437 438 439
   if (_checkpoints.size() && _checkpoints.rbegin()->second != block_id_type()) {
      auto itr = _checkpoints.find(block_num);
      if (itr != _checkpoints.end())
         FC_ASSERT(next_block.id() == itr->second,
                   "Block did not match checkpoint", ("checkpoint",*itr)("block_id",next_block.id()));
N
Nathan Hourt 已提交
440

441
      if (_checkpoints.rbegin()->first >= block_num)
N
Nathan Hourt 已提交
442 443
         skip = ~0;// WE CAN SKIP ALMOST EVERYTHING
   }
444
   with_skip_flags(skip, [&](){ _apply_block(next_block); });
N
Nathan Hourt 已提交
445 446
}

D
Daniel Larimer 已提交
447

448
void chain_controller::_apply_block(const signed_block& next_block)
N
Nathan Hourt 已提交
449 450 451 452
{ try {
   uint32_t next_block_num = next_block.block_num();
   uint32_t skip = get_node_properties().skip_flags;

453 454 455
   FC_ASSERT((skip & skip_merkle_check) || next_block.transaction_merkle_root == next_block.calculate_merkle_root(),
             "", ("next_block.transaction_merkle_root", next_block.transaction_merkle_root)
             ("calc",next_block.calculate_merkle_root())("next_block",next_block)("id",next_block.id()));
N
Nathan Hourt 已提交
456 457 458

   const producer_object& signing_producer = validate_block_header(skip, next_block);

N
Nathan Hourt 已提交
459 460 461 462 463 464
   /* We do not need to push the undo state for each transaction
    * because they either all apply and are valid or the
    * entire block fails to apply.  We only need an "undo" state
    * for transactions when validating broadcast transactions or
    * when building a block.
    */
465 466 467
   for (const auto& cycle : next_block.cycles) {
      for (const auto& thread : cycle) {
         for(const auto& trx : thread.generated_input ) {
N
Nathan Hourt 已提交
468 469
#warning TODO: Process generated transaction
         }
470 471 472 473 474
         for(const auto& trx : thread.user_input ) {
            apply_transaction(trx);
         }
      }
   }
N
Nathan Hourt 已提交
475

476
   update_global_properties(next_block);
N
Nathan Hourt 已提交
477 478 479 480 481 482 483 484 485 486 487
   update_global_dynamic_data(next_block);
   update_signing_producer(signing_producer, next_block);
   update_last_irreversible_block();

   create_block_summary(next_block);
   clear_expired_transactions();

   if( !_node_property_object.debug_updates.empty() )
      apply_debug_updates();

   // notify observers that the block has been applied
488
   // TODO: do this outside the write lock...? 
N
Nathan Hourt 已提交
489 490 491 492
   applied_block( next_block ); //emit

} FC_CAPTURE_AND_RETHROW( (next_block.block_num()) )  }

493
void chain_controller::apply_transaction(const SignedTransaction& trx, uint32_t skip)
N
Nathan Hourt 已提交
494
{
D
Daniel Larimer 已提交
495
   with_skip_flags( skip, [&]() { _apply_transaction(trx); });
N
Nathan Hourt 已提交
496 497
}

498
void chain_controller::validate_transaction(const SignedTransaction& trx)const {
499
try {
500
   EOS_ASSERT(trx.messages.size() > 0, transaction_exception, "A transaction must have at least one message");
N
Nathan Hourt 已提交
501

502 503 504 505
   validate_uniqueness(trx);
   validate_tapos(trx);
   validate_referenced_accounts(trx);
   validate_expiration(trx);
506
   validate_message_types(trx);
507

N
Nathan Hourt 已提交
508 509
   for (const auto& tm : trx.messages) { /// TODO: this loop can be processed in parallel
      Message m(tm);
N
Nathan Hourt 已提交
510
      message_validate_context mvc(m);
N
Nathan Hourt 已提交
511 512 513 514
      auto contract_handlers_itr = message_validate_handlers.find(m.recipient);
      if (contract_handlers_itr != message_validate_handlers.end()) {
         auto message_handler_itr = contract_handlers_itr->second.find({m.recipient, m.type});
         if (message_handler_itr != contract_handlers_itr->second.end()) {
515 516 517 518
            message_handler_itr->second(mvc);
            continue;
         }
      }
519

520 521
      /// TODO: dispatch to script if not handled above
   }
522 523
} FC_CAPTURE_AND_RETHROW( (trx) ) }

524
void chain_controller::validate_uniqueness( const SignedTransaction& trx )const {
N
Nathan Hourt 已提交
525
   if( !should_check_for_duplicate_transactions() ) return;
N
Nathan Hourt 已提交
526 527

   auto trx_id = trx.id();
528
   auto& trx_idx = _db.get_index<transaction_multi_index>();
529 530
   EOS_ASSERT(trx_idx.indices().get<by_trx_id>().find(trx_id) == trx_idx.indices().get<by_trx_id>().end(),
              transaction_exception, "Transaction is not unique");
531
}
532

533
void chain_controller::validate_tapos(const SignedTransaction& trx)const {
N
Nathan Hourt 已提交
534
   if (!should_check_tapos()) return;
535

536
   const auto& tapos_block_summary = _db.get<block_summary_object>((uint16_t)trx.refBlockNum);
537 538

   //Verify TaPoS block summary has correct ID prefix, and that this block's time is not past the expiration
N
Nathan Hourt 已提交
539 540 541 542
   EOS_ASSERT(trx.verify_reference_block(tapos_block_summary.block_id), transaction_exception,
              "Transaction's reference block did not match. Is this transaction from a different fork?",
              ("tapos_summary", tapos_block_summary));
}
543

544
void chain_controller::validate_referenced_accounts(const SignedTransaction& trx)const {
N
Nathan Hourt 已提交
545
   for(const auto& auth : trx.authorizations) {
N
Nathan Hourt 已提交
546
      require_account(auth.account);
547
   }
548
   for(const auto& msg : trx.messages) {
N
Nathan Hourt 已提交
549 550
      require_account(msg.sender);
      require_account(msg.recipient);
N
Nathan Hourt 已提交
551
      const AccountName* previous_notify_account = nullptr;
552
      for(const auto& current_notify_account : msg.notify) {
N
Nathan Hourt 已提交
553
         require_account(current_notify_account);
554
         if(previous_notify_account) {
555 556
            EOS_ASSERT(current_notify_account < *previous_notify_account, message_validate_exception,
                       "Message notify accounts out of order. Possibly a bug in the wallet?");
557 558
         }

559 560 561 562 563
         EOS_ASSERT(current_notify_account != msg.sender, message_validate_exception,
                    "Message sender is listed in accounts to notify. Possibly a bug in the wallet?");
         EOS_ASSERT(current_notify_account != msg.recipient, message_validate_exception,
                    "Message recipient is listed in accounts to notify. Possibly a bug in the wallet?");
         previous_notify_account = &current_notify_account;
564 565 566
      }
   }
}
D
Daniel Larimer 已提交
567

568
void chain_controller::validate_expiration(const SignedTransaction& trx) const
569 570
{ try {
   fc::time_point_sec now = head_block_time();
571
   const BlockchainConfiguration& chain_configuration = get_global_properties().configuration;
572

573
   EOS_ASSERT(trx.expiration <= now + int32_t(chain_configuration.maxTrxLifetime),
574 575
              transaction_exception, "Transaction expiration is too far in the future",
              ("trx.expiration",trx.expiration)("now",now)
576
              ("max_til_exp",chain_configuration.maxTrxLifetime));
577 578 579
   EOS_ASSERT(now <= trx.expiration, transaction_exception, "Transaction is expired",
              ("now",now)("trx.exp",trx.expiration));
} FC_CAPTURE_AND_RETHROW((trx)) }
580

581
void chain_controller::validate_message_types(const SignedTransaction& trx)const {
582 583 584
try {
   for( const auto& msg : trx.messages ) {
      try {
585
         _db.get<type_object, by_scope_name>( boost::make_tuple(msg.recipient, msg.type) );
N
Nathan Hourt 已提交
586 587 588 589
      } catch(std::out_of_range) {
         FC_THROW_EXCEPTION(message_validate_exception, "Unrecognized message recipient and type",
                            ("recipient", msg.recipient)("type", msg.type));
      }
590 591 592
   }
} FC_CAPTURE_AND_RETHROW( (trx) ) }

593
void chain_controller::validate_message_precondition( precondition_validate_context& context )const 
594
{ try {
595
    const auto& m = context.msg;
N
Nathan Hourt 已提交
596
    auto contract_handlers_itr = precondition_validate_handlers.find( m.recipient );
597
    if( contract_handlers_itr != precondition_validate_handlers.end() ) {
598
       auto message_handler_itr = contract_handlers_itr->second.find( {context.scope, m.type} );
N
Nathan Hourt 已提交
599 600
       if( message_handler_itr != contract_handlers_itr->second.end() ) {
          message_handler_itr->second(context);
601 602 603 604
          return;
       }
    }
    /// TODO: dispatch to script if not handled above
N
Nathan Hourt 已提交
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
   } FC_CAPTURE_AND_RETHROW() }

void chain_controller::process_message(Message message) {
   apply_context apply_ctx(_db, message, message.recipient);

   /** TODO: pre condition validation and application can occur in parallel */
   /** TODO: verify that message is fully authorized
          (check that @ref SignedTransaction::authorizations are all present) */
   validate_message_precondition(apply_ctx);
   apply_message(apply_ctx);

   for (const auto& notify_account : message.notify) {
      try {
         apply_context notify_ctx(_db, message, notify_account);
         validate_message_precondition(notify_ctx);
         apply_message(notify_ctx);
      } FC_CAPTURE_AND_RETHROW((notify_account)(message))
   }
}
624

625
void chain_controller::apply_message( apply_context& context ) 
626
{ try {
627
    const auto& m = context.msg;
N
Nathan Hourt 已提交
628
    auto contract_handlers_itr = apply_handlers.find( m.recipient );
629
    if( contract_handlers_itr != apply_handlers.end() ) {
630
       auto message_handler_itr = contract_handlers_itr->second.find( {context.scope, m.type} );
N
Nathan Hourt 已提交
631 632
       if( message_handler_itr != contract_handlers_itr->second.end() ) {
          message_handler_itr->second(context);
633 634 635
          return;
       }
    }
636
    const auto& scope = _db.get<account_object,by_name>( context.scope );
637
    const auto& recipient = _db.get<account_object,by_name>( context.msg.recipient );
638

639
    auto handler = _db.find<action_code_object,by_processor_recipient_type>( boost::make_tuple(scope.id, recipient.id, context.msg.type) );
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
    if( handler ) {
       wdump((handler->apply.c_str()));
      wrenpp::VM vm;
      vm.executeString( R"(
          foreign class ApplyContext {
             foreign get(key)
             foreign set(key,value)
          }
      )");
      vm.executeString( handler->apply.c_str() );
      auto apply_method = vm.method( "main", "Handler", "apply(_,_)" );
      //apply_method( context, m.data );
      //apply_method( "context", 1 );
      apply_method( &context, 1 );
    }
655
    /// TODO: dispatch to script if not handled above
N
Nathan Hourt 已提交
656
} FC_CAPTURE_AND_RETHROW((context.msg)) }
N
Nathan Hourt 已提交
657 658


659
void chain_controller::_apply_transaction(const SignedTransaction& trx)
660
{ try {
661
   validate_transaction(trx);
662

N
Nathan Hourt 已提交
663 664
   for (const auto& message : trx.messages) {
      process_message(message);
N
Nathan Hourt 已提交
665 666 667
   }

   //Insert transaction into unique transactions database.
N
Nathan Hourt 已提交
668
   if (should_check_for_duplicate_transactions())
N
Nathan Hourt 已提交
669
   {
670
      _db.create<transaction_object>([&](transaction_object& transaction) {
671
         transaction.trx_id = trx.id(); /// TODO: consider caching ID
N
Nathan Hourt 已提交
672 673 674
         transaction.trx = trx;
      });
   }
N
Nathan Hourt 已提交
675 676 677 678 679 680
   } FC_CAPTURE_AND_RETHROW((trx)) }

void chain_controller::require_account(const types::AccountName& name) const {
   auto account = _db.find<account_object, by_name>(name);
   FC_ASSERT(account != nullptr, "Account not found: ${name}", ("name", name));
}
N
Nathan Hourt 已提交
681

682
const producer_object& chain_controller::validate_block_header(uint32_t skip, const signed_block& next_block)const {
683 684 685 686 687 688 689
   EOS_ASSERT(head_block_id() == next_block.previous, block_validate_exception, "",
              ("head_block_id",head_block_id())("next.prev",next_block.previous));
   EOS_ASSERT(head_block_time() < next_block.timestamp, block_validate_exception, "",
              ("head_block_time",head_block_time())("next",next_block.timestamp)("blocknum",next_block.block_num()));
   if (next_block.block_num() % config::ProducerCount != 0)
      EOS_ASSERT(next_block.producer_changes.empty(), block_validate_exception,
                 "Producer changes may only occur at the end of a round.");
N
Nathan Hourt 已提交
690
   const producer_object& producer = get_producer(get_scheduled_producer(get_slot_at_time(next_block.timestamp)));
N
Nathan Hourt 已提交
691

N
Nathan Hourt 已提交
692
   if(!(skip&skip_producer_signature))
693 694 695
      EOS_ASSERT(next_block.validate_signee(producer.signing_key), block_validate_exception,
                 "Incorrect block producer key: expected ${e} but got ${a}",
                 ("e", producer.signing_key)("a", public_key_type(next_block.signee())));
N
Nathan Hourt 已提交
696

697
   if(!(skip&skip_producer_schedule_check)) {
698 699 700
      EOS_ASSERT(next_block.producer == producer.owner, block_validate_exception,
                 "Producer produced block at wrong time",
                 ("block producer",next_block.producer)("scheduled producer",producer.owner));
N
Nathan Hourt 已提交
701 702 703 704 705
   }

   return producer;
}

706
void chain_controller::create_block_summary(const signed_block& next_block) {
707
   auto sid = next_block.block_num() & 0xffff;
708
   _db.modify( _db.get<block_summary_object,by_id>(sid), [&](block_summary_object& p) {
709 710
         p.block_id = next_block.id();
   });
N
Nathan Hourt 已提交
711 712
}

713
void chain_controller::update_global_properties(const signed_block& b) {
714
   // If we're at the end of a round, update the BlockchainConfiguration and producer schedule
715
   if (b.block_num() % config::ProducerCount == 0) {
716
      auto schedule = calculate_next_round(b);
717 718
      auto config = _admin->get_blockchain_configuration(_db, schedule);

719 720
      const auto& gpo = get_global_properties();
      _db.modify(gpo, [schedule = std::move(schedule), config = std::move(config)] (global_property_object& gpo) {
721 722 723 724 725 726
         gpo.active_producers = std::move(schedule);
         gpo.configuration = std::move(config);
      });
   }
}

727
void chain_controller::add_checkpoints( const flat_map<uint32_t,block_id_type>& checkpts ) {
728
   for (const auto& i : checkpts)
N
Nathan Hourt 已提交
729 730 731
      _checkpoints[i.first] = i.second;
}

732
bool chain_controller::before_last_checkpoint()const {
N
Nathan Hourt 已提交
733 734 735 736 737 738 739
   return (_checkpoints.size() > 0) && (_checkpoints.rbegin()->first >= head_block_num());
}

/**
 *  This method dumps the state of the blockchain in a semi-human readable form for the
 *  purpose of tracking down funds and mismatches in currency allocation
 */
740
void chain_controller::debug_dump() {
N
Nathan Hourt 已提交
741 742
}

743
void debug_apply_update(chain_controller& db, const fc::variant_object& vo) {
N
Nathan Hourt 已提交
744 745
}

746
void chain_controller::apply_debug_updates() {
N
Nathan Hourt 已提交
747 748
}

749
void chain_controller::debug_update(const fc::variant_object& update) {
N
Nathan Hourt 已提交
750 751
}

752
const global_property_object& chain_controller::get_global_properties()const {
753
   return _db.get<global_property_object>();
N
Nathan Hourt 已提交
754 755
}

756
const dynamic_global_property_object&chain_controller::get_dynamic_global_properties() const {
757
   return _db.get<dynamic_global_property_object>();
N
Nathan Hourt 已提交
758 759
}

760
time_point_sec chain_controller::head_block_time()const {
N
Nathan Hourt 已提交
761 762 763
   return get_dynamic_global_properties().time;
}

764
uint32_t chain_controller::head_block_num()const {
N
Nathan Hourt 已提交
765 766 767
   return get_dynamic_global_properties().head_block_number;
}

768
block_id_type chain_controller::head_block_id()const {
N
Nathan Hourt 已提交
769 770 771
   return get_dynamic_global_properties().head_block_id;
}

N
Nathan Hourt 已提交
772
types::AccountName chain_controller::head_block_producer() const {
N
Nathan Hourt 已提交
773
   if (auto head_block = fetch_block_by_id(head_block_id()))
774
      return head_block->producer;
N
Nathan Hourt 已提交
775 776 777
   return {};
}

778
const node_property_object& chain_controller::get_node_properties()const {
N
Nathan Hourt 已提交
779 780 781
   return _node_property_object;
}

N
Nathan Hourt 已提交
782 783 784 785
const producer_object& chain_controller::get_producer(const types::AccountName& ownerName) const {
   return _db.get<producer_object, by_owner>(ownerName);
}

786
node_property_object& chain_controller::node_properties() {
N
Nathan Hourt 已提交
787 788 789
   return _node_property_object;
}

790
uint32_t chain_controller::last_irreversible_block_num() const {
N
Nathan Hourt 已提交
791
   return get_dynamic_global_properties().last_irreversible_block_num;
N
Nathan Hourt 已提交
792 793
}

794
void chain_controller::initialize_indexes() {
795 796 797 798 799 800 801 802 803 804 805 806
   _db.add_index<account_index>();
   _db.add_index<permission_index>();
   _db.add_index<action_code_index>();
   _db.add_index<action_permission_index>();
   _db.add_index<type_index>();
   _db.add_index<key_value_index>();

   _db.add_index<global_property_multi_index>();
   _db.add_index<dynamic_global_property_multi_index>();
   _db.add_index<block_summary_multi_index>();
   _db.add_index<transaction_multi_index>();
   _db.add_index<producer_multi_index>();
N
Nathan Hourt 已提交
807 808
}

809
void chain_controller::initialize_chain(chain_initializer_interface& starter)
N
Nathan Hourt 已提交
810
{ try {
811
   if (!_db.find<global_property_object>()) {
N
Nathan Hourt 已提交
812
      _db.with_write_lock([this, &starter] {
813 814 815 816 817 818 819 820 821 822
         struct auth_inhibitor {
            auth_inhibitor(chain_controller& db) : db(db), old_flags(db.node_properties().skip_flags)
            { db.node_properties().skip_flags |= skip_authority_check; }
            ~auth_inhibitor()
            { db.node_properties().skip_flags = old_flags; }
         private:
            chain_controller& db;
            uint32_t old_flags;
         } inhibitor(*this);

N
Nathan Hourt 已提交
823 824 825 826
         auto initial_timestamp = starter.get_chain_start_time();
         FC_ASSERT(initial_timestamp != time_point_sec(), "Must initialize genesis timestamp." );
         FC_ASSERT(initial_timestamp.sec_since_epoch() % config::BlockIntervalSeconds == 0,
                    "Genesis timestamp must be divisible by config::BlockIntervalSeconds." );
N
Nathan Hourt 已提交
827

828
         // Create global properties
N
Nathan Hourt 已提交
829 830 831
         _db.create<global_property_object>([&starter](global_property_object& p) {
            p.configuration = starter.get_chain_start_configuration();
            p.active_producers = starter.get_chain_start_producers();
832 833
         });
         _db.create<dynamic_global_property_object>([&](dynamic_global_property_object& p) {
N
Nathan Hourt 已提交
834
            p.time = initial_timestamp;
835 836
            p.recent_slots_filled = uint64_t(-1);
         });
N
Nathan Hourt 已提交
837

N
Nathan Hourt 已提交
838
         // Initialize block summary index
839 840
         for (int i = 0; i < 0x10000; i++)
            _db.create<block_summary_object>([&](block_summary_object&) {});
N
Nathan Hourt 已提交
841

842
         auto messages = starter.prepare_database(*this, _db);
N
Nathan Hourt 已提交
843
         std::for_each(messages.begin(), messages.end(), [this](const auto& m) { process_message(m); });
844 845
      });
   }
N
Nathan Hourt 已提交
846 847
} FC_CAPTURE_AND_RETHROW() }

848 849 850
chain_controller::chain_controller(database& database, fork_database& fork_db, block_log& blocklog,
                                   chain_initializer_interface& starter, unique_ptr<chain_administration_interface> admin)
   : _db(database), _fork_db(fork_db), _block_log(blocklog), _admin(std::move(admin)) {
851 852 853 854 855 856 857 858 859
   static bool bound_apply = [](){
      wrenpp::beginModule( "main" )
         .bindClassReference<apply_context>( "ApplyContext" )
            .bindFunction< decltype( &apply_context::get ), &apply_context::get >( false, "get(_)")
            .bindFunction< decltype( &apply_context::set ), &apply_context::set >( false, "set(_,_)")
         .endClass()
      .endModule();
      return true;
   }();
N
Nathan Hourt 已提交
860

861
   initialize_indexes();
N
Nathan Hourt 已提交
862
   initialize_chain(starter);
863 864
   spinup_db();
   spinup_fork_db();
865 866 867

   if (_block_log.read_head() && head_block_num() < _block_log.read_head()->block_num())
      replay();
N
Nathan Hourt 已提交
868 869
}

870 871 872 873 874
chain_controller::~chain_controller() {
   clear_pending();
   _db.flush();
   _fork_db.reset();
}
N
Nathan Hourt 已提交
875

876
void chain_controller::replay() {
877
   ilog("Replaying blockchain");
N
Nathan Hourt 已提交
878
   auto start = fc::time_point::now();
879 880 881
   auto last_block = _block_log.read_head();
   if (!last_block) {
      elog("No blocks in block log; skipping replay");
N
Nathan Hourt 已提交
882 883 884 885 886
      return;
   }

   const auto last_block_num = last_block->block_num();

887 888 889 890 891 892
   ilog("Replaying blocks...");
   for (uint32_t i = 1; i <= last_block_num; ++i) {
      if (i % 5000 == 0)
         std::cerr << "   " << double(i*100)/last_block_num << "%   "<<i << " of " <<last_block_num<<"   \n";
      fc::optional<signed_block> block = _block_log.read_block_by_num(i);
      FC_ASSERT(block, "Could not find block #${n} in block_log!", ("n", i));
N
Nathan Hourt 已提交
893 894 895 896 897 898 899 900
      apply_block(*block, skip_producer_signature |
                          skip_transaction_signatures |
                          skip_transaction_dupe_check |
                          skip_tapos_check |
                          skip_producer_schedule_check |
                          skip_authority_check);
   }
   auto end = fc::time_point::now();
901 902
   ilog("Done replaying ${n} blocks, elapsed time: ${t} sec",
        ("n", head_block_num())("t",double((end-start).count())/1000000.0));
N
Nathan Hourt 已提交
903

904
   _db.set_revision(head_block_num());
905
}
N
Nathan Hourt 已提交
906

907 908 909 910
void chain_controller::spinup_db() {
   // Rewind the database to the last irreversible block
   _db.with_write_lock([&] {
      _db.undo_all();
D
Daniel Larimer 已提交
911

912 913 914 915
      FC_ASSERT(_db.revision() == head_block_num(), "Chainbase revision does not match head block num",
                ("rev", _db.revision())("head_block", head_block_num()));
   });
}
N
Nathan Hourt 已提交
916

917
void chain_controller::spinup_fork_db()
N
Nathan Hourt 已提交
918
{
919 920 921 922 923 924 925 926
   fc::optional<signed_block> last_block = _block_log.read_head();
   if(last_block.valid()) {
      _fork_db.start_block(*last_block);
      if (last_block->id() != head_block_id()) {
           FC_ASSERT(head_block_num() == 0, "last block ID does not match current chain state",
                     ("last_block->id", last_block->id())("head_block_num",head_block_num()));
      }
   }
N
Nathan Hourt 已提交
927 928
}

929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
ProducerRound chain_controller::calculate_next_round(const signed_block& next_block) {
   auto schedule = _admin->get_next_round(_db);
   std::sort(schedule.begin(), schedule.end());

   auto ReplaceOldProducers =
         boost::adaptors::transformed([changes = next_block.producer_changes] (AccountName producer) {
      auto itr = changes.find(producer);
      if (itr != changes.end())
         return itr->second;
      return producer;
   });

   ProducerRound round;
   boost::copy(get_global_properties().active_producers | ReplaceOldProducers, round.begin());
   std::sort(round.begin(), round.end());
   EOS_ASSERT(boost::range::equal(schedule, round), block_validate_exception,
              "Unexpected round changes in new block header");

   randutils::seed_seq_fe<1> seed{next_block.timestamp.sec_since_epoch()};
   randutils::random_generator<pcg32_fast> rng(seed);
   rng.shuffle(schedule);
   return schedule;
}

953
void chain_controller::update_global_dynamic_data(const signed_block& b) {
954
   const dynamic_global_property_object& _dgp = _db.get<dynamic_global_property_object>();
N
Nathan Hourt 已提交
955

956 957
   uint32_t missed_blocks = head_block_num() == 0? 1 : get_slot_at_time(b.timestamp);
   assert(missed_blocks != 0);
N
Nathan Hourt 已提交
958
   missed_blocks--;
N
Nathan Hourt 已提交
959

960 961
//   if (missed_blocks)
//      wlog("Blockchain continuing after gap of ${b} missed blocks", ("b", missed_blocks));
N
Nathan Hourt 已提交
962

N
Nathan Hourt 已提交
963
   for(uint32_t i = 0; i < missed_blocks; ++i) {
N
Nathan Hourt 已提交
964
      const auto& producer_missed = get_producer(get_scheduled_producer(i+1));
965
      if(producer_missed.owner != b.producer) {
N
Nathan Hourt 已提交
966 967 968 969 970 971
         /*
         const auto& producer_account = producer_missed.producer_account(*this);
         if( (fc::time_point::now() - b.timestamp) < fc::seconds(30) )
            wlog( "Producer ${name} missed block ${n} around ${t}", ("name",producer_account.name)("n",b.block_num())("t",b.timestamp) );
            */

972
         _db.modify( producer_missed, [&]( producer_object& w ) {
N
Nathan Hourt 已提交
973 974 975 976 977 978
           w.total_missed++;
         });
      }
   }

   // dynamic global properties updating
979
   _db.modify( _dgp, [&]( dynamic_global_property_object& dgp ){
N
Nathan Hourt 已提交
980 981 982
      dgp.head_block_number = b.block_num();
      dgp.head_block_id = b.id();
      dgp.time = b.timestamp;
983
      dgp.current_producer = b.producer;
N
Nathan Hourt 已提交
984 985 986 987 988 989 990 991 992
      dgp.current_absolute_slot += missed_blocks+1;

      // If we've missed more blocks than the bitmap stores, skip calculations and simply reset the bitmap
      if (missed_blocks < sizeof(dgp.recent_slots_filled) * 8) {
         dgp.recent_slots_filled <<= 1;
         dgp.recent_slots_filled += 1;
         dgp.recent_slots_filled <<= missed_blocks;
      } else
         dgp.recent_slots_filled = 0;
N
Nathan Hourt 已提交
993 994 995 996 997
   });

   _fork_db.set_max_size( _dgp.head_block_number - _dgp.last_irreversible_block_num + 1 );
}

998
void chain_controller::update_signing_producer(const producer_object& signing_producer, const signed_block& new_block)
N
Nathan Hourt 已提交
999 1000
{
   const dynamic_global_property_object& dpo = get_dynamic_global_properties();
N
Nathan Hourt 已提交
1001
   uint64_t new_block_aslot = dpo.current_absolute_slot + get_slot_at_time( new_block.timestamp );
N
Nathan Hourt 已提交
1002

1003
   _db.modify( signing_producer, [&]( producer_object& _wit )
N
Nathan Hourt 已提交
1004 1005 1006 1007 1008 1009
   {
      _wit.last_aslot = new_block_aslot;
      _wit.last_confirmed_block_num = new_block.block_num();
   } );
}

1010
void chain_controller::update_last_irreversible_block()
N
Nathan Hourt 已提交
1011 1012 1013 1014
{
   const global_property_object& gpo = get_global_properties();
   const dynamic_global_property_object& dpo = get_dynamic_global_properties();

N
Nathan Hourt 已提交
1015 1016 1017
   vector<const producer_object*> producer_objs;
   producer_objs.reserve(gpo.active_producers.size());
   std::transform(gpo.active_producers.begin(), gpo.active_producers.end(), std::back_inserter(producer_objs),
N
Nathan Hourt 已提交
1018
                  [this](const AccountName& owner) { return &get_producer(owner); });
N
Nathan Hourt 已提交
1019

N
Nathan Hourt 已提交
1020
   static_assert(config::IrreversibleThresholdPercent > 0, "irreversible threshold must be nonzero");
N
Nathan Hourt 已提交
1021

N
Nathan Hourt 已提交
1022
   size_t offset = EOS_PERCENT(producer_objs.size(), config::Percent100 - config::IrreversibleThresholdPercent);
1023 1024
   std::nth_element(producer_objs.begin(), producer_objs.begin() + offset, producer_objs.end(),
      [](const producer_object* a, const producer_object* b) {
N
Nathan Hourt 已提交
1025
         return a->last_confirmed_block_num < b->last_confirmed_block_num;
1026
      });
N
Nathan Hourt 已提交
1027

N
Nathan Hourt 已提交
1028
   uint32_t new_last_irreversible_block_num = producer_objs[offset]->last_confirmed_block_num;
N
Nathan Hourt 已提交
1029

1030
   if (new_last_irreversible_block_num > dpo.last_irreversible_block_num) {
1031
      _db.modify(dpo, [&](dynamic_global_property_object& _dpo) {
N
Nathan Hourt 已提交
1032
         _dpo.last_irreversible_block_num = new_last_irreversible_block_num;
1033
      });
N
Nathan Hourt 已提交
1034
   }
1035 1036

   // Write newly irreversible blocks to disk. First, get the number of the last block on disk...
1037
   auto old_last_irreversible_block = _block_log.head();
1038 1039 1040 1041
   int last_block_on_disk = 0;
   // If this is null, there are no blocks on disk, so the zero is correct
   if (old_last_irreversible_block)
      last_block_on_disk = old_last_irreversible_block->block_num();
1042

1043 1044 1045 1046 1047 1048
   if (last_block_on_disk < new_last_irreversible_block_num)
      for (auto block_to_write = last_block_on_disk + 1;
           block_to_write <= new_last_irreversible_block_num;
           ++block_to_write) {
         auto block = fetch_block_by_number(block_to_write);
         assert(block);
1049
         _block_log.append(*block);
1050
      }
N
Nathan Hourt 已提交
1051 1052 1053

   // Trim fork_database and undo histories
   _fork_db.set_max_size(head_block_num() - new_last_irreversible_block_num + 1);
1054
   _db.commit(new_last_irreversible_block_num);
N
Nathan Hourt 已提交
1055 1056
}

1057
void chain_controller::clear_expired_transactions()
N
Nathan Hourt 已提交
1058 1059 1060
{ try {
   //Look for expired transactions in the deduplication list, and remove them.
   //Transactions must have expired by at least two forking windows in order to be removed.
1061
   auto& transaction_idx = _db.get_mutable_index<transaction_multi_index>();
N
Nathan Hourt 已提交
1062 1063 1064 1065 1066 1067 1068
   const auto& dedupe_index = transaction_idx.indices().get<by_expiration>();
   while( (!dedupe_index.empty()) && (head_block_time() > dedupe_index.rbegin()->trx.expiration) )
      transaction_idx.remove(*dedupe_index.rbegin());
} FC_CAPTURE_AND_RETHROW() }

using boost::container::flat_set;

N
Nathan Hourt 已提交
1069
types::AccountName chain_controller::get_scheduled_producer(uint32_t slot_num)const
N
Nathan Hourt 已提交
1070 1071
{
   const dynamic_global_property_object& dpo = get_dynamic_global_properties();
N
Nathan Hourt 已提交
1072
   uint64_t current_aslot = dpo.current_absolute_slot + slot_num;
1073
   const auto& gpo = _db.get<global_property_object>();
N
Nathan Hourt 已提交
1074
   return gpo.active_producers[current_aslot % gpo.active_producers.size()];
N
Nathan Hourt 已提交
1075 1076
}

1077
fc::time_point_sec chain_controller::get_slot_time(uint32_t slot_num)const
N
Nathan Hourt 已提交
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
{
   if( slot_num == 0 )
      return fc::time_point_sec();

   auto interval = block_interval();
   const dynamic_global_property_object& dpo = get_dynamic_global_properties();

   if( head_block_num() == 0 )
   {
      // n.b. first block is at genesis_time plus one block interval
      fc::time_point_sec genesis_time = dpo.time;
      return genesis_time + slot_num * interval;
   }

   int64_t head_block_abs_slot = head_block_time().sec_since_epoch() / interval;
   fc::time_point_sec head_slot_time(head_block_abs_slot * interval);

   return head_slot_time + (slot_num * interval);
}

1098
uint32_t chain_controller::get_slot_at_time(fc::time_point_sec when)const
N
Nathan Hourt 已提交
1099 1100 1101 1102 1103 1104 1105
{
   fc::time_point_sec first_slot_time = get_slot_time( 1 );
   if( when < first_slot_time )
      return 0;
   return (when - first_slot_time).to_seconds() / block_interval() + 1;
}

1106
uint32_t chain_controller::producer_participation_rate()const
N
Nathan Hourt 已提交
1107 1108
{
   const dynamic_global_property_object& dpo = get_dynamic_global_properties();
N
Nathan Hourt 已提交
1109
   return uint64_t(config::Percent100) * __builtin_popcountll(dpo.recent_slots_filled) / 64;
N
Nathan Hourt 已提交
1110 1111
}

1112
void chain_controller::set_validate_handler( const AccountName& contract, const AccountName& scope, const TypeName& action, message_validate_handler v ) {
D
Daniel Larimer 已提交
1113
   message_validate_handlers[contract][std::make_pair(scope,action)] = v;
1114
}
1115
void chain_controller::set_precondition_validate_handler(  const AccountName& contract, const AccountName& scope, const TypeName& action, precondition_validate_handler v ) {
D
Daniel Larimer 已提交
1116
   precondition_validate_handlers[contract][std::make_pair(scope,action)] = v;
1117
}
1118
void chain_controller::set_apply_handler( const AccountName& contract, const AccountName& scope, const TypeName& action, apply_handler v ) {
D
Daniel Larimer 已提交
1119
   apply_handlers[contract][std::make_pair(scope,action)] = v;
1120
}
N
Nathan Hourt 已提交
1121

1122
chain_initializer_interface::~chain_initializer_interface() {}
N
Nathan Hourt 已提交
1123

N
Nathan Hourt 已提交
1124
} }