Skip to content

Commit

Permalink
remove apply_outgoing_settlement function
Browse files Browse the repository at this point in the history
Adding it resulted in incorerct behavior the balance is already lowered in the PROCESS_FULFILL script. With it,  the balance was lowered by 2x the amount it should.

Note that the reason the balance is lowered first is because we don't want two packets fulfilled simultaneously to trigger settlements for the same amount_to_settle. By reducing the balance by the amount_to_settle before making the settlement client send the request to the settlement engine, this ensures that the settlement will only happen once. The refund_settlement is there in case that call to the SE fails and we need to revert the change to the balance.
  • Loading branch information
gakonst committed Jul 25, 2019
1 parent 1f0d86c commit ee965bd
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,11 @@ where
store
.load_account_id_from_address(addr)
.and_then(move |id| {
self_clone.notify_connector(id.to_string(), amount.low_u64(), tx_hash)
self_clone.notify_connector(
id.to_string(),
amount.low_u64(),
tx_hash,
)
})
.and_then(move |_| {
// only save the transaction hash if the connector
Expand Down Expand Up @@ -461,10 +465,7 @@ where
.push(&account_id.clone())
.push("settlement");
let client = Client::new();
debug!(
"Making POST to {:?} {:?} about {:?}",
url, amount, tx_hash
);
debug!("Making POST to {:?} {:?} about {:?}", url, amount, tx_hash);
let action = move || {
let account_id = account_id.clone();
client
Expand Down
41 changes: 0 additions & 41 deletions crates/interledger-store-redis/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,6 @@ local settle_amount = tonumber(ARGV[2])
local balance = redis.call('HINCRBY', account, 'balance', settle_amount)
return balance";
static APPLY_OUTGOING_SETTLEMENT: &str = "
local account = 'accounts:' .. ARGV[1]
local settle_amount = tonumber(ARGV[2])
local balance = redis.call('HINCRBY', account, 'balance', 0 - settle_amount)
return balance";
static PROCESS_INCOMING_SETTLEMENT: &str = "
local account = 'accounts:' .. ARGV[1]
local amount = tonumber(ARGV[2])
Expand Down Expand Up @@ -394,39 +388,6 @@ impl RedisStore {
)
}

fn apply_outgoing_settlement(
&self,
account_id: u64,
settle_amount: u64,
) -> impl Future<Item = (), Error = ()> {
trace!(
"Applying settlement for account: {}, amount: {}",
account_id,
settle_amount
);
cmd("EVAL")
.arg(APPLY_OUTGOING_SETTLEMENT)
.arg(0)
.arg(account_id)
.arg(settle_amount)
.query_async(self.connection.as_ref().clone())
.map_err(move |err| {
error!(
"Error applying settlement for account: {}, amount: {}: {:?}",
account_id, settle_amount, err
)
})
.and_then(move |(_connection, balance): (_, i64)| {
trace!(
"Applied settlement for account: {}, amount: {}. Balance is now: {}",
account_id,
settle_amount,
balance
);
Ok(())
})
}

fn refund_settlement(
&self,
account_id: u64,
Expand Down Expand Up @@ -608,10 +569,8 @@ impl BalanceStore for RedisStore {
// settlement engine for the status of each
// outgoing settlement and putting unnecessary
// load on the settlement engine.
let store_clone = store.clone();
spawn(settlement_client
.send_settlement(to_account, amount_to_settle)
.and_then(move |_| { store_clone.apply_outgoing_settlement(to_account_id, amount_to_settle) })
.or_else(move |_| store.refund_settlement(to_account_id, amount_to_settle)));
} else {
trace!(
Expand Down

0 comments on commit ee965bd

Please sign in to comment.