diff --git a/src/index.rs b/src/index.rs index 689b4a6a3a..b871cd52c4 100644 --- a/src/index.rs +++ b/src/index.rs @@ -50,6 +50,9 @@ mod reorg; mod rtx; pub(crate) mod updater; +mod extend; + + #[cfg(test)] pub(crate) mod testing; @@ -788,7 +791,7 @@ impl Index { Ok(()) } - fn begin_read(&self) -> Result { + pub(crate) fn begin_read(&self) -> Result { Ok(rtx::Rtx(self.database.begin_read()?)) } @@ -878,10 +881,6 @@ impl Index { self.begin_read()?.block_hash(height) } - pub(crate) fn latest_block(&self) -> Result> { - self.begin_read()?.latest_block() - } - pub(crate) fn blocks(&self, take: usize) -> Result> { let rtx = self.begin_read()?; diff --git a/src/index/extend.rs b/src/index/extend.rs new file mode 100644 index 0000000000..5b2d83e42d --- /dev/null +++ b/src/index/extend.rs @@ -0,0 +1,51 @@ +use super::*; +use redb::ReadTransaction; + +impl Index { + fn get_inscriptions_on_output_with_satpoints_with_rtx( + outpoint: OutPoint, + rtx: &ReadTransaction, + ) -> Result> { + let satpoint_to_sequence_number = rtx.open_multimap_table(SATPOINT_TO_SEQUENCE_NUMBER)?; + let sequence_number_to_inscription_entry = + rtx.open_table(SEQUENCE_NUMBER_TO_INSCRIPTION_ENTRY)?; + + Self::inscriptions_on_output( + &satpoint_to_sequence_number, + &sequence_number_to_inscription_entry, + outpoint, + ) + } + + pub(crate) fn get_inscriptions_on_output_with_rtx( + outpoint: OutPoint, + rtx: &ReadTransaction, + ) -> Result> { + Ok( + Self::get_inscriptions_on_output_with_satpoints_with_rtx(outpoint, rtx)? + .iter() + .map(|(_satpoint, inscription_id)| *inscription_id) + .collect(), + ) + } + + pub(crate) fn get_inscription_satpoint_by_id_with_rtx( + inscription_id: InscriptionId, + rtx: &ReadTransaction, + ) -> Result> { + let Some(sequence_number) = rtx + .open_table(INSCRIPTION_ID_TO_SEQUENCE_NUMBER)? + .get(&inscription_id.store())? + .map(|guard| guard.value()) + else { + return Ok(None); + }; + + let satpoint = rtx + .open_table(SEQUENCE_NUMBER_TO_SATPOINT)? + .get(sequence_number)? + .map(|satpoint| Entry::load(*satpoint.value())); + + Ok(satpoint) + } +} diff --git a/src/subcommand/server/ord/outpoint.rs b/src/subcommand/server/ord/outpoint.rs index c7234bc2f7..0833c84c77 100644 --- a/src/subcommand/server/ord/outpoint.rs +++ b/src/subcommand/server/ord/outpoint.rs @@ -67,13 +67,15 @@ pub(crate) async fn ord_outpoint( ) -> ApiResult { log::debug!("rpc: get ord_outpoint: {outpoint}"); - let (latest_height, latest_blockhash) = index + let rtx = index.begin_read()?; + + let (latest_height, latest_blockhash) = rtx .latest_block() .ok() .flatten() .ok_or_api_err(|| ApiError::internal("Failed to get the latest block."))?; - let inscriptions = index.get_inscriptions_on_output(outpoint)?; + let inscriptions = Index::get_inscriptions_on_output_with_rtx(outpoint, &rtx.0)?; if inscriptions.is_empty() { return Ok(Json(ApiResponse::ok(OutPointResult { result: None, @@ -111,8 +113,7 @@ pub(crate) async fn ord_outpoint( .ok_or(anyhow!( "Failed to get the inscription number by ID, there may be an error in the database." ))?, - location: index - .get_inscription_satpoint_by_id(id)? + location: Index::get_inscription_satpoint_by_id_with_rtx(id, &rtx.0)? .ok_or(anyhow!( "Failed to get the inscription location, there may be an error in the database." ))?