stacks_common/util/
mod.rs#[macro_use]
pub mod log;
#[macro_use]
pub mod macros;
pub mod chunked_encoding;
pub mod hash;
pub mod pair;
pub mod pipe;
pub mod retry;
pub mod secp256k1;
pub mod uint;
#[cfg(feature = "vrf")]
pub mod vrf;
use std::time::{SystemTime, UNIX_EPOCH};
use std::{error, fmt, thread, time};
pub fn get_epoch_time_secs() -> u64 {
let start = SystemTime::now();
let since_the_epoch = start
.duration_since(UNIX_EPOCH)
.expect("Time went backwards");
since_the_epoch.as_secs()
}
pub fn get_epoch_time_ms() -> u128 {
let start = SystemTime::now();
let since_the_epoch = start
.duration_since(UNIX_EPOCH)
.expect("Time went backwards");
since_the_epoch.as_millis()
}
pub fn sleep_ms(millis: u64) {
let t = time::Duration::from_millis(millis);
thread::sleep(t);
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum HexError {
BadLength(usize),
BadCharacter(char),
}
impl fmt::Display for HexError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
HexError::BadLength(n) => write!(f, "bad length {} for hex string", n),
HexError::BadCharacter(c) => write!(f, "bad character {} for hex string", c),
}
}
}
impl error::Error for HexError {
fn cause(&self) -> Option<&dyn error::Error> {
None
}
fn description(&self) -> &str {
match *self {
HexError::BadLength(_) => "hex string non-64 length",
HexError::BadCharacter(_) => "bad hex character",
}
}
}
pub fn slice_partialeq<T: PartialEq>(s1: &[T], s2: &[T]) -> bool {
if s1.len() != s2.len() {
return false;
}
for i in 0..s1.len() {
if s1[i] != s2[i] {
return false;
}
}
true
}
pub mod db_common {
use std::{thread, time};
use rand::{thread_rng, Rng};
pub fn tx_busy_handler(run_count: i32) -> bool {
let mut sleep_count = 10;
if run_count > 0 {
sleep_count = 2u64.saturating_pow(run_count as u32);
}
sleep_count = sleep_count.saturating_add(thread_rng().gen::<u64>() % sleep_count);
if sleep_count > 5000 {
sleep_count = 5000;
}
crate::debug!(
"Database is locked; sleeping {}ms and trying again",
&sleep_count
);
thread::sleep(time::Duration::from_millis(sleep_count));
true
}
}