Skip to main content

ChainSource

Struct ChainSource 

Source
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:

Examples:

use bark::chain::{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);
#[cfg(feature = "socks5-proxy")]
let socks5 = Some("socks5h://127.0.0.1:9050");

let instance = ChainSource::new(spec, network, fallback_fee, socks5).await.unwrap();

Implementations§

Source§

impl ChainSource

Source

pub async 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

Source

pub async fn fee_rates(&self) -> FeeRates

Gets a cached copy of the calculated network FeeRates

Source

pub fn network(&self) -> Network

Gets the network that the ChainSource was validated against.

Source

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_source is of type ChainSourceSpec::Bitcoind, it creates a Bitcoin Core RPC client using the provided URL and authentication parameters.
  • If chain_source is 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.
Source

pub async fn tip(&self) -> Result<BlockHeight>

Source

pub async fn invalidate_caches(&self)

Drop the cached tip and fee-rate values, forcing the next call to tip() or update_fee_rates() to round-trip the backend. Useful in tests that fabricate chain changes faster than the TTL so the next observation is deterministic without sleeping.

Source

pub async fn tip_ref(&self) -> Result<BlockRef>

Source

pub async fn block_ref(&self, height: BlockHeight) -> Result<BlockRef>

Source

pub async fn block(&self, hash: BlockHash) -> Result<Option<Block>>

Source

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.

Source

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.

Source

pub async fn broadcast_tx(&self, tx: &Transaction) -> Result<()>

Source

pub async fn broadcast_package( &self, txs: &[impl Borrow<Transaction>], ) -> Result<(), BroadcastError>

Source

pub async fn get_tx(&self, txid: &Txid) -> Result<Option<Transaction>>

Source

pub async fn tx_confirmed(&self, txid: Txid) -> Result<Option<BlockHeight>>

Returns the block height the tx is confirmed in, if any.

Source

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

Source

pub async fn txout_value(&self, outpoint: &OutPoint) -> Result<Amount>

Source

pub async fn update_fee_rates( &self, fallback_fee: Option<FeeRate>, ) -> Result<()>

Gets the current fee rates from the chain source, falling back to user-specified values if necessary.

No-ops if a previous successful call ran within FEE_RATES_CACHE_TTL. The fallback path overwrites the cached rates but deliberately does not advance the cache timestamp, so the next call retries the backend instead of serving the fallback for another full TTL.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<L> LayerExt<L> for L

Source§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in Layered.
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more