From dafd725f38bb856f8eb3ae00b07089fc984c64b4 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Mon, 23 Jan 2023 17:58:55 -0800 Subject: [PATCH 1/7] Pute timestmaps in --- templates/block.html | 2 +- templates/inscription.html | 2 +- templates/sat.html | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/templates/block.html b/templates/block.html index 0ef316974c..9e6e4f082f 100644 --- a/templates/block.html +++ b/templates/block.html @@ -2,7 +2,7 @@

Block {{ self.height }}

hash
{{self.hash}}
target
{{self.target}}
-
timestamp
{{self.block.header.time}}
+
timestamp
size
{{self.block.size()}}
weight
{{self.block.weight()}}
%% if self.height.0 > 0 { diff --git a/templates/inscription.html b/templates/inscription.html index ea348741f7..397649d8ea 100644 --- a/templates/inscription.html +++ b/templates/inscription.html @@ -38,7 +38,7 @@

Inscription {{ self.number }}

{{ content_type }}
%% }
timestamp
-
{{ self.timestamp }}
+
genesis height
{{ self.genesis_height }}
genesis transaction
diff --git a/templates/sat.html b/templates/sat.html index a30f62510b..a00cbddd46 100644 --- a/templates/sat.html +++ b/templates/sat.html @@ -10,7 +10,7 @@

Sat {{ self.sat.n() }}

