-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3148 from autonomys/gateway-fetch
Implement gateway RPC launch and piece getting
- Loading branch information
Showing
13 changed files
with
285 additions
and
37 deletions.
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 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 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 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 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 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,43 @@ | ||
//! RPC service configuration and launch. | ||
use clap::Parser; | ||
use jsonrpsee::server::{ServerBuilder, ServerHandle}; | ||
use std::net::{IpAddr, Ipv4Addr, SocketAddr}; | ||
use subspace_gateway_rpc::{SubspaceGatewayRpc, SubspaceGatewayRpcApiServer}; | ||
use tracing::info; | ||
|
||
/// The default gateway RPC port. | ||
pub const RPC_DEFAULT_PORT: u16 = 9955; | ||
|
||
/// Options for the RPC server. | ||
#[derive(Debug, Parser)] | ||
pub(crate) struct RpcOptions<const DEFAULT_PORT: u16> { | ||
/// IP and port (TCP) on which to listen for RPC requests. | ||
/// | ||
/// This RPC method is not safe to be exposed on a public IP address. | ||
#[arg(long, default_value_t = SocketAddr::new( | ||
IpAddr::V4(Ipv4Addr::LOCALHOST), | ||
DEFAULT_PORT, | ||
))] | ||
rpc_listen_on: SocketAddr, | ||
} | ||
|
||
/// Launch the RPC server `api` with the provided `options`. | ||
// TODO: | ||
// - add an argument for a custom tokio runtime | ||
// - move this RPC code into a new library part of this crate | ||
// - make a RPC config that is independent of clap | ||
pub async fn launch_rpc_server<const P: u16>( | ||
rpc_api: SubspaceGatewayRpc, | ||
rpc_options: RpcOptions<P>, | ||
) -> anyhow::Result<ServerHandle> { | ||
let server = ServerBuilder::default() | ||
.build(rpc_options.rpc_listen_on) | ||
.await?; | ||
let addr = server.local_addr()?; | ||
let server_handle = server.start(rpc_api.into_rpc()); | ||
|
||
info!(?addr, "Running JSON-RPC server"); | ||
|
||
Ok(server_handle) | ||
} |
This file contains 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 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 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,77 @@ | ||
//! An object piece getter which uses the DSN to fetch pieces. | ||
use async_trait::async_trait; | ||
use futures::stream::StreamExt; | ||
use std::fmt; | ||
use std::ops::{Deref, DerefMut}; | ||
use subspace_core_primitives::pieces::{Piece, PieceIndex}; | ||
use subspace_data_retrieval::piece_getter::{BoxError, ObjectPieceGetter}; | ||
use subspace_networking::utils::piece_provider::{PieceProvider, PieceValidator}; | ||
use subspace_networking::Node; | ||
|
||
/// Wrapper type for PieceProvider, so it can implement ObjectPieceGetter. | ||
pub struct DsnPieceGetter<PV: PieceValidator>(pub PieceProvider<PV>); | ||
|
||
impl<PV> fmt::Debug for DsnPieceGetter<PV> | ||
where | ||
PV: PieceValidator, | ||
{ | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_tuple("DsnPieceGetter") | ||
.field(&format!("{:?}", self.0)) | ||
.finish() | ||
} | ||
} | ||
|
||
impl<PV> Deref for DsnPieceGetter<PV> | ||
where | ||
PV: PieceValidator, | ||
{ | ||
type Target = PieceProvider<PV>; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl<PV> DerefMut for DsnPieceGetter<PV> | ||
where | ||
PV: PieceValidator, | ||
{ | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.0 | ||
} | ||
} | ||
|
||
// TODO: | ||
// - change ObjectPieceGetter trait to take a list of piece indexes | ||
// - move this piece getter impl into a new library part of this crate | ||
#[async_trait] | ||
impl<PV> ObjectPieceGetter for DsnPieceGetter<PV> | ||
where | ||
PV: PieceValidator, | ||
{ | ||
async fn get_piece(&self, piece_index: PieceIndex) -> Result<Option<Piece>, BoxError> { | ||
if let Some((got_piece_index, maybe_piece)) = | ||
self.get_from_cache([piece_index]).await.next().await | ||
{ | ||
assert_eq!(piece_index, got_piece_index); | ||
|
||
if let Some(piece) = maybe_piece { | ||
return Ok(Some(piece)); | ||
} | ||
} | ||
|
||
Ok(None) | ||
} | ||
} | ||
|
||
impl<PV> DsnPieceGetter<PV> | ||
where | ||
PV: PieceValidator, | ||
{ | ||
/// Creates new DSN piece getter. | ||
pub fn new(node: Node, piece_validator: PV) -> Self { | ||
Self(PieceProvider::new(node, piece_validator)) | ||
} | ||
} |
Oops, something went wrong.