bomputer: init

This commit is contained in:
nym21
2025-01-31 11:43:14 +01:00
parent 8c610f8a83
commit ad34d9d402
67 changed files with 498 additions and 249 deletions

View File

@@ -1,36 +1,71 @@
use std::path::Path;
use std::path::{Path, PathBuf};
use bindex::Indexer;
use bindex::{Height, Indexer};
use biter::rpc;
use exit::Exit;
mod storage;
mod structs;
use storage::{Fjalls, StorableVecs};
use structs::*;
pub struct Bomputer;
impl Bomputer {
pub fn compute() {}
pub struct Computer {
outputs_dir: PathBuf,
vecs: StorableVecs,
trees: Fjalls,
}
pub fn main() -> color_eyre::Result<()> {
color_eyre::install()?;
impl Computer {
pub fn import(outputs_dir: &Path) -> color_eyre::Result<Self> {
let outputs_dir = outputs_dir.to_owned();
let computed_dir = outputs_dir.join("computed");
let vecs = StorableVecs::import(&computed_dir.join("vecs"))?;
let trees = Fjalls::import(&computed_dir.join("fjall"))?;
Ok(Self {
outputs_dir,
vecs,
trees,
})
}
let data_dir = Path::new("../../bitcoin");
let rpc = rpc::Client::new(
"http://localhost:8332",
rpc::Auth::CookieFile(Path::new(data_dir).join(".cookie")),
)?;
let exit = Exit::new();
pub fn compute(&mut self, bitcoin_dir: &Path, rpc: rpc::Client, exit: &Exit) -> color_eyre::Result<()> {
let mut indexer = Indexer::import(&self.outputs_dir.join("indexes"))?;
let i = std::time::Instant::now();
if false {
indexer.index(bitcoin_dir, rpc, exit)?;
}
let mut indexer = Indexer::import(Path::new("indexes"))?;
// TODO: Remove all outdated
indexer.index(data_dir, rpc, &exit)?;
// Compute txindex to X
dbg!(i.elapsed());
// Compute height to X
indexer
.vecs()
.height_to_timestamp
.read_from_(self.vecs.height_to_date.len(), |(_height, timestamp)| {
self.vecs
.height_to_date
.push_if_needed(Height::from(_height), Date::from(timestamp))
})?;
self.vecs
.height_to_date
.read_from_(self.vecs.date_to_first_height.len(), |(_height, date)| {
self.vecs
.date_to_first_height
.push_if_needed(*date, Height::from(_height))
})?;
Ok(())
// Compute date to X
// ...
// Compute month to X
// ...
// Compute year to X
// ...
Ok(())
}
}

View File

@@ -1,13 +1,28 @@
use structs::Date;
use std::path::Path;
use biter::rpc;
use bomputer::Computer;
use exit::Exit;
mod structs;
pub fn main() -> color_eyre::Result<()> {
let date1 = Date::from(jiff::civil::Date::constant(2009, 1, 9));
let date2 = Date::from(jiff::civil::Date::constant(2009, 1, 31));
let date3 = Date::from(jiff::civil::Date::constant(2019, 1, 9));
dbg!(usize::try_from(date1))?;
dbg!(usize::try_from(date2))?;
dbg!(usize::try_from(date3))?;
color_eyre::install()?;
let data_dir = Path::new("../../bitcoin");
let rpc = rpc::Client::new(
"http://localhost:8332",
rpc::Auth::CookieFile(Path::new(data_dir).join(".cookie")),
)?;
let exit = Exit::new();
let i = std::time::Instant::now();
let mut computer = Computer::import(Path::new("../_outputs"))?;
computer.compute(data_dir, rpc, &exit)?;
dbg!(i.elapsed());
Ok(())
}

View File

@@ -0,0 +1,22 @@
use std::path::Path;
use bindex::{Store, Version};
use crate::structs::{AddressindexTxoutindex, Unit};
pub struct Fjalls {
pub address_txoutindex_in: Store<AddressindexTxoutindex, Unit>,
pub address_txoutindex_out: Store<AddressindexTxoutindex, Unit>,
}
impl Fjalls {
pub fn import(path: &Path) -> color_eyre::Result<Self> {
let address_txoutindex_in = Store::import(&path.join("address_txoutindex_in"), Version::from(1))?;
let address_txoutindex_out = Store::import(&path.join("address_txoutindex_out"), Version::from(1))?;
Ok(Self {
address_txoutindex_in,
address_txoutindex_out,
})
}
}

View File

@@ -0,0 +1,5 @@
mod fjalls;
mod storable_vecs;
pub use fjalls::*;
pub use storable_vecs::*;

View File

@@ -0,0 +1,74 @@
use std::{
fmt::Debug,
io,
ops::{Deref, DerefMut},
path::Path,
};
use bindex::{Indexer, Version};
use crate::Computer;
pub struct StorableVec<I, T> {
vec: bindex::StorableVec<I, T>,
f: Box<dyn Fn(&Indexer, &Computer) -> storable_vec::Result<Vec<(I, T)>>>,
}
impl<I, T> StorableVec<I, T>
where
I: TryInto<usize>,
T: Sized + Debug + Clone,
{
pub fn import<F>(path: &Path, version: Version, f: F) -> io::Result<Self>
where
F: Fn(&Indexer, &Computer) -> storable_vec::Result<Vec<(I, T)>> + 'static,
{
let vec = bindex::StorableVec::import(path, version)?;
Ok(Self { vec, f: Box::new(f) })
}
pub fn compute(&mut self, indexer: &Indexer, computer: &Computer) -> storable_vec::Result<()> {
(self.f)(indexer, computer)?
.into_iter()
.try_for_each(|(i, v)| self.push_if_needed(i, v))
}
// pub fn fill(&mut self) {
// self
// .vecs()
// .height_to_timestamp
// .read_iter(move |(_height, timestamp)| {
// let height = Height::from(_height);
// let date = Date::from(timestamp);
// self.vecs.date_to_first_height.push_if_needed(date, height)?;
// Ok(())
// })?;
// }
}
impl<I, T> Deref for StorableVec<I, T> {
type Target = bindex::StorableVec<I, T>;
fn deref(&self) -> &Self::Target {
&self.vec
}
}
impl<I, T> DerefMut for StorableVec<I, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.vec
}
}
pub trait AnyComputedStorableVec {
fn compute(&mut self, indexer: &Indexer, computer: &Computer) -> storable_vec::Result<()>;
}
impl<I, T> AnyComputedStorableVec for StorableVec<I, T>
where
I: TryInto<usize>,
T: Sized + Debug + Clone,
{
fn compute(&mut self, indexer: &Indexer, computer: &Computer) -> storable_vec::Result<()> {
self.compute(indexer, computer)
}
}

