Skip to content

Commit

Permalink
wallet: save descriptor inside wallet cache and data
Browse files Browse the repository at this point in the history
this is required for identifying which data/cache belongs to each wallet, when stored separately using persistence providers
  • Loading branch information
dr-orlovsky committed Sep 1, 2024
1 parent 6630037 commit 5dc38aa
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/indexers/electrum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl Indexer for Client {
&self,
descriptor: &WalletDescr<K, D, L2::Descr>,
) -> MayError<WalletCache<L2::Cache>, Vec<Self::Error>> {
let mut cache = WalletCache::new();
let mut cache = WalletCache::new_nonsync(descriptor.generator());

Check warning on line 69 in src/indexers/electrum.rs

View check run for this annotation

Codecov / codecov/patch

src/indexers/electrum.rs#L69

Added line #L69 was not covered by tests
let mut errors = Vec::<ElectrumError>::new();

let mut address_index = BTreeMap::new();
Expand Down
2 changes: 1 addition & 1 deletion src/indexers/esplora.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ impl Indexer for Client {
&self,
descriptor: &WalletDescr<K, D, L2::Descr>,
) -> MayError<WalletCache<L2::Cache>, Vec<Self::Error>> {
let mut cache = WalletCache::new();
let mut cache = WalletCache::new_nonsync(descriptor.generator());

Check warning on line 199 in src/indexers/esplora.rs

View check run for this annotation

Codecov / codecov/patch

src/indexers/esplora.rs#L199

Added line #L199 was not covered by tests
let mut errors = vec![];

let mut address_index = BTreeMap::new();
Expand Down
17 changes: 10 additions & 7 deletions src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ pub struct WalletData<L2: Layer2Data> {
#[cfg_attr(feature = "serde", serde(skip))]
persistence: Option<Persistence<Self>>,

#[cfg_attr(feature = "serde", serde(skip))]
pub descriptor: String,
pub name: String,
pub tx_annotations: BTreeMap<Txid, String>,
pub txout_annotations: BTreeMap<Outpoint, String>,
Expand All @@ -210,6 +212,7 @@ impl<L2: Layer2Data> CloneNoPersistence for WalletData<L2> {
fn clone_no_persistence(&self) -> Self {
Self {
persistence: None,
descriptor: self.descriptor.clone(),
name: self.name.clone(),
tx_annotations: self.tx_annotations.clone(),
txout_annotations: self.txout_annotations.clone(),
Expand Down Expand Up @@ -257,6 +260,8 @@ pub struct WalletCache<L2: Layer2Cache> {
#[cfg_attr(feature = "serde", serde(skip))]
persistence: Option<Persistence<Self>>,

#[cfg_attr(feature = "serde", serde(skip))]
pub descriptor: String,
pub last_block: MiningInfo,
pub last_change: NormalIndex,
pub headers: BTreeSet<BlockInfo>,
Expand All @@ -266,14 +271,11 @@ pub struct WalletCache<L2: Layer2Cache> {
pub layer2: L2,
}

impl<L2: Layer2Cache> Default for WalletCache<L2> {
fn default() -> Self { WalletCache::new() }
}

impl<L2C: Layer2Cache> WalletCache<L2C> {
pub(crate) fn new() -> Self {
pub(crate) fn new_nonsync<K, D: Descriptor<K>>(descriptor: &D) -> Self {

Check warning on line 275 in src/wallet.rs

View check run for this annotation

Codecov / codecov/patch

src/wallet.rs#L275

Added line #L275 was not covered by tests
WalletCache {
persistence: None,
descriptor: descriptor.to_string(),

Check warning on line 278 in src/wallet.rs

View check run for this annotation

Codecov / codecov/patch

src/wallet.rs#L277-L278

Added lines #L277 - L278 were not covered by tests
last_block: MiningInfo::genesis(),
last_change: NormalIndex::ZERO,
headers: none!(),
Expand Down Expand Up @@ -344,6 +346,7 @@ impl<L2: Layer2Cache> CloneNoPersistence for WalletCache<L2> {
fn clone_no_persistence(&self) -> Self {
Self {
persistence: None,
descriptor: self.descriptor.clone(),
last_block: self.last_block.clone(),
last_change: self.last_change.clone(),
headers: self.headers.clone(),
Expand Down Expand Up @@ -430,9 +433,9 @@ impl<K, D: Descriptor<K>, L2: Layer2> PsbtConstructor for Wallet<K, D, L2> {
impl<K, D: Descriptor<K>> Wallet<K, D> {
pub fn new_layer1(descr: D, network: Network) -> Self {
Wallet {
cache: WalletCache::new_nonsync(&descr),

Check warning on line 436 in src/wallet.rs

View check run for this annotation

Codecov / codecov/patch

src/wallet.rs#L436

Added line #L436 was not covered by tests
descr: WalletDescr::new_standard(descr, network),
data: empty!(),
cache: WalletCache::new(),
layer2: none!(),

Check warning on line 439 in src/wallet.rs

View check run for this annotation

Codecov / codecov/patch

src/wallet.rs#L439

Added line #L439 was not covered by tests
}
}
Expand All @@ -441,9 +444,9 @@ impl<K, D: Descriptor<K>> Wallet<K, D> {
impl<K, D: Descriptor<K>, L2: Layer2> Wallet<K, D, L2> {
pub fn new_layer2(descr: D, l2_descr: L2::Descr, layer2: L2, network: Network) -> Self {
Wallet {
cache: WalletCache::new_nonsync(&descr),

Check warning on line 447 in src/wallet.rs

View check run for this annotation

Codecov / codecov/patch

src/wallet.rs#L447

Added line #L447 was not covered by tests
descr: WalletDescr::new_layer2(descr, l2_descr, network),
data: empty!(),
cache: WalletCache::new(),
layer2,
}
}
Expand Down

0 comments on commit 5dc38aa

Please sign in to comment.