Ejemplo n.º 1
0
 ~impl() {
   try {
     tcp_serv.close();
     if( accept_complete.valid() )
        accept_complete.wait();
   }catch(...){}
 }
Ejemplo n.º 2
0
 ~client()
 {
      try {
          if( chain_connect_loop_complete.valid() )
          {
             try {
                _chain_con.close();
                chain_connect_loop_complete.cancel();
                chain_connect_loop_complete.wait();
             } 
             catch( fc::exception& e )
             {
               wlog( "unhandled exception thrown in destructor.\n${e}", ("e", e.to_detail_string() ) );
             }
          }
          _tcp_serv.close();
          if( _accept_loop_complete.valid() )
          {
             _accept_loop_complete.cancel();
             _accept_loop_complete.wait();
          }
      } 
      catch ( const fc::canceled_exception& ){}
      catch ( const fc::exception& e )
      {
         wlog( "unhandled exception thrown in destructor.\n${e}", ("e", e.to_detail_string() ) );
      }
 }
Ejemplo n.º 3
0
      void accept_loop() throw()
      {
         try
         {
            while( !accept_loop_complete.canceled() )
            {
               auto sock = std::make_shared<fc::tcp_socket>();
               tcp_serv.accept( *sock );

               // do the acceptance process async
               fc::async( [=](){ accept_connection( sock ); } );

               fc::usleep(fc::microseconds(1000) );
            }
         } 
         catch ( fc::eof_exception& e )
         {
            ilog( "accept loop eof" );
         }
         catch ( fc::canceled_exception& e )
         {
            ilog( "accept loop canceled" );
         }
         catch ( fc::exception& e )
         {
            elog( "tcp server socket threw exception\n ${e}", 
                                 ("e", e.to_detail_string() ) );
            // TODO: notify the server delegate of the error.
         }
         catch( ... )
         {
            elog( "unexpected exception" );
         }
      } 
Ejemplo n.º 4
0
       void client_impl::trustee_loop()
       {
         _last_block = _chain_db->get_head_block().timestamp;
         while (!_trustee_loop_complete.canceled())
         {
           signed_transactions pending_trxs;
           pending_trxs = get_pending_transactions();
           if (pending_trxs.size() && (fc::time_point::now() - _last_block) > fc::seconds(30))
           {
             try {
               bts::blockchain::trx_block blk = _wallet->generate_next_block(*_chain_db, pending_trxs);
               blk.sign(_trustee_key);
               // _chain_db->push_block( blk );
               if (_chain_client)
                 _chain_client->broadcast_block(blk);
               else
               {
                 _p2p_node->broadcast(block_message(blk.id(), blk, blk.trustee_signature));
                 // with the p2p code, if you broadcast something to the network, it will not
                 // immediately send it back to you 
                 on_new_block(blk);
               }

               _last_block = fc::time_point::now();
             }
             catch (const fc::exception& e)
             {
               elog("error producing block?: ${e}", ("e", e.to_detail_string()));
             }
           }
           fc::usleep(fc::seconds(1));
         }
       }
Ejemplo n.º 5
0
 void accept_loop() {
       while( !accept_complete.canceled() )
       {
         http::connection_ptr con = std::make_shared<http::connection>();
         tcp_serv.accept( con->get_socket() );
         //ilog( "Accept Connection" );
         fc::async( [=](){ handle_connection( con, on_req ); }, "http_server handle_connection" );
       }
 }
 ~bts_server_process_info()
 {
   server_process->kill();
   if (stdout_reader_done.valid() && !stdout_reader_done.ready())
     stdout_reader_done.wait();
   if (stderr_reader_done.valid() && !stderr_reader_done.ready())
     stderr_reader_done.wait();
 }
