bdk_electrum API improvements and simplifications
				
					
				
			* `ElectrumUpdate::missing_full_txs` now returns a `Vec<Txid>` so we don't keep a reference to the passed-in `graph`. * `ElectrumUpdate::finalize*` methods now takes in `missing` txids instead of `full_txs`. `Client::batch_transaction_get` is called within the methods. Other changes: * `wallet::ChangeSet` is now made public externally. This is required as a wallet db should implement `PersistBackend<wallet::ChangeSet>`.
This commit is contained in:
		
							parent
							
								
									92709d03ce
								
							
						
					
					
						commit
						78a7920ba3
					
				| @ -97,7 +97,8 @@ pub struct Wallet<D = ()> { | |||||||
| pub type Update = LocalUpdate<KeychainKind, ConfirmationTimeAnchor>; | pub type Update = LocalUpdate<KeychainKind, ConfirmationTimeAnchor>; | ||||||
| 
 | 
 | ||||||
| // /// The changeset produced internally by applying an update.
 | // /// The changeset produced internally by applying an update.
 | ||||||
| pub(crate) type ChangeSet = LocalChangeSet<KeychainKind, ConfirmationTimeAnchor>; | pub type ChangeSet = LocalChangeSet<KeychainKind, ConfirmationTimeAnchor>; | ||||||
|  | 
 | ||||||
| /// The address index selection strategy to use to derived an address from the wallet's external
 | /// The address index selection strategy to use to derived an address from the wallet's external
 | ||||||
| /// descriptor. See [`Wallet::get_address`]. If you're unsure which one to use use `WalletIndex::New`.
 | /// descriptor. See [`Wallet::get_address`]. If you're unsure which one to use use `WalletIndex::New`.
 | ||||||
| #[derive(Debug)] | #[derive(Debug)] | ||||||
|  | |||||||
| @ -30,20 +30,22 @@ impl<K, A> Default for ElectrumUpdate<K, A> { | |||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| impl<'a, K, A: Anchor> ElectrumUpdate<K, A> { | impl<K, A: Anchor> ElectrumUpdate<K, A> { | ||||||
|     pub fn missing_full_txs<A2>( |     pub fn missing_full_txs<A2>(&self, graph: &TxGraph<A2>) -> Vec<Txid> { | ||||||
|         &'a self, |  | ||||||
|         graph: &'a TxGraph<A2>, |  | ||||||
|     ) -> impl Iterator<Item = &'a Txid> + 'a { |  | ||||||
|         self.graph_update |         self.graph_update | ||||||
|             .keys() |             .keys() | ||||||
|             .filter(move |&&txid| graph.as_ref().get_tx(txid).is_none()) |             .filter(move |&&txid| graph.as_ref().get_tx(txid).is_none()) | ||||||
|  |             .cloned() | ||||||
|  |             .collect() | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     pub fn finalize<T>(self, seen_at: Option<u64>, new_txs: T) -> LocalUpdate<K, A> |     pub fn finalize( | ||||||
|     where |         self, | ||||||
|         T: IntoIterator<Item = Transaction>, |         client: &Client, | ||||||
|     { |         seen_at: Option<u64>, | ||||||
|  |         missing: Vec<Txid>, | ||||||
|  |     ) -> Result<LocalUpdate<K, A>, Error> { | ||||||
|  |         let new_txs = client.batch_transaction_get(&missing)?; | ||||||
|         let mut graph_update = TxGraph::<A>::new(new_txs); |         let mut graph_update = TxGraph::<A>::new(new_txs); | ||||||
|         for (txid, anchors) in self.graph_update { |         for (txid, anchors) in self.graph_update { | ||||||
|             if let Some(seen_at) = seen_at { |             if let Some(seen_at) = seen_at { | ||||||
| @ -53,11 +55,11 @@ impl<'a, K, A: Anchor> ElectrumUpdate<K, A> { | |||||||
|                 let _ = graph_update.insert_anchor(txid, anchor); |                 let _ = graph_update.insert_anchor(txid, anchor); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|         LocalUpdate { |         Ok(LocalUpdate { | ||||||
|             keychain: self.keychain_update, |             keychain: self.keychain_update, | ||||||
|             graph: graph_update, |             graph: graph_update, | ||||||
|             chain: self.chain_update, |             chain: self.chain_update, | ||||||
|         } |         }) | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -68,16 +70,13 @@ impl<K> ElectrumUpdate<K, ConfirmationHeightAnchor> { | |||||||
|     /// **Note:** The confirmation time might not be precisely correct if there has been a reorg.
 |     /// **Note:** The confirmation time might not be precisely correct if there has been a reorg.
 | ||||||
|     /// Electrum's API intends that we use the merkle proof API, we should change `bdk_electrum` to
 |     /// Electrum's API intends that we use the merkle proof API, we should change `bdk_electrum` to
 | ||||||
|     /// use it.
 |     /// use it.
 | ||||||
|     pub fn finalize_as_confirmation_time<T>( |     pub fn finalize_as_confirmation_time( | ||||||
|         self, |         self, | ||||||
|         client: &Client, |         client: &Client, | ||||||
|         seen_at: Option<u64>, |         seen_at: Option<u64>, | ||||||
|         new_txs: T, |         missing: Vec<Txid>, | ||||||
|     ) -> Result<LocalUpdate<K, ConfirmationTimeAnchor>, Error> |     ) -> Result<LocalUpdate<K, ConfirmationTimeAnchor>, Error> { | ||||||
|     where |         let update = self.finalize(client, seen_at, missing)?; | ||||||
|         T: IntoIterator<Item = Transaction>, |  | ||||||
|     { |  | ||||||
|         let update = self.finalize(seen_at, new_txs); |  | ||||||
| 
 | 
 | ||||||
|         let relevant_heights = { |         let relevant_heights = { | ||||||
|             let mut visited_heights = HashSet::new(); |             let mut visited_heights = HashSet::new(); | ||||||
| @ -111,7 +110,7 @@ impl<K> ElectrumUpdate<K, ConfirmationHeightAnchor> { | |||||||
|                     .anchors |                     .anchors | ||||||
|                     .into_iter() |                     .into_iter() | ||||||
|                     .map(|(height_anchor, txid)| { |                     .map(|(height_anchor, txid)| { | ||||||
|                         let confirmation_height = dbg!(height_anchor.confirmation_height); |                         let confirmation_height = height_anchor.confirmation_height; | ||||||
|                         let confirmation_time = height_to_time[&confirmation_height]; |                         let confirmation_time = height_to_time[&confirmation_height]; | ||||||
|                         let time_anchor = ConfirmationTimeAnchor { |                         let time_anchor = ConfirmationTimeAnchor { | ||||||
|                             anchor_block: height_anchor.anchor_block, |                             anchor_block: height_anchor.anchor_block, | ||||||
|  | |||||||
| @ -278,20 +278,15 @@ fn main() -> anyhow::Result<()> { | |||||||
| 
 | 
 | ||||||
|     let missing_txids = { |     let missing_txids = { | ||||||
|         let graph = &*graph.lock().unwrap(); |         let graph = &*graph.lock().unwrap(); | ||||||
|         response |         response.missing_full_txs(graph.graph()) | ||||||
|             .missing_full_txs(graph.graph()) |  | ||||||
|             .cloned() |  | ||||||
|             .collect::<Vec<_>>() |  | ||||||
|     }; |     }; | ||||||
| 
 | 
 | ||||||
|     let new_txs = client |  | ||||||
|         .batch_transaction_get(&missing_txids) |  | ||||||
|         .context("fetching full transactions")?; |  | ||||||
|     let now = std::time::UNIX_EPOCH |     let now = std::time::UNIX_EPOCH | ||||||
|         .elapsed() |         .elapsed() | ||||||
|         .expect("must get time") |         .expect("must get time") | ||||||
|         .as_secs(); |         .as_secs(); | ||||||
|     let final_update = response.finalize(Some(now), new_txs); | 
 | ||||||
|  |     let final_update = response.finalize(&client, Some(now), missing_txids)?; | ||||||
| 
 | 
 | ||||||
|     let db_changeset = { |     let db_changeset = { | ||||||
|         let mut chain = chain.lock().unwrap(); |         let mut chain = chain.lock().unwrap(); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user