Skip to content

Commit

Permalink
feature!: export core Error to WASM and update browser Client creatio…
Browse files Browse the repository at this point in the history
…n API (#451)
  • Loading branch information
Ma233 authored Jul 3, 2023
1 parent 155c428 commit 9c40586
Show file tree
Hide file tree
Showing 54 changed files with 126 additions and 132 deletions.
4 changes: 2 additions & 2 deletions core/src/channels/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use async_channel::Receiver;
use async_channel::Sender;
use async_trait::async_trait;

use crate::err::Error;
use crate::err::Result;
use crate::error::Error;
use crate::error::Result;
use crate::types::channel::Channel;

/// Channel combine with async_channel::Sender and async_channel::Receiver.
Expand Down
4 changes: 2 additions & 2 deletions core/src/channels/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use async_trait::async_trait;
use futures::channel::mpsc;
use futures::lock::Mutex;

use crate::err::Error;
use crate::err::Result;
use crate::error::Error;
use crate::error::Result;
use crate::types::channel::Channel;

type Sender<T> = Arc<mpsc::UnboundedSender<T>>;
Expand Down
4 changes: 2 additions & 2 deletions core/src/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use uuid::Uuid;
use crate::consts::DEFAULT_TTL_MS;
use crate::consts::MAX_TTL_MS;
use crate::consts::TS_OFFSET_TOLERANCE_MS;
use crate::err::Error;
use crate::err::Result;
use crate::error::Error;
use crate::error::Result;
use crate::utils::get_epoch_ms;

/// A data structure to presenting Chunks
Expand Down
4 changes: 2 additions & 2 deletions core/src/dht/chord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use crate::dht::Did;
use crate::dht::LiveDid;
use crate::dht::SuccessorReader;
use crate::dht::SuccessorWriter;
use crate::err::Error;
use crate::err::Result;
use crate::error::Error;
use crate::error::Result;
use crate::storage::MemStorage;
use crate::storage::PersistenceStorage;
use crate::storage::PersistenceStorageReadAndWrite;
Expand Down
4 changes: 2 additions & 2 deletions core/src/dht/did.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use web3::contract::tokens::Tokenizable;
use web3::types::H160;

use crate::ecc::HashStr;
use crate::err::Error;
use crate::err::Result;
use crate::error::Error;
use crate::error::Result;

/// Did is a finate Ring R(P) where P = 2^160, wrap H160.
#[derive(Copy, Clone, Eq, Ord, PartialEq, PartialOrd, Debug, Serialize, Deserialize, Hash)]
Expand Down
2 changes: 1 addition & 1 deletion core/src/dht/stabilization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::dht::Chord;
use crate::dht::PeerRing;
use crate::dht::PeerRingAction;
use crate::dht::PeerRingRemoteAction;
use crate::err::Result;
use crate::error::Result;
use crate::message::FindSuccessorReportHandler;
use crate::message::FindSuccessorSend;
use crate::message::FindSuccessorThen;
Expand Down
4 changes: 2 additions & 2 deletions core/src/dht/subring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use super::vnode::VNodeType;
use super::vnode::VirtualNode;
use super::FingerTable;
use crate::dht::Did;
use crate::err::Error;
use crate::err::Result;
use crate::error::Error;
use crate::error::Result;

/// A Subring is like a [super::PeerRing] without storage functional.
/// Subring also have two extra fields: `name` and `creator`.
Expand Down
4 changes: 2 additions & 2 deletions core/src/dht/successor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use std::sync::RwLockReadGuard;
use crate::dht::did::BiasId;
use crate::dht::did::SortRing;
use crate::dht::Did;
use crate::err::Error;
use crate::err::Result;
use crate::error::Error;
use crate::error::Result;

/// A sequence of successors for a node on the ring.
/// It's necessary to have multiple successors to prevent a single point of failure.
Expand Down
2 changes: 1 addition & 1 deletion core/src/dht/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::chord::TopoInfo;
use super::did::Did;
use super::vnode::VNodeOperation;
use super::vnode::VirtualNode;
use crate::err::Result;
use crate::error::Result;

/// Chord is a distributed hash table (DHT) algorithm that is designed to efficiently
/// distribute data across peer-to-peer network nodes. You may want to browse its
Expand Down
4 changes: 2 additions & 2 deletions core/src/dht/vnode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use super::subring::Subring;
use crate::consts::VNODE_DATA_MAX_LEN;
use crate::dht::Did;
use crate::ecc::HashStr;
use crate::err::Error;
use crate::err::Result;
use crate::error::Error;
use crate::error::Result;
use crate::message::Encoded;
use crate::message::Encoder;
use crate::message::MessagePayload;
Expand Down
4 changes: 2 additions & 2 deletions core/src/ecc/elgamal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ use libsecp256k1::curve::Scalar;
use crate::ecc::CurveEle;
use crate::ecc::PublicKey;
use crate::ecc::SecretKey;
use crate::err::Error;
use crate::err::Result;
use crate::error::Error;
use crate::error::Result;

pub fn str_to_field(s: &str) -> Vec<Field> {
s.as_bytes()
Expand Down
4 changes: 2 additions & 2 deletions core/src/ecc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use sha1::Sha1;
use web3::signing::keccak256;
use web3::types::Address;

use crate::err::Error;
use crate::err::Result;
use crate::error::Error;
use crate::error::Result;
pub mod elgamal;
pub mod signers;
mod types;
Expand Down
2 changes: 1 addition & 1 deletion core/src/ecc/signers/bip137.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use sha2::Sha256;

use crate::ecc::Address;
use crate::ecc::PublicKey;
use crate::err::Result;
use crate::error::Result;

/// recover pubkey according to signature.
pub fn recover(msg: &str, sig: impl AsRef<[u8]>) -> Result<PublicKey> {
Expand Down
2 changes: 1 addition & 1 deletion core/src/ecc/signers/eip191.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use web3::signing::keccak256;
use crate::ecc::Address;
use crate::ecc::PublicKey;
use crate::ecc::SecretKey;
use crate::err::Result;
use crate::error::Result;

/// sign function passing raw message parameter.
pub fn sign_raw(sec: SecretKey, msg: &str) -> [u8; 65] {
Expand Down
2 changes: 1 addition & 1 deletion core/src/ecc/signers/secp256k1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use web3::signing::keccak256;
use crate::ecc::Address;
use crate::ecc::PublicKey;
use crate::ecc::SecretKey;
use crate::err::Result;
use crate::error::Result;

/// sign function passing raw message parameter.
pub fn sign_raw(sec: SecretKey, msg: &str) -> [u8; 65] {
Expand Down
4 changes: 2 additions & 2 deletions core/src/ecc/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
use serde::Deserialize;
use serde::Serialize;

use crate::err::Error;
use crate::err::Result;
use crate::error::Error;
use crate::error::Result;

/// PublicKey for ECDSA and EdDSA.
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
Expand Down
13 changes: 10 additions & 3 deletions core/src/err.rs → core/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! Error of rings_core

/// A wrap `Result` contains custom errors.
pub type Result<T> = std::result::Result<T, Error>;

/// Errors collections in ring-core.
#[derive(thiserror::Error, Debug)]
#[non_exhaustive]
Expand Down Expand Up @@ -301,7 +304,7 @@ pub enum Error {
#[error("Invalid capacity value")]
InvalidCapacity,

#[cfg(feature = "default")]
#[cfg(not(feature = "wasm"))]
#[error("Sled error, {0}")]
SledError(sled::Error),

Expand Down Expand Up @@ -349,5 +352,9 @@ pub enum Error {
SessionExpired,
}

/// A Result wrapper contain custom Errors.
pub type Result<T> = std::result::Result<T, Error>;
#[cfg(feature = "wasm")]
impl From<Error> for wasm_bindgen::JsValue {
fn from(err: Error) -> Self {
wasm_bindgen::JsValue::from_str(&err.to_string())
}
}
2 changes: 1 addition & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
pub mod channels;
pub mod dht;
pub mod ecc;
pub mod err;
pub mod error;
pub mod macros;
pub mod message;
pub mod prelude;
Expand Down
4 changes: 2 additions & 2 deletions core/src/message/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use bytes::Bytes;
use serde::Deserialize;
use serde::Serialize;

use crate::err::Error;
use crate::err::Result;
use crate::error::Error;
use crate::error::Result;

pub trait Encoder {
fn encode(&self) -> Result<Encoded>;
Expand Down
4 changes: 2 additions & 2 deletions core/src/message/handlers/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use crate::dht::Chord;
use crate::dht::ChordStorageSync;
use crate::dht::PeerRingAction;
use crate::dht::PeerRingRemoteAction;
use crate::err::Error;
use crate::err::Result;
use crate::error::Error;
use crate::error::Result;
use crate::message::types::ConnectNodeReport;
use crate::message::types::ConnectNodeSend;
use crate::message::types::FindSuccessorReport;
Expand Down
2 changes: 1 addition & 1 deletion core/src/message/handlers/custom.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use async_trait::async_trait;

use crate::err::Result;
use crate::error::Result;
use crate::message::types::CustomMessage;
use crate::message::types::Message;
use crate::message::HandleMsg;
Expand Down
4 changes: 2 additions & 2 deletions core/src/message/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use super::MessagePayload;
use crate::dht::vnode::VirtualNode;
use crate::dht::Did;
use crate::dht::PeerRing;
use crate::err::Error;
use crate::err::Result;
use crate::error::Error;
use crate::error::Result;
use crate::message::ConnectNodeReport;
use crate::message::ConnectNodeSend;

Expand Down
2 changes: 1 addition & 1 deletion core/src/message/handlers/stabilization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::dht::Chord;
use crate::dht::ChordStorageSync;
use crate::dht::PeerRingAction;
use crate::dht::PeerRingRemoteAction;
use crate::err::Result;
use crate::error::Result;
use crate::message::types::Message;
use crate::message::types::NotifyPredecessorReport;
use crate::message::types::NotifyPredecessorSend;
Expand Down
4 changes: 2 additions & 2 deletions core/src/message/handlers/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::dht::Did;
use crate::dht::PeerRing;
use crate::dht::PeerRingAction;
use crate::dht::PeerRingRemoteAction;
use crate::err::Error;
use crate::err::Result;
use crate::error::Error;
use crate::error::Result;
use crate::message::types::FoundVNode;
use crate::message::types::Message;
use crate::message::types::SearchVNode;
Expand Down
2 changes: 1 addition & 1 deletion core/src/message/handlers/subring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use async_trait::async_trait;
use super::storage::handle_storage_store_act;
use crate::dht::ChordStorage;
use crate::dht::PeerRing;
use crate::err::Result;
use crate::error::Result;
use crate::prelude::vnode::VNodeOperation;
use crate::swarm::Swarm;

Expand Down
4 changes: 2 additions & 2 deletions core/src/message/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use crate::dht::Chord;
use crate::dht::Did;
use crate::dht::PeerRing;
use crate::dht::PeerRingAction;
use crate::err::Error;
use crate::err::Result;
use crate::error::Error;
use crate::error::Result;
use crate::session::SessionManager;
use crate::utils::get_epoch_ms;

Expand Down
4 changes: 2 additions & 2 deletions core/src/message/protocols/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use serde::Deserialize;
use serde::Serialize;

use crate::dht::Did;
use crate::err::Error;
use crate::err::Result;
use crate::error::Error;
use crate::error::Result;

/// MessageRelay guide message passing on rings network by relay.
///
Expand Down
9 changes: 6 additions & 3 deletions core/src/message/protocols/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use serde::Serialize;

use crate::ecc::signers;
use crate::ecc::PublicKey;
use crate::err::Error;
use crate::err::Result;
use crate::error::Error;
use crate::error::Result;
use crate::session::Session;

/// Message Verification is based on session, and sig.
Expand All @@ -26,7 +26,10 @@ impl MessageVerification {
/// Verify a MessageVerification
pub fn verify<T>(&self, data: &T) -> bool
where T: Serialize {
let Ok(msg) = self.msg(data) else {tracing::warn!("MessageVerification pack_msg failed"); return false};
let Ok(msg) = self.msg(data) else {
tracing::warn!("MessageVerification pack_msg failed");
return false;
};

self.session
.verify(&msg, &self.sig)
Expand Down
2 changes: 1 addition & 1 deletion core/src/message/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::Serialize;
use crate::dht::vnode::VNodeOperation;
use crate::dht::vnode::VirtualNode;
use crate::dht::Did;
use crate::err::Result;
use crate::error::Result;
use crate::types::ice_transport::HandshakeInfo;

/// MessageType use to ask for connection, send to remote with transport_uuid and handshake_info.
Expand Down
7 changes: 3 additions & 4 deletions core/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use crate::dht::Did;
use crate::ecc::signers;
use crate::ecc::PublicKey;
use crate::ecc::SecretKey;
use crate::err::Error;
use crate::err::Result;
use crate::error::Error;
use crate::error::Result;
use crate::utils;

fn pack_session(session_id: Did, ts_ms: u128, ttl_ms: usize) -> String {
Expand Down Expand Up @@ -59,6 +59,7 @@ pub struct SessionManagerBuilder {
///
/// To verify the session, use `verify_self()` method of [Session].
/// To verify a message, use `verify(msg, sig)` method of [Session].
#[wasm_export]
#[derive(Debug)]
pub struct SessionManager {
/// Session
Expand Down Expand Up @@ -160,9 +161,7 @@ impl SessionManagerBuilder {
self.ttl_ms = ttl_ms.unwrap_or(DEFAULT_SESSION_TTL_MS);
self
}
}

impl SessionManagerBuilder {
/// Build the [SessionManager].
pub fn build(self) -> Result<SessionManager> {
let authorizer = Authorizer::try_from((self.authorizer_entity, self.authorizer_type))?;
Expand Down
4 changes: 2 additions & 2 deletions core/src/storage/persistence/idb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use wasm_bindgen::JsValue;
use super::PersistenceStorageOperation;
use super::PersistenceStorageReadAndWrite;
use super::PersistenceStorageRemove;
use crate::err::Error;
use crate::err::Result;
use crate::error::Error;
use crate::error::Result;
use crate::utils::js_value;

/// Default IndexedDB database and storage name
Expand Down
4 changes: 2 additions & 2 deletions core/src/storage/persistence/kv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use sled;
use super::PersistenceStorageOperation;
use super::PersistenceStorageReadAndWrite;
use super::PersistenceStorageRemove;
use crate::err::Error;
use crate::err::Result;
use crate::error::Error;
use crate::error::Result;

trait KvStorageBasic {
fn get_db(&self) -> &sled::Db;
Expand Down
2 changes: 1 addition & 1 deletion core/src/storage/persistence/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use async_trait::async_trait;
pub use self::idb::IDBStorage;
#[cfg(not(feature = "wasm"))]
pub use self::kv::KvStorage;
use crate::err::Result;
use crate::error::Result;

/// Persistence Storage read and write functions
#[cfg_attr(feature = "wasm", async_trait(?Send))]
Expand Down
4 changes: 2 additions & 2 deletions core/src/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use crate::channels::Channel;
use crate::dht::Chord;
use crate::dht::Did;
use crate::dht::PeerRing;
use crate::err::Error;
use crate::err::Result;
use crate::error::Error;
use crate::error::Result;
use crate::inspect::SwarmInspect;
use crate::measure::Measure;
use crate::measure::MeasureCounter;
Expand Down
Loading

0 comments on commit 9c40586

Please sign in to comment.