Skip to content

Commit 9560382

Browse files
author
bay
committed
Tests updates. Monor fixes for dump
1 parent 806abb4 commit 9560382

File tree

6 files changed

+142
-99
lines changed

6 files changed

+142
-99
lines changed

controller/tests/check.rs

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ macro_rules! wallet_info {
5454
}
5555

5656
/// Various tests on checking functionality
57-
fn scan_impl(test_dir: &str) -> Result<(), wallet::Error> {
57+
fn scan_impl(test_dir: &str) {
5858
global::set_local_chain_type(global::ChainTypes::AutomatedTesting);
5959
// Create a new proxy to simulate server and wallet responses
6060
let tx_pool: Arc<Mutex<Vec<Transaction>>> = Arc::new(Mutex::new(Vec::new()));
@@ -107,14 +107,16 @@ fn scan_impl(test_dir: &str) -> Result<(), wallet::Error> {
107107
api.create_account_path(m, "account_3")?;
108108
api.set_active_account(m, "named_account_1")?;
109109
Ok(())
110-
})?;
110+
})
111+
.unwrap();
111112

112113
// add account to wallet 2
113114
wallet::controller::owner_single_use(Some(wallet2.clone()), mask2, None, |api, m| {
114115
api.create_account_path(m, "account_1")?;
115116
api.set_active_account(m, "account_1")?;
116117
Ok(())
117-
})?;
118+
})
119+
.unwrap();
118120

119121
// Do some mining
120122
let bh = 20u64;
@@ -140,44 +142,48 @@ fn scan_impl(test_dir: &str) -> Result<(), wallet::Error> {
140142
assert_eq!(wallet1_info.total, c);
141143
assert_eq!(txs.len(), bh as usize);
142144
Ok(())
143-
})?;
145+
})
146+
.unwrap();
144147

145148
// Accidentally delete some outputs
146149
let mut w1_outputs_commits = vec![];
147150
wallet::controller::owner_single_use(Some(wallet1.clone()), mask1, None, |api, m| {
148151
w1_outputs_commits = api.retrieve_outputs(m, false, true, None)?.1;
149152
Ok(())
150-
})?;
153+
})
154+
.unwrap();
151155
let w1_outputs: Vec<libwallet::OutputData> =
152156
w1_outputs_commits.into_iter().map(|m| m.output).collect();
153157
{
154-
wallet_inst!(wallet1, w);
158+
wallet_inst_test!(wallet1, w);
155159
{
156-
let mut batch = w.batch(mask1)?;
157-
batch.delete(&w1_outputs[4].key_id, &None)?;
158-
batch.delete(&w1_outputs[10].key_id, &None)?;
160+
let mut batch = w.batch(mask1).unwrap();
161+
batch.delete(&w1_outputs[4].key_id, &None).unwrap();
162+
batch.delete(&w1_outputs[10].key_id, &None).unwrap();
159163
let mut accidental_spent = w1_outputs[13].clone();
160164
accidental_spent.status = libwallet::OutputStatus::Spent;
161-
batch.save(accidental_spent)?;
162-
batch.commit()?;
165+
batch.save(accidental_spent).unwrap();
166+
batch.commit().unwrap();
163167
}
164168
}
165169

166170
// check we have a problem now
167171
wallet::controller::owner_single_use(Some(wallet1.clone()), mask1, None, |api, m| {
168172
// For MWC we don't 'refresh_from_node'. Otherwise issue will be corrected
169-
let (_, wallet1_info) = api.retrieve_summary_info(m, false, 1)?;
173+
let (_, wallet1_info) = api.retrieve_summary_info(m, false, 1).unwrap();
170174
let (_, txs) = api.retrieve_txs(m, false, None, None, None, None)?;
171175
let (c, _) = libwallet::TxLogEntry::sum_confirmed(&txs);
172176
assert!(wallet1_info.total != c);
173177
Ok(())
174-
})?;
178+
})
179+
.unwrap();
175180

176181
// this should restore our missing outputs
177182
wallet::controller::owner_single_use(Some(wallet1.clone()), mask1, None, |api, m| {
178183
api.scan(m, None, true)?;
179184
Ok(())
180-
})?;
185+
})
186+
.unwrap();
181187

182188
// check our outputs match again
183189
wallet::controller::owner_single_use(Some(wallet1.clone()), mask1, None, |api, m| {
@@ -190,7 +196,8 @@ fn scan_impl(test_dir: &str) -> Result<(), wallet::Error> {
190196
assert!(api.set_active_account(m, "account_1").is_err());
191197
assert!(api.set_active_account(m, "named_account_1").is_ok());
192198
Ok(())
193-
})?;
199+
})
200+
.unwrap();
194201

195202
// perform a transaction, but don't let it finish
196203
wallet::controller::owner_single_use(Some(wallet1.clone()), mask1, None, |api, m| {
@@ -211,7 +218,8 @@ fn scan_impl(test_dir: &str) -> Result<(), wallet::Error> {
211218
PathToSlatePutter::build_plain(Some(send_file.into())).put_tx(&slate, None, true, &secp)?;
212219
api.tx_lock_outputs(m, &slate, None, 0)?;
213220
Ok(())
214-
})?;
221+
})
222+
.unwrap();
215223

