Skip to content

Commit 902a341

Browse files
committed
feat: add utility functions for UTXO serialization and deserialization
1 parent b2295f0 commit 902a341

File tree

3 files changed

+27
-19
lines changed

3 files changed

+27
-19
lines changed

utxo_indexer/indexer/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

utxo_indexer/indexer/src/main.rs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
mod bitcoin_primitives;
22
mod bitcoin_serialization;
3+
mod utils;
34

45
mod cli;
56

67
use anyhow::{Context, Result};
78
use bitcoin::hashes::Hash;
89
use clap::Parser;
910
use rocksdb::{DB, IteratorMode, Options};
10-
use std::{
11-
fs::File,
12-
io::{BufReader, BufWriter},
13-
};
1411

1512
use bitcoin::hashes::sha256;
1613

1714
use crate::{
1815
bitcoin_primitives::{CoinKey, CoinValue},
1916
bitcoin_serialization::deobfuscate,
17+
utils::{P2PKH_UTXO_SIZE, load_utxos, save_utxos},
2018
};
2119

2220
const OBFUSCATION_KEY_DB_KEY: &[u8] = b"\x0e\x00obfuscate_key";
23-
const P2PKH_UTXO_SIZE: usize = 8 + 25; // 8 bytes for amount, 25 bytes for P2PKH scriptPubKey
2421

2522
fn main() -> Result<()> {
2623
let cli = cli::Cli::parse();
@@ -120,17 +117,3 @@ fn run_build_merkle_root(utxo_index_path: &str) -> Result<()> {
120117

121118
Ok(())
122119
}
123-
124-
fn save_utxos(utxos: &Vec<[u8; P2PKH_UTXO_SIZE]>, path: &str) -> Result<()> {
125-
let file = File::create(path)?;
126-
let mut writer = BufWriter::new(file);
127-
bincode::encode_into_std_write(utxos, &mut writer, bincode::config::standard())?;
128-
Ok(())
129-
}
130-
131-
fn load_utxos(path: &str) -> Result<Vec<[u8; P2PKH_UTXO_SIZE]>> {
132-
let file = File::open(path)?;
133-
let mut reader = BufReader::new(file);
134-
let utxos = bincode::decode_from_std_read(&mut reader, bincode::config::standard())?;
135-
Ok(utxos)
136-
}

utxo_indexer/indexer/src/utils.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use std::{
2+
fs::File,
3+
io::{BufReader, BufWriter},
4+
};
5+
6+
use anyhow::Result;
7+
8+
pub const P2PKH_UTXO_SIZE: usize = 8 + 25; // 8 bytes for amount, 25 bytes for P2PKH scriptPubKey
9+
10+
pub fn save_utxos(utxos: &Vec<[u8; P2PKH_UTXO_SIZE]>, path: &str) -> Result<()> {
11+
let file = File::create(path)?;
12+
let mut writer = BufWriter::new(file);
13+
bincode::encode_into_std_write(utxos, &mut writer, bincode::config::standard())?;
14+
15+
Ok(())
16+
}
17+
18+
pub fn load_utxos(path: &str) -> Result<Vec<[u8; P2PKH_UTXO_SIZE]>> {
19+
let file = File::open(path)?;
20+
let mut reader = BufReader::new(file);
21+
let utxos = bincode::decode_from_std_read(&mut reader, bincode::config::standard())?;
22+
23+
Ok(utxos)
24+
}

0 commit comments

Comments
 (0)