split state from main handler struct

This commit is contained in:
hinto.janai 2024-09-05 18:11:27 -04:00
parent d268c50246
commit d443cfb9b7
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
5 changed files with 85 additions and 81 deletions

View file

@ -9,4 +9,4 @@ mod handler;
mod json;
mod other;
pub use handler::CupratedRpcHandler;
pub use handler::{CupratedRpcHandler, CupratedRpcHandlerState};

View file

@ -9,11 +9,11 @@ use cuprate_rpc_types::{
json::{GetOutputDistributionRequest, GetOutputDistributionResponse},
};
use crate::rpc::CupratedRpcHandler;
use crate::rpc::CupratedRpcHandlerState;
/// Map a [`BinRequest`] to the function that will lead to a [`BinResponse`].
pub(super) async fn map_request(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: BinRequest,
) -> Result<BinResponse, RpcError> {
use BinRequest as Req;
@ -35,49 +35,49 @@ pub(super) async fn map_request(
}
async fn get_blocks(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetBlocksRequest,
) -> Result<GetBlocksResponse, RpcError> {
todo!()
}
async fn get_blocks_by_height(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetBlocksByHeightRequest,
) -> Result<GetBlocksByHeightResponse, RpcError> {
todo!()
}
async fn get_hashes(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetHashesRequest,
) -> Result<GetHashesResponse, RpcError> {
todo!()
}
async fn get_output_indexes(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetOutputIndexesRequest,
) -> Result<GetOutputIndexesResponse, RpcError> {
todo!()
}
async fn get_outs(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetOutsRequest,
) -> Result<GetOutsResponse, RpcError> {
todo!()
}
async fn get_transaction_pool_hashes(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetTransactionPoolHashesRequest,
) -> Result<GetTransactionPoolHashesResponse, RpcError> {
todo!()
}
async fn get_output_distribution(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetOutputDistributionRequest,
) -> Result<GetOutputDistributionResponse, RpcError> {
todo!()

View file

@ -23,8 +23,19 @@ use crate::rpc::{bin, json, other};
#[derive(Clone)]
pub struct CupratedRpcHandler {
/// Should this RPC server be [restricted](RpcHandler::restricted)?
//
// INVARIANT:
// We don't need to include this in `state` and check for
// `self.is_restricted()` because `cuprate-rpc-interface` handles that.
pub restricted: bool,
/// State needed for request -> response mapping.
pub state: CupratedRpcHandlerState,
}
/// TODO
#[derive(Clone)]
pub struct CupratedRpcHandlerState {
/// Read handle to the blockchain database.
pub blockchain: BlockchainReadHandle,
@ -38,13 +49,6 @@ impl RpcHandler for CupratedRpcHandler {
}
}
// INVARIANT:
//
// We don't need to check for `self.is_restricted()`
// here because `cuprate-rpc-interface` handles that.
//
// We can assume the request coming has the required permissions.
impl Service<JsonRpcRequest> for CupratedRpcHandler {
type Response = JsonRpcResponse;
type Error = RpcError;
@ -55,7 +59,7 @@ impl Service<JsonRpcRequest> for CupratedRpcHandler {
}
fn call(&mut self, request: JsonRpcRequest) -> Self::Future {
let state = Self::clone(self);
let state = CupratedRpcHandlerState::clone(&self.state);
Box::pin(json::map_request(state, request))
}
}
@ -70,7 +74,7 @@ impl Service<BinRequest> for CupratedRpcHandler {
}
fn call(&mut self, request: BinRequest) -> Self::Future {
let state = Self::clone(self);
let state = CupratedRpcHandlerState::clone(&self.state);
Box::pin(bin::map_request(state, request))
}
}
@ -85,7 +89,7 @@ impl Service<OtherRequest> for CupratedRpcHandler {
}
fn call(&mut self, request: OtherRequest) -> Self::Future {
let state = Self::clone(self);
let state = CupratedRpcHandlerState::clone(&self.state);
Box::pin(other::map_request(state, request))
}
}

View file

