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 bd6e4c3
Showing 1 changed file with 0 additions and 40 deletions.
40 changes: 0 additions & 40 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 @@ -611,7 +572,6 @@ impl BalanceStore for RedisStore {
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 bd6e4c3

Please sign in to comment.