From 0954049df09a8634243a902fb214ef13517db83e Mon Sep 17 00:00:00 2001 From: Alekos Filini Date: Thu, 6 Aug 2020 10:44:40 +0200 Subject: [PATCH] [wallet] Cleanup, remove unnecessary mutable references --- src/blockchain/electrum.rs | 22 ++++++++--------- src/blockchain/esplora.rs | 22 ++++++++--------- src/blockchain/mod.rs | 10 ++++---- src/blockchain/utils.rs | 12 +++++----- src/wallet/mod.rs | 48 ++++++++------------------------------ 5 files changed, 43 insertions(+), 71 deletions(-) diff --git a/src/blockchain/electrum.rs b/src/blockchain/electrum.rs index 8225f554..d0bc9710 100644 --- a/src/blockchain/electrum.rs +++ b/src/blockchain/electrum.rs @@ -38,41 +38,41 @@ impl OnlineBlockchain for ElectrumBlockchain { } fn setup( - &mut self, + &self, stop_gap: Option, database: &mut D, progress_update: P, ) -> Result<(), Error> { self.0 - .as_mut() + .as_ref() .ok_or(Error::OfflineClient)? .electrum_like_setup(stop_gap, database, progress_update) } - fn get_tx(&mut self, txid: &Txid) -> Result, Error> { + fn get_tx(&self, txid: &Txid) -> Result, Error> { Ok(self .0 - .as_mut() + .as_ref() .ok_or(Error::OfflineClient)? .transaction_get(txid) .map(Option::Some)?) } - fn broadcast(&mut self, tx: &Transaction) -> Result<(), Error> { + fn broadcast(&self, tx: &Transaction) -> Result<(), Error> { Ok(self .0 - .as_mut() + .as_ref() .ok_or(Error::OfflineClient)? .transaction_broadcast(tx) .map(|_| ())?) } - fn get_height(&mut self) -> Result { + fn get_height(&self) -> Result { // TODO: unsubscribe when added to the client, or is there a better call to use here? Ok(self .0 - .as_mut() + .as_ref() .ok_or(Error::OfflineClient)? .block_headers_subscribe() .map(|data| data.height)?) @@ -81,7 +81,7 @@ impl OnlineBlockchain for ElectrumBlockchain { impl ElectrumLikeSync for Client { fn els_batch_script_get_history<'s, I: IntoIterator>( - &mut self, + &self, scripts: I, ) -> Result>, Error> { self.batch_script_get_history(scripts) @@ -105,7 +105,7 @@ impl ElectrumLikeSync for Client { } fn els_batch_script_list_unspent<'s, I: IntoIterator>( - &mut self, + &self, scripts: I, ) -> Result>, Error> { self.batch_script_list_unspent(scripts) @@ -132,7 +132,7 @@ impl ElectrumLikeSync for Client { .map_err(Error::Electrum) } - fn els_transaction_get(&mut self, txid: &Txid) -> Result { + fn els_transaction_get(&self, txid: &Txid) -> Result { self.transaction_get(txid).map_err(Error::Electrum) } } diff --git a/src/blockchain/esplora.rs b/src/blockchain/esplora.rs index 6ba6a596..f49fdcfd 100644 --- a/src/blockchain/esplora.rs +++ b/src/blockchain/esplora.rs @@ -64,38 +64,38 @@ impl OnlineBlockchain for EsploraBlockchain { } fn setup( - &mut self, + &self, stop_gap: Option, database: &mut D, progress_update: P, ) -> Result<(), Error> { maybe_await!(self .0 - .as_mut() + .as_ref() .ok_or(Error::OfflineClient)? .electrum_like_setup(stop_gap, database, progress_update)) } - fn get_tx(&mut self, txid: &Txid) -> Result, Error> { + fn get_tx(&self, txid: &Txid) -> Result, Error> { Ok(await_or_block!(self .0 - .as_mut() + .as_ref() .ok_or(Error::OfflineClient)? ._get_tx(txid))?) } - fn broadcast(&mut self, tx: &Transaction) -> Result<(), Error> { + fn broadcast(&self, tx: &Transaction) -> Result<(), Error> { Ok(await_or_block!(self .0 - .as_mut() + .as_ref() .ok_or(Error::OfflineClient)? ._broadcast(tx))?) } - fn get_height(&mut self) -> Result { + fn get_height(&self) -> Result { Ok(await_or_block!(self .0 - .as_mut() + .as_ref() .ok_or(Error::OfflineClient)? ._get_height())?) } @@ -237,7 +237,7 @@ impl UrlClient { #[maybe_async] impl ElectrumLikeSync for UrlClient { fn els_batch_script_get_history<'s, I: IntoIterator>( - &mut self, + &self, scripts: I, ) -> Result>, Error> { let future = async { @@ -251,7 +251,7 @@ impl ElectrumLikeSync for UrlClient { } fn els_batch_script_list_unspent<'s, I: IntoIterator>( - &mut self, + &self, scripts: I, ) -> Result>, Error> { let future = async { @@ -264,7 +264,7 @@ impl ElectrumLikeSync for UrlClient { await_or_block!(future) } - fn els_transaction_get(&mut self, txid: &Txid) -> Result { + fn els_transaction_get(&self, txid: &Txid) -> Result { Ok(await_or_block!(self._get_tx(txid))? .ok_or_else(|| EsploraError::TransactionNotFound(*txid))?) } diff --git a/src/blockchain/mod.rs b/src/blockchain/mod.rs index 2a250515..2c7081ac 100644 --- a/src/blockchain/mod.rs +++ b/src/blockchain/mod.rs @@ -46,13 +46,13 @@ pub trait OnlineBlockchain: Blockchain { fn get_capabilities(&self) -> HashSet; fn setup( - &mut self, + &self, stop_gap: Option, database: &mut D, progress_update: P, ) -> Result<(), Error>; fn sync( - &mut self, + &self, stop_gap: Option, database: &mut D, progress_update: P, @@ -60,10 +60,10 @@ pub trait OnlineBlockchain: Blockchain { maybe_await!(self.setup(stop_gap, database, progress_update)) } - fn get_tx(&mut self, txid: &Txid) -> Result, Error>; - fn broadcast(&mut self, tx: &Transaction) -> Result<(), Error>; + fn get_tx(&self, txid: &Txid) -> Result, Error>; + fn broadcast(&self, tx: &Transaction) -> Result<(), Error>; - fn get_height(&mut self) -> Result; + fn get_height(&self) -> Result; } pub type ProgressData = (f32, Option); diff --git a/src/blockchain/utils.rs b/src/blockchain/utils.rs index 76bc5e70..f45ec037 100644 --- a/src/blockchain/utils.rs +++ b/src/blockchain/utils.rs @@ -30,21 +30,21 @@ pub struct ELSListUnspentRes { #[maybe_async] pub trait ElectrumLikeSync { fn els_batch_script_get_history<'s, I: IntoIterator>( - &mut self, + &self, scripts: I, ) -> Result>, Error>; fn els_batch_script_list_unspent<'s, I: IntoIterator>( - &mut self, + &self, scripts: I, ) -> Result>, Error>; - fn els_transaction_get(&mut self, txid: &Txid) -> Result; + fn els_transaction_get(&self, txid: &Txid) -> Result; // Provided methods down here... fn electrum_like_setup( - &mut self, + &self, stop_gap: Option, database: &mut D, _progress_update: P, @@ -173,7 +173,7 @@ pub trait ElectrumLikeSync { } fn check_tx_and_descendant( - &mut self, + &self, database: &mut D, txid: &Txid, height: Option, @@ -268,7 +268,7 @@ pub trait ElectrumLikeSync { } fn check_history( - &mut self, + &self, database: &mut D, script_pubkey: Script, txs: Vec, diff --git a/src/wallet/mod.rs b/src/wallet/mod.rs index c3c92d36..018c215f 100644 --- a/src/wallet/mod.rs +++ b/src/wallet/mod.rs @@ -37,7 +37,7 @@ pub struct Wallet { current_height: Option, - client: RefCell, + client: B, database: RefCell, } @@ -82,7 +82,7 @@ where current_height: None, - client: RefCell::new(B::offline()), + client: B::offline(), database: RefCell::new(database), }) } @@ -714,43 +714,15 @@ where descriptor: &str, change_descriptor: Option<&str>, network: Network, - mut database: D, - mut client: B, + database: D, + client: B, ) -> Result { - database.check_descriptor_checksum( - ScriptType::External, - get_checksum(descriptor)?.as_bytes(), - )?; - let descriptor = ExtendedDescriptor::from_str(descriptor)?; - let change_descriptor = match change_descriptor { - Some(desc) => { - database.check_descriptor_checksum( - ScriptType::Internal, - get_checksum(desc)?.as_bytes(), - )?; + let mut wallet = Self::new_offline(descriptor, change_descriptor, network, database)?; - let parsed = ExtendedDescriptor::from_str(desc)?; - if !parsed.same_structure(descriptor.as_ref()) { - return Err(Error::DifferentDescriptorStructure); - } + wallet.current_height = Some(maybe_await!(client.get_height())? as u32); + wallet.client = client; - Some(parsed) - } - None => None, - }; - - let current_height = Some(maybe_await!(client.get_height())? as u32); - - Ok(Wallet { - descriptor, - change_descriptor, - network, - - current_height, - - client: RefCell::new(client), - database: RefCell::new(database), - }) + Ok(wallet) } #[maybe_async] @@ -813,7 +785,7 @@ where self.database.borrow_mut().commit_batch(address_batch)?; } - maybe_await!(self.client.borrow_mut().sync( + maybe_await!(self.client.sync( None, self.database.borrow_mut().deref_mut(), noop_progress(), @@ -822,7 +794,7 @@ where #[maybe_async] pub fn broadcast(&self, tx: Transaction) -> Result { - maybe_await!(self.client.borrow_mut().broadcast(&tx))?; + maybe_await!(self.client.broadcast(&tx))?; Ok(tx.txid()) }