Skip to content

Commit 635e9f6

Browse files
author
Nikita Khateev
committed
Merge branch 'feature/nullpay-tests'
2 parents c9bf420 + 91a9a1a commit 635e9f6

File tree

5 files changed

+128
-133
lines changed

5 files changed

+128
-133
lines changed

libnullpay/src/payment_method.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -67,25 +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.clone()) {
70+
match parse_req_id_from_request(&req_json) {
7171
Err(ec) => return ec,
7272
_ => ()
7373
};
7474
//we have enough money for this txn, give it back
7575
let seq_no = payment_ledger::add_txn(inputs_json.clone(), outputs_json.clone());
7676

77-
_process_inputs(inputs_json);
78-
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);
7979

80-
_save_response(infos, req_json.clone())
80+
_save_response(&infos, &req_json)
8181
} else {
8282
//we don't have enough money, send GET_TXN transaction to callback and in response PaymentsInsufficientFundsError will be returned
8383
ledger::build_get_txn_request(
8484
submitter_did.as_str(),
8585
1,
8686
Box::new(move |ec, res| {
8787
let ec = if ec == ErrorCode::Success {
88-
_add_response(res.clone(), "INSUFFICIENT_FUNDS".to_string())
88+
_add_response(&res, "INSUFFICIENT_FUNDS")
8989
} else { ec };
9090
trace!("libnullpay::add_request_fees::handle >>");
9191
_process_callback(cmd_handle, ec, res, cb);
@@ -121,9 +121,9 @@ pub mod build_get_utxo_request {
121121
1,
122122
Box::new(move |ec, res| {
123123
let ec = if ec == ErrorCode::Success {
124-
let utxos = utxo_cache::get_utxos_by_payment_address(payment_address.clone());
125-
let infos: Vec<UTXOInfo> = utxos.into_iter().filter_map(|utxo| payment_ledger::get_utxo_info(utxo)).collect();
126-
_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)
127127
} else { ec };
128128

129129
trace!("libnullpay::build_get_utxo_request::handle >>");
@@ -165,12 +165,12 @@ pub mod build_payment_req {
165165
if total_balance >= total_payments {
166166
let seq_no = payment_ledger::add_txn(inputs_json.clone(), outputs_json.clone());
167167

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

171-
_save_response(infos, res.clone())
171+
_save_response(&infos, &res)
172172
} else {
173-
_add_response(res.clone(), "INSUFFICIENT_FUNDS".to_string());
173+
_add_response(&res, "INSUFFICIENT_FUNDS");
174174
ErrorCode::Success
175175
}
176176
} else {
@@ -210,7 +210,7 @@ pub mod build_mint_req {
210210
let seq_no = payment_ledger::add_txn(vec![], outputs_json.clone());
211211

212212
outputs_json.clone().into_iter().for_each(|output| {
213-
utxo_cache::add_utxo(output.payment_address, seq_no, output.amount);
213+
utxo_cache::add_utxo(&output.payment_address, seq_no, output.amount);
214214
});
215215
}
216216

@@ -259,7 +259,7 @@ pub mod build_get_txn_fees_req {
259259
let info = config_ledger::get_all_fees();
260260

261261
match to_string(&info).map_err(|_| ErrorCode::CommonInvalidState) {
262-
Ok(str) => _add_response(res.clone(), str),
262+
Ok(str) => _add_response(&res, &str),
263263
Err(ec) => ec
264264
}
265265
} else { ec };
@@ -299,40 +299,40 @@ fn _process_callback(cmd_handle: i32, err: ErrorCode, response: String, cb: Opti
299299
}
300300
}
301301

302-
fn _process_outputs(outputs: Vec<UTXOOutput>, seq_no: i32) -> Vec<UTXOInfo> {
302+
fn _process_outputs(outputs: &Vec<UTXOOutput>, seq_no: i32) -> Vec<UTXOInfo> {
303303
outputs.into_iter().map(|out| {
304-
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)
305305
.map(|utxo| payment_ledger::get_utxo_info(utxo)) {
306306
Some(Some(utxo_info)) => utxo_info,
307307
_ => panic!("Some UTXO was not processed!")
308308
}
309309
}).collect()
310310
}
311311

312-
fn _process_inputs(inputs: Vec<String>) {
312+
fn _process_inputs(inputs: &Vec<String>) {
313313
inputs.into_iter().for_each(|s| {
314314
utxo_cache::remove_utxo(s);
315315
});
316316
}
317317

318-
fn _save_response(infos: Vec<UTXOInfo>, request: String) -> ErrorCode {
319-
match serialize_infos(infos) {
320-
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),
321321
Err(ec) => ec
322322
}
323323
}
324324

325-
fn _add_response(request: String, response: String) -> ErrorCode {
325+
fn _add_response(request: &str, response: &str) -> ErrorCode {
326326
match add_response(request, response) {
327327
Err(ec) => ec,
328328
_ => ErrorCode::Success
329329
}
330330
}
331331

332332
fn _count_total_inputs(inputs: &Vec<String>) -> i32 {
333-
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)
334334
}
335335

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

libnullpay/src/services/response_storage.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ 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> {
10+
pub fn add_response(request: &str, response: &str) -> Result<(), ErrorCode> {
1111
let req_id = parse_req_id_from_request(request)?;
1212
let mut responses = RESPONSES.lock().unwrap();
13-
responses.insert(req_id.to_string(), response);
13+
responses.insert(req_id.to_string(), response.to_string());
1414
Ok(())
1515
}
1616

17-
pub fn parse_req_id_from_request(request: String) -> Result<u64, ErrorCode> {
18-
let val = str_to_val(request.as_str())?;
17+
pub fn parse_req_id_from_request(request: &str) -> Result<u64, ErrorCode> {
18+
let val = str_to_val(request)?;
1919
let object = val_to_obj(&val)?;
2020
let req_id = get_val_from_obj(object, "reqId")?;
2121
val_to_u64(req_id)

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)