Added some known blockchains
This commit is contained in:
57
src/lib.rs
57
src/lib.rs
@@ -1,20 +1,61 @@
|
|||||||
|
use std::convert::Into;
|
||||||
use chrono::prelude::*;
|
use chrono::prelude::*;
|
||||||
|
|
||||||
// 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
|
||||||
|
|
||||||
|
|
||||||
|
pub enum BlockchainSupported {
|
||||||
|
BTC,
|
||||||
|
BSV,
|
||||||
|
XMR,
|
||||||
|
ETH,
|
||||||
|
}
|
||||||
|
|
||||||
type BlockHeight = i64;
|
type BlockHeight = i64;
|
||||||
|
|
||||||
pub struct BlockchainBlockInfo {
|
pub struct BlockchainInfo {
|
||||||
block_period: u64, // in seconds maybe?
|
block_period: u64, // in seconds maybe?
|
||||||
|
|
||||||
latest_block_height: BlockHeight,
|
latest_block_height: BlockHeight,
|
||||||
latest_block_time: DateTime<Utc>,
|
latest_block_time: DateTime<Utc>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn time_to_blockheight(time: DateTime<Utc>, blockchain: BlockchainBlockInfo) -> BlockHeight {
|
impl From<BlockchainSupported> for BlockchainInfo {
|
||||||
let time_difference = (time - blockchain.latest_block_time).num_seconds();
|
fn from (val: BlockchainSupported) -> Self {
|
||||||
let num_blocks = time_difference / blockchain.block_period as i64;
|
match val {
|
||||||
num_blocks + blockchain.latest_block_height
|
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(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn time_to_blockheight_specific_chain(time: DateTime<Utc>, blockchain: BlockchainSupported) -> BlockHeight {
|
||||||
|
let bi: BlockchainInfo = blockchain.into();
|
||||||
|
time_to_blockheight(time, bi)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn time_to_blockheight(time: DateTime<Utc>, bi: BlockchainInfo) -> BlockHeight {
|
||||||
|
let time_difference = (time - bi.latest_block_time).num_seconds();
|
||||||
|
let num_blocks = time_difference / bi.block_period as i64;
|
||||||
|
num_blocks + bi.latest_block_height
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -25,7 +66,7 @@ mod tests {
|
|||||||
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();
|
||||||
let wanted_future_block_time = Utc.with_ymd_and_hms(2024, 10, 10, 10, 20, 1).unwrap();
|
let wanted_future_block_time = Utc.with_ymd_and_hms(2024, 10, 10, 10, 20, 1).unwrap();
|
||||||
let blockchain_info = BlockchainBlockInfo {
|
let blockchain_info = BlockchainInfo {
|
||||||
block_period: 60 * 10, // 10 minutes
|
block_period: 60 * 10, // 10 minutes
|
||||||
latest_block_height: 1000,
|
latest_block_height: 1000,
|
||||||
latest_block_time,
|
latest_block_time,
|
||||||
@@ -38,7 +79,7 @@ mod tests {
|
|||||||
fn one_block_time_previous() {
|
fn one_block_time_previous() {
|
||||||
let latest_block_time = Utc.with_ymd_and_hms(2024, 10, 10, 10, 20, 1).unwrap();
|
let latest_block_time = Utc.with_ymd_and_hms(2024, 10, 10, 10, 20, 1).unwrap();
|
||||||
let wanted_future_block_time = Utc.with_ymd_and_hms(2024, 10, 10, 10, 10, 0).unwrap();
|
let wanted_future_block_time = Utc.with_ymd_and_hms(2024, 10, 10, 10, 10, 0).unwrap();
|
||||||
let blockchain_info = BlockchainBlockInfo {
|
let blockchain_info = BlockchainInfo {
|
||||||
block_period: 60 * 10, // 10 minutes
|
block_period: 60 * 10, // 10 minutes
|
||||||
latest_block_height: 1000,
|
latest_block_height: 1000,
|
||||||
latest_block_time,
|
latest_block_time,
|
||||||
@@ -51,7 +92,7 @@ mod tests {
|
|||||||
fn negative_block_time() {
|
fn negative_block_time() {
|
||||||
let latest_block_time = Utc.with_ymd_and_hms(2024, 10, 10, 10, 30, 0).unwrap();
|
let latest_block_time = Utc.with_ymd_and_hms(2024, 10, 10, 10, 30, 0).unwrap();
|
||||||
let wanted_negative_block_time = Utc.with_ymd_and_hms(2024, 10, 10, 10, 10, 0).unwrap();
|
let wanted_negative_block_time = Utc.with_ymd_and_hms(2024, 10, 10, 10, 10, 0).unwrap();
|
||||||
let blockchain_info = BlockchainBlockInfo {
|
let blockchain_info = BlockchainInfo {
|
||||||
block_period: 60 * 10, // 10 minutes
|
block_period: 60 * 10, // 10 minutes
|
||||||
latest_block_height: 1,
|
latest_block_height: 1,
|
||||||
latest_block_time,
|
latest_block_time,
|
||||||
|
|||||||
Reference in New Issue
Block a user