Skip to content

Commit

Permalink
wallet: separate persisting layer2 in Wallet from co-persisting layer2
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Sep 25, 2024
1 parent 7935e20 commit 0405406
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 41 deletions.
42 changes: 12 additions & 30 deletions src/layer2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,18 @@ use std::fmt::Debug;
use nonasync::persistence::{CloneNoPersistence, Persistence, Persisting};

pub trait Layer2: Debug + CloneNoPersistence + Persisting {
type Descr: Layer2Descriptor<LoadError = Self::LoadError, StoreError = Self::StoreError>;
type Data: Layer2Data<LoadError = Self::LoadError, StoreError = Self::StoreError>;
type Cache: Layer2Cache<LoadError = Self::LoadError, StoreError = Self::StoreError>;
type Descr: Layer2Descriptor;
type Data: Layer2Data;
type Cache: Layer2Cache;
type LoadError: error::Error;
type StoreError: error::Error;
}

pub trait Layer2Descriptor: Debug + CloneNoPersistence {
type LoadError: error::Error;
type StoreError: error::Error;
}
pub trait Layer2Descriptor: Debug + Clone {}

pub trait Layer2Data: Debug + CloneNoPersistence + Default {
type LoadError: error::Error;
type StoreError: error::Error;
}

pub trait Layer2Cache: Debug + CloneNoPersistence + Default {
type LoadError: error::Error;
type StoreError: error::Error;
pub trait Layer2Data: Debug + Clone + Default {}

pub trait Layer2Cache: Debug + Clone + Default {
type Tx: Layer2Tx;
type Coin: Layer2Coin;
}
Expand Down Expand Up @@ -100,29 +91,20 @@ impl Persisting for NoLayer2 {
}

impl Layer2 for NoLayer2 {
type Descr = NoLayer2;
type Data = NoLayer2;
type Cache = NoLayer2;
type Descr = Layer2Empty;
type Data = Layer2Empty;
type Cache = Layer2Empty;
type LoadError = Infallible;
type StoreError = Infallible;
}

impl Layer2Descriptor for NoLayer2 {
type LoadError = Infallible;
type StoreError = Infallible;
}
impl Layer2Descriptor for Layer2Empty {}

impl Layer2Data for NoLayer2 {
type LoadError = Infallible;
type StoreError = Infallible;
}
impl Layer2Data for Layer2Empty {}

impl Layer2Cache for NoLayer2 {
impl Layer2Cache for Layer2Empty {
type Tx = Layer2Empty;
type Coin = Layer2Empty;

type LoadError = Infallible;
type StoreError = Infallible;
}

impl Layer2Tx for Layer2Empty {}
Expand Down
22 changes: 11 additions & 11 deletions src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ use nonasync::persistence::{
use psbt::{PsbtConstructor, Utxo};

use crate::{
BlockInfo, CoinRow, Indexer, Layer2, Layer2Cache, Layer2Data, Layer2Descriptor, MayError,
MiningInfo, NoLayer2, Party, TxRow, WalletAddr, WalletTx, WalletUtxo,
BlockInfo, CoinRow, Indexer, Layer2, Layer2Cache, Layer2Data, Layer2Descriptor, Layer2Empty,
MayError, MiningInfo, NoLayer2, Party, TxRow, WalletAddr, WalletTx, WalletUtxo,
};

#[derive(Copy, Clone, Eq, PartialEq, Debug, Display, Error)]
Expand Down Expand Up @@ -82,7 +82,7 @@ impl<'descr, K, D: Descriptor<K>> Iterator for AddrIter<'descr, K, D> {
)
)]
#[derive(Getters, Debug)]
pub struct WalletDescr<K, D, L2 = NoLayer2>
pub struct WalletDescr<K, D, L2 = Layer2Empty>
where
D: Descriptor<K>,
L2: Layer2Descriptor,
Expand All @@ -99,7 +99,7 @@ where
_phantom: PhantomData<K>,
}

impl<K, D: Descriptor<K>> WalletDescr<K, D, NoLayer2> {
impl<K, D: Descriptor<K>> WalletDescr<K, D, Layer2Empty> {
pub fn new_standard(descr: D, network: Network) -> Self {
WalletDescr {
persistence: None,
Expand Down Expand Up @@ -154,7 +154,7 @@ impl<K, D: Descriptor<K>, L2: Layer2Descriptor> CloneNoPersistence for WalletDes
persistence: None,
generator: self.generator.clone(),
network: self.network,
layer2: self.layer2.clone_no_persistence(),
layer2: self.layer2.clone(),
_phantom: PhantomData,
}
}
Expand Down Expand Up @@ -204,8 +204,8 @@ pub struct WalletData<L2: Layer2Data> {
pub txout_annotations: BTreeMap<Outpoint, String>,
pub txin_annotations: BTreeMap<Outpoint, String>,
pub addr_annotations: BTreeMap<Address, String>,
pub layer2_annotations: L2,
pub last_used: BTreeMap<Keychain, NormalIndex>,
pub layer2: L2,
}

impl<L2: Layer2Data> CloneNoPersistence for WalletData<L2> {
Expand All @@ -218,7 +218,7 @@ impl<L2: Layer2Data> CloneNoPersistence for WalletData<L2> {
txout_annotations: self.txout_annotations.clone(),
txin_annotations: self.txin_annotations.clone(),
addr_annotations: self.addr_annotations.clone(),
layer2_annotations: self.layer2_annotations.clone_no_persistence(),
layer2: self.layer2.clone(),
last_used: self.last_used.clone(),
}
}
Expand All @@ -233,7 +233,7 @@ impl<L2: Layer2Data> Persisting for WalletData<L2> {
fn as_mut_persistence(&mut self) -> &mut Option<Persistence<Self>> { &mut self.persistence }
}

impl WalletData<NoLayer2> {
impl WalletData<Layer2Empty> {
pub fn new_layer1() -> Self {
WalletData {
persistence: None,
Expand All @@ -243,7 +243,7 @@ impl WalletData<NoLayer2> {
txout_annotations: empty!(),
txin_annotations: empty!(),
addr_annotations: empty!(),
layer2_annotations: none!(),
layer2: none!(),
last_used: empty!(),
}
}
Expand All @@ -260,7 +260,7 @@ impl<L2: Layer2Data> WalletData<L2> {
txout_annotations: empty!(),
txin_annotations: empty!(),
addr_annotations: empty!(),
layer2_annotations: none!(),
layer2: none!(),
last_used: empty!(),
}
}
Expand Down Expand Up @@ -417,7 +417,7 @@ impl<L2: Layer2Cache> CloneNoPersistence for WalletCache<L2> {
tx: self.tx.clone(),
utxo: self.utxo.clone(),
addr: self.addr.clone(),
layer2: self.layer2.clone_no_persistence(),
layer2: self.layer2.clone(),
}
}
}
Expand Down

0 comments on commit 0405406

Please sign in to comment.