美文网首页
eos源码解析(五):区块的分叉与回退

eos源码解析(五):区块的分叉与回退

作者: 荒原葱郁 | 来源:发表于2018-09-12 20:52 被阅读523次

    谢谢可爱的小哥哥小姐姐的支持,不知不觉来到了第五节,看完点个赞呗,不然度娘都搜不到,不方便大家查找O(∩_∩)O
    惊闻以太坊暴跌如雷,慌乱中瞄了瞄钱包的柚子,还好,没跌破30。如果说个人是被时代的洪流裹挟前进,那么币圈则被资本的洪流包围。资本现在流向哪里,相信善搭梯子各位,心中已有答案。郑重声明:以上纯属虚构,言归正传,继续第五节

    image

    1,收到一个区块

    2,把自己的pending放弃

    4,将区块信息加入fork_db中,先在fork_db中查找引用区块的id,没有找到就算成是unlink的区块,直接放弃。然后更新fork_db的head,这个指向可逆区块高度最高同时序号最大的一个区块。然后选出fork_db中current链最小的区块,把它以及与低于它的所有区块信息从fork_db中擦除。然后将db的相应内存退栈丢弃(4.4)。

          my->head = *my->index.get<by_lib_block_num>().begin();
          auto lib    = my->head->dpos_irreversible_blocknum;
          auto oldest = *my->index.get<by_block_num>().begin();
          if( oldest->block_num < lib ) {
             prune( oldest );
          }
    

    5,如果当前fork_db中的head大于chain中的head,则考虑是不是应该换链,如果fork_db中head指向的是chain中的head则不换链,只是在当前链上加一个区块,如果fork_db中的head指向的不是当前链的head则要换链。

    6,将旧链的内存栈丢弃。

    9,创建新的可撤销内存栈

    内存栈的结构:

    eos存放于内存数据又是以一张张表存进去的,eos把当前的“状态”都存放在内存里面,这点与以太坊不一样,比如账户数据存放在内存的account_index表中。

    void add_indices() {
          reversible_blocks.add_index<reversible_block_index>();
    
          db.add_index<account_index>();
          db.add_index<account_sequence_index>();
    
          db.add_index<table_id_multi_index>();
          db.add_index<key_value_index>();
          db.add_index<index64_index>();
          db.add_index<index128_index>();
          db.add_index<index256_index>();
          db.add_index<index_double_index>();
          db.add_index<index_long_double_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<generated_transaction_multi_index>();
    
          authorization.add_indices();
          resource_limits.add_indices();
       }
    
    

    比如account_index表,这个表会经过generic_index封装,然后赋值给:

    _index_map[ type_id ].reset( new_index );_index_list.push_back( new_index );
    

    _index_map和_index_list就是db中存放这些表的地方,里面存放的是被generic_index封装的一张张表。这里,可以看到,_index_map和_index_list里面存的内容是一致的,之所以使用map是为了更好搜索,接下来看generic_index数据结构:

    .......
    boost::interprocess::deque< undo_state_type, allocator<undo_state_type> > _stack;
    ........
    typedef MultiIndexType        index_type; //account_index
    index_type                      _indices;
    .......
    session start_undo_session( bool enabled ) {
       if( enabled ) {
          _stack.emplace_back( _indices.get_allocator() );
          _stack.back().old_next_id = _next_id;
          _stack.back().revision = ++_revision;
          return session( *this, _revision );
       } else {
          return session( *this, -1 );
       }
    }
    .......
    void undo() {
       if( !enabled() ) return;
    
       const auto& head = _stack.back();
    
       for( auto& item : head.old_values ) {
          auto ok = _indices.modify( _indices.find( item.second.id ), [&]( value_type& v ) {
             v = std::move( item.second );
          });
          if( !ok ) BOOST_THROW_EXCEPTION( std::logic_error( "Could not modify object, most likely a uniqueness constraint was violated" ) );
       }
    
       for( auto id : head.new_ids )
       {
          _indices.erase( _indices.find( id ) );
       }
       _next_id = head.old_next_id;
    
       for( auto& item : head.removed_values ) {
          bool ok = _indices.emplace( std::move( item.second ) ).second;
          if( !ok ) BOOST_THROW_EXCEPTION( std::logic_error( "Could not restore object, most likely a uniqueness constraint was violated" ) );
       }
    
       _stack.pop_back();
       --_revision;
    }
    .......
    
    

    可以发现,新建一个段就是在_stack末尾压入一个表,删除一个段就是去掉末尾的那个表。

    在第8步的start_block中有:

    pending = db.start_undo_session(true);
    

    这个函数在的实现如下:

    database::session database::start_undo_session( bool enabled )
    {
       if( enabled ) {
          vector< std::unique_ptr<abstract_session> > _sub_sessions;
          _sub_sessions.reserve( _index_list.size() );
          for( auto& item : _index_list ) {
             _sub_sessions.push_back( item->start_undo_session( enabled ) );
          }
          return session( std::move( _sub_sessions ) );
       } else {
          return session();
       }
    }
    

    可以这样粗鲁的理解,db中存放的是一个map和数组,它们是一样的东东,map只是为了搜索方便。这个数组的元素是一个封装了某种表的对象,这个对象里面是用队列存了好多张表,是按块序号存放的,只有最后一张是现在用的,撤销或者增加只要对队列进行相应操作就行,比如增加一个新区块,就是在数组中的每个元素上新增一张表。

    相关文章

      网友评论

          本文标题:eos源码解析(五):区块的分叉与回退

          本文链接:https://www.haomeiwen.com/subject/zmsfwftx.html