Esempio n. 1
0
std::string websocket_api_connection::on_message(
   const std::string& message,
   bool send_message /* = true */ )
{
   wdump((message));
   try
   {
      auto var = fc::json::from_string(message);
      const auto& var_obj = var.get_object();
      if( var_obj.contains( "method" ) )
      {
         auto call = var.as<fc::rpc::request>();
         exception_ptr optexcept;
         try
         {
            auto result = _rpc_state.local_call( call.method, call.params );
            if( call.id )
            {
               auto reply = fc::json::to_string( response( *call.id, result ) );
               if( send_message )
                  _connection.send_message( reply );
               return reply;
            }
         }
         catch ( const fc::exception& e )
         {
            if( call.id )
            {
               optexcept = e.dynamic_copy_exception();
            }
         }
         if( optexcept ) {

               auto reply = fc::json::to_string( response( *call.id,  error_object{ 1, optexcept->to_detail_string(), fc::variant(*optexcept)}  ) );
               if( send_message )
                  _connection.send_message( reply );

               return reply;
         }
      }
      else
      {
         auto reply = var.as<fc::rpc::response>();
         _rpc_state.handle_reply( reply );
      }
   }
   catch ( const fc::exception& e )
   {
      wdump((e.to_detail_string()));
      return e.to_detail_string();
   }
   return string();
}
Esempio n. 2
0
void http_api_connection::on_request( const fc::http::request& req, const fc::http::server::response& resp )
{
   // this must be called by outside HTTP server's on_request method
   std::string resp_body;
   http::reply::status_code resp_status;

   try
   {
      resp.add_header( "Content-Type", "application/json" );
      std::string req_body( req.body.begin(), req.body.end() );
      auto var = fc::json::from_string( req_body );
      const auto& var_obj = var.get_object();

      if( var_obj.contains( "method" ) )
      {
         auto call = var.as<fc::rpc::request>();
         try
         {
            auto result = _rpc_state.local_call( call.method, call.params );
            resp_body = fc::json::to_string( fc::rpc::response( *call.id, result ) );
            resp_status = http::reply::OK;
         }
         catch ( const fc::exception& e )
         {
            resp_body = fc::json::to_string( fc::rpc::response( *call.id, error_object{ 1, e.to_detail_string(), fc::variant(e)} ) );
            resp_status = http::reply::InternalServerError;
         }
      }
      else
      {
         resp_status = http::reply::BadRequest;
         resp_body = "";
      }
   }
   catch ( const fc::exception& e )
   {
      resp_status = http::reply::InternalServerError;
      resp_body = "";
      wdump((e.to_detail_string()));
   }
   try
   {
      resp.set_status( resp_status );
      resp.set_length( resp_body.length() );
      resp.write( resp_body.c_str(), resp_body.length() );
   }
   catch( const fc::exception& e )
   {
      wdump((e.to_detail_string()));
   }
   return;
}
void dump_ethernet_raw_packet(uint8 bInput, char* szMessage, uint8 *payload, int32 len)
{
	extern int g_modePacketDump;
	extern int g_modePacketInfo;
	extern uint8_t g_macFilterUse;
	extern uint8_t g_macFilter1[6];
	extern uint8_t g_macFilter2[6];

	if ( g_macFilterUse )
	{
		if ( memcmp(payload, g_macFilter1, 6)==0 && memcmp(payload+6, g_macFilter1, 6)==0 && memcmp(payload+6, g_macFilter2, 6)==0 && memcmp(payload+6, g_macFilter2, 6)==0 )
		{
			return;
		}
	}

	if ( g_modePacketDump==1 )
	{
		ets_uart_printf(szMessage);
		ets_uart_printf(" (%d)\r\n", len);
		wdump(payload, len);
	}

	if ( g_modePacketInfo==1 )
	{
		void packet_handler(uint32_t packet_length, const uint8_t *packet);
		packet_handler(len, payload);
	}
}
Esempio n. 4
0
    void rpc_client_impl::connect_to(const fc::ip::endpoint& remote_endpoint,
                                     const bts::blockchain::public_key_type& remote_public_key)
    {
       fc::buffered_istream_ptr buffered_istream;
       fc::buffered_ostream_ptr buffered_ostream;

       if( remote_public_key != bts::blockchain::public_key_type() )
       {
          net::stcp_socket_ptr socket = std::make_shared<bts::net::stcp_socket>();

          try
          {
             socket->connect_to(remote_endpoint);
          }
          catch ( const fc::exception& e )
          {
             elog( "fatal: error opening RPC socket to endpoint ${endpoint}: ${e}", ("endpoint", remote_endpoint)("e", e.to_detail_string() ) );
             throw;
          }

          std::vector<char> packed_signature(80, ' ');
          FC_ASSERT( socket->readsome(packed_signature.data(), packed_signature.size()) == 80, "Unexpected number of bytes read from RPC socket" );
          fc::ecc::compact_signature signature;
          signature = fc::raw::unpack<fc::ecc::compact_signature>(packed_signature);
          wdump((signature));
          FC_ASSERT(blockchain::public_key_type(fc::ecc::public_key(signature, fc::digest(socket->get_shared_secret())))
                    == remote_public_key,
                    "Unable to establish secure connection with server.");

          buffered_istream = std::make_shared<fc::buffered_istream>(socket);
          buffered_ostream = std::make_shared<utilities::padding_ostream<>>(socket);
       } else {
          fc::tcp_socket_ptr socket = std::make_shared<fc::tcp_socket>();

          try
          {
             socket->connect_to(remote_endpoint);
          }
          catch ( const fc::exception& e )
          {
             elog( "fatal: error opening RPC socket to endpoint ${endpoint}: ${e}", ("endpoint", remote_endpoint)("e", e.to_detail_string() ) );
             throw;
          }

          buffered_istream = std::make_shared<fc::buffered_istream>(socket);
          buffered_ostream = std::make_shared<fc::buffered_ostream>(socket);
       }

       _json_connection = std::make_shared<fc::rpc::json_connection>(std::move(buffered_istream),
                                                                     std::move(buffered_ostream));
       _json_exec_loop_complete = fc::async([=](){ _json_connection->exec(); }, "json exec loop");
    }
