Skip to content

Commit

Permalink
Don't store serialized inscriptions (ordinals#872)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Dec 1, 2022
1 parent 98fb95e commit 690b5ae
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 41 deletions.
30 changes: 5 additions & 25 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ const ORDINAL_TO_SATPOINT: TableDefinition<u64, &SatPointArray> =
const OUTPOINT_TO_ORDINAL_RANGES: TableDefinition<&OutPointArray, [u8]> =
TableDefinition::new("OUTPOINT_TO_ORDINAL_RANGES");
const STATISTIC_TO_COUNT: TableDefinition<u64, u64> = TableDefinition::new("STATISTIC_TO_COUNT");
const INSCRIPTION_ID_TO_INSCRIPTION: TableDefinition<&InscriptionIdArray, str> =
TableDefinition::new("INSCRIPTION_ID_TO_INSCRIPTION");
const WRITE_TRANSACTION_STARTING_BLOCK_COUNT_TO_TIMESTAMP: TableDefinition<u64, u128> =
TableDefinition::new("WRITE_TRANSACTION_START_BLOCK_COUNT_TO_TIMESTAMP");
const INSCRIPTION_ID_TO_SATPOINT: TableDefinition<&InscriptionIdArray, &SatPointArray> =
Expand Down Expand Up @@ -201,7 +199,6 @@ impl Index {
};

tx.open_table(HEIGHT_TO_BLOCK_HASH)?;
tx.open_table(INSCRIPTION_ID_TO_INSCRIPTION)?;
tx.open_table(INSCRIPTION_ID_TO_SATPOINT)?;
tx.open_table(ORDINAL_TO_INSCRIPTION_ID)?;
tx.open_table(ORDINAL_TO_SATPOINT)?;
Expand Down Expand Up @@ -429,34 +426,17 @@ impl Index {

Ok(
self
.database
.begin_read()?
.open_table(INSCRIPTION_ID_TO_INSCRIPTION)?
.get(txid)?
.map(|inscription| {
serde_json::from_str(inscription)
.expect("failed to deserialize inscription (JSON) from database")
}),
.get_inscription_by_inscription_id(Txid::from_inner(*txid))?
.map(|(inscription, _)| inscription),
)
}

