2017-02-21 17:38:18 +00:00
// Copyright (c) 2014-2017, The Monero Project
2016-10-10 20:41:24 +00:00
//
2014-07-23 13:03:52 +00:00
// All rights reserved.
2016-10-10 20:41:24 +00:00
//
2014-07-23 13:03:52 +00:00
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
2016-10-10 20:41:24 +00:00
//
2014-07-23 13:03:52 +00:00
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
2016-10-10 20:41:24 +00:00
//
2014-07-23 13:03:52 +00:00
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
2016-10-10 20:41:24 +00:00
//
2014-07-23 13:03:52 +00:00
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
2016-10-10 20:41:24 +00:00
//
2014-07-23 13:03:52 +00:00
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2016-10-10 20:41:24 +00:00
//
2014-07-23 13:03:52 +00:00
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
2014-03-03 22:07:58 +00:00
# include "include_base_utils.h"
using namespace epee ;
# include "core_rpc_server.h"
# include "common/command_line.h"
2017-02-24 23:16:13 +00:00
# include "common/updates.h"
# include "common/download.h"
# include "common/util.h"
2017-01-26 15:07:23 +00:00
# include "cryptonote_basic/cryptonote_format_utils.h"
# include "cryptonote_basic/account.h"
# include "cryptonote_basic/cryptonote_basic_impl.h"
2014-03-03 22:07:58 +00:00
# include "misc_language.h"
# include "crypto/hash.h"
2017-02-05 22:48:03 +00:00
# include "rpc/rpc_args.h"
2014-03-03 22:07:58 +00:00
# include "core_rpc_server_error_codes.h"
2017-02-27 20:25:35 +00:00
# undef MONERO_DEFAULT_LOG_CATEGORY
# define MONERO_DEFAULT_LOG_CATEGORY "daemon.rpc"
2016-06-29 19:43:14 +00:00
# define MAX_RESTRICTED_FAKE_OUTS_COUNT 40
2016-08-02 20:48:09 +00:00
# define MAX_RESTRICTED_GLOBAL_FAKE_OUTS_COUNT 500
2016-06-29 19:43:14 +00:00
2017-09-16 09:31:49 +00:00
namespace
{
void add_reason ( std : : string & reasons , const char * reason )
{
if ( ! reasons . empty ( ) )
reasons + = " , " ;
reasons + = reason ;
}
}
2014-03-03 22:07:58 +00:00
namespace cryptonote
{
//-----------------------------------------------------------------------------------
void core_rpc_server : : init_options ( boost : : program_options : : options_description & desc )
{
command_line : : add_arg ( desc , arg_rpc_bind_port ) ;
2014-09-08 17:07:15 +00:00
command_line : : add_arg ( desc , arg_testnet_rpc_bind_port ) ;
2015-11-27 18:24:29 +00:00
command_line : : add_arg ( desc , arg_restricted_rpc ) ;
2017-02-05 22:48:03 +00:00
cryptonote : : rpc_args : : init_options ( desc ) ;
2014-03-03 22:07:58 +00:00
}
//------------------------------------------------------------------------------------------------------------------------------
2014-09-09 14:58:53 +00:00
core_rpc_server : : core_rpc_server (
core & cr
, nodetool : : node_server < cryptonote : : t_cryptonote_protocol_handler < cryptonote : : core > > & p2p
)
: m_core ( cr )
, m_p2p ( p2p )
2014-03-03 22:07:58 +00:00
{ }
//------------------------------------------------------------------------------------------------------------------------------
2017-02-05 22:48:03 +00:00
bool core_rpc_server : : init (
2014-09-08 17:07:15 +00:00
const boost : : program_options : : variables_map & vm
)
2014-03-03 22:07:58 +00:00
{
2017-02-05 22:48:03 +00:00
m_testnet = command_line : : get_arg ( vm , command_line : : arg_testnet_on ) ;
m_net_server . set_threads_prefix ( " RPC " ) ;
2014-09-09 14:58:53 +00:00
auto p2p_bind_arg = m_testnet ? arg_testnet_rpc_bind_port : arg_rpc_bind_port ;
2014-09-08 17:07:15 +00:00
2017-02-05 22:48:03 +00:00
auto rpc_config = cryptonote : : rpc_args : : process ( vm ) ;
if ( ! rpc_config )
return false ;
2015-11-27 18:24:29 +00:00
m_restricted = command_line : : get_arg ( vm , arg_restricted_rpc ) ;
2015-01-29 22:10:53 +00:00
2017-02-05 22:48:03 +00:00
boost : : optional < epee : : net_utils : : http : : login > http_login { } ;
std : : string port = command_line : : get_arg ( vm , p2p_bind_arg ) ;
if ( rpc_config - > login )
http_login . emplace ( std : : move ( rpc_config - > login - > username ) , std : : move ( rpc_config - > login - > password ) . password ( ) ) ;
return epee : : http_server_impl_base < core_rpc_server , connection_context > : : init (
2017-10-24 13:26:05 +00:00
std : : move ( port ) , std : : move ( rpc_config - > bind_ip ) , std : : move ( rpc_config - > access_control_origins ) , std : : move ( http_login )
2017-02-05 22:48:03 +00:00
) ;
2014-03-03 22:07:58 +00:00
}
//------------------------------------------------------------------------------------------------------------------------------
2014-06-01 21:53:44 +00:00
bool core_rpc_server : : check_core_busy ( )
2014-03-03 22:07:58 +00:00
{
2014-06-01 21:53:44 +00:00
if ( m_p2p . get_payload_object ( ) . get_core ( ) . get_blockchain_storage ( ) . is_storing_blockchain ( ) )
2014-03-20 11:46:11 +00:00
{
return false ;
}
2014-06-01 21:53:44 +00:00
return true ;
}
2014-10-06 13:18:16 +00:00
# define CHECK_CORE_BUSY() do { if(!check_core_busy()){res.status = CORE_RPC_STATUS_BUSY;return true;} } while(0)
2014-06-01 21:53:44 +00:00
//------------------------------------------------------------------------------------------------------------------------------
bool core_rpc_server : : check_core_ready ( )
{
if ( ! m_p2p . get_payload_object ( ) . is_synchronized ( ) )
2014-03-20 11:46:11 +00:00
{
return false ;
}
2014-06-01 21:53:44 +00:00
return check_core_busy ( ) ;
2014-03-20 11:46:11 +00:00
}
2014-10-06 13:18:16 +00:00
# define CHECK_CORE_READY() do { if(!check_core_ready()){res.status = CORE_RPC_STATUS_BUSY;return true;} } while(0)
2014-03-20 11:46:11 +00:00
//------------------------------------------------------------------------------------------------------------------------------
2015-03-27 12:01:30 +00:00
bool core_rpc_server : : on_get_height ( const COMMAND_RPC_GET_HEIGHT : : request & req , COMMAND_RPC_GET_HEIGHT : : response & res )
2014-03-20 11:46:11 +00:00
{
2014-06-01 21:53:44 +00:00
CHECK_CORE_BUSY ( ) ;
2014-03-03 22:07:58 +00:00
res . height = m_core . get_current_blockchain_height ( ) ;
2014-03-20 11:46:11 +00:00
res . status = CORE_RPC_STATUS_OK ;
2014-03-03 22:07:58 +00:00
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2015-03-27 12:01:30 +00:00
bool core_rpc_server : : on_get_info ( const COMMAND_RPC_GET_INFO : : request & req , COMMAND_RPC_GET_INFO : : response & res )
2014-03-03 22:07:58 +00:00
{
2014-06-01 21:53:44 +00:00
CHECK_CORE_BUSY ( ) ;
2015-12-18 19:56:17 +00:00
crypto : : hash top_hash ;
2017-09-09 11:06:24 +00:00
m_core . get_blockchain_top ( res . height , top_hash ) ;
2015-12-18 19:56:17 +00:00
+ + res . height ; // turn top block height into blockchain height
res . top_block_hash = string_tools : : pod_to_hex ( top_hash ) ;
2014-06-04 20:50:13 +00:00
res . target_height = m_core . get_target_blockchain_height ( ) ;
2014-03-03 22:07:58 +00:00
res . difficulty = m_core . get_blockchain_storage ( ) . get_difficulty_for_next_block ( ) ;
2017-04-06 19:01:07 +00:00
res . target = m_core . get_blockchain_storage ( ) . get_difficulty_target ( ) ;
2014-03-03 22:07:58 +00:00
res . tx_count = m_core . get_blockchain_storage ( ) . get_total_transactions ( ) - res . height ; //without coinbase
res . tx_pool_size = m_core . get_pool_transactions_count ( ) ;
res . alt_blocks_count = m_core . get_blockchain_storage ( ) . get_alternative_blocks_count ( ) ;
uint64_t total_conn = m_p2p . get_connections_count ( ) ;
res . outgoing_connections_count = m_p2p . get_outgoing_connections_count ( ) ;
res . incoming_connections_count = total_conn - res . outgoing_connections_count ;
res . white_peerlist_size = m_p2p . get_peerlist_manager ( ) . get_white_peers_count ( ) ;
res . grey_peerlist_size = m_p2p . get_peerlist_manager ( ) . get_gray_peers_count ( ) ;
2015-10-28 18:08:14 +00:00
res . testnet = m_testnet ;
2016-10-15 14:00:21 +00:00
res . cumulative_difficulty = m_core . get_blockchain_storage ( ) . get_db ( ) . get_block_cumulative_difficulty ( res . height - 1 ) ;
2017-01-06 19:26:47 +00:00
res . block_size_limit = m_core . get_blockchain_storage ( ) . get_current_cumulative_blocksize_limit ( ) ;
2014-03-20 11:46:11 +00:00
res . status = CORE_RPC_STATUS_OK ;
2017-01-08 23:50:29 +00:00
res . start_time = ( uint64_t ) m_core . get_start_time ( ) ;
2014-03-03 22:07:58 +00:00
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2017-02-27 20:26:17 +00:00
static cryptonote : : blobdata get_pruned_tx_blob ( const cryptonote : : blobdata & blobdata )
{
cryptonote : : transaction tx ;
if ( ! cryptonote : : parse_and_validate_tx_from_blob ( blobdata , tx ) )
{
MERROR ( " Failed to parse and validate tx from blob " ) ;
return blobdata ;
}
std : : stringstream ss ;
binary_archive < true > ba ( ss ) ;
bool r = tx . serialize_base ( ba ) ;
CHECK_AND_ASSERT_MES ( r , blobdata , " Failed to serialize rct signatures base " ) ;
return ss . str ( ) ;
}
//------------------------------------------------------------------------------------------------------------------------------
2015-03-27 12:01:30 +00:00
bool core_rpc_server : : on_get_blocks ( const COMMAND_RPC_GET_BLOCKS_FAST : : request & req , COMMAND_RPC_GET_BLOCKS_FAST : : response & res )
2014-03-03 22:07:58 +00:00
{
2014-06-01 21:53:44 +00:00
CHECK_CORE_BUSY ( ) ;
2017-01-15 16:05:55 +00:00
std : : list < std : : pair < cryptonote : : blobdata , std : : list < cryptonote : : blobdata > > > bs ;
2014-08-01 08:17:50 +00:00
if ( ! m_core . find_blockchain_supplement ( req . start_height , req . block_ids , bs , res . current_height , res . start_height , COMMAND_RPC_GET_BLOCKS_FAST_MAX_COUNT ) )
2014-03-03 22:07:58 +00:00
{
res . status = " Failed " ;
return false ;
}
2017-02-27 20:26:17 +00:00
size_t pruned_size = 0 , unpruned_size = 0 , ntxes = 0 ;
2017-01-15 16:05:55 +00:00
for ( auto & bd : bs )
2014-03-03 22:07:58 +00:00
{
res . blocks . resize ( res . blocks . size ( ) + 1 ) ;
2017-01-15 16:05:55 +00:00
res . blocks . back ( ) . block = bd . first ;
2017-02-27 20:26:17 +00:00
pruned_size + = bd . first . size ( ) ;
unpruned_size + = bd . first . size ( ) ;
2016-07-13 18:26:11 +00:00
res . output_indices . push_back ( COMMAND_RPC_GET_BLOCKS_FAST : : block_output_indices ( ) ) ;
res . output_indices . back ( ) . indices . push_back ( COMMAND_RPC_GET_BLOCKS_FAST : : tx_output_indices ( ) ) ;
2017-01-15 16:05:55 +00:00
block b ;
if ( ! parse_and_validate_block_from_blob ( bd . first , b ) )
{
res . status = " Invalid block " ;
return false ;
}
bool r = m_core . get_tx_outputs_gindexs ( get_transaction_hash ( b . miner_tx ) , res . output_indices . back ( ) . indices . back ( ) . indices ) ;
2016-07-13 18:26:11 +00:00
if ( ! r )
{
res . status = " Failed " ;
return false ;
}
size_t txidx = 0 ;
2017-02-27 20:26:17 +00:00
ntxes + = bd . second . size ( ) ;
2017-08-25 17:59:29 +00:00
for ( std : : list < cryptonote : : blobdata > : : iterator i = bd . second . begin ( ) ; i ! = bd . second . end ( ) ; + + i )
2014-03-03 22:07:58 +00:00
{
2017-08-25 17:59:29 +00:00
unpruned_size + = i - > size ( ) ;
2017-02-27 20:26:17 +00:00
if ( req . prune )
2017-08-25 17:59:29 +00:00
res . blocks . back ( ) . txs . push_back ( get_pruned_tx_blob ( std : : move ( * i ) ) ) ;
2017-02-27 20:26:17 +00:00
else
2017-08-25 17:59:29 +00:00
res . blocks . back ( ) . txs . push_back ( std : : move ( * i ) ) ;
i - > clear ( ) ;
i - > shrink_to_fit ( ) ;
2017-02-27 20:26:17 +00:00
pruned_size + = res . blocks . back ( ) . txs . back ( ) . size ( ) ;
2017-08-25 17:59:29 +00:00
2016-07-13 18:26:11 +00:00
res . output_indices . back ( ) . indices . push_back ( COMMAND_RPC_GET_BLOCKS_FAST : : tx_output_indices ( ) ) ;
2017-01-15 16:05:55 +00:00
bool r = m_core . get_tx_outputs_gindexs ( b . tx_hashes [ txidx + + ] , res . output_indices . back ( ) . indices . back ( ) . indices ) ;
2016-07-13 18:26:11 +00:00
if ( ! r )
{
res . status = " Failed " ;
return false ;
}
2014-03-03 22:07:58 +00:00
}
}
2017-02-27 20:26:17 +00:00
MDEBUG ( " on_get_blocks: " < < bs . size ( ) < < " blocks, " < < ntxes < < " txes, pruned size " < < pruned_size < < " , unpruned size " < < unpruned_size ) ;
2014-03-03 22:07:58 +00:00
res . status = CORE_RPC_STATUS_OK ;
return true ;
2017-07-04 04:32:44 +00:00
}
bool core_rpc_server : : on_get_alt_blocks_hashes ( const COMMAND_RPC_GET_ALT_BLOCKS_HASHES : : request & req , COMMAND_RPC_GET_ALT_BLOCKS_HASHES : : response & res )
{
CHECK_CORE_BUSY ( ) ;
std : : list < block > blks ;
if ( ! m_core . get_alternative_blocks ( blks ) )
{
res . status = " Failed " ;
return false ;
}
res . blks_hashes . reserve ( blks . size ( ) ) ;
for ( auto const & blk : blks )
{
res . blks_hashes . push_back ( epee : : string_tools : : pod_to_hex ( get_block_hash ( blk ) ) ) ;
}
MDEBUG ( " on_get_alt_blocks_hashes: " < < blks . size ( ) < < " blocks " ) ;
res . status = CORE_RPC_STATUS_OK ;
return true ;
2014-03-03 22:07:58 +00:00
}
//------------------------------------------------------------------------------------------------------------------------------
2016-12-25 08:18:15 +00:00
bool core_rpc_server : : on_get_blocks_by_height ( const COMMAND_RPC_GET_BLOCKS_BY_HEIGHT : : request & req , COMMAND_RPC_GET_BLOCKS_BY_HEIGHT : : response & res )
{
CHECK_CORE_BUSY ( ) ;
res . status = " Failed " ;
res . blocks . clear ( ) ;
res . blocks . reserve ( req . heights . size ( ) ) ;
for ( uint64_t height : req . heights )
{
block blk ;
try
{
blk = m_core . get_blockchain_storage ( ) . get_db ( ) . get_block_from_height ( height ) ;
}
catch ( . . . )
{
2017-01-26 18:11:37 +00:00
res . status = " Error retrieving block at height " + std : : to_string ( height ) ;
2016-12-25 08:18:15 +00:00
return true ;
}
std : : list < transaction > txs ;
std : : list < crypto : : hash > missed_txs ;
m_core . get_transactions ( blk . tx_hashes , txs , missed_txs ) ;
res . blocks . resize ( res . blocks . size ( ) + 1 ) ;
res . blocks . back ( ) . block = block_to_blob ( blk ) ;
for ( auto & tx : txs )
res . blocks . back ( ) . txs . push_back ( tx_to_blob ( tx ) ) ;
}
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2016-04-13 22:45:02 +00:00
bool core_rpc_server : : on_get_hashes ( const COMMAND_RPC_GET_HASHES_FAST : : request & req , COMMAND_RPC_GET_HASHES_FAST : : response & res )
{
CHECK_CORE_BUSY ( ) ;
NOTIFY_RESPONSE_CHAIN_ENTRY : : request resp ;
resp . start_height = req . start_height ;
if ( ! m_core . find_blockchain_supplement ( req . block_ids , resp ) )
{
res . status = " Failed " ;
return false ;
}
res . current_height = resp . total_height ;
res . start_height = resp . start_height ;
res . m_block_ids = std : : move ( resp . m_block_ids ) ;
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2015-03-27 12:01:30 +00:00
bool core_rpc_server : : on_get_random_outs ( const COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS : : request & req , COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS : : response & res )
2014-03-03 22:07:58 +00:00
{
2014-06-01 21:53:44 +00:00
CHECK_CORE_BUSY ( ) ;
2014-03-03 22:07:58 +00:00
res . status = " Failed " ;
2016-06-29 19:43:14 +00:00
if ( m_restricted )
{
if ( req . amounts . size ( ) > 100 | | req . outs_count > MAX_RESTRICTED_FAKE_OUTS_COUNT )
{
res . status = " Too many outs requested " ;
return true ;
}
}
2014-03-03 22:07:58 +00:00
if ( ! m_core . get_random_outs_for_amounts ( req , res ) )
{
return true ;
}
res . status = CORE_RPC_STATUS_OK ;
std : : stringstream ss ;
typedef COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS : : outs_for_amount outs_for_amount ;
typedef COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS : : out_entry out_entry ;
std : : for_each ( res . outs . begin ( ) , res . outs . end ( ) , [ & ] ( outs_for_amount & ofa )
2014-03-20 11:46:11 +00:00
{
ss < < " [ " < < ofa . amount < < " ]: " ;
CHECK_AND_ASSERT_MES ( ofa . outs . size ( ) , ; , " internal error: ofa.outs.size() is empty for amount " < < ofa . amount ) ;
std : : for_each ( ofa . outs . begin ( ) , ofa . outs . end ( ) , [ & ] ( out_entry & oe )
{
ss < < oe . global_amount_index < < " " ;
} ) ;
ss < < ENDL ;
} ) ;
2014-03-03 22:07:58 +00:00
std : : string s = ss . str ( ) ;
LOG_PRINT_L2 ( " COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AMOUNTS: " < < ENDL < < s ) ;
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2016-11-22 20:00:40 +00:00
bool core_rpc_server : : on_get_outs_bin ( const COMMAND_RPC_GET_OUTPUTS_BIN : : request & req , COMMAND_RPC_GET_OUTPUTS_BIN : : response & res )
2016-08-02 20:48:09 +00:00
{
CHECK_CORE_BUSY ( ) ;
res . status = " Failed " ;
if ( m_restricted )
{
if ( req . outputs . size ( ) > MAX_RESTRICTED_GLOBAL_FAKE_OUTS_COUNT )
{
res . status = " Too many outs requested " ;
return true ;
}
}
if ( ! m_core . get_outs ( req , res ) )
{
return true ;
}
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2016-11-22 20:00:40 +00:00
bool core_rpc_server : : on_get_outs ( const COMMAND_RPC_GET_OUTPUTS : : request & req , COMMAND_RPC_GET_OUTPUTS : : response & res )
{
CHECK_CORE_BUSY ( ) ;
res . status = " Failed " ;
if ( m_restricted )
{
if ( req . outputs . size ( ) > MAX_RESTRICTED_GLOBAL_FAKE_OUTS_COUNT )
{
res . status = " Too many outs requested " ;
return true ;
}
}
cryptonote : : COMMAND_RPC_GET_OUTPUTS_BIN : : request req_bin ;
req_bin . outputs = req . outputs ;
cryptonote : : COMMAND_RPC_GET_OUTPUTS_BIN : : response res_bin ;
if ( ! m_core . get_outs ( req_bin , res_bin ) )
{
return true ;
}
// convert to text
for ( const auto & i : res_bin . outs )
{
res . outs . push_back ( cryptonote : : COMMAND_RPC_GET_OUTPUTS : : outkey ( ) ) ;
cryptonote : : COMMAND_RPC_GET_OUTPUTS : : outkey & outkey = res . outs . back ( ) ;
outkey . key = epee : : string_tools : : pod_to_hex ( i . key ) ;
outkey . mask = epee : : string_tools : : pod_to_hex ( i . mask ) ;
outkey . unlocked = i . unlocked ;
2016-12-23 12:04:54 +00:00
outkey . height = i . height ;
outkey . txid = epee : : string_tools : : pod_to_hex ( i . txid ) ;
2016-11-22 20:00:40 +00:00
}
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2016-06-05 09:46:18 +00:00
bool core_rpc_server : : on_get_random_rct_outs ( const COMMAND_RPC_GET_RANDOM_RCT_OUTPUTS : : request & req , COMMAND_RPC_GET_RANDOM_RCT_OUTPUTS : : response & res )
{
CHECK_CORE_BUSY ( ) ;
res . status = " Failed " ;
if ( ! m_core . get_random_rct_outs ( req , res ) )
{
return true ;
}
res . status = CORE_RPC_STATUS_OK ;
std : : stringstream ss ;
typedef COMMAND_RPC_GET_RANDOM_RCT_OUTPUTS : : out_entry out_entry ;
CHECK_AND_ASSERT_MES ( res . outs . size ( ) , true , " internal error: res.outs.size() is empty " ) ;
std : : for_each ( res . outs . begin ( ) , res . outs . end ( ) , [ & ] ( out_entry & oe )
{
ss < < oe . global_amount_index < < " " ;
} ) ;
ss < < ENDL ;
std : : string s = ss . str ( ) ;
LOG_PRINT_L2 ( " COMMAND_RPC_GET_RANDOM_RCT_OUTPUTS: " < < ENDL < < s ) ;
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2015-03-27 12:01:30 +00:00
bool core_rpc_server : : on_get_indexes ( const COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES : : request & req , COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES : : response & res )
2014-03-03 22:07:58 +00:00
{
2014-06-01 21:53:44 +00:00
CHECK_CORE_BUSY ( ) ;
2014-03-03 22:07:58 +00:00
bool r = m_core . get_tx_outputs_gindexs ( req . txid , res . o_indexes ) ;
if ( ! r )
{
res . status = " Failed " ;
return true ;
}
res . status = CORE_RPC_STATUS_OK ;
LOG_PRINT_L2 ( " COMMAND_RPC_GET_TX_GLOBAL_OUTPUTS_INDEXES: [ " < < res . o_indexes . size ( ) < < " ] " ) ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2015-03-27 12:01:30 +00:00
bool core_rpc_server : : on_get_transactions ( const COMMAND_RPC_GET_TRANSACTIONS : : request & req , COMMAND_RPC_GET_TRANSACTIONS : : response & res )
2014-03-03 22:07:58 +00:00
{
2014-06-01 21:53:44 +00:00
CHECK_CORE_BUSY ( ) ;
2014-03-03 22:07:58 +00:00
std : : vector < crypto : : hash > vh ;
2017-01-22 20:38:10 +00:00
for ( const auto & tx_hex_str : req . txs_hashes )
2014-03-03 22:07:58 +00:00
{
blobdata b ;
if ( ! string_tools : : parse_hexstr_to_binbuff ( tx_hex_str , b ) )
{
res . status = " Failed to parse hex representation of transaction hash " ;
return true ;
}
if ( b . size ( ) ! = sizeof ( crypto : : hash ) )
{
res . status = " Failed, size of data mismatch " ;
2016-01-30 13:28:12 +00:00
return true ;
2014-03-03 22:07:58 +00:00
}
vh . push_back ( * reinterpret_cast < const crypto : : hash * > ( b . data ( ) ) ) ;
}
std : : list < crypto : : hash > missed_txs ;
std : : list < transaction > txs ;
bool r = m_core . get_transactions ( vh , txs , missed_txs ) ;
if ( ! r )
{
res . status = " Failed " ;
return true ;
}
2015-08-11 14:53:55 +00:00
LOG_PRINT_L2 ( " Found " < < txs . size ( ) < < " / " < < vh . size ( ) < < " transactions on the blockchain " ) ;
// try the pool for any missing txes
size_t found_in_pool = 0 ;
2016-04-03 11:51:28 +00:00
std : : unordered_set < crypto : : hash > pool_tx_hashes ;
2017-09-22 12:57:20 +00:00
std : : unordered_map < crypto : : hash , bool > double_spend_seen ;
2015-08-11 14:53:55 +00:00
if ( ! missed_txs . empty ( ) )
{
2017-09-22 12:57:20 +00:00
std : : vector < tx_info > pool_tx_info ;
std : : vector < spent_key_image_info > pool_key_image_info ;
bool r = m_core . get_pool_transactions_and_spent_keys_info ( pool_tx_info , pool_key_image_info ) ;
2015-08-11 14:53:55 +00:00
if ( r )
{
2017-09-01 20:22:45 +00:00
// sort to match original request
std : : list < transaction > sorted_txs ;
2017-09-22 12:57:20 +00:00
std : : vector < tx_info > : : const_iterator i ;
2017-09-01 20:22:45 +00:00
for ( const crypto : : hash & h : vh )
2015-08-11 14:53:55 +00:00
{
2017-09-01 20:22:45 +00:00
if ( std : : find ( missed_txs . begin ( ) , missed_txs . end ( ) , h ) = = missed_txs . end ( ) )
2015-08-11 14:53:55 +00:00
{
2017-09-01 20:22:45 +00:00
// core returns the ones it finds in the right order
if ( get_transaction_hash ( txs . front ( ) ) ! = h )
{
res . status = " Failed: tx hash mismatch " ;
return true ;
}
sorted_txs . push_back ( std : : move ( txs . front ( ) ) ) ;
txs . pop_front ( ) ;
}
2017-09-22 12:57:20 +00:00
else if ( ( i = std : : find_if ( pool_tx_info . begin ( ) , pool_tx_info . end ( ) , [ h ] ( const tx_info & txi ) { return epee : : string_tools : : pod_to_hex ( h ) = = txi . id_hash ; } ) ) ! = pool_tx_info . end ( ) )
2017-09-01 20:22:45 +00:00
{
2017-09-22 12:57:20 +00:00
cryptonote : : transaction tx ;
if ( ! cryptonote : : parse_and_validate_tx_from_blob ( i - > tx_blob , tx ) )
{
res . status = " Failed to parse and validate tx from blob " ;
return true ;
}
sorted_txs . push_back ( tx ) ;
2017-09-01 20:22:45 +00:00
missed_txs . remove ( h ) ;
2017-09-29 15:26:57 +00:00
pool_tx_hashes . insert ( h ) ;
2017-09-22 12:57:20 +00:00
const std : : string hash_string = epee : : string_tools : : pod_to_hex ( h ) ;
for ( const auto & ti : pool_tx_info )
{
if ( ti . id_hash = = hash_string )
{
double_spend_seen . insert ( std : : make_pair ( h , ti . double_spend_seen ) ) ;
break ;
}
}
2015-08-11 14:53:55 +00:00
+ + found_in_pool ;
}
}
2017-09-01 20:22:45 +00:00
txs = sorted_txs ;
2015-08-11 14:53:55 +00:00
}
LOG_PRINT_L2 ( " Found " < < found_in_pool < < " / " < < vh . size ( ) < < " transactions in the pool " ) ;
}
2014-03-03 22:07:58 +00:00
2016-04-03 11:51:28 +00:00
std : : list < std : : string > : : const_iterator txhi = req . txs_hashes . begin ( ) ;
std : : vector < crypto : : hash > : : const_iterator vhi = vh . begin ( ) ;
2017-01-22 20:38:10 +00:00
for ( auto & tx : txs )
2014-03-03 22:07:58 +00:00
{
2016-04-03 11:51:28 +00:00
res . txs . push_back ( COMMAND_RPC_GET_TRANSACTIONS : : entry ( ) ) ;
COMMAND_RPC_GET_TRANSACTIONS : : entry & e = res . txs . back ( ) ;
crypto : : hash tx_hash = * vhi + + ;
e . tx_hash = * txhi + + ;
2014-03-03 22:07:58 +00:00
blobdata blob = t_serializable_object_to_blob ( tx ) ;
2016-04-03 11:51:28 +00:00
e . as_hex = string_tools : : buff_to_hex_nodelimer ( blob ) ;
if ( req . decode_as_json )
e . as_json = obj_to_json_str ( tx ) ;
e . in_pool = pool_tx_hashes . find ( tx_hash ) ! = pool_tx_hashes . end ( ) ;
if ( e . in_pool )
{
2017-08-30 02:30:31 +00:00
e . block_height = e . block_timestamp = std : : numeric_limits < uint64_t > : : max ( ) ;
2017-09-22 12:57:20 +00:00
if ( double_spend_seen . find ( tx_hash ) ! = double_spend_seen . end ( ) )
{
e . double_spend_seen = double_spend_seen [ tx_hash ] ;
}
else
{
MERROR ( " Failed to determine double spend status for " < < tx_hash ) ;
e . double_spend_seen = false ;
}
2016-04-03 11:51:28 +00:00
}
else
{
e . block_height = m_core . get_blockchain_storage ( ) . get_db ( ) . get_tx_block_height ( tx_hash ) ;
2017-08-30 02:30:31 +00:00
e . block_timestamp = m_core . get_blockchain_storage ( ) . get_db ( ) . get_block_timestamp ( e . block_height ) ;
2017-09-22 12:57:20 +00:00
e . double_spend_seen = false ;
2016-04-03 11:51:28 +00:00
}
// fill up old style responses too, in case an old wallet asks
res . txs_as_hex . push_back ( e . as_hex ) ;
2015-10-13 21:11:52 +00:00
if ( req . decode_as_json )
2016-04-03 11:51:28 +00:00
res . txs_as_json . push_back ( e . as_json ) ;
2016-11-20 14:12:19 +00:00
2016-11-23 18:55:32 +00:00
// output indices too if not in pool
if ( pool_tx_hashes . find ( tx_hash ) = = pool_tx_hashes . end ( ) )
2016-11-20 14:12:19 +00:00
{
2016-11-23 18:55:32 +00:00
bool r = m_core . get_tx_outputs_gindexs ( tx_hash , e . output_indices ) ;
if ( ! r )
{
res . status = " Failed " ;
return false ;
}
2016-11-20 14:12:19 +00:00
}
2014-03-03 22:07:58 +00:00
}
2017-01-22 20:38:10 +00:00
for ( const auto & miss_tx : missed_txs )
2014-03-03 22:07:58 +00:00
{
res . missed_tx . push_back ( string_tools : : pod_to_hex ( miss_tx ) ) ;
}
2016-04-03 11:51:28 +00:00
LOG_PRINT_L2 ( res . txs . size ( ) < < " transactions found, " < < res . missed_tx . size ( ) < < " not found " ) ;
2014-03-03 22:07:58 +00:00
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2017-11-08 12:06:41 +00:00
bool core_rpc_server : : on_is_key_image_spent ( const COMMAND_RPC_IS_KEY_IMAGE_SPENT : : request & req , COMMAND_RPC_IS_KEY_IMAGE_SPENT : : response & res , bool request_has_rpc_origin )
2015-08-11 09:49:15 +00:00
{
CHECK_CORE_BUSY ( ) ;
std : : vector < crypto : : key_image > key_images ;
2017-01-22 20:38:10 +00:00
for ( const auto & ki_hex_str : req . key_images )
2015-08-11 09:49:15 +00:00
{
blobdata b ;
if ( ! string_tools : : parse_hexstr_to_binbuff ( ki_hex_str , b ) )
{
res . status = " Failed to parse hex representation of key image " ;
return true ;
}
if ( b . size ( ) ! = sizeof ( crypto : : key_image ) )
{
res . status = " Failed, size of data mismatch " ;
}
key_images . push_back ( * reinterpret_cast < const crypto : : key_image * > ( b . data ( ) ) ) ;
}
2015-08-13 15:33:28 +00:00
std : : vector < bool > spent_status ;
bool r = m_core . are_key_images_spent ( key_images , spent_status ) ;
2015-08-11 09:49:15 +00:00
if ( ! r )
{
res . status = " Failed " ;
return true ;
}
2015-08-13 15:33:28 +00:00
res . spent_status . clear ( ) ;
for ( size_t n = 0 ; n < spent_status . size ( ) ; + + n )
2016-01-05 21:57:43 +00:00
res . spent_status . push_back ( spent_status [ n ] ? COMMAND_RPC_IS_KEY_IMAGE_SPENT : : SPENT_IN_BLOCKCHAIN : COMMAND_RPC_IS_KEY_IMAGE_SPENT : : UNSPENT ) ;
// check the pool too
std : : vector < cryptonote : : tx_info > txs ;
std : : vector < cryptonote : : spent_key_image_info > ki ;
2017-11-08 12:06:41 +00:00
r = m_core . get_pool_transactions_and_spent_keys_info ( txs , ki , ! request_has_rpc_origin | | ! m_restricted ) ;
2016-01-05 21:57:43 +00:00
if ( ! r )
{
res . status = " Failed " ;
return true ;
}
for ( std : : vector < cryptonote : : spent_key_image_info > : : const_iterator i = ki . begin ( ) ; i ! = ki . end ( ) ; + + i )
{
crypto : : hash hash ;
crypto : : key_image spent_key_image ;
if ( parse_hash256 ( i - > id_hash , hash ) )
{
memcpy ( & spent_key_image , & hash , sizeof ( hash ) ) ; // a bit dodgy, should be other parse functions somewhere
for ( size_t n = 0 ; n < res . spent_status . size ( ) ; + + n )
{
if ( res . spent_status [ n ] = = COMMAND_RPC_IS_KEY_IMAGE_SPENT : : UNSPENT )
{
if ( key_images [ n ] = = spent_key_image )
{
res . spent_status [ n ] = COMMAND_RPC_IS_KEY_IMAGE_SPENT : : SPENT_IN_POOL ;
break ;
}
}
}
}
}
2015-08-11 09:49:15 +00:00
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2015-03-27 12:01:30 +00:00
bool core_rpc_server : : on_send_raw_tx ( const COMMAND_RPC_SEND_RAW_TX : : request & req , COMMAND_RPC_SEND_RAW_TX : : response & res )
2014-03-03 22:07:58 +00:00
{
2014-04-02 16:00:17 +00:00
CHECK_CORE_READY ( ) ;
2014-03-03 22:07:58 +00:00
std : : string tx_blob ;
if ( ! string_tools : : parse_hexstr_to_binbuff ( req . tx_as_hex , tx_blob ) )
{
LOG_PRINT_L0 ( " [on_send_raw_tx]: Failed to parse tx from hexbuff: " < < req . tx_as_hex ) ;
res . status = " Failed " ;
return true ;
}
cryptonote_connection_context fake_context = AUTO_VAL_INIT ( fake_context ) ;
tx_verification_context tvc = AUTO_VAL_INIT ( tvc ) ;
2017-01-14 13:01:21 +00:00
if ( ! m_core . handle_incoming_tx ( tx_blob , tvc , false , false , req . do_not_relay ) | | tvc . m_verifivation_failed )
2014-03-03 22:07:58 +00:00
{
res . status = " Failed " ;
2017-09-16 09:31:49 +00:00
res . reason = " " ;
2016-03-27 11:35:36 +00:00
if ( ( res . low_mixin = tvc . m_low_mixin ) )
2017-09-16 09:31:49 +00:00
add_reason ( res . reason , " ring size too small " ) ;
2016-03-27 11:35:36 +00:00
if ( ( res . double_spend = tvc . m_double_spend ) )
2017-09-16 09:31:49 +00:00
add_reason ( res . reason , " double spend " ) ;
2016-03-27 11:35:36 +00:00
if ( ( res . invalid_input = tvc . m_invalid_input ) )
2017-09-16 09:31:49 +00:00
add_reason ( res . reason , " invalid input " ) ;
2016-03-27 11:35:36 +00:00
if ( ( res . invalid_output = tvc . m_invalid_output ) )
2017-09-16 09:31:49 +00:00
add_reason ( res . reason , " invalid output " ) ;
2016-03-27 11:35:36 +00:00
if ( ( res . too_big = tvc . m_too_big ) )
2017-09-16 09:31:49 +00:00
add_reason ( res . reason , " too big " ) ;
2016-03-27 11:35:36 +00:00
if ( ( res . overspend = tvc . m_overspend ) )
2017-09-16 09:31:49 +00:00
add_reason ( res . reason , " overspend " ) ;
2016-03-27 11:35:36 +00:00
if ( ( res . fee_too_low = tvc . m_fee_too_low ) )
2017-09-16 09:31:49 +00:00
add_reason ( res . reason , " fee too low " ) ;
2016-07-02 09:02:12 +00:00
if ( ( res . not_rct = tvc . m_not_rct ) )
2017-09-16 09:31:49 +00:00
add_reason ( res . reason , " tx is not ringct " ) ;
const std : : string punctuation = res . reason . empty ( ) ? " " : " : " ;
if ( tvc . m_verifivation_failed )
{
LOG_PRINT_L0 ( " [on_send_raw_tx]: tx verification failed " < < punctuation < < res . reason ) ;
}
else
{
LOG_PRINT_L0 ( " [on_send_raw_tx]: Failed to process tx " < < punctuation < < res . reason ) ;
}
2014-03-03 22:07:58 +00:00
return true ;
}
2017-01-14 13:01:21 +00:00
if ( ! tvc . m_should_be_relayed )
2014-03-03 22:07:58 +00:00
{
LOG_PRINT_L0 ( " [on_send_raw_tx]: tx accepted, but not relayed " ) ;
2016-03-27 11:35:36 +00:00
res . reason = " Not relayed " ;
res . not_relayed = true ;
res . status = CORE_RPC_STATUS_OK ;
2014-03-03 22:07:58 +00:00
return true ;
}
NOTIFY_NEW_TRANSACTIONS : : request r ;
r . txs . push_back ( tx_blob ) ;
m_core . get_protocol ( ) - > relay_transactions ( r , fake_context ) ;
//TODO: make sure that tx has reached other nodes here, probably wait to receive reflections from other nodes
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2015-03-27 12:01:30 +00:00
bool core_rpc_server : : on_start_mining ( const COMMAND_RPC_START_MINING : : request & req , COMMAND_RPC_START_MINING : : response & res )
2014-03-03 22:07:58 +00:00
{
2014-03-20 11:46:11 +00:00
CHECK_CORE_READY ( ) ;
2017-02-19 02:42:10 +00:00
cryptonote : : address_parse_info info ;
if ( ! get_account_address_from_str ( info , m_testnet , req . miner_address ) )
2014-03-03 22:07:58 +00:00
{
res . status = " Failed, wrong address " ;
2015-05-28 22:13:32 +00:00
LOG_PRINT_L0 ( res . status ) ;
2014-03-03 22:07:58 +00:00
return true ;
}
2017-02-19 02:42:10 +00:00
if ( info . is_subaddress )
{
res . status = " Mining to subaddress isn't supported yet " ;
LOG_PRINT_L0 ( res . status ) ;
return true ;
}
2014-03-03 22:07:58 +00:00
2017-01-26 19:31:56 +00:00
unsigned int concurrency_count = boost : : thread : : hardware_concurrency ( ) * 4 ;
// if we couldn't detect threads, set it to a ridiculously high number
if ( concurrency_count = = 0 )
{
concurrency_count = 257 ;
}
// if there are more threads requested than the hardware supports
// then we fail and log that.
if ( req . threads_count > concurrency_count )
{
res . status = " Failed, too many threads relative to CPU cores. " ;
LOG_PRINT_L0 ( res . status ) ;
return true ;
}
2014-04-30 20:50:06 +00:00
boost : : thread : : attributes attrs ;
attrs . set_stack_size ( THREAD_STACK_SIZE ) ;
2017-02-19 02:42:10 +00:00
if ( ! m_core . get_miner ( ) . start ( info . address , static_cast < size_t > ( req . threads_count ) , attrs , req . do_background_mining , req . ignore_battery ) )
2014-03-03 22:07:58 +00:00
{
res . status = " Failed, mining not started " ;
2015-05-28 22:13:32 +00:00
LOG_PRINT_L0 ( res . status ) ;
2014-03-03 22:07:58 +00:00
return true ;
}
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2015-03-27 12:01:30 +00:00
bool core_rpc_server : : on_stop_mining ( const COMMAND_RPC_STOP_MINING : : request & req , COMMAND_RPC_STOP_MINING : : response & res )
2014-03-03 22:07:58 +00:00
{
if ( ! m_core . get_miner ( ) . stop ( ) )
{
res . status = " Failed, mining not stopped " ;
2015-05-28 22:13:32 +00:00
LOG_PRINT_L0 ( res . status ) ;
2014-03-03 22:07:58 +00:00
return true ;
}
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2015-03-27 12:01:30 +00:00
bool core_rpc_server : : on_mining_status ( const COMMAND_RPC_MINING_STATUS : : request & req , COMMAND_RPC_MINING_STATUS : : response & res )
2014-05-25 19:36:12 +00:00
{
2017-02-23 20:40:28 +00:00
CHECK_CORE_BUSY ( ) ;
2014-05-25 19:36:12 +00:00
const miner & lMiner = m_core . get_miner ( ) ;
res . active = lMiner . is_mining ( ) ;
2017-02-08 21:17:50 +00:00
res . is_background_mining_enabled = lMiner . get_is_background_mining_enabled ( ) ;
2016-10-10 20:41:24 +00:00
2014-05-25 19:36:12 +00:00
if ( lMiner . is_mining ( ) ) {
res . speed = lMiner . get_speed ( ) ;
res . threads_count = lMiner . get_threads_count ( ) ;
const account_public_address & lMiningAdr = lMiner . get_mining_address ( ) ;
2017-02-19 02:42:10 +00:00
res . address = get_account_address_as_str ( m_testnet , false , lMiningAdr ) ;
2014-05-25 19:36:12 +00:00
}
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2015-03-27 12:01:30 +00:00
bool core_rpc_server : : on_save_bc ( const COMMAND_RPC_SAVE_BC : : request & req , COMMAND_RPC_SAVE_BC : : response & res )
2014-05-15 22:21:43 +00:00
{
2014-06-01 21:53:44 +00:00
CHECK_CORE_BUSY ( ) ;
2014-05-15 22:21:43 +00:00
if ( ! m_core . get_blockchain_storage ( ) . store_blockchain ( ) )
{
res . status = " Error while storing blockhain " ;
return true ;
}
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2015-03-27 12:01:30 +00:00
bool core_rpc_server : : on_get_peer_list ( const COMMAND_RPC_GET_PEER_LIST : : request & req , COMMAND_RPC_GET_PEER_LIST : : response & res )
2015-02-05 09:11:20 +00:00
{
std : : list < nodetool : : peerlist_entry > white_list ;
std : : list < nodetool : : peerlist_entry > gray_list ;
2016-07-26 19:02:43 +00:00
m_p2p . get_peerlist_manager ( ) . get_peerlist_full ( gray_list , white_list ) ;
2015-05-28 13:07:31 +00:00
2015-02-05 09:11:20 +00:00
for ( auto & entry : white_list )
{
2017-06-28 08:00:29 +00:00
if ( entry . adr . get_type_id ( ) = = epee : : net_utils : : ipv4_network_address : : ID )
2017-05-27 10:35:54 +00:00
res . white_list . emplace_back ( entry . id , entry . adr . as < epee : : net_utils : : ipv4_network_address > ( ) . ip ( ) ,
entry . adr . as < epee : : net_utils : : ipv4_network_address > ( ) . port ( ) , entry . last_seen ) ;
else
res . white_list . emplace_back ( entry . id , entry . adr . str ( ) , entry . last_seen ) ;
2015-02-05 09:11:20 +00:00
}
for ( auto & entry : gray_list )
{
2017-06-28 08:00:29 +00:00
if ( entry . adr . get_type_id ( ) = = epee : : net_utils : : ipv4_network_address : : ID )
2017-05-27 10:35:54 +00:00
res . gray_list . emplace_back ( entry . id , entry . adr . as < epee : : net_utils : : ipv4_network_address > ( ) . ip ( ) ,
entry . adr . as < epee : : net_utils : : ipv4_network_address > ( ) . port ( ) , entry . last_seen ) ;
else
res . gray_list . emplace_back ( entry . id , entry . adr . str ( ) , entry . last_seen ) ;
2015-02-05 09:11:20 +00:00
}
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2015-03-27 12:01:30 +00:00
bool core_rpc_server : : on_set_log_hash_rate ( const COMMAND_RPC_SET_LOG_HASH_RATE : : request & req , COMMAND_RPC_SET_LOG_HASH_RATE : : response & res )
2015-02-05 09:11:20 +00:00
{
if ( m_core . get_miner ( ) . is_mining ( ) )
{
m_core . get_miner ( ) . do_print_hashrate ( req . visible ) ;
res . status = CORE_RPC_STATUS_OK ;
}
else
{
res . status = CORE_RPC_STATUS_NOT_MINING ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2015-03-27 12:01:30 +00:00
bool core_rpc_server : : on_set_log_level ( const COMMAND_RPC_SET_LOG_LEVEL : : request & req , COMMAND_RPC_SET_LOG_LEVEL : : response & res )
2015-02-05 09:11:20 +00:00
{
Change logging to easylogging++
This replaces the epee and data_loggers logging systems with
a single one, and also adds filename:line and explicit severity
levels. Categories may be defined, and logging severity set
by category (or set of categories). epee style 0-4 log level
maps to a sensible severity configuration. Log files now also
rotate when reaching 100 MB.
To select which logs to output, use the MONERO_LOGS environment
variable, with a comma separated list of categories (globs are
supported), with their requested severity level after a colon.
If a log matches more than one such setting, the last one in
the configuration string applies. A few examples:
This one is (mostly) silent, only outputting fatal errors:
MONERO_LOGS=*:FATAL
This one is very verbose:
MONERO_LOGS=*:TRACE
This one is totally silent (logwise):
MONERO_LOGS=""
This one outputs all errors and warnings, except for the
"verify" category, which prints just fatal errors (the verify
category is used for logs about incoming transactions and
blocks, and it is expected that some/many will fail to verify,
hence we don't want the spam):
MONERO_LOGS=*:WARNING,verify:FATAL
Log levels are, in decreasing order of priority:
FATAL, ERROR, WARNING, INFO, DEBUG, TRACE
Subcategories may be added using prefixes and globs. This
example will output net.p2p logs at the TRACE level, but all
other net* logs only at INFO:
MONERO_LOGS=*:ERROR,net*:INFO,net.p2p:TRACE
Logs which are intended for the user (which Monero was using
a lot through epee, but really isn't a nice way to go things)
should use the "global" category. There are a few helper macros
for using this category, eg: MGINFO("this shows up by default")
or MGINFO_RED("this is red"), to try to keep a similar look
and feel for now.
Existing epee log macros still exist, and map to the new log
levels, but since they're used as a "user facing" UI element
as much as a logging system, they often don't map well to log
severities (ie, a log level 0 log may be an error, or may be
something we want the user to see, such as an important info).
In those cases, I tried to use the new macros. In other cases,
I left the existing macros in. When modifying logs, it is
probably best to switch to the new macros with explicit levels.
The --log-level options and set_log commands now also accept
category settings, in addition to the epee style log levels.
2017-01-01 16:34:23 +00:00
if ( req . level < 0 | | req . level > 4 )
2015-02-05 09:11:20 +00:00
{
res . status = " Error: log level not valid " ;
Change logging to easylogging++
This replaces the epee and data_loggers logging systems with
a single one, and also adds filename:line and explicit severity
levels. Categories may be defined, and logging severity set
by category (or set of categories). epee style 0-4 log level
maps to a sensible severity configuration. Log files now also
rotate when reaching 100 MB.
To select which logs to output, use the MONERO_LOGS environment
variable, with a comma separated list of categories (globs are
supported), with their requested severity level after a colon.
If a log matches more than one such setting, the last one in
the configuration string applies. A few examples:
This one is (mostly) silent, only outputting fatal errors:
MONERO_LOGS=*:FATAL
This one is very verbose:
MONERO_LOGS=*:TRACE
This one is totally silent (logwise):
MONERO_LOGS=""
This one outputs all errors and warnings, except for the
"verify" category, which prints just fatal errors (the verify
category is used for logs about incoming transactions and
blocks, and it is expected that some/many will fail to verify,
hence we don't want the spam):
MONERO_LOGS=*:WARNING,verify:FATAL
Log levels are, in decreasing order of priority:
FATAL, ERROR, WARNING, INFO, DEBUG, TRACE
Subcategories may be added using prefixes and globs. This
example will output net.p2p logs at the TRACE level, but all
other net* logs only at INFO:
MONERO_LOGS=*:ERROR,net*:INFO,net.p2p:TRACE
Logs which are intended for the user (which Monero was using
a lot through epee, but really isn't a nice way to go things)
should use the "global" category. There are a few helper macros
for using this category, eg: MGINFO("this shows up by default")
or MGINFO_RED("this is red"), to try to keep a similar look
and feel for now.
Existing epee log macros still exist, and map to the new log
levels, but since they're used as a "user facing" UI element
as much as a logging system, they often don't map well to log
severities (ie, a log level 0 log may be an error, or may be
something we want the user to see, such as an important info).
In those cases, I tried to use the new macros. In other cases,
I left the existing macros in. When modifying logs, it is
probably best to switch to the new macros with explicit levels.
The --log-level options and set_log commands now also accept
category settings, in addition to the epee style log levels.
2017-01-01 16:34:23 +00:00
return true ;
2015-02-05 09:11:20 +00:00
}
Change logging to easylogging++
This replaces the epee and data_loggers logging systems with
a single one, and also adds filename:line and explicit severity
levels. Categories may be defined, and logging severity set
by category (or set of categories). epee style 0-4 log level
maps to a sensible severity configuration. Log files now also
rotate when reaching 100 MB.
To select which logs to output, use the MONERO_LOGS environment
variable, with a comma separated list of categories (globs are
supported), with their requested severity level after a colon.
If a log matches more than one such setting, the last one in
the configuration string applies. A few examples:
This one is (mostly) silent, only outputting fatal errors:
MONERO_LOGS=*:FATAL
This one is very verbose:
MONERO_LOGS=*:TRACE
This one is totally silent (logwise):
MONERO_LOGS=""
This one outputs all errors and warnings, except for the
"verify" category, which prints just fatal errors (the verify
category is used for logs about incoming transactions and
blocks, and it is expected that some/many will fail to verify,
hence we don't want the spam):
MONERO_LOGS=*:WARNING,verify:FATAL
Log levels are, in decreasing order of priority:
FATAL, ERROR, WARNING, INFO, DEBUG, TRACE
Subcategories may be added using prefixes and globs. This
example will output net.p2p logs at the TRACE level, but all
other net* logs only at INFO:
MONERO_LOGS=*:ERROR,net*:INFO,net.p2p:TRACE
Logs which are intended for the user (which Monero was using
a lot through epee, but really isn't a nice way to go things)
should use the "global" category. There are a few helper macros
for using this category, eg: MGINFO("this shows up by default")
or MGINFO_RED("this is red"), to try to keep a similar look
and feel for now.
Existing epee log macros still exist, and map to the new log
levels, but since they're used as a "user facing" UI element
as much as a logging system, they often don't map well to log
severities (ie, a log level 0 log may be an error, or may be
something we want the user to see, such as an important info).
In those cases, I tried to use the new macros. In other cases,
I left the existing macros in. When modifying logs, it is
probably best to switch to the new macros with explicit levels.
The --log-level options and set_log commands now also accept
category settings, in addition to the epee style log levels.
2017-01-01 16:34:23 +00:00
mlog_set_log_level ( req . level ) ;
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
bool core_rpc_server : : on_set_log_categories ( const COMMAND_RPC_SET_LOG_CATEGORIES : : request & req , COMMAND_RPC_SET_LOG_CATEGORIES : : response & res )
{
2017-02-12 11:37:09 +00:00
mlog_set_log ( req . categories . c_str ( ) ) ;
2017-09-22 16:54:58 +00:00
res . categories = mlog_get_categories ( ) ;
Change logging to easylogging++
This replaces the epee and data_loggers logging systems with
a single one, and also adds filename:line and explicit severity
levels. Categories may be defined, and logging severity set
by category (or set of categories). epee style 0-4 log level
maps to a sensible severity configuration. Log files now also
rotate when reaching 100 MB.
To select which logs to output, use the MONERO_LOGS environment
variable, with a comma separated list of categories (globs are
supported), with their requested severity level after a colon.
If a log matches more than one such setting, the last one in
the configuration string applies. A few examples:
This one is (mostly) silent, only outputting fatal errors:
MONERO_LOGS=*:FATAL
This one is very verbose:
MONERO_LOGS=*:TRACE
This one is totally silent (logwise):
MONERO_LOGS=""
This one outputs all errors and warnings, except for the
"verify" category, which prints just fatal errors (the verify
category is used for logs about incoming transactions and
blocks, and it is expected that some/many will fail to verify,
hence we don't want the spam):
MONERO_LOGS=*:WARNING,verify:FATAL
Log levels are, in decreasing order of priority:
FATAL, ERROR, WARNING, INFO, DEBUG, TRACE
Subcategories may be added using prefixes and globs. This
example will output net.p2p logs at the TRACE level, but all
other net* logs only at INFO:
MONERO_LOGS=*:ERROR,net*:INFO,net.p2p:TRACE
Logs which are intended for the user (which Monero was using
a lot through epee, but really isn't a nice way to go things)
should use the "global" category. There are a few helper macros
for using this category, eg: MGINFO("this shows up by default")
or MGINFO_RED("this is red"), to try to keep a similar look
and feel for now.
Existing epee log macros still exist, and map to the new log
levels, but since they're used as a "user facing" UI element
as much as a logging system, they often don't map well to log
severities (ie, a log level 0 log may be an error, or may be
something we want the user to see, such as an important info).
In those cases, I tried to use the new macros. In other cases,
I left the existing macros in. When modifying logs, it is
probably best to switch to the new macros with explicit levels.
The --log-level options and set_log commands now also accept
category settings, in addition to the epee style log levels.
2017-01-01 16:34:23 +00:00
res . status = CORE_RPC_STATUS_OK ;
2015-02-05 09:11:20 +00:00
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2017-11-08 12:06:41 +00:00
bool core_rpc_server : : on_get_transaction_pool ( const COMMAND_RPC_GET_TRANSACTION_POOL : : request & req , COMMAND_RPC_GET_TRANSACTION_POOL : : response & res , bool request_has_rpc_origin )
2015-02-05 09:11:20 +00:00
{
CHECK_CORE_BUSY ( ) ;
2017-11-08 12:06:41 +00:00
m_core . get_pool_transactions_and_spent_keys_info ( res . transactions , res . spent_key_images , ! request_has_rpc_origin | | ! m_restricted ) ;
2015-02-05 09:11:20 +00:00
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2017-11-08 12:06:41 +00:00
bool core_rpc_server : : on_get_transaction_pool_hashes ( const COMMAND_RPC_GET_TRANSACTION_POOL_HASHES : : request & req , COMMAND_RPC_GET_TRANSACTION_POOL_HASHES : : response & res , bool request_has_rpc_origin )
2017-03-22 18:03:23 +00:00
{
CHECK_CORE_BUSY ( ) ;
2017-11-08 12:06:41 +00:00
m_core . get_pool_transaction_hashes ( res . tx_hashes , ! request_has_rpc_origin | | ! m_restricted ) ;
2017-03-22 18:03:23 +00:00
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2017-11-08 12:06:41 +00:00
bool core_rpc_server : : on_get_transaction_pool_stats ( const COMMAND_RPC_GET_TRANSACTION_POOL_STATS : : request & req , COMMAND_RPC_GET_TRANSACTION_POOL_STATS : : response & res , bool request_has_rpc_origin )
2017-05-31 18:11:56 +00:00
{
CHECK_CORE_BUSY ( ) ;
2017-11-08 12:06:41 +00:00
m_core . get_pool_transaction_stats ( res . pool_stats , ! request_has_rpc_origin | | ! m_restricted ) ;
2017-05-31 18:11:56 +00:00
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2015-03-27 12:01:30 +00:00
bool core_rpc_server : : on_stop_daemon ( const COMMAND_RPC_STOP_DAEMON : : request & req , COMMAND_RPC_STOP_DAEMON : : response & res )
2015-02-05 09:11:20 +00:00
{
// FIXME: replace back to original m_p2p.send_stop_signal() after
// investigating why that isn't working quite right.
2015-02-05 10:38:49 +00:00
m_p2p . send_stop_signal ( ) ;
2015-02-05 09:11:20 +00:00
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2015-03-27 12:01:30 +00:00
bool core_rpc_server : : on_getblockcount ( const COMMAND_RPC_GETBLOCKCOUNT : : request & req , COMMAND_RPC_GETBLOCKCOUNT : : response & res )
2014-03-03 22:07:58 +00:00
{
2014-06-01 21:53:44 +00:00
CHECK_CORE_BUSY ( ) ;
2014-03-20 11:46:11 +00:00
res . count = m_core . get_current_blockchain_height ( ) ;
res . status = CORE_RPC_STATUS_OK ;
2014-03-03 22:07:58 +00:00
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2015-03-27 12:01:30 +00:00
bool core_rpc_server : : on_getblockhash ( const COMMAND_RPC_GETBLOCKHASH : : request & req , COMMAND_RPC_GETBLOCKHASH : : response & res , epee : : json_rpc : : error & error_resp )
2014-03-03 22:07:58 +00:00
{
2014-06-01 21:53:44 +00:00
if ( ! check_core_busy ( ) )
2014-03-20 11:46:11 +00:00
{
error_resp . code = CORE_RPC_ERROR_CODE_CORE_BUSY ;
error_resp . message = " Core is busy " ;
return false ;
}
2014-03-03 22:07:58 +00:00
if ( req . size ( ) ! = 1 )
{
error_resp . code = CORE_RPC_ERROR_CODE_WRONG_PARAM ;
error_resp . message = " Wrong parameters, expected height " ;
return false ;
}
uint64_t h = req [ 0 ] ;
if ( m_core . get_current_blockchain_height ( ) < = h )
{
error_resp . code = CORE_RPC_ERROR_CODE_TOO_BIG_HEIGHT ;
2016-03-15 05:09:21 +00:00
error_resp . message = std : : string ( " Too big height: " ) + std : : to_string ( h ) + " , current blockchain height = " + std : : to_string ( m_core . get_current_blockchain_height ( ) ) ;
2014-03-03 22:07:58 +00:00
}
res = string_tools : : pod_to_hex ( m_core . get_block_id_by_height ( h ) ) ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2014-10-06 09:27:34 +00:00
// equivalent of strstr, but with arbitrary bytes (ie, NULs)
// This does not differentiate between "not found" and "found at offset 0"
uint64_t slow_memmem ( const void * start_buff , size_t buflen , const void * pat , size_t patlen )
2014-03-03 22:07:58 +00:00
{
2014-10-06 09:27:34 +00:00
const void * buf = start_buff ;
const void * end = ( const char * ) buf + buflen ;
if ( patlen > buflen | | patlen = = 0 ) return 0 ;
while ( buflen > 0 & & ( buf = memchr ( buf , ( ( const char * ) pat ) [ 0 ] , buflen - patlen + 1 ) ) )
2014-03-03 22:07:58 +00:00
{
if ( memcmp ( buf , pat , patlen ) = = 0 )
2014-10-06 09:27:34 +00:00
return ( const char * ) buf - ( const char * ) start_buff ;
buf = ( const char * ) buf + 1 ;
buflen = ( const char * ) end - ( const char * ) buf ;
2014-03-03 22:07:58 +00:00
}
return 0 ;
}
//------------------------------------------------------------------------------------------------------------------------------
2015-03-27 12:01:30 +00:00
bool core_rpc_server : : on_getblocktemplate ( const COMMAND_RPC_GETBLOCKTEMPLATE : : request & req , COMMAND_RPC_GETBLOCKTEMPLATE : : response & res , epee : : json_rpc : : error & error_resp )
2014-03-03 22:07:58 +00:00
{
2014-03-20 11:46:11 +00:00
if ( ! check_core_ready ( ) )
{
error_resp . code = CORE_RPC_ERROR_CODE_CORE_BUSY ;
error_resp . message = " Core is busy " ;
return false ;
}
2014-03-03 22:07:58 +00:00
if ( req . reserve_size > 255 )
{
error_resp . code = CORE_RPC_ERROR_CODE_TOO_BIG_RESERVE_SIZE ;
error_resp . message = " To big reserved size, maximum 255 " ;
return false ;
}
2017-02-19 02:42:10 +00:00
cryptonote : : address_parse_info info ;
2014-03-03 22:07:58 +00:00
2017-02-19 02:42:10 +00:00
if ( ! req . wallet_address . size ( ) | | ! cryptonote : : get_account_address_from_str ( info , m_testnet , req . wallet_address ) )
2014-03-03 22:07:58 +00:00
{
error_resp . code = CORE_RPC_ERROR_CODE_WRONG_WALLET_ADDRESS ;
error_resp . message = " Failed to parse wallet address " ;
return false ;
}
2017-02-19 02:42:10 +00:00
if ( info . is_subaddress )
{
error_resp . code = CORE_RPC_ERROR_CODE_MINING_TO_SUBADDRESS ;
error_resp . message = " Mining to subaddress is not supported yet " ;
return false ;
}
2014-03-03 22:07:58 +00:00
block b = AUTO_VAL_INIT ( b ) ;
cryptonote : : blobdata blob_reserve ;
blob_reserve . resize ( req . reserve_size , 0 ) ;
2017-02-19 02:42:10 +00:00
if ( ! m_core . get_block_template ( b , info . address , res . difficulty , res . height , res . expected_reward , blob_reserve ) )
2014-03-03 22:07:58 +00:00
{
error_resp . code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR ;
error_resp . message = " Internal error: failed to create block template " ;
LOG_ERROR ( " Failed to create block template " ) ;
return false ;
}
blobdata block_blob = t_serializable_object_to_blob ( b ) ;
2014-05-03 16:19:43 +00:00
crypto : : public_key tx_pub_key = cryptonote : : get_tx_pub_key_from_extra ( b . miner_tx ) ;
2014-03-03 22:07:58 +00:00
if ( tx_pub_key = = null_pkey )
{
error_resp . code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR ;
error_resp . message = " Internal error: failed to create block template " ;
LOG_ERROR ( " Failed to tx pub key in coinbase extra " ) ;
return false ;
}
res . reserved_offset = slow_memmem ( ( void * ) block_blob . data ( ) , block_blob . size ( ) , & tx_pub_key , sizeof ( tx_pub_key ) ) ;
if ( ! res . reserved_offset )
{
error_resp . code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR ;
error_resp . message = " Internal error: failed to create block template " ;
LOG_ERROR ( " Failed to find tx pub key in blockblob " ) ;
return false ;
}
2017-08-26 16:38:27 +00:00
res . reserved_offset + = sizeof ( tx_pub_key ) + 2 ; //2 bytes: tag for TX_EXTRA_NONCE(1 byte), counter in TX_EXTRA_NONCE(1 byte)
2014-03-03 22:07:58 +00:00
if ( res . reserved_offset + req . reserve_size > block_blob . size ( ) )
{
error_resp . code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR ;
error_resp . message = " Internal error: failed to create block template " ;
LOG_ERROR ( " Failed to calculate offset for " ) ;
return false ;
}
2016-03-30 01:50:51 +00:00
blobdata hashing_blob = get_block_hashing_blob ( b ) ;
2015-01-06 16:37:10 +00:00
res . prev_hash = string_tools : : pod_to_hex ( b . prev_id ) ;
2014-03-03 22:07:58 +00:00
res . blocktemplate_blob = string_tools : : buff_to_hex_nodelimer ( block_blob ) ;
2016-03-30 01:50:51 +00:00
res . blockhashing_blob = string_tools : : buff_to_hex_nodelimer ( hashing_blob ) ;
2014-05-25 17:06:40 +00:00
res . status = CORE_RPC_STATUS_OK ;
2014-03-03 22:07:58 +00:00
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2015-03-27 12:01:30 +00:00
bool core_rpc_server : : on_submitblock ( const COMMAND_RPC_SUBMITBLOCK : : request & req , COMMAND_RPC_SUBMITBLOCK : : response & res , epee : : json_rpc : : error & error_resp )
2014-03-03 22:07:58 +00:00
{
2014-03-20 11:46:11 +00:00
CHECK_CORE_READY ( ) ;
2014-03-03 22:07:58 +00:00
if ( req . size ( ) ! = 1 )
{
error_resp . code = CORE_RPC_ERROR_CODE_WRONG_PARAM ;
error_resp . message = " Wrong param " ;
return false ;
}
blobdata blockblob ;
if ( ! string_tools : : parse_hexstr_to_binbuff ( req [ 0 ] , blockblob ) )
{
error_resp . code = CORE_RPC_ERROR_CODE_WRONG_BLOCKBLOB ;
error_resp . message = " Wrong block blob " ;
return false ;
}
2016-10-10 20:41:24 +00:00
2014-06-11 14:46:56 +00:00
// Fixing of high orphan issue for most pools
// Thanks Boolberry!
block b = AUTO_VAL_INIT ( b ) ;
if ( ! parse_and_validate_block_from_blob ( blockblob , b ) )
{
error_resp . code = CORE_RPC_ERROR_CODE_WRONG_BLOCKBLOB ;
error_resp . message = " Wrong block blob " ;
return false ;
}
2014-06-11 21:32:53 +00:00
// Fix from Boolberry neglects to check block
// size, do that with the function below
if ( ! m_core . check_incoming_block_size ( blockblob ) )
{
error_resp . code = CORE_RPC_ERROR_CODE_WRONG_BLOCKBLOB_SIZE ;
error_resp . message = " Block bloc size is too big, rejecting block " ;
return false ;
}
2014-06-11 14:46:56 +00:00
if ( ! m_core . handle_block_found ( b ) )
2014-03-03 22:07:58 +00:00
{
error_resp . code = CORE_RPC_ERROR_CODE_BLOCK_NOT_ACCEPTED ;
error_resp . message = " Block not accepted " ;
return false ;
}
2014-05-25 17:06:40 +00:00
res . status = CORE_RPC_STATUS_OK ;
2014-03-03 22:07:58 +00:00
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2014-04-09 12:14:35 +00:00
uint64_t core_rpc_server : : get_block_reward ( const block & blk )
{
uint64_t reward = 0 ;
2017-01-22 20:38:10 +00:00
for ( const tx_out & out : blk . miner_tx . vout )
2014-04-09 12:14:35 +00:00
{
reward + = out . amount ;
}
return reward ;
}
//------------------------------------------------------------------------------------------------------------------------------
2016-09-29 13:38:12 +00:00
bool core_rpc_server : : fill_block_header_response ( const block & blk , bool orphan_status , uint64_t height , const crypto : : hash & hash , block_header_response & response )
{
response . major_version = blk . major_version ;
response . minor_version = blk . minor_version ;
response . timestamp = blk . timestamp ;
response . prev_hash = string_tools : : pod_to_hex ( blk . prev_id ) ;
response . nonce = blk . nonce ;
response . orphan_status = orphan_status ;
response . height = height ;
response . depth = m_core . get_current_blockchain_height ( ) - height - 1 ;
response . hash = string_tools : : pod_to_hex ( hash ) ;
response . difficulty = m_core . get_blockchain_storage ( ) . block_difficulty ( height ) ;
response . reward = get_block_reward ( blk ) ;
2017-01-06 19:35:59 +00:00
response . block_size = m_core . get_blockchain_storage ( ) . get_db ( ) . get_block_size ( height ) ;
2017-01-08 11:14:11 +00:00
response . num_txes = blk . tx_hashes . size ( ) ;
2014-04-09 12:14:35 +00:00
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2015-03-27 12:01:30 +00:00
bool core_rpc_server : : on_get_last_block_header ( const COMMAND_RPC_GET_LAST_BLOCK_HEADER : : request & req , COMMAND_RPC_GET_LAST_BLOCK_HEADER : : response & res , epee : : json_rpc : : error & error_resp )
2014-04-09 12:14:35 +00:00
{
2014-06-01 21:53:44 +00:00
if ( ! check_core_busy ( ) )
2014-04-09 12:14:35 +00:00
{
error_resp . code = CORE_RPC_ERROR_CODE_CORE_BUSY ;
error_resp . message = " Core is busy. " ;
return false ;
}
uint64_t last_block_height ;
crypto : : hash last_block_hash ;
2017-09-09 11:06:24 +00:00
m_core . get_blockchain_top ( last_block_height , last_block_hash ) ;
2014-04-09 12:14:35 +00:00
block last_block ;
bool have_last_block = m_core . get_block_by_hash ( last_block_hash , last_block ) ;
if ( ! have_last_block )
{
error_resp . code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR ;
error_resp . message = " Internal error: can't get last block. " ;
return false ;
}
2016-09-29 13:38:12 +00:00
bool response_filled = fill_block_header_response ( last_block , false , last_block_height , last_block_hash , res . block_header ) ;
if ( ! response_filled )
2014-04-09 12:14:35 +00:00
{
error_resp . code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR ;
error_resp . message = " Internal error: can't produce valid response. " ;
return false ;
}
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2015-03-27 12:01:30 +00:00
bool core_rpc_server : : on_get_block_header_by_hash ( const COMMAND_RPC_GET_BLOCK_HEADER_BY_HASH : : request & req , COMMAND_RPC_GET_BLOCK_HEADER_BY_HASH : : response & res , epee : : json_rpc : : error & error_resp ) {
2014-06-01 21:53:44 +00:00
if ( ! check_core_busy ( ) )
2014-04-09 12:14:35 +00:00
{
error_resp . code = CORE_RPC_ERROR_CODE_CORE_BUSY ;
error_resp . message = " Core is busy. " ;
return false ;
}
crypto : : hash block_hash ;
bool hash_parsed = parse_hash256 ( req . hash , block_hash ) ;
if ( ! hash_parsed )
{
error_resp . code = CORE_RPC_ERROR_CODE_WRONG_PARAM ;
error_resp . message = " Failed to parse hex representation of block hash. Hex = " + req . hash + ' . ' ;
return false ;
}
block blk ;
2017-01-22 12:20:55 +00:00
bool orphan = false ;
bool have_block = m_core . get_block_by_hash ( block_hash , blk , & orphan ) ;
2014-04-09 12:14:35 +00:00
if ( ! have_block )
{
error_resp . code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR ;
error_resp . message = " Internal error: can't get block by hash. Hash = " + req . hash + ' . ' ;
return false ;
}
if ( blk . miner_tx . vin . front ( ) . type ( ) ! = typeid ( txin_gen ) )
{
error_resp . code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR ;
error_resp . message = " Internal error: coinbase transaction in the block has the wrong type " ;
return false ;
}
uint64_t block_height = boost : : get < txin_gen > ( blk . miner_tx . vin . front ( ) ) . height ;
2017-01-22 12:20:55 +00:00
bool response_filled = fill_block_header_response ( blk , orphan , block_height , block_hash , res . block_header ) ;
2016-09-29 13:38:12 +00:00
if ( ! response_filled )
2014-04-09 12:14:35 +00:00
{
error_resp . code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR ;
error_resp . message = " Internal error: can't produce valid response. " ;
return false ;
}
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2016-10-02 09:21:21 +00:00
bool core_rpc_server : : on_get_block_headers_range ( const COMMAND_RPC_GET_BLOCK_HEADERS_RANGE : : request & req , COMMAND_RPC_GET_BLOCK_HEADERS_RANGE : : response & res , epee : : json_rpc : : error & error_resp ) {
if ( ! check_core_busy ( ) )
{
error_resp . code = CORE_RPC_ERROR_CODE_CORE_BUSY ;
error_resp . message = " Core is busy. " ;
return false ;
}
const uint64_t bc_height = m_core . get_current_blockchain_height ( ) ;
if ( req . start_height > = bc_height | | req . end_height > = bc_height | | req . start_height > req . end_height )
{
error_resp . code = CORE_RPC_ERROR_CODE_TOO_BIG_HEIGHT ;
error_resp . message = " Invalid start/end heights. " ;
return false ;
}
for ( uint64_t h = req . start_height ; h < = req . end_height ; + + h )
{
crypto : : hash block_hash = m_core . get_block_id_by_height ( h ) ;
block blk ;
bool have_block = m_core . get_block_by_hash ( block_hash , blk ) ;
if ( ! have_block )
{
error_resp . code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR ;
error_resp . message = " Internal error: can't get block by height. Height = " + boost : : lexical_cast < std : : string > ( h ) + " . Hash = " + epee : : string_tools : : pod_to_hex ( block_hash ) + ' . ' ;
return false ;
}
if ( blk . miner_tx . vin . front ( ) . type ( ) ! = typeid ( txin_gen ) )
{
error_resp . code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR ;
error_resp . message = " Internal error: coinbase transaction in the block has the wrong type " ;
return false ;
}
uint64_t block_height = boost : : get < txin_gen > ( blk . miner_tx . vin . front ( ) ) . height ;
if ( block_height ! = h )
{
error_resp . code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR ;
error_resp . message = " Internal error: coinbase transaction in the block has the wrong height " ;
return false ;
}
2016-10-04 11:55:55 +00:00
res . headers . push_back ( block_header_response ( ) ) ;
2017-06-18 08:12:54 +00:00
bool response_filled = fill_block_header_response ( blk , false , block_height , block_hash , res . headers . back ( ) ) ;
if ( ! response_filled )
2016-10-02 09:21:21 +00:00
{
error_resp . code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR ;
error_resp . message = " Internal error: can't produce valid response. " ;
return false ;
}
}
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2015-03-27 12:01:30 +00:00
bool core_rpc_server : : on_get_block_header_by_height ( const COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT : : request & req , COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT : : response & res , epee : : json_rpc : : error & error_resp ) {
2014-06-01 21:53:44 +00:00
if ( ! check_core_busy ( ) )
2014-04-09 12:14:35 +00:00
{
error_resp . code = CORE_RPC_ERROR_CODE_CORE_BUSY ;
error_resp . message = " Core is busy. " ;
return false ;
}
if ( m_core . get_current_blockchain_height ( ) < = req . height )
{
error_resp . code = CORE_RPC_ERROR_CODE_TOO_BIG_HEIGHT ;
2016-03-15 05:09:21 +00:00
error_resp . message = std : : string ( " Too big height: " ) + std : : to_string ( req . height ) + " , current blockchain height = " + std : : to_string ( m_core . get_current_blockchain_height ( ) ) ;
2014-04-09 12:14:35 +00:00
return false ;
}
crypto : : hash block_hash = m_core . get_block_id_by_height ( req . height ) ;
block blk ;
bool have_block = m_core . get_block_by_hash ( block_hash , blk ) ;
if ( ! have_block )
{
error_resp . code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR ;
2014-04-30 17:52:21 +00:00
error_resp . message = " Internal error: can't get block by height. Height = " + std : : to_string ( req . height ) + ' . ' ;
2014-04-09 12:14:35 +00:00
return false ;
}
2016-09-29 13:38:12 +00:00
bool response_filled = fill_block_header_response ( blk , false , req . height , block_hash , res . block_header ) ;
if ( ! response_filled )
2014-04-09 12:14:35 +00:00
{
error_resp . code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR ;
error_resp . message = " Internal error: can't produce valid response. " ;
return false ;
}
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2015-10-13 20:37:35 +00:00
bool core_rpc_server : : on_get_block ( const COMMAND_RPC_GET_BLOCK : : request & req , COMMAND_RPC_GET_BLOCK : : response & res , epee : : json_rpc : : error & error_resp ) {
if ( ! check_core_busy ( ) )
{
error_resp . code = CORE_RPC_ERROR_CODE_CORE_BUSY ;
error_resp . message = " Core is busy. " ;
return false ;
}
crypto : : hash block_hash ;
if ( ! req . hash . empty ( ) )
{
bool hash_parsed = parse_hash256 ( req . hash , block_hash ) ;
if ( ! hash_parsed )
{
error_resp . code = CORE_RPC_ERROR_CODE_WRONG_PARAM ;
error_resp . message = " Failed to parse hex representation of block hash. Hex = " + req . hash + ' . ' ;
return false ;
}
}
else
{
if ( m_core . get_current_blockchain_height ( ) < = req . height )
{
error_resp . code = CORE_RPC_ERROR_CODE_TOO_BIG_HEIGHT ;
2016-03-15 05:09:21 +00:00
error_resp . message = std : : string ( " Too big height: " ) + std : : to_string ( req . height ) + " , current blockchain height = " + std : : to_string ( m_core . get_current_blockchain_height ( ) ) ;
2015-10-13 20:37:35 +00:00
return false ;
}
block_hash = m_core . get_block_id_by_height ( req . height ) ;
}
block blk ;
2017-01-22 12:20:55 +00:00
bool orphan = false ;
bool have_block = m_core . get_block_by_hash ( block_hash , blk , & orphan ) ;
2015-10-13 20:37:35 +00:00
if ( ! have_block )
{
error_resp . code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR ;
error_resp . message = " Internal error: can't get block by hash. Hash = " + req . hash + ' . ' ;
return false ;
}
if ( blk . miner_tx . vin . front ( ) . type ( ) ! = typeid ( txin_gen ) )
{
error_resp . code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR ;
error_resp . message = " Internal error: coinbase transaction in the block has the wrong type " ;
return false ;
}
uint64_t block_height = boost : : get < txin_gen > ( blk . miner_tx . vin . front ( ) ) . height ;
2017-01-22 12:20:55 +00:00
bool response_filled = fill_block_header_response ( blk , orphan , block_height , block_hash , res . block_header ) ;
2016-09-29 13:38:12 +00:00
if ( ! response_filled )
2015-10-13 20:37:35 +00:00
{
error_resp . code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR ;
error_resp . message = " Internal error: can't produce valid response. " ;
return false ;
}
for ( size_t n = 0 ; n < blk . tx_hashes . size ( ) ; + + n )
{
res . tx_hashes . push_back ( epee : : string_tools : : pod_to_hex ( blk . tx_hashes [ n ] ) ) ;
}
2016-06-09 20:48:29 +00:00
res . blob = string_tools : : buff_to_hex_nodelimer ( t_serializable_object_to_blob ( blk ) ) ;
2015-10-13 20:37:35 +00:00
res . json = obj_to_json_str ( blk ) ;
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2015-03-27 12:01:30 +00:00
bool core_rpc_server : : on_get_connections ( const COMMAND_RPC_GET_CONNECTIONS : : request & req , COMMAND_RPC_GET_CONNECTIONS : : response & res , epee : : json_rpc : : error & error_resp )
2014-07-18 23:33:03 +00:00
{
if ( ! check_core_busy ( ) )
{
error_resp . code = CORE_RPC_ERROR_CODE_CORE_BUSY ;
error_resp . message = " Core is busy. " ;
return false ;
}
res . connections = m_p2p . get_payload_object ( ) . get_connections ( ) ;
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2015-03-27 12:01:30 +00:00
bool core_rpc_server : : on_get_info_json ( const COMMAND_RPC_GET_INFO : : request & req , COMMAND_RPC_GET_INFO : : response & res , epee : : json_rpc : : error & error_resp )
2014-07-22 18:00:10 +00:00
{
if ( ! check_core_busy ( ) )
{
error_resp . code = CORE_RPC_ERROR_CODE_CORE_BUSY ;
error_resp . message = " Core is busy. " ;
return false ;
}
2016-10-15 13:35:29 +00:00
crypto : : hash top_hash ;
2017-09-09 11:06:24 +00:00
m_core . get_blockchain_top ( res . height , top_hash ) ;
2016-10-15 13:35:29 +00:00
+ + res . height ; // turn top block height into blockchain height
res . top_block_hash = string_tools : : pod_to_hex ( top_hash ) ;
2014-07-22 18:00:10 +00:00
res . target_height = m_core . get_target_blockchain_height ( ) ;
res . difficulty = m_core . get_blockchain_storage ( ) . get_difficulty_for_next_block ( ) ;
2016-01-29 15:09:17 +00:00
res . target = m_core . get_blockchain_storage ( ) . get_current_hard_fork_version ( ) < 2 ? DIFFICULTY_TARGET_V1 : DIFFICULTY_TARGET_V2 ;
2014-07-22 18:00:10 +00:00
res . tx_count = m_core . get_blockchain_storage ( ) . get_total_transactions ( ) - res . height ; //without coinbase
res . tx_pool_size = m_core . get_pool_transactions_count ( ) ;
res . alt_blocks_count = m_core . get_blockchain_storage ( ) . get_alternative_blocks_count ( ) ;
uint64_t total_conn = m_p2p . get_connections_count ( ) ;
res . outgoing_connections_count = m_p2p . get_outgoing_connections_count ( ) ;
res . incoming_connections_count = total_conn - res . outgoing_connections_count ;
res . white_peerlist_size = m_p2p . get_peerlist_manager ( ) . get_white_peers_count ( ) ;
res . grey_peerlist_size = m_p2p . get_peerlist_manager ( ) . get_gray_peers_count ( ) ;
2016-01-11 22:52:01 +00:00
res . testnet = m_testnet ;
2016-10-15 14:00:21 +00:00
res . cumulative_difficulty = m_core . get_blockchain_storage ( ) . get_db ( ) . get_block_cumulative_difficulty ( res . height - 1 ) ;
2017-01-06 19:26:47 +00:00
res . block_size_limit = m_core . get_blockchain_storage ( ) . get_current_cumulative_blocksize_limit ( ) ;
2014-07-22 18:00:10 +00:00
res . status = CORE_RPC_STATUS_OK ;
2017-01-08 23:50:29 +00:00
res . start_time = ( uint64_t ) m_core . get_start_time ( ) ;
2014-07-22 18:00:10 +00:00
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2015-09-19 15:34:29 +00:00
bool core_rpc_server : : on_hard_fork_info ( const COMMAND_RPC_HARD_FORK_INFO : : request & req , COMMAND_RPC_HARD_FORK_INFO : : response & res , epee : : json_rpc : : error & error_resp )
{
if ( ! check_core_busy ( ) )
{
error_resp . code = CORE_RPC_ERROR_CODE_CORE_BUSY ;
error_resp . message = " Core is busy. " ;
return false ;
}
const Blockchain & blockchain = m_core . get_blockchain_storage ( ) ;
2016-08-12 18:19:25 +00:00
uint8_t version = req . version > 0 ? req . version : blockchain . get_next_hard_fork_version ( ) ;
2015-09-19 15:34:29 +00:00
res . version = blockchain . get_current_hard_fork_version ( ) ;
2015-12-19 14:52:30 +00:00
res . enabled = blockchain . get_hard_fork_voting_info ( version , res . window , res . votes , res . threshold , res . earliest_height , res . voting ) ;
2015-09-19 15:34:29 +00:00
res . state = blockchain . get_hard_fork_state ( ) ;
2015-10-26 10:17:48 +00:00
res . status = CORE_RPC_STATUS_OK ;
2015-09-19 15:34:29 +00:00
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2015-11-26 00:04:22 +00:00
bool core_rpc_server : : on_get_bans ( const COMMAND_RPC_GETBANS : : request & req , COMMAND_RPC_GETBANS : : response & res , epee : : json_rpc : : error & error_resp )
{
if ( ! check_core_busy ( ) )
{
error_resp . code = CORE_RPC_ERROR_CODE_CORE_BUSY ;
error_resp . message = " Core is busy. " ;
return false ;
}
2016-03-12 13:44:55 +00:00
auto now = time ( nullptr ) ;
2017-05-27 10:35:54 +00:00
std : : map < std : : string , time_t > blocked_hosts = m_p2p . get_blocked_hosts ( ) ;
for ( std : : map < std : : string , time_t > : : const_iterator i = blocked_hosts . begin ( ) ; i ! = blocked_hosts . end ( ) ; + + i )
2015-11-26 00:04:22 +00:00
{
2016-03-12 13:44:55 +00:00
if ( i - > second > now ) {
COMMAND_RPC_GETBANS : : ban b ;
2017-05-27 10:35:54 +00:00
b . host = i - > first ;
b . ip = 0 ;
uint32_t ip ;
if ( epee : : string_tools : : get_ip_int32_from_string ( ip , i - > first ) )
b . ip = ip ;
2016-03-12 13:44:55 +00:00
b . seconds = i - > second - now ;
res . bans . push_back ( b ) ;
}
2015-11-26 00:04:22 +00:00
}
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
bool core_rpc_server : : on_set_bans ( const COMMAND_RPC_SETBANS : : request & req , COMMAND_RPC_SETBANS : : response & res , epee : : json_rpc : : error & error_resp )
{
if ( ! check_core_busy ( ) )
{
error_resp . code = CORE_RPC_ERROR_CODE_CORE_BUSY ;
error_resp . message = " Core is busy. " ;
return false ;
}
for ( auto i = req . bans . begin ( ) ; i ! = req . bans . end ( ) ; + + i )
{
2017-05-27 10:35:54 +00:00
epee : : net_utils : : network_address na ;
if ( ! i - > host . empty ( ) )
{
if ( ! epee : : net_utils : : create_network_address ( na , i - > host ) )
{
error_resp . code = CORE_RPC_ERROR_CODE_WRONG_PARAM ;
error_resp . message = " Unsupported host type " ;
return false ;
}
}
else
{
2017-08-25 15:14:46 +00:00
na = epee : : net_utils : : ipv4_network_address { i - > ip , 0 } ;
2017-05-27 10:35:54 +00:00
}
2015-11-26 00:04:22 +00:00
if ( i - > ban )
2017-05-27 10:35:54 +00:00
m_p2p . block_host ( na , i - > seconds ) ;
2015-11-26 00:04:22 +00:00
else
2017-05-27 10:35:54 +00:00
m_p2p . unblock_host ( na ) ;
2015-11-26 00:04:22 +00:00
}
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2016-01-30 13:28:26 +00:00
bool core_rpc_server : : on_flush_txpool ( const COMMAND_RPC_FLUSH_TRANSACTION_POOL : : request & req , COMMAND_RPC_FLUSH_TRANSACTION_POOL : : response & res , epee : : json_rpc : : error & error_resp )
{
if ( ! check_core_busy ( ) )
{
error_resp . code = CORE_RPC_ERROR_CODE_CORE_BUSY ;
error_resp . message = " Core is busy. " ;
return false ;
}
bool failed = false ;
std : : list < crypto : : hash > txids ;
if ( req . txids . empty ( ) )
{
std : : list < transaction > pool_txs ;
bool r = m_core . get_pool_transactions ( pool_txs ) ;
if ( ! r )
{
res . status = " Failed to get txpool contents " ;
return true ;
}
for ( const auto & tx : pool_txs )
{
txids . push_back ( cryptonote : : get_transaction_hash ( tx ) ) ;
}
}
else
{
for ( const auto & str : req . txids )
{
cryptonote : : blobdata txid_data ;
if ( ! epee : : string_tools : : parse_hexstr_to_binbuff ( str , txid_data ) )
{
failed = true ;
}
crypto : : hash txid = * reinterpret_cast < const crypto : : hash * > ( txid_data . data ( ) ) ;
txids . push_back ( txid ) ;
}
}
if ( ! m_core . get_blockchain_storage ( ) . flush_txes_from_pool ( txids ) )
{
res . status = " Failed to remove one more tx " ;
return false ;
}
if ( failed )
{
res . status = " Failed to parse txid " ;
return false ;
}
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2016-03-26 14:30:23 +00:00
bool core_rpc_server : : on_get_output_histogram ( const COMMAND_RPC_GET_OUTPUT_HISTOGRAM : : request & req , COMMAND_RPC_GET_OUTPUT_HISTOGRAM : : response & res , epee : : json_rpc : : error & error_resp )
{
if ( ! check_core_busy ( ) )
{
error_resp . code = CORE_RPC_ERROR_CODE_CORE_BUSY ;
error_resp . message = " Core is busy. " ;
return false ;
}
2016-09-17 14:45:51 +00:00
std : : map < uint64_t , std : : tuple < uint64_t , uint64_t , uint64_t > > histogram ;
2016-03-26 14:30:23 +00:00
try
{
2016-09-17 14:45:51 +00:00
histogram = m_core . get_blockchain_storage ( ) . get_output_histogram ( req . amounts , req . unlocked , req . recent_cutoff ) ;
2016-03-26 14:30:23 +00:00
}
catch ( const std : : exception & e )
{
res . status = " Failed to get output histogram " ;
return true ;
}
res . histogram . clear ( ) ;
res . histogram . reserve ( histogram . size ( ) ) ;
for ( const auto & i : histogram )
{
2016-09-17 14:45:51 +00:00
if ( std : : get < 0 > ( i . second ) > = req . min_count & & ( std : : get < 0 > ( i . second ) < = req . max_count | | req . max_count = = 0 ) )
res . histogram . push_back ( COMMAND_RPC_GET_OUTPUT_HISTOGRAM : : entry ( i . first , std : : get < 0 > ( i . second ) , std : : get < 1 > ( i . second ) , std : : get < 2 > ( i . second ) ) ) ;
2016-03-26 14:30:23 +00:00
}
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2016-07-10 15:49:40 +00:00
bool core_rpc_server : : on_get_version ( const COMMAND_RPC_GET_VERSION : : request & req , COMMAND_RPC_GET_VERSION : : response & res , epee : : json_rpc : : error & error_resp )
{
res . version = CORE_RPC_VERSION ;
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2016-10-10 19:45:51 +00:00
bool core_rpc_server : : on_get_coinbase_tx_sum ( const COMMAND_RPC_GET_COINBASE_TX_SUM : : request & req , COMMAND_RPC_GET_COINBASE_TX_SUM : : response & res , epee : : json_rpc : : error & error_resp )
{
2016-10-10 23:55:18 +00:00
std : : pair < uint64_t , uint64_t > amounts = m_core . get_coinbase_tx_sum ( req . height , req . count ) ;
res . emission_amount = amounts . first ;
res . fee_amount = amounts . second ;
2017-02-13 23:05:33 +00:00
res . status = CORE_RPC_STATUS_OK ;
2016-10-10 19:45:51 +00:00
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2016-10-28 20:19:40 +00:00
bool core_rpc_server : : on_get_per_kb_fee_estimate ( const COMMAND_RPC_GET_PER_KB_FEE_ESTIMATE : : request & req , COMMAND_RPC_GET_PER_KB_FEE_ESTIMATE : : response & res , epee : : json_rpc : : error & error_resp )
{
res . fee = m_core . get_blockchain_storage ( ) . get_dynamic_per_kb_fee_estimate ( req . grace_blocks ) ;
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2016-12-17 11:25:15 +00:00
bool core_rpc_server : : on_get_alternate_chains ( const COMMAND_RPC_GET_ALTERNATE_CHAINS : : request & req , COMMAND_RPC_GET_ALTERNATE_CHAINS : : response & res , epee : : json_rpc : : error & error_resp )
{
try
{
std : : list < std : : pair < Blockchain : : block_extended_info , uint64_t > > chains = m_core . get_blockchain_storage ( ) . get_alternative_chains ( ) ;
for ( const auto & i : chains )
{
res . chains . push_back ( COMMAND_RPC_GET_ALTERNATE_CHAINS : : chain_info { epee : : string_tools : : pod_to_hex ( get_block_hash ( i . first . bl ) ) , i . first . height , i . second , i . first . cumulative_difficulty } ) ;
}
res . status = CORE_RPC_STATUS_OK ;
}
catch ( . . . )
{
res . status = " Error retrieving alternate chains " ;
}
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2017-09-17 19:19:53 +00:00
bool core_rpc_server : : on_get_limit ( const COMMAND_RPC_GET_LIMIT : : request & req , COMMAND_RPC_GET_LIMIT : : response & res )
{
res . limit_down = epee : : net_utils : : connection_basic : : get_rate_down_limit ( ) ;
res . limit_up = epee : : net_utils : : connection_basic : : get_rate_up_limit ( ) ;
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
bool core_rpc_server : : on_set_limit ( const COMMAND_RPC_SET_LIMIT : : request & req , COMMAND_RPC_SET_LIMIT : : response & res )
{
// -1 = reset to default
// 0 = do not modify
if ( req . limit_down > 0 )
{
epee : : net_utils : : connection_basic : : set_rate_down_limit ( req . limit_down ) ;
}
else if ( req . limit_down < 0 )
{
if ( req . limit_down ! = - 1 )
{
res . status = CORE_RPC_ERROR_CODE_WRONG_PARAM ;
return false ;
}
epee : : net_utils : : connection_basic : : set_rate_down_limit ( nodetool : : default_limit_down * 1024 ) ;
}
if ( req . limit_up > 0 )
{
epee : : net_utils : : connection_basic : : set_rate_up_limit ( req . limit_up ) ;
}
else if ( req . limit_up < 0 )
{
if ( req . limit_up ! = - 1 )
{
res . status = CORE_RPC_ERROR_CODE_WRONG_PARAM ;
return false ;
}
epee : : net_utils : : connection_basic : : set_rate_up_limit ( nodetool : : default_limit_up * 1024 ) ;
}
res . limit_down = epee : : net_utils : : connection_basic : : get_rate_down_limit ( ) ;
res . limit_up = epee : : net_utils : : connection_basic : : get_rate_up_limit ( ) ;
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2015-04-01 17:00:45 +00:00
bool core_rpc_server : : on_out_peers ( const COMMAND_RPC_OUT_PEERS : : request & req , COMMAND_RPC_OUT_PEERS : : response & res )
{
2017-10-06 07:40:14 +00:00
size_t n_connections = m_p2p . get_outgoing_connections_count ( ) ;
size_t n_delete = ( n_connections > req . out_peers ) ? n_connections - req . out_peers : 0 ;
m_p2p . m_config . m_net_config . connections_count = req . out_peers ;
if ( n_delete )
m_p2p . delete_connections ( n_delete ) ;
res . status = CORE_RPC_STATUS_OK ;
return true ;
2015-04-01 17:00:45 +00:00
}
//------------------------------------------------------------------------------------------------------------------------------
bool core_rpc_server : : on_start_save_graph ( const COMMAND_RPC_START_SAVE_GRAPH : : request & req , COMMAND_RPC_START_SAVE_GRAPH : : response & res )
{
m_p2p . set_save_graph ( true ) ;
2017-02-13 23:05:33 +00:00
res . status = CORE_RPC_STATUS_OK ;
2015-04-01 17:00:45 +00:00
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
bool core_rpc_server : : on_stop_save_graph ( const COMMAND_RPC_STOP_SAVE_GRAPH : : request & req , COMMAND_RPC_STOP_SAVE_GRAPH : : response & res )
{
m_p2p . set_save_graph ( false ) ;
2017-02-13 23:05:33 +00:00
res . status = CORE_RPC_STATUS_OK ;
2015-04-01 17:00:45 +00:00
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2017-02-24 23:16:13 +00:00
bool core_rpc_server : : on_update ( const COMMAND_RPC_UPDATE : : request & req , COMMAND_RPC_UPDATE : : response & res )
{
static const char software [ ] = " monero " ;
2017-03-04 18:45:33 +00:00
# ifdef BUILD_TAG
static const char buildtag [ ] = BOOST_PP_STRINGIZE ( BUILD_TAG ) ;
2017-09-22 20:48:19 +00:00
static const char subdir [ ] = " cli " ;
2017-02-24 23:16:13 +00:00
# else
static const char buildtag [ ] = " source " ;
2017-09-22 20:48:19 +00:00
static const char subdir [ ] = " source " ;
2017-02-24 23:16:13 +00:00
# endif
if ( req . command ! = " check " & & req . command ! = " download " & & req . command ! = " update " )
{
res . status = std : : string ( " unknown command: ' " ) + req . command + " ' " ;
return true ;
}
std : : string version , hash ;
if ( ! tools : : check_updates ( software , buildtag , version , hash ) )
{
res . status = " Error checking for updates " ;
return true ;
}
if ( tools : : vercmp ( version . c_str ( ) , MONERO_VERSION ) < = 0 )
{
res . update = false ;
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
res . update = true ;
res . version = version ;
2017-09-22 20:48:19 +00:00
res . user_uri = tools : : get_update_url ( software , subdir , buildtag , version , true ) ;
res . auto_uri = tools : : get_update_url ( software , subdir , buildtag , version , false ) ;
2017-02-24 23:16:13 +00:00
res . hash = hash ;
if ( req . command = = " check " )
{
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
boost : : filesystem : : path path ;
if ( req . path . empty ( ) )
{
std : : string filename ;
const char * slash = strrchr ( res . auto_uri . c_str ( ) , ' / ' ) ;
if ( slash )
filename = slash + 1 ;
else
filename = std : : string ( software ) + " -update- " + version ;
path = epee : : string_tools : : get_current_module_folder ( ) ;
path / = filename ;
}
else
{
path = req . path ;
}
crypto : : hash file_hash ;
if ( ! tools : : sha256sum ( path . string ( ) , file_hash ) | | ( hash ! = epee : : string_tools : : pod_to_hex ( file_hash ) ) )
{
MDEBUG ( " We don't have that file already, downloading " ) ;
if ( ! tools : : download ( path . string ( ) , res . auto_uri ) )
{
MERROR ( " Failed to download " < < res . auto_uri ) ;
return false ;
}
if ( ! tools : : sha256sum ( path . string ( ) , file_hash ) )
{
MERROR ( " Failed to hash " < < path ) ;
return false ;
}
if ( hash ! = epee : : string_tools : : pod_to_hex ( file_hash ) )
{
MERROR ( " Download from " < < res . auto_uri < < " does not match the expected hash " ) ;
return false ;
}
MINFO ( " New version downloaded to " < < path ) ;
}
else
{
MDEBUG ( " We already have " < < path < < " with expected hash " ) ;
}
res . path = path . string ( ) ;
if ( req . command = = " download " )
{
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
res . status = " 'update' not implemented yet " ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2017-04-02 11:17:35 +00:00
bool core_rpc_server : : on_relay_tx ( const COMMAND_RPC_RELAY_TX : : request & req , COMMAND_RPC_RELAY_TX : : response & res , epee : : json_rpc : : error & error_resp )
{
if ( ! check_core_busy ( ) )
{
error_resp . code = CORE_RPC_ERROR_CODE_CORE_BUSY ;
error_resp . message = " Core is busy. " ;
return false ;
}
bool failed = false ;
for ( const auto & str : req . txids )
{
cryptonote : : blobdata txid_data ;
if ( ! epee : : string_tools : : parse_hexstr_to_binbuff ( str , txid_data ) )
{
res . status = std : : string ( " Invalid transaction id: " ) + str ;
failed = true ;
}
crypto : : hash txid = * reinterpret_cast < const crypto : : hash * > ( txid_data . data ( ) ) ;
2017-05-14 13:06:55 +00:00
cryptonote : : blobdata txblob ;
bool r = m_core . get_pool_transaction ( txid , txblob ) ;
2017-04-02 11:17:35 +00:00
if ( r )
{
cryptonote_connection_context fake_context = AUTO_VAL_INIT ( fake_context ) ;
NOTIFY_NEW_TRANSACTIONS : : request r ;
2017-05-14 13:06:55 +00:00
r . txs . push_back ( txblob ) ;
2017-04-02 11:17:35 +00:00
m_core . get_protocol ( ) - > relay_transactions ( r , fake_context ) ;
//TODO: make sure that tx has reached other nodes here, probably wait to receive reflections from other nodes
}
else
{
res . status = std : : string ( " Transaction not found in pool: " ) + str ;
failed = true ;
}
}
if ( failed )
{
return false ;
}
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2017-07-02 21:41:15 +00:00
bool core_rpc_server : : on_sync_info ( const COMMAND_RPC_SYNC_INFO : : request & req , COMMAND_RPC_SYNC_INFO : : response & res , epee : : json_rpc : : error & error_resp )
{
if ( ! check_core_busy ( ) )
{
error_resp . code = CORE_RPC_ERROR_CODE_CORE_BUSY ;
error_resp . message = " Core is busy. " ;
return false ;
}
crypto : : hash top_hash ;
2017-09-09 11:06:24 +00:00
m_core . get_blockchain_top ( res . height , top_hash ) ;
2017-07-02 21:41:15 +00:00
+ + res . height ; // turn top block height into blockchain height
res . target_height = m_core . get_target_blockchain_height ( ) ;
for ( const auto & c : m_p2p . get_payload_object ( ) . get_connections ( ) )
res . peers . push_back ( { c } ) ;
const cryptonote : : block_queue & block_queue = m_p2p . get_payload_object ( ) . get_block_queue ( ) ;
block_queue . foreach ( [ & ] ( const cryptonote : : block_queue : : span & span ) {
uint32_t speed = ( uint32_t ) ( 100.0f * block_queue . get_speed ( span . connection_id ) + 0.5f ) ;
std : : string address = " " ;
for ( const auto & c : m_p2p . get_payload_object ( ) . get_connections ( ) )
if ( c . connection_id = = span . connection_id )
address = c . address ;
res . spans . push_back ( { span . start_block_height , span . nblocks , span . connection_id , ( uint32_t ) ( span . rate + 0.5f ) , speed , span . size , address } ) ;
return true ;
} ) ;
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2017-08-26 15:23:31 +00:00
bool core_rpc_server : : on_get_txpool_backlog ( const COMMAND_RPC_GET_TRANSACTION_POOL_BACKLOG : : request & req , COMMAND_RPC_GET_TRANSACTION_POOL_BACKLOG : : response & res , epee : : json_rpc : : error & error_resp )
{
if ( ! check_core_busy ( ) )
{
error_resp . code = CORE_RPC_ERROR_CODE_CORE_BUSY ;
error_resp . message = " Core is busy. " ;
return false ;
}
if ( ! m_core . get_txpool_backlog ( res . backlog ) )
{
error_resp . code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR ;
error_resp . message = " Failed to get txpool backlog " ;
return false ;
}
res . status = CORE_RPC_STATUS_OK ;
return true ;
}
//------------------------------------------------------------------------------------------------------------------------------
2015-01-29 22:10:53 +00:00
const command_line : : arg_descriptor < std : : string > core_rpc_server : : arg_rpc_bind_port = {
" rpc-bind-port "
, " Port for RPC server "
, std : : to_string ( config : : RPC_DEFAULT_PORT )
} ;
const command_line : : arg_descriptor < std : : string > core_rpc_server : : arg_testnet_rpc_bind_port = {
" testnet-rpc-bind-port "
, " Port for testnet RPC server "
, std : : to_string ( config : : testnet : : RPC_DEFAULT_PORT )
} ;
2015-11-27 18:24:29 +00:00
const command_line : : arg_descriptor < bool > core_rpc_server : : arg_restricted_rpc = {
" restricted-rpc "
2017-11-08 12:06:41 +00:00
, " Restrict RPC to view only commands and do not return privacy sensitive data in RPC calls "
2015-11-27 18:24:29 +00:00
, false
} ;
2015-01-29 22:10:53 +00:00
} // namespace cryptonote