Skip to content

Commit

Permalink
chore: use a single target_arch.rs to simplify imports for wasm32 or no
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuef committed Jan 25, 2024
1 parent b88a7d5 commit 22cc892
Show file tree
Hide file tree
Showing 15 changed files with 53 additions and 90 deletions.
20 changes: 5 additions & 15 deletions sn_client/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use libp2p::{
#[cfg(feature = "open-metrics")]
use prometheus_client::registry::Registry;
use rand::{thread_rng, Rng};
use sn_networking::target_arch::{interval, spawn, timeout, Instant};
use sn_networking::{
multiaddr_is_global, Error as NetworkError, GetRecordCfg, GetRecordError, NetworkBuilder,
NetworkEvent, PutRecordCfg, VerificationKind, CLOSE_GROUP_SIZE,
Expand All @@ -38,25 +39,16 @@ use sn_protocol::{
};
use sn_registers::{Permissions, SignedRegister};
use sn_transfers::{CashNote, CashNoteRedemption, MainPubkey, NanoTokens, Payment, SignedSpend};
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;

use std::{
collections::{HashMap, HashSet},
num::NonZeroUsize,
path::PathBuf,
};
#[cfg(not(target_arch = "wasm32"))]
use tokio::task::spawn;

use tokio::time::Duration;
#[cfg(not(target_arch = "wasm32"))]
use tokio::time::{interval, timeout};

use tracing::trace;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_futures::spawn_local as spawn;
#[cfg(target_arch = "wasm32")]
use wasmtimer::std::Instant;
#[cfg(target_arch = "wasm32")]
use wasmtimer::tokio::{interval, timeout};
use xor_name::XorName;

/// The maximum duration the client will wait for a connection to the network before timing out.
Expand Down Expand Up @@ -89,11 +81,9 @@ impl Client {

info!("Startup a client with peers {peers:?} and local {local:?} flag");
info!("Starting Kad swarm in client mode...");
debug!("Starting Kad swarm in client mode.1..");
trace!("Starting Kad swarm in client mode..2.");

#[cfg(target_arch = "wasm32")]
let root_dir = PathBuf::from("dumb");
let root_dir = PathBuf::from("dummy path, wasm32/browser environments will not use this");
#[cfg(not(target_arch = "wasm32"))]
let root_dir = std::env::temp_dir();
trace!("Starting Kad swarm in client mode..{root_dir:?}.");
Expand Down
5 changes: 1 addition & 4 deletions sn_client/src/audit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@ use super::{
};

use futures::future::join_all;
use sn_networking::target_arch::Instant;
use sn_transfers::{
CashNoteRedemption, SignedSpend, SpendAddress, Transfer, WalletError, WalletResult,
NETWORK_ROYALTIES_PK,
};
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;
use std::{collections::BTreeSet, iter::Iterator, path::Path};
#[cfg(target_arch = "wasm32")]
use wasmtimer::std::Instant;

impl Client {
/// Verify that a spend is valid on the network.
Expand Down
6 changes: 2 additions & 4 deletions sn_client/src/files/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ use bytes::Bytes;
use futures::StreamExt;
use itertools::Itertools;
use self_encryption::{decrypt_full_set, DataMap, EncryptedChunk, StreamSelfDecryptor};
use sn_networking::target_arch::Instant;
use sn_protocol::storage::{Chunk, ChunkAddress};
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;

use std::{collections::HashMap, fs, path::PathBuf};
use tokio::sync::mpsc::{self};
#[cfg(target_arch = "wasm32")]
use wasmtimer::std::Instant;
use xor_name::XorName;

/// The events emitted from the download process.
Expand Down
1 change: 0 additions & 1 deletion sn_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ pub async fn main_js() -> std::result::Result<(), JsValue> {
// #[cfg(debug_assertions)]
console_error_panic_hook::set_once();

// Your code goes here!
console::log_1(&JsValue::from_str("Hello safe world!"));

// Tracing
Expand Down
7 changes: 3 additions & 4 deletions sn_client/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,21 @@ use super::{error::Result, Client};
use backoff::{backoff::Backoff, ExponentialBackoff};
use futures::{future::join_all, TryFutureExt};
use libp2p::PeerId;
use sn_networking::target_arch::Instant;
use sn_networking::{GetRecordError, PayeeQuote};
use sn_protocol::NetworkAddress;
use sn_transfers::{
CashNote, DerivationIndex, LocalWallet, MainPubkey, NanoTokens, Payment, PaymentQuote,
SignedSpend, SpendAddress, Transaction, Transfer, UniquePubkey, WalletError, WalletResult,
};
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;

use std::{
collections::{BTreeMap, BTreeSet},
iter::Iterator,
};
use tokio::time::Duration;
use tokio::{task::JoinSet, time::sleep};
#[cfg(target_arch = "wasm32")]
use wasmtimer::std::Instant;

use xor_name::XorName;

/// A wallet client can be used to send and receive tokens to and from other wallets.
Expand Down
10 changes: 2 additions & 8 deletions sn_networking/src/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,9 @@
// permissions and limitations relating to use of the SAFE Network Software.

use crate::{driver::PendingGetClosestType, SwarmDriver};
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;
use tokio::time::Duration;
#[cfg(not(target_arch = "wasm32"))]
use tokio::time::{interval, Interval};
#[cfg(target_arch = "wasm32")]
use wasmtimer::std::Instant;
#[cfg(target_arch = "wasm32")]
use wasmtimer::tokio::{interval, Interval};

use crate::target_arch::{interval, Instant, Interval};

/// The interval in which kad.bootstrap is called
pub(crate) const BOOTSTRAP_INTERVAL: Duration = Duration::from_secs(5);
Expand Down
5 changes: 1 addition & 4 deletions sn_networking/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ use std::{
use tokio::sync::oneshot;
use xor_name::XorName;

#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;
#[cfg(target_arch = "wasm32")]
use wasmtimer::std::Instant;
use crate::target_arch::Instant;

/// Commands to send to the Swarm
#[allow(clippy::large_enum_variant)]
Expand Down
16 changes: 1 addition & 15 deletions sn_networking/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use libp2p::quic::{tokio::Transport as TokioTransport, Config as TransportConfig
#[cfg(all(not(target_arch = "wasm32"), feature = "websockets"))]
use libp2p::websocket::WsConfig;

use crate::target_arch::{interval, spawn, Instant};
#[cfg(all(not(target_arch = "wasm32"), feature = "websockets"))]
use libp2p::tcp::{tokio::Transport as TokioTransport, Config as TransportConfig};
#[cfg(target_arch = "wasm32")]
Expand Down Expand Up @@ -69,24 +70,9 @@ use std::{
};
use tiny_keccak::{Hasher, Sha3};
use tokio::sync::{mpsc, oneshot};
#[cfg(not(target_arch = "wasm32"))]
use tokio::time::interval;
use tokio::time::Duration;
#[cfg(target_arch = "wasm32")]
use wasmtimer::tokio::interval;

#[cfg(not(target_arch = "wasm32"))]
use tokio::task::spawn;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_futures::spawn_local as spawn;

use tracing::warn;

#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;
#[cfg(target_arch = "wasm32")]
use wasmtimer::std::Instant;

/// The ways in which the Get Closest queries are used.
pub(crate) enum PendingGetClosestType {
/// The network discovery method is present at the networking layer
Expand Down
5 changes: 1 addition & 4 deletions sn_networking/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ use libp2p::{
Multiaddr, PeerId, TransportError,
};

#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;
#[cfg(target_arch = "wasm32")]
use wasmtimer::std::Instant;
use crate::target_arch::Instant;

use sn_protocol::{
messages::{CmdResponse, Query, Request, Response},
Expand Down
14 changes: 6 additions & 8 deletions sn_networking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ mod network_discovery;
mod record_store;
mod record_store_api;
mod replication_fetcher;
pub mod target_arch;
mod transfers;

// re-export arch dependent deps for use in the crate, or above
pub use target_arch::{interval, sleep, spawn, Instant, Interval};

pub use self::{
cmd::SwarmLocalState,
driver::{GetRecordCfg, NetworkBuilder, PutRecordCfg, SwarmDriver, VerificationKind},
Expand Down Expand Up @@ -61,16 +65,10 @@ use tokio::sync::{
mpsc::{self, Sender},
oneshot,
};
#[cfg(not(target_arch = "wasm32"))]
use tokio::task::spawn;
#[cfg(not(target_arch = "wasm32"))]
use tokio::time::sleep;

use tokio::time::Duration;
use tracing::trace;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_futures::spawn_local as spawn;
#[cfg(target_arch = "wasm32")]
use wasmtimer::tokio::sleep;

/// The type of quote for a selected payee.
pub type PayeeQuote = (PeerId, MainPubkey, PaymentQuote);

Expand Down
5 changes: 1 addition & 4 deletions sn_networking/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use crate::target_arch::sleep;
use libp2p::metrics::{Metrics as Libp2pMetrics, Recorder};
use prometheus_client::{metrics::gauge::Gauge, registry::Registry};
use sysinfo::{Pid, PidExt, ProcessExt, ProcessRefreshKind, System, SystemExt};
#[cfg(not(target_arch = "wasm32"))]
use tokio::time::sleep;
use tokio::time::Duration;
#[cfg(target_arch = "wasm32")]
use wasmtimer::tokio::sleep;

const UPDATE_INTERVAL: Duration = Duration::from_secs(15);
const TO_MB: u64 = 1_000_000;
Expand Down
5 changes: 1 addition & 4 deletions sn_networking/src/network_discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use crate::target_arch::Instant;
use libp2p::{kad::KBucketKey, PeerId};
use rand::{thread_rng, Rng};
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use sn_protocol::NetworkAddress;
use std::collections::{btree_map::Entry, BTreeMap};
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;
#[cfg(target_arch = "wasm32")]
use wasmtimer::std::Instant;

// The number of PeerId to generate when starting an instance of NetworkDiscovery
const INITIAL_GENERATION_ATTEMPTS: usize = 10_000;
Expand Down
10 changes: 1 addition & 9 deletions sn_networking/src/record_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// permissions and limitations relating to use of the SAFE Network Software.
#![allow(clippy::mutable_key_type)] // for the Bytes in NetworkAddress

use crate::target_arch::{spawn, Instant};
use crate::{cmd::SwarmCmd, event::NetworkEvent, send_swarm_cmd};
use libp2p::{
identity::PeerId,
Expand All @@ -22,8 +23,6 @@ use sn_protocol::{
NetworkAddress, PrettyPrintRecordKey,
};
use sn_transfers::NanoTokens;
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;
use std::{
borrow::Cow,
collections::{HashMap, HashSet},
Expand All @@ -32,13 +31,6 @@ use std::{
vec,
};
use tokio::sync::mpsc;
#[cfg(not(target_arch = "wasm32"))]
use tokio::task::spawn;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_futures::spawn_local as spawn;
#[cfg(target_arch = "wasm32")]
use wasmtimer::std::Instant;

use xor_name::XorName;

/// Max number of records a node can store
Expand Down
7 changes: 1 addition & 6 deletions sn_networking/src/replication_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,13 @@
// permissions and limitations relating to use of the SAFE Network Software.
#![allow(clippy::mutable_key_type)]

use crate::target_arch::Instant;
use libp2p::{
kad::{RecordKey, K_VALUE},
PeerId,
};
use sn_protocol::{storage::RecordType, NetworkAddress, PrettyPrintRecordKey};
use std::collections::HashMap;

#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;
#[cfg(target_arch = "wasm32")]
use wasmtimer::std::Instant;

use tokio::time::Duration;

// Max parallel fetches that can be undertaken at the same time.
Expand Down
27 changes: 27 additions & 0 deletions sn_networking/src/target_arch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2023 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

#[cfg(not(target_arch = "wasm32"))]
pub use std::time::Instant;
/// Wasm32 target arch does not support `time` or spawning via tokio
/// so we shim in alternatives here when building for that architecture

#[cfg(not(target_arch = "wasm32"))]
pub use tokio::{
spawn,
time::{interval, sleep, timeout, Duration, Interval},
};

#[cfg(target_arch = "wasm32")]
pub use wasmtimer::{
std::Instant,
tokio::{interval, sleep, timeout, Interval},
};

#[cfg(target_arch = "wasm32")]
pub use wasm_bindgen_futures::spawn_local as spawn;

0 comments on commit 22cc892

Please sign in to comment.