@ -22,11 +22,11 @@ use cuprate_rpc_types::json::{
};
use tower::ServiceExt;
use crate::rpc::CupratedRpcHandler;
use crate::rpc::CupratedRpcHandlerState;
/// Map a [`JsonRpcRequest`] to the function that will lead to a [`JsonRpcResponse`].
pub(super) async fn map_request(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: JsonRpcRequest,
) -> Result<JsonRpcResponse, RpcError> {
use JsonRpcRequest as Req;
@ -83,210 +83,210 @@ pub(super) async fn map_request(
}
async fn get_block_count(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetBlockCountRequest,
) -> Result<GetBlockCountResponse, RpcError> {
todo!()
}
async fn on_get_block_hash(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: OnGetBlockHashRequest,
) -> Result<OnGetBlockHashResponse, RpcError> {
todo!()
}
async fn submit_block(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: SubmitBlockRequest,
) -> Result<SubmitBlockResponse, RpcError> {
todo!()
}
async fn generate_blocks(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GenerateBlocksRequest,
) -> Result<GenerateBlocksResponse, RpcError> {
todo!()
}
async fn get_last_block_header(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetLastBlockHeaderRequest,
) -> Result<GetLastBlockHeaderResponse, RpcError> {
todo!()
}
async fn get_block_header_by_hash(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetBlockHeaderByHashRequest,
) -> Result<GetBlockHeaderByHashResponse, RpcError> {
todo!()
}
async fn get_block_header_by_height(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetBlockHeaderByHeightRequest,
) -> Result<GetBlockHeaderByHeightResponse, RpcError> {
todo!()
}
async fn get_block_headers_range(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetBlockHeadersRangeRequest,
) -> Result<GetBlockHeadersRangeResponse, RpcError> {
todo!()
}
async fn get_block(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetBlockRequest,
) -> Result<GetBlockResponse, RpcError> {
todo!()
}
async fn get_connections(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetConnectionsRequest,
) -> Result<GetConnectionsResponse, RpcError> {
todo!()
}
async fn get_info(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetInfoRequest,
) -> Result<GetInfoResponse, RpcError> {
todo!()
}
async fn hard_fork_info(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: HardForkInfoRequest,
) -> Result<HardForkInfoResponse, RpcError> {
todo!()
}
async fn set_bans(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: SetBansRequest,
) -> Result<SetBansResponse, RpcError> {
todo!()
}
async fn get_bans(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetBansRequest,
) -> Result<GetBansResponse, RpcError> {
todo!()
}
async fn banned(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: BannedRequest,
) -> Result<BannedResponse, RpcError> {
todo!()
}
async fn flush_transaction_pool(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: FlushTransactionPoolRequest,
) -> Result<FlushTransactionPoolResponse, RpcError> {
todo!()
}
async fn get_output_histogram(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetOutputHistogramRequest,
) -> Result<GetOutputHistogramResponse, RpcError> {
todo!()
}
async fn get_coinbase_tx_sum(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetCoinbaseTxSumRequest,
) -> Result<GetCoinbaseTxSumResponse, RpcError> {
todo!()
}
async fn get_version(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetVersionRequest,
) -> Result<GetVersionResponse, RpcError> {
todo!()
}
async fn get_fee_estimate(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetFeeEstimateRequest,
) -> Result<GetFeeEstimateResponse, RpcError> {
todo!()
}
async fn get_alternate_chains(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetAlternateChainsRequest,
) -> Result<GetAlternateChainsResponse, RpcError> {
todo!()
}
async fn relay_tx(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: RelayTxRequest,
) -> Result<RelayTxResponse, RpcError> {
todo!()
}
async fn sync_info(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: SyncInfoRequest,
) -> Result<SyncInfoResponse, RpcError> {
todo!()
}
async fn get_transaction_pool_backlog(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetTransactionPoolBacklogRequest,
) -> Result<GetTransactionPoolBacklogResponse, RpcError> {
todo!()
}
async fn get_miner_data(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetMinerDataRequest,
) -> Result<GetMinerDataResponse, RpcError> {
todo!()
}
async fn prune_blockchain(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: PruneBlockchainRequest,
) -> Result<PruneBlockchainResponse, RpcError> {
todo!()
}
async fn calc_pow(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: CalcPowRequest,
) -> Result<CalcPowResponse, RpcError> {
todo!()
}
async fn flush_cache(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: FlushCacheRequest,
) -> Result<FlushCacheResponse, RpcError> {
todo!()
}
async fn add_aux_pow(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: AddAuxPowRequest,
) -> Result<AddAuxPowResponse, RpcError> {
todo!()
}
async fn get_tx_ids_loose(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetTxIdsLooseRequest,
) -> Result<GetTxIdsLooseResponse, RpcError> {
todo!()

View file

@ -16,11 +16,11 @@ use cuprate_rpc_types::other::{
StopDaemonResponse, StopMiningRequest, StopMiningResponse, UpdateRequest, UpdateResponse,
};
use crate::rpc::CupratedRpcHandler;
use crate::rpc::CupratedRpcHandlerState;
/// Map a [`OtherRequest`] to the function that will lead to a [`OtherResponse`].
pub(super) async fn map_request(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: OtherRequest,
) -> Result<OtherResponse, RpcError> {
use OtherRequest as Req;
@ -70,189 +70,189 @@ pub(super) async fn map_request(
}
async fn get_height(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetHeightRequest,
) -> Result<GetHeightResponse, RpcError> {
todo!()
}
async fn get_transactions(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetTransactionsRequest,
) -> Result<GetTransactionsResponse, RpcError> {
todo!()
}
async fn get_alt_blocks_hashes(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetAltBlocksHashesRequest,
) -> Result<GetAltBlocksHashesResponse, RpcError> {
todo!()
}
async fn is_key_image_spent(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: IsKeyImageSpentRequest,
) -> Result<IsKeyImageSpentResponse, RpcError> {
todo!()
}
async fn send_raw_transaction(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: SendRawTransactionRequest,
) -> Result<SendRawTransactionResponse, RpcError> {
todo!()
}
async fn start_mining(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: StartMiningRequest,
) -> Result<StartMiningResponse, RpcError> {
todo!()
}
async fn stop_mining(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: StopMiningRequest,
) -> Result<StopMiningResponse, RpcError> {
todo!()
}
async fn mining_status(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: MiningStatusRequest,
) -> Result<MiningStatusResponse, RpcError> {
todo!()
}
async fn save_bc(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: SaveBcRequest,
) -> Result<SaveBcResponse, RpcError> {
todo!()
}
async fn get_peer_list(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetPeerListRequest,
) -> Result<GetPeerListResponse, RpcError> {
todo!()
}
async fn set_log_hash_rate(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: SetLogHashRateRequest,
) -> Result<SetLogHashRateResponse, RpcError> {
todo!()
}
async fn set_log_level(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: SetLogLevelRequest,
) -> Result<SetLogLevelResponse, RpcError> {
todo!()
}
async fn set_log_categories(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: SetLogCategoriesRequest,
) -> Result<SetLogCategoriesResponse, RpcError> {
todo!()
}
async fn set_bootstrap_daemon(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: SetBootstrapDaemonRequest,
) -> Result<SetBootstrapDaemonResponse, RpcError> {
todo!()
}
async fn get_transaction_pool(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetTransactionPoolRequest,
) -> Result<GetTransactionPoolResponse, RpcError> {
todo!()
}
async fn get_transaction_pool_stats(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetTransactionPoolStatsRequest,
) -> Result<GetTransactionPoolStatsResponse, RpcError> {
todo!()
}
async fn stop_daemon(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: StopDaemonRequest,
) -> Result<StopDaemonResponse, RpcError> {
todo!()
}
async fn get_limit(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetLimitRequest,
) -> Result<GetLimitResponse, RpcError> {
todo!()
}
async fn set_limit(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: SetLimitRequest,
) -> Result<SetLimitResponse, RpcError> {
todo!()
}
async fn out_peers(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: OutPeersRequest,
) -> Result<OutPeersResponse, RpcError> {
todo!()
}
async fn in_peers(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: InPeersRequest,
) -> Result<InPeersResponse, RpcError> {
todo!()
}
async fn get_net_stats(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetNetStatsRequest,
) -> Result<GetNetStatsResponse, RpcError> {
todo!()
}
async fn get_outs(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetOutsRequest,
) -> Result<GetOutsResponse, RpcError> {
todo!()
}
async fn update(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: UpdateRequest,
) -> Result<UpdateResponse, RpcError> {
todo!()
}
async fn pop_blocks(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: PopBlocksRequest,
) -> Result<PopBlocksResponse, RpcError> {
todo!()
}
async fn get_transaction_pool_hashes(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetTransactionPoolHashesRequest,
) -> Result<GetTransactionPoolHashesResponse, RpcError> {
todo!()
}
async fn get_public_nodes(
state: CupratedRpcHandler,
state: CupratedRpcHandlerState,
request: GetPublicNodesRequest,
) -> Result<GetPublicNodesResponse, RpcError> {
todo!()