pub struct ChainSource { /* private fields */ }Expand description
Client for interacting with the configured on-chain backend.
ChainSource abstracts over multiple backends using ChainSourceSpec to provide:
- Chain queries (tip, block headers/blocks, transaction status and fetching)
- Mempool-related utilities (ancestor fee/weight, spending lookups)
- Broadcasting single transactions or packages (RBF/CPFP workflows)
- Fee estimation and caching with optional fallback values
Behavior notes:
- ChainSource::update_fee_rates refreshes internal fee estimates; if backend estimates fail and a fallback fee is provided, it will be used for all tiers.
- ChainSource::fee_rates returns the last cached FeeRates.
Examples:
use bark::onchain::{ChainSource, ChainSourceSpec};
use bdk_bitcoind_rpc::bitcoincore_rpc::Auth;
use bitcoin::{FeeRate, Network};
let spec = ChainSourceSpec::Bitcoind {
url: "http://localhost:8332".into(),
auth: Auth::UserPass("user".into(), "password".into()),
};
let network = Network::Bitcoin;
let fallback_fee = FeeRate::from_sat_per_vb(5);
let instance = ChainSource::new(spec, network, fallback_fee).await.unwrap();Implementations§
Source§impl ChainSource
impl ChainSource
Sourcepub fn require_version(&self) -> Result<()>
pub fn require_version(&self) -> Result<()>
Checks that the version of the chain source is compatible with Bark.
For bitcoind, it checks if the version is at least 29.0 This is the first version for which 0 fee-anchors are considered standard
Sourcepub async fn fee_rates(&self) -> FeeRates
pub async fn fee_rates(&self) -> FeeRates
Gets a cached copy of the calculated network FeeRates
Sourcepub fn network(&self) -> Network
pub fn network(&self) -> Network
Gets the network that the ChainSource was validated against.
Sourcepub async fn new(
spec: ChainSourceSpec,
network: Network,
fallback_fee: Option<FeeRate>,
) -> Result<Self>
pub async fn new( spec: ChainSourceSpec, network: Network, fallback_fee: Option<FeeRate>, ) -> Result<Self>
Creates a new instance of the object with the specified chain source, network, and optional fallback fee rate.
This function initializes the internal chain source client based on the provided chain_source:
- If
chain_sourceis of type ChainSourceSpec::Bitcoind, it creates a Bitcoin Core RPC client using the provided URL and authentication parameters. - If
chain_sourceis of type ChainSourceSpec::Esplora, it creates an Esplora client with the given URL.
Both clients are initialized asynchronously, and any errors encountered during their creation will be returned as part of the anyhow::Result.
Additionally, the function performs a network consistency check to ensure the specified
network (e.g., mainnet or signet) matches the network configuration of the initialized
chain source client.
The fallback_fee parameter is optional. If provided, it is used as the default fee rate
for transactions. If not specified, the FeeRate::BROADCAST_MIN is used as the default fee
rate.
§Arguments
chain_source- Specifies the backend to use for blockchain data.network- The Bitcoin network to operate on (e.g.,mainnet,testnet,regtest).fallback_fee- An optional fallback fee rate to use for transaction fee estimation. If not provided, a default fee rate of FeeRate::BROADCAST_MIN will be used.
§Returns
Ok(Self)- If the object is successfully created with all necessary configurations.Err(anyhow::Error)- If there is an error in initializing the chain source client or verifying the network.
pub async fn tip(&self) -> Result<BlockHeight>
pub async fn block_ref(&self, height: BlockHeight) -> Result<BlockRef>
pub async fn block(&self, hash: BlockHash) -> Result<Option<Block>>
Sourcepub async fn mempool_ancestor_info(
&self,
txid: Txid,
) -> Result<MempoolAncestorInfo>
pub async fn mempool_ancestor_info( &self, txid: Txid, ) -> Result<MempoolAncestorInfo>
Retrieves basic CPFP ancestry information of the given transaction. Confirmed transactions are ignored as they are not relevant to CPFP.
Sourcepub async fn txs_spending_inputs<T: IntoIterator<Item = OutPoint>>(
&self,
outpoints: T,
block_scan_start: BlockHeight,
) -> Result<TxsSpendingInputsResult>
pub async fn txs_spending_inputs<T: IntoIterator<Item = OutPoint>>( &self, outpoints: T, block_scan_start: BlockHeight, ) -> Result<TxsSpendingInputsResult>
For each provided outpoint, fetches the ID of any confirmed or unconfirmed in which the outpoint is spent.
pub async fn broadcast_tx(&self, tx: &Transaction) -> Result<()>
pub async fn broadcast_package( &self, txs: &[impl Borrow<Transaction>], ) -> Result<()>
pub async fn get_tx(&self, txid: &Txid) -> Result<Option<Transaction>>
Sourcepub async fn tx_confirmed(&self, txid: Txid) -> Result<Option<BlockHeight>>
pub async fn tx_confirmed(&self, txid: Txid) -> Result<Option<BlockHeight>>
Returns the block height the tx is confirmed in, if any.
Sourcepub async fn tx_status(&self, txid: Txid) -> Result<TxStatus>
pub async fn tx_status(&self, txid: Txid) -> Result<TxStatus>
Returns the status of the given transaction, including the block height if it is confirmed
pub async fn txout_value(&self, outpoint: &OutPoint) -> Result<Amount>
Auto Trait Implementations§
impl !Freeze for ChainSource
impl !RefUnwindSafe for ChainSource
impl Send for ChainSource
impl Sync for ChainSource
impl Unpin for ChainSource
impl !UnwindSafe for ChainSource
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request