mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-26 07:39:59 -07:00
snapshot
This commit is contained in:
@@ -5,7 +5,6 @@ use brk_computer::Computer;
|
||||
use brk_error::Result;
|
||||
use brk_fetcher::Fetcher;
|
||||
use brk_indexer::{Indexer, Indexes};
|
||||
use brk_iterator::Blocks;
|
||||
use brk_reader::Reader;
|
||||
use brk_rpc::{Auth, Client};
|
||||
use log::{debug, info};
|
||||
@@ -37,9 +36,7 @@ fn run() -> Result<()> {
|
||||
|
||||
let reader = Reader::new(bitcoin_dir.join("blocks"), &client);
|
||||
|
||||
let blocks = Blocks::new(&client, &reader);
|
||||
|
||||
let mut indexer = Indexer::forced_import(&outputs_dir)?;
|
||||
let indexer = Indexer::forced_import(&outputs_dir)?;
|
||||
|
||||
let fetcher = Fetcher::import(true, None)?;
|
||||
|
||||
|
||||
97
crates/brk_computer/examples/computer_read.rs
Normal file
97
crates/brk_computer/examples/computer_read.rs
Normal file
@@ -0,0 +1,97 @@
|
||||
use std::{env, path::Path, thread};
|
||||
|
||||
use brk_computer::Computer;
|
||||
use brk_error::Result;
|
||||
use brk_fetcher::Fetcher;
|
||||
use brk_indexer::Indexer;
|
||||
use brk_types::TxIndex;
|
||||
use vecdb::{AnyStoredVec, Exit, GenericStoredVec};
|
||||
|
||||
pub fn main() -> Result<()> {
|
||||
// Can't increase main thread's stack size, thus we need to use another thread
|
||||
thread::Builder::new()
|
||||
.stack_size(512 * 1024 * 1024)
|
||||
.spawn(run)?
|
||||
.join()
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn run() -> Result<()> {
|
||||
brk_logger::init(Some(Path::new(".log")))?;
|
||||
|
||||
let outputs_dir = Path::new(&env::var("HOME").unwrap()).join(".brk");
|
||||
// let outputs_dir = Path::new("../../_outputs");
|
||||
|
||||
let indexer = Indexer::forced_import(&outputs_dir)?;
|
||||
|
||||
let fetcher = Fetcher::import(true, None)?;
|
||||
|
||||
let exit = Exit::new();
|
||||
exit.set_ctrlc_handler();
|
||||
|
||||
let computer = Computer::forced_import(&outputs_dir, &indexer, Some(fetcher))?;
|
||||
|
||||
let txindex = TxIndex::new(134217893);
|
||||
|
||||
dbg!(
|
||||
indexer
|
||||
.vecs
|
||||
.txindex_to_txid
|
||||
.read_once(txindex)
|
||||
.unwrap()
|
||||
.to_string()
|
||||
);
|
||||
let first_txinindex = indexer.vecs.txindex_to_first_txinindex.read_once(txindex)?;
|
||||
dbg!(first_txinindex);
|
||||
let first_txoutindex = indexer
|
||||
.vecs
|
||||
.txindex_to_first_txoutindex
|
||||
.read_once(txindex)?;
|
||||
dbg!(first_txoutindex);
|
||||
let input_count = *computer.indexes.txindex_to_input_count.read_once(txindex)?;
|
||||
dbg!(input_count);
|
||||
let output_count = *computer
|
||||
.indexes
|
||||
.txindex_to_output_count
|
||||
.read_once(txindex)?;
|
||||
dbg!(output_count);
|
||||
dbg!(
|
||||
computer
|
||||
.indexes
|
||||
.txinindex_to_txoutindex
|
||||
.read_once(first_txinindex)
|
||||
);
|
||||
dbg!(
|
||||
computer
|
||||
.indexes
|
||||
.txinindex_to_txoutindex
|
||||
.read_once(first_txinindex + 1)
|
||||
);
|
||||
dbg!(computer.chain.txinindex_to_value.read_once(first_txinindex));
|
||||
dbg!(
|
||||
computer
|
||||
.chain
|
||||
.txinindex_to_value
|
||||
.read_once(first_txinindex + 1)
|
||||
);
|
||||
dbg!(indexer.vecs.txoutindex_to_value.read_once(first_txoutindex));
|
||||
dbg!(
|
||||
indexer
|
||||
.vecs
|
||||
.txoutindex_to_value
|
||||
.read_once(first_txoutindex + 1)
|
||||
);
|
||||
dbg!(computer.chain.txindex_to_input_value.read_once(txindex));
|
||||
dbg!(computer.chain.txindex_to_input_value.read_once(txindex));
|
||||
dbg!(computer.chain.txindex_to_output_value.read_once(txindex));
|
||||
// dbg!(computer.indexes.txindex_to_txindex.ge(txindex));
|
||||
dbg!(
|
||||
computer
|
||||
.indexes
|
||||
.txinindex_to_txoutindex
|
||||
.region()
|
||||
.meta()
|
||||
.len()
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
@@ -10,8 +10,8 @@ use brk_types::{
|
||||
TxVersion, Version, WeekIndex, Weight, YearIndex,
|
||||
};
|
||||
use vecdb::{
|
||||
AnyVec, Database, EagerVec, Exit, GenericStoredVec, IterableCloneableVec, IterableVec,
|
||||
LazyVecFrom1, LazyVecFrom2, PAGE_SIZE, TypedVecIterator, VecIndex, unlikely,
|
||||
Database, EagerVec, Exit, GenericStoredVec, IterableCloneableVec, IterableVec, LazyVecFrom1,
|
||||
LazyVecFrom2, PAGE_SIZE, TypedVecIterator, VecIndex, unlikely,
|
||||
};
|
||||
|
||||
use crate::grouped::{
|
||||
@@ -1311,20 +1311,6 @@ impl Vecs {
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// Debug: verify the computed txinindex_to_value
|
||||
dbg!("txinindex_to_value first 20:");
|
||||
for i in 0..20.min(self.txinindex_to_value.len()) {
|
||||
let val = self.txinindex_to_value.read_at_unwrap_once(i);
|
||||
dbg!((TxInIndex::from(i), val));
|
||||
}
|
||||
|
||||
// Debug: verify the computed txindex_to_input_count
|
||||
dbg!("txindex_to_input_count first 20:");
|
||||
for i in 0..20.min(indexes.txindex_to_input_count.len()) {
|
||||
let val = indexes.txindex_to_input_count.read_at_unwrap_once(i);
|
||||
dbg!((TxInIndex::from(i), val));
|
||||
}
|
||||
|
||||
self.txindex_to_input_value.compute_sum_from_indexes(
|
||||
starting_indexes.txindex,
|
||||
&indexer.vecs.txindex_to_first_txinindex,
|
||||
@@ -1333,12 +1319,6 @@ impl Vecs {
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// Debug: verify the computed input values
|
||||
for i in 0..10.min(self.txindex_to_input_value.len()) {
|
||||
let val = self.txindex_to_input_value.read_at_unwrap_once(i);
|
||||
dbg!((TxIndex::from(i), "input_value", val));
|
||||
}
|
||||
|
||||
self.txindex_to_output_value.compute_sum_from_indexes(
|
||||
starting_indexes.txindex,
|
||||
&indexer.vecs.txindex_to_first_txoutindex,
|
||||
@@ -1347,26 +1327,17 @@ impl Vecs {
|
||||
exit,
|
||||
)?;
|
||||
|
||||
// Debug: verify the computed output values
|
||||
for i in 0..10.min(self.txindex_to_output_value.len()) {
|
||||
let val = self.txindex_to_output_value.read_at_unwrap_once(i);
|
||||
dbg!((TxIndex::from(i), "output_value", val));
|
||||
}
|
||||
|
||||
self.txindex_to_fee.compute_transform2(
|
||||
starting_indexes.txindex,
|
||||
&self.txindex_to_input_value,
|
||||
&self.txindex_to_output_value,
|
||||
|(i, input, output, ..)| {
|
||||
(
|
||||
i,
|
||||
if unlikely(input.is_max()) {
|
||||
Sats::ZERO
|
||||
} else {
|
||||
dbg!((i, input, output));
|
||||
input.checked_sub(output).unwrap()
|
||||
},
|
||||
)
|
||||
let fee = if unlikely(input.is_max()) {
|
||||
Sats::ZERO
|
||||
} else {
|
||||
input - output
|
||||
};
|
||||
(i, fee)
|
||||
},
|
||||
exit,
|
||||
)?;
|
||||
@@ -1381,11 +1352,12 @@ impl Vecs {
|
||||
|
||||
self.indexes_to_sent
|
||||
.compute_all(indexes, price, starting_indexes, exit, |v| {
|
||||
v.compute_sum_from_indexes(
|
||||
v.compute_filtered_sum_from_indexes(
|
||||
starting_indexes.height,
|
||||
&indexer.vecs.height_to_first_txindex,
|
||||
&indexes.height_to_txindex_count,
|
||||
&self.txindex_to_input_value,
|
||||
|sats| !sats.is_max(),
|
||||
exit,
|
||||
)?;
|
||||
Ok(())
|
||||
@@ -1472,6 +1444,8 @@ impl Vecs {
|
||||
},
|
||||
exit,
|
||||
)?;
|
||||
drop(height_to_coinbase_iter);
|
||||
|
||||
if let Some(mut height_to_coinbase_iter) = self
|
||||
.indexes_to_coinbase
|
||||
.dollars
|
||||
@@ -1493,18 +1467,20 @@ impl Vecs {
|
||||
)?;
|
||||
}
|
||||
|
||||
drop(height_to_coinbase_iter);
|
||||
|
||||
self.indexes_to_subsidy
|
||||
.compute_all(indexes, price, starting_indexes, exit, |vec| {
|
||||
let mut indexes_to_fee_sum_iter =
|
||||
self.indexes_to_fee.sats.height.unwrap_sum().iter();
|
||||
vec.compute_transform(
|
||||
vec.compute_transform2(
|
||||
starting_indexes.height,
|
||||
self.indexes_to_coinbase.sats.height.as_ref().unwrap(),
|
||||
|(height, coinbase, ..)| {
|
||||
let fees = indexes_to_fee_sum_iter.get_unwrap(height);
|
||||
(height, coinbase.checked_sub(fees).unwrap())
|
||||
self.indexes_to_fee.sats.height.unwrap_sum(),
|
||||
|(height, coinbase, fees, ..)| {
|
||||
(
|
||||
height,
|
||||
coinbase.checked_sub(fees).unwrap_or_else(|| {
|
||||
dbg!(height, coinbase, fees);
|
||||
panic!()
|
||||
}),
|
||||
)
|
||||
},
|
||||
exit,
|
||||
)?;
|
||||
|
||||
@@ -235,21 +235,17 @@ where
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn compute<I2>(
|
||||
pub fn compute<A>(
|
||||
&mut self,
|
||||
max_from: I,
|
||||
source: &impl IterableVec<I2, T>,
|
||||
first_indexes: &impl IterableVec<I, I2>,
|
||||
source: &impl IterableVec<A, T>,
|
||||
first_indexes: &impl IterableVec<I, A>,
|
||||
count_indexes: &impl IterableVec<I, StoredU64>,
|
||||
exit: &Exit,
|
||||
) -> Result<()>
|
||||
where
|
||||
I2: VecIndex + VecValue + CheckedSub<I2>,
|
||||
A: VecIndex + VecValue + CheckedSub<A>,
|
||||
{
|
||||
dbg!(source.len());
|
||||
dbg!(first_indexes.len());
|
||||
dbg!(count_indexes.len());
|
||||
|
||||
self.validate_computed_version_or_reset(
|
||||
source.version() + first_indexes.version() + count_indexes.version(),
|
||||
)?;
|
||||
@@ -399,16 +395,16 @@ where
|
||||
}
|
||||
|
||||
#[allow(clippy::wrong_self_convention)]
|
||||
pub fn from_aligned<I2>(
|
||||
pub fn from_aligned<A>(
|
||||
&mut self,
|
||||
max_from: I,
|
||||
source: &EagerVecsBuilder<I2, T>,
|
||||
first_indexes: &impl IterableVec<I, I2>,
|
||||
source: &EagerVecsBuilder<A, T>,
|
||||
first_indexes: &impl IterableVec<I, A>,
|
||||
count_indexes: &impl IterableVec<I, StoredU64>,
|
||||
exit: &Exit,
|
||||
) -> Result<()>
|
||||
where
|
||||
I2: VecIndex + VecValue + CheckedSub<I2>,
|
||||
A: VecIndex + VecValue + CheckedSub<A>,
|
||||
{
|
||||
if self.pct90.is_some()
|
||||
|| self.pct75.is_some()
|
||||
|
||||
@@ -438,9 +438,9 @@ impl Vecs {
|
||||
starting_indexes: brk_indexer::Indexes,
|
||||
exit: &Exit,
|
||||
) -> Result<Indexes> {
|
||||
let idxs = self.compute_(indexer, starting_indexes, exit)?;
|
||||
let indexes = self.compute_(indexer, starting_indexes, exit)?;
|
||||
self.db.compact()?;
|
||||
Ok(idxs)
|
||||
Ok(indexes)
|
||||
}
|
||||
|
||||
fn compute_(
|
||||
@@ -460,13 +460,12 @@ impl Vecs {
|
||||
&indexer.vecs.txinindex_to_outpoint,
|
||||
|(txinindex, outpoint, ..)| {
|
||||
if unlikely(outpoint.is_coinbase()) {
|
||||
(txinindex, TxOutIndex::COINBASE)
|
||||
} else {
|
||||
let txoutindex = txindex_to_first_txoutindex
|
||||
.read_unwrap(outpoint.txindex(), &txindex_to_first_txoutindex_reader)
|
||||
+ outpoint.vout();
|
||||
(txinindex, txoutindex)
|
||||
return (txinindex, TxOutIndex::COINBASE);
|
||||
}
|
||||
let txoutindex = txindex_to_first_txoutindex
|
||||
.read_unwrap(outpoint.txindex(), &txindex_to_first_txoutindex_reader)
|
||||
+ outpoint.vout();
|
||||
(txinindex, txoutindex)
|
||||
},
|
||||
exit,
|
||||
)?;
|
||||
|
||||
@@ -157,8 +157,6 @@ impl Computer {
|
||||
info!("Computing indexes...");
|
||||
let mut starting_indexes = self.indexes.compute(indexer, starting_indexes, exit)?;
|
||||
|
||||
return Ok(());
|
||||
|
||||
if let Some(fetched) = self.fetched.as_mut() {
|
||||
info!("Computing fetched...");
|
||||
fetched.compute(indexer, &self.indexes, &starting_indexes, exit)?;
|
||||
@@ -201,6 +199,8 @@ impl Computer {
|
||||
self.market.compute(price, &starting_indexes, exit)?;
|
||||
}
|
||||
|
||||
return Ok(());
|
||||
|
||||
// constants.join().unwrap()?;
|
||||
// chain.join().unwrap()?;
|
||||
// Ok(())
|
||||
|
||||
Reference in New Issue
Block a user