216224
// check we're all locked
217225
wallet::controller::owner_single_use(Some(wallet1.clone()), mask1, None, |api, m| {
@@ -220,25 +228,27 @@ fn scan_impl(test_dir: &str) -> Result<(), wallet::Error> {
220228
assert!(!wallet1_refreshed); // mwc implementation must be without refresh to see an issue. Refresh will fix everything
221229
assert!(wallet1_info.amount_currently_spendable == 0);
222230
Ok(())
223-
})?;
231+
})
232+
.unwrap();
224233

225234
// unlock/restore
226235
wallet::controller::owner_single_use(Some(wallet1.clone()), mask1, None, |api, m| {
227236
api.scan(m, None, true)?;
228237
Ok(())
229-
})?;
238+
})
239+
.unwrap();
230240

231241
// check spendable amount again
232242
wallet::controller::owner_single_use(Some(wallet1.clone()), mask1, None, |api, m| {
233243
let (_, wallet1_info) = api.retrieve_summary_info(m, true, 1)?;
234244
assert_eq!(wallet1_info.amount_currently_spendable, (bh - cm) * reward);
235245
Ok(())
236-
})?;
246+
})
247+
.unwrap();
237248

238249
// let logging finish
239250
stopper.store(false, Ordering::Relaxed);
240251
thread::sleep(Duration::from_millis(200));
241-
Ok(())
242252
}
243253

