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

Limit blocks #320

Merged
merged 9 commits into from
Aug 17, 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
31 changes: 15 additions & 16 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use {
redb::WriteStrategy,
};

mod rtx;

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");
Expand Down Expand Up @@ -231,29 +233,26 @@ impl Index {
Ok(false)
}

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

let height_to_hash = tx.open_table(HEIGHT_TO_HASH)?;
fn begin_read(&self) -> Result<rtx::Rtx> {
Ok(rtx::Rtx(self.database.begin_read()?))
}

Ok(
height_to_hash
.range(0..)?
.rev()
.next()
.map(|(height, _hash)| height)
.unwrap_or(0),
)
pub(crate) fn height(&self) -> Result<u64> {
self.begin_read()?.height()
}

pub(crate) fn all(&self) -> Result<Vec<(u64, BlockHash)>> {
pub(crate) fn blocks(&self, take: u64) -> Result<Vec<(u64, BlockHash)>> {
let mut blocks = Vec::new();

let tx = self.database.begin_read()?;
let rtx = self.begin_read()?;

let height = rtx.height()?;

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

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

while let Some(next) = cursor.next() {
blocks.push((next.0, BlockHash::from_slice(next.1)?));
Expand Down
18 changes: 18 additions & 0 deletions src/index/rtx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use super::*;

pub(crate) struct Rtx<'a>(pub(crate) redb::ReadTransaction<'a>);

impl Rtx<'_> {
pub(crate) fn height(&self) -> Result<u64> {
let height_to_hash = self.0.open_table(HEIGHT_TO_HASH)?;

Ok(
height_to_hash
.range(0..)?
.rev()
.next()
.map(|(height, _hash)| height)
.unwrap_or(0),
)
}
}
2 changes: 1 addition & 1 deletion src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ impl Server {
}

async fn root(index: extract::Extension<Arc<Index>>) -> impl IntoResponse {
match index.all() {
match index.blocks(100) {
Ok(blocks) => RootHtml { blocks }.page().into_response(),
Err(err) => {
eprintln!("Error getting blocks: {err}");
Expand Down
13 changes: 13 additions & 0 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,19 @@ fn root() {
);
}

#[test]
fn root_block_limit() {
let mut state = State::new();

state.blocks(200);

state.request_regex(
"/",
200,
".*<ul>\n( <li>[[:digit:]]{3} - <a href=/block/[[:xdigit:]]{64}>[[:xdigit:]]{64}</a></li>\n){101}</ul>.*"
);
}

#[test]
fn block() {
let mut state = State::new();
Expand Down