-
Notifications
You must be signed in to change notification settings - Fork 49
cuprated: initial RPC module skeleton #262
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
Merged
Merged
Changes from 19 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
b28c52a
readme
hinto-janai ac1815a
cuprated: add all workspace deps
hinto-janai d83a93f
cuprated: add lints
hinto-janai 5b0e56f
!!
hinto-janai 29587d9
Merge branch 'main' into rpc-handler
hinto-janai abfc3c5
add state, fn signatures
hinto-janai f89e29e
fixes
hinto-janai 57677a5
Merge branch 'main' into rpc-handler
hinto-janai 5f37771
error signatures
hinto-janai c57020d
interface: handle json-rpc concepts
hinto-janai 68ba5f4
split rpc calls into 3 `Service`s
hinto-janai 7487aa4
interface: extract out to `RpcService`
hinto-janai 3965349
Merge branch 'main' into rpc-handler
hinto-janai 4118f48
fix merge
hinto-janai 29afe23
remove crate lints
hinto-janai 7406afb
use `BoxFuture`
hinto-janai d268c50
rpc/interface: impl `thiserror::Error`
hinto-janai d443cfb
split state from main handler struct
hinto-janai 9f8f2c7
cleanup
hinto-janai 1b94435
fix imports
hinto-janai 7e4015a
replace `RpcError` with `anyhow::Error`
hinto-janai aac1a31
interface: update error
hinto-janai 6b16fb5
cuprated: update error type
hinto-janai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# `cuprated` | ||
TODO |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Outdated
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)) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.