Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make index settings harder to misuse #3893

Merged
merged 2 commits into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -92,6 +92,7 @@ pub(crate) enum Statistic {
IndexSpentSats = 13,
InitialSyncTime = 14,
IndexAddresses = 15,
IndexInscriptions = 16,
}

impl Statistic {
Expand Down Expand Up @@ -192,6 +193,7 @@ pub struct Index {
genesis_block_coinbase_txid: Txid,
height_limit: Option<u32>,
index_addresses: bool,
index_inscriptions: bool,
index_runes: bool,
index_sats: bool,
index_spent_sats: bool,
Expand Down Expand Up @@ -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 };
Expand Down Expand Up @@ -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)?;
Expand All @@ -442,6 +452,7 @@ impl Index {
index_sats,
index_spent_sats,
index_transactions,
index_inscriptions,
settings: settings.clone(),
path,
started: Utc::now(),
Expand Down Expand Up @@ -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()?
Expand Down
14 changes: 7 additions & 7 deletions src/index/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -240,13 +240,13 @@ impl<'index> Updater<'index> {
}

fn spawn_fetcher(
settings: &Settings,
index: &Index,
) -> Result<(
mpsc::Sender<OutPoint>,
broadcast::Receiver<TxOut>,
Option<broadcast::Receiver<TxOut>>,
)> {
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;
Expand All @@ -258,7 +258,7 @@ impl<'index> Updater<'index> {

let (txout_sender, txout_receiver) = broadcast::channel::<TxOut>(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
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,31 +533,31 @@ 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
}

pub fn index_cache_size(&self) -> usize {
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
}

Expand Down Expand Up @@ -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]
Expand Down
Loading