Esempio n. 5
0
void dynamic_global_property_object::update_virtual_net_bandwidth( const chain_config& cfg ) {

  if( average_block_size.average()> (cfg.target_block_size * config::rate_limiting_precision) ) {
    virtual_net_bandwidth *= 99;
    virtual_net_bandwidth /= 100;
    wlog( "reducing virtual net bandwidth by 1%, ${vnb}", ("vnb", virtual_net_bandwidth) );
    wdump((average_block_size.average())(cfg.target_block_size*config::rate_limiting_precision));
  } else {
    virtual_net_bandwidth *= 1000;
    virtual_net_bandwidth /= 999;
//    ilog( "increasing virtual net bandwidth by .1%, ${vnb}", ("vnb", virtual_net_bandwidth) );
  }

  auto min = (cfg.max_block_size * config::blocksize_average_window_ms) / config::block_interval_ms;

  if( virtual_net_bandwidth <  min )         virtual_net_bandwidth = min;
  if( virtual_net_bandwidth >  min * 1000 )  virtual_net_bandwidth = min * 1000;
}
Esempio n. 6
0
int main( int argc, char** argv )
{
   try {
      auto InB1 = fc::sha256::hash("InB1");
      auto InB2 = fc::sha256::hash("InB2");
      auto OutB1 = fc::sha256::hash("OutB1");


      auto InC1 = fc::ecc::blind(InB1,25);
      auto InC2 = fc::ecc::blind(InB2,75);

      auto OutC1 = fc::ecc::blind(OutB1,40);

      auto OutB2 = fc::ecc::blind_sum( {InB1,InB2,OutB1}, 2 );
      auto OutC2 = fc::ecc::blind( OutB2, 60 );

      FC_ASSERT( fc::ecc::verify_sum( {InC1,InC2}, {OutC1,OutC2}, 0 ) );
      auto nonce = fc::sha256::hash("nonce");

      auto proof = fc::ecc::range_proof_sign( 0, OutC1, OutB1, nonce, 0, 0, 40 );
      wdump( (proof.size()));

      auto result = fc::ecc::range_get_info( proof );
      wdump((result));
      FC_ASSERT( result.max_value >= 60 );
      FC_ASSERT( result.min_value >= 0 );


      auto B1 = fc::sha256::hash("B1");
      auto B2 = fc::sha256::hash("B2");
      auto b3 = fc::sha256::hash("b3");
      auto B4 = fc::sha256::hash("B4");
      auto C1 = fc::ecc::blind( B1, 1  );
      auto C2 = fc::ecc::blind( B2, 2  );
      auto c3 = fc::ecc::blind( b3, 3  );
      auto C4 = fc::ecc::blind( B4, -1 );

      auto B3 = fc::ecc::blind_sum( {B1,B2}, 2 );
      auto C3 = fc::ecc::blind( B3, 3 );


      auto B2m1 = fc::ecc::blind_sum( {B2,B1}, 1 );
      auto C2m1 = fc::ecc::blind( B2m1, 1 );

      FC_ASSERT( fc::ecc::verify_sum( {C1,C2}, {C3}, 0 ) );
      FC_ASSERT( fc::ecc::verify_sum( {C3}, {C1,C2}, 0 ) );
      FC_ASSERT( fc::ecc::verify_sum( {C3}, {C1,C2}, 0 ) );


      {
         auto B1 = fc::sha256::hash("B1");
         auto B2 = fc::sha256::hash("B2");
         auto B3 = fc::sha256::hash("B3");

         //secp256k1_scalar_get_b32((unsigned char*)&B1, (const secp256k1_scalar_t*)&B2);
         //B1 = fc::variant("b2e5da56ef9f2a34d3e22fd12634bc99261e95c87b9960bf94ed3d27b30").as<fc::sha256>();

         auto C1 = fc::ecc::blind( B1, INT64_MAX );
         auto C2 = fc::ecc::blind( B1, 0 );
         auto C3 = fc::ecc::blind( B1, 1 );

         FC_ASSERT( fc::ecc::verify_sum( {C2}, {C3}, -1 ) );
         FC_ASSERT( fc::ecc::verify_sum( {C1}, {C1}, 0 ) );
         FC_ASSERT( fc::ecc::verify_sum( {C2}, {C2}, 0 ) );
         FC_ASSERT( fc::ecc::verify_sum( {C3}, {C2}, 1 ) );
         FC_ASSERT( fc::ecc::verify_sum( {C1}, {C2}, INT64_MAX ) );
         FC_ASSERT( fc::ecc::verify_sum( {C2}, {C1}, -INT64_MAX ) );


      }


   }
   catch ( const fc::exception& e )
   {
      edump((e.to_detail_string()));
   }
   return 0;
}