[wallet] Cleanup, remove unnecessary mutable references

This commit is contained in:
Alekos Filini 2020-08-06 10:44:40 +02:00
parent 5683a83288
commit 0954049df0
No known key found for this signature in database
GPG Key ID: 5E8AFC3034FDFA4F
5 changed files with 43 additions and 71 deletions

View File

@ -38,41 +38,41 @@ impl OnlineBlockchain for ElectrumBlockchain {
} }
fn setup<D: BatchDatabase + DatabaseUtils, P: Progress>( fn setup<D: BatchDatabase + DatabaseUtils, P: Progress>(
&mut self, &self,
stop_gap: Option<usize>, stop_gap: Option<usize>,
database: &mut D, database: &mut D,
progress_update: P, progress_update: P,
) -> Result<(), Error> { ) -> Result<(), Error> {
self.0 self.0
.as_mut() .as_ref()
.ok_or(Error::OfflineClient)? .ok_or(Error::OfflineClient)?
.electrum_like_setup(stop_gap, database, progress_update) .electrum_like_setup(stop_gap, database, progress_update)
} }
fn get_tx(&mut self, txid: &Txid) -> Result<Option<Transaction>, Error> { fn get_tx(&self, txid: &Txid) -> Result<Option<Transaction>, Error> {
Ok(self Ok(self
.0 .0
.as_mut() .as_ref()
.ok_or(Error::OfflineClient)? .ok_or(Error::OfflineClient)?
.transaction_get(txid) .transaction_get(txid)
.map(Option::Some)?) .map(Option::Some)?)
} }
fn broadcast(&mut self, tx: &Transaction) -> Result<(), Error> { fn broadcast(&self, tx: &Transaction) -> Result<(), Error> {
Ok(self Ok(self
.0 .0
.as_mut() .as_ref()
.ok_or(Error::OfflineClient)? .ok_or(Error::OfflineClient)?
.transaction_broadcast(tx) .transaction_broadcast(tx)
.map(|_| ())?) .map(|_| ())?)
} }
fn get_height(&mut self) -> Result<usize, Error> { fn get_height(&self) -> Result<usize, Error> {
// TODO: unsubscribe when added to the client, or is there a better call to use here? // TODO: unsubscribe when added to the client, or is there a better call to use here?
Ok(self Ok(self
.0 .0
.as_mut() .as_ref()
.ok_or(Error::OfflineClient)? .ok_or(Error::OfflineClient)?
.block_headers_subscribe() .block_headers_subscribe()
.map(|data| data.height)?) .map(|data| data.height)?)
@ -81,7 +81,7 @@ impl OnlineBlockchain for ElectrumBlockchain {
impl ElectrumLikeSync for Client { impl ElectrumLikeSync for Client {
fn els_batch_script_get_history<'s, I: IntoIterator<Item = &'s Script>>( fn els_batch_script_get_history<'s, I: IntoIterator<Item = &'s Script>>(
&mut self, &self,
scripts: I, scripts: I,
) -> Result<Vec<Vec<ELSGetHistoryRes>>, Error> { ) -> Result<Vec<Vec<ELSGetHistoryRes>>, Error> {
self.batch_script_get_history(scripts) self.batch_script_get_history(scripts)
@ -105,7 +105,7 @@ impl ElectrumLikeSync for Client {
} }
fn els_batch_script_list_unspent<'s, I: IntoIterator<Item = &'s Script>>( fn els_batch_script_list_unspent<'s, I: IntoIterator<Item = &'s Script>>(
&mut self, &self,
scripts: I, scripts: I,
) -> Result<Vec<Vec<ELSListUnspentRes>>, Error> { ) -> Result<Vec<Vec<ELSListUnspentRes>>, Error> {
self.batch_script_list_unspent(scripts) self.batch_script_list_unspent(scripts)
@ -132,7 +132,7 @@ impl ElectrumLikeSync for Client {
.map_err(Error::Electrum) .map_err(Error::Electrum)
} }
fn els_transaction_get(&mut self, txid: &Txid) -> Result<Transaction, Error> { fn els_transaction_get(&self, txid: &Txid) -> Result<Transaction, Error> {
self.transaction_get(txid).map_err(Error::Electrum) self.transaction_get(txid).map_err(Error::Electrum)
} }
} }

View File

@ -64,38 +64,38 @@ impl OnlineBlockchain for EsploraBlockchain {
} }
fn setup<D: BatchDatabase + DatabaseUtils, P: Progress>( fn setup<D: BatchDatabase + DatabaseUtils, P: Progress>(
&mut self, &self,
stop_gap: Option<usize>, stop_gap: Option<usize>,
database: &mut D, database: &mut D,
progress_update: P, progress_update: P,
) -> Result<(), Error> { ) -> Result<(), Error> {
maybe_await!(self maybe_await!(self
.0 .0
.as_mut() .as_ref()
.ok_or(Error::OfflineClient)? .ok_or(Error::OfflineClient)?
.electrum_like_setup(stop_gap, database, progress_update)) .electrum_like_setup(stop_gap, database, progress_update))
} }
fn get_tx(&mut self, txid: &Txid) -> Result<Option<Transaction>, Error> { fn get_tx(&self, txid: &Txid) -> Result<Option<Transaction>, Error> {
Ok(await_or_block!(self Ok(await_or_block!(self
.0 .0
.as_mut() .as_ref()
.ok_or(Error::OfflineClient)? .ok_or(Error::OfflineClient)?
._get_tx(txid))?) ._get_tx(txid))?)
} }
fn broadcast(&mut self, tx: &Transaction) -> Result<(), Error> { fn broadcast(&self, tx: &Transaction) -> Result<(), Error> {
Ok(await_or_block!(self Ok(await_or_block!(self
.0 .0
.as_mut() .as_ref()
.ok_or(Error::OfflineClient)? .ok_or(Error::OfflineClient)?
._broadcast(tx))?) ._broadcast(tx))?)
} }
fn get_height(&mut self) -> Result<usize, Error> { fn get_height(&self) -> Result<usize, Error> {
Ok(await_or_block!(self Ok(await_or_block!(self
.0 .0
.as_mut() .as_ref()
.ok_or(Error::OfflineClient)? .ok_or(Error::OfflineClient)?
._get_height())?) ._get_height())?)
} }
@ -237,7 +237,7 @@ impl UrlClient {
#[maybe_async] #[maybe_async]
impl ElectrumLikeSync for UrlClient { impl ElectrumLikeSync for UrlClient {
fn els_batch_script_get_history<'s, I: IntoIterator<Item = &'s Script>>( fn els_batch_script_get_history<'s, I: IntoIterator<Item = &'s Script>>(
&mut self, &self,
scripts: I, scripts: I,
) -> Result<Vec<Vec<ELSGetHistoryRes>>, Error> { ) -> Result<Vec<Vec<ELSGetHistoryRes>>, Error> {
let future = async { let future = async {
@ -251,7 +251,7 @@ impl ElectrumLikeSync for UrlClient {
} }
fn els_batch_script_list_unspent<'s, I: IntoIterator<Item = &'s Script>>( fn els_batch_script_list_unspent<'s, I: IntoIterator<Item = &'s Script>>(
&mut self, &self,
scripts: I, scripts: I,
) -> Result<Vec<Vec<ELSListUnspentRes>>, Error> { ) -> Result<Vec<Vec<ELSListUnspentRes>>, Error> {
let future = async { let future = async {
@ -264,7 +264,7 @@ impl ElectrumLikeSync for UrlClient {
await_or_block!(future) await_or_block!(future)
} }
fn els_transaction_get(&mut self, txid: &Txid) -> Result<Transaction, Error> { fn els_transaction_get(&self, txid: &Txid) -> Result<Transaction, Error> {
Ok(await_or_block!(self._get_tx(txid))? Ok(await_or_block!(self._get_tx(txid))?
.ok_or_else(|| EsploraError::TransactionNotFound(*txid))?) .ok_or_else(|| EsploraError::TransactionNotFound(*txid))?)
} }

View File

@ -46,13 +46,13 @@ pub trait OnlineBlockchain: Blockchain {
fn get_capabilities(&self) -> HashSet<Capability>; fn get_capabilities(&self) -> HashSet<Capability>;
fn setup<D: BatchDatabase + DatabaseUtils, P: Progress>( fn setup<D: BatchDatabase + DatabaseUtils, P: Progress>(
&mut self, &self,
stop_gap: Option<usize>, stop_gap: Option<usize>,
database: &mut D, database: &mut D,
progress_update: P, progress_update: P,
) -> Result<(), Error>; ) -> Result<(), Error>;
fn sync<D: BatchDatabase + DatabaseUtils, P: Progress>( fn sync<D: BatchDatabase + DatabaseUtils, P: Progress>(
&mut self, &self,
stop_gap: Option<usize>, stop_gap: Option<usize>,
database: &mut D, database: &mut D,
progress_update: P, progress_update: P,
@ -60,10 +60,10 @@ pub trait OnlineBlockchain: Blockchain {
maybe_await!(self.setup(stop_gap, database, progress_update)) maybe_await!(self.setup(stop_gap, database, progress_update))
} }
fn get_tx(&mut self, txid: &Txid) -> Result<Option<Transaction>, Error>; fn get_tx(&self, txid: &Txid) -> Result<Option<Transaction>, Error>;
fn broadcast(&mut self, tx: &Transaction) -> Result<(), Error>; fn broadcast(&self, tx: &Transaction) -> Result<(), Error>;
fn get_height(&mut self) -> Result<usize, Error>; fn get_height(&self) -> Result<usize, Error>;
} }
pub type ProgressData = (f32, Option<String>); pub type ProgressData = (f32, Option<String>);

View File

@ -30,21 +30,21 @@ pub struct ELSListUnspentRes {
#[maybe_async] #[maybe_async]
pub trait ElectrumLikeSync { pub trait ElectrumLikeSync {
fn els_batch_script_get_history<'s, I: IntoIterator<Item = &'s Script>>( fn els_batch_script_get_history<'s, I: IntoIterator<Item = &'s Script>>(
&mut self, &self,
scripts: I, scripts: I,
) -> Result<Vec<Vec<ELSGetHistoryRes>>, Error>; ) -> Result<Vec<Vec<ELSGetHistoryRes>>, Error>;
fn els_batch_script_list_unspent<'s, I: IntoIterator<Item = &'s Script>>( fn els_batch_script_list_unspent<'s, I: IntoIterator<Item = &'s Script>>(
&mut self, &self,
scripts: I, scripts: I,
) -> Result<Vec<Vec<ELSListUnspentRes>>, Error>; ) -> Result<Vec<Vec<ELSListUnspentRes>>, Error>;
fn els_transaction_get(&mut self, txid: &Txid) -> Result<Transaction, Error>; fn els_transaction_get(&self, txid: &Txid) -> Result<Transaction, Error>;
// Provided methods down here... // Provided methods down here...
fn electrum_like_setup<D: BatchDatabase + DatabaseUtils, P: Progress>( fn electrum_like_setup<D: BatchDatabase + DatabaseUtils, P: Progress>(
&mut self, &self,
stop_gap: Option<usize>, stop_gap: Option<usize>,
database: &mut D, database: &mut D,
_progress_update: P, _progress_update: P,
@ -173,7 +173,7 @@ pub trait ElectrumLikeSync {
} }
fn check_tx_and_descendant<D: DatabaseUtils + BatchDatabase>( fn check_tx_and_descendant<D: DatabaseUtils + BatchDatabase>(
&mut self, &self,
database: &mut D, database: &mut D,
txid: &Txid, txid: &Txid,
height: Option<u32>, height: Option<u32>,
@ -268,7 +268,7 @@ pub trait ElectrumLikeSync {
} }
fn check_history<D: DatabaseUtils + BatchDatabase>( fn check_history<D: DatabaseUtils + BatchDatabase>(
&mut self, &self,
database: &mut D, database: &mut D,
script_pubkey: Script, script_pubkey: Script,
txs: Vec<ELSGetHistoryRes>, txs: Vec<ELSGetHistoryRes>,

View File

@ -37,7 +37,7 @@ pub struct Wallet<B: Blockchain, D: BatchDatabase> {
current_height: Option<u32>, current_height: Option<u32>,
client: RefCell<B>, client: B,
database: RefCell<D>, database: RefCell<D>,
} }
@ -82,7 +82,7 @@ where
current_height: None, current_height: None,
client: RefCell::new(B::offline()), client: B::offline(),
database: RefCell::new(database), database: RefCell::new(database),
}) })
} }
@ -714,43 +714,15 @@ where
descriptor: &str, descriptor: &str,
change_descriptor: Option<&str>, change_descriptor: Option<&str>,
network: Network, network: Network,
mut database: D, database: D,
mut client: B, client: B,
) -> Result<Self, Error> { ) -> Result<Self, Error> {
database.check_descriptor_checksum( let mut wallet = Self::new_offline(descriptor, change_descriptor, network, database)?;
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 parsed = ExtendedDescriptor::from_str(desc)?; wallet.current_height = Some(maybe_await!(client.get_height())? as u32);
if !parsed.same_structure(descriptor.as_ref()) { wallet.client = client;
return Err(Error::DifferentDescriptorStructure);
}
Some(parsed) Ok(wallet)
}
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),
})
} }
#[maybe_async] #[maybe_async]
@ -813,7 +785,7 @@ where
self.database.borrow_mut().commit_batch(address_batch)?; self.database.borrow_mut().commit_batch(address_batch)?;
} }
maybe_await!(self.client.borrow_mut().sync( maybe_await!(self.client.sync(
None, None,
self.database.borrow_mut().deref_mut(), self.database.borrow_mut().deref_mut(),
noop_progress(), noop_progress(),
@ -822,7 +794,7 @@ where
#[maybe_async] #[maybe_async]
pub fn broadcast(&self, tx: Transaction) -> Result<Txid, Error> { pub fn broadcast(&self, tx: Transaction) -> Result<Txid, Error> {
maybe_await!(self.client.borrow_mut().broadcast(&tx))?; maybe_await!(self.client.broadcast(&tx))?;
Ok(tx.txid()) Ok(tx.txid())
} }