Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sequence number #2460

Merged
merged 11 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 45 additions & 46 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ mod reorg;
mod rtx;
mod updater;

const SCHEMA_VERSION: u64 = 6;
const SCHEMA_VERSION: u64 = 7;

macro_rules! define_table {
($name:ident, $key:ty, $value:ty) => {
Expand All @@ -48,14 +48,16 @@ define_multimap_table! { INSCRIPTION_ID_TO_CHILDREN, &InscriptionIdValue, &Inscr
define_multimap_table! { SATPOINT_TO_INSCRIPTION_ID, &SatPointValue, &InscriptionIdValue }
define_multimap_table! { SAT_TO_INSCRIPTION_ID, u64, &InscriptionIdValue }
define_table! { HEIGHT_TO_BLOCK_HASH, u64, &BlockHashValue }
define_table! { HEIGHT_TO_LAST_INSCRIPTION_NUMBER, u64, (i64, i64) }
define_table! { HEIGHT_TO_LAST_SEQUENCE_NUMBER, u64, u64 }
define_table! { INSCRIPTION_ID_TO_INSCRIPTION_ENTRY, &InscriptionIdValue, InscriptionEntryValue }
define_table! { INSCRIPTION_ID_TO_SATPOINT, &InscriptionIdValue, &SatPointValue }
define_table! { INSCRIPTION_NUMBER_TO_INSCRIPTION_ID, i64, &InscriptionIdValue }
define_table! { OUTPOINT_TO_SAT_RANGES, &OutPointValue, &[u8] }
define_table! { OUTPOINT_TO_VALUE, &OutPointValue, u64}
// maybe can get rid of this or use actual number
define_table! { REINSCRIPTION_ID_TO_SEQUENCE_NUMBER, &InscriptionIdValue, u64 }
define_table! { SAT_TO_SATPOINT, u64, &SatPointValue }
define_table! { SEQUENCE_NUMBER_TO_INSCRIPTION_ID, u64, &InscriptionIdValue }
define_table! { STATISTIC_TO_COUNT, u64, u64 }
define_table! { WRITE_TRANSACTION_STARTING_BLOCK_COUNT_TO_TIMESTAMP, u64, u128 }

Expand Down Expand Up @@ -238,13 +240,14 @@ impl Index {
tx.open_multimap_table(SATPOINT_TO_INSCRIPTION_ID)?;
tx.open_multimap_table(SAT_TO_INSCRIPTION_ID)?;
tx.open_table(HEIGHT_TO_BLOCK_HASH)?;
tx.open_table(HEIGHT_TO_LAST_INSCRIPTION_NUMBER)?;
tx.open_table(HEIGHT_TO_LAST_SEQUENCE_NUMBER)?;
tx.open_table(INSCRIPTION_ID_TO_INSCRIPTION_ENTRY)?;
tx.open_table(INSCRIPTION_ID_TO_SATPOINT)?;
tx.open_table(INSCRIPTION_NUMBER_TO_INSCRIPTION_ID)?;
tx.open_table(OUTPOINT_TO_VALUE)?;
tx.open_table(REINSCRIPTION_ID_TO_SEQUENCE_NUMBER)?;
tx.open_table(SAT_TO_SATPOINT)?;
tx.open_table(SEQUENCE_NUMBER_TO_INSCRIPTION_ID)?;
tx.open_table(WRITE_TRANSACTION_STARTING_BLOCK_COUNT_TO_TIMESTAMP)?;

tx.open_table(STATISTIC_TO_COUNT)?
Expand Down Expand Up @@ -1003,34 +1006,30 @@ impl Index {
pub(crate) fn get_inscriptions_in_block(&self, block_height: u64) -> Result<Vec<InscriptionId>> {
let rtx = self.database.begin_read()?;

let height_to_last_inscription_number = rtx.open_table(HEIGHT_TO_LAST_INSCRIPTION_NUMBER)?;
let inscription_id_by_number = rtx.open_table(INSCRIPTION_NUMBER_TO_INSCRIPTION_ID)?;

let block_inscriptions = match (
height_to_last_inscription_number
.get(block_height.saturating_sub(1))?
.map(|ag| ag.value())
.unwrap_or((0, -1)),
height_to_last_inscription_number
.get(&block_height)?
.map(|ag| ag.value()),
) {
((oldest_blessed, oldest_cursed), Some((newest_blessed, newest_cursed))) => {
((newest_cursed + 1)..=oldest_cursed)
.chain(oldest_blessed..newest_blessed)
.map(|num| match inscription_id_by_number.get(&num) {
Ok(Some(inscription_id)) => Ok(InscriptionId::load(*inscription_id.value())),
Ok(None) => Err(anyhow!(
"could not find inscription for inscription number {num}"
)),
Err(err) => Err(anyhow!(err)),
})
.collect::<Result<Vec<InscriptionId>>>()?
}
_ => Vec::new(),
let height_to_last_sequence_number = rtx.open_table(HEIGHT_TO_LAST_SEQUENCE_NUMBER)?;
let sequence_number_to_inscription_id = rtx.open_table(SEQUENCE_NUMBER_TO_INSCRIPTION_ID)?;

let Some(newest_sequence_number) = height_to_last_sequence_number
.get(&block_height)?
.map(|ag| ag.value())
else {
return Ok(Vec::new());
};

Ok(block_inscriptions)
let oldest_sequence_number = height_to_last_sequence_number
.get(block_height.saturating_sub(1))?
.map(|ag| ag.value())
.unwrap_or(0);

(oldest_sequence_number..newest_sequence_number)
.map(|num| match sequence_number_to_inscription_id.get(&num) {
Ok(Some(inscription_id)) => Ok(InscriptionId::load(*inscription_id.value())),
Ok(None) => Err(anyhow!(
"could not find inscription for inscription number {num}"
)),
Err(err) => Err(anyhow!(err)),
})
.collect::<Result<Vec<InscriptionId>>>()
}

pub(crate) fn get_highest_paying_inscriptions_in_block(
Expand Down Expand Up @@ -2707,7 +2706,7 @@ mod tests {
.get_inscription_entry(inscription_id)
.unwrap()
.unwrap()
.number,
.inscription_number,
-1
);
}
Expand Down Expand Up @@ -2751,7 +2750,7 @@ mod tests {
.get_inscription_entry(inscription_id)
.unwrap()
.unwrap()
.number,
.inscription_number,
0
);
}
Expand Down Expand Up @@ -2797,7 +2796,7 @@ mod tests {
.get_inscription_entry(second_inscription_id)
.unwrap()
.unwrap()
.number,
.inscription_number,
-1
);
}
Expand Down Expand Up @@ -2860,7 +2859,7 @@ mod tests {
.get_inscription_entry(first)
.unwrap()
.unwrap()
.number,
.inscription_number,
0
);

Expand All @@ -2870,7 +2869,7 @@ mod tests {
.get_inscription_entry(second)
.unwrap()
.unwrap()
.number,
.inscription_number,
-1
);

Expand All @@ -2880,7 +2879,7 @@ mod tests {
.get_inscription_entry(third)
.unwrap()
.unwrap()
.number,
.inscription_number,
-2
);
}
Expand Down Expand Up @@ -2965,7 +2964,7 @@ mod tests {
.get_inscription_entry(first)
.unwrap()
.unwrap()
.number,
.inscription_number,
0
);

Expand All @@ -2975,7 +2974,7 @@ mod tests {
.get_inscription_entry(second)
.unwrap()
.unwrap()
.number,
.inscription_number,
-1
);

Expand All @@ -2985,7 +2984,7 @@ mod tests {
.get_inscription_entry(third)
.unwrap()
.unwrap()
.number,
.inscription_number,
-2
);
}
Expand Down Expand Up @@ -3075,7 +3074,7 @@ mod tests {
.get_inscription_entry(first)
.unwrap()
.unwrap()
.number,
.inscription_number,
0
);

Expand All @@ -3085,7 +3084,7 @@ mod tests {
.get_inscription_entry(fourth)
.unwrap()
.unwrap()
.number,
.inscription_number,
-3
);

Expand All @@ -3095,7 +3094,7 @@ mod tests {
.get_inscription_entry(ninth)
.unwrap()
.unwrap()
.number,
.inscription_number,
-8
);
}
Expand Down Expand Up @@ -3207,7 +3206,7 @@ mod tests {
.get_inscription_entry(cursed)
.unwrap()
.unwrap()
.number,
.inscription_number,
-1
);

Expand Down Expand Up @@ -3243,7 +3242,7 @@ mod tests {
.get_inscription_entry(reinscription_on_cursed)
.unwrap()
.unwrap()
.number,
.inscription_number,
1
);
}
Expand Down Expand Up @@ -3288,7 +3287,7 @@ mod tests {
.get_inscription_entry(cursed)
.unwrap()
.unwrap()
.number,
.inscription_number,
-1
);

