unsupported requests, get_outs

This commit is contained in:
hinto.janai 2024-09-10 20:47:33 -04:00
parent 0ab7fe1c46
commit 3b36c10231
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
2 changed files with 73 additions and 13 deletions

View file

@ -1,7 +1,10 @@
//! These are convenience functions that make //! These are convenience functions that make
//! sending [`BlockchainReadRequest`] less verbose. //! sending [`BlockchainReadRequest`] less verbose.
use std::sync::Arc; use std::{
collections::{HashMap, HashSet},
sync::Arc,
};
use anyhow::{anyhow, Error}; use anyhow::{anyhow, Error};
use futures::StreamExt; use futures::StreamExt;
@ -13,7 +16,8 @@ use cuprate_helper::{
map::split_u128_into_low_high_bits, map::split_u128_into_low_high_bits,
}; };
use cuprate_types::{ use cuprate_types::{
blockchain::BlockchainReadRequest, Chain, ExtendedBlockHeader, VerifiedBlockInformation, blockchain::BlockchainReadRequest, Chain, ExtendedBlockHeader, OutputOnChain,
VerifiedBlockInformation,
}; };
use crate::rpc::{CupratedRpcHandlerState, RESTRICTED_BLOCK_COUNT, RESTRICTED_BLOCK_HEADER_RANGE}; use crate::rpc::{CupratedRpcHandlerState, RESTRICTED_BLOCK_COUNT, RESTRICTED_BLOCK_HEADER_RANGE};
@ -148,6 +152,24 @@ pub(super) async fn key_image_spent(
Ok(is_spent) Ok(is_spent)
} }
/// [`BlockchainResponse::Outputs`]
pub(super) async fn outputs(
state: &mut CupratedRpcHandlerState,
outputs: HashMap<u64, HashSet<u64>>,
) -> Result<HashMap<u64, HashMap<u64, OutputOnChain>>, Error> {
let BlockchainResponse::Outputs(outputs) = state
.blockchain
.ready()
.await?
.call(BlockchainReadRequest::Outputs(outputs))
.await?
else {
unreachable!();
};
Ok(outputs)
}
// FindBlock([u8; 32]), // FindBlock([u8; 32]),
// FilterUnknownHashes(HashSet<[u8; 32]>), // FilterUnknownHashes(HashSet<[u8; 32]>),
// BlockExtendedHeaderInRange(Range<usize>, Chain), // BlockExtendedHeaderInRange(Range<usize>, Chain),

View file

