mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-25 07:09:59 -07:00
global: snapshot
This commit is contained in:
@@ -4,8 +4,8 @@ use bitcoin::{Network, PublicKey, ScriptBuf};
|
||||
use brk_error::{Error, Result};
|
||||
use brk_types::{
|
||||
Address, AddressBytes, AddressChainStats, AddressHash, AddressIndexOutPoint,
|
||||
AddressIndexTxIndex, AddressStats, AnyAddressDataIndexEnum, OutputType, Sats, TxIndex,
|
||||
TxStatus, Txid, TypeIndex, Unit, Utxo, Vout,
|
||||
AddressIndexTxIndex, AddressStats, AnyAddressDataIndexEnum, OutputType, Sats, Transaction,
|
||||
TxIndex, TxStatus, Txid, TypeIndex, Unit, Utxo, Vout,
|
||||
};
|
||||
use vecdb::{ReadableVec, VecIndex};
|
||||
|
||||
@@ -32,16 +32,12 @@ impl Query {
|
||||
return Err(Error::InvalidAddress);
|
||||
};
|
||||
|
||||
dbg!(&script);
|
||||
|
||||
let outputtype = OutputType::from(&script);
|
||||
dbg!(outputtype);
|
||||
let Ok(bytes) = AddressBytes::try_from((&script, outputtype)) else {
|
||||
return Err(Error::InvalidAddress);
|
||||
};
|
||||
let addresstype = outputtype;
|
||||
let hash = AddressHash::from(&bytes);
|
||||
dbg!(hash);
|
||||
|
||||
let Ok(Some(type_index)) = stores
|
||||
.addresstype_to_addresshash_to_addressindex
|
||||
@@ -94,16 +90,44 @@ impl Query {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn address_txs(
|
||||
&self,
|
||||
address: Address,
|
||||
after_txid: Option<Txid>,
|
||||
limit: usize,
|
||||
) -> Result<Vec<Transaction>> {
|
||||
let txindices = self.address_txindices(&address, after_txid, limit)?;
|
||||
txindices
|
||||
.into_iter()
|
||||
.map(|txindex| self.transaction_by_index(txindex))
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn address_txids(
|
||||
&self,
|
||||
address: Address,
|
||||
after_txid: Option<Txid>,
|
||||
limit: usize,
|
||||
) -> Result<Vec<Txid>> {
|
||||
let txindices = self.address_txindices(&address, after_txid, limit)?;
|
||||
let txid_reader = self.indexer().vecs.transactions.txid.reader();
|
||||
let txids = txindices
|
||||
.into_iter()
|
||||
.map(|txindex| txid_reader.get(txindex.to_usize()))
|
||||
.collect();
|
||||
Ok(txids)
|
||||
}
|
||||
|
||||
fn address_txindices(
|
||||
&self,
|
||||
address: &Address,
|
||||
after_txid: Option<Txid>,
|
||||
limit: usize,
|
||||
) -> Result<Vec<TxIndex>> {
|
||||
let indexer = self.indexer();
|
||||
let stores = &indexer.stores;
|
||||
|
||||
let (outputtype, type_index) = self.resolve_address(&address)?;
|
||||
let (outputtype, type_index) = self.resolve_address(address)?;
|
||||
|
||||
let store = stores
|
||||
.addresstype_to_addressindex_and_txindex
|
||||
@@ -124,7 +148,7 @@ impl Query {
|
||||
None
|
||||
};
|
||||
|
||||
let txindices: Vec<TxIndex> = store
|
||||
Ok(store
|
||||
.prefix(prefix)
|
||||
.rev()
|
||||
.filter(|(key, _): &(AddressIndexTxIndex, Unit)| {
|
||||
@@ -136,15 +160,7 @@ impl Query {
|
||||
})
|
||||
.take(limit)
|
||||
.map(|(key, _)| key.txindex())
|
||||
.collect();
|
||||
|
||||
let txid_reader = indexer.vecs.transactions.txid.reader();
|
||||
let txids: Vec<Txid> = txindices
|
||||
.into_iter()
|
||||
.map(|txindex| txid_reader.get(txindex.to_usize()))
|
||||
.collect();
|
||||
|
||||
Ok(txids)
|
||||
.collect())
|
||||
}
|
||||
|
||||
pub fn address_utxos(&self, address: Address) -> Result<Vec<Utxo>> {
|
||||
|
||||
@@ -35,7 +35,7 @@ impl Query {
|
||||
if let Some(indexes) = self.vecs().metric_to_indexes(metric.clone()) {
|
||||
let supported = indexes
|
||||
.iter()
|
||||
.map(|i| format!("/api/metric/{metric}/{i}"))
|
||||
.map(|i| format!("/api/metric/{metric}/{}", i.name()))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
return Error::MetricUnsupportedIndex {
|
||||
@@ -253,7 +253,12 @@ impl Query {
|
||||
}
|
||||
|
||||
/// Format a resolved query as raw data (just the JSON array, no MetricData wrapper).
|
||||
/// CSV output is identical to `format` (no wrapper distinction for CSV).
|
||||
pub fn format_raw(&self, resolved: ResolvedQuery) -> Result<MetricOutput> {
|
||||
if resolved.format() == Format::CSV {
|
||||
return self.format(resolved);
|
||||
}
|
||||
|
||||
let ResolvedQuery {
|
||||
vecs, version, total, start, end, ..
|
||||
} = resolved;
|
||||
|
||||
@@ -3,8 +3,8 @@ use std::io::Cursor;
|
||||
use bitcoin::{consensus::Decodable, hex::DisplayHex};
|
||||
use brk_error::{Error, Result};
|
||||
use brk_types::{
|
||||
Sats, Transaction, TxIn, TxInIndex, TxIndex, TxOut, TxOutspend, TxStatus, Txid, TxidParam,
|
||||
TxidPrefix, Vin, Vout, Weight,
|
||||
OutputType, Sats, Transaction, TxIn, TxInIndex, TxIndex, TxOut, TxOutspend, TxStatus, Txid,
|
||||
TxidParam, TxidPrefix, Vin, Vout, Weight,
|
||||
};
|
||||
use vecdb::{ReadableVec, VecIndex};
|
||||
|
||||
@@ -242,6 +242,8 @@ impl Query {
|
||||
let txid_reader = indexer.vecs.transactions.txid.reader();
|
||||
let first_txoutindex_reader = indexer.vecs.transactions.first_txoutindex.reader();
|
||||
let value_reader = indexer.vecs.outputs.value.reader();
|
||||
let outputtype_reader = indexer.vecs.outputs.outputtype.reader();
|
||||
let typeindex_reader = indexer.vecs.outputs.typeindex.reader();
|
||||
|
||||
// Batch-read outpoints for all inputs (avoids per-input PcoVec page decompression)
|
||||
let outpoints: Vec<_> = indexer.vecs.inputs.outpoint.collect_range_at(
|
||||
@@ -272,13 +274,16 @@ impl Query {
|
||||
first_txoutindex_reader.get(prev_txindex.to_usize());
|
||||
let prev_txoutindex = prev_first_txoutindex + prev_vout;
|
||||
|
||||
// Get the value of the prevout
|
||||
let prev_value = value_reader.get(usize::from(prev_txoutindex));
|
||||
let prev_outputtype: OutputType =
|
||||
outputtype_reader.get(usize::from(prev_txoutindex));
|
||||
let prev_typeindex = typeindex_reader.get(usize::from(prev_txoutindex));
|
||||
let script_pubkey = indexer
|
||||
.vecs
|
||||
.addresses
|
||||
.script_pubkey(prev_outputtype, prev_typeindex);
|
||||
|
||||
let prevout = Some(TxOut::from((
|
||||
bitcoin::ScriptBuf::new(), // Placeholder - would need to reconstruct
|
||||
prev_value,
|
||||
)));
|
||||
let prevout = Some(TxOut::from((script_pubkey, prev_value)));
|
||||
|
||||
(prev_txid, prev_vout, prevout)
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user