[wallet] Abstract, multi-platform datetime utils
This commit is contained in:
parent
0954049df0
commit
927c2f37b9
@ -26,6 +26,7 @@ tokio = { version = "0.2", features = ["rt-core"] }
|
|||||||
|
|
||||||
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
||||||
async-trait = "0.1"
|
async-trait = "0.1"
|
||||||
|
js-sys = "0.3"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
minimal = []
|
minimal = []
|
||||||
|
@ -2,7 +2,6 @@ use std::cell::RefCell;
|
|||||||
use std::collections::{BTreeMap, HashSet};
|
use std::collections::{BTreeMap, HashSet};
|
||||||
use std::ops::DerefMut;
|
use std::ops::DerefMut;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::time::{Instant, SystemTime, UNIX_EPOCH};
|
|
||||||
|
|
||||||
use bitcoin::blockdata::opcodes;
|
use bitcoin::blockdata::opcodes;
|
||||||
use bitcoin::blockdata::script::Builder;
|
use bitcoin::blockdata::script::Builder;
|
||||||
@ -17,9 +16,11 @@ use miniscript::BitcoinSig;
|
|||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
use log::{debug, error, info, trace};
|
use log::{debug, error, info, trace};
|
||||||
|
|
||||||
|
pub mod time;
|
||||||
pub mod utils;
|
pub mod utils;
|
||||||
|
|
||||||
use self::utils::IsDust;
|
use self::utils::IsDust;
|
||||||
|
|
||||||
use crate::blockchain::{noop_progress, Blockchain, OfflineBlockchain, OnlineBlockchain};
|
use crate::blockchain::{noop_progress, Blockchain, OfflineBlockchain, OnlineBlockchain};
|
||||||
use crate::database::{BatchDatabase, BatchOperations, DatabaseUtils};
|
use crate::database::{BatchDatabase, BatchOperations, DatabaseUtils};
|
||||||
use crate::descriptor::{get_checksum, DescriptorMeta, ExtendedDescriptor, ExtractPolicy, Policy};
|
use crate::descriptor::{get_checksum, DescriptorMeta, ExtendedDescriptor, ExtractPolicy, Policy};
|
||||||
@ -295,7 +296,7 @@ where
|
|||||||
let transaction_details = TransactionDetails {
|
let transaction_details = TransactionDetails {
|
||||||
transaction: None,
|
transaction: None,
|
||||||
txid: txid,
|
txid: txid,
|
||||||
timestamp: Self::get_timestamp(),
|
timestamp: time::get_timestamp(),
|
||||||
received,
|
received,
|
||||||
sent: outgoing,
|
sent: outgoing,
|
||||||
height: None,
|
height: None,
|
||||||
@ -526,19 +527,6 @@ where
|
|||||||
|
|
||||||
// Internals
|
// Internals
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
|
||||||
fn get_timestamp() -> u64 {
|
|
||||||
SystemTime::now()
|
|
||||||
.duration_since(UNIX_EPOCH)
|
|
||||||
.unwrap()
|
|
||||||
.as_secs()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(target_arch = "wasm32")]
|
|
||||||
fn get_timestamp() -> u64 {
|
|
||||||
0
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_descriptor_for(&self, script_type: ScriptType) -> &ExtendedDescriptor {
|
fn get_descriptor_for(&self, script_type: ScriptType) -> &ExtendedDescriptor {
|
||||||
let desc = match script_type {
|
let desc = match script_type {
|
||||||
ScriptType::External => &self.descriptor,
|
ScriptType::External => &self.descriptor,
|
||||||
@ -752,8 +740,7 @@ where
|
|||||||
// cache a few of our addresses
|
// cache a few of our addresses
|
||||||
if last_addr.is_none() {
|
if last_addr.is_none() {
|
||||||
let mut address_batch = self.database.borrow().begin_batch();
|
let mut address_batch = self.database.borrow().begin_batch();
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
let start = time::Instant::new();
|
||||||
let start = Instant::now();
|
|
||||||
|
|
||||||
for i in 0..=max_address {
|
for i in 0..=max_address {
|
||||||
let derived = self.descriptor.derive(i).unwrap();
|
let derived = self.descriptor.derive(i).unwrap();
|
||||||
@ -776,7 +763,6 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
|
||||||
info!(
|
info!(
|
||||||
"derivation of {} addresses, took {} ms",
|
"derivation of {} addresses, took {} ms",
|
||||||
max_address,
|
max_address,
|
||||||
|
52
src/wallet/time.rs
Normal file
52
src/wallet/time.rs
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
use js_sys::Date;
|
||||||
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
|
use std::time::{Instant as SystemInstant, SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
|
pub fn get_timestamp() -> u64 {
|
||||||
|
SystemTime::now()
|
||||||
|
.duration_since(UNIX_EPOCH)
|
||||||
|
.unwrap()
|
||||||
|
.as_secs()
|
||||||
|
}
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
pub fn get_timestamp() -> u64 {
|
||||||
|
let millis = Date::now();
|
||||||
|
|
||||||
|
(millis / 1000.0) as u64
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
|
pub struct Instant(SystemInstant);
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
pub struct Instant(Duration);
|
||||||
|
|
||||||
|
impl Instant {
|
||||||
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Instant(SystemInstant::now())
|
||||||
|
}
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
pub fn new() -> Self {
|
||||||
|
let millis = Date::now();
|
||||||
|
|
||||||
|
let secs = millis / 1000.0;
|
||||||
|
let nanos = (millis % 1000.0) * 1e6;
|
||||||
|
|
||||||
|
Instant(Duration::new(secs as u64, nanos as u32))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
|
pub fn elapsed(&self) -> Duration {
|
||||||
|
self.0.elapsed()
|
||||||
|
}
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
pub fn elapsed(&self) -> Duration {
|
||||||
|
let now = Instant::new();
|
||||||
|
|
||||||
|
now.0.checked_sub(self.0).unwrap_or(Duration::new(0, 0))
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user