From d7b9bc3e79d996f322e0a2e87da7287608f0f6a4 Mon Sep 17 00:00:00 2001 From: Greg Martin Date: Sun, 28 Jan 2024 14:01:49 +0000 Subject: [PATCH] Add flag `--ignore-txt-and-json` to ignore text and json inscriptions. --- src/index.rs | 4 ++-- src/index/updater.rs | 1 + src/index/updater/inscription_updater.rs | 3 ++- src/inscriptions/envelope.rs | 8 +++++++- src/options.rs | 2 ++ src/subcommand/decode.rs | 2 +- 6 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/index.rs b/src/index.rs index ce2395594d..de20de5f97 100644 --- a/src/index.rs +++ b/src/index.rs @@ -1438,7 +1438,7 @@ impl Index { } Ok(self.get_transaction(inscription_id.txid)?.and_then(|tx| { - ParsedEnvelope::from_transaction(&tx) + ParsedEnvelope::from_transaction(&tx, false) .into_iter() .nth(inscription_id.index as usize) .map(|envelope| envelope.payload) @@ -2033,7 +2033,7 @@ impl Index { return Ok(None); }; - let Some(inscription) = ParsedEnvelope::from_transaction(&transaction) + let Some(inscription) = ParsedEnvelope::from_transaction(&transaction, false) .into_iter() .nth(entry.id.index as usize) .map(|envelope| envelope.payload) diff --git a/src/index/updater.rs b/src/index/updater.rs index 7a6df6437d..27fb17ed2c 100644 --- a/src/index/updater.rs +++ b/src/index/updater.rs @@ -456,6 +456,7 @@ impl<'index> Updater<'_> { home_inscriptions: &mut home_inscriptions, id_to_sequence_number: &mut inscription_id_to_sequence_number, ignore_cursed: index.options.ignore_cursed, + ignore_txt_and_json: index.options.ignore_txt_and_json, index_transactions: self.index.index_transactions, inscription_number_to_sequence_number: &mut inscription_number_to_sequence_number, lost_sats, diff --git a/src/index/updater/inscription_updater.rs b/src/index/updater/inscription_updater.rs index b81a3ae9ca..1038ebd7c6 100644 --- a/src/index/updater/inscription_updater.rs +++ b/src/index/updater/inscription_updater.rs @@ -48,6 +48,7 @@ pub(super) struct InscriptionUpdater<'a, 'db, 'tx> { pub(super) home_inscriptions: &'a mut Table<'db, 'tx, u32, InscriptionIdValue>, pub(super) id_to_sequence_number: &'a mut Table<'db, 'tx, InscriptionIdValue, u32>, pub(super) ignore_cursed: bool, + pub(super) ignore_txt_and_json: bool, pub(super) index_transactions: bool, pub(super) inscription_number_to_sequence_number: &'a mut Table<'db, 'tx, i32, u32>, pub(super) lost_sats: u64, @@ -85,7 +86,7 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> { let total_output_value = tx.output.iter().map(|txout| txout.value).sum::(); self.tx_count += 1; - let envelopes = ParsedEnvelope::from_transaction(tx); + let envelopes = ParsedEnvelope::from_transaction(tx, self.ignore_txt_and_json); let inscriptions = !envelopes.is_empty(); let mut envelopes = envelopes.into_iter().peekable(); diff --git a/src/inscriptions/envelope.rs b/src/inscriptions/envelope.rs index 76836f7733..0ae38a74e9 100644 --- a/src/inscriptions/envelope.rs +++ b/src/inscriptions/envelope.rs @@ -89,10 +89,16 @@ impl From for ParsedEnvelope { } impl ParsedEnvelope { - pub(crate) fn from_transaction(transaction: &Transaction) -> Vec { + pub(crate) fn from_transaction(transaction: &Transaction, ignore_txt_and_json: bool) -> Vec { RawEnvelope::from_transaction(transaction) .into_iter() .map(|envelope| envelope.into()) + .filter(|envelope: &ParsedEnvelope| !ignore_txt_and_json || match envelope.payload.content_type.clone() { + Some(content_type) => + !content_type.to_vec().starts_with("text/plain".as_bytes()) && + !content_type.to_vec().starts_with("application/json".as_bytes()), + None => true, + }) .collect() } } diff --git a/src/options.rs b/src/options.rs index afe14af88a..b24fcd4d63 100644 --- a/src/options.rs +++ b/src/options.rs @@ -78,6 +78,8 @@ pub struct Options { pub(crate) ignore_cursed: bool, #[arg(long, default_value = "5000", help = "Commit changes to the index file on disk every blocks.")] pub(crate) commit: usize, + #[arg(long, help = "Ignore text and json inscriptions.")] + pub(crate) ignore_txt_and_json: bool, } impl Options { diff --git a/src/subcommand/decode.rs b/src/subcommand/decode.rs index 38d2275524..e31d1b8cfc 100644 --- a/src/subcommand/decode.rs +++ b/src/subcommand/decode.rs @@ -85,7 +85,7 @@ impl Decode { Transaction::consensus_decode(&mut io::stdin())? }; - let inscriptions = ParsedEnvelope::from_transaction(&transaction); + let inscriptions = ParsedEnvelope::from_transaction(&transaction, false); if self.compact { Ok(Box::new(CompactOutput {