Skip to content

Commit 11b99a8

Browse files
committed
fix(tests): return 0 if there were no leftovers found in the store
1 parent dce326d commit 11b99a8

File tree

3 files changed

+54
-47
lines changed

3 files changed

+54
-47
lines changed

crates/interledger-settlement-engines/src/engines/ethereum_ledger/eth_engine.rs

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -494,67 +494,66 @@ where
494494
amount: String,
495495
tx_hash: H256,
496496
) -> impl Future<Item = (), Error = ()> {
497-
let mut url = self.connector_url.clone();
498-
let account_id_clone = account_id.clone();
499497
let engine_scale = self.asset_scale;
498+
let mut url = self.connector_url.clone();
500499
url.path_segments_mut()
501500
.expect("Invalid connector URL")
502501
.push("accounts")
503502
.push(&account_id.clone())
504503
.push("settlements");
505504
debug!("Making POST to {:?} {:?} about {:?}", url, amount, tx_hash);
506-
let self_clone = self.clone();
507-
let store = self.store.clone();
508-
let amount_clone = amount.clone();
509-
let action = move || {
510-
// need to make 2 clones, one to own the variables in the function
511-
// and one for the retry closure..
512-
let self_clone = self_clone.clone();
513-
let store = store.clone();
514-
let account_id = account_id.clone();
515-
let account_id_clone2 = account_id.clone();
516-
let amount = amount.clone();
517-
let url = url.clone();
518-
519-
// settle for amount + leftovers
520-
store.pop_leftovers(account_id.clone())
505+
506+
// settle for amount + leftovers
507+
self.store
508+
.pop_leftovers(account_id.clone())
521509
.and_then(move |leftovers| {
522-
result(BigUint::from_str(&amount.clone()).map_err(move |err| {
510+
debug!("POPPED LEFTOVERS {:?}", leftovers);
511+
result(BigUint::from_str(&amount).map_err(move |err| {
523512
let error_msg = format!("Error converting to BigUint {:?}", err);
524513
error!("{:?}", error_msg);
525514
}))
526515
.and_then(move |amount| {
527-
Ok(amount + leftovers)
528-
})
529-
})
530-
.and_then(move |full_amount| {
531-
let client = Client::new();
532-
let full_amount_clone = full_amount.clone();
533-
client
534-
.post(url.clone())
535-
.header("Idempotency-Key", tx_hash.to_string())
536-
.json(&json!({ "amount": full_amount.clone().to_string(), "scale" : engine_scale }))
537-
.send()
538-
.map_err(move |err| {
539-
error!(
540-
"Error notifying Accounting System's account: {:?}, amount: {:?}: {:?}",
541-
account_id, full_amount_clone, err
542-
)
516+
debug!("Got uncredited amount {}", amount);
517+
let full_amount = amount + leftovers;
518+
let client = Client::new();
519+
debug!(
520+
"Notifying accounting system about full amount: {}",
521+
full_amount
522+
);
523+
let url = url.clone();
524+
let account_id_clone = account_id.clone();
525+
let full_amount_clone = full_amount.clone();
526+
527+
let action = move || {
528+
let account_id = account_id.clone();
529+
let full_amount = full_amount.clone();
530+
client
531+
.post(url.clone())
532+
.header("Idempotency-Key", tx_hash.to_string())
533+
.json(&json!(Quantity::new(full_amount.clone(), engine_scale)))
534+
.send()
535+
.map_err(move |err| {
536+
error!(
537+
"Error notifying Accounting System's account: {:?}, amount: {:?}: {:?}",
538+
account_id.clone(), full_amount.clone(), err
539+
);
540+
})
541+
};
542+
543+
Retry::spawn(
544+
ExponentialBackoff::from_millis(10).take(MAX_RETRIES),
545+
action,
546+
)
547+
.map_err(move |_| {
548+
error!("Exceeded max retries when notifying connector about account {:?} for amount {:?} and transaction hash {:?}. Please check your API.", account_id_clone, full_amount_clone, tx_hash)
543549
})
544550
.and_then(move |response| {
545551
trace!("Accounting system responded with {:?}", response);
546552
Ok(()) // This call causes the type_length_error
547553
// self_clone.process_connector_response(account_id_clone2, response, full_amount.clone())
548554
})
555+
})
549556
})
550-
};
551-
Retry::spawn(
552-
ExponentialBackoff::from_millis(10).take(MAX_RETRIES),
553-
action,
554-
)
555-
.map_err(move |_| {
556-
error!("Exceeded max retries when notifying connector about account {:?} for amount {:?} and transaction hash {:?}. Please check your API.", account_id_clone, amount_clone, tx_hash)
557-
})
558557
}
559558

560559
fn process_connector_response(

crates/interledger-settlement-engines/src/engines/ethereum_ledger/test_helpers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl LeftoversStore for TestStore {
9191
(*guard).insert(account_id, Zero::zero());
9292
Box::new(ok(l.clone()))
9393
} else {
94-
Box::new(err(()))
94+
Box::new(ok(Zero::zero()))
9595
}
9696
}
9797
}

crates/interledger-settlement-engines/src/stores/redis_ethereum_ledger/store.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::collections::HashMap;
99
use std::str::FromStr;
1010

1111
use crate::engines::ethereum_ledger::{EthereumAccount, EthereumAddresses, EthereumStore};
12+
use num_traits::Zero;
1213
use redis::{self, cmd, r#async::SharedConnection, ConnectionInfo, PipelineCommands, Value};
1314

1415
use log::{error, trace};
@@ -116,6 +117,7 @@ impl LeftoversStore for EthereumLedgerRedisStore {
116117
account_id: String,
117118
leftovers: Self::AssetType,
118119
) -> Box<dyn Future<Item = (), Error = ()> + Send> {
120+
trace!("Saving leftovers {:?} {:?}", account_id, leftovers);
119121
let mut pipe = redis::pipe();
120122
pipe.set(format!("leftovers:{}", account_id), leftovers.to_string())
121123
.ignore();
@@ -130,18 +132,24 @@ impl LeftoversStore for EthereumLedgerRedisStore {
130132
&self,
131133
account_id: String,
132134
) -> Box<dyn Future<Item = Self::AssetType, Error = ()> + Send> {
135+
trace!("Loading leftovers {:?}", account_id);
133136
let mut pipe = redis::pipe();
134137
// Loads the value and resets it to 0
135138
pipe.getset(format!("leftovers:{}", account_id), 0);
136139
Box::new(
137140
pipe.query_async(self.connection.clone())
138141
.map_err(move |err| error!("Error loading leftovers {:?}: ", err))
139142
.and_then(move |(_conn, leftovers): (_, Vec<String>)| {
140-
// redis.rs returns a bulk value for some reason, length is always 1
141-
if let Ok(leftovers) = BigUint::from_str(&leftovers[0]) {
142-
Box::new(ok(leftovers))
143+
// redis.rs returns a bulk value for some reason, length is
144+
// always 1
145+
if leftovers.len() == 1 {
146+
if let Ok(leftovers) = BigUint::from_str(&leftovers[0]) {
147+
Box::new(ok(leftovers))
148+
} else {
149+
Box::new(ok(Zero::zero()))
150+
}
143151
} else {
144-
Box::new(err(()))
152+
Box::new(ok(Zero::zero()))
145153
}
146154
}),
147155
)

0 commit comments

Comments
 (0)