Skip to content

Commit

Permalink
Display initial sync time on status page (ordinals#3250)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey committed Mar 8, 2024
1 parent d591c93 commit 1dd26f0
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ pub(crate) enum Statistic {
UnboundInscriptions = 11,
IndexTransactions = 12,
IndexSpentSats = 13,
InitialSyncTime = 14,
}

impl Statistic {
Expand Down Expand Up @@ -464,6 +465,7 @@ impl Index {

let blessed_inscriptions = statistic(Statistic::BlessedInscriptions)?;
let cursed_inscriptions = statistic(Statistic::CursedInscriptions)?;
let initial_sync_time = statistic(Statistic::InitialSyncTime)?;

let mut content_type_counts = rtx
.open_table(CONTENT_TYPE_TO_COUNT)?
Expand All @@ -481,6 +483,7 @@ impl Index {
content_type_counts,
cursed_inscriptions,
height,
initial_sync_time: Duration::from_micros(initial_sync_time),
inscriptions: blessed_inscriptions + cursed_inscriptions,
lost_sats: statistic(Statistic::LostSats)?,
minimum_rune_for_next_block: Rune::minimum_at_height(
Expand Down
14 changes: 11 additions & 3 deletions src/index/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ pub(crate) struct Updater<'index> {

impl<'index> Updater<'index> {
pub(crate) fn update_index<'a>(&'a mut self, mut wtx: WriteTransaction<'a>) -> Result {
let start = Instant::now();
let starting_height = u32::try_from(self.index.client.get_block_count()?).unwrap() + 1;
let starting_index_height = self.height;

wtx
.open_table(WRITE_TRANSACTION_STARTING_BLOCK_COUNT_TO_TIMESTAMP)?
Expand Down Expand Up @@ -120,9 +122,8 @@ impl<'index> Updater<'index> {
.insert(
&self.height,
&SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.map(|duration| duration.as_millis())
.unwrap_or(0),
.duration_since(SystemTime::UNIX_EPOCH)?
.as_millis(),
)?;
}

Expand All @@ -131,6 +132,13 @@ impl<'index> Updater<'index> {
}
}

if starting_index_height == 0 && self.height > 0 {
wtx.open_table(STATISTIC_TO_COUNT)?.insert(
Statistic::InitialSyncTime.key(),
&u64::try_from(start.elapsed().as_micros())?,
)?;
}

if uncommitted > 0 {
self.commit(wtx, value_cache)?;
}
Expand Down
3 changes: 3 additions & 0 deletions src/subcommand/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,12 @@ rpcport={bitcoind_port}

eprintln!(
"{}
{server_url}
{}
bitcoin-cli -datadir='{relative}' getblockchaininfo
{}
{} --data-dir '{relative}' wallet balance",
"`ord` server URL:".blue().bold(),
"Example `bitcoin-cli` command:".blue().bold(),
"Example `ord` command:".blue().bold(),
ord.display(),
Expand Down
1 change: 1 addition & 0 deletions src/templates/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub struct StatusHtml {
pub content_type_counts: Vec<(Option<Vec<u8>>, u64)>,
pub cursed_inscriptions: u64,
pub height: Option<u32>,
pub initial_sync_time: Duration,
pub inscriptions: u64,
pub lost_sats: u64,
pub minimum_rune_for_next_block: Rune,
Expand Down
2 changes: 2 additions & 0 deletions templates/status.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ <h1>Status</h1>
<dd>{{ self.started }}</dd>
<dt>uptime</dt>
<dd>{{ humantime::format_duration(self.uptime) }}</dd>
<dt>initial sync time</dt>
<dd>{{ humantime::format_duration(self.initial_sync_time) }}</dd>
<dt>minimum rune for next block</dt>
<dd>{{ self.minimum_rune_for_next_block }}</dd>
<dt>version</dt>
Expand Down
8 changes: 5 additions & 3 deletions tests/json_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,10 +482,11 @@ fn get_status() {
.parse::<DateTime<Utc>>()
.unwrap();

let dummy_uptime = Duration::from_secs(1);
let dummy_duration = Duration::from_secs(1);

status_json.initial_sync_time = dummy_duration;
status_json.started = dummy_started;
status_json.uptime = dummy_uptime;
status_json.uptime = dummy_duration;

pretty_assert_eq!(
status_json,
Expand All @@ -495,6 +496,7 @@ fn get_status() {
content_type_counts: vec![(Some("text/plain;charset=utf-8".into()), 1)],
cursed_inscriptions: 0,
height: Some(3),
initial_sync_time: dummy_duration,
inscriptions: 1,
lost_sats: 0,
minimum_rune_for_next_block: Rune(99218849511960410),
Expand All @@ -504,7 +506,7 @@ fn get_status() {
started: dummy_started,
transaction_index: false,
unrecoverably_reorged: false,
uptime: dummy_uptime,
uptime: dummy_duration,
}
);
}
Expand Down

0 comments on commit 1dd26f0

Please sign in to comment.