Skip to content

Commit

Permalink
use same rtx in api: ord_outpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
cwbhhjl authored and wanyvic committed Jan 12, 2024
1 parent 94f7cbe commit 61ddcda
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 9 deletions.
9 changes: 4 additions & 5 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ mod reorg;
mod rtx;
pub(crate) mod updater;

mod extend;


#[cfg(test)]
pub(crate) mod testing;

Expand Down Expand Up @@ -788,7 +791,7 @@ impl Index {
Ok(())
}

fn begin_read(&self) -> Result<rtx::Rtx> {
pub(crate) fn begin_read(&self) -> Result<rtx::Rtx> {
Ok(rtx::Rtx(self.database.begin_read()?))
}

Expand Down Expand Up @@ -878,10 +881,6 @@ impl Index {
self.begin_read()?.block_hash(height)
}

pub(crate) fn latest_block(&self) -> Result<Option<(Height, BlockHash)>> {
self.begin_read()?.latest_block()
}

pub(crate) fn blocks(&self, take: usize) -> Result<Vec<(u32, BlockHash)>> {
let rtx = self.begin_read()?;

Expand Down
51 changes: 51 additions & 0 deletions src/index/extend.rs
Original file line number Diff line number Diff line change
@@ -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<Vec<(SatPoint, InscriptionId)>> {
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<Vec<InscriptionId>> {
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<Option<SatPoint>> {
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)
}
}
9 changes: 5 additions & 4 deletions src/subcommand/server/ord/outpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,15 @@ pub(crate) async fn ord_outpoint(
) -> ApiResult<OutPointResult> {
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,
Expand Down Expand Up @@ -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."
))?
Expand Down

0 comments on commit 61ddcda

Please sign in to comment.