Skip to content

Commit 65229db

Browse files
Support WASM in the v2 API
1 parent 0950060 commit 65229db

File tree

8 files changed

+884
-817
lines changed

8 files changed

+884
-817
lines changed

core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ color-eyre = { workspace = true }
7878
ed25519-compact = "2.1.1"
7979
# NOTE: This is used due bug explained at: https://github.com/tomaka/wasm-timer/pull/13
8080
fluvio-wasm-timer = "0.2.5"
81+
hyper = { version = "0.14.23", features = ["http1"] }
8182
libp2p = { workspace = true, features = ["wasm-bindgen"] }
8283
libp2p-webrtc-websys = { workspace = true }
8384
rand = { workspace = true, features = ["std_rng"] }

core/src/api/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
pub mod configuration;
2+
#[cfg(not(target_arch = "wasm32"))]
23
pub mod diagnostics;
4+
#[cfg(not(target_arch = "wasm32"))]
35
pub mod server;
46
pub mod types;
7+
#[cfg(not(target_arch = "wasm32"))]
58
mod v1;
69
pub mod v2;

core/src/api/types.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,20 @@ use avail_rust::{
88
AvailHeader, H256,
99
};
1010
use codec::Encode;
11-
use color_eyre::{
12-
eyre::{eyre, WrapErr},
13-
Report, Result,
14-
};
11+
#[cfg(not(target_arch = "wasm32"))]
12+
use color_eyre::eyre::eyre;
13+
use color_eyre::{eyre::WrapErr, Report, Result};
1514
use derive_more::From;
15+
#[cfg(not(target_arch = "wasm32"))]
1616
use hyper::{http, StatusCode};
1717
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
18-
use std::{
19-
collections::{HashMap, HashSet},
20-
sync::Arc,
21-
};
18+
use std::collections::HashSet;
19+
#[cfg(not(target_arch = "wasm32"))]
20+
use std::{collections::HashMap, sync::Arc};
21+
#[cfg(not(target_arch = "wasm32"))]
2222
use tokio::sync::{mpsc::UnboundedSender, RwLock};
2323
use uuid::Uuid;
24+
#[cfg(not(target_arch = "wasm32"))]
2425
use warp::{
2526
ws::{self, Message},
2627
Reply,
@@ -41,6 +42,7 @@ use crate::{
4142
#[derive(Debug)]
4243
pub struct InternalServerError {}
4344

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

4648
#[derive(Serialize, Deserialize, Clone, Debug)]
@@ -49,6 +51,7 @@ pub struct Version {
4951
pub network_version: String,
5052
}
5153

54+
#[cfg(not(target_arch = "wasm32"))]
5255
impl Reply for Version {
5356
fn into_response(self) -> warp::reply::Response {
5457
warp::reply::json(&self).into_response()
@@ -124,6 +127,7 @@ pub struct SubmitResponse {
124127
pub index: u32,
125128
}
126129

130+
#[cfg(not(target_arch = "wasm32"))]
127131
impl Reply for SubmitResponse {
128132
fn into_response(self) -> warp::reply::Response {
129133
warp::reply::json(&self).into_response()
@@ -174,6 +178,7 @@ impl From<&SharedConfig> for Vec<Mode> {
174178
}
175179
}
176180

181+
#[cfg(not(target_arch = "wasm32"))]
177182
impl Reply for Status {
178183
fn into_response(self) -> warp::reply::Response {
179184
warp::reply::json(&self).into_response()
@@ -318,6 +323,7 @@ impl Block {
318323
}
319324
}
320325

326+
#[cfg(not(target_arch = "wasm32"))]
321327
impl Reply for Block {
322328
fn into_response(self) -> warp::reply::Response {
323329
warp::reply::json(&self).into_response()
@@ -348,6 +354,7 @@ pub struct Header {
348354
received_at: u64,
349355
}
350356

357+
#[cfg(not(target_arch = "wasm32"))]
351358
impl Reply for Header {
352359
fn into_response(self) -> warp::reply::Response {
353360
warp::reply::json(&self).into_response()
@@ -547,6 +554,7 @@ pub struct DataResponse {
547554
pub data_transactions: Vec<DataTransaction>,
548555
}
549556

557+
#[cfg(not(target_arch = "wasm32"))]
550558
impl Reply for DataResponse {
551559
fn into_response(self) -> warp::reply::Response {
552560
warp::reply::json(&self).into_response()
@@ -618,6 +626,7 @@ pub enum PublishMessage {
618626
DataVerified(DataMessage),
619627
}
620628

629+
#[cfg(not(target_arch = "wasm32"))]
621630
impl PublishMessage {
622631
fn apply_filter(&mut self, fields: &HashSet<DataField>) {
623632
match self {
@@ -630,6 +639,7 @@ impl PublishMessage {
630639
}
631640
}
632641

642+
#[cfg(not(target_arch = "wasm32"))]
633643
impl TryFrom<PublishMessage> for Message {
634644
type Error = Report;
635645
fn try_from(value: PublishMessage) -> Result<Self, Self::Error> {
@@ -639,13 +649,16 @@ impl TryFrom<PublishMessage> for Message {
639649
}
640650
}
641651

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

655+
#[cfg(not(target_arch = "wasm32"))]
644656
pub struct WsClient {
645657
pub subscription: Subscription,
646658
pub sender: Option<Sender>,
647659
}
648660

661+
#[cfg(not(target_arch = "wasm32"))]
649662
impl WsClient {
650663
pub fn new(subscription: Subscription) -> Self {
651664
WsClient {
@@ -665,9 +678,11 @@ impl WsClient {
665678
}
666679
}
667680

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

685+
#[cfg(not(target_arch = "wasm32"))]
671686
impl WsClients {
672687
pub async fn set_sender(&self, subscription_id: &str, sender: Sender) -> Result<()> {
673688
let mut clients = self.0.write().await;
@@ -707,6 +722,7 @@ impl WsClients {
707722
}
708723
}
709724

725+
#[cfg(not(target_arch = "wasm32"))]
710726
impl Default for WsClients {
711727
fn default() -> Self {
712728
Self(Arc::new(RwLock::new(HashMap::new())))
@@ -718,6 +734,7 @@ pub struct SubscriptionId {
718734
pub subscription_id: String,
719735
}
720736

737+
#[cfg(not(target_arch = "wasm32"))]
721738
impl Reply for SubscriptionId {
722739
fn into_response(self) -> warp::reply::Response {
723740
warp::reply::json(&self).into_response()
@@ -754,6 +771,7 @@ impl<T> Response<T> {
754771
}
755772
}
756773

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

@@ -816,6 +834,7 @@ impl Error {
816834
Self::new(Some(request_id), None, ErrorCode::BadRequest, message)
817835
}
818836

837+
#[cfg(not(target_arch = "wasm32"))]
819838
fn status(&self) -> StatusCode {
820839
match self.error_code {
821840
ErrorCode::NotFound => StatusCode::NOT_FOUND,
@@ -825,6 +844,7 @@ impl Error {
825844
}
826845
}
827846

847+
#[cfg(not(target_arch = "wasm32"))]
828848
impl Reply for Error {
829849
fn into_response(self) -> warp::reply::Response {
830850
http::Response::builder()

core/src/api/v2/messages.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use super::transactions;
2+
use crate::{
3+
api::{
4+
configuration::SharedConfig,
5+
types::{Error, Payload, Request, Response, Status, Version, WsResponse},
6+
},
7+
data::{Database, RpcNodeKey},
8+
};
9+
use std::sync::Arc;
10+
11+
pub async fn handle_request(
12+
request: Request,
13+
version: &str,
14+
config: &SharedConfig,
15+
submitter: Option<Arc<impl transactions::Submit>>,
16+
db: impl Database,
17+
) -> Result<WsResponse, Error> {
18+
let request_id = request.request_id;
19+
match request.payload {
20+
Payload::Version => {
21+
let version = Version {
22+
version: version.to_string(),
23+
network_version: db.get(RpcNodeKey).unwrap_or_default().system_version,
24+
};
25+
Ok(Response::new(request_id, version).into())
26+
},
27+
Payload::Status => {
28+
let status = Status::new(config, db);
29+
Ok(Response::new(request_id, status).into())
30+
},
31+
Payload::Submit(transaction) => {
32+
let Some(submitter) = submitter else {
33+
return Err(Error::bad_request(request_id, "Submit is not configured."));
34+
};
35+
if transaction.is_empty() {
36+
return Err(Error::bad_request(request_id, "Transaction is empty."));
37+
}
38+
39+
submitter
40+
.submit(transaction)
41+
.await
42+
.map(|response| Response::new(request_id, response).into())
43+
.map_err(Error::internal_server_error)
44+
},
45+
}
46+
}

0 commit comments

Comments
 (0)