コード例 #1
0
          void read_loop()
          {
            const int BUFFER_SIZE = 16;
            const int LEFTOVER = BUFFER_SIZE - sizeof(message_header);
            try {
               message m;
               while( !read_loop_complete.canceled() )
               {
                  char tmp[BUFFER_SIZE];
                  ilog( "read.." );
                  sock->read( tmp, BUFFER_SIZE );
                  ilog( "." );
                  memcpy( (char*)&m, tmp, sizeof(message_header) );
                  m.data.resize( m.size + 16 ); //give extra 16 bytes to allow for padding added in send call
                  memcpy( (char*)m.data.data(), tmp + sizeof(message_header), LEFTOVER );
                  sock->read( m.data.data() + LEFTOVER, 16*((m.size -LEFTOVER + 15)/16) );

                  try { // message handling errors are warnings... 
                    con_del->on_connection_message( self, m );
                  } 
                  catch ( fc::canceled_exception& e ) { wlog(".");throw; }
                  catch ( fc::eof_exception& e ) { wlog(".");throw; }
                  catch ( fc::exception& e ) 
                  { 
                     wlog( "disconnected ${er}", ("er", e.to_detail_string() ) );
                     return;
                     // TODO: log and potentiall disconnect... for now just warn.
                  }
                  catch( ... )
                  {
                     wlog("...????" );
                     return;
                  }
               }
            } 
            catch ( const fc::canceled_exception& e )
            {
              if( con_del )
              {
                 con_del->on_connection_disconnected( self );
              }
              else
              {
                wlog( "disconnected ${e}", ("e", e.to_detail_string() ) );
              }
              wlog( "exit read loop" );
              return;
            }
            catch ( const fc::eof_exception& e )
            {
              if( con_del )
              {
                 ilog( ".");
                 fc::async( [=](){con_del->on_connection_disconnected( self );} );
                 ilog( ".");
              }
              else
              {
                wlog( "disconnected ${e}", ("e", e.to_detail_string() ) );
              }
            }
            catch ( fc::exception& er )
            {
               wlog( ".." );
              if( con_del )
              {
                elog( "disconnected ${er}", ("er", er.to_detail_string() ) );
                //con_del->on_connection_disconnected( self );
                fc::async( [=](){con_del->on_connection_disconnected( self );} );
              }
              else
              {
                elog( "disconnected ${e}", ("e", er.to_detail_string() ) );
              }
              FC_RETHROW_EXCEPTION( er, warn, "disconnected ${e}", ("e", er.to_detail_string() ) );
            }
            catch ( ... )
            {
               wlog( "unhandled??" );
              // TODO: call con_del->????
              FC_THROW_EXCEPTION( unhandled_exception, "disconnected: {e}", ("e", fc::except_str() ) );
            }
          }
コード例 #2
0
 void mining_loop()
 {
    while( !_mining_loop_complete.canceled() )
    {
      try {
          if( _current_block.prev == block_id_type() || !_callback || _effort < 0.01 || _prev_header.next_difficulty == 0 )
          {
             ilog( "${current.prev}  _effort ${effort}  prev_header: ${prev_header}", 
                   ("current.prev",_current_block.prev)("effort",_effort)("prev_header",_prev_header) );
             fc::usleep( fc::microseconds( 1000*1000 ) );
             continue;
          }
          auto start = fc::time_point::now();
         
          block_header tmp = _current_block;
          tmp.timestamp = fc::time_point::now();
          auto next_diff = _prev_header.next_difficulty * 300*1000000ll / (tmp.timestamp - _prev_header.timestamp).count();
          tmp.next_difficulty = (_prev_header.next_difficulty * 24 + next_diff ) / 25;
         
          tmp.noncea = 0;
          tmp.nonceb = 0;
          auto tmp_id = tmp.id();
          auto seed = fc::sha256::hash( (char*)&tmp_id, sizeof(tmp_id) );
          auto pairs = momentum_search( seed );
          for( auto collision : pairs )
          {
             tmp.noncea = collision.first;
             tmp.nonceb = collision.second;
             FC_ASSERT( _min_votes > 0 );
             FC_ASSERT( _prev_header.next_difficulty > 0 );
             ilog( "difficlty ${d}  target ${t}  tmp.get_difficulty ${dd}  mv ${mv} min: ${min}  block:\n${block}", ("min",_min_votes)("mv",_miner_votes)("dd",tmp.get_difficulty())
                                                                                   ("d",(tmp.get_difficulty() * _miner_votes)/_min_votes)("t",_prev_header.next_difficulty)("block",_current_block) );
             if( (tmp.get_difficulty() * _miner_votes)/_min_votes  >= _prev_header.next_difficulty )
             {
                if( _callback )
                {
                   auto cb = _callback; 
                   _main_thread->async( [cb,tmp](){cb( tmp );} );
                }
                _effort = 0;
                break;
             }
          }
         
          // search space...
          
          auto end   = fc::time_point::now();
         
          // wait while checking for cancel...
          if( _effort < 1.0 )
          {
             auto calc_time = (end-start).count();
             auto wait_time = ((1-_effort)/_effort) * calc_time;
         
             auto wait_until = end + fc::microseconds(wait_time);
             if( wait_until > fc::time_point::now() && !_mining_loop_complete.canceled() )
             {
                ilog( "." );
                fc::usleep( fc::microseconds( 1000*100 ) );
             }
          }
          else
          {
             ilog( "." );
             fc::usleep( fc::microseconds(1000*10) );
          }
      }
      catch ( const fc::exception& e )
      {
         wlog( "${e}", ("e",e.to_detail_string() ));
      }
    } // while 
 } /// mining_loop