Merge branch 'rpc-handler' into rpc-handler-json

This commit is contained in:
hinto.janai 2024-09-06 20:18:09 -04:00
commit 81d8d2baa6
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
17 changed files with 138 additions and 164 deletions

10
Cargo.lock generated
View file

@ -56,6 +56,12 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b"
[[package]]
name = "anyhow"
version = "1.0.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10f00e1f6e58a40e807377c75c6a7f97bf9044fab57816f2414e6f5f4499d7b8"
[[package]]
name = "async-stream"
version = "0.3.5"
@ -799,8 +805,8 @@ dependencies = [
name = "cuprate-rpc-interface"
version = "0.0.0"
dependencies = [
"anyhow",
"axum",
"cuprate-database",
"cuprate-epee-encoding",
"cuprate-helper",
"cuprate-json-rpc",
@ -810,7 +816,6 @@ dependencies = [
"paste",
"serde",
"serde_json",
"thiserror",
"tokio",
"tower",
"ureq",
@ -911,6 +916,7 @@ dependencies = [
name = "cuprated"
version = "0.1.0"
dependencies = [
"anyhow",
"async-trait",
"bitflags 2.5.0",
"borsh",

View file

@ -48,6 +48,7 @@ opt-level = 1
opt-level = 3
[workspace.dependencies]
anyhow = { version = "1.0.87", default-features = false }
async-trait = { version = "0.1.74", default-features = false }
bitflags = { version = "2.4.2", default-features = false }
borsh = { version = "1.2.1", default-features = false }

View file

@ -35,6 +35,7 @@ cuprate-rpc-interface = { path = "../../rpc/interface" }
cuprate-rpc-types = { path = "../../rpc/types" }
# TODO: after v1.0.0, remove unneeded dependencies.
anyhow = { workspace = true }
async-trait = { workspace = true }
bitflags = { workspace = true }
borsh = { workspace = true }

View file

@ -1,4 +1,5 @@
use cuprate_rpc_interface::RpcError;
use anyhow::Error;
use cuprate_rpc_types::{
bin::{
BinRequest, BinResponse, GetBlocksByHeightRequest, GetBlocksByHeightResponse,
@ -15,7 +16,7 @@ use crate::rpc::CupratedRpcHandlerState;
pub(super) async fn map_request(
state: CupratedRpcHandlerState,
request: BinRequest,
) -> Result<BinResponse, RpcError> {
) -> Result<BinResponse, Error> {
use BinRequest as Req;
use BinResponse as Resp;
@ -37,48 +38,48 @@ pub(super) async fn map_request(
async fn get_blocks(
state: CupratedRpcHandlerState,
request: GetBlocksRequest,
) -> Result<GetBlocksResponse, RpcError> {
) -> Result<GetBlocksResponse, Error> {
todo!()
}
async fn get_blocks_by_height(
state: CupratedRpcHandlerState,
request: GetBlocksByHeightRequest,
) -> Result<GetBlocksByHeightResponse, RpcError> {
) -> Result<GetBlocksByHeightResponse, Error> {
todo!()
}
async fn get_hashes(
state: CupratedRpcHandlerState,
request: GetHashesRequest,
) -> Result<GetHashesResponse, RpcError> {
) -> Result<GetHashesResponse, Error> {
todo!()
}
async fn get_output_indexes(
state: CupratedRpcHandlerState,
request: GetOutputIndexesRequest,
) -> Result<GetOutputIndexesResponse, RpcError> {
) -> Result<GetOutputIndexesResponse, Error> {
todo!()
}
async fn get_outs(
state: CupratedRpcHandlerState,
request: GetOutsRequest,
) -> Result<GetOutsResponse, RpcError> {
) -> Result<GetOutsResponse, Error> {
todo!()
}
async fn get_transaction_pool_hashes(
state: CupratedRpcHandlerState,
request: GetTransactionPoolHashesRequest,
) -> Result<GetTransactionPoolHashesResponse, RpcError> {
) -> Result<GetTransactionPoolHashesResponse, Error> {
todo!()
}
async fn get_output_distribution(
state: CupratedRpcHandlerState,
request: GetOutputDistributionRequest,
) -> Result<GetOutputDistributionResponse, RpcError> {
) -> Result<GetOutputDistributionResponse, Error> {
todo!()
}

View file

@ -2,6 +2,7 @@
use std::task::{Context, Poll};
use anyhow::Error;
use futures::{channel::oneshot::channel, future::BoxFuture};
use serde::{Deserialize, Serialize};
use tower::Service;
@ -9,7 +10,7 @@ use tower::Service;
use cuprate_blockchain::service::BlockchainReadHandle;
use cuprate_helper::asynch::InfallibleOneshotReceiver;
use cuprate_json_rpc::Id;
use cuprate_rpc_interface::{RpcError, RpcHandler};
use cuprate_rpc_interface::RpcHandler;
use cuprate_rpc_types::{
bin::{BinRequest, BinResponse},
json::{JsonRpcRequest, JsonRpcResponse},
@ -58,8 +59,8 @@ impl RpcHandler for CupratedRpcHandler {
impl Service<JsonRpcRequest> for CupratedRpcHandler {
type Response = JsonRpcResponse;
type Error = RpcError;
type Future = BoxFuture<'static, Result<JsonRpcResponse, RpcError>>;
type Error = Error;
type Future = BoxFuture<'static, Result<JsonRpcResponse, Error>>;
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
@ -73,8 +74,8 @@ impl Service<JsonRpcRequest> for CupratedRpcHandler {
impl Service<BinRequest> for CupratedRpcHandler {
type Response = BinResponse;
type Error = RpcError;
type Future = BoxFuture<'static, Result<BinResponse, RpcError>>;
type Error = Error;
type Future = BoxFuture<'static, Result<BinResponse, Error>>;
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
@ -88,8 +89,8 @@ impl Service<BinRequest> for CupratedRpcHandler {
impl Service<OtherRequest> for CupratedRpcHandler {
type Response = OtherResponse;
type Error = RpcError;
type Future = BoxFuture<'static, Result<OtherResponse, RpcError>>;
type Error = Error;
type Future = BoxFuture<'static, Result<OtherResponse, Error>>;
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))

View file

@ -1,11 +1,11 @@
use std::sync::Arc;
use anyhow::Error;
use futures::StreamExt;
use tower::{Service, ServiceExt};
use cuprate_consensus::BlockchainResponse;
use cuprate_helper::cast::{u64_to_usize, usize_to_u64};
use cuprate_rpc_interface::RpcError;
use cuprate_rpc_types::{
base::{AccessResponseBase, ResponseBase},
json::{
@ -40,7 +40,7 @@ use super::RESTRICTED_BLOCK_COUNT;
pub(super) async fn map_request(
state: CupratedRpcHandlerState,
request: JsonRpcRequest,
) -> Result<JsonRpcResponse, RpcError> {
) -> Result<JsonRpcResponse, Error> {
use JsonRpcRequest as Req;
use JsonRpcResponse as Resp;
@ -97,7 +97,7 @@ pub(super) async fn map_request(
async fn get_block_count(
state: CupratedRpcHandlerState,
request: GetBlockCountRequest,
) -> Result<GetBlockCountResponse, RpcError> {
) -> Result<GetBlockCountResponse, Error> {
let BlockchainResponse::ChainHeight(count, hash) = state
.blockchain
.oneshot(BlockchainReadRequest::ChainHeight)
@ -115,7 +115,7 @@ async fn get_block_count(
async fn on_get_block_hash(
state: CupratedRpcHandlerState,
request: OnGetBlockHashRequest,
) -> Result<OnGetBlockHashResponse, RpcError> {
) -> Result<OnGetBlockHashResponse, Error> {
let BlockchainResponse::BlockHash(hash) = state
.blockchain
.oneshot(BlockchainReadRequest::BlockHash(
@ -135,28 +135,28 @@ async fn on_get_block_hash(
async fn submit_block(
state: CupratedRpcHandlerState,
request: SubmitBlockRequest,
) -> Result<SubmitBlockResponse, RpcError> {
) -> Result<SubmitBlockResponse, Error> {
todo!()
}
async fn generate_blocks(
state: CupratedRpcHandlerState,
request: GenerateBlocksRequest,
) -> Result<GenerateBlocksResponse, RpcError> {
) -> Result<GenerateBlocksResponse, Error> {
todo!()
}
async fn get_last_block_header(
state: CupratedRpcHandlerState,
request: GetLastBlockHeaderRequest,
) -> Result<GetLastBlockHeaderResponse, RpcError> {
) -> Result<GetLastBlockHeaderResponse, Error> {
todo!()
}
async fn get_block_header_by_hash(
mut state: CupratedRpcHandlerState,
request: GetBlockHeaderByHashRequest,
) -> Result<GetBlockHeaderByHashResponse, RpcError> {
) -> Result<GetBlockHeaderByHashResponse, Error> {
let restricted = todo!();
if restricted && request.hashes.len() > RESTRICTED_BLOCK_COUNT {
let message = "Too many block headers requested in restricted mode";
@ -238,167 +238,167 @@ async fn get_block_header_by_hash(
async fn get_block_header_by_height(
state: CupratedRpcHandlerState,
request: GetBlockHeaderByHeightRequest,
) -> Result<GetBlockHeaderByHeightResponse, RpcError> {
) -> Result<GetBlockHeaderByHeightResponse, Error> {
todo!()
}
async fn get_block_headers_range(
state: CupratedRpcHandlerState,
request: GetBlockHeadersRangeRequest,
) -> Result<GetBlockHeadersRangeResponse, RpcError> {
) -> Result<GetBlockHeadersRangeResponse, Error> {
todo!()
}
async fn get_block(
state: CupratedRpcHandlerState,
request: GetBlockRequest,
) -> Result<GetBlockResponse, RpcError> {
) -> Result<GetBlockResponse, Error> {
todo!()
}
async fn get_connections(
state: CupratedRpcHandlerState,
request: GetConnectionsRequest,
) -> Result<GetConnectionsResponse, RpcError> {
) -> Result<GetConnectionsResponse, Error> {
todo!()
}
async fn get_info(
state: CupratedRpcHandlerState,
request: GetInfoRequest,
) -> Result<GetInfoResponse, RpcError> {
) -> Result<GetInfoResponse, Error> {
todo!()
}
async fn hard_fork_info(
state: CupratedRpcHandlerState,
request: HardForkInfoRequest,
) -> Result<HardForkInfoResponse, RpcError> {
) -> Result<HardForkInfoResponse, Error> {
todo!()
}
async fn set_bans(
state: CupratedRpcHandlerState,
request: SetBansRequest,
) -> Result<SetBansResponse, RpcError> {
) -> Result<SetBansResponse, Error> {
todo!()
}
async fn get_bans(
state: CupratedRpcHandlerState,
request: GetBansRequest,
) -> Result<GetBansResponse, RpcError> {
) -> Result<GetBansResponse, Error> {
todo!()
}
async fn banned(
state: CupratedRpcHandlerState,
request: BannedRequest,
) -> Result<BannedResponse, RpcError> {
) -> Result<BannedResponse, Error> {
todo!()
}
async fn flush_transaction_pool(
state: CupratedRpcHandlerState,
request: FlushTransactionPoolRequest,
) -> Result<FlushTransactionPoolResponse, RpcError> {
) -> Result<FlushTransactionPoolResponse, Error> {
todo!()
}
async fn get_output_histogram(
state: CupratedRpcHandlerState,
request: GetOutputHistogramRequest,
) -> Result<GetOutputHistogramResponse, RpcError> {
) -> Result<GetOutputHistogramResponse, Error> {
todo!()
}
async fn get_coinbase_tx_sum(
state: CupratedRpcHandlerState,
request: GetCoinbaseTxSumRequest,
) -> Result<GetCoinbaseTxSumResponse, RpcError> {
) -> Result<GetCoinbaseTxSumResponse, Error> {
todo!()
}
async fn get_version(
state: CupratedRpcHandlerState,
request: GetVersionRequest,
) -> Result<GetVersionResponse, RpcError> {
) -> Result<GetVersionResponse, Error> {
todo!()
}
async fn get_fee_estimate(
state: CupratedRpcHandlerState,
request: GetFeeEstimateRequest,
) -> Result<GetFeeEstimateResponse, RpcError> {
) -> Result<GetFeeEstimateResponse, Error> {
todo!()
}
async fn get_alternate_chains(
state: CupratedRpcHandlerState,
request: GetAlternateChainsRequest,
) -> Result<GetAlternateChainsResponse, RpcError> {
) -> Result<GetAlternateChainsResponse, Error> {
todo!()
}
async fn relay_tx(
state: CupratedRpcHandlerState,
request: RelayTxRequest,
) -> Result<RelayTxResponse, RpcError> {
) -> Result<RelayTxResponse, Error> {
todo!()
}
async fn sync_info(
state: CupratedRpcHandlerState,
request: SyncInfoRequest,
) -> Result<SyncInfoResponse, RpcError> {
) -> Result<SyncInfoResponse, Error> {
todo!()
}
async fn get_transaction_pool_backlog(
state: CupratedRpcHandlerState,
request: GetTransactionPoolBacklogRequest,
) -> Result<GetTransactionPoolBacklogResponse, RpcError> {
) -> Result<GetTransactionPoolBacklogResponse, Error> {
todo!()
}
async fn get_miner_data(
state: CupratedRpcHandlerState,
request: GetMinerDataRequest,
) -> Result<GetMinerDataResponse, RpcError> {
) -> Result<GetMinerDataResponse, Error> {
todo!()
}
async fn prune_blockchain(
state: CupratedRpcHandlerState,
request: PruneBlockchainRequest,
) -> Result<PruneBlockchainResponse, RpcError> {
) -> Result<PruneBlockchainResponse, Error> {
todo!()
}
async fn calc_pow(
state: CupratedRpcHandlerState,
request: CalcPowRequest,
) -> Result<CalcPowResponse, RpcError> {
) -> Result<CalcPowResponse, Error> {
todo!()
}
async fn flush_cache(
state: CupratedRpcHandlerState,
request: FlushCacheRequest,
) -> Result<FlushCacheResponse, RpcError> {
) -> Result<FlushCacheResponse, Error> {
todo!()
}
async fn add_aux_pow(
state: CupratedRpcHandlerState,
request: AddAuxPowRequest,
) -> Result<AddAuxPowResponse, RpcError> {
) -> Result<AddAuxPowResponse, Error> {
todo!()
}
async fn get_tx_ids_loose(
state: CupratedRpcHandlerState,
request: GetTxIdsLooseRequest,
) -> Result<GetTxIdsLooseResponse, RpcError> {
) -> Result<GetTxIdsLooseResponse, Error> {
todo!()
}

View file

@ -1,4 +1,5 @@
use cuprate_rpc_interface::RpcError;
use anyhow::Error;
use cuprate_rpc_types::other::{
GetAltBlocksHashesRequest, GetAltBlocksHashesResponse, GetHeightRequest, GetHeightResponse,
GetLimitRequest, GetLimitResponse, GetNetStatsRequest, GetNetStatsResponse, GetOutsRequest,
@ -22,7 +23,7 @@ use crate::rpc::CupratedRpcHandlerState;
pub(super) async fn map_request(
state: CupratedRpcHandlerState,
request: OtherRequest,
) -> Result<OtherResponse, RpcError> {
) -> Result<OtherResponse, Error> {
use OtherRequest as Req;
use OtherResponse as Resp;
@ -72,188 +73,188 @@ pub(super) async fn map_request(
async fn get_height(
state: CupratedRpcHandlerState,
request: GetHeightRequest,
) -> Result<GetHeightResponse, RpcError> {
) -> Result<GetHeightResponse, Error> {
todo!()
}
async fn get_transactions(
state: CupratedRpcHandlerState,
request: GetTransactionsRequest,
) -> Result<GetTransactionsResponse, RpcError> {
) -> Result<GetTransactionsResponse, Error> {
todo!()
}
async fn get_alt_blocks_hashes(
state: CupratedRpcHandlerState,
request: GetAltBlocksHashesRequest,
) -> Result<GetAltBlocksHashesResponse, RpcError> {
) -> Result<GetAltBlocksHashesResponse, Error> {
todo!()
}
async fn is_key_image_spent(
state: CupratedRpcHandlerState,
request: IsKeyImageSpentRequest,
) -> Result<IsKeyImageSpentResponse, RpcError> {
) -> Result<IsKeyImageSpentResponse, Error> {
todo!()
}
async fn send_raw_transaction(
state: CupratedRpcHandlerState,
request: SendRawTransactionRequest,
) -> Result<SendRawTransactionResponse, RpcError> {
) -> Result<SendRawTransactionResponse, Error> {
todo!()
}
async fn start_mining(
state: CupratedRpcHandlerState,
request: StartMiningRequest,
) -> Result<StartMiningResponse, RpcError> {
) -> Result<StartMiningResponse, Error> {
todo!()
}
async fn stop_mining(
state: CupratedRpcHandlerState,
request: StopMiningRequest,
) -> Result<StopMiningResponse, RpcError> {
) -> Result<StopMiningResponse, Error> {
todo!()
}
async fn mining_status(
state: CupratedRpcHandlerState,
request: MiningStatusRequest,
) -> Result<MiningStatusResponse, RpcError> {
) -> Result<MiningStatusResponse, Error> {
todo!()
}
async fn save_bc(
state: CupratedRpcHandlerState,
request: SaveBcRequest,
) -> Result<SaveBcResponse, RpcError> {
) -> Result<SaveBcResponse, Error> {
todo!()
}
async fn get_peer_list(
state: CupratedRpcHandlerState,
request: GetPeerListRequest,
) -> Result<GetPeerListResponse, RpcError> {
) -> Result<GetPeerListResponse, Error> {
todo!()
}
async fn set_log_hash_rate(
state: CupratedRpcHandlerState,
request: SetLogHashRateRequest,
) -> Result<SetLogHashRateResponse, RpcError> {
) -> Result<SetLogHashRateResponse, Error> {
todo!()
}
async fn set_log_level(
state: CupratedRpcHandlerState,
request: SetLogLevelRequest,
) -> Result<SetLogLevelResponse, RpcError> {
) -> Result<SetLogLevelResponse, Error> {
todo!()
}
async fn set_log_categories(
state: CupratedRpcHandlerState,
request: SetLogCategoriesRequest,
) -> Result<SetLogCategoriesResponse, RpcError> {
) -> Result<SetLogCategoriesResponse, Error> {
todo!()
}
async fn set_bootstrap_daemon(
state: CupratedRpcHandlerState,
request: SetBootstrapDaemonRequest,
) -> Result<SetBootstrapDaemonResponse, RpcError> {
) -> Result<SetBootstrapDaemonResponse, Error> {
todo!()
}
async fn get_transaction_pool(
state: CupratedRpcHandlerState,
request: GetTransactionPoolRequest,
) -> Result<GetTransactionPoolResponse, RpcError> {
) -> Result<GetTransactionPoolResponse, Error> {
todo!()
}
async fn get_transaction_pool_stats(
state: CupratedRpcHandlerState,
request: GetTransactionPoolStatsRequest,
) -> Result<GetTransactionPoolStatsResponse, RpcError> {
) -> Result<GetTransactionPoolStatsResponse, Error> {
todo!()
}
async fn stop_daemon(
state: CupratedRpcHandlerState,
request: StopDaemonRequest,
) -> Result<StopDaemonResponse, RpcError> {
) -> Result<StopDaemonResponse, Error> {
todo!()
}
async fn get_limit(
state: CupratedRpcHandlerState,
request: GetLimitRequest,
) -> Result<GetLimitResponse, RpcError> {
) -> Result<GetLimitResponse, Error> {
todo!()
}
async fn set_limit(
state: CupratedRpcHandlerState,
request: SetLimitRequest,
) -> Result<SetLimitResponse, RpcError> {
) -> Result<SetLimitResponse, Error> {
todo!()
}
async fn out_peers(
state: CupratedRpcHandlerState,
request: OutPeersRequest,
) -> Result<OutPeersResponse, RpcError> {
) -> Result<OutPeersResponse, Error> {
todo!()
}
async fn in_peers(
state: CupratedRpcHandlerState,
request: InPeersRequest,
) -> Result<InPeersResponse, RpcError> {
) -> Result<InPeersResponse, Error> {
todo!()
}
async fn get_net_stats(
state: CupratedRpcHandlerState,
request: GetNetStatsRequest,
) -> Result<GetNetStatsResponse, RpcError> {
) -> Result<GetNetStatsResponse, Error> {
todo!()
}
async fn get_outs(
state: CupratedRpcHandlerState,
request: GetOutsRequest,
) -> Result<GetOutsResponse, RpcError> {
) -> Result<GetOutsResponse, Error> {
todo!()
}
async fn update(
state: CupratedRpcHandlerState,
request: UpdateRequest,
) -> Result<UpdateResponse, RpcError> {
) -> Result<UpdateResponse, Error> {
todo!()
}
async fn pop_blocks(
state: CupratedRpcHandlerState,
request: PopBlocksRequest,
) -> Result<PopBlocksResponse, RpcError> {
) -> Result<PopBlocksResponse, Error> {
todo!()
}
async fn get_transaction_pool_hashes(
state: CupratedRpcHandlerState,
request: GetTransactionPoolHashesRequest,
) -> Result<GetTransactionPoolHashesResponse, RpcError> {
) -> Result<GetTransactionPoolHashesResponse, Error> {
todo!()
}
async fn get_public_nodes(
state: CupratedRpcHandlerState,
request: GetPublicNodesRequest,
) -> Result<GetPublicNodesResponse, RpcError> {
) -> Result<GetPublicNodesResponse, Error> {
todo!()
}

View file

@ -17,12 +17,11 @@ cuprate-epee-encoding = { path = "../../net/epee-encoding", default-features = f
cuprate-json-rpc = { path = "../json-rpc", default-features = false }
cuprate-rpc-types = { path = "../types", features = ["serde", "epee"], default-features = false }
cuprate-helper = { path = "../../helper", features = ["asynch"], default-features = false }
cuprate-database = { path = "../../storage/database" }
anyhow = { workspace = true }
axum = { version = "0.7.5", features = ["json"], default-features = false }
serde = { workspace = true, optional = true }
tower = { workspace = true }
thiserror = { workspace = true }
paste = { workspace = true }
futures = { workspace = true }

View file

@ -45,15 +45,16 @@ The proper usage of this crate is to:
This is your [`tower::Service`] that converts `Request`s into `Response`s,
i.e. the "inner handler".
Said concretely, `RpcHandler` is 3 `tower::Service`s where the request/response types are
the 3 endpoint enums from [`cuprate_rpc_types`] and the error type is from this crate:
Said concretely, `RpcHandler` is 3 `tower::Service`s where the
request/response types are the 3 endpoint enums from [`cuprate_rpc_types`]:
- [`JsonRpcRequest`](cuprate_rpc_types::json::JsonRpcRequest) & [`JsonRpcResponse`](cuprate_rpc_types::json::JsonRpcResponse)
- [`BinRequest`](cuprate_rpc_types::bin::BinRequest) & [`BinResponse`](cuprate_rpc_types::bin::BinRequest)
- [`OtherRequest`](cuprate_rpc_types::other::OtherRequest) & [`OtherResponse`](cuprate_rpc_types::other::OtherRequest)
- [`RpcError`]
`RpcHandler`'s [`Future`](std::future::Future) is generic, _although_,
it must output `Result<$RESPONSE, RpcError>`.
it must output `Result<$RESPONSE, anyhow::Error>`.
The error type must always be [`anyhow::Error`].
The `RpcHandler` must also hold some state that is required
for RPC server operation.

View file

@ -3,14 +3,12 @@
mod route;
mod router_builder;
mod rpc_error;
mod rpc_handler;
#[cfg(feature = "dummy")]
mod rpc_handler_dummy;
mod rpc_service;
pub use router_builder::RouterBuilder;
pub use rpc_error::RpcError;
pub use rpc_handler::RpcHandler;
#[cfg(feature = "dummy")]
pub use rpc_handler_dummy::RpcHandlerDummy;

View file

@ -5,7 +5,14 @@ use axum::{body::Bytes, extract::State, http::StatusCode};
use tower::ServiceExt;
use cuprate_epee_encoding::from_bytes;
use cuprate_rpc_types::bin::{BinRequest, BinResponse, GetTransactionPoolHashesRequest};
use cuprate_rpc_types::{
bin::{
BinRequest, BinResponse, GetBlocksByHeightRequest, GetBlocksRequest, GetHashesRequest,
GetOutputIndexesRequest, GetOutsRequest, GetTransactionPoolHashesRequest,
},
json::GetOutputDistributionRequest,
RpcCall,
};
use crate::rpc_handler::RpcHandler;
@ -66,8 +73,16 @@ macro_rules! generate_endpoints_inner {
($variant:ident, $handler:ident, $request:expr) => {
paste::paste! {
{
// Check if restricted.
if [<$variant Request>]::IS_RESTRICTED && $handler.restricted() {
// TODO: mimic `monerod` behavior.
return Err(StatusCode::FORBIDDEN);
}
// Send request.
let response = $handler.oneshot($request).await?;
let Ok(response) = $handler.oneshot($request).await else {
return Err(StatusCode::INTERNAL_SERVER_ERROR);
};
let BinResponse::$variant(response) = response else {
panic!("RPC handler returned incorrect response");

View file

@ -50,7 +50,9 @@ pub(crate) async fn json_rpc<H: RpcHandler>(
}
// Send request.
let response = handler.oneshot(request.body).await?;
let Ok(response) = handler.oneshot(request.body).await else {
return Err(StatusCode::INTERNAL_SERVER_ERROR);
};
Ok(Json(Response::ok(id, response)))
}

View file

@ -82,7 +82,9 @@ macro_rules! generate_endpoints_inner {
// Send request.
let request = OtherRequest::$variant($request);
let response = $handler.oneshot(request).await?;
let Ok(response) = $handler.oneshot(request).await else {
return Err(StatusCode::INTERNAL_SERVER_ERROR);
};
let OtherResponse::$variant(response) = response else {
panic!("RPC handler returned incorrect response")

View file

@ -1,53 +0,0 @@
//! RPC errors.
//---------------------------------------------------------------------------------------------------- Import
use axum::http::StatusCode;
use cuprate_database::RuntimeError;
//---------------------------------------------------------------------------------------------------- RpcError
/// Possible errors during RPC operation.
///
/// These are any errors that can happen _during_ a handler function.
/// I.e. if this error surfaces, it happened _after_ the request was
/// deserialized.
///
/// This is the `Error` type required to be used in an [`RpcHandler`](crate::RpcHandler).
///
/// TODO: This is empty as possible errors will be
/// enumerated when the handler functions are created.
#[derive(Debug, thiserror::Error)]
pub enum RpcError {
/// A [`std::io::Error`] from the database.
#[error("database I/O error: {0}")]
DatabaseIo(#[from] std::io::Error),
/// A (non-I/O related) database error.
#[error("database error: {0}")]
DatabaseError(RuntimeError),
}
impl From<RpcError> for StatusCode {
fn from(_: RpcError) -> Self {
// TODO
Self::INTERNAL_SERVER_ERROR
}
}
impl From<RuntimeError> for RpcError {
fn from(error: RuntimeError) -> Self {
match error {
RuntimeError::Io(io) => Self::DatabaseIo(io),
RuntimeError::KeyExists
| RuntimeError::KeyNotFound
| RuntimeError::ResizeNeeded
| RuntimeError::TableNotFound => Self::DatabaseError(error),
}
}
}
//---------------------------------------------------------------------------------------------------- Tests
#[cfg(test)]
mod test {
// use super::*;
}

View file

@ -22,7 +22,7 @@ use crate::RpcService;
/// In other words, an [`RpcHandler`] is a type that implements [`tower::Service`] 3 times,
/// one for each request/response enum type found in [`cuprate_rpc_types`].
///
/// The error type must always be [`RpcError`](crate::RpcError).
/// The error type must always be [`anyhow::Error`].
///
/// See this crate's `RpcHandlerDummy` for an implementation example of this trait.
///

View file

@ -3,19 +3,20 @@
//---------------------------------------------------------------------------------------------------- Use
use std::task::Poll;
use cuprate_rpc_types::{
bin::{BinRequest, BinResponse},
json::{JsonRpcRequest, JsonRpcResponse},
other::{OtherRequest, OtherResponse},
};
use anyhow::Error;
use futures::channel::oneshot::channel;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use tower::Service;
use cuprate_helper::asynch::InfallibleOneshotReceiver;
use cuprate_rpc_types::{
bin::{BinRequest, BinResponse},
json::{JsonRpcRequest, JsonRpcResponse},
other::{OtherRequest, OtherResponse},
};
use crate::{rpc_error::RpcError, rpc_handler::RpcHandler};
use crate::rpc_handler::RpcHandler;
//---------------------------------------------------------------------------------------------------- RpcHandlerDummy
/// An [`RpcHandler`] that always returns [`Default::default`].
@ -45,8 +46,8 @@ impl RpcHandler for RpcHandlerDummy {
impl Service<JsonRpcRequest> for RpcHandlerDummy {
type Response = JsonRpcResponse;
type Error = RpcError;
type Future = InfallibleOneshotReceiver<Result<JsonRpcResponse, RpcError>>;
type Error = Error;
type Future = InfallibleOneshotReceiver<Result<JsonRpcResponse, Error>>;
fn poll_ready(&mut self, _: &mut std::task::Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
@ -100,8 +101,8 @@ impl Service<JsonRpcRequest> for RpcHandlerDummy {
impl Service<BinRequest> for RpcHandlerDummy {
type Response = BinResponse;
type Error = RpcError;
type Future = InfallibleOneshotReceiver<Result<BinResponse, RpcError>>;
type Error = Error;
type Future = InfallibleOneshotReceiver<Result<BinResponse, Error>>;
fn poll_ready(&mut self, _: &mut std::task::Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
@ -130,8 +131,8 @@ impl Service<BinRequest> for RpcHandlerDummy {
impl Service<OtherRequest> for RpcHandlerDummy {
type Response = OtherResponse;
type Error = RpcError;
type Future = InfallibleOneshotReceiver<Result<OtherResponse, RpcError>>;
type Error = Error;
type Future = InfallibleOneshotReceiver<Result<OtherResponse, Error>>;
fn poll_ready(&mut self, _: &mut std::task::Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))

View file

@ -5,8 +5,6 @@ use std::future::Future;
use tower::Service;
use crate::rpc_error::RpcError;
//---------------------------------------------------------------------------------------------------- RpcService
/// An RPC [`tower::Service`].
///
@ -17,7 +15,7 @@ use crate::rpc_error::RpcError;
/// The `Request` and `Response` are generic and
/// are used in the [`tower::Service`] bounds.
///
/// The error type is always [`RpcError`].
/// The error type is always [`anyhow::Error`].
///
/// There is a blanket implementation that implements this
/// trait on types that implement `tower::Service` correctly.
@ -31,8 +29,8 @@ pub trait RpcService<Request, Response>:
+ Service<
Request,
Response = Response,
Error = RpcError,
Future: Future<Output = Result<Response, RpcError>> + Send + 'static,
Error = anyhow::Error,
Future: Future<Output = Result<Response, anyhow::Error>> + Send + 'static,
>
{
}
@ -45,8 +43,8 @@ impl<Request, Response, T> RpcService<Request, Response> for T where
+ Service<
Request,
Response = Response,
Error = RpcError,
Future: Future<Output = Result<Response, RpcError>> + Send + 'static,
Error = anyhow::Error,
Future: Future<Output = Result<Response, anyhow::Error>> + Send + 'static,
>
{
}