Added getting and updating BSV time
This commit is contained in:
1097
Cargo.lock
generated
1097
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -8,3 +8,5 @@ edition = "2021"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
chrono = "0.4.38"
|
chrono = "0.4.38"
|
||||||
lazy_static = "1.5.0"
|
lazy_static = "1.5.0"
|
||||||
|
reqwest = { version = "0.12.5", features = ["json", "blocking"] }
|
||||||
|
serde_json = "1.0.120"
|
||||||
|
|||||||
52
src/lib.rs
52
src/lib.rs
@@ -2,6 +2,9 @@ use chrono::prelude::*;
|
|||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use std::convert::Into;
|
use std::convert::Into;
|
||||||
use std::sync::RwLock;
|
use std::sync::RwLock;
|
||||||
|
use reqwest;
|
||||||
|
use serde_json;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
// TODO review whether a genesis block has a block height of 1 or 0
|
// TODO review whether a genesis block has a block height of 1 or 0
|
||||||
|
|
||||||
@@ -9,12 +12,13 @@ pub enum BlockchainSupported {
|
|||||||
BTC,
|
BTC,
|
||||||
BSV,
|
BSV,
|
||||||
XMR,
|
XMR,
|
||||||
ETH,
|
ETH
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
type BlockHeight = i64;
|
type BlockHeight = i64;
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy, PartialEq, Debug)]
|
||||||
pub struct BlockchainInfo {
|
pub struct BlockchainInfo {
|
||||||
block_period: u64, // In seconds
|
block_period: u64, // In seconds
|
||||||
latest_block_height: BlockHeight,
|
latest_block_height: BlockHeight,
|
||||||
@@ -69,11 +73,55 @@ pub fn time_to_blockheight(time: DateTime<Utc>, bi: BlockchainInfo) -> BlockHeig
|
|||||||
num_blocks + bi.latest_block_height
|
num_blocks + bi.latest_block_height
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn update_bsv_blockheight() {
|
||||||
|
let whatsonchain_url = "https://api.whatsonchain.com/v1/bsv/main/block/headers";
|
||||||
|
match reqwest::blocking::get(whatsonchain_url) {
|
||||||
|
Ok(resp) => {
|
||||||
|
let json = resp.json::<Vec<HashMap<String, serde_json::Value>>>();
|
||||||
|
match json {
|
||||||
|
Ok(json) => {
|
||||||
|
let height = json[0].get("height").unwrap().as_i64().unwrap();
|
||||||
|
|
||||||
|
// I am presuming that "time" is when WhatsOnChain saw the block rather than the
|
||||||
|
// timestamp in the block. I could be very wrong about that though... As I understand
|
||||||
|
// the timestamp in the block has no garuntee of anything other than maybe it's more
|
||||||
|
// than the last one.
|
||||||
|
let unixtime = json[0].get("time").unwrap().as_i64().unwrap();
|
||||||
|
let datetime = Utc.timestamp_opt(unixtime, 0).unwrap();
|
||||||
|
{
|
||||||
|
let mut bi = BSV_BI.write().unwrap();
|
||||||
|
bi.latest_block_height = height;
|
||||||
|
bi.latest_block_time = datetime;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
println!("Err: {}", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
println!("Err: {}", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use chrono::Duration;
|
use chrono::Duration;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[ignore]
|
||||||
|
fn get_latest_bsv_blockinfo() {
|
||||||
|
let bi_a = *BSV_BI.read().unwrap();
|
||||||
|
update_bsv_blockheight();
|
||||||
|
let bi_b = *BSV_BI.read().unwrap();
|
||||||
|
assert_ne!(bi_a, bi_b);
|
||||||
|
assert!(bi_a.latest_block_height < bi_b.latest_block_height);
|
||||||
|
assert!(bi_a.latest_block_time < bi_b.latest_block_time);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn one_block_time_away() {
|
fn one_block_time_away() {
|
||||||
let latest_block_time = Utc.with_ymd_and_hms(2024, 10, 10, 10, 10, 0).unwrap();
|
let latest_block_time = Utc.with_ymd_and_hms(2024, 10, 10, 10, 10, 0).unwrap();
|
||||||
|
|||||||
Reference in New Issue
Block a user