View File

@@ -0,0 +1,77 @@
use std::{fs, path::Path};
use bindex::{Addressindex, Amount, Height, StorableVec, Timestamp, Txindex, Txinindex, Txoutindex, Version};
use crate::structs::{Date, Feerate};
// mod base;
// use base::*;
pub struct StorableVecs {
pub date_to_first_height: StorableVec<Date, Height>,
// pub height_to_block_interval: StorableVec<Height, Timestamp>,
pub height_to_date: StorableVec<Height, Date>,
// pub height_to_fee: StorableVec<Txindex, Amount>,
// pub height_to_inputcount: StorableVec<Txindex, u32>,
// pub height_to_last_addressindex: StorableVec<Height, Addressindex>,
// pub height_to_last_txindex: StorableVec<Height, Txindex>,
// pub height_to_last_txoutindex: StorableVec<Height, Txoutindex>,
// pub height_to_maxfeerate: StorableVec<Txindex, Feerate>,
// pub height_to_medianfeerate: StorableVec<Txindex, Feerate>,
// pub height_to_minfeerate: StorableVec<Txindex, Feerate>,
// pub height_to_outputcount: StorableVec<Txindex, u32>,
// pub height_to_subsidy: StorableVec<Txindex, u32>,
// pub height_to_totalfees: StorableVec<Height, Amount>,
// pub height_to_txcount: StorableVec<Txindex, u32>,
pub txindex_to_fee: StorableVec<Txindex, Amount>,
// pub txindex_to_feerate: StorableVec<Txindex, Feerate>,
pub txindex_to_inputcount: StorableVec<Txindex, u32>,
pub txindex_to_last_txinindex: StorableVec<Txindex, Txinindex>,
pub txindex_to_last_txoutindex: StorableVec<Txindex, Txoutindex>,
pub txindex_to_outputcount: StorableVec<Txindex, u32>,
}
impl StorableVecs {
pub fn import(path: &Path) -> color_eyre::Result<Self> {
fs::create_dir_all(path)?;
Ok(Self {
date_to_first_height: StorableVec::import(&path.join("date_to_first_height"), Version::from(1))?,
// height_to_block_interval: StorableVec::import(&path.join("height_to_block_interval"), Version::from(1))?,
height_to_date: StorableVec::import(&path.join("height_to_date"), Version::from(1))?,
// height_to_fee: StorableVec::import(&path.join("height_to_fee"), Version::from(1))?,
// height_to_inputcount: StorableVec::import(&path.join("height_to_inputcount"), Version::from(1))?,
// height_to_last_addressindex: StorableVec::import(
// &path.join("height_to_last_addressindex"),
// Version::from(1),
// )?,
// height_to_last_txindex: StorableVec::import(&path.join("height_to_last_txindex"), Version::from(1))?,
// height_to_last_txoutindex: StorableVec::import(&path.join("height_to_last_txoutindex"), Version::from(1))?,
// height_to_maxfeerate: StorableVec::import(&path.join("height_to_maxfeerate"), Version::from(1))?,
// height_to_medianfeerate: StorableVec::import(&path.join("height_to_medianfeerate"), Version::from(1))?,
// height_to_minfeerate: StorableVec::import(&path.join("height_to_minfeerate"), Version::from(1))?,
// height_to_outputcount: StorableVec::import(&path.join("height_to_outputcount"), Version::from(1))?,
// height_to_subsidy: StorableVec::import(&path.join("height_to_subsidy"), Version::from(1))?,
// height_to_totalfees: StorableVec::import(&path.join("height_to_totalfees"), Version::from(1))?,
// height_to_txcount: StorableVec::import(&path.join("height_to_txcount"), Version::from(1))?,
txindex_to_fee: StorableVec::import(&path.join("txindex_to_fee"), Version::from(1))?,
// txindex_to_feerate: StorableVec::import(&path.join("txindex_to_feerate"), Version::from(1))?,
txindex_to_inputcount: StorableVec::import(&path.join("txindex_to_inputcount"), Version::from(1))?,
txindex_to_last_txinindex: StorableVec::import(&path.join("txindex_to_last_txinindex"), Version::from(1))?,
txindex_to_last_txoutindex: StorableVec::import(
&path.join("txindex_to_last_txoutindex"),
Version::from(1),
)?,
txindex_to_outputcount: StorableVec::import(&path.join("txindex_to_outputcount"), Version::from(1))?,
})
}
// pub fn as_slice(&self) -> [&dyn AnyComputedStorableVec; 1] {
// [&self.date_to_first_height]
// }
// pub fn as_mut_slice(&mut self) -> [&mut dyn AnyComputedStorableVec; 1] {
// [&mut self.date_to_first_height]
// }
}

