global: tiny snapshot

This commit is contained in:
nym21
2025-09-01 20:21:51 +02:00
parent 08cfefc02a
commit fe5343c1d6
8 changed files with 96 additions and 76 deletions
+51 -7
View File
@@ -1,5 +1,6 @@
use std::path::Path;
use bitcoin::{Block, consensus::serde::hex, script::Instruction};
use bitcoincore_rpc::{Auth, Client, Result};
use brk_parser::Parser;
use brk_structs::Height;
@@ -8,26 +9,33 @@ use brk_structs::Height;
fn main() -> Result<()> {
let i = std::time::Instant::now();
let bitcoin_dir = Path::new("").join("");
let brk_dir = Path::new("").join("");
let bitcoin_dir = Path::new(&std::env::var("HOME").unwrap())
.join("Library")
.join("Application Support")
.join("Bitcoin");
let brk_dir = Path::new(&std::env::var("HOME").unwrap()).join(".brk");
let rpc = Box::leak(Box::new(Client::new(
"http://localhost:8332",
Auth::CookieFile(bitcoin_dir.join(".cookie")),
)?));
let parser = Parser::new(bitcoin_dir.join("blocks"), Some(brk_dir), rpc);
let start = None;
let end = None;
let parser = Parser::new(bitcoin_dir.join("blocks"), brk_dir, rpc);
parser
.parse(start, end)
.iter()
.for_each(|(height, _block, hash)| {
println!("{height}: {hash}");
println!("{height}: {}", _block.get_coinbase_message());
});
println!(
"391487: {}",
parser.get(Height::new(391487)).get_coinbase_message()
);
let block_0 = parser.get(Height::new(0));
println!(
@@ -42,7 +50,23 @@ fn main() -> Result<()> {
.script_pubkey
);
let block_840_000 = parser.get(Height::new(840_000));
println!("{}", block_0.get_coinbase_message());
let block_158251 = parser.get(Height::new(158251));
println!(
"{}",
block_158251
.txdata
.first()
.unwrap()
.output
.first()
.unwrap()
.script_pubkey
);
println!("{}", block_158251.get_coinbase_message());
let block_840_000 = parser.get(Height::new(840_004));
println!(
"{}",
@@ -56,7 +80,27 @@ fn main() -> Result<()> {
.value
);
println!("{}", block_840_000.get_coinbase_message());
dbg!(i.elapsed());
Ok(())
}
pub trait BlockExtended {
fn get_coinbase_message(&self) -> String;
}
impl BlockExtended for Block {
fn get_coinbase_message(&self) -> String {
let Some(input) = self.txdata.first().and_then(|tx| tx.input.first()) else {
return String::new();
};
let bytes = input.script_sig.as_bytes();
String::from_utf8_lossy(bytes)
.chars()
.filter(|&c| c != '\u{FFFD}' && (c >= ' ' || c == '\n' || c == '\r' || c == '\t'))
.take(1_024)
.collect()
}
}
+1 -1
View File
@@ -21,7 +21,7 @@ fn main() {
// let start = None;
// let end = None;
let parser = Parser::new(bitcoin_dir.join("blocks"), brk_dir, rpc);
let parser = Parser::new(bitcoin_dir.join("blocks"), Some(brk_dir), rpc);
// parser
// .parse(start, end)
+7 -4
View File
@@ -35,14 +35,14 @@ const BOUND_CAP: usize = 50;
pub struct Parser {
blocks_dir: PathBuf,
outputs_dir: PathBuf,
outputs_dir: Option<PathBuf>,
rpc: &'static bitcoincore_rpc::Client,
}
impl Parser {
pub fn new(
blocks_dir: PathBuf,
outputs_dir: PathBuf,
outputs_dir: Option<PathBuf>,
rpc: &'static bitcoincore_rpc::Client,
) -> Self {
Self {
@@ -79,8 +79,11 @@ impl Parser {
let blk_index_to_blk_path = BlkIndexToBlkPath::scan(blocks_dir);
let (mut blk_index_to_blk_recap, blk_index) =
BlkIndexToBlkRecap::import(&self.outputs_dir, &blk_index_to_blk_path, start);
let (mut blk_index_to_blk_recap, blk_index) = BlkIndexToBlkRecap::import(
self.outputs_dir.as_ref().unwrap(),
&blk_index_to_blk_path,
start,
);
let xor_bytes = XORBytes::from(blocks_dir);