问题

看nodeos的启动参数的时候,看到这么一项: --checkpoint arg Pairs of [BLOCK_NUM,BLOCK_ID] that should be enforced as checkpoints.
这checkpoint是什么,主要用来干嘛的?
问题来自:eosfans

解答

查看config中的注释
Pairs of [BLOCK_NUM,BLOCK_ID] that should be enforced as checkpoints. (eosio::chain_plugin)
多对区块高度+区块id,用来作为检查点。默认注释掉,不设置检查点

大概的意思就是,接收区块时,根据区段的高度,查看当前区块的id与预期的一样不一样,防止数据出错。

撸一遍代码
eos\plugins\chain_plugin\chain_plugin.cpp

if( options.count("checkpoint") ) {
         auto cps = options.at("checkpoint").as<vector<string>>();
         my->loaded_checkpoints.reserve(cps.size());
         for( const auto& cp : cps ) {
            auto item = fc::json::from_string(cp).as<std::pair<uint32_t,block_id_type>>();
            auto itr = my->loaded_checkpoints.find(item.first);
            if( itr != my->loaded_checkpoints.end() ) {
               EOS_ASSERT( itr->second == item.second,
                           plugin_config_exception,
                          "redefining existing checkpoint at block number ${num}: original: ${orig} new: ${new}",
                          ("num", item.first)("orig", itr->second)("new", item.second)
               );
            } else {
               my->loaded_checkpoints[item.first] = item.second;
            }
         }
      }

先将config中配置的,或者命令行传入的,多对区块高度+区块id存入my->loaded_checkpoints,并去重。

接收区块时,进行对比验证

// relay signals to channels
      my->pre_accepted_block_connection = my->chain->pre_accepted_block.connect([this](const signed_block_ptr& blk) {
         auto itr = my->loaded_checkpoints.find( blk->block_num() );
         if( itr != my->loaded_checkpoints.end() ) {
            auto id = blk->id();
            EOS_ASSERT( itr->second == id, checkpoint_exception,
                        "Checkpoint does not match for block number ${num}: expected: ${expected} actual: ${actual}",
                        ("num", blk->block_num())("expected", itr->second)("actual", id)
            );
         }

         my->pre_accepted_block_channel.publish(blk);
      });

当接收的区块的高度与预定中的某高度一致,比较当前区块id与预定的id,如果不一致,则报错Checkpoint does not match for block number...