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: initial RPC module skeleton #262

Merged
merged 23 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
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
72 changes: 72 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ serde_bytes = { version = "0.11.12", default-features = false }
serde_json = { version = "1.0.108", default-features = false }
serde = { version = "1.0.190", default-features = false }
thiserror = { version = "1.0.50", default-features = false }
thread_local = { version = "1.1.7", default-features = false }
thread_local = { version = "1.1.7", default-features = false }
tokio-util = { version = "0.7.10", default-features = false }
tokio-stream = { version = "0.1.14", default-features = false }
tokio = { version = "1.33.0", default-features = false }
Expand Down
62 changes: 62 additions & 0 deletions binaries/cuprated/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,68 @@ authors = ["Boog900", "hinto-janai", "SyntheticBird45"]
repository = "https://github.com/Cuprate/cuprate/tree/main/binaries/cuprated"

[dependencies]
# TODO: after v1.0.0, remove unneeded dependencies.
Boog900 marked this conversation as resolved.
Show resolved Hide resolved
cuprate-consensus = { path = "../../consensus" }
cuprate-fast-sync = { path = "../../consensus/fast-sync" }
cuprate-consensus-rules = { path = "../../consensus/rules" }
cuprate-cryptonight = { path = "../../cryptonight" }
cuprate-helper = { path = "../../helper" }
cuprate-epee-encoding = { path = "../../net/epee-encoding" }
cuprate-fixed-bytes = { path = "../../net/fixed-bytes" }
cuprate-levin = { path = "../../net/levin" }
cuprate-wire = { path = "../../net/wire" }
cuprate-p2p = { path = "../../p2p/p2p" }
cuprate-p2p-core = { path = "../../p2p/p2p-core" }
cuprate-dandelion-tower = { path = "../../p2p/dandelion-tower" }
cuprate-async-buffer = { path = "../../p2p/async-buffer" }
cuprate-address-book = { path = "../../p2p/address-book" }
cuprate-blockchain = { path = "../../storage/blockchain" }
cuprate-database-service = { path = "../../storage/service" }
cuprate-txpool = { path = "../../storage/txpool" }
cuprate-database = { path = "../../storage/database" }
cuprate-pruning = { path = "../../pruning" }
cuprate-test-utils = { path = "../../test-utils" }
cuprate-types = { path = "../../types" }
cuprate-json-rpc = { path = "../../rpc/json-rpc" }
cuprate-rpc-interface = { path = "../../rpc/interface" }
cuprate-rpc-types = { path = "../../rpc/types" }

# TODO: after v1.0.0, remove unneeded dependencies.
async-trait = { workspace = true }
bitflags = { workspace = true }
borsh = { workspace = true }
bytemuck = { workspace = true }
bytes = { workspace = true }
cfg-if = { workspace = true }
clap = { workspace = true }
chrono = { workspace = true }
crypto-bigint = { workspace = true }
crossbeam = { workspace = true }
curve25519-dalek = { workspace = true }
dashmap = { workspace = true }
dirs = { workspace = true }
futures = { workspace = true }
hex = { workspace = true }
hex-literal = { workspace = true }
indexmap = { workspace = true }
monero-serai = { workspace = true }
paste = { workspace = true }
pin-project = { workspace = true }
randomx-rs = { workspace = true }
rand = { workspace = true }
rand_distr = { workspace = true }
rayon = { workspace = true }
serde_bytes = { workspace = true }
serde_json = { workspace = true }
serde = { workspace = true }
thiserror = { workspace = true }
thread_local = { workspace = true }
tokio-util = { workspace = true }
tokio-stream = { workspace = true }
tokio = { workspace = true }
tower = { workspace = true }
tracing-subscriber = { workspace = true }
tracing = { workspace = true }

