Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Allow one participant to withdraw for all participants #13

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 47 additions & 23 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,37 +447,54 @@ impl Adjudicator {
/// withdraw is used to withdraw a party's balance from a closed channel.
/// If the party_idx is false, withdraw is executed on behalf ob party A, else on behalf
/// of party B.
pub fn withdraw(env: Env, channel_id: BytesN<32>, party_idx: bool) -> Result<(), Error> {
// checks
pub fn withdraw(
env: Env,
channel_id: BytesN<32>,
party_idx: bool,
one_withdrawer: bool,
) -> Result<(), Error> {
// Retrieve the channel from storage
let mut channel = get_channel(&env, &channel_id).ok_or(Error::ChannelNotFound)?;

// Verify that the channel is closed.
if !channel.control.closed {
return Err(Error::WithdrawOnOpenChannel);
}
let (actor, amount) = match party_idx {

// Determine the amount to withdraw based on party_idx
let (amount, receiver) = match party_idx {
A => {
// We verify that A has not yet withdrawn (or 0 balance).
if channel.control.withdrawn_a {
return Err(Error::AlreadyFunded);
}
// We mark that A has now withdrawn.
channel.control.withdrawn_a = true; // effect

(
channel.params.a.addr.clone(),
channel.state.balances.bal_a.clone(),
channel.params.a.addr.clone(),
)
}
B => {
if channel.control.withdrawn_b {
return Err(Error::AlreadyFunded);
}
channel.control.withdrawn_b = true; // effect
(
channel.params.b.addr.clone(),
channel.state.balances.bal_b.clone(),
channel.params.b.addr.clone(),
)
}
};

// Always authenticate as party B if oneWithdrawer is true
let actor = if one_withdrawer {
channel.params.b.addr.clone()
} else {
match party_idx {
A => channel.params.a.addr.clone(),
B => channel.params.b.addr.clone(),
}
};

// Perform the authentication
actor.require_auth();

// Emit a withdraw event with the party index.
Expand All @@ -486,19 +503,7 @@ impl Adjudicator {
(channel.clone(), party_idx),
);

// effects
if is_withdrawn(&channel) {
// If the channel is withdrawn completely, emit an according event and delete it.
env.events().publish(
(symbol_short!("perun"), symbol_short!("pay_c")),
channel.clone(),
);
delete_channel(&env, &channel_id);
} else {
set_channel(&env, &channel);
}

// interact
// Perform the token transfers
let contract = env.current_contract_address();
let tokens = &channel.state.balances.tokens;

Expand All @@ -508,10 +513,29 @@ impl Adjudicator {
if let Some(amt) = amount.get(i) {
if amt > 0 {
// Transfer the correct amount to the withdrawing party.
token_client.transfer(&contract, &actor, &amt);
token_client.transfer(&contract, &receiver, &amt);
}
}
}

// Mark the appropriate party's withdrawal as complete
match party_idx {
A => channel.control.withdrawn_a = true,
B => channel.control.withdrawn_b = true,
}

// Handle channel state post-withdrawal
if is_withdrawn(&channel) {
// If the channel is completely withdrawn, emit a corresponding event and delete it.
env.events().publish(
(symbol_short!("perun"), symbol_short!("pay_c")),
channel.clone(),
);
delete_channel(&env, &channel_id);
} else {
set_channel(&env, &channel);
}

Ok(())
}

Expand Down
18 changes: 12 additions & 6 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ fn test_signature_verification() {

#[test]
fn test_honest_payment() {
let one_withdrawer = false;

let env = Env::default();

// let bal_a = vec![&env, 100, 150];
Expand Down Expand Up @@ -140,11 +142,11 @@ fn test_honest_payment() {
t.verify_state(&t.state);
t.verify_bal_contract(bal_contract_after_final);

t.client.withdraw(&t.state.channel_id, &A);
t.client.withdraw(&t.state.channel_id, &A, &one_withdrawer);
t.verify_bal_a(bal_a_after_awdraw);
t.verify_bal_contract(bal_contract_after_awdraw);

t.client.withdraw(&t.state.channel_id, &B);
t.client.withdraw(&t.state.channel_id, &B, &one_withdrawer);
t.verify_bal_b(bal_b_after_bwdraw);
t.verify_bal_contract(bal_contract_after_bwdraw);
}
Expand Down Expand Up @@ -178,6 +180,8 @@ fn test_funding_abort() {

#[test]
fn test_dispute() {
let one_withdrawer = false;

let env = Env::default();

let bal_a = vec![&env, 100, 150];
Expand Down Expand Up @@ -222,17 +226,19 @@ fn test_dispute() {
t.verify_state(&t.state);
t.verify_bal_contract(bal_contract_after_fclose);

t.client.withdraw(&t.channel_id, &A);
t.client.withdraw(&t.channel_id, &A, &one_withdrawer);
t.verify_bal_a(bal_a_after_wdraw);
t.verify_bal_contract(bal_contract_after_awdraw);

t.client.withdraw(&t.channel_id, &B);
t.client.withdraw(&t.channel_id, &B, &one_withdrawer);
t.verify_bal_b(bal_b_after_wdraw);
t.verify_bal_contract(bal_contract_after_bwdraw);
}

#[test]
fn test_malicious_dispute() {
let one_withdrawer = false;

let env = Env::default();

let bal_a = vec![&env, 100, 150];
Expand Down Expand Up @@ -288,11 +294,11 @@ fn test_malicious_dispute() {
t.verify_state(&t.state);
t.verify_bal_contract(bal_contract_after_fclose);

t.client.withdraw(&t.state.channel_id, &A);
t.client.withdraw(&t.state.channel_id, &A, &one_withdrawer);
t.verify_bal_a(bal_a_after_fwdraw);
t.verify_bal_contract(bal_contract_after_awdraw);

t.client.withdraw(&t.state.channel_id, &B);
t.client.withdraw(&t.state.channel_id, &B, &one_withdrawer);
t.verify_bal_b(bal_b_after_fwdraw);
t.verify_bal_contract(bal_contract_after_bwdraw);
}
Expand Down