pub trait BlockchainFactory {
    type Inner: Blockchain;

    // Required method
    fn build(
        &self,
        wallet_name: &str,
        override_skip_blocks: Option<u32>
    ) -> Result<Self::Inner, Error>;

    // Provided methods
    fn build_for_wallet<D: BatchDatabase>(
        &self,
        wallet: &Wallet<D>,
        override_skip_blocks: Option<u32>
    ) -> Result<Self::Inner, Error> { ... }
    fn sync_wallet<D: BatchDatabase>(
        &self,
        wallet: &Wallet<D>,
        override_skip_blocks: Option<u32>,
        sync_options: SyncOptions
    ) -> Result<(), Error> { ... }
}
Expand description

Trait for a factory of blockchains that share the underlying connection or configuration

Example

This example shows how to sync multiple walles and return the sum of their balances

fn sum_of_balances<B: BlockchainFactory>(blockchain_factory: B, wallets: &[Wallet<MemoryDatabase>]) -> Result<Balance, Error> {
    Ok(wallets
        .iter()
        .map(|w| -> Result<_, Error> {
            blockchain_factory.sync_wallet(&w, None, SyncOptions::default())?;
            w.get_balance()
        })
        .collect::<Result<Vec<_>, _>>()?
        .into_iter()
        .sum())
}

Required Associated Types§

source

type Inner: Blockchain

The type returned when building a blockchain from this factory

Required Methods§

source

fn build( &self, wallet_name: &str, override_skip_blocks: Option<u32> ) -> Result<Self::Inner, Error>

Build a new blockchain for the given descriptor wallet_name

If override_skip_blocks is None, the returned blockchain will inherit the number of blocks from the factory. Since it’s not possible to override the value to None, set it to Some(0) to rescan from the genesis.

Provided Methods§

source

fn build_for_wallet<D: BatchDatabase>( &self, wallet: &Wallet<D>, override_skip_blocks: Option<u32> ) -> Result<Self::Inner, Error>

Build a new blockchain for a given wallet

Internally uses wallet_name_from_descriptor to derive the name, and then calls BlockchainFactory::build to create the blockchain instance.

source

fn sync_wallet<D: BatchDatabase>( &self, wallet: &Wallet<D>, override_skip_blocks: Option<u32>, sync_options: SyncOptions ) -> Result<(), Error>

Available on non-crate feature async-interface only.

Use BlockchainFactory::build_for_wallet to get a blockchain, then sync the wallet

This can be used when a new blockchain would only be used to sync a wallet and then immediately dropped. Keep in mind that specific blockchain factories may perform slow operations to build a blockchain for a given wallet, so if a wallet needs to be synced often it’s recommended to use BlockchainFactory::build_for_wallet to reuse the same blockchain multiple times.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<T: StatelessBlockchain> BlockchainFactory for Arc<T>

§

type Inner = Arc<T>

source§

fn build( &self, _wallet_name: &str, _override_skip_blocks: Option<u32> ) -> Result<Self, Error>

Implementors§

source§

impl BlockchainFactory for RpcBlockchainFactory

Available on crate feature rpc only.