From 1a1f438c1d27c1a4f22b24fad1338a61344c0d1b Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Thu, 1 Dec 2022 12:14:56 -0800 Subject: [PATCH 1/2] Don't store inscriptions in database --- src/index.rs | 30 +++++------------------------- src/index/updater.rs | 16 +++------------- src/subcommand/server.rs | 4 ++-- 3 files changed, 10 insertions(+), 40 deletions(-) diff --git a/src/index.rs b/src/index.rs index 1637a91ca5..7dd712ced2 100644 --- a/src/index.rs +++ b/src/index.rs @@ -28,8 +28,6 @@ const ORDINAL_TO_SATPOINT: TableDefinition = const OUTPOINT_TO_ORDINAL_RANGES: TableDefinition<&OutPointArray, [u8]> = TableDefinition::new("OUTPOINT_TO_ORDINAL_RANGES"); const STATISTIC_TO_COUNT: TableDefinition = 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 = TableDefinition::new("WRITE_TRANSACTION_START_BLOCK_COUNT_TO_TIMESTAMP"); const INSCRIPTION_ID_TO_SATPOINT: TableDefinition<&InscriptionIdArray, &SatPointArray> = @@ -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)?; @@ -436,14 +433,8 @@ 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), ) } @@ -451,19 +442,8 @@ impl Index { &self, txid: Txid, ) -> Result> { - 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( @@ -478,7 +458,7 @@ impl Index { Ok(Some((inscription, satpoint))) } - pub(crate) fn transaction(&self, txid: Txid) -> Result> { + pub(crate) fn get_transaction(&self, txid: Txid) -> Result> { if txid == self.genesis_block_coinbase_txid { Ok(Some(self.genesis_block_coinbase_transaction.clone())) } else { diff --git a/src/index/updater.rs b/src/index/updater.rs index 2deab09eb4..f1e35cbf00 100644 --- a/src/index/updater.rs +++ b/src/index/updater.rs @@ -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)?; @@ -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, @@ -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, @@ -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, )?; @@ -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 { - 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())?; }; @@ -421,7 +413,7 @@ impl Updater { } } - Ok(inscription.is_some()) + Ok(inscribed) } pub(crate) fn index_transaction_ordinals( @@ -430,7 +422,6 @@ impl Updater { txid: Txid, ordinal_to_satpoint: &mut Table, ordinal_to_inscription_id: &mut Table, - 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)>, @@ -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, )? { diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index 48148d3d38..f7e75eda03 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -333,7 +333,7 @@ impl Server { Path(outpoint): Path, ) -> ServerResult { let output = index - .transaction(outpoint.txid) + .get_transaction(outpoint.txid) .map_err(ServerError::Internal)? .ok_or_else(|| ServerError::NotFound(format!("output {outpoint} unknown")))? .output @@ -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}" From 0be5eaf5b6d54b016b5cc307cb4608a7746be7a9 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Thu, 1 Dec 2022 13:16:13 -0800 Subject: [PATCH 2/2] Don't derive serde traits for Inscription --- src/inscription.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/inscription.rs b/src/inscription.rs index 951bcca6b0..e9b9d399eb 100644 --- a/src/inscription.rs +++ b/src/inscription.rs @@ -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),