From 9620fdb1164128e09b9eb678793929dd61f9936f Mon Sep 17 00:00:00 2001 From: Arthur Roberts Date: Sat, 13 Jul 2024 19:26:44 +0100 Subject: [PATCH] Moved creation of the blockchain infos to a static context --- Cargo.lock | 7 ++++++ Cargo.toml | 1 + src/lib.rs | 62 +++++++++++++++++++++++++++++++----------------------- 3 files changed, 44 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6b9e5ef..efdd565 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -28,6 +28,7 @@ name = "blockchain_time" version = "0.1.0" dependencies = [ "chrono", + "lazy_static", ] [[package]] @@ -100,6 +101,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + [[package]] name = "libc" version = "0.2.155" diff --git a/Cargo.toml b/Cargo.toml index b019362..ee6b955 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,4 @@ edition = "2021" [dependencies] chrono = "0.4.38" +lazy_static = "1.5.0" diff --git a/src/lib.rs b/src/lib.rs index 600f959..3eab8e4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,9 @@ -use std::convert::Into; use chrono::prelude::*; +use lazy_static::lazy_static; +use std::convert::Into; // TODO review whether a genesis block has a block height of 1 or 0 - pub enum BlockchainSupported { BTC, BSV, @@ -13,41 +13,51 @@ pub enum BlockchainSupported { type BlockHeight = i64; +#[derive(Clone, Copy)] pub struct BlockchainInfo { - block_period: u64, // in seconds maybe? - + block_period: u64, // In seconds latest_block_height: BlockHeight, latest_block_time: DateTime, } +lazy_static! { + static ref BSV_BI: BlockchainInfo = BlockchainInfo { + block_period: 10 * 60, // 10 minutes, + latest_block_height: 852792, + latest_block_time: Utc.with_ymd_and_hms(2024, 7, 11, 19, 53, 7).unwrap(), + }; + static ref BTC_BI: BlockchainInfo = BlockchainInfo { + block_period: 10 * 60, // 10 minutes, + latest_block_height: 851724, + latest_block_time: Utc.with_ymd_and_hms(2024, 7, 11, 19, 38, 6).unwrap(), + }; + static ref XMR_BI: BlockchainInfo = BlockchainInfo { + block_period: 2 * 60, // 2 minutes, + latest_block_height: 3190705, + latest_block_time: Utc.with_ymd_and_hms(2024, 7, 11, 19, 54, 11).unwrap(), + }; + static ref ETH_BI: BlockchainInfo = BlockchainInfo { + block_period: 12, + latest_block_height: 20285486, + latest_block_time: Utc.with_ymd_and_hms(2024, 7, 11, 19, 53, 59).unwrap(), + }; +} + impl From for BlockchainInfo { - fn from (val: BlockchainSupported) -> Self { + fn from(val: BlockchainSupported) -> Self { match val { - BlockchainSupported::BTC => BlockchainInfo { - block_period: 10 * 60, // 10 minutes, - latest_block_height: 851724, - latest_block_time: Utc.with_ymd_and_hms(2024, 7, 11, 19, 38, 6).unwrap(), - }, - BlockchainSupported::BSV => BlockchainInfo { - block_period: 10 * 60, // 10 minutes, - latest_block_height: 852792, - latest_block_time: Utc.with_ymd_and_hms(2024, 7, 11, 19, 53, 7).unwrap(), - }, - BlockchainSupported::XMR => BlockchainInfo { - block_period: 2 * 60, // 2 minutes, - latest_block_height: 3190705, - latest_block_time: Utc.with_ymd_and_hms(2024, 7, 11, 19, 54, 11).unwrap(), - }, - BlockchainSupported::ETH => BlockchainInfo { - block_period: 12, // TODO investigate what the story is here... I don't think it's as simple as 12 - latest_block_height: 20285486, - latest_block_time: Utc.with_ymd_and_hms(2024, 7, 11, 19, 53, 59).unwrap(), - }, + BlockchainSupported::BTC => *BTC_BI, + BlockchainSupported::BSV => *BSV_BI, + BlockchainSupported::XMR => *XMR_BI, + BlockchainSupported::ETH => *ETH_BI, } } } -pub fn time_to_blockheight_specific_chain(time: DateTime, blockchain: BlockchainSupported) -> BlockHeight { +pub fn time_to_blockheight_specific_chain( + time: DateTime, + blockchain: BlockchainSupported, +) -> BlockHeight { let bi: BlockchainInfo = blockchain.into(); time_to_blockheight(time, bi) }