Expand Down Expand Up @@ -3324,7 +3323,7 @@ mod tests {
.get_inscription_entry(reinscription_on_cursed)
.unwrap()
.unwrap()
.number,
.inscription_number,
1
);

Expand Down Expand Up @@ -3360,7 +3359,7 @@ mod tests {
.get_inscription_entry(second_reinscription_on_cursed)
.unwrap()
.unwrap()
.number,
.inscription_number,
-2
);

Expand Down
15 changes: 10 additions & 5 deletions src/index/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,26 @@ impl Entry for BlockHash {
pub(crate) struct InscriptionEntry {
pub(crate) fee: u64,
pub(crate) height: u64,
pub(crate) number: i64,
pub(crate) inscription_number: i64,
pub(crate) sequence_number: u64,
pub(crate) parent: Option<InscriptionId>,
pub(crate) sat: Option<Sat>,
pub(crate) timestamp: u32,
}

pub(crate) type InscriptionEntryValue = (u64, u64, i64, ParentValue, u64, u32);
pub(crate) type InscriptionEntryValue = (u64, u64, i64, u64, ParentValue, u64, u32);

impl Entry for InscriptionEntry {
type Value = InscriptionEntryValue;

fn load((fee, height, number, parent, sat, timestamp): InscriptionEntryValue) -> Self {
fn load(
(fee, height, inscription_number, sequence_number, parent, sat, timestamp): InscriptionEntryValue,
) -> Self {
Self {
fee,
height,
number,
inscription_number,
sequence_number,
parent: ParentEntry::load(parent),
sat: if sat == u64::MAX {
None
Expand All @@ -56,7 +60,8 @@ impl Entry for InscriptionEntry {
(
self.fee,
self.height,
self.number,
self.inscription_number,
self.sequence_number,
self.parent.store(),
match self.sat {
Some(sat) => sat.n(),
Expand Down
18 changes: 7 additions & 11 deletions src/index/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,7 @@ impl<'index> Updater<'_> {
}

let mut height_to_block_hash = wtx.open_table(HEIGHT_TO_BLOCK_HASH)?;
let mut height_to_last_inscription_number =
wtx.open_table(HEIGHT_TO_LAST_INSCRIPTION_NUMBER)?;
let mut height_to_last_sequence_number = wtx.open_table(HEIGHT_TO_LAST_SEQUENCE_NUMBER)?;
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)?;
Expand All @@ -389,6 +388,8 @@ impl<'index> Updater<'_> {
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)?;
let mut statistic_to_count = wtx.open_table(STATISTIC_TO_COUNT)?;

let mut lost_sats = statistic_to_count
Expand All @@ -409,6 +410,7 @@ impl<'index> Updater<'_> {
&mut inscription_id_to_inscription_entry,
lost_sats,
&mut inscription_number_to_inscription_id,
&mut sequence_number_to_inscription_id,
&mut outpoint_to_value,
&mut reinscription_id_to_seq_num,
&mut sat_to_inscription_id,
Expand Down Expand Up @@ -515,7 +517,7 @@ impl<'index> Updater<'_> {
}

self.index_block_inscription_numbers(
&mut height_to_last_inscription_number,
&mut height_to_last_sequence_number,
&inscription_updater,
index_inscriptions,
)?;
Expand Down Expand Up @@ -608,21 +610,15 @@ impl<'index> Updater<'_> {

fn index_block_inscription_numbers(
&mut self,
height_to_inscription_number: &mut Table<u64, (i64, i64)>,
height_to_sequence_number: &mut Table<u64, u64>,
inscription_updater: &InscriptionUpdater,
index_inscription: bool,
) -> Result {
if !index_inscription {
return Ok(());
}

height_to_inscription_number.insert(
&self.height,
(
inscription_updater.next_number,
inscription_updater.next_cursed_number,
),
)?;
height_to_sequence_number.insert(&self.height, inscription_updater.next_sequence_number)?;

Ok(())
}
Expand Down
Loading
Loading