[lints]
workspace = true
2 changes: 2 additions & 0 deletions binaries/cuprated/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# `cuprated`
TODO
13 changes: 13 additions & 0 deletions binaries/cuprated/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
#![doc = include_str!("../README.md")]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![allow(
unused_imports,
unreachable_pub,
unused_crate_dependencies,
dead_code,
unused_variables,
clippy::needless_pass_by_value,
clippy::unused_async,
reason = "TODO: remove after v1.0.0"
)]
Boog900 marked this conversation as resolved.
Show resolved Hide resolved

mod blockchain;
mod config;
mod p2p;
Expand Down
7 changes: 6 additions & 1 deletion binaries/cuprated/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@
//!
//! Will contain the code to initiate the RPC and a request handler.

mod request_handler;
mod bin;
mod handler;
mod json;
mod other;

pub use handler::{CupratedRpcHandler, CupratedRpcHandlerState};
84 changes: 84 additions & 0 deletions binaries/cuprated/src/rpc/bin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use cuprate_rpc_interface::RpcError;
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::CupratedRpcHandlerState;

/// Map a [`BinRequest`] to the function that will lead to a [`BinResponse`].
pub(super) async fn map_request(
state: CupratedRpcHandlerState,
request: BinRequest,
) -> Result<BinResponse, RpcError> {
use BinRequest as Req;
use BinResponse as Resp;

Ok(match request {
Req::GetBlocks(r) => Resp::GetBlocks(get_blocks(state, r).await?),
Req::GetBlocksByHeight(r) => Resp::GetBlocksByHeight(get_blocks_by_height(state, r).await?),
Req::GetHashes(r) => Resp::GetHashes(get_hashes(state, r).await?),
Req::GetOutputIndexes(r) => Resp::GetOutputIndexes(get_output_indexes(state, r).await?),
Req::GetOuts(r) => Resp::GetOuts(get_outs(state, r).await?),
Req::GetTransactionPoolHashes(r) => {
Resp::GetTransactionPoolHashes(get_transaction_pool_hashes(state, r).await?)
}
Req::GetOutputDistribution(r) => {
Resp::GetOutputDistribution(get_output_distribution(state, r).await?)
}
})
}

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

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

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

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

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

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

async fn get_output_distribution(
state: CupratedRpcHandlerState,
request: GetOutputDistributionRequest,
) -> Result<GetOutputDistributionResponse, RpcError> {
todo!()
}
102 changes: 102 additions & 0 deletions binaries/cuprated/src/rpc/handler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//! Dummy implementation of [`RpcHandler`].

use std::task::{Context, Poll};

use cuprate_rpc_types::{
bin::{BinRequest, BinResponse},
json::{JsonRpcRequest, JsonRpcResponse},
other::{OtherRequest, OtherResponse},
};
use futures::{channel::oneshot::channel, future::BoxFuture};
use serde::{Deserialize, Serialize};
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_txpool::service::TxpoolReadHandle;
Boog900 marked this conversation as resolved.
Show resolved Hide resolved

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

/// TODO
#[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,

/// Read handle to the transaction pool database.
pub txpool: TxpoolReadHandle,
}
Boog900 marked this conversation as resolved.
Show resolved Hide resolved

impl CupratedRpcHandler {
/// TODO
pub fn init() {
todo!()
}
}

impl RpcHandler for CupratedRpcHandler {
fn restricted(&self) -> bool {
self.restricted
}
}

impl Service<JsonRpcRequest> for CupratedRpcHandler {
type Response = JsonRpcResponse;
type Error = RpcError;
type Future = BoxFuture<'static, Result<JsonRpcResponse, RpcError>>;

fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}

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

impl Service<BinRequest> for CupratedRpcHandler {
type Response = BinResponse;
type Error = RpcError;
type Future = BoxFuture<'static, Result<BinResponse, RpcError>>;

fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}

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

impl Service<OtherRequest> for CupratedRpcHandler {
type Response = OtherResponse;
type Error = RpcError;
type Future = BoxFuture<'static, Result<OtherResponse, RpcError>>;

fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}

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