Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cuprated: internal signatures required for RPC #297

Merged
merged 27 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
96099eb
add request methods
hinto-janai Sep 23, 2024
4dc3b2c
add p2p messages
hinto-janai Sep 24, 2024
e8de295
add txpool msgs
hinto-janai Sep 24, 2024
dfbdec3
add blockchain_context msgs
hinto-janai Sep 24, 2024
e8cb951
add blockchain msgs
hinto-janai Sep 25, 2024
e618bb7
fmt
hinto-janai Sep 25, 2024
a1b3bda
blockchain_manager msgs
hinto-janai Sep 25, 2024
cf30954
blockchain manager msg types
hinto-janai Sep 27, 2024
02a99f3
add DB fn signatures
hinto-janai Sep 27, 2024
6878a25
add statics module
hinto-janai Sep 27, 2024
3fb69f2
Merge branch 'main' into rpc-msgs
hinto-janai Oct 2, 2024
f9ff593
p2p msg changes, docs
hinto-janai Oct 2, 2024
41f4b78
Merge branch 'main' into rpc-msgs
hinto-janai Oct 2, 2024
cf33188
txpool docs/types
hinto-janai Oct 2, 2024
64a3e5d
blockchain docs/types
hinto-janai Oct 2, 2024
42d6f97
`AlternateChains`, docs
hinto-janai Oct 3, 2024
e7f052f
fixes
hinto-janai Oct 3, 2024
2228d4d
remove blockchain write handle, fix docs
hinto-janai Oct 4, 2024
d0888c8
remove `BlockchainReadRequest::Difficulty`
hinto-janai Oct 4, 2024
aaed13a
remove `BlockchainReadRequest::MinerData`
hinto-janai Oct 4, 2024
4f9c5f1
fix p2p ban types
hinto-janai Oct 4, 2024
c886f80
`CurrentRxVm` -> `CurrentRxVms`
hinto-janai Oct 4, 2024
4fa37de
storage: remove `Clone` off write handle
hinto-janai Oct 7, 2024
337f42f
Update p2p/p2p-core/src/ban.rs
hinto-janai Oct 8, 2024
9aa9ef5
Merge branch 'main' into rpc-msgs
hinto-janai Oct 8, 2024
0913a2b
Merge branch 'rpc-msgs' of github.com:hinto-janai/cuprate into rpc-msgs
hinto-janai Oct 8, 2024
364ce37
fix merge
hinto-janai Oct 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),
Comment on lines +36 to +37
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be add block, this also happens to be the one request I already have in the blockchain manager PR (handle_incoming_block).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it have the same semantics though? It adds a block to our local chain and broadcasts it?

The usecase is submit_block.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep


/// 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,
Boog900 marked this conversation as resolved.
Show resolved Hide resolved
}

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
Loading