diff --git a/binaries/cuprated/src/rpc/bin.rs b/binaries/cuprated/src/rpc/bin.rs index 3cd4c94..0552f91 100644 --- a/binaries/cuprated/src/rpc/bin.rs +++ b/binaries/cuprated/src/rpc/bin.rs @@ -4,6 +4,7 @@ use anyhow::{anyhow, Error}; use bytes::Bytes; use cuprate_constants::rpc::{RESTRICTED_BLOCK_COUNT, RESTRICTED_TRANSACTIONS_COUNT}; +use cuprate_fixed_bytes::ByteArrayVec; use cuprate_rpc_interface::RpcHandler; use cuprate_rpc_types::{ base::{AccessResponseBase, ResponseBase}, @@ -18,7 +19,11 @@ use cuprate_rpc_types::{ }; use cuprate_types::{rpc::PoolInfoExtent, BlockCompleteEntry}; -use crate::rpc::{helper, request::blockchain, shared, CupratedRpcHandler}; +use crate::rpc::{ + helper, + request::{blockchain, txpool}, + shared, CupratedRpcHandler, +}; /// Map a [`BinRequest`] to the function that will lead to a [`BinResponse`]. pub(super) async fn map_request( @@ -179,14 +184,12 @@ async fn get_hashes( /// async fn get_output_indexes( - state: CupratedRpcHandler, + mut state: CupratedRpcHandler, request: GetOutputIndexesRequest, ) -> Result { - let o_indexes = blockchain::tx_output_indexes(&mut state.blockchain_read, request.txid).await?; - Ok(GetOutputIndexesResponse { base: helper::access_response_base(false), - o_indexes, + o_indexes: blockchain::tx_output_indexes(&mut state.blockchain_read, request.txid).await?, }) } @@ -200,12 +203,14 @@ async fn get_outs( /// async fn get_transaction_pool_hashes( - state: CupratedRpcHandler, - request: GetTransactionPoolHashesRequest, + mut state: CupratedRpcHandler, + _: GetTransactionPoolHashesRequest, ) -> Result { Ok(GetTransactionPoolHashesResponse { base: helper::access_response_base(false), - ..todo!() + tx_hashes: shared::get_transaction_pool_hashes(state) + .await + .map(ByteArrayVec::from)?, }) } @@ -214,8 +219,5 @@ async fn get_output_distribution( state: CupratedRpcHandler, request: GetOutputDistributionRequest, ) -> Result { - Ok(GetOutputDistributionResponse { - base: helper::access_response_base(false), - ..todo!() - }) + shared::get_output_distribution(state, request).await } diff --git a/binaries/cuprated/src/rpc/json.rs b/binaries/cuprated/src/rpc/json.rs index 598cdbe..03b7c0b 100644 --- a/binaries/cuprated/src/rpc/json.rs +++ b/binaries/cuprated/src/rpc/json.rs @@ -62,7 +62,7 @@ use crate::{ rpc::{ helper, request::{address_book, blockchain, blockchain_context, blockchain_manager, txpool}, - CupratedRpcHandler, + shared, CupratedRpcHandler, }, statics::START_INSTANT_UNIX, }; @@ -958,35 +958,10 @@ async fn get_transaction_pool_backlog( /// async fn get_output_distribution( - mut state: CupratedRpcHandler, + state: CupratedRpcHandler, request: GetOutputDistributionRequest, ) -> Result { - if state.is_restricted() && request.amounts != [1, 0] { - return Err(anyhow!( - "Restricted RPC can only get output distribution for RCT outputs. Use your own node." - )); - } - - // 0 is placeholder for the whole chain - let req_to_height = if request.to_height == 0 { - helper::top_height(&mut state).await?.0.saturating_sub(1) - } else { - request.to_height - }; - - let distributions = request.amounts.into_iter().map(|amount| { - fn get_output_distribution() -> Result { - todo!("https://github.com/monero-project/monero/blob/893916ad091a92e765ce3241b94e706ad012b62a/src/rpc/rpc_handler.cpp#L29"); - Err(anyhow!("Failed to get output distribution")) - } - - get_output_distribution() - }).collect::, _>>()?; - - Ok(GetOutputDistributionResponse { - base: helper::access_response_base(false), - distributions, - }) + shared::get_output_distribution(state, request).await } /// diff --git a/binaries/cuprated/src/rpc/other.rs b/binaries/cuprated/src/rpc/other.rs index 7e74e1d..45a279c 100644 --- a/binaries/cuprated/src/rpc/other.rs +++ b/binaries/cuprated/src/rpc/other.rs @@ -651,20 +651,13 @@ async fn get_transaction_pool_hashes( mut state: CupratedRpcHandler, _: GetTransactionPoolHashesRequest, ) -> Result { - let include_sensitive_txs = !state.is_restricted(); - - // FIXME: this request is a bit overkill, we only need the hashes. - // We could create a separate request for this. - let tx_hashes = txpool::pool(&mut state.txpool_read, include_sensitive_txs) - .await? - .0 - .into_iter() - .map(|tx| tx.id_hash) - .collect(); - Ok(GetTransactionPoolHashesResponse { base: helper::response_base(false), - tx_hashes, + tx_hashes: shared::get_transaction_pool_hashes(state) + .await? + .into_iter() + .map(Hex) + .collect(), }) } diff --git a/binaries/cuprated/src/rpc/request/blockchain.rs b/binaries/cuprated/src/rpc/request/blockchain.rs index 0d9c9fd..dc0533b 100644 --- a/binaries/cuprated/src/rpc/request/blockchain.rs +++ b/binaries/cuprated/src/rpc/request/blockchain.rs @@ -438,7 +438,7 @@ pub(crate) async fn tx_output_indexes( let BlockchainResponse::TxOutputIndexes(o_indexes) = blockchain_read .ready() .await? - .call(BlockchainReadRequest::TxOutputIndexes(tx_hash)) + .call(BlockchainReadRequest::TxOutputIndexes { tx_hash }) .await? else { unreachable!(); diff --git a/binaries/cuprated/src/rpc/shared.rs b/binaries/cuprated/src/rpc/shared.rs index 624a012..48ae0f9 100644 --- a/binaries/cuprated/src/rpc/shared.rs +++ b/binaries/cuprated/src/rpc/shared.rs @@ -10,11 +10,19 @@ use cuprate_helper::cast::usize_to_u64; use cuprate_hex::Hex; use cuprate_rpc_interface::RpcHandler; use cuprate_rpc_types::{ - bin::{GetOutsRequest, GetOutsResponse}, - misc::OutKeyBin, + bin::{ + GetOutsRequest, GetOutsResponse, GetTransactionPoolHashesRequest, + GetTransactionPoolHashesResponse, + }, + json::{GetOutputDistributionRequest, GetOutputDistributionResponse}, + misc::{Distribution, OutKeyBin}, }; -use crate::rpc::{helper, request::blockchain, CupratedRpcHandler}; +use crate::rpc::{ + helper, + request::{blockchain, txpool}, + CupratedRpcHandler, +}; /// /// @@ -48,7 +56,7 @@ pub(super) async fn get_outs( .await? .into_iter() .flat_map(|(amount, index_map)| { - index_map.into_iter().map(|(_, out)| OutKeyBin { + index_map.into_values().map(|out| OutKeyBin { key: out.key.map_or([0; 32], |e| e.compress().0), mask: out.commitment.compress().0, unlocked: matches!(out.time_lock, Timelock::None), @@ -63,3 +71,62 @@ pub(super) async fn get_outs( outs, }) } + +/// +/// +/// Shared between: +/// - Other JSON's `/get_transaction_pool_hashes` +/// - Binary's `/get_transaction_pool_hashes.bin` +/// +/// Returns transaction hashes. +pub(super) async fn get_transaction_pool_hashes( + mut state: CupratedRpcHandler, +) -> Result, Error> { + let include_sensitive_txs = !state.is_restricted(); + + // FIXME: this request is a bit overkill, we only need the hashes. + // We could create a separate request for this. + Ok(txpool::pool(&mut state.txpool_read, include_sensitive_txs) + .await? + .0 + .into_iter() + .map(|tx| tx.id_hash.0) + .collect()) +} + +/// +/// +/// Shared between: +/// - JSON-RPC's `get_output_distribution` +/// - Binary's `/get_output_distribution.bin` +pub(super) async fn get_output_distribution( + mut state: CupratedRpcHandler, + request: GetOutputDistributionRequest, +) -> Result { + if state.is_restricted() && request.amounts != [1, 0] { + return Err(anyhow!( + "Restricted RPC can only get output distribution for RCT outputs. Use your own node." + )); + } + + // 0 is placeholder for the whole chain + let req_to_height = if request.to_height == 0 { + helper::top_height(&mut state).await?.0.saturating_sub(1) + } else { + request.to_height + }; + + let distributions = request.amounts.into_iter().map(|amount| { + fn get_output_distribution() -> Result { + todo!("https://github.com/monero-project/monero/blob/893916ad091a92e765ce3241b94e706ad012b62a/src/rpc/rpc_handler.cpp#L29"); + Err(anyhow!("Failed to get output distribution")) + } + + get_output_distribution() + }).collect::, _>>()?; + + Ok(GetOutputDistributionResponse { + base: helper::access_response_base(false), + distributions, + }) +}