Skip to content

Commit 806abb4

Browse files
author
bay
committed
Lock wallet for whole operation. Updated tests because of test framework blocking post.
1 parent 4246ccb commit 806abb4

40 files changed

+1430
-628
lines changed

api/src/foreign.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use crate::libwallet::{
2424
use crate::util::secp::key::SecretKey;
2525
use crate::util::Mutex;
2626
use ed25519_dalek::PublicKey as DalekPublicKey;
27+
use libwallet::wallet_lock;
2728
use std::sync::Arc;
2829

2930
/// ForeignAPI Middleware Check callback
@@ -218,9 +219,7 @@ where
218219
/// ```
219220
220221
pub fn get_proof_address(&self) -> Result<String, Error> {
221-
let mut w_lock = self.wallet_inst.lock();
222-
let w = w_lock.lc_provider()?.wallet_inst()?;
223-
222+
wallet_lock!(self.wallet_inst, w);
224223
foreign::get_proof_address(&mut **w, (&self.keychain_mask).as_ref())
225224
}
226225

@@ -275,8 +274,7 @@ where
275274
/// ```
276275
277276
pub fn build_coinbase(&self, block_fees: &BlockFees) -> Result<CbData, Error> {
278-
let mut w_lock = self.wallet_inst.lock();
279-
let w = w_lock.lc_provider()?.wallet_inst()?;
277+
wallet_lock!(self.wallet_inst, w);
280278
if let Some(m) = self.middleware.as_ref() {
281279
m(
282280
ForeignCheckMiddlewareFn::BuildCoinbase,
@@ -332,11 +330,13 @@ where
332330
333331
pub fn verify_slate_messages(&self, slate: &Slate) -> Result<(), Error> {
334332
if let Some(m) = self.middleware.as_ref() {
335-
let mut w_lock = self.wallet_inst.lock();
336-
let w = w_lock.lc_provider()?.wallet_inst()?;
333+
let version_info = {
334+
wallet_lock!(self.wallet_inst, w);
335+
w.w2n_client().get_version_info()
336+
};
337337
m(
338338
ForeignCheckMiddlewareFn::VerifySlateMessages,
339-
w.w2n_client().get_version_info(),
339+
version_info,
340340
Some(slate),
341341
)?;
342342
}
@@ -406,8 +406,7 @@ where
406406
dest_acct_name: &Option<String>,
407407
message: Option<String>,
408408
) -> Result<Slate, Error> {
409-
let mut w_lock = self.wallet_inst.lock();
410-
let w = w_lock.lc_provider()?.wallet_inst()?;
409+
wallet_lock!(self.wallet_inst, w);
411410
if let Some(m) = self.middleware.as_ref() {
412411
m(
413412
ForeignCheckMiddlewareFn::ReceiveTx,
@@ -479,8 +478,7 @@ where
479478
/// ```
480479
481480
pub fn finalize_invoice_tx(&self, slate: &Slate) -> Result<Slate, Error> {
482-
let mut w_lock = self.wallet_inst.lock();
483-
let w = w_lock.lc_provider()?.wallet_inst()?;
481+
wallet_lock!(self.wallet_inst, w);
484482
if let Some(m) = self.middleware.as_ref() {
485483
m(
486484
ForeignCheckMiddlewareFn::FinalizeInvoiceTx,
@@ -520,8 +518,7 @@ where
520518
&self,
521519
encrypted_slate: VersionedSlate,
522520
) -> Result<(Slate, SlatePurpose, Option<DalekPublicKey>), Error> {
523-
let mut w_lock = self.wallet_inst.lock();
524-
let w = w_lock.lc_provider()?.wallet_inst()?;
521+
wallet_lock!(self.wallet_inst, w);
525522
let (slate, content, sender, _receiver) = foreign::decrypt_slate(
526523
&mut **w,
527524
(&self.keychain_mask).as_ref(),
@@ -541,8 +538,7 @@ where
541538
address_index: Option<u32>,
542539
use_test_rng: bool,
543540
) -> Result<VersionedSlate, Error> {
544-
let mut w_lock = self.wallet_inst.lock();
545-
let w = w_lock.lc_provider()?.wallet_inst()?;
541+
wallet_lock!(self.wallet_inst, w);
546542
let vslate = foreign::encrypt_slate(
547543
&mut **w,
548544
(&self.keychain_mask).as_ref(),

api/src/foreign_rpc.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ use crate::{Foreign, ForeignCheckMiddlewareFn};
2424
use easy_jsonrpc_mwc;
2525
use ed25519_dalek::PublicKey as DalekPublicKey;
2626
use libwallet::slatepack::SlatePurpose;
27+
use libwallet::wallet_lock_test;
2728
use mwc_wallet_libwallet::proof::proofaddress::{self, ProvableAddress};
29+
use mwc_wallet_util::mwc_core::core::Transaction;
2830
use mwc_wallet_util::mwc_util::secp::Secp256k1;
31+
use std::ops::DerefMut;
2932

3033
/// Public definition used to generate Foreign jsonrpc api.
3134
/// * When running `mwc-wallet listen` with defaults, the V2 api is available at
@@ -1071,11 +1074,12 @@ pub fn run_doctest_foreign(
10711074
let _ = fs::remove_dir_all(test_dir);
10721075
global::set_local_chain_type(ChainTypes::AutomatedTesting);
10731076

1077+
let tx_pool: Arc<Mutex<Vec<Transaction>>> = Arc::new(Mutex::new(Vec::new()));
10741078
let mut wallet_proxy: WalletProxy<
10751079
DefaultLCProvider<LocalWalletClient, ExtKeychain>,
10761080
LocalWalletClient,
10771081
ExtKeychain,
1078-
> = WalletProxy::new(test_dir.into());
1082+
> = WalletProxy::new(test_dir.into(), tx_pool.clone());
10791083
let chain = wallet_proxy.chain.clone();
10801084

10811085
let rec_phrase_1 = util::ZeroingString::from(
@@ -1175,6 +1179,7 @@ pub fn run_doctest_foreign(
11751179
(&mask1).as_ref(),
11761180
1 as usize,
11771181
false,
1182+
tx_pool.lock().deref_mut(),
11781183
);
11791184
//update local outputs after each block, so transaction IDs stay consistent
11801185
let (wallet_refreshed, _) = api_impl::owner::retrieve_summary_info(
@@ -1227,7 +1232,10 @@ pub fn run_doctest_foreign(
12271232

12281233
api_impl::owner::issue_invoice_tx(&mut **w, (&mask2).as_ref(), &args, true, 1).unwrap()
12291234
};
1230-
api_impl::owner::update_wallet_state(wallet1.clone(), (&mask1).as_ref(), &None).unwrap();
1235+
{
1236+
wallet_lock_test!(wallet1.clone(), w1);
1237+
api_impl::owner::update_wallet_state(&mut **w1, (&mask1).as_ref(), &None).unwrap();
1238+
}
12311239
slate = {
12321240
let mut w_lock = wallet1.lock();
12331241
let w = w_lock.lc_provider().unwrap().wallet_inst().unwrap();
@@ -1273,7 +1281,10 @@ pub fn run_doctest_foreign(
12731281
}
12741282
}
12751283

1276-
api_impl::owner::update_wallet_state(wallet1.clone(), (&mask1).as_ref(), &None).unwrap();
1284+
{
1285+
wallet_lock_test!(wallet1.clone(), w1);
1286+
api_impl::owner::update_wallet_state(&mut **w1, (&mask1).as_ref(), &None).unwrap();
1287+
}
12771288
if init_tx {
12781289
let amount = 2_000_000_000;
12791290
let mut w_lock = wallet1.lock();

api/src/owner.rs

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use crate::libwallet::{
4242
use crate::util::logger::LoggingConfig;
4343
use crate::util::secp::key::SecretKey;
4444
use crate::util::{from_hex, Mutex, ZeroingString};
45-
use libwallet::{OwnershipProof, OwnershipProofValidation, RetrieveTxQueryArgs};
45+
use libwallet::{wallet_lock, OwnershipProof, OwnershipProofValidation, RetrieveTxQueryArgs};
4646
use mwc_wallet_util::mwc_util::secp::key::PublicKey;
4747
use mwc_wallet_util::mwc_util::static_secp_instance;
4848
use std::sync::atomic::{AtomicBool, Ordering};
@@ -295,8 +295,7 @@ where
295295
&self,
296296
keychain_mask: Option<&SecretKey>,
297297
) -> Result<Vec<AcctPathMapping>, Error> {
298-
let mut w_lock = self.wallet_inst.lock();
299-
let w = w_lock.lc_provider()?.wallet_inst()?;
298+
wallet_lock!(self.wallet_inst, w);
300299
// Test keychain mask, to keep API consistent
301300
let _ = w.keychain(keychain_mask)?;
302301
owner::accounts(&mut **w)
@@ -347,8 +346,7 @@ where
347346
keychain_mask: Option<&SecretKey>,
348347
label: &str,
349348
) -> Result<Identifier, Error> {
350-
let mut w_lock = self.wallet_inst.lock();
351-
let w = w_lock.lc_provider()?.wallet_inst()?;
349+
wallet_lock!(self.wallet_inst, w);
352350
owner::create_account_path(&mut **w, keychain_mask, label)
353351
}
354352

@@ -395,8 +393,7 @@ where
395393
keychain_mask: Option<&SecretKey>,
396394
label: &str,
397395
) -> Result<(), Error> {
398-
let mut w_lock = self.wallet_inst.lock();
399-
let w = w_lock.lc_provider()?.wallet_inst()?;
396+
wallet_lock!(self.wallet_inst, w);
400397
// Test keychain mask, to keep API consistent
401398
let _ = w.keychain(keychain_mask)?;
402399
owner::set_active_account(&mut **w, label)
@@ -708,7 +705,8 @@ where
708705
) -> Result<Slate, Error> {
709706
let address = args.address.clone();
710707

711-
owner::update_wallet_state(self.wallet_inst.clone(), keychain_mask, &None)?;
708+
wallet_lock!(self.wallet_inst, w);
709+
owner::update_wallet_state(&mut **w, keychain_mask, &None)?;
712710
let send_args = args.send_args.clone();
713711
//minimum_confirmations cannot be zero.
714712
let minimum_confirmations = args.minimum_confirmations.clone();
@@ -768,11 +766,8 @@ where
768766
None
769767
};
770768

771-
let mut slate = {
772-
let mut w_lock = self.wallet_inst.lock();
773-
let w = w_lock.lc_provider()?.wallet_inst()?;
774-
owner::init_send_tx(&mut **w, keychain_mask, &args, self.doctest_mode, routputs)?
775-
};
769+
let mut slate =
770+
{ owner::init_send_tx(&mut **w, keychain_mask, &args, self.doctest_mode, routputs)? };
776771

777772
match send_args {
778773
Some(sa) => {
@@ -781,8 +776,6 @@ where
781776
match sender_info {
782777
Some((sender, other_wallet_info)) => {
783778
let (slatepack_secret, height, secp) = {
784-
let mut w_lock = self.wallet_inst.lock();
785-
let w = w_lock.lc_provider()?.wallet_inst()?;
786779
let keychain = w.keychain(keychain_mask)?;
787780
let (height, _, _) = w.w2n_client().get_chain_tip()?;
788781
let slatepack_secret =
@@ -893,8 +886,7 @@ where
893886
keychain_mask: Option<&SecretKey>,
894887
args: &IssueInvoiceTxArgs,
895888
) -> Result<Slate, Error> {
896-
let mut w_lock = self.wallet_inst.lock();
897-
let w = w_lock.lc_provider()?.wallet_inst()?;
889+
wallet_lock!(self.wallet_inst, w);
898890
owner::issue_invoice_tx(&mut **w, keychain_mask, args, self.doctest_mode, 1)
899891
}
900892

@@ -959,7 +951,9 @@ where
959951
slate: &Slate,
960952
args: &InitTxArgs,
961953
) -> Result<Slate, Error> {
962-
owner::update_wallet_state(self.wallet_inst.clone(), keychain_mask, &None)?;
954+
wallet_lock!(self.wallet_inst, w);
955+
956+
owner::update_wallet_state(&mut **w, keychain_mask, &None)?;
963957

964958
//minimum_confirmations cannot be zero.
965959
let minimum_confirmations = args.minimum_confirmations.clone();
@@ -968,8 +962,6 @@ where
968962
"minimum_confirmations can not smaller than 1".to_owned(),
969963
));
970964
}
971-
let mut w_lock = self.wallet_inst.lock();
972-
let w = w_lock.lc_provider()?.wallet_inst()?;
973965
owner::process_invoice_tx(
974966
&mut **w,
975967
keychain_mask,
@@ -1043,8 +1035,7 @@ where
10431035
address: Option<String>,
10441036
participant_id: usize,
10451037
) -> Result<(), Error> {
1046-
let mut w_lock = self.wallet_inst.lock();
1047-
let w = w_lock.lc_provider()?.wallet_inst()?;
1038+
wallet_lock!(self.wallet_inst, w);
10481039
owner::tx_lock_outputs(
10491040
&mut **w,
10501041
keychain_mask,
@@ -1120,8 +1111,7 @@ where
11201111
keychain_mask: Option<&SecretKey>,
11211112
slate: &Slate,
11221113
) -> Result<Slate, Error> {
1123-
let mut w_lock = self.wallet_inst.lock();
1124-
let w = w_lock.lc_provider()?.wallet_inst()?;
1114+
wallet_lock!(self.wallet_inst, w);
11251115
let (slate_res, _context) =
11261116
owner::finalize_tx(&mut **w, keychain_mask, slate, true, self.doctest_mode)?;
11271117

@@ -1187,8 +1177,7 @@ where
11871177
fluff: bool,
11881178
) -> Result<(), Error> {
11891179
let client = {
1190-
let mut w_lock = self.wallet_inst.lock();
1191-
let w = w_lock.lc_provider()?.wallet_inst()?;
1180+
wallet_lock!(self.wallet_inst, w);
11921181
// Test keychain mask, to keep API consistent
11931182
let _ = w.keychain(keychain_mask)?;
11941183
w.w2n_client().clone()
@@ -1310,17 +1299,15 @@ where
13101299
keychain_mask: Option<&SecretKey>,
13111300
tx_log_entry: &TxLogEntry,
13121301
) -> Result<Option<Transaction>, Error> {
1313-
let mut w_lock = self.wallet_inst.lock();
1314-
let w = w_lock.lc_provider()?.wallet_inst()?;
1302+
wallet_lock!(self.wallet_inst, w);
13151303
// Test keychain mask, to keep API consistent
13161304
let _ = w.keychain(keychain_mask)?;
13171305
owner::get_stored_tx(&**w, tx_log_entry)
13181306
}
13191307

13201308
/// Loads a stored transaction from a file
13211309
pub fn load_stored_tx(&self, file: &String) -> Result<Transaction, Error> {
1322-
let mut w_lock = self.wallet_inst.lock();
1323-
let w = w_lock.lc_provider()?.wallet_inst()?;
1310+
wallet_lock!(self.wallet_inst, w);
13241311
owner::load_stored_tx(&**w, file)
13251312
}
13261313

@@ -1381,8 +1368,7 @@ where
13811368
slate: &Slate,
13821369
) -> Result<(), Error> {
13831370
{
1384-
let mut w_lock = self.wallet_inst.lock();
1385-
let w = w_lock.lc_provider()?.wallet_inst()?;
1371+
wallet_lock!(self.wallet_inst, w);
13861372
// Test keychain mask, to keep API consistent
13871373
let _ = w.keychain(keychain_mask)?;
13881374
}
@@ -1596,8 +1582,7 @@ where
15961582
keychain_mask: Option<&SecretKey>,
15971583
) -> Result<NodeHeightResult, Error> {
15981584
{
1599-
let mut w_lock = self.wallet_inst.lock();
1600-
let w = w_lock.lc_provider()?.wallet_inst()?;
1585+
wallet_lock!(self.wallet_inst, w);
16011586
// Test keychain mask, to keep API consistent
16021587
let _ = w.keychain(keychain_mask)?;
16031588
}
@@ -2473,7 +2458,10 @@ where
24732458
params: &SwapStartArgs,
24742459
) -> Result<String, Error> {
24752460
// Updating wallet state first because we need to select outputs.
2476-
owner::update_wallet_state(self.wallet_inst.clone(), keychain_mask, &None)?;
2461+
{
2462+
wallet_lock!(self.wallet_inst, w);
2463+
owner::update_wallet_state(&mut **w, keychain_mask, &None)?;
2464+
}
24772465
owner_swap::swap_start(self.wallet_inst.clone(), keychain_mask, params)
24782466
.map_err(|e| e.into())
24792467
}
@@ -2720,8 +2708,7 @@ where
27202708
),
27212709
Error,
27222710
> {
2723-
let mut w_lock = self.wallet_inst.lock();
2724-
let w = w_lock.lc_provider()?.wallet_inst()?;
2711+
wallet_lock!(self.wallet_inst, w);
27252712
foreign::decrypt_slate(&mut **w, keychain_mask, encrypted_slate, address_index)
27262713
}
27272714

@@ -2736,8 +2723,7 @@ where
27362723
address_index: Option<u32>,
27372724
use_test_rng: bool,
27382725
) -> Result<VersionedSlate, Error> {
2739-
let mut w_lock = self.wallet_inst.lock();
2740-
let w = w_lock.lc_provider()?.wallet_inst()?;
2726+
wallet_lock!(self.wallet_inst, w);
27412727
foreign::encrypt_slate(
27422728
&mut **w,
27432729
keychain_mask,
@@ -2757,8 +2743,7 @@ where
27572743
features: OutputFeatures,
27582744
amount: u64,
27592745
) -> Result<BuiltOutput, Error> {
2760-
let mut w_lock = self.wallet_inst.lock();
2761-
let w = w_lock.lc_provider()?.wallet_inst()?;
2746+
wallet_lock!(self.wallet_inst, w);
27622747
owner::build_output(&mut **w, keychain_mask, features, amount)
27632748
}
27642749
}

0 commit comments

Comments
 (0)