From 76a00ed7a6fb9acf89c70dc9b5ae8945ad0c965d Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Tue, 11 Oct 2022 23:27:26 -0700 Subject: [PATCH 1/2] Track number of commits --- src/index.rs | 60 ++++++++++++++++++++++++++++++---------- src/main.rs | 1 - src/subcommand/server.rs | 37 ++++++++++++++++++++++++- 3 files changed, 81 insertions(+), 17 deletions(-) diff --git a/src/index.rs b/src/index.rs index d5bbfd1747..0fd674fe38 100644 --- a/src/index.rs +++ b/src/index.rs @@ -4,7 +4,10 @@ use { bitcoin::BlockHeader, bitcoincore_rpc::{json::GetBlockHeaderResult, Auth, Client}, rayon::iter::{IntoParallelRefIterator, ParallelIterator}, - redb::{MultimapTableDefinition, ReadableMultimapTable, WriteStrategy}, + redb::{ + Database, MultimapTableDefinition, ReadableMultimapTable, ReadableTable, Table, + TableDefinition, WriteStrategy, WriteTransaction, + }, std::sync::atomic::{AtomicBool, Ordering}, }; @@ -12,6 +15,7 @@ mod rtx; const HASH_TO_RUNE: TableDefinition<[u8; 32], str> = TableDefinition::new("HASH_TO_RUNE"); const HEIGHT_TO_HASH: TableDefinition = TableDefinition::new("HEIGHT_TO_HASH"); +const INDEX_TO_ENTRY: TableDefinition = TableDefinition::new("INDEX_TO_ENTRY"); const ORDINAL_TO_SATPOINT: TableDefinition = TableDefinition::new("ORDINAL_TO_SATPOINT"); const ORDINAL_TO_RUNE_HASHES: MultimapTableDefinition = @@ -52,9 +56,11 @@ pub(crate) enum List { Unspent(Vec<(u64, u64)>), } +#[derive(Copy, Clone)] #[repr(u64)] -enum Statistic { +pub(crate) enum Statistic { OutputsTraversed = 0, + Commits = 1, } impl From for u64 { @@ -130,6 +136,7 @@ impl Index { tx.open_multimap_table(ORDINAL_TO_RUNE_HASHES)?; tx.open_table(HASH_TO_RUNE)?; tx.open_table(HEIGHT_TO_HASH)?; + tx.open_table(INDEX_TO_ENTRY)?; tx.open_table(ORDINAL_TO_SATPOINT)?; tx.open_table(OUTPOINT_TO_ORDINAL_RANGES)?; tx.open_table(STATISTICS)?; @@ -213,6 +220,7 @@ impl Index { .map(|(height, _hash)| height + 1) .unwrap_or(0); + let mut uncomitted = 0; for (i, height) in (0..).zip(height..) { if let Some(height_limit) = self.height_limit { if height > height_limit { @@ -222,9 +230,15 @@ impl Index { let done = self.index_block(&mut wtx, height)?; - if i > 0 && i % 1000 == 0 { + if !done { + uncomitted += 1; + } + + if uncomitted > 0 && i % 1000 == 0 { + Self::increment_statistic(&wtx, Statistic::Commits, 1)?; wtx.commit()?; wtx = self.begin_write()?; + uncomitted = 0; } if done || INTERRUPTS.load(atomic::Ordering::Relaxed) > 0 { @@ -232,7 +246,10 @@ impl Index { } } - wtx.commit()?; + if uncomitted > 0 { + Self::increment_statistic(&wtx, Statistic::Commits, 1)?; + wtx.commit()?; + } Ok(()) } @@ -245,7 +262,6 @@ impl Index { let mut height_to_hash = wtx.open_table(HEIGHT_TO_HASH)?; let mut ordinal_to_satpoint = wtx.open_table(ORDINAL_TO_SATPOINT)?; let mut outpoint_to_ordinal_ranges = wtx.open_table(OUTPOINT_TO_ORDINAL_RANGES)?; - let mut statistics = wtx.open_table(STATISTICS)?; let start = Instant::now(); let mut ordinal_ranges_written = 0; @@ -357,13 +373,7 @@ impl Index { height_to_hash.insert(&height, &block.block_hash().as_hash().into_inner())?; - statistics.insert( - &Statistic::OutputsTraversed.into(), - &(statistics - .get(&(Statistic::OutputsTraversed.into()))? - .unwrap_or(0) - + outputs_in_block), - )?; + Self::increment_statistic(wtx, Statistic::OutputsTraversed, outputs_in_block)?; log::info!( "Wrote {ordinal_ranges_written} ordinal ranges in {}ms", @@ -377,7 +387,7 @@ impl Index { Ok(rtx::Rtx(self.database.begin_read()?)) } - fn begin_write(&self) -> Result { + fn begin_write(&self) -> Result { if cfg!(test) { let mut tx = self.database.begin_write()?; tx.set_durability(redb::Durability::None); @@ -387,6 +397,27 @@ impl Index { } } + fn increment_statistic(wtx: &WriteTransaction, statistic: Statistic, n: u64) -> Result { + let mut statistics = wtx.open_table(STATISTICS)?; + statistics.insert( + &statistic.into(), + &(statistics.get(&(statistic.into()))?.unwrap_or(0) + n), + )?; + Ok(()) + } + + #[cfg(test)] + pub(crate) fn statistic(&self, statistic: Statistic) -> Result { + Ok( + self + .database + .begin_read()? + .open_table(STATISTICS)? + .get(&(statistic.into()))? + .unwrap_or(0), + ) + } + pub(crate) fn height(&self) -> Result { Ok(Height(self.begin_read()?.height()?)) } @@ -566,8 +597,7 @@ impl Index { pub(crate) fn insert_rune(&self, rune: &Rune) -> Result<(bool, sha256::Hash)> { let json = serde_json::to_string(rune)?; let hash = sha256::Hash::hash(json.as_ref()); - - let wtx = self.database.begin_write()?; + let wtx = self.begin_write()?; let created = wtx .open_table(HASH_TO_RUNE)? diff --git a/src/main.rs b/src/main.rs index f3636aeb08..a720220761 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,7 +31,6 @@ use { chrono::{DateTime, NaiveDateTime, Utc}, clap::Parser, derive_more::{Display, FromStr}, - redb::{Database, ReadableTable, Table, TableDefinition, WriteTransaction}, regex::Regex, serde::{Deserialize, Serialize}, std::{ diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index c6ab6b0e54..377f723a7f 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -148,8 +148,8 @@ impl Server { .route("/output/:output", get(Self::output)) .route("/range/:start/:end", get(Self::range)) .route("/rare.txt", get(Self::rare_txt)) - .route("/rune/:hash", get(Self::rune_get)) .route("/rune", put(Self::rune_put)) + .route("/rune/:hash", get(Self::rune_get)) .route("/search", get(Self::search_by_query)) .route("/search/:query", get(Self::search_by_path)) .route("/static/*path", get(Self::static_asset)) @@ -1388,4 +1388,39 @@ mod tests {
8ca6ee12cb891766de56e5698a73cd6546f27a88bd27c8b8d914bc4162f9e4b5
.*", ); } + + #[test] + fn commit_are_tracked() { + let server = TestServer::new(); + + assert_eq!( + server + .index + .statistic(crate::index::Statistic::Commits) + .unwrap(), + 1 + ); + + server.index.index().unwrap(); + + assert_eq!( + server + .index + .statistic(crate::index::Statistic::Commits) + .unwrap(), + 1 + ); + + server.bitcoin_rpc_server.mine_blocks(1); + + server.index.index().unwrap(); + + assert_eq!( + server + .index + .statistic(crate::index::Statistic::Commits) + .unwrap(), + 2 + ); + } } From f7de5bbe9dc7b7c11636d32d504606d5e464f837 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Thu, 13 Oct 2022 10:16:54 -0700 Subject: [PATCH 2/2] Remove unused table --- src/index.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/index.rs b/src/index.rs index 0fd674fe38..1dbe747b13 100644 --- a/src/index.rs +++ b/src/index.rs @@ -15,7 +15,6 @@ mod rtx; const HASH_TO_RUNE: TableDefinition<[u8; 32], str> = TableDefinition::new("HASH_TO_RUNE"); const HEIGHT_TO_HASH: TableDefinition = TableDefinition::new("HEIGHT_TO_HASH"); -const INDEX_TO_ENTRY: TableDefinition = TableDefinition::new("INDEX_TO_ENTRY"); const ORDINAL_TO_SATPOINT: TableDefinition = TableDefinition::new("ORDINAL_TO_SATPOINT"); const ORDINAL_TO_RUNE_HASHES: MultimapTableDefinition = @@ -136,7 +135,6 @@ impl Index { tx.open_multimap_table(ORDINAL_TO_RUNE_HASHES)?; tx.open_table(HASH_TO_RUNE)?; tx.open_table(HEIGHT_TO_HASH)?; - tx.open_table(INDEX_TO_ENTRY)?; tx.open_table(ORDINAL_TO_SATPOINT)?; tx.open_table(OUTPOINT_TO_ORDINAL_RANGES)?; tx.open_table(STATISTICS)?;