Author Topic: BTS源代码解读(二)  (Read 2621 times)

0 Members and 1 Guest are viewing this topic.

Offline callmeluc

  • Hero Member
  • *****
  • Posts: 552
    • View Profile
BTS_自扯自淡

Offline sudo

  • Hero Member
  • *****
  • Posts: 2255
    • View Profile
  • BitShares: ags
多来几期解读。赞。 +5% +5%

Offline merlin0113

  • Sr. Member
  • ****
  • Posts: 286
    • View Profile
这种解读意义大,能深入代码本质,还能发现bug?!赞!

Offline BTSdac

  • Hero Member
  • *****
  • Posts: 1219
    • View Profile
  • BitShares: K1
github.com :pureland
BTS2.0 API :ws://139.196.37.179:8091
BTS2.0 API 数据源ws://139.196.37.179:8091

Offline kinglaw0577

  • Full Member
  • ***
  • Posts: 95
    • View Profile
    • BitShares Richlist
BTS源代码解读(二)【首发比特股之家bts.hk】
1.市场锚定资产的利息来源
大家知道货币流通才创造价值,现实生活中,货币只有借给别人或定存银行才有利息,现金存在家里是没有利息的,可以说利息是货币使用权的对价。
而Bitshares系统中市场锚定资产(如bitUSD、bitCNY)存在账户就可以拿利息(系统不能把它贷给别人),这个利息是从哪里来的呢? Bitshares系统会从牵涉到该资产的交易以及市场费用(市场锚定资产的交易费用是用该资产支付的,用户发行资产是用BTS支付费用)收集起来, 为其持有者产生回报. 这将会是浮动收益, 。看起来是比特股系统把市场锚定资产的流通价值又反馈给了所有持有人的一种设计。

源代码略...


2.销毁资产的功能(Burn命令的补充说明)
   Burn命令销毁的资产可以是BTS或用户自定义资产,市场锚定资产不能销毁。销毁BTS,一次最少为1BTS。所有销毁记录记录到接受方的荣誉墙,销毁BTS才可以附带消息。
   (^-^就是焚烧资产奉献给谁,表示支持或反对,就能在谁的墙上留下焚烧记录^-^)

      if( message.size() )
          FC_ASSERT( amount.asset_id == 0 );//<=如果附带消息,销毁资产ID必须是0号,即BTS

      if( amount.asset_id == 0 )
      {
          FC_ASSERT( amount.amount >= BTS_BLOCKCHAIN_MIN_BURN_FEE, "",//<=如果销毁资产ID是0号,即BTS,必须1BTS以上。
                     ("amount",amount)
                     ("BTS_BLOCKCHAIN_MIN_BURN_FEE",BTS_BLOCKCHAIN_MIN_BURN_FEE) );
      }

      oasset_record asset_rec = eval_state._current_state->get_asset_record( amount.asset_id );
      FC_ASSERT( asset_rec.valid() );
      FC_ASSERT( !asset_rec->is_market_issued() );

      asset_rec->current_share_supply -= this->amount.amount;
      eval_state.sub_balance( address(), this->amount );

      eval_state._current_state->store_asset_record( *asset_rec );

      if( account_id != 0 ) // you can offer burnt offerings to God if you like... otherwise it must be an account
      {
          // TODO: support burning to any OBJECT ID not just accounts
          const oaccount_record account_rec = eval_state._current_state->get_account_record( abs( this->account_id ) );
          FC_ASSERT( account_rec.valid() );
      }
      eval_state._current_state->store_burn_record( burn_record( burn_record_key( {account_id, eval_state.trx.id()} ),
                                                                 burn_record_value( {amount,message,message_signature} ) ) );
3.受托人随机排序问题
当块号(block_num) 除 101,除数为0, 更新受托人列表,并随机排列。
//BTS_BLOCKCHAIN_NUM_DELEGATES  = 101
void chain_database_impl::update_active_delegate_list( const full_block& block_data,
                                                             const pending_chain_state_ptr& pending_state )
      { try {
          if( block_data.block_num % BTS_BLOCKCHAIN_NUM_DELEGATES != 0 )   //除数不为0,直接返回
              return;

          auto active_del = self->next_round_active_delegates();  //取得下一轮受托人列表
          const size_t num_del = active_del.size();//取得受托人列表长度,应该是101

          // Perform a random shuffle of the sorted delegate list.
          fc::sha256 rand_seed = fc::sha256::hash( pending_state->get_current_random_seed() ); //取当前随机数
          for( uint32_t i = 0; i < num_del; ++i )
          {
            ////first time,here,i==0;the second time,for start i==5
             for( uint32_t x = 0; x < 4 && i < num_del; ++x, ++i )
                std::swap( active_del[ i ], active_del[ rand_seed._hash[ x ] % num_del ] );  //这里对0~100位置上的受托人与随机位置上的受托人重新进行一次调换
             rand_seed = fc::sha256::hash( rand_seed );
          } 
         ////look,first time,here i==4
          pending_state->set_active_delegates( active_del );
         ////and end of for ++i  then i==5
      } FC_CAPTURE_AND_RETHROW() }

细心的人应该看出来了,这里代码出现一个Bug,在4,9,14,19...位置上的受托人,在代码中被跳过执行 调换的那个语句了。会导致这些位置上的受托人下一轮很大几率上还在原来的位置。
本人发现了这个Bug,并第一时间报告了3I。

下期解读预告:
1.Bitshares随机数产生原理;
2.BTS投票的时候具体是怎样一个过程。


☆bitshares Richlist富豪榜 http://richlist.btsabc.org
☆BTSABC wallet online比特帝国在线钱包 https://bit.btsabc.org