244254
fn two_wallets_one_seed_impl(test_dir: &str) {
@@ -1117,9 +1127,7 @@ fn output_scanning_impl(test_dir: &str) -> Result<(), wallet::Error> {
11171127
fn scan() {
11181128
let test_dir = "test_output/scan";
11191129
setup(test_dir);
1120-
if let Err(e) = scan_impl(test_dir) {
1121-
panic!("Libwallet Error: {}", e);
1122-
}
1130+
scan_impl(test_dir);
11231131
clean_output_dir(test_dir);
11241132
}
11251133

controller/tests/common/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ macro_rules! wallet_inst {
4040
};
4141
}
4242

43+
#[macro_export]
44+
macro_rules! wallet_inst_test {
45+
($wallet:ident, $w: ident) => {
46+
let mut w_lock = $wallet.lock();
47+
let lc = w_lock.lc_provider().unwrap();
48+
let $w = lc.wallet_inst().unwrap();
49+
};
50+
}
51+
4352
#[macro_export]
4453
macro_rules! create_wallet_and_add {
4554
($client:ident, $wallet: ident, $mask: ident, $test_dir: expr, $name: expr, $seed_phrase: expr, $proxy: expr, $create_mask: expr) => {

impls/src/backends/lmdb.rs

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -677,13 +677,11 @@ where
677677

678678
fn save(&mut self, out: OutputData) -> Result<(), Error> {
679679
// Save the output data to the db.
680-
{
681-
let key = match out.mmr_index {
682-
Some(i) => to_key_u64(OUTPUT_PREFIX, &mut out.key_id.to_bytes().to_vec(), i),
683-
None => to_key(OUTPUT_PREFIX, &mut out.key_id.to_bytes().to_vec()),
684-
};
685-
self.db.borrow().as_ref().unwrap().put_ser(&key, &out)?;
686-
}
680+
let key = match out.mmr_index {
681+
Some(i) => to_key_u64(OUTPUT_PREFIX, &mut out.key_id.to_bytes().to_vec(), i),
682+
None => to_key(OUTPUT_PREFIX, &mut out.key_id.to_bytes().to_vec()),
683+
};
684+
self.db.borrow().as_ref().unwrap().put_ser(&key, &out)?;
687685

688686
Ok(())
689687
}
@@ -704,48 +702,36 @@ where
704702
self.iter_impl(OUTPUT_PREFIX)
705703
}
706704

707-
fn archive_output(&mut self, output_key_id: &Identifier) -> Result<(), Error> {
708-
let key = to_key(OUTPUT_PREFIX, output_key_id.to_bytes().to_vec());
705+
fn archive_output(&mut self, out: &OutputData) -> Result<(), Error> {
706+
let mut key = match out.mmr_index {
707+
Some(i) => to_key_u64(OUTPUT_PREFIX, out.key_id.to_bytes().to_vec(), i),
708+
None => to_key(OUTPUT_PREFIX, out.key_id.to_bytes().to_vec()),
709+
};
709710

710711
let db = self.db.borrow();
711712
let db = db.as_ref().unwrap();
712-
let protocol_version = db.protocol_version();
713-
let outputs: Vec<(Vec<u8>, OutputData)> = db
714-
.iter(&key, move |key, mut v| {
715-
Ok((
716-
key.to_vec(),
717-
ser::deserialize(
718-
&mut v,
719-
protocol_version,
720-
ser::DeserializationMode::default(),
721-
)?,
722-
))
723-
})?
724-
.collect();
725713

726-
for (key, output) in &outputs {
727-
if output.key_id == *output_key_id {
728-
// delete current copy, data can be duplicated, so delete can fail
729-
let _ = db.delete(&key);
730-
// moving it into the archive
731-
let mut key = key.clone();
732-
key[0] = OUTPUT_ARCHIVE_PREFIX;
733-
db.put_ser(&key, output)?;
734-
}
735-
}
714+
db.delete(&key)?;
715+
716+
key[0] = OUTPUT_ARCHIVE_PREFIX;
717+
db.put_ser(&key, &out)?;
736718

737719
Ok(())
738720
}
739721

740722
fn delete(&mut self, id: &Identifier, mmr_index: &Option<u64>) -> Result<(), Error> {
741723
// Delete the output data.
742-
{
743-
let key = match mmr_index {
744-
Some(i) => to_key_u64(OUTPUT_PREFIX, &mut id.to_bytes().to_vec(), *i),
745-
None => to_key(OUTPUT_PREFIX, &mut id.to_bytes().to_vec()),
746-
};
747-
let _ = self.db.borrow().as_ref().unwrap().delete(&key);
748-
}
724+
let key = match mmr_index {
725+
Some(i) => to_key_u64(OUTPUT_PREFIX, &mut id.to_bytes().to_vec(), *i),
726+
None => to_key(OUTPUT_PREFIX, &mut id.to_bytes().to_vec()),
727+
};
728+
let _ = self.db.borrow().as_ref().unwrap().delete(&key);
729+
730+
let key = match mmr_index {
731+
Some(i) => to_key_u64(OUTPUT_ARCHIVE_PREFIX, &mut id.to_bytes().to_vec(), *i),
732+
None => to_key(OUTPUT_ARCHIVE_PREFIX, &mut id.to_bytes().to_vec()),
733+
};
734+
let _ = self.db.borrow().as_ref().unwrap().delete(&key);
749735

750736
Ok(())
751737
}
@@ -957,8 +943,20 @@ where
957943
&mut parent_id.to_bytes().to_vec(),
958944
tx_id as u64,
959945
);
960-
self.db.borrow().as_ref().unwrap().delete(&tx_log_key)?;
961-
Ok(())
946+
let res1 = self.db.borrow().as_ref().unwrap().delete(&tx_log_key);
947+
948+
let tx_log_key = to_key_u64(
949+
TX_ARCHIVE_LOG_ENTRY_PREFIX,
950+
&mut parent_id.to_bytes().to_vec(),
951+
tx_id as u64,
952+
);
953+
let res2 = self.db.borrow().as_ref().unwrap().delete(&tx_log_key);
954+
955+
if res1.is_ok() || res2.is_ok() {
956+
Ok(())
957+
} else {
958+
res1.map_err(|e| e.into())
959+
}
962960
}
963961

964962
fn rename_acct_path(

libwallet/src/api_impl/owner.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,13 +1286,13 @@ where
12861286
match w.get_stored_tx_by_uuid(&uuid_str, false) {
12871287
Ok(t) => {
12881288
write_info(
1289-
format!(" Slate for {}: {:?}", uuid_str, t),
1289+
format!("tx for {}: {:?} Transaction: {:?}", uuid_str, tx_log, t),
12901290
file.as_mut(),
12911291
status_send_channel,
12921292
);
12931293
}
12941294
Err(_) => write_info(
1295-
format!(" Slate for {} not found", uuid_str),
1295+
format!("tx for {}: {:?} Slate not found", uuid_str, tx_log),
12961296
file.as_mut(),
12971297
status_send_channel,
12981298
),
@@ -1301,24 +1301,35 @@ where
13011301
}
13021302

13031303
for tx_log in w.tx_log_archive_iter() {
1304-
write_info(format!("{:?}", tx_log), file.as_mut(), status_send_channel);
13051304
// Checking if Slate is available
13061305
if let Some(uuid) = tx_log.tx_slate_id {
13071306
let uuid_str = uuid.to_string();
13081307
match w.get_stored_tx_by_uuid(&uuid_str, true) {
13091308
Ok(t) => {
13101309
write_info(
1311-
format!(" Archived Slate for {}: {:?}", uuid_str, t),
1310+
format!(
1311+
"Archived tx for {}: {:?} Transaction: {:?}",
1312+
uuid_str, tx_log, t
1313+
),
13121314
file.as_mut(),
13131315
status_send_channel,
13141316
);
13151317
}
13161318
Err(_) => write_info(
1317-
format!(" Archived Slate for {} not found", uuid_str),
1319+
format!(
1320+
"Archived tx for {}: {:?} Slate not found",
1321+
uuid_str, tx_log
1322+
),
13181323
file.as_mut(),
13191324
status_send_channel,
13201325
),
13211326
}
1327+
} else {
1328+
write_info(
1329+
format!("Archived tx {:?}", tx_log),
1330+
file.as_mut(),
1331+
status_send_channel,
1332+
);
13221333
}
13231334
}
13241335

0 commit comments

Comments
 (0)