From 5f377717295173cf8578a5fad562af49b36482ee Mon Sep 17 00:00:00 2001 From: "hinto.janai" Date: Mon, 2 Sep 2024 18:08:52 -0400 Subject: [PATCH] error signatures --- binaries/cuprated/src/rpc.rs | 2 + binaries/cuprated/src/rpc/bin.rs | 73 +++++---- binaries/cuprated/src/rpc/handler.rs | 35 ++--- binaries/cuprated/src/rpc/json.rs | 197 ++++++++++++++----------- binaries/cuprated/src/rpc/other.rs | 196 +++++++++++++----------- rpc/interface/src/route/json_rpc.rs | 4 +- rpc/interface/src/rpc_handler_dummy.rs | 4 +- rpc/interface/src/rpc_request.rs | 2 +- rpc/interface/src/rpc_response.rs | 2 +- 9 files changed, 294 insertions(+), 221 deletions(-) diff --git a/binaries/cuprated/src/rpc.rs b/binaries/cuprated/src/rpc.rs index efbe72e..55051e1 100644 --- a/binaries/cuprated/src/rpc.rs +++ b/binaries/cuprated/src/rpc.rs @@ -2,6 +2,8 @@ //! //! Will contain the code to initiate the RPC and a request handler. +#![allow(clippy::needless_pass_by_value)] // TODO: remove after impl. + mod bin; mod handler; mod json; diff --git a/binaries/cuprated/src/rpc/bin.rs b/binaries/cuprated/src/rpc/bin.rs index 547848f..a03808d 100644 --- a/binaries/cuprated/src/rpc/bin.rs +++ b/binaries/cuprated/src/rpc/bin.rs @@ -1,68 +1,83 @@ -use cuprate_rpc_types::bin::{ - GetBlocksByHeightRequest, GetBlocksByHeightResponse, GetBlocksRequest, GetBlocksResponse, - GetHashesRequest, GetHashesResponse, GetOutputDistributionRequest, - GetOutputDistributionResponse, GetOutputIndexesRequest, GetOutputIndexesResponse, - GetOutsRequest, GetOutsResponse, GetTransactionPoolHashesRequest, - GetTransactionPoolHashesResponse, +use cuprate_rpc_interface::{RpcError, RpcResponse}; +use cuprate_rpc_types::{ + bin::{ + BinRequest, BinResponse, GetBlocksByHeightRequest, GetBlocksByHeightResponse, + GetBlocksRequest, GetBlocksResponse, GetHashesRequest, GetHashesResponse, + GetOutputIndexesRequest, GetOutputIndexesResponse, GetOutsRequest, GetOutsResponse, + GetTransactionPoolHashesRequest, GetTransactionPoolHashesResponse, + }, + json::{GetOutputDistributionRequest, GetOutputDistributionResponse}, }; use crate::rpc::CupratedRpcHandler; -pub(super) async fn map_request(state: CupratedRpcHandler, request: BinRpcRequest) -> BinRpcResponse { - use BinRpcRequest as Req; - use BinRpcResponse as Resp; +pub(super) fn map_request( + state: CupratedRpcHandler, + request: BinRequest, +) -> Result { + use BinRequest as Req; + use BinResponse as Resp; - match request { - Req::GetBlocks(r) => Resp::GetBlocks(get_blocks(state, r)), - Req::GetBlocksByHeight(r) => Resp::GetBlocksByHeight(get_blocks_by_height(state, r)), - Req::GetHashes(r) => Resp::GetHashes(get_hashes(state, r)), - Req::GetOutputIndexes(r) => Resp::GetOutputIndexes(get_output_indexes(state, r)), - Req::GetOuts(r) => Resp::GetOuts(get_outs(state, r)), + Ok(match request { + Req::GetBlocks(r) => Resp::GetBlocks(get_blocks(state, r)?), + Req::GetBlocksByHeight(r) => Resp::GetBlocksByHeight(get_blocks_by_height(state, r)?), + Req::GetHashes(r) => Resp::GetHashes(get_hashes(state, r)?), + Req::GetOutputIndexes(r) => Resp::GetOutputIndexes(get_output_indexes(state, r)?), + Req::GetOuts(r) => Resp::GetOuts(get_outs(state, r)?), Req::GetTransactionPoolHashes(r) => { - Resp::GetTransactionPoolHashes(get_transaction_pool_hashes(state, r)) + Resp::GetTransactionPoolHashes(get_transaction_pool_hashes(state, r)?) } Req::GetOutputDistribution(r) => { - Resp::GetOutputDistribution(get_output_distribution(state, r)) + Resp::GetOutputDistribution(get_output_distribution(state, r)?) } - } + }) } -async fn get_blocks(state: CupratedRpcHandler, request: GetBlocksRequest) -> GetBlocksResponse { +fn get_blocks( + state: CupratedRpcHandler, + request: GetBlocksRequest, +) -> Result { todo!() } -async fn get_blocks_by_height( +fn get_blocks_by_height( state: CupratedRpcHandler, request: GetBlocksByHeightRequest, -) -> GetBlocksByHeightResponse { +) -> Result { todo!() } -async fn get_hashes(state: CupratedRpcHandler, request: GetHashesRequest) -> GetHashesResponse { +fn get_hashes( + state: CupratedRpcHandler, + request: GetHashesRequest, +) -> Result { todo!() } -async fn get_output_indexes( +fn get_output_indexes( state: CupratedRpcHandler, request: GetOutputIndexesRequest, -) -> GetOutputIndexesResponse { +) -> Result { todo!() } -async fn get_outs(state: CupratedRpcHandler, request: GetOutsRequest) -> GetOutsResponse { +fn get_outs( + state: CupratedRpcHandler, + request: GetOutsRequest, +) -> Result { todo!() } -async fn get_transaction_pool_hashes( +fn get_transaction_pool_hashes( state: CupratedRpcHandler, request: GetTransactionPoolHashesRequest, -) -> GetTransactionPoolHashesResponse { +) -> Result { todo!() } -async fn get_output_distribution( +fn get_output_distribution( state: CupratedRpcHandler, request: GetOutputDistributionRequest, -) -> GetOutputDistributionResponse { +) -> Result { todo!() } diff --git a/binaries/cuprated/src/rpc/handler.rs b/binaries/cuprated/src/rpc/handler.rs index 760c3db..86897f1 100644 --- a/binaries/cuprated/src/rpc/handler.rs +++ b/binaries/cuprated/src/rpc/handler.rs @@ -7,34 +7,26 @@ use futures::channel::oneshot::channel; use serde::{Deserialize, Serialize}; use tower::Service; -use cuprate_blockchain::service::{BlockchainReadHandle, BlockchainWriteHandle}; +use cuprate_blockchain::service::BlockchainReadHandle; use cuprate_helper::asynch::InfallibleOneshotReceiver; use cuprate_json_rpc::Id; use cuprate_rpc_interface::{RpcError, RpcHandler, RpcRequest, RpcResponse}; -use cuprate_txpool::service::{TxpoolReadHandle, TxpoolWriteHandle}; +use cuprate_txpool::service::TxpoolReadHandle; use crate::rpc::{bin, json, other}; //---------------------------------------------------------------------------------------------------- CupratedRpcHandler /// TODO -#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)] +#[derive(Clone)] pub struct CupratedRpcHandler { /// Should this RPC server be [restricted](RpcHandler::restricted)? pub restricted: bool, /// Read handle to the blockchain database. - pub blockchain_read: BlockchainReadHandle, - /// Write handle to the blockchain database. - pub blockchain_write: BlockchainWriteHandle, - /// Direct handle to the blockchain database. - pub blockchain_db: Arc, + pub blockchain: BlockchainReadHandle, /// Read handle to the transaction pool database. - pub txpool_read: TxpoolReadHandle, - /// Write handle to the transaction pool database. - pub txpool_write: TxpoolWriteHandle, - /// Direct handle to the transaction pool database. - pub txpool_db: Arc, + pub txpool: TxpoolReadHandle, } //---------------------------------------------------------------------------------------------------- RpcHandler Impl @@ -60,16 +52,19 @@ impl Service for CupratedRpcHandler { /// /// We can assume the request coming has the required permissions. fn call(&mut self, req: RpcRequest) -> Self::Future { - let state = CupratedRpcHandler::clone(self); + let state = Self::clone(self); let resp = match req { - RpcRequest::JsonRpc(r) => json::map_request(state, r), // JSON-RPC 2.0 requests. - RpcRequest::Binary(r) => bin::map_request(state, r), // Binary requests. - RpcRequest::Other(o) => other::map_request(state, r), // JSON (but not JSON-RPC) requests. + RpcRequest::JsonRpc(r) => { + RpcResponse::JsonRpc(json::map_request(state, r).expect("TODO")) + } // JSON-RPC 2.0 requests. + RpcRequest::Binary(r) => RpcResponse::Binary(bin::map_request(state, r).expect("TODO")), // Binary requests. + RpcRequest::Other(r) => RpcResponse::Other(other::map_request(state, r).expect("TODO")), // JSON (but not JSON-RPC) requests. }; - let (tx, rx) = channel(); - drop(tx.send(Ok(resp))); - InfallibleOneshotReceiver::from(rx) + todo!() + // let (tx, rx) = channel(); + // drop(tx.send(Ok(resp))); + // InfallibleOneshotReceiver::from(rx) } } diff --git a/binaries/cuprated/src/rpc/json.rs b/binaries/cuprated/src/rpc/json.rs index 14cbd25..70a9e49 100644 --- a/binaries/cuprated/src/rpc/json.rs +++ b/binaries/cuprated/src/rpc/json.rs @@ -1,5 +1,6 @@ use std::sync::Arc; +use cuprate_rpc_interface::{RpcError, RpcResponse}; use cuprate_rpc_types::json::{ AddAuxPowRequest, AddAuxPowResponse, BannedRequest, BannedResponse, CalcPowRequest, CalcPowResponse, FlushCacheRequest, FlushCacheResponse, FlushTransactionPoolRequest, @@ -22,230 +23,260 @@ use cuprate_rpc_types::json::{ use crate::rpc::CupratedRpcHandler; -pub(super) async fn map_request( +pub(super) fn map_request( state: CupratedRpcHandler, request: JsonRpcRequest, -) -> JsonRpcResponse { +) -> Result { use JsonRpcRequest as Req; use JsonRpcResponse as Resp; - match request { - Req::GetBlockCount(r) => Resp::GetBlockCount(get_block_count(state, r)), - Req::OnGetBlockHash(r) => Resp::OnGetBlockHash(on_get_block_hash(state, r)), - Req::SubmitBlock(r) => Resp::SubmitBlock(submit_block(state, r)), - Req::GenerateBlocks(r) => Resp::GenerateBlocks(generate_blocks(state, r)), - Req::GetLastBlockHeader(r) => Resp::GetLastBlockHeader(get_last_block_header(state, r)), + Ok(match request { + Req::GetBlockCount(r) => Resp::GetBlockCount(get_block_count(state, r)?), + Req::OnGetBlockHash(r) => Resp::OnGetBlockHash(on_get_block_hash(state, r)?), + Req::SubmitBlock(r) => Resp::SubmitBlock(submit_block(state, r)?), + Req::GenerateBlocks(r) => Resp::GenerateBlocks(generate_blocks(state, r)?), + Req::GetLastBlockHeader(r) => Resp::GetLastBlockHeader(get_last_block_header(state, r)?), Req::GetBlockHeaderByHash(r) => { - Resp::GetBlockHeaderByHash(get_block_header_by_hash(state, r)) + Resp::GetBlockHeaderByHash(get_block_header_by_hash(state, r)?) } Req::GetBlockHeaderByHeight(r) => { - Resp::GetBlockHeaderByHeight(get_block_header_by_height(state, r)) + Resp::GetBlockHeaderByHeight(get_block_header_by_height(state, r)?) } Req::GetBlockHeadersRange(r) => { - Resp::GetBlockHeadersRange(get_block_headers_range(state, r)) + Resp::GetBlockHeadersRange(get_block_headers_range(state, r)?) } - Req::GetBlock(r) => Resp::GetBlock(get_block(state, r)), - Req::GetConnections(r) => Resp::GetConnections(get_connections(state, r)), - Req::GetInfo(r) => Resp::GetInfo(get_info(state, r)), - Req::HardForkInfo(r) => Resp::HardForkInfo(hard_fork_info(state, r)), - Req::SetBans(r) => Resp::SetBans(set_bans(state, r)), - Req::GetBans(r) => Resp::GetBans(get_bans(state, r)), - Req::Banned(r) => Resp::Banned(banned(state, r)), + Req::GetBlock(r) => Resp::GetBlock(get_block(state, r)?), + Req::GetConnections(r) => Resp::GetConnections(get_connections(state, r)?), + Req::GetInfo(r) => Resp::GetInfo(get_info(state, r)?), + Req::HardForkInfo(r) => Resp::HardForkInfo(hard_fork_info(state, r)?), + Req::SetBans(r) => Resp::SetBans(set_bans(state, r)?), + Req::GetBans(r) => Resp::GetBans(get_bans(state, r)?), + Req::Banned(r) => Resp::Banned(banned(state, r)?), Req::FlushTransactionPool(r) => { - Resp::FlushTransactionPool(flush_transaction_pool(state, r)) + Resp::FlushTransactionPool(flush_transaction_pool(state, r)?) } - Req::GetOutputHistogram(r) => Resp::GetOutputHistogram(get_output_histogram(state, r)), - Req::GetCoinbaseTxSum(r) => Resp::GetCoinbaseTxSum(get_coinbase_tx_sum(state, r)), - Req::GetVersion(r) => Resp::GetVersion(get_version(state, r)), - Req::GetFeeEstimate(r) => Resp::GetFeeEstimate(get_fee_estimate(state, r)), - Req::GetAlternateChains(r) => Resp::GetAlternateChains(get_alternate_chains(state, r)), - Req::RelayTx(r) => Resp::RelayTx(relay_tx(state, r)), - Req::SyncInfo(r) => Resp::SyncInfo(sync_info(state, r)), + Req::GetOutputHistogram(r) => Resp::GetOutputHistogram(get_output_histogram(state, r)?), + Req::GetCoinbaseTxSum(r) => Resp::GetCoinbaseTxSum(get_coinbase_tx_sum(state, r)?), + Req::GetVersion(r) => Resp::GetVersion(get_version(state, r)?), + Req::GetFeeEstimate(r) => Resp::GetFeeEstimate(get_fee_estimate(state, r)?), + Req::GetAlternateChains(r) => Resp::GetAlternateChains(get_alternate_chains(state, r)?), + Req::RelayTx(r) => Resp::RelayTx(relay_tx(state, r)?), + Req::SyncInfo(r) => Resp::SyncInfo(sync_info(state, r)?), Req::GetTransactionPoolBacklog(r) => { - Resp::GetTransactionPoolBacklog(get_transaction_pool_backlog(state, r)) + Resp::GetTransactionPoolBacklog(get_transaction_pool_backlog(state, r)?) } - Req::GetMinerData(r) => Resp::GetMinerData(get_miner_data(state, r)), - Req::PruneBlockchain(r) => Resp::PruneBlockchain(prune_blockchain(state, r)), - Req::CalcPow(r) => Resp::CalcPow(calc_pow(state, r)), - Req::FlushCache(r) => Resp::FlushCache(flush_cache(state, r)), - Req::AddAuxPow(r) => Resp::AddAuxPow(add_aux_pow(state, r)), - Req::GetTxIdsLoose(r) => Resp::GetTxIdsLoose(get_tx_ids_loose(state, r)), - } + Req::GetMinerData(r) => Resp::GetMinerData(get_miner_data(state, r)?), + Req::PruneBlockchain(r) => Resp::PruneBlockchain(prune_blockchain(state, r)?), + Req::CalcPow(r) => Resp::CalcPow(calc_pow(state, r)?), + Req::FlushCache(r) => Resp::FlushCache(flush_cache(state, r)?), + Req::AddAuxPow(r) => Resp::AddAuxPow(add_aux_pow(state, r)?), + Req::GetTxIdsLoose(r) => Resp::GetTxIdsLoose(get_tx_ids_loose(state, r)?), + }) } -async fn get_block_count( +fn get_block_count( state: CupratedRpcHandler, request: GetBlockCountRequest, -) -> GetBlockCountResponse { +) -> Result { todo!() } -async fn on_get_block_hash( +fn on_get_block_hash( state: CupratedRpcHandler, request: OnGetBlockHashRequest, -) -> OnGetBlockHashResponse { +) -> Result { todo!() } -async fn submit_block( +fn submit_block( state: CupratedRpcHandler, request: SubmitBlockRequest, -) -> SubmitBlockResponse { +) -> Result { todo!() } -async fn generate_blocks( +fn generate_blocks( state: CupratedRpcHandler, request: GenerateBlocksRequest, -) -> GenerateBlocksResponse { +) -> Result { todo!() } -async fn get_last_block_header( +fn get_last_block_header( state: CupratedRpcHandler, request: GetLastBlockHeaderRequest, -) -> GetLastBlockHeaderResponse { +) -> Result { todo!() } -async fn get_block_header_by_hash( +fn get_block_header_by_hash( state: CupratedRpcHandler, request: GetBlockHeaderByHashRequest, -) -> GetBlockHeaderByHashResponse { +) -> Result { todo!() } -async fn get_block_header_by_height( +fn get_block_header_by_height( state: CupratedRpcHandler, request: GetBlockHeaderByHeightRequest, -) -> GetBlockHeaderByHeightResponse { +) -> Result { todo!() } -async fn get_block_headers_range( +fn get_block_headers_range( state: CupratedRpcHandler, request: GetBlockHeadersRangeRequest, -) -> GetBlockHeadersRangeResponse { +) -> Result { todo!() } -async fn get_block(state: CupratedRpcHandler, request: GetBlockRequest) -> GetBlockResponse { +fn get_block( + state: CupratedRpcHandler, + request: GetBlockRequest, +) -> Result { todo!() } -async fn get_connections( +fn get_connections( state: CupratedRpcHandler, request: GetConnectionsRequest, -) -> GetConnectionsResponse { +) -> Result { todo!() } -async fn get_info(state: CupratedRpcHandler, request: GetInfoRequest) -> GetInfoResponse { +fn get_info( + state: CupratedRpcHandler, + request: GetInfoRequest, +) -> Result { todo!() } -async fn hard_fork_info( +fn hard_fork_info( state: CupratedRpcHandler, request: HardForkInfoRequest, -) -> HardForkInfoResponse { +) -> Result { todo!() } -async fn set_bans(state: CupratedRpcHandler, request: SetBansRequest) -> SetBansResponse { +fn set_bans( + state: CupratedRpcHandler, + request: SetBansRequest, +) -> Result { todo!() } -async fn get_bans(state: CupratedRpcHandler, request: GetBansRequest) -> GetBansResponse { +fn get_bans( + state: CupratedRpcHandler, + request: GetBansRequest, +) -> Result { todo!() } -async fn banned(state: CupratedRpcHandler, request: BannedRequest) -> BannedResponse { +fn banned(state: CupratedRpcHandler, request: BannedRequest) -> Result { todo!() } -async fn flush_transaction_pool( +fn flush_transaction_pool( state: CupratedRpcHandler, request: FlushTransactionPoolRequest, -) -> FlushTransactionPoolResponse { +) -> Result { todo!() } -async fn get_output_histogram( +fn get_output_histogram( state: CupratedRpcHandler, request: GetOutputHistogramRequest, -) -> GetOutputHistogramResponse { +) -> Result { todo!() } -async fn get_coinbase_tx_sum( +fn get_coinbase_tx_sum( state: CupratedRpcHandler, request: GetCoinbaseTxSumRequest, -) -> GetCoinbaseTxSumResponse { +) -> Result { todo!() } -async fn get_version(state: CupratedRpcHandler, request: GetVersionRequest) -> GetVersionResponse { +fn get_version( + state: CupratedRpcHandler, + request: GetVersionRequest, +) -> Result { todo!() } -async fn get_fee_estimate( +fn get_fee_estimate( state: CupratedRpcHandler, request: GetFeeEstimateRequest, -) -> GetFeeEstimateResponse { +) -> Result { todo!() } -async fn get_alternate_chains( +fn get_alternate_chains( state: CupratedRpcHandler, request: GetAlternateChainsRequest, -) -> GetAlternateChainsResponse { +) -> Result { todo!() } -async fn relay_tx(state: CupratedRpcHandler, request: RelayTxRequest) -> RelayTxResponse { +fn relay_tx( + state: CupratedRpcHandler, + request: RelayTxRequest, +) -> Result { todo!() } -async fn sync_info(state: CupratedRpcHandler, request: SyncInfoRequest) -> SyncInfoResponse { +fn sync_info( + state: CupratedRpcHandler, + request: SyncInfoRequest, +) -> Result { todo!() } -async fn get_transaction_pool_backlog( +fn get_transaction_pool_backlog( state: CupratedRpcHandler, request: GetTransactionPoolBacklogRequest, -) -> GetTransactionPoolBacklogResponse { +) -> Result { todo!() } -async fn get_miner_data( +fn get_miner_data( state: CupratedRpcHandler, request: GetMinerDataRequest, -) -> GetMinerDataResponse { +) -> Result { todo!() } -async fn prune_blockchain( +fn prune_blockchain( state: CupratedRpcHandler, request: PruneBlockchainRequest, -) -> PruneBlockchainResponse { +) -> Result { todo!() } -async fn calc_pow(state: CupratedRpcHandler, request: CalcPowRequest) -> CalcPowResponse { +fn calc_pow( + state: CupratedRpcHandler, + request: CalcPowRequest, +) -> Result { todo!() } -async fn flush_cache(state: CupratedRpcHandler, request: FlushCacheRequest) -> FlushCacheResponse { +fn flush_cache( + state: CupratedRpcHandler, + request: FlushCacheRequest, +) -> Result { todo!() } -async fn add_aux_pow(state: CupratedRpcHandler, request: AddAuxPowRequest) -> AddAuxPowResponse { +fn add_aux_pow( + state: CupratedRpcHandler, + request: AddAuxPowRequest, +) -> Result { todo!() } -async fn get_tx_ids_loose( +fn get_tx_ids_loose( state: CupratedRpcHandler, request: GetTxIdsLooseRequest, -) -> GetTxIdsLooseResponse { +) -> Result { todo!() } diff --git a/binaries/cuprated/src/rpc/other.rs b/binaries/cuprated/src/rpc/other.rs index c570dfa..6a84c65 100644 --- a/binaries/cuprated/src/rpc/other.rs +++ b/binaries/cuprated/src/rpc/other.rs @@ -1,3 +1,4 @@ +use cuprate_rpc_interface::{RpcError, RpcResponse}; use cuprate_rpc_types::other::{ GetAltBlocksHashesRequest, GetAltBlocksHashesResponse, GetHeightRequest, GetHeightResponse, GetLimitRequest, GetLimitResponse, GetNetStatsRequest, GetNetStatsResponse, GetOutsRequest, @@ -6,211 +7,238 @@ use cuprate_rpc_types::other::{ GetTransactionPoolRequest, GetTransactionPoolResponse, GetTransactionPoolStatsRequest, GetTransactionPoolStatsResponse, GetTransactionsRequest, GetTransactionsResponse, InPeersRequest, InPeersResponse, IsKeyImageSpentRequest, IsKeyImageSpentResponse, - MiningStatusRequest, MiningStatusResponse, OutPeersRequest, OutPeersResponse, PopBlocksRequest, - PopBlocksResponse, SaveBcRequest, SaveBcResponse, SendRawTransactionRequest, - SendRawTransactionResponse, SetBootstrapDaemonRequest, SetBootstrapDaemonResponse, - SetLimitRequest, SetLimitResponse, SetLogCategoriesRequest, SetLogCategoriesResponse, - SetLogHashRateRequest, SetLogHashRateResponse, SetLogLevelRequest, SetLogLevelResponse, - StartMiningRequest, StartMiningResponse, StopDaemonRequest, StopDaemonResponse, - StopMiningRequest, StopMiningResponse, UpdateRequest, UpdateResponse, + MiningStatusRequest, MiningStatusResponse, OtherRequest, OtherResponse, OutPeersRequest, + OutPeersResponse, PopBlocksRequest, PopBlocksResponse, SaveBcRequest, SaveBcResponse, + SendRawTransactionRequest, SendRawTransactionResponse, SetBootstrapDaemonRequest, + SetBootstrapDaemonResponse, SetLimitRequest, SetLimitResponse, SetLogCategoriesRequest, + SetLogCategoriesResponse, SetLogHashRateRequest, SetLogHashRateResponse, SetLogLevelRequest, + SetLogLevelResponse, StartMiningRequest, StartMiningResponse, StopDaemonRequest, + StopDaemonResponse, StopMiningRequest, StopMiningResponse, UpdateRequest, UpdateResponse, }; use crate::rpc::CupratedRpcHandler; -pub(super) async fn map_request( +pub(super) fn map_request( state: CupratedRpcHandler, - request: OtherRpcRequest, -) -> OtherRpcResponse { - use OtherRpcRequest as Req; - use OtherRpcResponse as Resp; + request: OtherRequest, +) -> Result { + use OtherRequest as Req; + use OtherResponse as Resp; - match request { - Req::GetHeight(r) => Resp::GetHeight(get_height(state, r)), - Req::GetTransactions(r) => Resp::GetTransactions(get_transactions(state, r)), - Req::GetAltBlocksHashes(r) => Resp::GetAltBlocksHashes(get_alt_blocks_hashes(state, r)), - Req::IsKeyImageSpent(r) => Resp::IsKeyImageSpent(is_key_image_spent(state, r)), - Req::SendRawTransaction(r) => Resp::SendRawTransaction(send_raw_transaction(state, r)), - Req::StartMining(r) => Resp::StartMining(start_mining(state, r)), - Req::StopMining(r) => Resp::StopMining(stop_mining(state, r)), - Req::MiningStatus(r) => Resp::MiningStatus(mining_status(state, r)), - Req::SaveBc(r) => Resp::SaveBc(save_bc(state, r)), - Req::GetPeerList(r) => Resp::GetPeerList(get_peer_list(state, r)), - Req::SetLogHashRate(r) => Resp::SetLogHashRate(set_log_hash_rate(state, r)), - Req::SetLogLevel(r) => Resp::SetLogLevel(set_log_level(state, r)), - Req::SetLogCategories(r) => Resp::SetLogCategories(set_log_categories(state, r)), - Req::SetBootstrapDaemon(r) => Resp::SetBootstrapDaemon(set_bootstrap_daemon(state, r)), - Req::GetTransactionPool(r) => Resp::GetTransactionPool(get_transaction_pool(state, r)), + Ok(match request { + Req::GetHeight(r) => Resp::GetHeight(get_height(state, r)?), + Req::GetTransactions(r) => Resp::GetTransactions(get_transactions(state, r)?), + Req::GetAltBlocksHashes(r) => Resp::GetAltBlocksHashes(get_alt_blocks_hashes(state, r)?), + Req::IsKeyImageSpent(r) => Resp::IsKeyImageSpent(is_key_image_spent(state, r)?), + Req::SendRawTransaction(r) => Resp::SendRawTransaction(send_raw_transaction(state, r)?), + Req::StartMining(r) => Resp::StartMining(start_mining(state, r)?), + Req::StopMining(r) => Resp::StopMining(stop_mining(state, r)?), + Req::MiningStatus(r) => Resp::MiningStatus(mining_status(state, r)?), + Req::SaveBc(r) => Resp::SaveBc(save_bc(state, r)?), + Req::GetPeerList(r) => Resp::GetPeerList(get_peer_list(state, r)?), + Req::SetLogHashRate(r) => Resp::SetLogHashRate(set_log_hash_rate(state, r)?), + Req::SetLogLevel(r) => Resp::SetLogLevel(set_log_level(state, r)?), + Req::SetLogCategories(r) => Resp::SetLogCategories(set_log_categories(state, r)?), + Req::SetBootstrapDaemon(r) => Resp::SetBootstrapDaemon(set_bootstrap_daemon(state, r)?), + Req::GetTransactionPool(r) => Resp::GetTransactionPool(get_transaction_pool(state, r)?), Req::GetTransactionPoolStats(r) => { - Resp::GetTransactionPoolStats(get_transaction_pool_stats(state, r)) + Resp::GetTransactionPoolStats(get_transaction_pool_stats(state, r)?) } - Req::StopDaemon(r) => Resp::StopDaemon(stop_daemon(state, r)), - Req::GetLimit(r) => Resp::GetLimit(get_limit(state, r)), - Req::SetLimit(r) => Resp::SetLimit(set_limit(state, r)), - Req::OutPeers(r) => Resp::OutPeers(out_peers(state, r)), - Req::InPeers(r) => Resp::InPeers(in_peers(state, r)), - Req::GetNetStats(r) => Resp::GetNetStats(get_net_stats(state, r)), - Req::GetOuts(r) => Resp::GetOuts(get_outs(state, r)), - Req::Update(r) => Resp::Update(update(state, r)), - Req::PopBlocks(r) => Resp::PopBlocks(pop_blocks(state, r)), + Req::StopDaemon(r) => Resp::StopDaemon(stop_daemon(state, r)?), + Req::GetLimit(r) => Resp::GetLimit(get_limit(state, r)?), + Req::SetLimit(r) => Resp::SetLimit(set_limit(state, r)?), + Req::OutPeers(r) => Resp::OutPeers(out_peers(state, r)?), + Req::InPeers(r) => Resp::InPeers(in_peers(state, r)?), + Req::GetNetStats(r) => Resp::GetNetStats(get_net_stats(state, r)?), + Req::GetOuts(r) => Resp::GetOuts(get_outs(state, r)?), + Req::Update(r) => Resp::Update(update(state, r)?), + Req::PopBlocks(r) => Resp::PopBlocks(pop_blocks(state, r)?), Req::GetTransactionPoolHashes(r) => { - Resp::GetTransactionPoolHashes(get_transaction_pool_hashes(state, r)) + Resp::GetTransactionPoolHashes(get_transaction_pool_hashes(state, r)?) } - Req::GetPublicNodes(r) => Resp::GetPublicNodes(get_public_nodes(state, r)), - } + Req::GetPublicNodes(r) => Resp::GetPublicNodes(get_public_nodes(state, r)?), + }) } -async fn get_height(state: CupratedRpcHandler, request: GetHeightRequest) -> GetHeightResponse { +fn get_height( + state: CupratedRpcHandler, + request: GetHeightRequest, +) -> Result { todo!() } -async fn get_transactions( +fn get_transactions( state: CupratedRpcHandler, request: GetTransactionsRequest, -) -> GetTransactionsResponse { +) -> Result { todo!() } -async fn get_alt_blocks_hashes( +fn get_alt_blocks_hashes( state: CupratedRpcHandler, request: GetAltBlocksHashesRequest, -) -> GetAltBlocksHashesResponse { +) -> Result { todo!() } -async fn is_key_image_spent( +fn is_key_image_spent( state: CupratedRpcHandler, request: IsKeyImageSpentRequest, -) -> IsKeyImageSpentResponse { +) -> Result { todo!() } -async fn send_raw_transaction( +fn send_raw_transaction( state: CupratedRpcHandler, request: SendRawTransactionRequest, -) -> SendRawTransactionResponse { +) -> Result { todo!() } -async fn start_mining( +fn start_mining( state: CupratedRpcHandler, request: StartMiningRequest, -) -> StartMiningResponse { +) -> Result { todo!() } -async fn stop_mining(state: CupratedRpcHandler, request: StopMiningRequest) -> StopMiningResponse { +fn stop_mining( + state: CupratedRpcHandler, + request: StopMiningRequest, +) -> Result { todo!() } -async fn mining_status( +fn mining_status( state: CupratedRpcHandler, request: MiningStatusRequest, -) -> MiningStatusResponse { +) -> Result { todo!() } -async fn save_bc(state: CupratedRpcHandler, request: SaveBcRequest) -> SaveBcResponse { +fn save_bc(state: CupratedRpcHandler, request: SaveBcRequest) -> Result { todo!() } -async fn get_peer_list( +fn get_peer_list( state: CupratedRpcHandler, request: GetPeerListRequest, -) -> GetPeerListResponse { +) -> Result { todo!() } -async fn set_log_hash_rate( +fn set_log_hash_rate( state: CupratedRpcHandler, request: SetLogHashRateRequest, -) -> SetLogHashRateResponse { +) -> Result { todo!() } -async fn set_log_level( +fn set_log_level( state: CupratedRpcHandler, request: SetLogLevelRequest, -) -> SetLogLevelResponse { +) -> Result { todo!() } -async fn set_log_categories( +fn set_log_categories( state: CupratedRpcHandler, request: SetLogCategoriesRequest, -) -> SetLogCategoriesResponse { +) -> Result { todo!() } -async fn set_bootstrap_daemon( +fn set_bootstrap_daemon( state: CupratedRpcHandler, request: SetBootstrapDaemonRequest, -) -> SetBootstrapDaemonResponse { +) -> Result { todo!() } -async fn get_transaction_pool( +fn get_transaction_pool( state: CupratedRpcHandler, request: GetTransactionPoolRequest, -) -> GetTransactionPoolResponse { +) -> Result { todo!() } -async fn get_transaction_pool_stats( +fn get_transaction_pool_stats( state: CupratedRpcHandler, request: GetTransactionPoolStatsRequest, -) -> GetTransactionPoolStatsResponse { +) -> Result { todo!() } -async fn stop_daemon(state: CupratedRpcHandler, request: StopDaemonRequest) -> StopDaemonResponse { +fn stop_daemon( + state: CupratedRpcHandler, + request: StopDaemonRequest, +) -> Result { todo!() } -async fn get_limit(state: CupratedRpcHandler, request: GetLimitRequest) -> GetLimitResponse { +fn get_limit( + state: CupratedRpcHandler, + request: GetLimitRequest, +) -> Result { todo!() } -async fn set_limit(state: CupratedRpcHandler, request: SetLimitRequest) -> SetLimitResponse { +fn set_limit( + state: CupratedRpcHandler, + request: SetLimitRequest, +) -> Result { todo!() } -async fn out_peers(state: CupratedRpcHandler, request: OutPeersRequest) -> OutPeersResponse { +fn out_peers( + state: CupratedRpcHandler, + request: OutPeersRequest, +) -> Result { todo!() } -async fn in_peers(state: CupratedRpcHandler, request: InPeersRequest) -> InPeersResponse { +fn in_peers( + state: CupratedRpcHandler, + request: InPeersRequest, +) -> Result { todo!() } -async fn get_net_stats( +fn get_net_stats( state: CupratedRpcHandler, request: GetNetStatsRequest, -) -> GetNetStatsResponse { +) -> Result { todo!() } -async fn get_outs(state: CupratedRpcHandler, request: GetOutsRequest) -> GetOutsResponse { +fn get_outs( + state: CupratedRpcHandler, + request: GetOutsRequest, +) -> Result { todo!() } -async fn update(state: CupratedRpcHandler, request: UpdateRequest) -> UpdateResponse { +fn update(state: CupratedRpcHandler, request: UpdateRequest) -> Result { todo!() } -async fn pop_blocks(state: CupratedRpcHandler, request: PopBlocksRequest) -> PopBlocksResponse { +fn pop_blocks( + state: CupratedRpcHandler, + request: PopBlocksRequest, +) -> Result { todo!() } -async fn get_transaction_pool_hashes( +fn get_transaction_pool_hashes( state: CupratedRpcHandler, request: GetTransactionPoolHashesRequest, -) -> GetTransactionPoolHashesResponse { +) -> Result { todo!() } -async fn get_public_nodes( +fn get_public_nodes( state: CupratedRpcHandler, request: GetPublicNodesRequest, -) -> GetPublicNodesResponse { +) -> Result { todo!() } diff --git a/rpc/interface/src/route/json_rpc.rs b/rpc/interface/src/route/json_rpc.rs index bd35e43..28ab80c 100644 --- a/rpc/interface/src/route/json_rpc.rs +++ b/rpc/interface/src/route/json_rpc.rs @@ -50,7 +50,7 @@ pub(crate) async fn json_rpc( } // Send request. - let request = RpcRequest::JsonRpc(request); + let request = RpcRequest::JsonRpc(request.body); let channel = handler.oneshot(request).await?; // Assert the response from the inner handler is correct. @@ -58,6 +58,8 @@ pub(crate) async fn json_rpc( panic!("RPC handler returned incorrect response"); }; + let response = todo!(); + Ok(Json(response)) } diff --git a/rpc/interface/src/rpc_handler_dummy.rs b/rpc/interface/src/rpc_handler_dummy.rs index 73ffe9c..22c7fec 100644 --- a/rpc/interface/src/rpc_handler_dummy.rs +++ b/rpc/interface/src/rpc_handler_dummy.rs @@ -62,7 +62,7 @@ impl Service for RpcHandlerDummy { #[rustfmt::skip] #[allow(clippy::default_trait_access)] let resp = match req { - RpcRequest::JsonRpc(j) => RpcResponse::JsonRpc(cuprate_json_rpc::Response::ok(Id::Null, match j.body { + RpcRequest::JsonRpc(j) => RpcResponse::JsonRpc(match j { JReq::GetBlockCount(_) => JResp::GetBlockCount(Default::default()), JReq::OnGetBlockHash(_) => JResp::OnGetBlockHash(Default::default()), JReq::SubmitBlock(_) => JResp::SubmitBlock(Default::default()), @@ -93,7 +93,7 @@ impl Service for RpcHandlerDummy { JReq::FlushCache(_) => JResp::FlushCache(Default::default()), JReq::AddAuxPow(_) => JResp::AddAuxPow(Default::default()), JReq::GetTxIdsLoose(_) => JResp::GetTxIdsLoose(Default::default()), - })), + }), RpcRequest::Binary(b) => RpcResponse::Binary(match b { BReq::GetBlocks(_) => BResp::GetBlocks(Default::default()), BReq::GetBlocksByHeight(_) => BResp::GetBlocksByHeight(Default::default()), diff --git a/rpc/interface/src/rpc_request.rs b/rpc/interface/src/rpc_request.rs index 3b66a78..acac6dd 100644 --- a/rpc/interface/src/rpc_request.rs +++ b/rpc/interface/src/rpc_request.rs @@ -19,7 +19,7 @@ use cuprate_rpc_types::{bin::BinRequest, json::JsonRpcRequest, other::OtherReque #[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum RpcRequest { /// JSON-RPC 2.0 requests. - JsonRpc(cuprate_json_rpc::Request), + JsonRpc(JsonRpcRequest), /// Binary requests. Binary(BinRequest), /// Other JSON requests. diff --git a/rpc/interface/src/rpc_response.rs b/rpc/interface/src/rpc_response.rs index 7e8ecdb..8b61595 100644 --- a/rpc/interface/src/rpc_response.rs +++ b/rpc/interface/src/rpc_response.rs @@ -19,7 +19,7 @@ use cuprate_rpc_types::{bin::BinResponse, json::JsonRpcResponse, other::OtherRes #[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub enum RpcResponse { /// JSON RPC 2.0 responses. - JsonRpc(cuprate_json_rpc::Response), + JsonRpc(JsonRpcResponse), /// Binary responses. Binary(BinResponse), /// Other JSON responses.