-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
489 additions
and
685 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
mod rpc; | ||
mod types; | ||
|
||
use anyhow::Result; | ||
use jsonrpsee::server::Server; | ||
use tracing_subscriber::{fmt, Registry, util::SubscriberInitExt, EnvFilter, layer::SubscriberExt}; | ||
|
||
pub use crate::rpc::{XpsMethods, XpsServer}; | ||
|
||
/// Entrypoint for the xps Gateway | ||
pub async fn run() -> Result<()> { | ||
init_logging(); | ||
|
||
// a port of 0 allows the OS to choose an open port | ||
let server = Server::builder().build("127.0.0.1:0").await?; | ||
let addr = server.local_addr()?; | ||
let handle = server.start(rpc::XpsMethods.into_rpc()); | ||
|
||
log::info!("Server Started at {addr}"); | ||
handle.stopped().await; | ||
Ok(()) | ||
} | ||
|
||
/// Start [`tracing`] logging | ||
pub(crate) fn init_logging() { | ||
let fmt = fmt::layer().compact(); | ||
Registry::default() | ||
.with(EnvFilter::from_default_env()) | ||
.with(fmt) | ||
.init() | ||
} |
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 |
---|---|---|
@@ -1,68 +1,8 @@ | ||
use std::net::SocketAddr; | ||
use std::time::Duration; | ||
|
||
use http::header::HeaderName; | ||
use tonic::transport::Server; | ||
use tonic_web::GrpcWebLayer; | ||
use tower_http::cors::{AllowOrigin, CorsLayer}; | ||
|
||
mod inbox; | ||
|
||
use inbox::v1::proto::inbox_server::InboxServer; | ||
|
||
use clap::Parser; | ||
|
||
/// XMTP RPC node | ||
#[derive(Parser, Debug)] | ||
#[command(author, version, about, long_about = None)] | ||
struct Args { | ||
/// Port to listen on for public gRPC | ||
#[arg(short, long, default_value_t = 50051)] | ||
port: u16, | ||
} | ||
|
||
const DEFAULT_MAX_AGE: Duration = Duration::from_secs(24 * 60 * 60); | ||
const DEFAULT_EXPOSED_HEADERS: [&str; 3] = | ||
["grpc-status", "grpc-message", "grpc-status-details-bin"]; | ||
const DEFAULT_ALLOW_HEADERS: [&str; 4] = | ||
["x-grpc-web", "content-type", "x-user-agent", "grpc-timeout"]; | ||
use anyhow::Result; | ||
use gateway::run; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
let args = Args::parse(); | ||
|
||
let inbox_v1 = inbox::v1::InboxService::default(); | ||
|
||
let addr: SocketAddr = format!("[::1]:{}", args.port).parse()?; | ||
let server = Server::builder() | ||
.accept_http1(true) | ||
.layer( | ||
CorsLayer::new() | ||
.allow_origin(AllowOrigin::mirror_request()) | ||
.allow_credentials(true) | ||
.max_age(DEFAULT_MAX_AGE) | ||
.expose_headers( | ||
DEFAULT_EXPOSED_HEADERS | ||
.iter() | ||
.cloned() | ||
.map(HeaderName::from_static) | ||
.collect::<Vec<HeaderName>>(), | ||
) | ||
.allow_headers( | ||
DEFAULT_ALLOW_HEADERS | ||
.iter() | ||
.cloned() | ||
.map(HeaderName::from_static) | ||
.collect::<Vec<HeaderName>>(), | ||
), | ||
) | ||
.layer(GrpcWebLayer::new()) | ||
.add_service(InboxServer::new(inbox_v1)); | ||
|
||
let listener = tokio::net::TcpListener::bind(addr).await?; | ||
println!("Listening on {}", listener.local_addr().unwrap()); | ||
let stream = tokio_stream::wrappers::TcpListenerStream::new(listener); | ||
server.serve_with_incoming(stream).await?; | ||
|
||
async fn main() -> Result<()> { | ||
crate::run().await?; | ||
Ok(()) | ||
} |
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,6 @@ | ||
//! RPC Interface and Implementations for XPS | ||
mod api; | ||
mod methods; | ||
|
||
pub use api::*; | ||
pub use methods::*; |
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,13 @@ | ||
//! Trait Interface Definitions for XPS JSON-RPC | ||
|
||
use jsonrpsee::{proc_macros::rpc, types::ErrorObjectOwned}; | ||
|
||
use crate::types::Message; | ||
|
||
/// XPS JSON-RPC Interface Methods | ||
#[rpc(server, client, namespace = "xps")] | ||
pub trait Xps { | ||
// Placeholder for send_message, see [the discussion](https://github.com/xmtp/xps-gateway/discussions/11) | ||
#[method(name = "sendMessage")] | ||
async fn send_message(&self, _message: Message) -> Result<(), ErrorObjectOwned>; | ||
} |
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,20 @@ | ||
//! Interface Implementations for XPS JSON-RPC | ||
|
||
use super::api::*; | ||
|
||
use async_trait::async_trait; | ||
use jsonrpsee::types::ErrorObjectOwned; | ||
|
||
use crate::types::Message; | ||
|
||
/// Gateway Methods for XPS | ||
pub struct XpsMethods; | ||
|
||
#[async_trait] | ||
impl XpsServer for XpsMethods { | ||
async fn send_message(&self, _message: Message) -> Result<(), ErrorObjectOwned> { | ||
//TODO: Stub for sendMessage, ref: [discussion](https://github.com/xmtp/xps-gateway/discussions/11) | ||
log::debug!("xps_sendMessage called"); | ||
todo!(); | ||
} | ||
} |
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,17 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
/// A message sent to a conversation | ||
#[derive(Serialize, Deserialize)] | ||
pub struct Message { | ||
// Unique identifier for a conversation | ||
#[serde(rename = "groupId")] | ||
group_id: Vec<u8>, | ||
/// message content in bytes | ||
payload: Vec<u8>, | ||
/// Signature of V | ||
v: Vec<u8>, | ||
/// Signature of R | ||
r: Vec<u8>, | ||
/// Signature of S | ||
s: Vec<u8>, | ||
} |