@ -1,8 +1,11 @@
use std::collections::{HashMap, HashSet};
use anyhow::{anyhow, Error}; use anyhow::{anyhow, Error};
use cuprate_helper::cast::usize_to_u64;
use cuprate_rpc_types::{ use cuprate_rpc_types::{
base::{AccessResponseBase, ResponseBase}, base::{AccessResponseBase, ResponseBase},
misc::{KeyImageSpentStatus, Status}, misc::{KeyImageSpentStatus, OutKey, Status},
other::{ other::{
GetAltBlocksHashesRequest, GetAltBlocksHashesResponse, GetHeightRequest, GetHeightResponse, GetAltBlocksHashesRequest, GetAltBlocksHashesResponse, GetHeightRequest, GetHeightResponse,
GetLimitRequest, GetLimitResponse, GetNetStatsRequest, GetNetStatsResponse, GetOutsRequest, GetLimitRequest, GetLimitResponse, GetNetStatsRequest, GetNetStatsResponse, GetOutsRequest,
@ -27,7 +30,7 @@ use crate::{
rpc::{blockchain, helper}, rpc::{blockchain, helper},
}; };
use super::RESTRICTED_SPENT_KEY_IMAGES_COUNT; use super::{MAX_RESTRICTED_GLOBAL_FAKE_OUTS_COUNT, RESTRICTED_SPENT_KEY_IMAGES_COUNT};
/// Map a [`OtherRequest`] to the function that will lead to a [`OtherResponse`]. /// Map a [`OtherRequest`] to the function that will lead to a [`OtherResponse`].
pub(super) async fn map_request( pub(super) async fn map_request(
@ -47,12 +50,8 @@ pub(super) async fn map_request(
Req::SendRawTransaction(r) => { Req::SendRawTransaction(r) => {
Resp::SendRawTransaction(send_raw_transaction(state, r).await?) Resp::SendRawTransaction(send_raw_transaction(state, r).await?)
} }
Req::StartMining(r) => Resp::StartMining(start_mining(state, r).await?),
Req::StopMining(r) => Resp::StopMining(stop_mining(state, r).await?),
Req::MiningStatus(r) => Resp::MiningStatus(mining_status(state, r).await?),
Req::SaveBc(r) => Resp::SaveBc(save_bc(state, r).await?), Req::SaveBc(r) => Resp::SaveBc(save_bc(state, r).await?),
Req::GetPeerList(r) => Resp::GetPeerList(get_peer_list(state, r).await?), Req::GetPeerList(r) => Resp::GetPeerList(get_peer_list(state, r).await?),
Req::SetLogHashRate(r) => Resp::SetLogHashRate(set_log_hash_rate(state, r).await?),
Req::SetLogLevel(r) => Resp::SetLogLevel(set_log_level(state, r).await?), Req::SetLogLevel(r) => Resp::SetLogLevel(set_log_level(state, r).await?),
Req::SetLogCategories(r) => Resp::SetLogCategories(set_log_categories(state, r).await?), Req::SetLogCategories(r) => Resp::SetLogCategories(set_log_categories(state, r).await?),
Req::SetBootstrapDaemon(r) => { Req::SetBootstrapDaemon(r) => {
@ -77,6 +76,14 @@ pub(super) async fn map_request(
Resp::GetTransactionPoolHashes(get_transaction_pool_hashes(state, r).await?) Resp::GetTransactionPoolHashes(get_transaction_pool_hashes(state, r).await?)
} }
Req::GetPublicNodes(r) => Resp::GetPublicNodes(get_public_nodes(state, r).await?), Req::GetPublicNodes(r) => Resp::GetPublicNodes(get_public_nodes(state, r).await?),
// Unsupported requests.
Req::StartMining(_)
| Req::StopMining(_)
| Req::MiningStatus(_)
| Req::SetLogHashRate(_) => {
return Err(anyhow!("Mining RPC calls are not supported by Cuprate"))
}
}) })
} }
@ -156,7 +163,7 @@ async fn start_mining(
state: CupratedRpcHandlerState, state: CupratedRpcHandlerState,
request: StartMiningRequest, request: StartMiningRequest,
) -> Result<StartMiningResponse, Error> { ) -> Result<StartMiningResponse, Error> {
todo!(); unreachable!();
Ok(StartMiningResponse { Ok(StartMiningResponse {
base: ResponseBase::ok(), base: ResponseBase::ok(),
}) })
@ -167,7 +174,7 @@ async fn stop_mining(
state: CupratedRpcHandlerState, state: CupratedRpcHandlerState,
request: StopMiningRequest, request: StopMiningRequest,
) -> Result<StopMiningResponse, Error> { ) -> Result<StopMiningResponse, Error> {
todo!(); unreachable!();
Ok(StopMiningResponse { Ok(StopMiningResponse {
base: ResponseBase::ok(), base: ResponseBase::ok(),
}) })
@ -178,6 +185,7 @@ async fn mining_status(
state: CupratedRpcHandlerState, state: CupratedRpcHandlerState,
request: MiningStatusRequest, request: MiningStatusRequest,
) -> Result<MiningStatusResponse, Error> { ) -> Result<MiningStatusResponse, Error> {
unreachable!();
Ok(MiningStatusResponse { Ok(MiningStatusResponse {
base: ResponseBase::ok(), base: ResponseBase::ok(),
..todo!() ..todo!()
@ -211,7 +219,7 @@ async fn set_log_hash_rate(
state: CupratedRpcHandlerState, state: CupratedRpcHandlerState,
request: SetLogHashRateRequest, request: SetLogHashRateRequest,
) -> Result<SetLogHashRateResponse, Error> { ) -> Result<SetLogHashRateResponse, Error> {
todo!(); unreachable!();
Ok(SetLogHashRateResponse { Ok(SetLogHashRateResponse {
base: ResponseBase::ok(), base: ResponseBase::ok(),
}) })
@ -336,12 +344,42 @@ async fn get_net_stats(
/// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L912-L957> /// <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/rpc/core_rpc_server.cpp#L912-L957>
async fn get_outs( async fn get_outs(
state: CupratedRpcHandlerState, mut state: CupratedRpcHandlerState,
request: GetOutsRequest, request: GetOutsRequest,
) -> Result<GetOutsResponse, Error> { ) -> Result<GetOutsResponse, Error> {
if state.restricted && request.outputs.len() > MAX_RESTRICTED_GLOBAL_FAKE_OUTS_COUNT {
return Err(anyhow!("Too many outs requested"));
}
let mut outputs = HashMap::<u64, HashSet<u64>>::with_capacity(request.outputs.len());
for out in request.outputs {
outputs
.entry(out.amount)
.and_modify(|set| {
set.insert(out.index);
})
.or_insert_with(|| HashSet::from([out.index]));
}
let outs = blockchain::outputs(&mut state, outputs)
.await?
.into_iter()
.flat_map(|(amount, index_map)| {
index_map.into_iter().map(|(index, out)| OutKey {
key: todo!(),
mask: todo!(),
unlocked: todo!(),
height: usize_to_u64(out.height),
txid: todo!(),
})
})
.collect::<Vec<OutKey>>();
// TODO: check txpool
Ok(GetOutsResponse { Ok(GetOutsResponse {
base: ResponseBase::ok(), base: ResponseBase::ok(),
..todo!() outs,
}) })
} }