Skip to content

Commit

Permalink
Merge pull request #255 from freedomlayer/real/feat/simplify-remote-m…
Browse files Browse the repository at this point in the history
…ax-debt

Simplify remote max debt
  • Loading branch information
realcr committed Dec 24, 2019
2 parents 1d7b52d + 909a113 commit 492e1a4
Show file tree
Hide file tree
Showing 37 changed files with 388 additions and 700 deletions.
9 changes: 4 additions & 5 deletions components/funder/src/friend.rs
Expand Up @@ -113,10 +113,9 @@ pub struct CurrencyConfig {
/// Rate of forwarding transactions that arrived from this friend to any other friend
/// for a certain currency.
pub rate: Rate,
/// Wanted credit frame for the remote side (Set by the user of this node)
/// It might take a while until this value is applied, as it needs to be communicated to the
/// remote side.
pub wanted_remote_max_debt: u128,
/// Credit frame for the remote side (Set by the user of this node)
/// The remote side does not know this value.
pub remote_max_debt: u128,
/// Can the remote friend send requests through us? This is a value chosen by the user, and it
/// might take some time until it is applied (As it should be communicated to the remote
/// friend).
Expand Down Expand Up @@ -172,7 +171,7 @@ impl CurrencyConfig {
pub fn new() -> Self {
Self {
rate: Rate::new(),
wanted_remote_max_debt: 0,
remote_max_debt: 0,
wanted_local_requests_status: RequestsStatus::Closed,
}
}
Expand Down
4 changes: 2 additions & 2 deletions components/funder/src/handler/handle_control.rs
Expand Up @@ -78,15 +78,15 @@ where
.currency_configs
.get(&set_friend_currency_max_debt.currency)
{
if currency_config.wanted_remote_max_debt == set_friend_currency_max_debt.remote_max_debt {
if currency_config.remote_max_debt == set_friend_currency_max_debt.remote_max_debt {
return Ok(());
}
currency_config.clone()
} else {
CurrencyConfig::new()
};

new_currency_config.wanted_remote_max_debt = set_friend_currency_max_debt.remote_max_debt;
new_currency_config.remote_max_debt = set_friend_currency_max_debt.remote_max_debt;

let friend_mutation = FriendMutation::UpdateCurrencyConfig((
set_friend_currency_max_debt.currency,
Expand Down
37 changes: 35 additions & 2 deletions components/funder/src/handler/handle_friend.rs
Expand Up @@ -429,6 +429,24 @@ fn handle_request_send_funds<B>(
);
}

fn handle_request_send_funds_cancel<B>(
m_state: &mut MutableFunderState<B>,
send_commands: &mut SendCommands,
remote_public_key: &PublicKey,
currency: &Currency,
request_send_funds: RequestSendFundsOp,
) where
B: Clone + PartialEq + Eq + CanonicalSerialize + Debug,
{
reply_with_cancel(
m_state,
send_commands,
remote_public_key,
currency,
&request_send_funds.request_id,
);
}

fn handle_response_send_funds<B>(
m_state: &mut MutableFunderState<B>,
send_commands: &mut SendCommands,
Expand Down Expand Up @@ -706,6 +724,15 @@ fn handle_move_token_output<B, R>(
request_send_funds,
);
}
IncomingMessage::RequestCancel(request_send_funds) => {
handle_request_send_funds_cancel(
m_state,
send_commands,
remote_public_key,
currency,
request_send_funds,
);
}
IncomingMessage::Response(IncomingResponseSendFundsOp {
pending_transaction,
incoming_response,
Expand Down Expand Up @@ -965,8 +992,14 @@ where
};

// We will only consider move token messages if we are in a consistent state:
let receive_move_token_res =
token_channel.simulate_receive_move_token(friend_move_token_request.move_token);
let remote_max_debts = friend
.currency_configs
.iter()
.map(|(currency, currency_config)| (currency.clone(), currency_config.remote_max_debt))
.collect();

let receive_move_token_res = token_channel
.simulate_receive_move_token(friend_move_token_request.move_token, &remote_max_debts);
let token_wanted = friend_move_token_request.token_wanted;

match receive_move_token_res {
Expand Down
2 changes: 1 addition & 1 deletion components/funder/src/handler/handle_init.rs
Expand Up @@ -62,7 +62,7 @@ mod tests {
use crate::state::{FunderMutation, FunderState};

use crate::handler::state_wrap::MutableFunderState;
use crate::tests::utils::{dummy_named_relay_address, dummy_relay_address};
use crate::handler::tests::utils::{dummy_named_relay_address, dummy_relay_address};

#[test]
fn test_handle_init_basic() {
Expand Down
2 changes: 1 addition & 1 deletion components/funder/src/handler/handle_liveness.rs
Expand Up @@ -108,8 +108,8 @@ mod tests {
use crate::state::{FunderMutation, FunderState};

use crate::handler::state_wrap::{MutableEphemeral, MutableFunderState};
use crate::handler::tests::utils::{dummy_named_relay_address, dummy_relay_address};
use crate::handler::types::SendCommands;
use crate::tests::utils::{dummy_named_relay_address, dummy_relay_address};

#[test]
fn test_handle_liveness_basic() {
Expand Down
20 changes: 9 additions & 11 deletions components/funder/src/handler/handler.rs
Expand Up @@ -197,16 +197,15 @@ where
// Send all possible messages according to SendCommands
// TODO: Maybe we should output outgoing_comms instead of friend_messages and
// outgoing_channeler_config. When we merge the two, we might be out of order!
let (sender_outgoing_control, friend_messages, outgoing_channeler_config) =
create_friend_messages(
&mut m_state,
m_ephemeral.ephemeral(),
&send_commands,
max_operations_in_batch,
identity_client,
rng,
)
.await;
let (friend_messages, outgoing_channeler_config) = create_friend_messages(
&mut m_state,
m_ephemeral.ephemeral(),
&send_commands,
max_operations_in_batch,
identity_client,
rng,
)
.await;

for channeler_config in outgoing_channeler_config {
outgoing_comms.push(FunderOutgoingComm::ChannelerConfig(channeler_config));
Expand Down Expand Up @@ -242,7 +241,6 @@ where

// We always send the report mutations first through the outgoing control:
outgoing_control.extend(handle_outgoing_control);
outgoing_control.extend(sender_outgoing_control);

Ok(FunderHandlerOutput {
funder_mutations,
Expand Down

0 comments on commit 492e1a4

Please sign in to comment.