Skip to content

Commit cf1dbd6

Browse files
committed
rename leftovers to uncredited_settlement_amount
1 parent 88dc75d commit cf1dbd6

File tree

5 files changed

+65
-45
lines changed

5 files changed

+65
-45
lines changed

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,9 @@ where
297297
let our_address = self.address.own_address;
298298
let token_address = self.address.token_address;
299299

300+
// We `Box` futures in these functions due to
301+
// https://github.com/rust-lang/rust/issues/54540#issuecomment-494749912.
302+
// Otherwise, we get `type_length_limit` errors.
300303
// get the current block number
301304
Box::new(
302305
web3.eth()
@@ -306,7 +309,6 @@ where
306309
trace!("Current block {}", current_block);
307310
// get the safe number of blocks to avoid reorgs
308311
let fetch_until = current_block - confirmations;
309-
// U256 does not implement IntoFuture so we must wrap it
310312
Ok((Ok(fetch_until), store.load_recently_observed_block()))
311313
})
312314
.flatten()
@@ -506,19 +508,19 @@ where
506508
.push("settlements");
507509
debug!("Making POST to {:?} {:?} about {:?}", url, amount, tx_hash);
508510

509-
// settle for amount + leftovers
511+
// settle for amount + uncredited_settlement_amount
510512
let account_id_clone = account_id.clone();
511513
let full_amount_fut = self
512514
.store
513-
.pop_leftovers(account_id.clone())
514-
.and_then(move |leftovers| {
515+
.load_uncredited_settlement_amount(account_id.clone())
516+
.and_then(move |uncredited_settlement_amount| {
515517
let full_amount_fut2 = result(BigUint::from_str(&amount).map_err(move |err| {
516518
let error_msg = format!("Error converting to BigUint {:?}", err);
517519
error!("{:?}", error_msg);
518520
}))
519521
.and_then(move |amount| {
520522
debug!("Got uncredited amount {}", amount);
521-
let full_amount = amount + leftovers;
523+
let full_amount = amount + uncredited_settlement_amount;
522524
debug!(
523525
"Notifying accounting system about full amount: {}",
524526
full_amount
@@ -573,7 +575,7 @@ where
573575

574576
/// Parses a response from a connector into a Quantity type and calls a
575577
/// function to further process the parsed data to check if the store's
576-
/// leftovers should be updated.
578+
/// uncredited settlement amount should be updated.
577579
fn process_connector_response(
578580
&self,
579581
account_id: String,
@@ -608,7 +610,7 @@ where
608610

609611
// Normalizes a received Quantity object against the local engine scale, and
610612
// if the normalized value is less than what the engine originally sent, it
611-
// stores it as leftovers in the store.
613+
// stores it as uncredited settlement amount in the store.
612614
fn process_received_quantity(
613615
&self,
614616
account_id: String,
@@ -635,8 +637,8 @@ where
635637
let diff = engine_amount - scaled_connector_amount;
636638
// connector settled less than we
637639
// instructed it to, so we must save
638-
// the difference for the leftovers
639-
store.save_leftovers(account_id, diff)
640+
// the difference
641+
store.save_uncredited_settlement_amount(account_id, diff)
640642
} else {
641643
Box::new(ok(()))
642644
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub struct TestStore {
6262
pub last_observed_block: Arc<RwLock<U256>>,
6363
pub saved_hashes: Arc<RwLock<HashMap<H256, bool>>>,
6464
pub cache_hits: Arc<RwLock<u64>>,
65-
pub leftovers: Arc<RwLock<HashMap<String, BigUint>>>,
65+
pub uncredited_settlement_amount: Arc<RwLock<HashMap<String, BigUint>>>,
6666
}
6767

6868
use crate::stores::LeftoversStore;
@@ -71,21 +71,21 @@ use num_bigint::BigUint;
7171
impl LeftoversStore for TestStore {
7272
type AssetType = BigUint;
7373

74-
fn save_leftovers(
74+
fn save_uncredited_settlement_amount(
7575
&self,
7676
account_id: String,
77-
leftovers: Self::AssetType,
77+
uncredited_settlement_amount: Self::AssetType,
7878
) -> Box<Future<Item = (), Error = ()> + Send> {
79-
let mut guard = self.leftovers.write();
80-
(*guard).insert(account_id, leftovers);
79+
let mut guard = self.uncredited_settlement_amount.write();
80+
(*guard).insert(account_id, uncredited_settlement_amount);
8181
Box::new(ok(()))
8282
}
8383

84-
fn pop_leftovers(
84+
fn load_uncredited_settlement_amount(
8585
&self,
8686
account_id: String,
8787
) -> Box<Future<Item = Self::AssetType, Error = ()> + Send> {
88-
let mut guard = self.leftovers.write();
88+
let mut guard = self.uncredited_settlement_amount.write();
8989
if let Some(l) = guard.get(&account_id) {
9090
let l = l.clone();
9191
(*guard).insert(account_id, Zero::zero());
@@ -269,7 +269,7 @@ impl TestStore {
269269
cache_hits: Arc::new(RwLock::new(0)),
270270
last_observed_block: Arc::new(RwLock::new(U256::from(0))),
271271
saved_hashes: Arc::new(RwLock::new(HashMap::new())),
272-
leftovers: Arc::new(RwLock::new(HashMap::new())),
272+
uncredited_settlement_amount: Arc::new(RwLock::new(HashMap::new())),
273273
}
274274
}
275275
}

crates/interledger-settlement-engines/src/stores/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ pub trait LeftoversStore {
3333
type AssetType;
3434

3535
/// Saves the leftover data
36-
fn save_leftovers(
36+
fn save_uncredited_settlement_amount(
3737
&self,
3838
account_id: String,
39-
leftovers: Self::AssetType,
39+
uncredited_settlement_amount: Self::AssetType,
4040
) -> Box<dyn Future<Item = (), Error = ()> + Send>;
4141

4242
/// Clears the leftover data in the database and returns the cleared value
43-
fn pop_leftovers(
43+
fn load_uncredited_settlement_amount(
4444
&self,
4545
account_id: String,
4646
) -> Box<dyn Future<Item = Self::AssetType, Error = ()> + Send>;

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

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -112,46 +112,64 @@ impl EthereumLedgerRedisStore {
112112
impl LeftoversStore for EthereumLedgerRedisStore {
113113
type AssetType = BigUint;
114114

115-
fn save_leftovers(
115+
fn save_uncredited_settlement_amount(
116116
&self,
117117
account_id: String,
118-
leftovers: Self::AssetType,
118+
uncredited_settlement_amount: Self::AssetType,
119119
) -> Box<dyn Future<Item = (), Error = ()> + Send> {
120-
trace!("Saving leftovers {:?} {:?}", account_id, leftovers);
120+
trace!(
121+
"Saving uncredited_settlement_amount {:?} {:?}",
122+
account_id,
123+
uncredited_settlement_amount
124+
);
121125
let mut pipe = redis::pipe();
122-
pipe.set(format!("leftovers:{}", account_id), leftovers.to_string())
123-
.ignore();
126+
pipe.set(
127+
format!("uncredited_settlement_amount:{}", account_id),
128+
uncredited_settlement_amount.to_string(),
129+
)
130+
.ignore();
124131
Box::new(
125132
pipe.query_async(self.connection.clone())
126-
.map_err(move |err| error!("Error saving leftovers {:?}: {:?}", leftovers, err))
133+
.map_err(move |err| {
134+
error!(
135+
"Error saving uncredited_settlement_amount {:?}: {:?}",
136+
uncredited_settlement_amount, err
137+
)
138+
})
127139
.and_then(move |(_conn, _ret): (_, Value)| Ok(())),
128140
)
129141
}
130142

131-
fn pop_leftovers(
143+
fn load_uncredited_settlement_amount(
132144
&self,
133145
account_id: String,
134146
) -> Box<dyn Future<Item = Self::AssetType, Error = ()> + Send> {
135-
trace!("Loading leftovers {:?}", account_id);
147+
trace!("Loading uncredited_settlement_amount {:?}", account_id);
136148
let mut pipe = redis::pipe();
137149
// Loads the value and resets it to 0
138-
pipe.getset(format!("leftovers:{}", account_id), 0);
150+
pipe.getset(format!("uncredited_settlement_amount:{}", account_id), 0);
139151
Box::new(
140152
pipe.query_async(self.connection.clone())
141-
.map_err(move |err| error!("Error loading leftovers {:?}: ", err))
142-
.and_then(move |(_conn, leftovers): (_, Vec<String>)| {
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))
153+
.map_err(move |err| {
154+
error!("Error loading uncredited_settlement_amount {:?}: ", err)
155+
})
156+
.and_then(
157+
move |(_conn, uncredited_settlement_amount): (_, Vec<String>)| {
158+
// redis.rs returns a bulk value for some reason, length is
159+
// always 1
160+
if uncredited_settlement_amount.len() == 1 {
161+
if let Ok(uncredited_settlement_amount) =
162+
BigUint::from_str(&uncredited_settlement_amount[0])
163+
{
164+
Box::new(ok(uncredited_settlement_amount))
165+
} else {
166+
Box::new(ok(Zero::zero()))
167+
}
148168
} else {
149169
Box::new(ok(Zero::zero()))
150170
}
151-
} else {
152-
Box::new(ok(Zero::zero()))
153-
}
154-
}),
171+
},
172+
),
155173
)
156174
}
157175
}
@@ -337,16 +355,16 @@ mod tests {
337355
use std::str::FromStr;
338356

339357
#[test]
340-
fn saves_and_pops_leftovers_properly() {
358+
fn saves_and_pops_uncredited_settlement_amount_properly() {
341359
let amount = BigUint::from(100u64);
342360
let acc = "0".to_string();
343361
block_on(test_store().and_then(|(store, context)| {
344362
store
345-
.save_leftovers(acc.clone(), amount.clone())
363+
.save_uncredited_settlement_amount(acc.clone(), amount.clone())
346364
.map_err(|err| eprintln!("Redis error: {:?}", err))
347365
.and_then(move |_| {
348366
store
349-
.pop_leftovers(acc)
367+
.load_uncredited_settlement_amount(acc)
350368
.map_err(|err| eprintln!("Redis error: {:?}", err))
351369
.and_then(move |ret| {
352370
assert_eq!(amount, ret);

crates/interledger-settlement/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ mod tests {
186186
.unwrap(),
187187
1
188188
);
189-
// there's leftovers for all number slots which do not increase in
189+
// there's uncredited_settlement_amount for all number slots which do not increase in
190190
// increments of 10^abs(to_scale-from_scale)
191191
assert_eq!(
192192
1u64.normalize_scale(ConvertDetails { from: 2, to: 1 })
@@ -219,7 +219,7 @@ mod tests {
219219
.unwrap(),
220220
100
221221
);
222-
// 299 units with base 3 is 29 units with base 2 (0.9 leftovers)
222+
// 299 units with base 3 is 29 units with base 2 (0.9 uncredited_settlement_amount)
223223
assert_eq!(
224224
299u64
225225
.normalize_scale(ConvertDetails { from: 3, to: 2 })

0 commit comments

Comments
 (0)