diff --git a/sn_client/src/api.rs b/sn_client/src/api.rs index 8751436706..a948e495de 100644 --- a/sn_client/src/api.rs +++ b/sn_client/src/api.rs @@ -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, @@ -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. @@ -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:?}."); diff --git a/sn_client/src/audit/mod.rs b/sn_client/src/audit/mod.rs index 8012490cc2..a50ad6319c 100644 --- a/sn_client/src/audit/mod.rs +++ b/sn_client/src/audit/mod.rs @@ -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. diff --git a/sn_client/src/files/download.rs b/sn_client/src/files/download.rs index 4310d7c85b..1eacc834f2 100644 --- a/sn_client/src/files/download.rs +++ b/sn_client/src/files/download.rs @@ -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. diff --git a/sn_client/src/lib.rs b/sn_client/src/lib.rs index edc261c838..264e2fc823 100644 --- a/sn_client/src/lib.rs +++ b/sn_client/src/lib.rs @@ -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 diff --git a/sn_client/src/wallet.rs b/sn_client/src/wallet.rs index 3bcd8cff03..172cd2a7bf 100644 --- a/sn_client/src/wallet.rs +++ b/sn_client/src/wallet.rs @@ -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. diff --git a/sn_networking/src/bootstrap.rs b/sn_networking/src/bootstrap.rs index d5dd18f368..1f7bf7cde7 100644 --- a/sn_networking/src/bootstrap.rs +++ b/sn_networking/src/bootstrap.rs @@ -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); diff --git a/sn_networking/src/cmd.rs b/sn_networking/src/cmd.rs index 34999c571b..61236f6543 100644 --- a/sn_networking/src/cmd.rs +++ b/sn_networking/src/cmd.rs @@ -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)] diff --git a/sn_networking/src/driver.rs b/sn_networking/src/driver.rs index c72c4e0f72..513f5e6898 100644 --- a/sn_networking/src/driver.rs +++ b/sn_networking/src/driver.rs @@ -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")] @@ -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 diff --git a/sn_networking/src/event.rs b/sn_networking/src/event.rs index 81d3075c2c..fed5c8839f 100644 --- a/sn_networking/src/event.rs +++ b/sn_networking/src/event.rs @@ -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}, diff --git a/sn_networking/src/lib.rs b/sn_networking/src/lib.rs index ea0ad314bd..d0d2a513ed 100644 --- a/sn_networking/src/lib.rs +++ b/sn_networking/src/lib.rs @@ -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}, @@ -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); diff --git a/sn_networking/src/metrics.rs b/sn_networking/src/metrics.rs index 96bf9c0722..186d455aea 100644 --- a/sn_networking/src/metrics.rs +++ b/sn_networking/src/metrics.rs @@ -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; diff --git a/sn_networking/src/network_discovery.rs b/sn_networking/src/network_discovery.rs index a5de038ec1..2fc94212c6 100644 --- a/sn_networking/src/network_discovery.rs +++ b/sn_networking/src/network_discovery.rs @@ -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; diff --git a/sn_networking/src/record_store.rs b/sn_networking/src/record_store.rs index adcbc7d2fe..07a9cadbe9 100644 --- a/sn_networking/src/record_store.rs +++ b/sn_networking/src/record_store.rs @@ -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, @@ -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}, @@ -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 diff --git a/sn_networking/src/replication_fetcher.rs b/sn_networking/src/replication_fetcher.rs index 137fc94288..d55e7c08b2 100644 --- a/sn_networking/src/replication_fetcher.rs +++ b/sn_networking/src/replication_fetcher.rs @@ -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. diff --git a/sn_networking/src/target_arch.rs b/sn_networking/src/target_arch.rs new file mode 100644 index 0000000000..d4e41b63e0 --- /dev/null +++ b/sn_networking/src/target_arch.rs @@ -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;