Skip to content

Commit

Permalink
chore: Make sure we run clippy for wasm and fix wasm build/lints
Browse files Browse the repository at this point in the history
  • Loading branch information
fl0rek committed Oct 25, 2023
1 parent f5a0cc4 commit 6c3b115
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 48 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ jobs:
version: "23.3"
repo-token: ${{ secrets.GITHUB_TOKEN }}

- name: Run clippy
- name: Run clippy for native
run: cargo clippy --all --all-targets -- -D warnings

- name: Run clippy for wasm32
run: cargo clippy --all --all-targets --target=wasm32-unknown-unkown -- -D warnings

fmt:
runs-on: ubuntu-latest
Expand Down
77 changes: 74 additions & 3 deletions 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 node/src/store/indexed_db_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ impl From<rexie::Error> for StoreError {

impl From<serde_wasm_bindgen::Error> for StoreError {
fn from(error: serde_wasm_bindgen::Error) -> StoreError {
StoreError::StoredDataError(format!("Error de/serializing: {}", error.to_string()))
StoreError::StoredDataError(format!("Error de/serializing: {error}"))
}
}

Expand Down
3 changes: 3 additions & 0 deletions rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ tracing = "0.1.37"
http = "0.2.9"
jsonrpsee = { version = "0.20", features = ["http-client", "ws-client"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
jsonrpsee = { version = "0.20", features = ["wasm-client"] }

[dev-dependencies]
anyhow = "1.0.71"
dotenvy = "0.15.7"
Expand Down
2 changes: 1 addition & 1 deletion rpc/tests/p2p.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(feature = "p2p")]
#![cfg(all(not(target_arch = "wasm32"), feature = "p2p"))]
use crate::utils::client::{new_test_client, AuthLevel};
use celestia_rpc::prelude::*;
use celestia_types::p2p;
Expand Down
99 changes: 58 additions & 41 deletions rpc/tests/utils/client.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
use std::env;
use std::sync::OnceLock;

use anyhow::Result;
use celestia_rpc::client::{new_http, new_websocket};
use celestia_rpc::prelude::*;
use celestia_types::{blob::SubmitOptions, Blob};
use jsonrpsee::core::client::ClientT;
use jsonrpsee::core::Error;
use jsonrpsee::http_client::HttpClient;
use jsonrpsee::ws_client::WsClient;
use tokio::sync::{Mutex, MutexGuard};

const WS_URL: &str = "ws://localhost:26658";
const HTTP_URL: &str = "http://localhost:26658";

async fn write_lock() -> MutexGuard<'static, ()> {
static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
LOCK.get_or_init(|| Mutex::new(())).lock().await
Expand All @@ -27,50 +20,74 @@ pub enum AuthLevel {
Admin,
}

fn token_from_env(auth_level: AuthLevel) -> Result<Option<String>> {
match auth_level {
AuthLevel::Public => Ok(None),
AuthLevel::Read => Ok(Some(env::var("CELESTIA_NODE_AUTH_TOKEN_READ")?)),
AuthLevel::Write => Ok(Some(env::var("CELESTIA_NODE_AUTH_TOKEN_WRITE")?)),
AuthLevel::Admin => Ok(Some(env::var("CELESTIA_NODE_AUTH_TOKEN_ADMIN")?)),
}
pub async fn blob_submit<C>(client: &C, blobs: &[Blob]) -> Result<u64, Error>
where
C: ClientT + Sync,
{
let _guard = write_lock().await;
client.blob_submit(blobs, SubmitOptions::default()).await
}

fn env_or(var_name: &str, or_value: &str) -> String {
env::var(var_name).unwrap_or_else(|_| or_value.to_owned())
}
pub use self::imp::*;

#[cfg(not(target_arch = "wasm32"))]
mod imp {
use super::*;
use celestia_rpc::client::{new_http, new_websocket};
use jsonrpsee::ws_client::WsClient;
use jsonrpsee::http_client::HttpClient;
use std::env;

pub async fn new_test_client(auth_level: AuthLevel) -> Result<WsClient> {
let _ = dotenvy::dotenv();
let token = token_from_env(auth_level)?;
let url = env_or("CELESTIA_RPC_URL", WS_URL);
const WS_URL: &str = "ws://localhost:26658";
const HTTP_URL: &str = "http://localhost:26658";

let client = new_websocket(&url, token.as_deref()).await?;
fn token_from_env(auth_level: AuthLevel) -> Result<Option<String>> {
match auth_level {
AuthLevel::Public => Ok(None),
AuthLevel::Read => Ok(Some(env::var("CELESTIA_NODE_AUTH_TOKEN_READ")?)),
AuthLevel::Write => Ok(Some(env::var("CELESTIA_NODE_AUTH_TOKEN_WRITE")?)),
AuthLevel::Admin => Ok(Some(env::var("CELESTIA_NODE_AUTH_TOKEN_ADMIN")?)),
}
}

// minimum 2 blocks
client.header_wait_for_height(2).await?;
fn env_or(var_name: &str, or_value: &str) -> String {
env::var(var_name).unwrap_or_else(|_| or_value.to_owned())
}

Ok(client)
}
pub async fn new_test_client(auth_level: AuthLevel) -> Result<WsClient> {
let _ = dotenvy::dotenv();
let token = token_from_env(auth_level)?;
let url = env_or("CELESTIA_RPC_URL", WS_URL);

// This can be used if you want to inspect the requests from `mitmproxy`.
pub async fn new_test_client_http(auth_level: AuthLevel) -> Result<HttpClient> {
let _ = dotenvy::dotenv();
let token = token_from_env(auth_level)?;
let url = env_or("CELESTIA_RPC_URL", HTTP_URL);
let client = new_websocket(&url, token.as_deref()).await?;

let client = new_http(&url, token.as_deref())?;
// minimum 2 blocks
client.header_wait_for_height(2).await?;

// minimum 2 blocks
client.header_wait_for_height(2).await?;
Ok(client)
}

// This can be used if you want to inspect the requests from `mitmproxy`.
pub async fn new_test_client_http(auth_level: AuthLevel) -> Result<HttpClient> {
let _ = dotenvy::dotenv();
let token = token_from_env(auth_level)?;
let url = env_or("CELESTIA_RPC_URL", HTTP_URL);

let client = new_http(&url, token.as_deref())?;

// minimum 2 blocks
client.header_wait_for_height(2).await?;

Ok(client)
Ok(client)
}
}

pub async fn blob_submit<C>(client: &C, blobs: &[Blob]) -> Result<u64, Error>
where
C: ClientT + Sync,
{
let _guard = write_lock().await;
client.blob_submit(blobs, SubmitOptions::default()).await
#[cfg(target_arch = "wasm32")]
mod imp {
use super::*;
use jsonrpsee::core::client::Client;

pub async fn new_test_client(_auth_level: AuthLevel) -> Result<Client> {
todo!()
}
}
2 changes: 1 addition & 1 deletion rpc/tests/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use celestia_types::nmt::{Namespace, NS_ID_V0_SIZE};
use rand::{Rng, RngCore};

pub mod client;
#[cfg(feature = "p2p")]
#[cfg(all(not(target_arch = "wasm32"), feature = "p2p"))]
pub mod tiny_node;

fn ns_to_u128(ns: Namespace) -> u128 {
Expand Down

0 comments on commit 6c3b115

Please sign in to comment.