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

Remove find command and KEY_TO_SATPOINT table #192

Merged
merged 2 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
57 changes: 0 additions & 57 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,29 +143,14 @@ impl Index {
.get_ordinal_ranges(key.as_slice())?
.ok_or_else(|| anyhow!("Could not find outpoint in index"))?;

let new = input_ordinal_ranges.len();

for chunk in ordinal_ranges.chunks_exact(11) {
input_ordinal_ranges.push_back(Self::decode_ordinal_range(chunk.try_into().unwrap()));
}

for (start, _end) in input_ordinal_ranges.range(new..) {
wtx.remove_satpoint(
&Key {
ordinal: *start,
block: 0,
transaction: 0,
}
.encode(),
)?;
}

wtx.remove_outpoint(&key)?;
}

self.index_transaction(
height,
tx_offset as u64,
*txid,
tx,
&mut wtx,
Expand All @@ -178,8 +163,6 @@ impl Index {

if let Some((txid, tx)) = txdata.first() {
self.index_transaction(
height,
0,
*txid,
tx,
&mut wtx,
Expand Down Expand Up @@ -208,8 +191,6 @@ impl Index {

fn index_transaction(
&self,
block: u64,
tx_offset: u64,
txid: Txid,
tx: &Transaction,
wtx: &mut WriteTransaction,
Expand Down Expand Up @@ -239,22 +220,6 @@ impl Index {
range
};

let mut satpoint = Vec::new();
SatPoint {
offset: output.value - remaining,
outpoint,
}
.consensus_encode(&mut satpoint)?;
wtx.insert_satpoint(
&Key {
ordinal: assigned.0,
block,
transaction: tx_offset,
}
.encode(),
&satpoint,
)?;

let base = assigned.0;
let delta = assigned.1 - assigned.0;

Expand Down Expand Up @@ -285,28 +250,6 @@ impl Index {
}
}

pub(crate) fn find(&self, ordinal: Ordinal) -> Result<Option<(u64, u64, SatPoint)>> {
if self.database.height()? <= ordinal.height().0 {
return Ok(None);
}

match self.database.find(ordinal)? {
Some((start_key, start_satpoint)) => {
let start_key = Key::decode(&start_key)?;
let start_satpoint = SatPoint::consensus_decode(start_satpoint.as_slice())?;
Ok(Some((
start_key.block,
start_key.transaction,
SatPoint {
offset: start_satpoint.offset + (ordinal.0 - start_key.ordinal),
outpoint: start_satpoint.outpoint,
},
)))
}
None => Ok(None),
}
}

pub(crate) fn list(&self, outpoint: OutPoint) -> Result<Option<Vec<(u64, u64)>>> {
let mut outpoint_encoded = Vec::new();
outpoint.consensus_encode(&mut outpoint_encoded)?;
Expand Down
50 changes: 0 additions & 50 deletions src/key.rs

This file was deleted.

53 changes: 0 additions & 53 deletions src/lmdb_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use {
};

const HEIGHT_TO_HASH: &str = "HEIGHT_TO_HASH";
const KEY_TO_SATPOINT: &str = "KEY_TO_SATPOINT";
const OUTPOINT_TO_ORDINAL_RANGES: &str = "OUTPOINT_TO_ORDINAL_RANGES";

trait LmdbResultExt<T> {
Expand Down Expand Up @@ -62,32 +61,6 @@ impl Database {
Ok(())
}

pub(crate) fn find(&self, ordinal: Ordinal) -> Result<Option<(Vec<u8>, Vec<u8>)>> {
let key_to_satpoint = lmdb::Database::open(
&self.0,
Some(KEY_TO_SATPOINT),
&lmdb::DatabaseOptions::new(lmdb::db::CREATE),
)?;

let tx = lmdb::ReadTransaction::new(&self.0)?;

let mut cursor = tx.cursor(key_to_satpoint)?;

let key = Key::new(ordinal).encode();

let access = tx.access();
cursor
.seek_range_k::<[u8], [u8]>(&access, key.as_slice())
.into_option()?;

Ok(
cursor
.prev::<[u8], [u8]>(&access)
.into_option()?
.map(|(start_key, start_satpoint)| (start_key.to_vec(), start_satpoint.to_vec())),
)
}

pub(crate) fn height(&self) -> Result<u64> {
let height_to_hash = lmdb::Database::open(
&self.0,
Expand Down Expand Up @@ -127,7 +100,6 @@ impl Database {
pub(crate) struct WriteTransaction<'a> {
height_to_hash: lmdb::Database<'a>,
lmdb_write_transaction: lmdb::WriteTransaction<'a>,
key_to_satpoint: lmdb::Database<'a>,
outpoint_to_ordinal_ranges: lmdb::Database<'a>,
}

Expand All @@ -145,19 +117,12 @@ impl<'a> WriteTransaction<'a> {
&lmdb::DatabaseOptions::new(lmdb::db::CREATE),
)?;

let key_to_satpoint = lmdb::Database::open(
environment,
Some(KEY_TO_SATPOINT),
&lmdb::DatabaseOptions::new(lmdb::db::CREATE),
)?;

let lmdb_write_transaction = lmdb::WriteTransaction::new(environment)?;

Ok(Self {
lmdb_write_transaction,
height_to_hash,
outpoint_to_ordinal_ranges,
key_to_satpoint,
})
}

Expand Down Expand Up @@ -226,22 +191,4 @@ impl<'a> WriteTransaction<'a> {
.map(|value| value.to_vec()),
)
}

pub(crate) fn insert_satpoint(&mut self, key: &[u8], satpoint: &[u8]) -> Result {
self.lmdb_write_transaction.access().put(
&self.key_to_satpoint,
key,
satpoint,
lmdb::put::Flags::empty(),
)?;
Ok(())
}

pub(crate) fn remove_satpoint(&mut self, key: &[u8]) -> Result {
let mut cursor = self.lmdb_write_transaction.cursor(&self.key_to_satpoint)?;
let mut access = self.lmdb_write_transaction.access();
cursor.seek_range_k::<[u8], [u8]>(&access, key)?;
cursor.del(&mut access, lmdb::del::Flags::empty())?;
Ok(())
}
}
5 changes: 2 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

use {
crate::{
arguments::Arguments, bytes::Bytes, epoch::Epoch, height::Height, index::Index, key::Key,
options::Options, ordinal::Ordinal, sat_point::SatPoint, subcommand::Subcommand,
arguments::Arguments, bytes::Bytes, epoch::Epoch, height::Height, index::Index,
options::Options, ordinal::Ordinal, subcommand::Subcommand,
},
anyhow::{anyhow, Context, Error},
axum::{extract, http::StatusCode, response::IntoResponse, routing::get, Json, Router},
Expand Down Expand Up @@ -49,7 +49,6 @@ mod bytes;
mod epoch;
mod height;
mod index;
mod key;
#[cfg(not(feature = "redb"))]
mod lmdb_database;
mod options;
Expand Down
59 changes: 0 additions & 59 deletions src/redb_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use {
const HEIGHT_TO_HASH: TableDefinition<u64, [u8]> = TableDefinition::new("HEIGHT_TO_HASH");
const OUTPOINT_TO_ORDINAL_RANGES: TableDefinition<[u8], [u8]> =
TableDefinition::new("OUTPOINT_TO_ORDINAL_RANGES");
const KEY_TO_SATPOINT: TableDefinition<[u8], [u8]> = TableDefinition::new("KEY_TO_SATPOINT");

pub(crate) struct Database(redb::Database);

Expand All @@ -24,7 +23,6 @@ impl Database {

tx.open_table(&HEIGHT_TO_HASH)?;
tx.open_table(&OUTPOINT_TO_ORDINAL_RANGES)?;
tx.open_table(&KEY_TO_SATPOINT)?;

tx.commit()?;

Expand Down Expand Up @@ -66,43 +64,6 @@ impl Database {
Ok(())
}

pub(crate) fn height(&self) -> Result<u64> {
let tx = self.0.begin_read()?;

let height_to_hash = tx.open_table(&HEIGHT_TO_HASH)?;

Ok(
height_to_hash
.range(0..)?
.rev()
.next()
.map(|(height, _hash)| height + 1)
.unwrap_or(0),
)
}

pub(crate) fn find(&self, ordinal: Ordinal) -> Result<Option<(Vec<u8>, Vec<u8>)>> {
let rtx = self.0.begin_read()?;

let height_to_hash = rtx.open_table(&HEIGHT_TO_HASH)?;

match height_to_hash.range(0..)?.rev().next() {
Some((height, _hash)) if height >= ordinal.height().0 => {}
_ => return Ok(None),
}

let key_to_satpoint = rtx.open_table(&KEY_TO_SATPOINT)?;

match key_to_satpoint
.range([].as_slice()..=Key::new(ordinal).encode().as_slice())?
.rev()
.next()
{
Some((start_key, start_satpoint)) => Ok(Some((start_key.to_vec(), start_satpoint.to_vec()))),
None => Ok(None),
}
}

pub(crate) fn list(&self, outpoint: &[u8]) -> Result<Option<Vec<u8>>> {
Ok(
self
Expand All @@ -119,21 +80,18 @@ pub(crate) struct WriteTransaction<'a> {
inner: redb::DatabaseTransaction<'a>,
height_to_hash: redb::Table<'a, u64, [u8]>,
outpoint_to_ordinal_ranges: redb::Table<'a, [u8], [u8]>,
key_to_satpoint: redb::Table<'a, [u8], [u8]>,
}

impl<'a> WriteTransaction<'a> {
pub(crate) fn new(database: &'a redb::Database) -> Result<Self> {
let inner = database.begin_write()?;
let height_to_hash = inner.open_table(&HEIGHT_TO_HASH)?;
let outpoint_to_ordinal_ranges = inner.open_table(&OUTPOINT_TO_ORDINAL_RANGES)?;
let key_to_satpoint = inner.open_table(&KEY_TO_SATPOINT)?;

Ok(Self {
inner,
height_to_hash,
outpoint_to_ordinal_ranges,
key_to_satpoint,
})
}

Expand Down Expand Up @@ -178,21 +136,4 @@ impl<'a> WriteTransaction<'a> {
pub(crate) fn get_ordinal_ranges(&self, outpoint: &[u8]) -> Result<Option<&[u8]>> {
Ok(self.outpoint_to_ordinal_ranges.get(outpoint)?)
}

pub(crate) fn insert_satpoint(&mut self, key: &[u8], satpoint: &[u8]) -> Result {
self.key_to_satpoint.insert(key, satpoint)?;
Ok(())
}

pub(crate) fn remove_satpoint(&mut self, key: &[u8]) -> Result {
let key = self
.key_to_satpoint
.range(key..)?
.next()
.unwrap()
.0
.to_vec();
self.key_to_satpoint.remove(&key)?;
Ok(())
}
}
3 changes: 0 additions & 3 deletions src/subcommand.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::*;

mod epochs;
mod find;
mod index;
mod info;
mod list;
Expand All @@ -14,7 +13,6 @@ mod traits;
#[derive(Parser)]
pub(crate) enum Subcommand {
Epochs,
Find(find::Find),
Index,
List(list::List),
Name(name::Name),
Expand All @@ -29,7 +27,6 @@ impl Subcommand {
pub(crate) fn run(self, options: Options) -> Result<()> {
match self {
Self::Epochs => epochs::run(),
Self::Find(find) => find.run(options),
Self::Index => index::run(options),
Self::List(list) => list.run(options),
Self::Name(name) => name.run(),
Expand Down
Loading