refactor: improve docs, cleanup unnecessary types and improve code

This commit is contained in:
Vladimir Fomene
2023-09-06 09:47:45 +03:00
parent 4104206980
commit d43ae0231f
11 changed files with 175 additions and 169 deletions

View File

@@ -13,7 +13,7 @@ use bdk_chain::{
};
use bdk_electrum::{
electrum_client::{self, ElectrumApi},
ElectrumExt,
ElectrumExt, ElectrumUpdate,
};
use example_cli::{
anyhow::{self, Context},
@@ -66,16 +66,16 @@ type ChangeSet = (
);
fn main() -> anyhow::Result<()> {
let (args, keymap, index, db, init_changeset) =
let (args, keymap, index, db, (disk_local_chain, disk_tx_graph)) =
example_cli::init::<ElectrumCommands, ChangeSet>(DB_MAGIC, DB_PATH)?;
let graph = Mutex::new({
let mut graph = IndexedTxGraph::new(index);
graph.apply_changeset(init_changeset.1);
graph.apply_changeset(disk_tx_graph);
graph
});
let chain = Mutex::new(LocalChain::from_changeset(init_changeset.0));
let chain = Mutex::new(LocalChain::from_changeset(disk_local_chain));
let electrum_url = match args.network {
Network::Bitcoin => "ssl://electrum.blockstream.info:50002",
@@ -251,18 +251,24 @@ fn main() -> anyhow::Result<()> {
// drop lock on graph and chain
drop((graph, chain));
let (chain_update, graph_update) = client
let electrum_update = client
.scan_without_keychain(tip, spks, txids, outpoints, scan_options.batch_size)
.context("scanning the blockchain")?;
(chain_update, graph_update, BTreeMap::new())
(electrum_update, BTreeMap::new())
}
};
let (chain_update, incomplete_graph_update, keychain_update) = response;
let (
ElectrumUpdate {
chain_update,
relevant_txids,
},
keychain_update,
) = response;
let missing_txids = {
let graph = &*graph.lock().unwrap();
incomplete_graph_update.missing_full_txs(graph.graph())
relevant_txids.missing_full_txs(graph.graph())
};
let now = std::time::UNIX_EPOCH
@@ -270,7 +276,7 @@ fn main() -> anyhow::Result<()> {
.expect("must get time")
.as_secs();
let graph_update = incomplete_graph_update.finalize(&client, Some(now), missing_txids)?;
let graph_update = relevant_txids.into_tx_graph(&client, Some(now), missing_txids)?;
let db_changeset = {
let mut chain = chain.lock().unwrap();

View File

@@ -7,11 +7,13 @@ use std::io::Write;
use std::str::FromStr;
use bdk::bitcoin::Address;
use bdk::wallet::WalletUpdate;
use bdk::wallet::Update;
use bdk::SignOptions;
use bdk::{bitcoin::Network, Wallet};
use bdk_electrum::electrum_client::{self, ElectrumApi};
use bdk_electrum::ElectrumExt;
use bdk_electrum::{
electrum_client::{self, ElectrumApi},
ElectrumExt, ElectrumUpdate,
};
use bdk_file_store::Store;
fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -53,16 +55,20 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
})
.collect();
let (chain_update, incomplete_graph_update, keychain_update) =
client.scan(prev_tip, keychain_spks, None, None, STOP_GAP, BATCH_SIZE)?;
let (
ElectrumUpdate {
chain_update,
relevant_txids,
},
keychain_update,
) = client.scan(prev_tip, keychain_spks, None, None, STOP_GAP, BATCH_SIZE)?;
println!();
let missing = incomplete_graph_update.missing_full_txs(wallet.as_ref());
let graph_update =
incomplete_graph_update.finalize_with_confirmation_time(&client, None, missing)?;
let missing = relevant_txids.missing_full_txs(wallet.as_ref());
let graph_update = relevant_txids.into_confirmation_time_tx_graph(&client, None, missing)?;
let wallet_update = WalletUpdate {
let wallet_update = Update {
last_active_indices: keychain_update,
graph: graph_update,
chain: Some(chain_update),

View File

@@ -2,7 +2,7 @@ use std::{io::Write, str::FromStr};
use bdk::{
bitcoin::{Address, Network},
wallet::{AddressIndex, WalletUpdate},
wallet::{AddressIndex, Update},
SignOptions, Wallet,
};
use bdk_esplora::{esplora_client, EsploraAsyncExt};
@@ -58,7 +58,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.await?;
let missing_heights = wallet.tx_graph().missing_heights(wallet.local_chain());
let chain_update = client.update_local_chain(prev_tip, missing_heights).await?;
let update = WalletUpdate {
let update = Update {
last_active_indices,
graph: update_graph,
chain: Some(chain_update),

View File

@@ -7,7 +7,7 @@ use std::{io::Write, str::FromStr};
use bdk::{
bitcoin::{Address, Network},
wallet::{AddressIndex, WalletUpdate},
wallet::{AddressIndex, Update},
SignOptions, Wallet,
};
use bdk_esplora::{esplora_client, EsploraExt};
@@ -57,7 +57,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
client.update_tx_graph(keychain_spks, None, None, STOP_GAP, PARALLEL_REQUESTS)?;
let missing_heights = wallet.tx_graph().missing_heights(wallet.local_chain());
let chain_update = client.update_local_chain(prev_tip, missing_heights)?;
let update = WalletUpdate {
let update = Update {
last_active_indices,
graph: update_graph,
chain: Some(chain_update),