Skip to content

Commit 66b5d7f

Browse files
author
Vyacheslav
authored
Merge pull request hyperledger-indy#787 from KitHat/master
Add negative tests for libnullpay
2 parents a262600 + c5abe05 commit 66b5d7f

File tree

8 files changed

+315
-172
lines changed

8 files changed

+315
-172
lines changed

libnullpay/src/payment_method.rs

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,25 @@ pub mod add_request_fees {
6767
let total_payments = _count_total_payments(&outputs_json);
6868

6969
let err = if total_amount >= total_payments + fee {
70+
match parse_req_id_from_request(&req_json) {
71+
Err(ec) => return ec,
72+
_ => ()
73+
};
7074
//we have enough money for this txn, give it back
7175
let seq_no = payment_ledger::add_txn(inputs_json.clone(), outputs_json.clone());
7276

73-
_process_inputs(inputs_json);
74-
let infos: Vec<UTXOInfo> = _process_outputs(outputs_json, seq_no);
77+
_process_inputs(&inputs_json);
78+
let infos: Vec<UTXOInfo> = _process_outputs(&outputs_json, seq_no);
7579

76-
_save_response(infos, req_json.clone())
80+
_save_response(&infos, &req_json)
7781
} else {
7882
//we don't have enough money, send GET_TXN transaction to callback and in response PaymentsInsufficientFundsError will be returned
7983
ledger::build_get_txn_request(
8084
submitter_did.as_str(),
8185
1,
8286
Box::new(move |ec, res| {
8387
let ec = if ec == ErrorCode::Success {
84-
_add_response(res.clone(), "INSUFFICIENT_FUNDS".to_string())
88+
_add_response(&res, "INSUFFICIENT_FUNDS")
8589
} else { ec };
8690
trace!("libnullpay::add_request_fees::handle >>");
8791
_process_callback(cmd_handle, ec, res, cb);
@@ -117,9 +121,9 @@ pub mod build_get_utxo_request {
117121
1,
118122
Box::new(move |ec, res| {
119123
let ec = if ec == ErrorCode::Success {
120-
let utxos = utxo_cache::get_utxos_by_payment_address(payment_address.clone());
121-
let infos: Vec<UTXOInfo> = utxos.into_iter().filter_map(|utxo| payment_ledger::get_utxo_info(utxo)).collect();
122-
_save_response(infos, res.clone())
124+
let utxos = utxo_cache::get_utxos_by_payment_address(&payment_address);
125+
let infos: Vec<UTXOInfo> = utxos.into_iter().filter_map(payment_ledger::get_utxo_info).collect();
126+
_save_response(&infos, &res)
123127
} else { ec };
124128

125129
trace!("libnullpay::build_get_utxo_request::handle >>");
@@ -161,12 +165,12 @@ pub mod build_payment_req {
161165
if total_balance >= total_payments {
162166
let seq_no = payment_ledger::add_txn(inputs_json.clone(), outputs_json.clone());
163167

164-
_process_inputs(inputs_json.clone());
165-
let infos = _process_outputs(outputs_json.clone(), seq_no);
168+
_process_inputs(&inputs_json);
169+
let infos = _process_outputs(&outputs_json, seq_no);
166170

167-
_save_response(infos, res.clone())
171+
_save_response(&infos, &res)
168172
} else {
169-
_add_response(res.clone(), "INSUFFICIENT_FUNDS".to_string());
173+
_add_response(&res, "INSUFFICIENT_FUNDS");
170174
ErrorCode::Success
171175
}
172176
} else {
@@ -206,7 +210,7 @@ pub mod build_mint_req {
206210
let seq_no = payment_ledger::add_txn(vec![], outputs_json.clone());
207211

208212
outputs_json.clone().into_iter().for_each(|output| {
209-
utxo_cache::add_utxo(output.payment_address, seq_no, output.amount);
213+
utxo_cache::add_utxo(&output.payment_address, seq_no, output.amount);
210214
});
211215
}
212216

@@ -255,7 +259,7 @@ pub mod build_get_txn_fees_req {
255259
let info = config_ledger::get_all_fees();
256260

257261
match to_string(&info).map_err(|_| ErrorCode::CommonInvalidState) {
258-
Ok(str) => _add_response(res.clone(), str),
262+
Ok(str) => _add_response(&res, &str),
259263
Err(ec) => ec
260264
}
261265
} else { ec };
@@ -295,40 +299,40 @@ fn _process_callback(cmd_handle: i32, err: ErrorCode, response: String, cb: Opti
295299
}
296300
}
297301

298-
fn _process_outputs(outputs: Vec<UTXOOutput>, seq_no: i32) -> Vec<UTXOInfo> {
302+
fn _process_outputs(outputs: &Vec<UTXOOutput>, seq_no: i32) -> Vec<UTXOInfo> {
299303
outputs.into_iter().map(|out| {
300-
match utxo_cache::add_utxo(out.payment_address, seq_no, out.amount)
304+
match utxo_cache::add_utxo(&out.payment_address, seq_no, out.amount)
301305
.map(|utxo| payment_ledger::get_utxo_info(utxo)) {
302306
Some(Some(utxo_info)) => utxo_info,
303307
_ => panic!("Some UTXO was not processed!")
304308
}
305309
}).collect()
306310
}
307311

308-
fn _process_inputs(inputs: Vec<String>) {
312+
fn _process_inputs(inputs: &Vec<String>) {
309313
inputs.into_iter().for_each(|s| {
310314
utxo_cache::remove_utxo(s);
311315
});
312316
}
313317

314-
fn _save_response(infos: Vec<UTXOInfo>, request: String) -> ErrorCode {
315-
match serialize_infos(infos) {
316-
Ok(str) => _add_response(request, str),
318+
fn _save_response(infos: &Vec<UTXOInfo>, request: &str) -> ErrorCode {
319+
match serialize_infos(&infos) {
320+
Ok(str) => _add_response(request, &str),
317321
Err(ec) => ec
318322
}
319323
}
320324

321-
fn _add_response(request: String, response: String) -> ErrorCode {
325+
fn _add_response(request: &str, response: &str) -> ErrorCode {
322326
match add_response(request, response) {
323327
Err(ec) => ec,
324328
_ => ErrorCode::Success
325329
}
326330
}
327331

328332
fn _count_total_inputs(inputs: &Vec<String>) -> i32 {
329-
inputs.clone().into_iter().filter_map(utxo_cache::get_balanse_of_utxo).fold(0, |acc, next| acc + next)
333+
inputs.into_iter().filter_map(utxo_cache::get_balanse_of_utxo).fold(0, |acc, next| acc + next)
330334
}
331335

332336
fn _count_total_payments(outputs: &Vec<UTXOOutput>) -> i32 {
333-
outputs.clone().into_iter().fold(0, |acc, next| acc + next.amount)
337+
outputs.into_iter().fold(0, |acc, next| acc + next.amount)
334338
}

libnullpay/src/services/response_storage.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,20 @@ lazy_static! {
77
static ref RESPONSES: Mutex<HashMap<String, String>> = Default::default();
88
}
99

10-
pub fn add_response(request: String, response: String) -> Result<(), ErrorCode> {
11-
let val = str_to_val(request.as_str())?;
12-
let object = val_to_obj(&val)?;
13-
let req_id = get_val_from_obj(object, "reqId")?;
14-
let req_id = val_to_u64(req_id)?;
15-
10+
pub fn add_response(request: &str, response: &str) -> Result<(), ErrorCode> {
11+
let req_id = parse_req_id_from_request(request)?;
1612
let mut responses = RESPONSES.lock().unwrap();
17-
responses.insert(req_id.to_string(), response);
13+
responses.insert(req_id.to_string(), response.to_string());
1814
Ok(())
1915
}
2016

17+
pub fn parse_req_id_from_request(request: &str) -> Result<u64, ErrorCode> {
18+
let val = str_to_val(request)?;
19+
let object = val_to_obj(&val)?;
20+
let req_id = get_val_from_obj(object, "reqId")?;
21+
val_to_u64(req_id)
22+
}
23+
2124
pub fn get_response(response: &str) -> Result<String, ErrorCode> {
2225
let val = str_to_val(response)?;
2326
let object = val_to_obj(&val)?;

libnullpay/src/services/utxo_cache.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,25 @@ lazy_static! {
88
static ref BALANCES: Mutex<HashMap<String, i32>> = Default::default();
99
}
1010

11-
pub fn get_utxos_by_payment_address(payment_address: String) -> Vec<String> {
11+
pub fn get_utxos_by_payment_address(payment_address: &str) -> Vec<String> {
1212
let utxos = UTXOS.lock().unwrap();
13-
match utxos.get(&payment_address) {
13+
match utxos.get(payment_address) {
1414
Some(v) => v.clone(),
1515
None => Vec::new()
1616
}
1717
}
1818

19-
pub fn get_balanse_of_utxo(utxo: String) -> Option<i32> {
19+
pub fn get_balanse_of_utxo(utxo: &String) -> Option<i32> {
2020
let balances = BALANCES.lock().unwrap();
21-
balances.get(&utxo).map(|a| a.clone())
21+
balances.get(utxo).map(|a| a.clone())
2222
}
2323

24-
pub fn add_utxo(payment_address: String, seq_no: i32, balance: i32) -> Option<String> {
25-
to_utxo(payment_address.as_str(), seq_no).map(|utxo| {
24+
pub fn add_utxo(payment_address: &str, seq_no: i32, balance: i32) -> Option<String> {
25+
to_utxo(payment_address, seq_no).map(|utxo| {
2626
let mut balances = BALANCES.lock().unwrap();
2727
let mut utxos = UTXOS.lock().unwrap();
2828
balances.insert(utxo.clone(), balance);
29-
let vec = utxos.remove(&payment_address);
29+
let vec = utxos.remove(payment_address);
3030
let vec = match vec {
3131
Some(v) => {
3232
let mut v = Vec::from(v);
@@ -35,22 +35,22 @@ pub fn add_utxo(payment_address: String, seq_no: i32, balance: i32) -> Option<St
3535
}
3636
None => vec![utxo.clone()]
3737
};
38-
utxos.insert(payment_address, vec);
38+
utxos.insert(payment_address.to_string(), vec);
3939
utxo
4040
})
4141
}
4242

43-
pub fn remove_utxo(utxo: String) {
44-
let res = from_utxo(utxo.as_str());
43+
pub fn remove_utxo(utxo: &str) {
44+
let res = from_utxo(utxo);
4545
match res {
4646
Some((_,payment_address)) => {
4747
let mut balances = BALANCES.lock().unwrap();
4848
let mut utxos = UTXOS.lock().unwrap();
49-
balances.remove(&utxo);
49+
balances.remove(utxo);
5050
match utxos.remove(&payment_address)
5151
.map(|vs|
5252
vs.into_iter()
53-
.filter(|v| v.to_string() != utxo)
53+
.filter(|v| v != utxo)
5454
.collect::<Vec<String>>()
5555
) {
5656
Some(ref v) if !v.is_empty() => {utxos.insert(payment_address, v.to_vec());},

libnullpay/src/utils/json_helper.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub fn parse_operation_from_request (req: &str) -> Result<String, ErrorCode> {
1515
}
1616
}
1717

18-
pub fn serialize_infos(infos: Vec<UTXOInfo>) -> Result<String, ErrorCode> {
18+
pub fn serialize_infos(infos: &Vec<UTXOInfo>) -> Result<String, ErrorCode> {
1919
to_string(&infos).map_err(|_| {
2020
error!("Can't deserialize UTXO Info");
2121
ErrorCode::CommonInvalidState

0 commit comments

Comments
 (0)