Skip to content

Commit

Permalink
Replace instant dependency with web-time
Browse files Browse the repository at this point in the history
web-time offers a number of advantages over the instant crate

- Actively maintained
- Stable 1.x API
- Exact std::time replacement
- Compatible with rustls pki-types web feature
- Re-exports std::time when not targeting wasm32-unknown-unknown
  without pulling in any dependencies
  • Loading branch information
DanGould committed Apr 1, 2024
1 parent e5068d4 commit dd87f4b
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 31 deletions.
12 changes: 11 additions & 1 deletion 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 mutiny-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ payjoin = { version = "0.13.0", features = ["send", "base64"] }
bincode = "1.3.3"
hex-conservative = "0.1.1"
async-lock = "3.2.0"
web-time = "1.1"

fedimint-client = { git = "https://github.com/fedimint/fedimint", rev = "5ade2536015a12a7e003a42b159ccc4a431e1a32" }
fedimint-core = { git = "https://github.com/fedimint/fedimint", rev = "5ade2536015a12a7e003a42b159ccc4a431e1a32" }
Expand Down Expand Up @@ -81,7 +82,6 @@ wasm-bindgen-futures = { version = "0.4.38" }
web-sys = { version = "0.3.65", features = ["console"] }
js-sys = "0.3.65"
gloo-net = { version = "0.4.0" }
instant = { version = "0.1", features = ["wasm-bindgen"] }
getrandom = { version = "0.2", features = ["js"] }
# add nip07 feature for wasm32
nostr = { version = "0.29.0", default-features = false, features = ["nip04", "nip05", "nip07", "nip47", "nip57"] }
Expand Down
5 changes: 1 addition & 4 deletions mutiny-core/src/federation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,17 @@ use fedimint_wallet_client::{WalletClientInit, WalletClientModule};
use futures::future::{self};
use futures_util::{pin_mut, StreamExt};
use hex_conservative::{DisplayHex, FromHex};
#[cfg(target_arch = "wasm32")]
use instant::Instant;
use lightning::{
ln::PaymentHash, log_debug, log_error, log_info, log_trace, log_warn, util::logger::Logger,
};
use lightning_invoice::Bolt11Invoice;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;
use std::{collections::HashMap, fmt::Debug, sync::Arc};
use std::{
str::FromStr,
sync::atomic::{AtomicU32, Ordering},
};
use web_time::Instant;

// The amount of time in milliseconds to wait for
// checking the status of a fedimint payment. This
Expand Down
5 changes: 1 addition & 4 deletions mutiny-core/src/gossip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use crate::{
use bitcoin::hashes::hex::FromHex;
use bitcoin::Network;
use hex_conservative::DisplayHex;
#[cfg(target_arch = "wasm32")]
use instant::Instant;
use lightning::ln::msgs::NodeAnnouncement;
use lightning::routing::gossip::NodeId;
use lightning::util::logger::Logger;
Expand All @@ -18,8 +16,7 @@ use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::str::FromStr;
use std::sync::Arc;
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;
use web_time::Instant;

use crate::logging::MutinyLogger;
use crate::node::{NetworkGraph, RapidGossipSync};
Expand Down
5 changes: 1 addition & 4 deletions mutiny-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ use fedimint_core::{api::InviteCode, config::FederationId};
use futures::{pin_mut, select, FutureExt};
use futures_util::join;
use hex_conservative::{DisplayHex, FromHex};
#[cfg(target_arch = "wasm32")]
use instant::Instant;
use itertools::Itertools;
use lightning::chain::BestBlock;
use lightning::ln::PaymentHash;
Expand All @@ -115,11 +113,10 @@ use reqwest::multipart::{Form, Part};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::sync::Arc;
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;
use std::{collections::HashMap, sync::atomic::AtomicBool};
use std::{str::FromStr, sync::atomic::Ordering};
use uuid::Uuid;
use web_time::Instant;

use crate::labels::LabelItem;
use crate::nostr::NostrKeySource;
Expand Down
5 changes: 1 addition & 4 deletions mutiny-core/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ use core::time::Duration;
use esplora_client::AsyncClient;
use futures_util::lock::Mutex;
use hex_conservative::DisplayHex;
#[cfg(target_arch = "wasm32")]
use instant::Instant;
use lightning::events::bump_transaction::{BumpTransactionEventHandler, Wallet};
use lightning::ln::channelmanager::ChannelDetails;
use lightning::ln::PaymentSecret;
Expand Down Expand Up @@ -83,15 +81,14 @@ use lightning_liquidity::{LiquidityClientConfig, LiquidityManager as LDKLSPLiqui
#[cfg(test)]
use mockall::predicate::*;
use std::collections::HashMap;
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;
use std::{
str::FromStr,
sync::{
atomic::{AtomicBool, Ordering},
Arc, RwLock,
},
};
use web_time::Instant;

const INITIAL_RECONNECTION_DELAY: u64 = 10;
const MAX_RECONNECTION_DELAY: u64 = 60;
Expand Down
5 changes: 1 addition & 4 deletions mutiny-core/src/nodemanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ use core::time::Duration;
use esplora_client::{AsyncClient, Builder};
use futures::{future::join_all, lock::Mutex};
use hex_conservative::DisplayHex;
#[cfg(target_arch = "wasm32")]
use instant::Instant;
use lightning::chain::Confirm;
use lightning::events::ClosureReason;
use lightning::ln::channelmanager::{ChannelDetails, PhantomRouteHints};
Expand All @@ -62,9 +60,8 @@ use std::cmp::max;
use std::io::Cursor;
use std::str::FromStr;
use std::sync::atomic::{AtomicBool, Ordering};
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;
use std::{collections::HashMap, ops::Deref, sync::Arc};
use web_time::Instant;

const BITCOIN_PRICE_CACHE_SEC: u64 = 300;
pub const DEVICE_LOCK_INTERVAL_SECS: u64 = 30;
Expand Down
12 changes: 3 additions & 9 deletions mutiny-core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,9 @@ pub async fn sleep(millis: i32) {
}
}
pub fn now() -> Duration {
#[cfg(target_arch = "wasm32")]
return instant::SystemTime::now()
.duration_since(instant::SystemTime::UNIX_EPOCH)
.unwrap();

#[cfg(not(target_arch = "wasm32"))]
return std::time::SystemTime::now()
.duration_since(std::time::SystemTime::UNIX_EPOCH)
.unwrap();
web_time::SystemTime::now()
.duration_since(web_time::SystemTime::UNIX_EPOCH)
.unwrap()
}

pub async fn fetch_with_timeout(
Expand Down

0 comments on commit dd87f4b

Please sign in to comment.