Skip to content

Commit

Permalink
start indexing at the correct block
Browse files Browse the repository at this point in the history
  • Loading branch information
partialord committed Sep 20, 2024
1 parent 243f4fa commit 17b10d5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
18 changes: 17 additions & 1 deletion src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ pub struct Index {
path: PathBuf,
settings: Settings,
started: DateTime<Utc>,
first_index_height: u32,
unrecoverably_reorged: AtomicBool,
}

Expand Down Expand Up @@ -419,12 +420,23 @@ impl Index {
let genesis_block_coinbase_transaction =
settings.chain().genesis_block().coinbase().unwrap().clone();

let first_index_height = if index_sats || index_addresses {
0
} else if index_inscriptions {
settings.first_inscription_height()
} else if index_runes {
settings.first_rune_height()
} else {
u32::MAX
};

Ok(Self {
genesis_block_coinbase_txid: genesis_block_coinbase_transaction.txid(),
client,
database,
durability,
event_sender,
first_index_height,
genesis_block_coinbase_transaction,
height_limit: settings.height_limit(),
index_addresses,
Expand All @@ -439,6 +451,10 @@ impl Index {
})
}

pub fn have_full_utxo_index(&self) -> bool {
self.first_index_height == 0
}

/// Unlike normal outpoints, which are added to index on creation and removed
/// when spent, the UTXO entry for special outpoints may be updated.
///
Expand Down Expand Up @@ -1675,7 +1691,7 @@ impl Index {
Ok(
outpoint != OutPoint::null()
&& outpoint != self.settings.chain().genesis_coinbase_outpoint()
&& if self.index_sats {
&& if self.have_full_utxo_index() {
self
.database
.begin_read()?
Expand Down
18 changes: 8 additions & 10 deletions src/index/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl<'index> Updater<'index> {
Some(progress_bar)
};

let rx = Self::fetch_blocks_from(self.index, self.height, self.index.index_sats)?;
let rx = Self::fetch_blocks_from(self.index, self.height)?;

let (mut output_sender, mut txout_receiver) = Self::spawn_fetcher(self.index)?;

Expand Down Expand Up @@ -153,24 +153,23 @@ impl<'index> Updater<'index> {
fn fetch_blocks_from(
index: &Index,
mut height: u32,
index_sats: bool,
) -> Result<std::sync::mpsc::Receiver<BlockData>> {
let (tx, rx) = std::sync::mpsc::sync_channel(32);

let first_index_height = index.first_index_height;

let height_limit = index.height_limit;

let client = index.settings.bitcoin_rpc_client(None)?;

let first_inscription_height = index.settings.first_inscription_height();

thread::spawn(move || loop {
if let Some(height_limit) = height_limit {
if height >= height_limit {
break;
}
}

match Self::get_block_with_retries(&client, height, index_sats, first_inscription_height) {
match Self::get_block_with_retries(&client, height, first_index_height) {
Ok(Some(block)) => {
if let Err(err) = tx.send(block.into()) {
log::info!("Block receiver disconnected: {err}");
Expand All @@ -192,8 +191,7 @@ impl<'index> Updater<'index> {
fn get_block_with_retries(
client: &Client,
height: u32,
index_sats: bool,
first_inscription_height: u32,
first_index_height: u32,
) -> Result<Option<Block>> {
let mut errors = 0;
loop {
Expand All @@ -203,7 +201,7 @@ impl<'index> Updater<'index> {
.and_then(|option| {
option
.map(|hash| {
if index_sats || height >= first_inscription_height {
if height >= first_index_height {
Ok(client.get_block(&hash)?)
} else {
Ok(Block {
Expand Down Expand Up @@ -437,7 +435,7 @@ impl<'index> Updater<'index> {
);
}

if !self.index.index_sats {
if !self.index.have_full_utxo_index() {
// Send all missing input outpoints to be fetched
let txids = block
.txdata
Expand Down Expand Up @@ -562,7 +560,7 @@ impl<'index> Updater<'index> {

entry.value().to_buf()
} else {
assert!(!self.index.index_sats);
assert!(!self.index.have_full_utxo_index());
let txout = txout_receiver.blocking_recv().map_err(|err| {
anyhow!(
"failed to get transaction for {}: {err}",
Expand Down

0 comments on commit 17b10d5

Please sign in to comment.