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

how exactly is data stored in the rocksdb please #1159

Open
mahalamobile opened this issue Feb 17, 2025 · 3 comments
Open

how exactly is data stored in the rocksdb please #1159

mahalamobile opened this issue Feb 17, 2025 · 3 comments

Comments

@mahalamobile
Copy link

Hi,

so am trying to query the rocksdb directly from rust_rocksdb library and basic code, say for example i have a BTC address and i want to see if this address has a balance and or has more than 1 transaction. I cant find any data say associated with BTC address that i know has BTC balance

i check all column_families, so i think im looking up incorrectly on the byte array? or doing some conversions wrong or there is different formats for different data? any help apprecaited :)

here is my basic code
extern crate rocksdb;
use rocksdb::{DB, Options, ColumnFamily};
use bitcoin::{Address};
use std::str::FromStr;

fn btc_address_to_script_hash(btc_address: &str) -> Option<Vec> {
// Parse the Bitcoin address
let address = Address::from_str(btc_address).ok()?;

// Check if the address is of type P2PKH and get the script hash
if address.script_pubkey().is_p2pkh() {
    let script_bytes = address.script_pubkey().as_bytes().to_vec(); // Fix temporary value issue
    // Extract the public key hash (this is the last 20 bytes of the script)
    Some(script_bytes[3..23].to_vec()) // Extract the P2PKH hash
} else {
    None
}

}

fn main() {
let path = "/electrs/db/bitcoin"; // Path to your RocksDB instance
let btc_address = "198aMn6ZYAczwrE5NvNTUMyJ5qkfy4g3Hi"; // Replace with the actual BTC address

// Convert BTC address to script hash
let script_hash = match btc_address_to_script_hash(btc_address) {
    Some(hash) => hash,
    None => {
        eprintln!("Invalid address or address type not supported");
        return;
    }
};

// Debug: Print the script hash to ensure it's correct
println!("Script hash for BTC address {}: {:?}", btc_address, script_hash);

// Open the RocksDB database with default options
let opts = Options::default();

// List of column families to open
let column_families = vec!["spending", "funding", "txid", "headers", "config"];

// Open the database with the column families
let db = match DB::open_cf(&opts, path, column_families.clone()) {
    Ok(db) => db,
    Err(e) => {
        eprintln!("Error opening RocksDB: {:?}", e);
        return;
    }
};

// Collect column family handles separately before using `db`
let mut cf_handles = Vec::new();
for cf_name in &column_families { // Borrow column_families as a reference
    match db.cf_handle(cf_name) {
        Some(cf) => cf_handles.push(cf),
        None => {
            eprintln!("Error: Column family {} not found", cf_name);
            return;
        }
    }
}

// Try querying data from multiple column families
for (i, cf) in cf_handles.iter().enumerate() {
    let cf_name = column_families[i]; // Use the names from the `column_families` vector

    match db.get_cf(cf, &script_hash) {
        Ok(Some(value)) => {
            println!("Found data for script hash {} in column family {}", btc_address, cf_name);
            println!("Data: {:?}", value);
            break; // Stop after finding the first result
        }
        Ok(None) => {
            println!("No data in column family {} for script hash {}", cf_name, btc_address);
        }
        Err(e) => {
            eprintln!("Error fetching data from column family {}: {:?}", cf_name, e);
        }
    }
}

}

@antonilol
Copy link
Contributor

If you really want to query rocksdb directly see doc/schema.md for how the data is stored there. Checking if an address has some balance can easily be done with electrs running using the Electrum protocol.

@mahalamobile
Copy link
Author

mahalamobile commented Feb 19, 2025 via email

@mahalamobile
Copy link
Author

mahalamobile commented Feb 19, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants