Skip to content

Commit

Permalink
/get_output_distribution.bin
Browse files Browse the repository at this point in the history
  • Loading branch information
hinto-janai committed Dec 17, 2024
1 parent 8cd319c commit 3664e2f
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 57 deletions.
26 changes: 14 additions & 12 deletions binaries/cuprated/src/rpc/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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(
Expand Down Expand Up @@ -179,14 +184,12 @@ async fn get_hashes(

/// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L959-L977>
async fn get_output_indexes(
state: CupratedRpcHandler,
mut state: CupratedRpcHandler,
request: GetOutputIndexesRequest,
) -> Result<GetOutputIndexesResponse, Error> {
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?,
})
}

Expand All @@ -200,12 +203,14 @@ async fn get_outs(

/// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L1689-L1711>
async fn get_transaction_pool_hashes(
state: CupratedRpcHandler,
request: GetTransactionPoolHashesRequest,
mut state: CupratedRpcHandler,
_: GetTransactionPoolHashesRequest,
) -> Result<GetTransactionPoolHashesResponse, Error> {
Ok(GetTransactionPoolHashesResponse {
base: helper::access_response_base(false),
..todo!()
tx_hashes: shared::get_transaction_pool_hashes(state)
.await
.map(ByteArrayVec::from)?,
})
}

Expand All @@ -214,8 +219,5 @@ async fn get_output_distribution(
state: CupratedRpcHandler,
request: GetOutputDistributionRequest,
) -> Result<GetOutputDistributionResponse, Error> {
Ok(GetOutputDistributionResponse {
base: helper::access_response_base(false),
..todo!()
})
shared::get_output_distribution(state, request).await
}
31 changes: 3 additions & 28 deletions binaries/cuprated/src/rpc/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ use crate::{
rpc::{
helper,
request::{address_book, blockchain, blockchain_context, blockchain_manager, txpool},
CupratedRpcHandler,
shared, CupratedRpcHandler,
},
statics::START_INSTANT_UNIX,
};
Expand Down Expand Up @@ -958,35 +958,10 @@ async fn get_transaction_pool_backlog(

/// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L3352-L3398>
async fn get_output_distribution(
mut state: CupratedRpcHandler,
state: CupratedRpcHandler,
request: GetOutputDistributionRequest,
) -> Result<GetOutputDistributionResponse, Error> {
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<Distribution, Error> {
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::<Result<Vec<Distribution>, _>>()?;

Ok(GetOutputDistributionResponse {
base: helper::access_response_base(false),
distributions,
})
shared::get_output_distribution(state, request).await
}

/// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L1998-L2033>
Expand Down
17 changes: 5 additions & 12 deletions binaries/cuprated/src/rpc/other.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,20 +651,13 @@ async fn get_transaction_pool_hashes(
mut state: CupratedRpcHandler,
_: GetTransactionPoolHashesRequest,
) -> Result<GetTransactionPoolHashesResponse, 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.
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(),
})
}

Expand Down
2 changes: 1 addition & 1 deletion binaries/cuprated/src/rpc/request/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!();
Expand Down
75 changes: 71 additions & 4 deletions binaries/cuprated/src/rpc/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

/// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L912-L957>
///
Expand Down Expand Up @@ -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),
Expand All @@ -63,3 +71,62 @@ pub(super) async fn get_outs(
outs,
})
}

/// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L1713-L1739>
///
/// 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<Vec<[u8; 32]>, 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())
}

/// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L3352-L3398>
///
/// 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<GetOutputDistributionResponse, Error> {
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<Distribution, Error> {
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::<Result<Vec<Distribution>, _>>()?;

Ok(GetOutputDistributionResponse {
base: helper::access_response_base(false),
distributions,
})
}

0 comments on commit 3664e2f

Please sign in to comment.