Implemented the wrong way around
This commit is contained in:
43
src/lib.rs
43
src/lib.rs
@@ -1,4 +1,4 @@
|
||||
use chrono::prelude::*;
|
||||
use chrono::{prelude::*, TimeDelta};
|
||||
use lazy_static::lazy_static;
|
||||
use rocket::serde::Serialize;
|
||||
use std::convert::Into;
|
||||
@@ -71,6 +71,18 @@ pub fn time_to_blockheight(time: DateTime<Utc>, bi: BlockchainInfo) -> BlockHeig
|
||||
num_blocks + bi.latest_block_height
|
||||
}
|
||||
|
||||
pub fn blockheight_specific_chain_to_time(height: BlockHeight, blockchain: BlockchainSupported) -> DateTime<Utc> {
|
||||
let bi: BlockchainInfo = blockchain.into();
|
||||
blockheight_to_time(height, bi)
|
||||
}
|
||||
|
||||
#[deprecated = "The output of this function (Date & Time) will eventually be redundant, please look to change your own time keeping system to the superior blocktime as set out in this repository"]
|
||||
pub fn blockheight_to_time(height: BlockHeight, bi: BlockchainInfo) -> DateTime<Utc> {
|
||||
// TODO check the + and - are the right way around
|
||||
let diff_seconds = (height - bi.latest_block_height) * bi.block_period as i64;
|
||||
bi.latest_block_time + TimeDelta::seconds(diff_seconds)
|
||||
}
|
||||
|
||||
// I know there's code duplication... I _could_ abstract it out.
|
||||
// I think it's probably very YAGNI
|
||||
|
||||
@@ -170,8 +182,9 @@ pub fn update_all_blockheights() {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use chrono::TimeDelta;
|
||||
|
||||
use super::*;
|
||||
use chrono::Duration;
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
@@ -230,6 +243,22 @@ mod tests {
|
||||
assert_eq!(result, 1001);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn back_and_forth() {
|
||||
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 blockchain_info = BlockchainInfo {
|
||||
block_period: 60 * 10, // 10 minutes
|
||||
latest_block_height: 1000,
|
||||
latest_block_time,
|
||||
};
|
||||
let result = time_to_blockheight(wanted_future_block_time, blockchain_info);
|
||||
assert_eq!(result, 1001);
|
||||
let future_time = blockheight_to_time(1001, blockchain_info);
|
||||
assert_eq!(future_time, Utc.with_ymd_and_hms(2024, 10, 10, 10, 20, 0).unwrap());
|
||||
// ignore the missing second... that second will not be needed in the future
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn one_block_time_previous() {
|
||||
let latest_block_time = Utc.with_ymd_and_hms(2024, 10, 10, 10, 20, 1).unwrap();
|
||||
@@ -261,23 +290,23 @@ mod tests {
|
||||
// Doesn't feel like an overly good test - but I can't think of anyother way that's "dynamic"
|
||||
// to me manually updating the times above
|
||||
let bi: BlockchainInfo = BlockchainSupported::BTC.into();
|
||||
let wanted_time = bi.latest_block_time + Duration::seconds(bi.block_period as i64 + 1);
|
||||
let wanted_time = bi.latest_block_time + TimeDelta::seconds(bi.block_period as i64 + 1);
|
||||
let bh = time_to_blockheight_specific_chain(wanted_time, BlockchainSupported::BTC);
|
||||
assert_eq!(bh, bi.latest_block_height + 1);
|
||||
let bi: BlockchainInfo = BlockchainSupported::BSV.into();
|
||||
let wanted_time = bi.latest_block_time + Duration::seconds(bi.block_period as i64 + 2);
|
||||
let wanted_time = bi.latest_block_time + TimeDelta::seconds(bi.block_period as i64 + 2);
|
||||
let bh = time_to_blockheight_specific_chain(wanted_time, BlockchainSupported::BSV);
|
||||
assert_eq!(bh, bi.latest_block_height + 1);
|
||||
let bi: BlockchainInfo = BlockchainSupported::XMR.into();
|
||||
let wanted_time = bi.latest_block_time + Duration::seconds(bi.block_period as i64 + 3);
|
||||
let wanted_time = bi.latest_block_time + TimeDelta::seconds(bi.block_period as i64 + 3);
|
||||
let bh = time_to_blockheight_specific_chain(wanted_time, BlockchainSupported::XMR);
|
||||
assert_eq!(bh, bi.latest_block_height + 1);
|
||||
let bi: BlockchainInfo = BlockchainSupported::ETH.into();
|
||||
let wanted_time = bi.latest_block_time + Duration::seconds(bi.block_period as i64 + 4);
|
||||
let wanted_time = bi.latest_block_time + TimeDelta::seconds(bi.block_period as i64 + 4);
|
||||
let bh = time_to_blockheight_specific_chain(wanted_time, BlockchainSupported::ETH);
|
||||
assert_eq!(bh, bi.latest_block_height + 1);
|
||||
let bi: BlockchainInfo = BlockchainSupported::ETH.into();
|
||||
let wanted_time = bi.latest_block_time + Duration::seconds(bi.block_period as i64 + 5);
|
||||
let wanted_time = bi.latest_block_time + TimeDelta::seconds(bi.block_period as i64 + 5);
|
||||
let bh = time_to_blockheight_specific_chain(wanted_time, BlockchainSupported::XMR);
|
||||
assert_ne!(bh, bi.latest_block_height + 1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user