View File

@@ -0,0 +1,21 @@
use bindex::{Addressindex, Txoutindex};
use fjall::Slice;
use unsafe_slice_serde::UnsafeSliceSerde;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct AddressindexTxoutindex {
addressindex: Addressindex,
txoutindex: Txoutindex,
}
impl TryFrom<Slice> for AddressindexTxoutindex {
type Error = unsafe_slice_serde::Error;
fn try_from(value: Slice) -> Result<Self, Self::Error> {
Ok(*Self::unsafe_try_from_slice(&value)?)
}
}
impl From<AddressindexTxoutindex> for Slice {
fn from(value: AddressindexTxoutindex) -> Self {
Self::new(value.unsafe_as_slice())
}
}

View File

@@ -3,7 +3,7 @@ use color_eyre::eyre::eyre;
use derive_deref::Deref;
use jiff::{civil::Date as _Date, tz::TimeZone};
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Deref)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Deref)]
pub struct Date(_Date);
impl Date {

View File

@@ -1,5 +1,9 @@
mod addressindextxoutindex;
mod date;
mod feerate;
mod unit;
pub use addressindextxoutindex::*;
pub use date::*;
pub use feerate::*;
pub use unit::*;

View File

@@ -0,0 +1,13 @@
use fjall::Slice;
pub struct Unit();
impl From<Slice> for Unit {
fn from(_: Slice) -> Self {
Self()
}
}
impl From<Unit> for Slice {
fn from(_: Unit) -> Self {
Self::new(&[])
}
}