Skip to content

Commit

Permalink
Hide protocol inscriptions (#2674)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Nov 15, 2023
1 parent e39031a commit ccef9a8
Show file tree
Hide file tree
Showing 13 changed files with 420 additions and 224 deletions.
15 changes: 15 additions & 0 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ define_multimap_table! { SATPOINT_TO_INSCRIPTION_ID, &SatPointValue, &Inscriptio
define_multimap_table! { SAT_TO_INSCRIPTION_ID, u64, &InscriptionIdValue }
define_table! { HEIGHT_TO_BLOCK_HASH, u64, &BlockHashValue }
define_table! { HEIGHT_TO_LAST_SEQUENCE_NUMBER, u64, u64 }
define_table! { HOME_INSCRIPTIONS, u64, &InscriptionIdValue }
define_table! { INSCRIPTION_ID_TO_INSCRIPTION_ENTRY, &InscriptionIdValue, InscriptionEntryValue }
define_table! { INSCRIPTION_ID_TO_RUNE, &InscriptionIdValue, u128 }
define_table! { INSCRIPTION_ID_TO_SATPOINT, &InscriptionIdValue, &SatPointValue }
Expand Down Expand Up @@ -274,6 +275,7 @@ impl Index {
tx.open_multimap_table(SAT_TO_INSCRIPTION_ID)?;
tx.open_table(HEIGHT_TO_BLOCK_HASH)?;
tx.open_table(HEIGHT_TO_LAST_SEQUENCE_NUMBER)?;
tx.open_table(HOME_INSCRIPTIONS)?;
tx.open_table(INSCRIPTION_ID_TO_INSCRIPTION_ENTRY)?;
tx.open_table(INSCRIPTION_ID_TO_RUNE)?;
tx.open_table(INSCRIPTION_ID_TO_SATPOINT)?;
Expand Down Expand Up @@ -1308,6 +1310,19 @@ impl Index {
))
}

pub(crate) fn get_home_inscriptions(&self) -> Result<Vec<InscriptionId>> {
Ok(
self
.database
.begin_read()?
.open_table(HOME_INSCRIPTIONS)?
.iter()?
.rev()
.flat_map(|result| result.map(|(_number, id)| InscriptionId::load(*id.value())))
.collect(),
)
}

pub(crate) fn get_feed_inscriptions(&self, n: usize) -> Result<Vec<(u64, InscriptionId)>> {
Ok(
self
Expand Down
47 changes: 31 additions & 16 deletions src/index/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,13 +378,14 @@ impl<'index> Updater<'_> {

let mut height_to_block_hash = wtx.open_table(HEIGHT_TO_BLOCK_HASH)?;
let mut height_to_last_sequence_number = wtx.open_table(HEIGHT_TO_LAST_SEQUENCE_NUMBER)?;
let mut home_inscriptions = wtx.open_table(HOME_INSCRIPTIONS)?;
let mut inscription_id_to_children = wtx.open_multimap_table(INSCRIPTION_ID_TO_CHILDREN)?;
let mut inscription_id_to_inscription_entry =
wtx.open_table(INSCRIPTION_ID_TO_INSCRIPTION_ENTRY)?;
let mut inscription_id_to_satpoint = wtx.open_table(INSCRIPTION_ID_TO_SATPOINT)?;
let mut inscription_number_to_inscription_id =
wtx.open_table(INSCRIPTION_NUMBER_TO_INSCRIPTION_ID)?;
let mut sat_to_inscription_id = wtx.open_multimap_table(SAT_TO_INSCRIPTION_ID)?;
let mut inscription_id_to_children = wtx.open_multimap_table(INSCRIPTION_ID_TO_CHILDREN)?;
let mut satpoint_to_inscription_id = wtx.open_multimap_table(SATPOINT_TO_INSCRIPTION_ID)?;
let mut sequence_number_to_inscription_id =
wtx.open_table(SEQUENCE_NUMBER_TO_INSCRIPTION_ID)?;
Expand All @@ -410,24 +411,38 @@ impl<'index> Updater<'_> {
.map(|unbound_inscriptions| unbound_inscriptions.value())
.unwrap_or(0);

let mut inscription_updater = InscriptionUpdater::new(
self.height,
&mut inscription_id_to_children,
&mut inscription_id_to_satpoint,
value_receiver,
&mut inscription_id_to_inscription_entry,
lost_sats,
&mut inscription_number_to_inscription_id,
cursed_inscription_count,
let next_sequence_number = sequence_number_to_inscription_id
.iter()?
.next_back()
.and_then(|result| result.ok())
.map(|(number, _id)| number.value() + 1)
.unwrap_or(0);

let home_inscription_count = home_inscriptions.len()?;

let mut inscription_updater = InscriptionUpdater {
blessed_inscription_count,
&mut sequence_number_to_inscription_id,
&mut outpoint_to_value,
&mut sat_to_inscription_id,
&mut satpoint_to_inscription_id,
block.header.time,
cursed_inscription_count,
flotsam: Vec::new(),
height: self.height,
home_inscription_count,
home_inscriptions: &mut home_inscriptions,
id_to_children: &mut inscription_id_to_children,
id_to_entry: &mut inscription_id_to_inscription_entry,
id_to_satpoint: &mut inscription_id_to_satpoint,
inscription_number_to_id: &mut inscription_number_to_inscription_id,
lost_sats,
next_sequence_number,
outpoint_to_value: &mut outpoint_to_value,
reward: Height(self.height).subsidy(),
sat_to_inscription_id: &mut sat_to_inscription_id,
satpoint_to_id: &mut satpoint_to_inscription_id,
sequence_number_to_id: &mut sequence_number_to_inscription_id,
timestamp: block.header.time,
unbound_inscriptions,
value_cache,
)?;
value_receiver,
};

if self.index.index_sats {
let mut sat_to_satpoint = wtx.open_table(SAT_TO_SATPOINT)?;
Expand Down
110 changes: 37 additions & 73 deletions src/index/updater/inscription_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ enum Origin {
New {
cursed: bool,
fee: u64,
hidden: bool,
parent: Option<InscriptionId>,
pointer: Option<u64>,
unbound: bool,
Expand All @@ -22,88 +23,35 @@ enum Origin {
}

pub(super) struct InscriptionUpdater<'a, 'db, 'tx> {
flotsam: Vec<Flotsam>,
height: u64,
id_to_children:
pub(super) flotsam: Vec<Flotsam>,
pub(super) height: u64,
pub(super) home_inscription_count: u64,
pub(super) home_inscriptions: &'a mut Table<'db, 'tx, u64, &'static InscriptionIdValue>,
pub(super) id_to_children:
&'a mut MultimapTable<'db, 'tx, &'static InscriptionIdValue, &'static InscriptionIdValue>,
id_to_satpoint: &'a mut Table<'db, 'tx, &'static InscriptionIdValue, &'static SatPointValue>,
value_receiver: &'a mut Receiver<u64>,
id_to_entry: &'a mut Table<'db, 'tx, &'static InscriptionIdValue, InscriptionEntryValue>,
pub(super) id_to_satpoint:
&'a mut Table<'db, 'tx, &'static InscriptionIdValue, &'static SatPointValue>,
pub(super) value_receiver: &'a mut Receiver<u64>,
pub(super) id_to_entry:
&'a mut Table<'db, 'tx, &'static InscriptionIdValue, InscriptionEntryValue>,
pub(super) lost_sats: u64,
pub(super) cursed_inscription_count: u64,
pub(super) blessed_inscription_count: u64,
pub(super) next_sequence_number: u64,
inscription_number_to_id: &'a mut Table<'db, 'tx, i64, &'static InscriptionIdValue>,
sequence_number_to_id: &'a mut Table<'db, 'tx, u64, &'static InscriptionIdValue>,
outpoint_to_value: &'a mut Table<'db, 'tx, &'static OutPointValue, u64>,
reward: u64,
sat_to_inscription_id: &'a mut MultimapTable<'db, 'tx, u64, &'static InscriptionIdValue>,
satpoint_to_id:
pub(super) inscription_number_to_id: &'a mut Table<'db, 'tx, i64, &'static InscriptionIdValue>,
pub(super) sequence_number_to_id: &'a mut Table<'db, 'tx, u64, &'static InscriptionIdValue>,
pub(super) outpoint_to_value: &'a mut Table<'db, 'tx, &'static OutPointValue, u64>,
pub(super) reward: u64,
pub(super) sat_to_inscription_id:
&'a mut MultimapTable<'db, 'tx, u64, &'static InscriptionIdValue>,
pub(super) satpoint_to_id:
&'a mut MultimapTable<'db, 'tx, &'static SatPointValue, &'static InscriptionIdValue>,
timestamp: u32,
pub(super) timestamp: u32,
pub(super) unbound_inscriptions: u64,
value_cache: &'a mut HashMap<OutPoint, u64>,
pub(super) value_cache: &'a mut HashMap<OutPoint, u64>,
}

impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> {
pub(super) fn new(
height: u64,
id_to_children: &'a mut MultimapTable<
'db,
'tx,
&'static InscriptionIdValue,
&'static InscriptionIdValue,
>,
id_to_satpoint: &'a mut Table<'db, 'tx, &'static InscriptionIdValue, &'static SatPointValue>,
value_receiver: &'a mut Receiver<u64>,
id_to_entry: &'a mut Table<'db, 'tx, &'static InscriptionIdValue, InscriptionEntryValue>,
lost_sats: u64,
inscription_number_to_id: &'a mut Table<'db, 'tx, i64, &'static InscriptionIdValue>,
cursed_inscription_count: u64,
blessed_inscription_count: u64,
sequence_number_to_id: &'a mut Table<'db, 'tx, u64, &'static InscriptionIdValue>,
outpoint_to_value: &'a mut Table<'db, 'tx, &'static OutPointValue, u64>,
sat_to_inscription_id: &'a mut MultimapTable<'db, 'tx, u64, &'static InscriptionIdValue>,
satpoint_to_id: &'a mut MultimapTable<
'db,
'tx,
&'static SatPointValue,
&'static InscriptionIdValue,
>,
timestamp: u32,
unbound_inscriptions: u64,
value_cache: &'a mut HashMap<OutPoint, u64>,
) -> Result<Self> {
let next_sequence_number = sequence_number_to_id
.iter()?
.next_back()
.and_then(|result| result.ok())
.map(|(number, _id)| number.value() + 1)
.unwrap_or(0);

Ok(Self {
flotsam: Vec::new(),
height,
id_to_children,
id_to_satpoint,
value_receiver,
id_to_entry,
lost_sats,
cursed_inscription_count,
blessed_inscription_count,
next_sequence_number,
sequence_number_to_id,
inscription_number_to_id,
outpoint_to_value,
reward: Height(height).subsidy(),
sat_to_inscription_id,
satpoint_to_id,
timestamp,
unbound_inscriptions,
value_cache,
})
}

pub(super) fn index_envelopes(
&mut self,
tx: &Transaction,
Expand Down Expand Up @@ -248,6 +196,7 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> {
origin: Origin::New {
cursed,
fee: 0,
hidden: inscription.payload.hidden(),
parent: inscription.payload.parent(),
pointer: inscription.payload.pointer(),
unbound,
Expand Down Expand Up @@ -290,6 +239,7 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> {
Origin::New {
cursed,
fee: _,
hidden,
parent,
pointer,
unbound,
Expand All @@ -300,8 +250,9 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> {
inscription_id,
offset,
origin: Origin::New {
fee: (total_input_value - total_output_value) / u64::from(id_counter),
cursed,
fee: (total_input_value - total_output_value) / u64::from(id_counter),
hidden,
parent,
pointer,
unbound,
Expand Down Expand Up @@ -444,6 +395,7 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> {
fee,
parent,
unbound,
hidden,
..
} => {
let inscription_number = if cursed {
Expand Down Expand Up @@ -500,6 +452,18 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> {
.insert(&parent.store(), &inscription_id)?;
}

if !hidden {
self
.home_inscriptions
.insert(&sequence_number, &inscription_id)?;

if self.home_inscription_count == 100 {
self.home_inscriptions.pop_first()?;
} else {
self.home_inscription_count += 1;
}
}

unbound
}
};
Expand Down
69 changes: 69 additions & 0 deletions src/inscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,40 @@ impl Inscription {

witness
}

pub(crate) fn hidden(&self) -> bool {
let Some(content_type) = self.content_type() else {
return false;
};

if content_type != "text/plain" && content_type != "text/plain;charset=utf-8" {
return false;
}

let Some(body) = &self.body else {
return false;
};

let Ok(text) = str::from_utf8(body) else {
return false;
};

let trimmed = text.trim();

if trimmed.starts_with('{') && trimmed.ends_with('}') {
return true;
}

if trimmed.starts_with("gib bc1") {
return true;
}

if trimmed.ends_with(".bitmap") {
return true;
}

false
}
}

#[cfg(test)]
Expand Down Expand Up @@ -689,4 +723,39 @@ mod tests {

assert_eq!(inscription.pointer, Some(vec![0, 1]));
}

#[test]
fn hidden() {
#[track_caller]
fn case(content_type: Option<&str>, body: Option<&str>, expected: bool) {
assert_eq!(
Inscription {
content_type: content_type.map(|content_type| content_type.as_bytes().into()),
body: body.map(|content_type| content_type.as_bytes().into()),
..Default::default()
}
.hidden(),
expected
);
}

case(None, None, false);
case(Some("foo"), None, false);
case(Some("foo"), Some("{}"), false);
case(Some("text/plain"), None, false);
case(Some("text/plain"), Some("foo{}bar"), false);

case(Some("text/plain"), Some("foo.bitmap"), true);
case(Some("text/plain"), Some("gib bc1"), true);
case(Some("text/plain"), Some("{}"), true);
case(Some("text/plain"), Some(" {} "), true);
case(Some("text/plain;charset=utf-8"), Some("foo.bitmap"), true);

assert!(!Inscription {
content_type: Some("text/plain".as_bytes().into()),
body: Some(b"{\xc3\x28}".as_slice().into()),
..Default::default()
}
.hidden());
}
}
Loading

0 comments on commit ccef9a8

Please sign in to comment.