use bitcoin::ScriptBuf; use brk_types::{ AddrBytes, OutputType, P2AAddrIndex, P2ABytes, P2PK33AddrIndex, P2PK33Bytes, P2PK65AddrIndex, P2PK65Bytes, P2PKHAddrIndex, P2PKHBytes, P2SHAddrIndex, P2SHBytes, P2TRAddrIndex, P2TRBytes, P2WPKHAddrIndex, P2WPKHBytes, P2WSHAddrIndex, P2WSHBytes, TxIndex, TxOutIndex, Txid, TypeIndex, }; use vecdb::{BytesStrategy, VecReader}; use crate::Vecs; pub struct AddrReaders { pub p2pk65: VecReader>, pub p2pk33: VecReader>, pub p2pkh: VecReader>, pub p2sh: VecReader>, pub p2wpkh: VecReader>, pub p2wsh: VecReader>, pub p2tr: VecReader>, pub p2a: VecReader>, } impl AddrReaders { pub fn script_pubkey(&self, output_type: OutputType, type_index: TypeIndex) -> ScriptBuf { let idx = usize::from(type_index); let bytes: Option = match output_type { OutputType::P2PK65 => self.p2pk65.try_get(idx).map(Into::into), OutputType::P2PK33 => self.p2pk33.try_get(idx).map(Into::into), OutputType::P2PKH => self.p2pkh.try_get(idx).map(Into::into), OutputType::P2SH => self.p2sh.try_get(idx).map(Into::into), OutputType::P2WPKH => self.p2wpkh.try_get(idx).map(Into::into), OutputType::P2WSH => self.p2wsh.try_get(idx).map(Into::into), OutputType::P2TR => self.p2tr.try_get(idx).map(Into::into), OutputType::P2A => self.p2a.try_get(idx).map(Into::into), _ => None, }; bytes.map(|b| b.to_script_pubkey()).unwrap_or_default() } } /// Readers for vectors that need to be accessed during block processing. /// /// All fields use `VecReader` which caches the mmap base pointer for O(1) /// random access without recomputing `region.start() + HEADER_OFFSET` per read. pub struct Readers { pub txid: VecReader>, pub tx_index_to_first_txout_index: VecReader>, pub txout_index_to_output_type: VecReader>, pub txout_index_to_type_index: VecReader>, pub addrbytes: AddrReaders, } impl Readers { pub fn new(vecs: &Vecs) -> Self { Self { txid: vecs.transactions.txid.reader(), tx_index_to_first_txout_index: vecs.transactions.first_txout_index.reader(), txout_index_to_output_type: vecs.outputs.output_type.reader(), txout_index_to_type_index: vecs.outputs.type_index.reader(), addrbytes: vecs.addrs.addr_readers(), } } }