Skip to content

Commit

Permalink
cuprated: internal signatures required for RPC (#297)
Browse files Browse the repository at this point in the history
* add request methods

* add p2p messages

* add txpool msgs

* add blockchain_context msgs

* add blockchain msgs

* fmt

* blockchain_manager msgs

* blockchain manager msg types

* add DB fn signatures

* add statics module

* p2p msg changes, docs

* txpool docs/types

* blockchain docs/types

* `AlternateChains`, docs

* fixes

* remove blockchain write handle, fix docs

* remove `BlockchainReadRequest::Difficulty`

* remove `BlockchainReadRequest::MinerData`

* fix p2p ban types

* `CurrentRxVm` -> `CurrentRxVms`

* storage: remove `Clone` off write handle

* Update p2p/p2p-core/src/ban.rs

Co-authored-by: Boog900 <[email protected]>

* fix merge

---------

Co-authored-by: Boog900 <[email protected]>
  • Loading branch information
hinto-janai and Boog900 authored Oct 8, 2024
1 parent 8be3698 commit 9923d8d
Show file tree
Hide file tree
Showing 37 changed files with 1,336 additions and 158 deletions.
2 changes: 1 addition & 1 deletion binaries/cuprated/src/blockchain/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub async fn init_blockchain_manager(
.ready()
.await
.expect(PANIC_CRITICAL_SERVICE_ERROR)
.call(BlockChainContextRequest::GetContext)
.call(BlockChainContextRequest::Context)
.await
.expect(PANIC_CRITICAL_SERVICE_ERROR)
else {
Expand Down
2 changes: 1 addition & 1 deletion binaries/cuprated/src/blockchain/manager/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ impl super::BlockchainManager {
.ready()
.await
.expect(PANIC_CRITICAL_SERVICE_ERROR)
.call(BlockChainContextRequest::GetContext)
.call(BlockChainContextRequest::Context)
.await
.expect(PANIC_CRITICAL_SERVICE_ERROR)
else {
Expand Down
4 changes: 2 additions & 2 deletions binaries/cuprated/src/blockchain/syncer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ where
let BlockChainContextResponse::Context(mut blockchain_ctx) = context_svc
.ready()
.await?
.call(BlockChainContextRequest::GetContext)
.call(BlockChainContextRequest::Context)
.await?
else {
unreachable!();
Expand Down Expand Up @@ -131,7 +131,7 @@ where
}

let BlockChainContextResponse::Context(ctx) = context_svc
.oneshot(BlockChainContextRequest::GetContext)
.oneshot(BlockChainContextRequest::Context)
.await?
else {
unreachable!();
Expand Down
1 change: 1 addition & 0 deletions binaries/cuprated/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![allow(
unused_imports,
unreachable_pub,
unreachable_code,
unused_crate_dependencies,
dead_code,
unused_variables,
Expand Down
3 changes: 2 additions & 1 deletion binaries/cuprated/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ mod bin;
mod handler;
mod json;
mod other;
mod request;

pub use handler::{CupratedRpcHandler, CupratedRpcHandlerState};
pub use handler::CupratedRpcHandler;
18 changes: 9 additions & 9 deletions binaries/cuprated/src/rpc/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ use cuprate_rpc_types::{
json::{GetOutputDistributionRequest, GetOutputDistributionResponse},
};

use crate::rpc::CupratedRpcHandlerState;
use crate::rpc::CupratedRpcHandler;

/// Map a [`BinRequest`] to the function that will lead to a [`BinResponse`].
pub(super) async fn map_request(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: BinRequest,
) -> Result<BinResponse, Error> {
use BinRequest as Req;
Expand All @@ -36,49 +36,49 @@ pub(super) async fn map_request(
}

async fn get_blocks(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetBlocksRequest,
) -> Result<GetBlocksResponse, Error> {
todo!()
}

async fn get_blocks_by_height(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetBlocksByHeightRequest,
) -> Result<GetBlocksByHeightResponse, Error> {
todo!()
}

async fn get_hashes(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetHashesRequest,
) -> Result<GetHashesResponse, Error> {
todo!()
}

async fn get_output_indexes(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetOutputIndexesRequest,
) -> Result<GetOutputIndexesResponse, Error> {
todo!()
}

async fn get_outs(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetOutsRequest,
) -> Result<GetOutsResponse, Error> {
todo!()
}

async fn get_transaction_pool_hashes(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetTransactionPoolHashesRequest,
) -> Result<GetTransactionPoolHashesResponse, Error> {
todo!()
}

async fn get_output_distribution(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetOutputDistributionRequest,
) -> Result<GetOutputDistributionResponse, Error> {
todo!()
Expand Down
132 changes: 106 additions & 26 deletions binaries/cuprated/src/rpc/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,131 @@
use std::task::{Context, Poll};

use anyhow::Error;
use futures::{channel::oneshot::channel, future::BoxFuture};
use serde::{Deserialize, Serialize};
use futures::future::BoxFuture;
use monero_serai::block::Block;
use tower::Service;

use cuprate_blockchain::service::BlockchainReadHandle;
use cuprate_helper::asynch::InfallibleOneshotReceiver;
use cuprate_json_rpc::Id;
use cuprate_blockchain::service::{BlockchainReadHandle, BlockchainWriteHandle};
use cuprate_rpc_interface::RpcHandler;
use cuprate_rpc_types::{
bin::{BinRequest, BinResponse},
json::{JsonRpcRequest, JsonRpcResponse},
other::{OtherRequest, OtherResponse},
};
use cuprate_txpool::service::TxpoolReadHandle;
use cuprate_txpool::service::{TxpoolReadHandle, TxpoolWriteHandle};

use crate::rpc::{bin, json, other};

/// TODO
/// TODO: use real type when public.
#[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,
#[expect(clippy::large_enum_variant)]
pub enum BlockchainManagerRequest {
/// Pop blocks off the top of the blockchain.
///
/// Input is the amount of blocks to pop.
PopBlocks { amount: usize },

/// Start pruning the blockchain.
Prune,

/// Is the blockchain pruned?
Pruned,

/// Relay a block to the network.
RelayBlock(Block),

/// Is the blockchain in the middle of syncing?
///
/// This returning `false` does not necessarily
/// mean [`BlockchainManagerRequest::Synced`] will
/// return `true`, for example, if the network has been
/// cut off and we have no peers, this will return `false`,
/// however, [`BlockchainManagerRequest::Synced`] may return
/// `true` if the latest known chain tip is equal to our height.
Syncing,

/// Is the blockchain fully synced?
Synced,

/// Current target block time.
Target,

/// The height of the next block in the chain.
TargetHeight,
}

/// TODO: use real type when public.
#[derive(Clone)]
pub enum BlockchainManagerResponse {
/// General OK response.
///
/// Response to:
/// - [`BlockchainManagerRequest::Prune`]
/// - [`BlockchainManagerRequest::RelayBlock`]
Ok,

/// Response to [`BlockchainManagerRequest::PopBlocks`]
PopBlocks { new_height: usize },

/// Response to [`BlockchainManagerRequest::Pruned`]
Pruned(bool),

/// Response to [`BlockchainManagerRequest::Syncing`]
Syncing(bool),

/// Response to [`BlockchainManagerRequest::Synced`]
Synced(bool),

/// Response to [`BlockchainManagerRequest::Target`]
Target(std::time::Duration),

/// Response to [`BlockchainManagerRequest::TargetHeight`]
TargetHeight { height: usize },
}

/// TODO: use real type when public.
pub type BlockchainManagerHandle = cuprate_database_service::DatabaseReadService<
BlockchainManagerRequest,
BlockchainManagerResponse,
>;

/// TODO
#[derive(Clone)]
pub struct CupratedRpcHandlerState {
pub struct CupratedRpcHandler {
/// Should this RPC server be [restricted](RpcHandler::restricted)?
///
/// This is not `pub` on purpose, as it should not be mutated after [`Self::new`].
restricted: bool,

/// Read handle to the blockchain database.
pub blockchain: BlockchainReadHandle,
pub blockchain_read: BlockchainReadHandle,

/// Handle to the blockchain manager.
pub blockchain_manager: BlockchainManagerHandle,

/// Read handle to the transaction pool database.
pub txpool: TxpoolReadHandle,
pub txpool_read: TxpoolReadHandle,

/// TODO: handle to txpool service.
pub txpool_manager: std::convert::Infallible,
}

impl CupratedRpcHandler {
/// TODO
pub fn init() {
todo!()
/// Create a new [`Self`].
pub const fn new(
restricted: bool,
blockchain_read: BlockchainReadHandle,
blockchain_manager: BlockchainManagerHandle,
txpool_read: TxpoolReadHandle,
txpool_manager: std::convert::Infallible,
) -> Self {
Self {
restricted,
blockchain_read,
blockchain_manager,
txpool_read,
txpool_manager,
}
}
}

Expand All @@ -67,7 +147,7 @@ impl Service<JsonRpcRequest> for CupratedRpcHandler {
}

fn call(&mut self, request: JsonRpcRequest) -> Self::Future {
let state = CupratedRpcHandlerState::clone(&self.state);
let state = self.clone();
Box::pin(json::map_request(state, request))
}
}
Expand All @@ -82,7 +162,7 @@ impl Service<BinRequest> for CupratedRpcHandler {
}

fn call(&mut self, request: BinRequest) -> Self::Future {
let state = CupratedRpcHandlerState::clone(&self.state);
let state = self.clone();
Box::pin(bin::map_request(state, request))
}
}
Expand All @@ -97,7 +177,7 @@ impl Service<OtherRequest> for CupratedRpcHandler {
}

fn call(&mut self, request: OtherRequest) -> Self::Future {
let state = CupratedRpcHandlerState::clone(&self.state);
let state = self.clone();
Box::pin(other::map_request(state, request))
}
}
Loading

0 comments on commit 9923d8d

Please sign in to comment.