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

Optimize index #134

Merged
merged 2 commits into from
Feb 15, 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
33 changes: 17 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ executable-path = "1.0.0"
integer-cbrt = "0.1.2"
integer-sqrt = "0.1.5"
log = "0.4.14"
memmap2 = "0.5.3"
redb = "0.0.4"
structopt = "0.3.25"
tempfile = "3.2.0"
Expand Down
101 changes: 56 additions & 45 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ pub(crate) struct Index {
}

impl Index {
const HASH_TO_BLOCK: &'static str = "HASH_TO_BLOCK";
const HASH_TO_CHILDREN: &'static str = "HASH_TO_CHILDREN";
const HASH_TO_HEIGHT: &'static str = "HASH_TO_HEIGHT";
const HASH_TO_LOCATION: &'static str = "HASH_TO_LOCATION";
const HEIGHT_TO_HASH: &'static str = "HEIGHT_TO_HASH";
const OUTPOINT_TO_ORDINAL_RANGES: &'static str = "OUTPOINT_TO_ORDINAL_RANGES";

Expand Down Expand Up @@ -44,6 +44,8 @@ impl Index {
}

fn index_ranges(&self) -> Result {
log::info!("Indexing ranges…");

let mut height = 0;
while let Some(block) = self.block(height)? {
let wtx = self.database.begin_write()?;
Expand Down Expand Up @@ -160,42 +162,33 @@ impl Index {
Ok(())
}

fn blockfile_path(&self, i: u64) -> PathBuf {
self.blocksdir.join(format!("blk{:05}.dat", i))
}

fn index_blockfiles(&self) -> Result {
let mut blockfiles = 0;
loop {
match File::open(self.blocksdir.join(format!("blk{:05}.dat", blockfiles))) {
Ok(_) => {}
Err(err) => {
if err.kind() == io::ErrorKind::NotFound {
break;
} else {
return Err(err.into());
}
}
}
blockfiles += 1;
}
let blockfiles = (0..)
.map(|i| self.blockfile_path(i))
.take_while(|path| path.is_file())
.count();

log::info!("Indexing {} blockfiles…", blockfiles);

for i in 0.. {
let blocks = match fs::read(self.blocksdir.join(format!("blk{:05}.dat", i))) {
Ok(blocks) => blocks,
Err(err) => {
if err.kind() == io::ErrorKind::NotFound {
break;
} else {
return Err(err.into());
}
}
};
let path = self.blockfile_path(i);

if !path.is_file() {
break;
}

let blocks = unsafe { Mmap::map(&File::open(path)?)? };

let tx = self.database.begin_write()?;

let mut hash_to_children: MultimapTable<[u8], [u8]> =
tx.open_multimap_table(Self::HASH_TO_CHILDREN)?;

let mut hash_to_block: Table<[u8], [u8]> = tx.open_table(Self::HASH_TO_BLOCK)?;
let mut hash_to_location: Table<[u8], u64> = tx.open_table(Self::HASH_TO_LOCATION)?;

let mut offset = 0;

Expand All @@ -208,18 +201,15 @@ impl Index {
break;
}

let magic = &blocks[offset..offset + 4];
if magic != Network::Bitcoin.magic().to_le_bytes() {
return Err(format!("Unknown magic bytes: {:?}", magic).into());
}
let rest = &blocks[offset..];

let len = u32::from_le_bytes(blocks[offset + 4..offset + 8].try_into()?) as usize;
let start = offset + 8;
let end = start + len;
if rest.starts_with(&[0, 0, 0, 0]) {
break;
}

let bytes = &blocks[start..end];
let block = Self::extract_block(rest)?;

let header = BlockHeader::consensus_decode(&bytes[0..80])?;
let header = BlockHeader::consensus_decode(&block[0..80])?;
let hash = header.block_hash();

if header.prev_blockhash == Default::default() {
Expand All @@ -238,14 +228,14 @@ impl Index {

hash_to_children.insert(&header.prev_blockhash, &hash)?;

hash_to_block.insert(&hash, bytes)?;
hash_to_location.insert(&hash, &((i as u64) << 32 | offset as u64))?;

offset = end;
offset = offset + 8 + block.len();

count += 1;
}

log::info!("{}/{}: Processed {} blocks…", i + 1, blockfiles, count);
log::info!("{}/{}: Processed {} blocks…", i + 1, blockfiles + 1, count);

tx.commit()?;
}
Expand All @@ -254,6 +244,8 @@ impl Index {
}

fn index_heights(&self) -> Result {
log::info!("Indexing heights…");

let write = self.database.begin_write()?;

let read = self.database.begin_read()?;
Expand Down Expand Up @@ -289,6 +281,17 @@ impl Index {
Ok(())
}

fn extract_block(blocks: &[u8]) -> Result<&[u8]> {
let magic = &blocks[0..4];
if magic != Network::Bitcoin.magic().to_le_bytes() {
return Err(format!("Unknown magic bytes: {:?}", magic).into());
}

let len = u32::from_le_bytes(blocks[4..8].try_into()?) as usize;

Ok(&blocks[8..8 + len])
}

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

Expand All @@ -299,14 +302,22 @@ impl Index {
Some(guard) => {
let hash = guard.to_value();

let hash_to_block: ReadOnlyTable<[u8], [u8]> = tx.open_table(Self::HASH_TO_BLOCK)?;
let hash_to_location: ReadOnlyTable<[u8], u64> = tx.open_table(Self::HASH_TO_LOCATION)?;

let location = hash_to_location
.get(hash)?
.ok_or("Could not find block location in index")?
.to_value();

let path = self.blockfile_path(location >> 32);

let offset = (location & 0xFFFFFFFF) as usize;

let blocks = unsafe { Mmap::map(&File::open(path)?)? };

let bytes = Self::extract_block(&blocks[offset..])?;

Ok(Some(Block::consensus_decode(
hash_to_block
.get(hash)?
.ok_or("Could not find block in index")?
.to_value(),
)?))
Ok(Some(Block::consensus_decode(bytes)?))
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use {
derive_more::{Display, FromStr},
integer_cbrt::IntegerCubeRoot,
integer_sqrt::IntegerSquareRoot,
memmap2::Mmap,
redb::{
Database, MultimapTable, ReadOnlyMultimapTable, ReadOnlyTable, ReadableMultimapTable,
ReadableTable, Table,
Expand All @@ -19,8 +20,7 @@ use {
cmp::Ordering,
collections::VecDeque,
fmt::{self, Display, Formatter},
fs::{self, File},
io,
fs::File,
ops::{Add, AddAssign, Deref, Sub},
path::{Path, PathBuf},
process,
Expand Down