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

Make table names more explicit #654

Merged
merged 2 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
48 changes: 25 additions & 23 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ use {

mod rtx;

const HASH_TO_RUNE: TableDefinition<[u8; 32], str> = TableDefinition::new("HASH_TO_RUNE");
const HEIGHT_TO_HASH: TableDefinition<u64, [u8; 32]> = TableDefinition::new("HEIGHT_TO_HASH");
const ORDINAL_TO_SATPOINT: TableDefinition<u64, [u8; 44]> =
TableDefinition::new("ORDINAL_TO_SATPOINT");
const HEIGHT_TO_BLOCK_HASH: TableDefinition<u64, [u8; 32]> =
TableDefinition::new("HEIGHT_TO_BLOCK_HASH");
const ORDINAL_TO_RUNE_HASHES: MultimapTableDefinition<u64, [u8; 32]> =
MultimapTableDefinition::new("ORDINAL_TO_RUNE_HASHES");
const ORDINAL_TO_SATPOINT: TableDefinition<u64, [u8; 44]> =
TableDefinition::new("ORDINAL_TO_SATPOINT");
const OUTPOINT_TO_ORDINAL_RANGES: TableDefinition<[u8; 36], [u8]> =
TableDefinition::new("OUTPOINT_TO_ORDINAL_RANGES");
const STATISTICS: TableDefinition<u64, u64> = TableDefinition::new("STATISTICS");
const RUNE_HASH_TO_RUNE: TableDefinition<[u8; 32], str> = TableDefinition::new("RUNE_HASH_TO_RUNE");
const STATISTIC_TO_COUNT: TableDefinition<u64, u64> = TableDefinition::new("STATISTIC_TO_COUNT");

fn encode_outpoint(outpoint: OutPoint) -> [u8; 36] {
let mut array = [0; 36];
Expand Down Expand Up @@ -139,11 +140,11 @@ impl Index {
};

tx.open_multimap_table(ORDINAL_TO_RUNE_HASHES)?;
tx.open_table(HASH_TO_RUNE)?;
tx.open_table(HEIGHT_TO_HASH)?;
tx.open_table(RUNE_HASH_TO_RUNE)?;
tx.open_table(HEIGHT_TO_BLOCK_HASH)?;
tx.open_table(ORDINAL_TO_SATPOINT)?;
tx.open_table(OUTPOINT_TO_ORDINAL_RANGES)?;
tx.open_table(STATISTICS)?;
tx.open_table(STATISTIC_TO_COUNT)?;

tx.commit()?;

Expand All @@ -165,7 +166,7 @@ impl Index {
let wtx = self.begin_write()?;

let blocks_indexed = wtx
.open_table(HEIGHT_TO_HASH)?
.open_table(HEIGHT_TO_BLOCK_HASH)?
.range(0..)?
.rev()
.next()
Expand All @@ -175,7 +176,7 @@ impl Index {
let utxos_indexed = wtx.open_table(OUTPOINT_TO_ORDINAL_RANGES)?.len()?;

let outputs_traversed = wtx
.open_table(STATISTICS)?
.open_table(STATISTIC_TO_COUNT)?
.get(&Statistic::OutputsTraversed.into())?
.unwrap_or(0);

Expand Down Expand Up @@ -217,7 +218,7 @@ impl Index {
let mut wtx = self.begin_write()?;

let height = wtx
.open_table(HEIGHT_TO_HASH)?
.open_table(HEIGHT_TO_BLOCK_HASH)?
.range(0..)?
.rev()
.next()
Expand Down Expand Up @@ -263,7 +264,7 @@ impl Index {
}

pub(crate) fn index_block(&self, wtx: &mut WriteTransaction, height: u64) -> Result<bool> {
let mut height_to_hash = wtx.open_table(HEIGHT_TO_HASH)?;
let mut height_to_block_hash = wtx.open_table(HEIGHT_TO_BLOCK_HASH)?;
let mut ordinal_to_satpoint = wtx.open_table(ORDINAL_TO_SATPOINT)?;
let mut outpoint_to_ordinal_ranges = wtx.open_table(OUTPOINT_TO_ORDINAL_RANGES)?;

Expand Down Expand Up @@ -309,7 +310,7 @@ impl Index {
);

if let Some(prev_height) = height.checked_sub(1) {
let prev_hash = height_to_hash.get(&prev_height)?.unwrap();
let prev_hash = height_to_block_hash.get(&prev_height)?.unwrap();

if prev_hash != block.header.prev_blockhash.as_ref() {
self.reorged.store(true, Ordering::Relaxed);
Expand Down Expand Up @@ -369,13 +370,14 @@ impl Index {
tx,
&mut ordinal_to_satpoint,
&mut outpoint_to_ordinal_ranges,
&mut txid_to_prime_ordinals,
&mut coinbase_inputs,
&mut ordinal_ranges_written,
&mut outputs_in_block,
)?;
}

height_to_hash.insert(&height, &block.block_hash().as_hash().into_inner())?;
height_to_block_hash.insert(&height, &block.block_hash().as_hash().into_inner())?;

Self::increment_statistic(wtx, Statistic::OutputsTraversed, outputs_in_block)?;

Expand All @@ -402,10 +404,10 @@ impl Index {
}

fn increment_statistic(wtx: &WriteTransaction, statistic: Statistic, n: u64) -> Result {
let mut statistics = wtx.open_table(STATISTICS)?;
statistics.insert(
let mut statistic_to_count = wtx.open_table(STATISTIC_TO_COUNT)?;
statistic_to_count.insert(
&statistic.into(),
&(statistics.get(&(statistic.into()))?.unwrap_or(0) + n),
&(statistic_to_count.get(&(statistic.into()))?.unwrap_or(0) + n),
)?;
Ok(())
}
Expand All @@ -416,7 +418,7 @@ impl Index {
self
.database
.begin_read()?
.open_table(STATISTICS)?
.open_table(STATISTIC_TO_COUNT)?
.get(&(statistic.into()))?
.unwrap_or(0),
)
Expand All @@ -433,9 +435,9 @@ impl Index {

let height = rtx.height()?;

let height_to_hash = rtx.0.open_table(HEIGHT_TO_HASH)?;
let height_to_block_hash = rtx.0.open_table(HEIGHT_TO_BLOCK_HASH)?;

let mut cursor = height_to_hash
let mut cursor = height_to_block_hash
.range(height.saturating_sub(take.saturating_sub(1))..=height)?
.rev();

Expand Down Expand Up @@ -591,7 +593,7 @@ impl Index {
self
.database
.begin_read()?
.open_table(HASH_TO_RUNE)?
.open_table(RUNE_HASH_TO_RUNE)?
.get(hash.as_inner())?
.map(serde_json::from_str)
.transpose()?,
Expand All @@ -604,7 +606,7 @@ impl Index {
let wtx = self.begin_write()?;

let created = wtx
.open_table(HASH_TO_RUNE)?
.open_table(RUNE_HASH_TO_RUNE)?
.insert(hash.as_inner(), &json)?
.is_none();

Expand Down Expand Up @@ -688,7 +690,7 @@ impl Index {
let tx = self.database.begin_read()?;

let current = tx
.open_table(HEIGHT_TO_HASH)?
.open_table(HEIGHT_TO_BLOCK_HASH)?
.range(0..)?
.rev()
.next()
Expand Down
2 changes: 1 addition & 1 deletion src/index/rtx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ impl Rtx<'_> {
Ok(
self
.0
.open_table(HEIGHT_TO_HASH)?
.open_table(HEIGHT_TO_BLOCK_HASH)?
.range(0..)?
.rev()
.next()
Expand Down