Skip to content

Commit

Permalink
Support WASM in the v2 API
Browse files Browse the repository at this point in the history
  • Loading branch information
aterentic-ethernal committed Dec 3, 2024
1 parent 2832aaa commit 986c3db
Show file tree
Hide file tree
Showing 9 changed files with 880 additions and 815 deletions.
1 change: 0 additions & 1 deletion 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 core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ color-eyre = { workspace = true }
ed25519-compact = "2.1.1"
# NOTE: This is used due bug explained at: https://github.com/tomaka/wasm-timer/pull/13
fluvio-wasm-timer = "0.2.5"
hyper = { version = "0.14.23", features = ["http1"] }
libp2p = { workspace = true, features = ["wasm-bindgen"] }
libp2p-webrtc-websys = { workspace = true }
rand = { workspace = true, features = ["std_rng"] }
thiserror-no-std = "2.0.2"
tokio_with_wasm = { version = "0.7.1", default-features = false, features = ["sync", "macros", "rt", "time"] }
wasm-bindgen = "0.2.90"
web-time = "1.1.0"

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
Expand Down
3 changes: 3 additions & 0 deletions core/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
pub mod configuration;
#[cfg(not(target_arch = "wasm32"))]
pub mod diagnostics;
#[cfg(not(target_arch = "wasm32"))]
pub mod server;
pub mod types;
#[cfg(not(target_arch = "wasm32"))]
mod v1;
pub mod v2;
38 changes: 29 additions & 9 deletions core/src/api/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@ use avail_rust::{
AvailHeader, H256,
};
use codec::Encode;
use color_eyre::{
eyre::{eyre, WrapErr},
Report, Result,
};
#[cfg(not(target_arch = "wasm32"))]
use color_eyre::eyre::eyre;
use color_eyre::{eyre::WrapErr, Report, Result};
use derive_more::From;
use hyper::{http, StatusCode};
#[cfg(not(target_arch = "wasm32"))]
use hyper::http;
use hyper::StatusCode;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use std::{
collections::{HashMap, HashSet},
sync::Arc,
};
use std::collections::HashSet;
#[cfg(not(target_arch = "wasm32"))]
use std::{collections::HashMap, sync::Arc};
#[cfg(not(target_arch = "wasm32"))]
use tokio::sync::{mpsc::UnboundedSender, RwLock};
use uuid::Uuid;
#[cfg(not(target_arch = "wasm32"))]
use warp::{
ws::{self, Message},
Reply,
Expand All @@ -41,6 +43,7 @@ use crate::{
#[derive(Debug)]
pub struct InternalServerError {}

#[cfg(not(target_arch = "wasm32"))]
impl warp::reject::Reject for InternalServerError {}

#[derive(Serialize, Deserialize, Clone, Debug)]
Expand All @@ -49,6 +52,7 @@ pub struct Version {
pub network_version: String,
}

#[cfg(not(target_arch = "wasm32"))]
impl Reply for Version {
fn into_response(self) -> warp::reply::Response {
warp::reply::json(&self).into_response()
Expand Down Expand Up @@ -124,6 +128,7 @@ pub struct SubmitResponse {
pub index: u32,
}

#[cfg(not(target_arch = "wasm32"))]
impl Reply for SubmitResponse {
fn into_response(self) -> warp::reply::Response {
warp::reply::json(&self).into_response()
Expand Down Expand Up @@ -174,6 +179,7 @@ impl From<&SharedConfig> for Vec<Mode> {
}
}

#[cfg(not(target_arch = "wasm32"))]
impl Reply for Status {
fn into_response(self) -> warp::reply::Response {
warp::reply::json(&self).into_response()
Expand Down Expand Up @@ -318,6 +324,7 @@ impl Block {
}
}

#[cfg(not(target_arch = "wasm32"))]
impl Reply for Block {
fn into_response(self) -> warp::reply::Response {
warp::reply::json(&self).into_response()
Expand Down Expand Up @@ -347,6 +354,7 @@ pub struct Header {
digest: Digest,
}

#[cfg(not(target_arch = "wasm32"))]
impl Reply for Header {
fn into_response(self) -> warp::reply::Response {
warp::reply::json(&self).into_response()
Expand Down Expand Up @@ -541,6 +549,7 @@ pub struct DataResponse {
pub data_transactions: Vec<DataTransaction>,
}

#[cfg(not(target_arch = "wasm32"))]
impl Reply for DataResponse {
fn into_response(self) -> warp::reply::Response {
warp::reply::json(&self).into_response()
Expand Down Expand Up @@ -612,6 +621,7 @@ pub enum PublishMessage {
DataVerified(DataMessage),
}

#[cfg(not(target_arch = "wasm32"))]
impl PublishMessage {
fn apply_filter(&mut self, fields: &HashSet<DataField>) {
match self {
Expand All @@ -624,6 +634,7 @@ impl PublishMessage {
}
}

#[cfg(not(target_arch = "wasm32"))]
impl TryFrom<PublishMessage> for Message {
type Error = Report;
fn try_from(value: PublishMessage) -> Result<Self, Self::Error> {
Expand All @@ -633,13 +644,16 @@ impl TryFrom<PublishMessage> for Message {
}
}

#[cfg(not(target_arch = "wasm32"))]
pub type Sender = UnboundedSender<Result<ws::Message, warp::Error>>;

#[cfg(not(target_arch = "wasm32"))]
pub struct WsClient {
pub subscription: Subscription,
pub sender: Option<Sender>,
}

#[cfg(not(target_arch = "wasm32"))]
impl WsClient {
pub fn new(subscription: Subscription) -> Self {
WsClient {
Expand All @@ -659,9 +673,11 @@ impl WsClient {
}
}

#[cfg(not(target_arch = "wasm32"))]
#[derive(Clone)]
pub struct WsClients(pub Arc<RwLock<HashMap<String, WsClient>>>);

#[cfg(not(target_arch = "wasm32"))]
impl WsClients {
pub async fn set_sender(&self, subscription_id: &str, sender: Sender) -> Result<()> {
let mut clients = self.0.write().await;
Expand Down Expand Up @@ -701,6 +717,7 @@ impl WsClients {
}
}

#[cfg(not(target_arch = "wasm32"))]
impl Default for WsClients {
fn default() -> Self {
Self(Arc::new(RwLock::new(HashMap::new())))
Expand All @@ -712,6 +729,7 @@ pub struct SubscriptionId {
pub subscription_id: String,
}

#[cfg(not(target_arch = "wasm32"))]
impl Reply for SubscriptionId {
fn into_response(self) -> warp::reply::Response {
warp::reply::json(&self).into_response()
Expand Down Expand Up @@ -748,6 +766,7 @@ impl<T> Response<T> {
}
}

#[cfg(not(target_arch = "wasm32"))]
impl TryFrom<ws::Message> for Request {
type Error = Report;

Expand Down Expand Up @@ -819,6 +838,7 @@ impl Error {
}
}

#[cfg(not(target_arch = "wasm32"))]
impl Reply for Error {
fn into_response(self) -> warp::reply::Response {
http::Response::builder()
Expand Down
46 changes: 46 additions & 0 deletions core/src/api/v2/messages.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use super::transactions;
use crate::{
api::{
configuration::SharedConfig,
types::{Error, Payload, Request, Response, Status, Version, WsResponse},
},
data::{Database, RpcNodeKey},
};
use std::sync::Arc;

pub async fn handle_request(

Check failure on line 11 in core/src/api/v2/messages.rs

View workflow job for this annotation

GitHub Actions / wasm

function `handle_request` is never used
request: Request,
version: &str,
config: &SharedConfig,
submitter: Option<Arc<impl transactions::Submit>>,
db: impl Database,
) -> Result<WsResponse, Error> {
let request_id = request.request_id;
match request.payload {
Payload::Version => {
let version = Version {
version: version.to_string(),
network_version: db.get(RpcNodeKey).unwrap_or_default().system_version,
};
Ok(Response::new(request_id, version).into())
},
Payload::Status => {
let status = Status::new(config, db);
Ok(Response::new(request_id, status).into())
},
Payload::Submit(transaction) => {
let Some(submitter) = submitter else {
return Err(Error::bad_request(request_id, "Submit is not configured."));
};
if transaction.is_empty() {
return Err(Error::bad_request(request_id, "Transaction is empty."));
}

submitter
.submit(transaction)
.await
.map(|response| Response::new(request_id, response).into())
.map_err(Error::internal_server_error)
},
}
}
Loading

0 comments on commit 986c3db

Please sign in to comment.