Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/1151 with evans poc #5

Draft
wants to merge 3 commits into
base: fix/1151
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/cont_integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ jobs:
cargo update -p zip:0.6.6 --precise "0.6.2"
cargo update -p time --precise "0.3.13"
cargo update -p cc --precise "1.0.81"
cargo update -p byteorder --precise "1.4.3"
- name: Build
run: cargo build ${{ matrix.features }}
- name: Test
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ cargo update -p zip:0.6.6 --precise "0.6.2"
cargo update -p time --precise "0.3.13"
# cc 1.0.82 has MSRV 1.61.0+
cargo update -p cc --precise "1.0.81"
# byteorder 1.5.0 has MSRV 1.60+
cargo update -p byteorder --precise "1.4.3"
```

## License
Expand Down
37 changes: 36 additions & 1 deletion crates/bdk/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,33 @@ pub struct Update {
pub chain: Option<local_chain::Update>,
}

impl From<TxGraph<ConfirmationTimeAnchor>> for Update {
fn from(graph: TxGraph<ConfirmationTimeAnchor>) -> Self {
Self {
graph,
..Default::default()
}
}
}

impl From<BTreeMap<KeychainKind, u32>> for Update {
fn from(last_active_indices: BTreeMap<KeychainKind, u32>) -> Self {
Self {
last_active_indices,
..Default::default()
}
}
}

impl From<local_chain::Update> for Update {
fn from(chain: local_chain::Update) -> Self {
Self {
chain: Some(chain),
..Default::default()
}
}
}

/// The changes made to a wallet by applying an [`Update`].
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, Default)]
pub struct ChangeSet {
Expand Down Expand Up @@ -1916,10 +1943,13 @@ impl<D> Wallet<D> {
/// transactions related to your wallet into it.
///
/// [`commit`]: Self::commit
pub fn apply_update(&mut self, update: Update) -> Result<(), CannotConnectError>
pub fn apply_update<U>(&mut self, update: U) -> Result<(), CannotConnectError>
where
U: Into<Update>,
D: PersistBackend<ChangeSet>,
{
let update = update.into();

let mut changeset = match update.chain {
Some(chain_update) => ChangeSet::from(self.chain.apply_update(chain_update)?),
None => ChangeSet::default(),
Expand All @@ -1940,6 +1970,11 @@ impl<D> Wallet<D> {
Ok(())
}

/// Get heights of missing checkpoints.
pub fn missing_checkpoints(&self) -> impl Iterator<Item = u32> + '_ {
self.tx_graph().missing_heights(self.local_chain())
}

/// Commits all currently [`staged`] changed to the persistence backend returning and error when
/// this fails.
///
Expand Down
19 changes: 10 additions & 9 deletions example-crates/wallet_esplora_async/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{io::Write, str::FromStr};

use bdk::{
bitcoin::{Address, Network},
wallet::{AddressIndex, Update},
wallet::AddressIndex,
SignOptions, Wallet,
};
use bdk_esplora::{esplora_client, EsploraAsyncExt};
Expand Down Expand Up @@ -53,17 +53,18 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
(k, k_spks)
})
.collect();

let (update_graph, last_active_indices) = client
.scan_txs_with_keychains(keychain_spks, None, None, STOP_GAP, PARALLEL_REQUESTS)
.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 = Update {
last_active_indices,
graph: update_graph,
chain: Some(chain_update),
};
wallet.apply_update(update)?;
wallet.apply_update(update_graph)?;
wallet.apply_update(last_active_indices)?;

let chain_update = client
.update_local_chain(prev_tip, wallet.missing_checkpoints())
.await?;
wallet.apply_update(chain_update)?;

wallet.commit()?;
println!();

Expand Down
17 changes: 7 additions & 10 deletions example-crates/wallet_esplora_blocking/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{io::Write, str::FromStr};

use bdk::{
bitcoin::{Address, Network},
wallet::{AddressIndex, Update},
wallet::AddressIndex,
SignOptions, Wallet,
};
use bdk_esplora::{esplora_client, EsploraExt};
Expand Down Expand Up @@ -55,15 +55,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

let (update_graph, last_active_indices) =
client.scan_txs_with_keychains(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 = Update {
last_active_indices,
graph: update_graph,
chain: Some(chain_update),
};

wallet.apply_update(update)?;
wallet.apply_update(update_graph)?;
wallet.apply_update(last_active_indices)?;

let chain_update = client.update_local_chain(prev_tip, wallet.missing_checkpoints())?;
wallet.apply_update(chain_update)?;

wallet.commit()?;
println!();

Expand Down