Ejemplo n.º 7
0
             void block_generation_loop()
             {
                while( !_block_gen_loop_complete.canceled() )
                {
                   if( _pending.size() && (fc::time_point::now() - _current_block.timestamp) > fc::seconds(60) )
                   {
                      signed_block next_block;
                      next_block.number     = _current_block.number + 1;
                      auto next_diff        = _current_block.difficulty * 500 / _pending.size(); 
                      next_diff             = (_current_block.difficulty * 99 + next_diff) / 100;
                      next_block.difficulty = std::max<uint64_t>(next_diff, 1000 );
                      next_block.timestamp  = fc::time_point::now();
       
                      for( auto rec : _pending )
                      {
                         next_block.records.push_back( rec.second );
                      }
       
                      next_block.sign( _trustee_key );
                      _block_database.store(next_block.number,next_block);
       
                      for( uint32_t rec = 0; rec < next_block.records.size(); ++rec )
                      {
                         auto new_rec = next_block.records[rec];
                         auto hist = _self->fetch_history( new_rec.name );
                         hist.updates.push_back( name_index( next_block.number, rec ) );
                         _name_index.store( new_rec.name, hist );
                         _key_to_name.store( new_rec.active_key.to_base58(), new_rec.name );
                      }
       
                      _current_block = next_block;
                      _current_block_id = _current_block.id();

                      fc::path block_file = _data_dir / "block" / fc::to_string( uint64_t(_current_block.number) );

                      std::ofstream out( block_file.generic_string().c_str() );
                      auto block_str = fc::json::to_pretty_string( _current_block );
                      out.write( block_str.c_str(), block_str.size() );
                   }
                   fc::usleep( fc::seconds( 1 ) );
                }
             }
Ejemplo n.º 8
0
      void accept_loop()
      {
        while( !_accept_loop_complete.canceled() )
        {
           fc::tcp_socket_ptr sock = std::make_shared<fc::tcp_socket>();
           try 
           {
             _tcp_serv.accept( *sock );
           }
           catch ( const fc::exception& e )
           {
             elog( "fatal: error opening socket for rpc connection: ${e}", ("e", e.to_detail_string() ) );
             return;
           }

           auto buf_istream = std::make_shared<fc::buffered_istream>( sock );
           auto buf_ostream = std::make_shared<fc::buffered_ostream>( sock );

           auto json_con = std::make_shared<fc::rpc::json_connection>( std::move(buf_istream), std::move(buf_ostream) );
           register_methods( json_con );

           fc::async( [json_con]{ json_con->exec().wait(); } );
        }
      }
Ejemplo n.º 9
0
        /**
         *  This method is called async 
         */
        void accept_loop() throw()
        {
           try
           {
              while( !accept_loop_complete.canceled() )
              {
                 stcp_socket_ptr sock = std::make_shared<stcp_socket>();
                 tcp_serv.accept( sock->get_socket() );

                 // do the acceptance process async
                 fc::async( [=](){ accept_connection( sock ); } );

                 // limit the rate at which we accept connections to prevent
                 // DOS attacks.
                 fc::usleep( fc::microseconds( 1000*1 ) );
              }
           } 
           catch ( fc::eof_exception& e )
           {
              ilog( "accept loop eof" );
           }
           catch ( fc::canceled_exception& e )
           {
              ilog( "accept loop canceled" );
           }
           catch ( fc::exception& e )
           {
              elog( "tcp server socket threw exception\n ${e}", 
                                   ("e", e.to_detail_string() ) );
              // TODO: notify the server delegate of the error.
           }
           catch( ... )
           {
              elog( "unexpected exception" );
           }
        }
Ejemplo n.º 10
0
 ~impl() {
   try {
     tcp_serv.close();
     accept_complete.wait();
   }catch(...){}
 }
Ejemplo n.º 11
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];
                  sock->read( tmp, BUFFER_SIZE );
                  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) );
                  m.data.resize(m.size);

                  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() ) );
            }
          }
Ejemplo n.º 12
0
 int wait_any( fc::future<T1>& f1, fc::future<T2>& f2, const microseconds& timeout_us = microseconds::max() ) {
   fc::vector<promise_base::ptr> p(2);
   p[0] = static_pointer_cast<promise_base*>(f1.promise());
   p[1] = static_pointer_cast<promise_base*>(f2.promise());
   return wait( fc::move(p), timeout_us );   
 }
Ejemplo n.º 13
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