From b44e58122d1171c0b34bee63ad26edcabb538e2b Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sat, 10 Aug 2024 11:59:03 -0700 Subject: [PATCH] Make index settings harder to misuse (#3893) --- src/index.rs | 29 ++++++++++++++++++++--------- src/index/updater.rs | 14 +++++++------- src/settings.rs | 18 +++++++++--------- 3 files changed, 36 insertions(+), 25 deletions(-) diff --git a/src/index.rs b/src/index.rs index 29d87d7694..17dd962730 100644 --- a/src/index.rs +++ b/src/index.rs @@ -48,7 +48,7 @@ mod updater; #[cfg(test)] pub(crate) mod testing; -const SCHEMA_VERSION: u64 = 26; +const SCHEMA_VERSION: u64 = 27; define_multimap_table! { SATPOINT_TO_SEQUENCE_NUMBER, &SatPointValue, u32 } define_multimap_table! { SAT_TO_SEQUENCE_NUMBER, u64, u32 } @@ -92,6 +92,7 @@ pub(crate) enum Statistic { IndexSpentSats = 13, InitialSyncTime = 14, IndexAddresses = 15, + IndexInscriptions = 16, } impl Statistic { @@ -192,6 +193,7 @@ pub struct Index { genesis_block_coinbase_txid: Txid, height_limit: Option, index_addresses: bool, + index_inscriptions: bool, index_runes: bool, index_sats: bool, index_spent_sats: bool, @@ -321,44 +323,50 @@ impl Index { let mut outpoint_to_sat_ranges = tx.open_table(OUTPOINT_TO_SAT_RANGES)?; let mut statistics = tx.open_table(STATISTIC_TO_COUNT)?; - if settings.index_sats() { + if settings.index_sats_raw() { outpoint_to_sat_ranges.insert(&OutPoint::null().store(), [].as_slice())?; } Self::set_statistic( &mut statistics, Statistic::IndexAddresses, - u64::from(settings.index_addresses()), + u64::from(settings.index_addresses_raw()), + )?; + + Self::set_statistic( + &mut statistics, + Statistic::IndexInscriptions, + u64::from(settings.index_inscriptions_raw()), )?; Self::set_statistic( &mut statistics, Statistic::IndexRunes, - u64::from(settings.index_runes()), + u64::from(settings.index_runes_raw()), )?; Self::set_statistic( &mut statistics, Statistic::IndexSats, - u64::from(settings.index_sats() || settings.index_spent_sats()), + u64::from(settings.index_sats_raw() || settings.index_spent_sats_raw()), )?; Self::set_statistic( &mut statistics, Statistic::IndexSpentSats, - u64::from(settings.index_spent_sats()), + u64::from(settings.index_spent_sats_raw()), )?; Self::set_statistic( &mut statistics, Statistic::IndexTransactions, - u64::from(settings.index_transactions()), + u64::from(settings.index_transactions_raw()), )?; Self::set_statistic(&mut statistics, Statistic::Schema, SCHEMA_VERSION)?; } - if settings.index_runes() && settings.chain() == Chain::Mainnet { + if settings.index_runes_raw() && settings.chain() == Chain::Mainnet { let rune = Rune(2055900680524219742); let id = RuneId { block: 1, tx: 0 }; @@ -414,11 +422,13 @@ impl Index { let index_sats; let index_spent_sats; let index_transactions; + let index_inscriptions; { let tx = database.begin_read()?; let statistics = tx.open_table(STATISTIC_TO_COUNT)?; index_addresses = Self::is_statistic_set(&statistics, Statistic::IndexAddresses)?; + index_inscriptions = Self::is_statistic_set(&statistics, Statistic::IndexInscriptions)?; index_runes = Self::is_statistic_set(&statistics, Statistic::IndexRunes)?; index_sats = Self::is_statistic_set(&statistics, Statistic::IndexSats)?; index_spent_sats = Self::is_statistic_set(&statistics, Statistic::IndexSpentSats)?; @@ -442,6 +452,7 @@ impl Index { index_sats, index_spent_sats, index_transactions, + index_inscriptions, settings: settings.clone(), path, started: Utc::now(), @@ -1660,7 +1671,7 @@ impl Index { Ok( outpoint != OutPoint::null() && outpoint != self.settings.chain().genesis_coinbase_outpoint() - && if self.settings.index_addresses() { + && if self.index_addresses { self .database .begin_read()? diff --git a/src/index/updater.rs b/src/index/updater.rs index 0d311be8e6..07461d7256 100644 --- a/src/index/updater.rs +++ b/src/index/updater.rs @@ -76,7 +76,7 @@ impl<'index> Updater<'index> { let rx = Self::fetch_blocks_from(self.index, self.height, self.index.index_sats)?; let (mut output_sender, mut txout_receiver, mut address_txout_receiver) = - Self::spawn_fetcher(&self.index.settings)?; + Self::spawn_fetcher(self.index)?; let mut uncommitted = 0; let mut utxo_cache = HashMap::new(); @@ -240,13 +240,13 @@ impl<'index> Updater<'index> { } fn spawn_fetcher( - settings: &Settings, + index: &Index, ) -> Result<( mpsc::Sender, broadcast::Receiver, Option>, )> { - let fetcher = Fetcher::new(settings)?; + let fetcher = Fetcher::new(&index.settings)?; // A block probably has no more than 20k inputs const CHANNEL_BUFFER_SIZE: usize = 20_000; @@ -258,7 +258,7 @@ impl<'index> Updater<'index> { let (txout_sender, txout_receiver) = broadcast::channel::(CHANNEL_BUFFER_SIZE); - let address_txout_receiver = if settings.index_addresses() { + let address_txout_receiver = if index.index_addresses { Some(txout_sender.subscribe()) } else { None @@ -267,7 +267,7 @@ impl<'index> Updater<'index> { // Default rpcworkqueue in bitcoind is 16, meaning more than 16 concurrent requests will be rejected. // Since we are already requesting blocks on a separate thread, and we don't want to break if anything // else runs a request, we keep this to 12. - let parallel_requests: usize = settings.bitcoin_rpc_limit().try_into().unwrap(); + let parallel_requests: usize = index.settings.bitcoin_rpc_limit().try_into().unwrap(); thread::spawn(move || { let rt = tokio::runtime::Builder::new_multi_thread() @@ -348,8 +348,8 @@ impl<'index> Updater<'index> { let mut outpoint_to_txout = wtx.open_table(OUTPOINT_TO_TXOUT)?; - let index_inscriptions = self.height >= self.index.first_inscription_height - && self.index.settings.index_inscriptions(); + let index_inscriptions = + self.height >= self.index.first_inscription_height && self.index.index_inscriptions; // If the receiver still has inputs something went wrong in the last // block and we shouldn't recover from this and commit the last block diff --git a/src/settings.rs b/src/settings.rs index 2c1f60db9b..05e587a1f2 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -533,15 +533,15 @@ impl Settings { self.index.as_ref().unwrap() } - pub fn index_addresses(&self) -> bool { + pub fn index_addresses_raw(&self) -> bool { self.index_addresses } - pub fn index_inscriptions(&self) -> bool { + pub fn index_inscriptions_raw(&self) -> bool { !self.no_index_inscriptions } - pub fn index_runes(&self) -> bool { + pub fn index_runes_raw(&self) -> bool { self.index_runes } @@ -549,15 +549,15 @@ impl Settings { self.index_cache_size.unwrap() } - pub fn index_sats(&self) -> bool { + pub fn index_sats_raw(&self) -> bool { self.index_sats } - pub fn index_spent_sats(&self) -> bool { + pub fn index_spent_sats_raw(&self) -> bool { self.index_spent_sats } - pub fn index_transactions(&self) -> bool { + pub fn index_transactions_raw(&self) -> bool { self.index_transactions } @@ -922,9 +922,9 @@ mod tests { #[test] fn index_runes() { - assert!(parse(&["--chain=signet", "--index-runes"]).index_runes()); - assert!(parse(&["--index-runes"]).index_runes()); - assert!(!parse(&[]).index_runes()); + assert!(parse(&["--chain=signet", "--index-runes"]).index_runes_raw()); + assert!(parse(&["--index-runes"]).index_runes_raw()); + assert!(!parse(&[]).index_runes_raw()); } #[test]