Skip to content

Commit

Permalink
remove protobuf add jsonrpc
Browse files Browse the repository at this point in the history
  • Loading branch information
insipx committed Dec 22, 2023
1 parent 4ea3201 commit 7ddd972
Show file tree
Hide file tree
Showing 12 changed files with 489 additions and 685 deletions.
839 changes: 389 additions & 450 deletions Cargo.lock

Large diffs are not rendered by default.

21 changes: 9 additions & 12 deletions xps-gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@
name = "gateway"
version = "0.1.0"
edition = "2021"
resolver = "2"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tonic = "0.9"
prost = "0.11"
http = "0.2.9"
log = "0.4"
tracing = "0.1"
tracing-subscriber = { version = "0.3.18", features = ["fmt", "env-filter"] }
serde = "1"
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
tokio-stream = { version = "0.1", features = ["net"] }
async-stream = "0.2"
clap = { version = "4.3.21", features = ["derive"] }
tonic-web = "0.9.2"
tower-http = { version = "0.4.0", default-features = false, features = [
"cors",
] }

[build-dependencies]
tonic-build = "0.9"
async-trait = "0.1"
jsonrpsee = { version = "0.21", features = ["macros", "server", "client-core"] }
anyhow = "1"
thiserror = "1"
5 changes: 0 additions & 5 deletions xps-gateway/build.rs

This file was deleted.

23 changes: 0 additions & 23 deletions xps-gateway/proto/inbox_v1.proto

This file was deleted.

1 change: 0 additions & 1 deletion xps-gateway/src/inbox/mod.rs

This file was deleted.

130 changes: 0 additions & 130 deletions xps-gateway/src/inbox/v1.rs

This file was deleted.

31 changes: 31 additions & 0 deletions xps-gateway/src/lib.rs
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()
}
68 changes: 4 additions & 64 deletions xps-gateway/src/main.rs
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(())
}
6 changes: 6 additions & 0 deletions xps-gateway/src/rpc.rs
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::*;
13 changes: 13 additions & 0 deletions xps-gateway/src/rpc/api.rs
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>;
}
20 changes: 20 additions & 0 deletions xps-gateway/src/rpc/methods.rs
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!();
}
}
17 changes: 17 additions & 0 deletions xps-gateway/src/types.rs
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>,
}

0 comments on commit 7ddd972

Please sign in to comment.