mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-07-30 04:08:11 -07:00
indexer: updated
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use brk_error::{Error, Result};
|
||||
use brk_types::{BlockHashPrefix, Timestamp};
|
||||
use tracing::error;
|
||||
use vecdb::GenericStoredVec;
|
||||
use vecdb::WritableVec;
|
||||
|
||||
use super::BlockProcessor;
|
||||
use crate::IndexesExt;
|
||||
@@ -26,12 +26,11 @@ impl BlockProcessor<'_> {
|
||||
|
||||
self.stores
|
||||
.blockhashprefix_to_height
|
||||
.insert_if_needed(blockhash_prefix, height, height);
|
||||
.insert(blockhash_prefix, height);
|
||||
|
||||
self.stores.height_to_coinbase_tag.insert_if_needed(
|
||||
self.stores.height_to_coinbase_tag.insert(
|
||||
height,
|
||||
self.block.coinbase_tag().into(),
|
||||
height,
|
||||
);
|
||||
|
||||
self.vecs
|
||||
@@ -46,14 +45,15 @@ impl BlockProcessor<'_> {
|
||||
.blocks
|
||||
.timestamp
|
||||
.checked_push(height, Timestamp::from(self.block.header.time))?;
|
||||
let (block_total_size, block_weight) = self.block.total_size_and_weight();
|
||||
self.vecs
|
||||
.blocks
|
||||
.total_size
|
||||
.checked_push(height, self.block.total_size().into())?;
|
||||
.checked_push(height, block_total_size.into())?;
|
||||
self.vecs
|
||||
.blocks
|
||||
.weight
|
||||
.checked_push(height, self.block.weight().into())?;
|
||||
.checked_push(height, block_weight.into())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use brk_error::{Error, Result};
|
||||
use brk_types::{StoredBool, TxIndex, Txid, TxidPrefix};
|
||||
use rayon::prelude::*;
|
||||
use vecdb::{AnyVec, GenericStoredVec, TypedVecIterator, likely};
|
||||
use vecdb::{AnyVec, WritableVec, likely};
|
||||
|
||||
use crate::constants::DUPLICATE_TXIDS;
|
||||
|
||||
@@ -9,8 +9,7 @@ use super::{BlockProcessor, ComputedTx};
|
||||
|
||||
impl<'a> BlockProcessor<'a> {
|
||||
pub fn compute_txids(&self) -> Result<Vec<ComputedTx<'a>>> {
|
||||
let will_check_collisions =
|
||||
self.check_collisions && self.stores.txidprefix_to_txindex.needs(self.height);
|
||||
let will_check_collisions = self.check_collisions;
|
||||
let base_txindex = self.indexes.txindex;
|
||||
|
||||
self.block
|
||||
@@ -36,6 +35,8 @@ impl<'a> BlockProcessor<'a> {
|
||||
txid,
|
||||
txid_prefix,
|
||||
prev_txindex_opt,
|
||||
base_size: tx.base_size() as u32,
|
||||
total_size: tx.total_size() as u32,
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
@@ -47,7 +48,6 @@ impl<'a> BlockProcessor<'a> {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mut txindex_to_txid_iter = self.vecs.transactions.txid.into_iter();
|
||||
for ct in txs.iter() {
|
||||
let Some(prev_txindex) = ct.prev_txindex_opt else {
|
||||
continue;
|
||||
@@ -58,8 +58,11 @@ impl<'a> BlockProcessor<'a> {
|
||||
}
|
||||
|
||||
let len = self.vecs.transactions.txid.len();
|
||||
let prev_txid = txindex_to_txid_iter
|
||||
.get(prev_txindex)
|
||||
let prev_txid = self
|
||||
.vecs
|
||||
.transactions
|
||||
.txid
|
||||
.get_pushed_or_read(prev_txindex, &self.readers.txid)
|
||||
.ok_or(Error::Internal("Missing txid for txindex"))
|
||||
.inspect_err(|_| {
|
||||
dbg!(ct.txindex, len);
|
||||
@@ -81,11 +84,9 @@ impl<'a> BlockProcessor<'a> {
|
||||
|
||||
for ct in txs {
|
||||
if ct.prev_txindex_opt.is_none() {
|
||||
self.stores.txidprefix_to_txindex.insert_if_needed(
|
||||
ct.txid_prefix,
|
||||
ct.txindex,
|
||||
height,
|
||||
);
|
||||
self.stores
|
||||
.txidprefix_to_txindex
|
||||
.insert(ct.txid_prefix, ct.txindex);
|
||||
}
|
||||
|
||||
self.vecs
|
||||
@@ -107,11 +108,11 @@ impl<'a> BlockProcessor<'a> {
|
||||
self.vecs
|
||||
.transactions
|
||||
.base_size
|
||||
.checked_push(ct.txindex, ct.tx.base_size().into())?;
|
||||
.checked_push(ct.txindex, ct.base_size.into())?;
|
||||
self.vecs
|
||||
.transactions
|
||||
.total_size
|
||||
.checked_push(ct.txindex, ct.tx.total_size().into())?;
|
||||
.checked_push(ct.txindex, ct.total_size.into())?;
|
||||
self.vecs
|
||||
.transactions
|
||||
.is_explicitly_rbf
|
||||
|
||||
@@ -5,33 +5,30 @@ use brk_types::{
|
||||
};
|
||||
use rayon::prelude::*;
|
||||
use rustc_hash::{FxHashMap, FxHashSet};
|
||||
use vecdb::GenericStoredVec;
|
||||
use vecdb::WritableVec;
|
||||
|
||||
use super::{BlockProcessor, ComputedTx, InputSource, SameBlockOutputInfo};
|
||||
|
||||
impl<'a> BlockProcessor<'a> {
|
||||
pub fn process_inputs<'c>(
|
||||
pub fn process_inputs(
|
||||
&self,
|
||||
txs: &[ComputedTx<'c>],
|
||||
) -> Result<Vec<(TxInIndex, InputSource<'a>)>> {
|
||||
txs: &[ComputedTx],
|
||||
) -> Result<Vec<(TxInIndex, InputSource)>> {
|
||||
let txid_prefix_to_txindex: FxHashMap<_, _> =
|
||||
txs.iter().map(|ct| (ct.txid_prefix, &ct.txindex)).collect();
|
||||
txs.iter().map(|ct| (ct.txid_prefix, ct.txindex)).collect();
|
||||
|
||||
let base_txindex = self.indexes.txindex;
|
||||
let base_txinindex = self.indexes.txinindex;
|
||||
|
||||
let txins = self
|
||||
.block
|
||||
.txdata
|
||||
.iter()
|
||||
.enumerate()
|
||||
.flat_map(|(index, tx)| {
|
||||
tx.input
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(move |(vin, txin)| (TxIndex::from(index), Vin::from(vin), txin, tx))
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
let total_inputs: usize = self.block.txdata.iter().map(|tx| tx.input.len()).sum();
|
||||
let mut items = Vec::with_capacity(total_inputs);
|
||||
for (index, tx) in self.block.txdata.iter().enumerate() {
|
||||
for (vin, txin) in tx.input.iter().enumerate() {
|
||||
items.push((TxIndex::from(index), Vin::from(vin), txin, tx));
|
||||
}
|
||||
}
|
||||
|
||||
let txins = items
|
||||
.into_par_iter()
|
||||
.enumerate()
|
||||
.map(
|
||||
@@ -44,7 +41,6 @@ impl<'a> BlockProcessor<'a> {
|
||||
txinindex,
|
||||
InputSource::SameBlock {
|
||||
txindex,
|
||||
txin,
|
||||
vin,
|
||||
outpoint: OutPoint::COINBASE,
|
||||
},
|
||||
@@ -56,44 +52,41 @@ impl<'a> BlockProcessor<'a> {
|
||||
let txid_prefix = TxidPrefix::from(&txid);
|
||||
let vout = Vout::from(outpoint.vout);
|
||||
|
||||
if let Some(&&same_block_txindex) = txid_prefix_to_txindex
|
||||
if let Some(&same_block_txindex) = txid_prefix_to_txindex
|
||||
.get(&txid_prefix) {
|
||||
let outpoint = OutPoint::new(same_block_txindex, vout);
|
||||
return Ok((
|
||||
txinindex,
|
||||
InputSource::SameBlock {
|
||||
txindex,
|
||||
txin,
|
||||
vin,
|
||||
outpoint,
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
let prev_txindex = if let Some(txindex) = self
|
||||
let store_result = self
|
||||
.stores
|
||||
.txidprefix_to_txindex
|
||||
.get(&txid_prefix)?
|
||||
.map(|v| *v)
|
||||
.and_then(|txindex| {
|
||||
(txindex < self.indexes.txindex).then_some(txindex)
|
||||
})
|
||||
{
|
||||
txindex
|
||||
} else {
|
||||
let store_result = self.stores.txidprefix_to_txindex.get(&txid_prefix)?;
|
||||
tracing::error!(
|
||||
"UnknownTxid: txid={}, prefix={:?}, store_result={:?}, current_txindex={:?}",
|
||||
txid, txid_prefix, store_result, self.indexes.txindex
|
||||
);
|
||||
return Err(Error::UnknownTxid);
|
||||
.map(|v| *v);
|
||||
|
||||
let prev_txindex = match store_result {
|
||||
Some(txindex) if txindex < self.indexes.txindex => txindex,
|
||||
_ => {
|
||||
tracing::error!(
|
||||
"UnknownTxid: txid={}, prefix={:?}, store_result={:?}, current_txindex={:?}",
|
||||
txid, txid_prefix, store_result, self.indexes.txindex
|
||||
);
|
||||
return Err(Error::UnknownTxid);
|
||||
}
|
||||
};
|
||||
|
||||
let txoutindex = self
|
||||
.vecs
|
||||
.transactions
|
||||
.first_txoutindex
|
||||
.get_pushed_or_read(prev_txindex, &self.readers.txindex_to_first_txoutindex)?
|
||||
.get_pushed_or_read(prev_txindex, &self.readers.txindex_to_first_txoutindex)
|
||||
.ok_or(Error::Internal("Missing txoutindex"))?
|
||||
+ vout;
|
||||
|
||||
@@ -103,14 +96,14 @@ impl<'a> BlockProcessor<'a> {
|
||||
.vecs
|
||||
.outputs
|
||||
.outputtype
|
||||
.get_pushed_or_read(txoutindex, &self.readers.txoutindex_to_outputtype)?
|
||||
.get_pushed_or_read(txoutindex, &self.readers.txoutindex_to_outputtype)
|
||||
.ok_or(Error::Internal("Missing outputtype"))?;
|
||||
|
||||
let typeindex = self
|
||||
.vecs
|
||||
.outputs
|
||||
.typeindex
|
||||
.get_pushed_or_read(txoutindex, &self.readers.txoutindex_to_typeindex)?
|
||||
.get_pushed_or_read(txoutindex, &self.readers.txoutindex_to_typeindex)
|
||||
.ok_or(Error::Internal("Missing typeindex"))?;
|
||||
|
||||
Ok((
|
||||
@@ -153,8 +146,6 @@ impl<'a> BlockProcessor<'a> {
|
||||
txins: Vec<(TxInIndex, InputSource)>,
|
||||
mut same_block_output_info: FxHashMap<OutPoint, SameBlockOutputInfo>,
|
||||
) -> Result<()> {
|
||||
let height = self.height;
|
||||
|
||||
for (txinindex, input_source) in txins {
|
||||
let (vin, txindex, outpoint, outputtype, typeindex) = match input_source {
|
||||
InputSource::PreviousBlock {
|
||||
@@ -166,7 +157,6 @@ impl<'a> BlockProcessor<'a> {
|
||||
} => (vin, txindex, outpoint, outputtype, typeindex),
|
||||
InputSource::SameBlock {
|
||||
txindex,
|
||||
txin,
|
||||
vin,
|
||||
outpoint,
|
||||
} => {
|
||||
@@ -177,7 +167,7 @@ impl<'a> BlockProcessor<'a> {
|
||||
.remove(&outpoint)
|
||||
.ok_or(Error::Internal("Same-block output not found"))
|
||||
.inspect_err(|_| {
|
||||
dbg!(&same_block_output_info, txin);
|
||||
dbg!(&same_block_output_info, outpoint);
|
||||
})?;
|
||||
(vin, txindex, outpoint, info.outputtype, info.typeindex)
|
||||
}
|
||||
@@ -217,16 +207,15 @@ impl<'a> BlockProcessor<'a> {
|
||||
self.stores
|
||||
.addresstype_to_addressindex_and_txindex
|
||||
.get_mut_unwrap(addresstype)
|
||||
.insert_if_needed(
|
||||
.insert(
|
||||
AddressIndexTxIndex::from((addressindex, txindex)),
|
||||
Unit,
|
||||
height,
|
||||
);
|
||||
|
||||
self.stores
|
||||
.addresstype_to_addressindex_and_unspentoutpoint
|
||||
.get_mut_unwrap(addresstype)
|
||||
.remove_if_needed(AddressIndexOutPoint::from((addressindex, outpoint)), height);
|
||||
.remove(AddressIndexOutPoint::from((addressindex, outpoint)));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -6,7 +6,7 @@ use brk_types::{
|
||||
};
|
||||
use rayon::prelude::*;
|
||||
use rustc_hash::{FxHashMap, FxHashSet};
|
||||
use vecdb::GenericStoredVec;
|
||||
use vecdb::WritableVec;
|
||||
|
||||
use super::{BlockProcessor, ProcessedOutput, SameBlockOutputInfo};
|
||||
|
||||
@@ -18,17 +18,15 @@ impl<'a> BlockProcessor<'a> {
|
||||
let base_txindex = self.indexes.txindex;
|
||||
let base_txoutindex = self.indexes.txoutindex;
|
||||
|
||||
self.block
|
||||
.txdata
|
||||
.iter()
|
||||
.enumerate()
|
||||
.flat_map(|(index, tx)| {
|
||||
tx.output
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(move |(vout, txout)| (TxIndex::from(index), Vout::from(vout), txout, tx))
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
let total_outputs: usize = self.block.txdata.iter().map(|tx| tx.output.len()).sum();
|
||||
let mut items = Vec::with_capacity(total_outputs);
|
||||
for (index, tx) in self.block.txdata.iter().enumerate() {
|
||||
for (vout, txout) in tx.output.iter().enumerate() {
|
||||
items.push((TxIndex::from(index), Vout::from(vout), txout, tx));
|
||||
}
|
||||
}
|
||||
|
||||
items
|
||||
.into_par_iter()
|
||||
.enumerate()
|
||||
.map(
|
||||
@@ -59,8 +57,7 @@ impl<'a> BlockProcessor<'a> {
|
||||
.stores
|
||||
.addresstype_to_addresshash_to_addressindex
|
||||
.get_unwrap(addresstype)
|
||||
.get(&address_hash)
|
||||
.unwrap()
|
||||
.get(&address_hash)?
|
||||
.map(|v| *v)
|
||||
.and_then(|typeindex_local| {
|
||||
(typeindex_local < self.indexes.to_typeindex(addresstype))
|
||||
@@ -68,22 +65,14 @@ impl<'a> BlockProcessor<'a> {
|
||||
});
|
||||
|
||||
if check_collisions && let Some(typeindex) = existing_typeindex {
|
||||
let prev_addressbytes_opt = self.vecs.get_addressbytes_by_type(
|
||||
let prev_addressbytes = self.vecs.get_addressbytes_by_type(
|
||||
addresstype,
|
||||
typeindex,
|
||||
self.readers.addressbytes.get_unwrap(addresstype),
|
||||
)?;
|
||||
let prev_addressbytes = prev_addressbytes_opt
|
||||
.as_ref()
|
||||
.ok_or(Error::Internal("Missing addressbytes"))?;
|
||||
)
|
||||
.ok_or(Error::Internal("Missing addressbytes"))?;
|
||||
|
||||
if self
|
||||
.stores
|
||||
.addresstype_to_addresshash_to_addressindex
|
||||
.get_unwrap(addresstype)
|
||||
.needs(height)
|
||||
&& prev_addressbytes != &address_bytes
|
||||
{
|
||||
if prev_addressbytes != address_bytes {
|
||||
let txid = tx.compute_txid();
|
||||
dbg!(
|
||||
height,
|
||||
@@ -121,7 +110,6 @@ impl<'a> BlockProcessor<'a> {
|
||||
txouts: Vec<ProcessedOutput>,
|
||||
same_block_spent_outpoints: &FxHashSet<OutPoint>,
|
||||
) -> Result<FxHashMap<OutPoint, SameBlockOutputInfo>> {
|
||||
let height = self.height;
|
||||
let mut already_added_addresshash: ByAddressType<FxHashMap<AddressHash, TypeIndex>> =
|
||||
ByAddressType::default();
|
||||
let mut same_block_output_info: FxHashMap<OutPoint, SameBlockOutputInfo> =
|
||||
@@ -172,7 +160,7 @@ impl<'a> BlockProcessor<'a> {
|
||||
self.stores
|
||||
.addresstype_to_addresshash_to_addressindex
|
||||
.get_mut_unwrap(addresstype)
|
||||
.insert_if_needed(address_hash, ti, height);
|
||||
.insert(address_hash, ti);
|
||||
self.vecs.push_bytes_if_needed(ti, address_bytes)?;
|
||||
|
||||
ti
|
||||
@@ -230,10 +218,9 @@ impl<'a> BlockProcessor<'a> {
|
||||
self.stores
|
||||
.addresstype_to_addressindex_and_txindex
|
||||
.get_mut_unwrap(addresstype)
|
||||
.insert_if_needed(
|
||||
.insert(
|
||||
AddressIndexTxIndex::from((addressindex, txindex)),
|
||||
Unit,
|
||||
height,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -254,10 +241,9 @@ impl<'a> BlockProcessor<'a> {
|
||||
self.stores
|
||||
.addresstype_to_addressindex_and_unspentoutpoint
|
||||
.get_mut_unwrap(addresstype)
|
||||
.insert_if_needed(
|
||||
.insert(
|
||||
AddressIndexOutPoint::from((addressindex, outpoint)),
|
||||
Unit,
|
||||
height,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use bitcoin::{Transaction, TxIn, TxOut};
|
||||
use bitcoin::{Transaction, TxOut};
|
||||
use brk_types::{
|
||||
AddressBytes, AddressHash, OutPoint, OutputType, TxIndex, TxOutIndex, Txid, TxidPrefix,
|
||||
TypeIndex, Vin, Vout,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum InputSource<'a> {
|
||||
pub enum InputSource {
|
||||
PreviousBlock {
|
||||
vin: Vin,
|
||||
txindex: TxIndex,
|
||||
@@ -15,7 +15,6 @@ pub enum InputSource<'a> {
|
||||
},
|
||||
SameBlock {
|
||||
txindex: TxIndex,
|
||||
txin: &'a TxIn,
|
||||
vin: Vin,
|
||||
outpoint: OutPoint,
|
||||
},
|
||||
@@ -43,4 +42,6 @@ pub struct ComputedTx<'a> {
|
||||
pub txid: Txid,
|
||||
pub txid_prefix: TxidPrefix,
|
||||
pub prev_txindex_opt: Option<TxIndex>,
|
||||
pub base_size: u32,
|
||||
pub total_size: u32,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user