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

run_block_file_eval: now writes evaluation results to output file #10

Merged
merged 1 commit into from
Oct 13, 2024
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
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ The best way to install Rust is to use [rustup](https://rustup.rs).

##### bitcoind

If on bitcoind v28.0, ensure the following flag is set prior to initial block download: `-blocksxor=0`

#### Environment Variables

### Clone code
Expand Down Expand Up @@ -50,11 +52,11 @@ $ cargo test

* execute indexer on a specific bitcoin block data file :

$ BITCOIND_DATA_DIR=/path/to/bitcoind/data/dir
$ BITCOIND_BLOCK_DATA_FILE=xxx.dat
$ export BITCOIND_DATA_DIR=/path/to/bitcoind/data/dir
$ export BITCOIND_BLOCK_DATA_FILE=xxx.dat

$ ./target/debug/gabriel block-file-eval \
-b $BITCOIND_DATA_DIR/$BITCOIND_BLOCK_DATA_FILE \
-b $BITCOIND_DATA_DIR/blocks/$BITCOIND_BLOCK_DATA_FILE \
-o /tmp/$BITCOIND_BLOCK_DATA_FILE.csv


Expand Down
10 changes: 1 addition & 9 deletions src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ use nom::{
use rayon::prelude::*;
use sha2::{Digest, Sha256};

const MAGIC_NUMBER: u32 = 0xD9B4BEF9; // Bitcoin Mainnet magic number

use crate::tx::{Transaction, TransactionInput, TransactionOutput, WitnessItem};

#[derive(Debug)]
Expand Down Expand Up @@ -404,13 +402,7 @@ fn parse_block(input: &[u8]) -> IResult<&[u8], BitcoinBlock> {

/// Parses a single block, including the magic number and block size
fn parse_block_with_magic(input: &[u8]) -> IResult<&[u8], BitcoinBlock> {
let (input, magic) = le_u32(input)?;
if magic != MAGIC_NUMBER {
return Err(nom::Err::Error(nom::error::Error::new(
input,
nom::error::ErrorKind::Tag,
)));
}
let (input, _magic) = le_u32(input)?;

let (input, block_size) = parse_block_size(input)?;
let block_size = block_size as usize;
Expand Down
29 changes: 22 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,23 @@ fn run_block_file_eval(args: &BlockFileEvalArgs) -> Result<()> {
let result_map: ResultMap = Default::default();
let pb = ProgressBar::new(1);

let size = process_block_file(
let blocks_processed = process_block_file(
&args.block_file_absolute_path,
&pb,
&result_map,
&tx_map,
&header_map,
);
println!("process_block_file size = {}", size);
println!(
"block_file_absolute_path: {} ; blocks processed = {}",
&args.block_file_absolute_path.display(),
blocks_processed
);
if blocks_processed < 1 {
return Ok(());
}

// prep output file
let mut file = OpenOptions::new()
.read(true)
.write(true)
Expand All @@ -97,12 +105,19 @@ fn run_block_file_eval(args: &BlockFileEvalArgs) -> Result<()> {
// When writing back to the file, ensure we start from the beginning
file.seek(std::io::SeekFrom::Start(0))?;
file.set_len(0)?; // Truncate the file

file.write_all(HEADER.as_bytes())?;
let mut out: Vec<String> = vec![];
// JA Bride: TO_DO
for line in &out {
writeln!(file, "{}", line)?;

let result_map_read = result_map.read().unwrap();
for (_key, record) in result_map_read.iter() {
// write a record to the file
let mut p2pk_addresses = &record.p2pk_addresses_added;
let binding = p2pk_addresses - &record.p2pk_addresses_spent;
p2pk_addresses = &binding;
let mut p2pk_coins = record.p2pk_sats_added.to_owned() as f64 / 100_000_000.0;
p2pk_coins -= record.p2pk_sats_spent.to_owned() as f64 / 100_000_000.0;
let date = &record.date;
let output_line = format!("0,{date},{p2pk_addresses},{p2pk_coins}");
writeln!(file, "{}", output_line)?;
}

Ok(())
Expand Down