block
{{ self.sat.height() }}
offset
{{ self.sat.third() }}
rarity
{{ self.sat.rarity() }}
-
time
{{ self.blocktime }}
+
timestamp
%% if let Some((inscription)) = &self.inscription {
inscription
{{ Iframe::thumbnail(*inscription) }}
%% } From dc49fc86e226d2eb59218137811fd01e619975d6 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Mon, 23 Jan 2023 18:24:56 -0800 Subject: [PATCH 2/7] Fix tests --- src/blocktime.rs | 29 ++++++++++++++++++----------- src/index.rs | 4 ++-- src/index/updater.rs | 2 +- src/lib.rs | 6 +++--- src/subcommand/server.rs | 4 ++-- src/templates/block.rs | 2 +- src/templates/inscription.rs | 4 ++-- src/templates/sat.rs | 16 ++++++++-------- tests/server.rs | 2 +- 9 files changed, 38 insertions(+), 31 deletions(-) diff --git a/src/blocktime.rs b/src/blocktime.rs index 8a346c1fff..3505985530 100644 --- a/src/blocktime.rs +++ b/src/blocktime.rs @@ -2,12 +2,20 @@ use super::*; #[derive(Copy, Clone)] pub(crate) enum Blocktime { - Confirmed(i64), - Expected(i64), + Confirmed(DateTime), + Expected(DateTime), } impl Blocktime { - fn timestamp(self) -> i64 { + pub(crate) fn confirmed(seconds: u32) -> Self { + Self::Confirmed(timestamp(seconds)) + } + + pub(crate) fn expected(seconds: u32) -> Self { + Self::Expected(timestamp(seconds)) + } + + fn timestamp(self) -> DateTime { match self { Self::Confirmed(timestamp) | Self::Expected(timestamp) => timestamp, } @@ -16,11 +24,7 @@ impl Blocktime { impl Display for Blocktime { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - write!( - f, - "{}", - NaiveDateTime::from_timestamp_opt(self.timestamp(), 0).unwrap() - )?; + write!(f, "{}", self.timestamp())?; if let Self::Expected(_) = self { write!(f, " (expected)")?; @@ -36,10 +40,13 @@ mod tests { #[test] fn display() { - assert_eq!(Blocktime::Confirmed(0).to_string(), "1970-01-01 00:00:00"); assert_eq!( - Blocktime::Expected(0).to_string(), - "1970-01-01 00:00:00 (expected)" + Blocktime::confirmed(0).to_string(), + "1970-01-01 00:00:00 UTC" + ); + assert_eq!( + Blocktime::expected(0).to_string(), + "1970-01-01 00:00:00 UTC (expected)" ); } } diff --git a/src/index.rs b/src/index.rs index d81a496822..e2cf008b72 100644 --- a/src/index.rs +++ b/src/index.rs @@ -614,7 +614,7 @@ impl Index { let height = height.n(); match self.get_block_by_height(height)? { - Some(block) => Ok(Blocktime::Confirmed(block.header.time.into())), + Some(block) => Ok(Blocktime::confirmed(block.header.time)), None => { let tx = self.database.begin_read()?; @@ -632,7 +632,7 @@ impl Index { })?; Ok(Blocktime::Expected( - Utc::now().timestamp() + 10 * 60 * i64::try_from(expected_blocks).unwrap(), + Utc::now() + chrono::Duration::seconds(10 * 60 * i64::try_from(expected_blocks).unwrap()), )) } } diff --git a/src/index/updater.rs b/src/index/updater.rs index 79fc06aecd..4c202b2fe3 100644 --- a/src/index/updater.rs +++ b/src/index/updater.rs @@ -255,7 +255,7 @@ impl Updater { let mut sat_ranges_written = 0; let mut outputs_in_block = 0; - let time = Utc.timestamp_opt(block.header.time.into(), 0).unwrap(); + let time = timestamp(block.header.time); log::info!( "Block {} at {} with {} transactions…", diff --git a/src/lib.rs b/src/lib.rs index 27a09b4f16..2cf877b543 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,7 +43,7 @@ use { }, bitcoincore_rpc::{Client, RpcApi}, chain::Chain, - chrono::{NaiveDateTime, TimeZone, Utc}, + chrono::{DateTime, TimeZone, Utc}, clap::{ArgGroup, Parser}, derive_more::{Display, FromStr}, html_escaper::{Escape, Trusted}, @@ -135,8 +135,8 @@ fn integration_test() -> bool { .unwrap_or(false) } -fn timestamp(seconds: u32) -> NaiveDateTime { - NaiveDateTime::from_timestamp_opt(seconds.into(), 0).unwrap() +fn timestamp(seconds: u32) -> DateTime { + Utc.timestamp_opt(seconds.into(), 0).unwrap() } pub fn main() { diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index 2293f3165d..ac977aec61 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -1562,7 +1562,7 @@ mod tests { TestServer::new().assert_response_regex( "/sat/0", StatusCode::OK, - ".*
time
2009-01-03 18:15:05
.*", + ".*
timestamp
.*", ); } @@ -1571,7 +1571,7 @@ mod tests { TestServer::new().assert_response_regex( "/sat/5000000000", StatusCode::OK, - ".*
time
.* \\(expected\\)
.*", + ".*
timestamp
.*", ); } diff --git a/src/templates/block.rs b/src/templates/block.rs index f86ac7675d..b61a11bd41 100644 --- a/src/templates/block.rs +++ b/src/templates/block.rs @@ -43,7 +43,7 @@ mod tests {
hash
000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
target
00000000ffff0000000000000000000000000000000000000000000000000000
-
timestamp
1231006505
+
timestamp
size
285
weight
1140
diff --git a/src/templates/inscription.rs b/src/templates/inscription.rs index 0a5df90265..98c9f57209 100644 --- a/src/templates/inscription.rs +++ b/src/templates/inscription.rs @@ -12,7 +12,7 @@ pub(crate) struct InscriptionHtml { pub(crate) previous: Option, pub(crate) sat: Option, pub(crate) satpoint: SatPoint, - pub(crate) timestamp: NaiveDateTime, + pub(crate) timestamp: DateTime, } impl PageContent for InscriptionHtml { @@ -68,7 +68,7 @@ mod tests {
content type
text/plain;charset=utf-8
timestamp
-
1970-01-01 00:00:00
+
genesis height
0
genesis transaction
diff --git a/src/templates/sat.rs b/src/templates/sat.rs index c05688b9b5..fd61d2c8ec 100644 --- a/src/templates/sat.rs +++ b/src/templates/sat.rs @@ -24,7 +24,7 @@ mod tests { SatHtml { sat: Sat(0), satpoint: None, - blocktime: Blocktime::Confirmed(0), + blocktime: Blocktime::confirmed(0), inscription: None, } .to_string(), @@ -41,7 +41,7 @@ mod tests {
block
0
offset
0
rarity
mythic
-
time
1970-01-01 00:00:00
+
timestamp
prev @@ -56,7 +56,7 @@ mod tests { SatHtml { sat: Sat(2099999997689999), satpoint: None, - blocktime: Blocktime::Confirmed(0), + blocktime: Blocktime::confirmed(0), inscription: None, } .to_string(), @@ -73,7 +73,7 @@ mod tests {
block
6929999
offset
0
rarity
uncommon
-
time
1970-01-01 00:00:00
+
timestamp
next @@ -88,7 +88,7 @@ mod tests { SatHtml { sat: Sat(1), satpoint: None, - blocktime: Blocktime::Confirmed(0), + blocktime: Blocktime::confirmed(0), inscription: None, }, r"

Sat 1

.*\n\n", @@ -101,7 +101,7 @@ mod tests { SatHtml { sat: Sat(0), satpoint: None, - blocktime: Blocktime::Confirmed(0), + blocktime: Blocktime::confirmed(0), inscription: Some(inscription_id(1)), }, r"

Sat 0

.*
inscription
.*
.*", @@ -114,7 +114,7 @@ mod tests { SatHtml { sat: Sat::LAST, satpoint: None, - blocktime: Blocktime::Confirmed(0), + blocktime: Blocktime::confirmed(0), inscription: None, }, r"

Sat 2099999997689999

.*\nnext\n", @@ -127,7 +127,7 @@ mod tests { SatHtml { sat: Sat(0), satpoint: Some(satpoint(1, 0)), - blocktime: Blocktime::Confirmed(0), + blocktime: Blocktime::confirmed(0), inscription: None, }, "

Sat 0

.*
location
1{64}:1:0
.*", diff --git a/tests/server.rs b/tests/server.rs index 010a0a7651..9050845d9e 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -67,7 +67,7 @@ fn inscription_page() {
content type
text/plain;charset=utf-8
timestamp
-
1970-01-01 00:00:02
+
genesis height
2
genesis transaction
From df4314e86cc40663e0b100cfb4c4709adfb5c102 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Mon, 23 Jan 2023 18:28:31 -0800 Subject: [PATCH 3/7] Fix everything --- src/blocktime.rs | 6 +----- src/index.rs | 6 +++++- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/blocktime.rs b/src/blocktime.rs index 3505985530..c9bf2e53b5 100644 --- a/src/blocktime.rs +++ b/src/blocktime.rs @@ -11,10 +11,6 @@ impl Blocktime { Self::Confirmed(timestamp(seconds)) } - pub(crate) fn expected(seconds: u32) -> Self { - Self::Expected(timestamp(seconds)) - } - fn timestamp(self) -> DateTime { match self { Self::Confirmed(timestamp) | Self::Expected(timestamp) => timestamp, @@ -45,7 +41,7 @@ mod tests { "1970-01-01 00:00:00 UTC" ); assert_eq!( - Blocktime::expected(0).to_string(), + Blocktime::Expected(timestamp(0)).to_string(), "1970-01-01 00:00:00 UTC (expected)" ); } diff --git a/src/index.rs b/src/index.rs index e2cf008b72..13b5eb9b4a 100644 --- a/src/index.rs +++ b/src/index.rs @@ -632,7 +632,11 @@ impl Index { })?; Ok(Blocktime::Expected( - Utc::now() + chrono::Duration::seconds(10 * 60 * i64::try_from(expected_blocks).unwrap()), + Utc::now() + .checked_add_signed(chrono::Duration::seconds( + 10 * 60 * i64::try_from(expected_blocks)?, + )) + .ok_or_else(|| anyhow!("block timestamp out of range"))?, )) } } From 27c3a9503e0301c8f0e98792730f89654788b380 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Mon, 23 Jan 2023 18:30:04 -0800 Subject: [PATCH 4/7] Display timestamp as UTC on block --- src/templates/block.rs | 2 +- templates/block.html | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/templates/block.rs b/src/templates/block.rs index b61a11bd41..1c8f0d933d 100644 --- a/src/templates/block.rs +++ b/src/templates/block.rs @@ -43,7 +43,7 @@ mod tests {
hash
000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
target
00000000ffff0000000000000000000000000000000000000000000000000000
-
timestamp
+
timestamp
size
285
weight
1140
diff --git a/templates/block.html b/templates/block.html index 9e6e4f082f..23ce1e6fc8 100644 --- a/templates/block.html +++ b/templates/block.html @@ -2,7 +2,7 @@

Block {{ self.height }}

hash
{{self.hash}}
target
{{self.target}}
-
timestamp
+
timestamp
size
{{self.block.size()}}
weight
{{self.block.weight()}}
%% if self.height.0 > 0 { From 1e57a0a5c80ed60bd86a08a482c7653b15bc8a85 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Mon, 23 Jan 2023 18:39:05 -0800 Subject: [PATCH 5/7] Add local time as title --- static/index.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/static/index.js b/static/index.js index 85eb7ec073..7a1575cf0e 100644 --- a/static/index.js +++ b/static/index.js @@ -17,3 +17,7 @@ if (previous) { } }); } + +for (let time of document.body.getElementsByTagName("time")) { + time.setAttribute('title', new Date(time.textContent)); +} From 5921d63b5b05cb839de745ae9a9d95e2d51fe9b8 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Mon, 23 Jan 2023 18:48:19 -0800 Subject: [PATCH 6/7] Move expected out of