pub(crate) fn get_inscription_by_inscription_id(
&self,
txid: Txid,
) -> Result<Option<(Inscription, SatPoint)>> {
let inscription = self
.database
.begin_read()?
.open_table(INSCRIPTION_ID_TO_INSCRIPTION)?
.get(txid.as_inner())?
.map(|inscription| {
serde_json::from_str(inscription)
.expect("failed to deserialize inscription (JSON) from database")
});

let inscription = match inscription {
Some(inscription) => inscription,
None => return Ok(None),
let Some(inscription) = self.get_transaction(txid)?.and_then(|tx| Inscription::from_transaction(&tx)) else {
return Ok(None);
};

let satpoint = decode_satpoint(
Expand All @@ -471,7 +451,7 @@ impl Index {
Ok(Some((inscription, satpoint)))
}

pub(crate) fn transaction(&self, txid: Txid) -> Result<Option<Transaction>> {
pub(crate) fn get_transaction(&self, txid: Txid) -> Result<Option<Transaction>> {
if txid == self.genesis_block_coinbase_txid {
Ok(Some(self.genesis_block_coinbase_transaction.clone()))
} else {
Expand Down
16 changes: 3 additions & 13 deletions src/index/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ impl Updater {
}
}

let mut inscription_id_to_inscription = wtx.open_table(INSCRIPTION_ID_TO_INSCRIPTION)?;
let mut inscription_id_to_satpoint = wtx.open_table(INSCRIPTION_ID_TO_SATPOINT)?;
let mut satpoint_to_inscription_id = wtx.open_table(SATPOINT_TO_INSCRIPTION_ID)?;

Expand Down Expand Up @@ -317,7 +316,6 @@ impl Updater {
*txid,
&mut ordinal_to_satpoint,
&mut ordinal_to_inscription_id,
&mut inscription_id_to_inscription,
&mut inscription_id_to_satpoint,
&mut satpoint_to_inscription_id,
&mut input_ordinal_ranges,
Expand All @@ -334,7 +332,6 @@ impl Updater {
*txid,
&mut ordinal_to_satpoint,
&mut ordinal_to_inscription_id,
&mut inscription_id_to_inscription,
&mut inscription_id_to_satpoint,
&mut satpoint_to_inscription_id,
&mut coinbase_inputs,
Expand All @@ -347,7 +344,6 @@ impl Updater {
self.index_transaction_inscriptions(
tx,
*txid,
&mut inscription_id_to_inscription,
&mut inscription_id_to_satpoint,
&mut satpoint_to_inscription_id,
)?;
Expand All @@ -374,21 +370,17 @@ impl Updater {
&mut self,
tx: &Transaction,
txid: Txid,
inscription_id_to_inscription: &mut Table<&InscriptionIdArray, str>,
inscription_id_to_satpoint: &mut Table<&InscriptionIdArray, &SatPointArray>,
satpoint_to_inscription_id: &mut Table<&SatPointArray, &InscriptionIdArray>,
) -> Result<bool> {
let inscription = Inscription::from_transaction(tx);
if let Some(inscription) = &inscription {
let json = serde_json::to_string(&inscription)
.expect("Inscription serialization should always succeed");
let inscribed = Inscription::from_transaction(tx).is_some();

if inscribed {
let satpoint = encode_satpoint(SatPoint {
outpoint: OutPoint { txid, vout: 0 },
offset: 0,
});

inscription_id_to_inscription.insert(txid.as_inner(), &json)?;
inscription_id_to_satpoint.insert(txid.as_inner(), &satpoint)?;
satpoint_to_inscription_id.insert(&satpoint, txid.as_inner())?;
};
Expand Down Expand Up @@ -421,7 +413,7 @@ impl Updater {
}
}

Ok(inscription.is_some())
Ok(inscribed)
}

pub(crate) fn index_transaction_ordinals(
Expand All @@ -430,7 +422,6 @@ impl Updater {
txid: Txid,
ordinal_to_satpoint: &mut Table<u64, &SatPointArray>,
ordinal_to_inscription_id: &mut Table<u64, &InscriptionIdArray>,
inscription_id_to_inscription: &mut Table<&InscriptionIdArray, str>,
inscription_id_to_satpoint: &mut Table<&InscriptionIdArray, &SatPointArray>,
satpoint_to_inscription_id: &mut Table<&SatPointArray, &InscriptionIdArray>,
input_ordinal_ranges: &mut VecDeque<(u64, u64)>,
Expand All @@ -440,7 +431,6 @@ impl Updater {
if self.index_transaction_inscriptions(
tx,
txid,
inscription_id_to_inscription,
inscription_id_to_satpoint,
satpoint_to_inscription_id,
)? {
Expand Down
2 changes: 1 addition & 1 deletion src/inscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use {
std::str::{self, Utf8Error},
};

#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[derive(Debug, PartialEq)]
pub(crate) enum Inscription {
Text(String),
Png(Vec<u8>),
Expand Down
4 changes: 2 additions & 2 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ impl Server {
Path(outpoint): Path<OutPoint>,
) -> ServerResult<PageHtml> {
let output = index
.transaction(outpoint.txid)
.get_transaction(outpoint.txid)
.map_err(ServerError::Internal)?
.ok_or_else(|| ServerError::NotFound(format!("output {outpoint} unknown")))?
.output
Expand Down Expand Up @@ -464,7 +464,7 @@ impl Server {
Ok(
TransactionHtml::new(
index
.transaction(txid)
.get_transaction(txid)
.map_err(|err| {
ServerError::Internal(anyhow!(
"error serving request for transaction {txid}: {err}"
Expand Down

0 comments on commit 690b5ae

Please sign in to comment.