diff --git a/crates/chain/src/chain_graph.rs b/crates/chain/src/chain_graph.rs index ab0db6d9..a446ed20 100644 --- a/crates/chain/src/chain_graph.rs +++ b/crates/chain/src/chain_graph.rs @@ -5,7 +5,7 @@ use crate::{ tx_graph::{self, TxGraph}, AsTransaction, BlockId, ForEachTxOut, FullTxOut, IntoOwned, TxHeight, }; -use alloc::{borrow::Cow, string::ToString, vec::Vec}; +use alloc::{string::ToString, vec::Vec}; use bitcoin::{OutPoint, Transaction, TxOut, Txid}; use core::fmt::Debug; @@ -129,19 +129,47 @@ where &self, update: SparseChain

, new_txs: impl IntoIterator, - ) -> Result>, NewError

> { + ) -> Result, NewError

> { + let mut inflated_chain = SparseChain::default(); let mut inflated_graph = TxGraph::default(); - for (_, txid) in update.txids() { - if let Some(tx) = self.graph.get_tx(*txid) { - let _ = inflated_graph.insert_tx(Cow::Borrowed(tx)); + + for (height, hash) in update.checkpoints().clone().into_iter() { + let _ = inflated_chain + .insert_checkpoint(BlockId { height, hash }) + .expect("must insert"); + } + + // [TODO] @evanlinjin: These need better comments + // - copy transactions that have changed positions into the graph + // - add new transactions to inflated chain + for (pos, txid) in update.txids() { + match self.chain.tx_position(*txid) { + Some(original_pos) => { + if original_pos != pos { + let tx = self + .graph + .get_tx(*txid) + .expect("tx must exist as it is referenced in sparsechain") + .clone(); + let _ = inflated_chain + .insert_tx(*txid, pos.clone()) + .expect("must insert since this was already in update"); + let _ = inflated_graph.insert_tx(tx); + } + } + None => { + let _ = inflated_chain + .insert_tx(*txid, pos.clone()) + .expect("must insert since this was already in update"); + } } } for tx in new_txs { - let _ = inflated_graph.insert_tx(Cow::Owned(tx)); + let _ = inflated_graph.insert_tx(tx); } - ChainGraph::new(update, inflated_graph) + ChainGraph::new(inflated_chain, inflated_graph) } /// Sets the checkpoint limit. diff --git a/crates/electrum/src/lib.rs b/crates/electrum/src/lib.rs index 8f07de59..9f5061ca 100644 --- a/crates/electrum/src/lib.rs +++ b/crates/electrum/src/lib.rs @@ -21,7 +21,6 @@ //! [`bdk_electrum_example`]: https://github.com/LLFourn/bdk_core_staging/tree/master/bdk_electrum_example use std::{ - borrow::Cow, collections::{BTreeMap, HashMap}, fmt::Debug, }; @@ -249,7 +248,7 @@ impl ElectrumUpdate { self, new_txs: Vec, chain_graph: &CG, - ) -> Result>, chain_graph::NewError

> + ) -> Result, chain_graph::NewError

> where T: AsTransaction + Clone + Ord, CG: AsRef>,