Skip to content

Commit

Permalink
initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
palango committed Jun 22, 2021
1 parent 39a6d57 commit 142cec5
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 53 deletions.
54 changes: 54 additions & 0 deletions raiden_contracts/data/source/raiden/TokenNetwork.sol
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,61 @@ contract TokenNetwork is Utils {
// balance proof in storage. This should never fail as we use isOpen.
assert(participant_state.nonce == 0);
assert(partner_state.nonce == 0);
}

struct CoopData {
address participant;
uint256 total_withdraw;
uint256 expiration_block;
bytes participant_signature;
bytes partner_signature;
}

function cooperativeSettle(
uint256 channel_identifier,
CoopData memory data1,
CoopData memory data2
)
external
isOpen(channel_identifier)
{
bytes32 pair_hash;

require(channel_identifier == getChannelIdentifier(data1.participant, data2.participant), "P1");

this.setTotalWithdraw(
channel_identifier,
data1.participant,
data1.total_withdraw,
data1.expiration_block,
data1.participant_signature,
data1.partner_signature
);
this.setTotalWithdraw(
channel_identifier,
data2.participant,
data2.total_withdraw,
data2.expiration_block,
data2.participant_signature,
data2.partner_signature
);

// Validate that authenticated partners and the channel identifier match
require(channel_identifier == getChannelIdentifier(data1.participant, data2.participant));

// Remove channel data from storage
Channel storage channel = channels[channel_identifier];
delete channel.participants[data1.participant];
delete channel.participants[data2.participant];
delete channels[channel_identifier];

// Remove the pair's channel counter
pair_hash = getParticipantsHash(data1.participant, data2.participant);
delete participants_hash_to_channel_identifier[pair_hash];

// Emit channel lifecycle events
emit ChannelClosed(channel_identifier, data1.participant, 0, 0); // FIXME: nonce?
emit ChannelSettled(channel_identifier, 0, 0, 0, 0); // FIXME
}

/// @notice Close the channel defined by the two participant addresses.
Expand Down
114 changes: 61 additions & 53 deletions raiden_contracts/tests/test_channel_cooperative_settle.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
from raiden_contracts.utils.events import check_channel_settled


@pytest.mark.skip(reason="Delayed until another milestone")
def test_cooperative_settle_channel_call(
token_network: Contract,
create_channel_and_deposit: Callable,
get_accounts: Callable,
create_cooperative_settle_signatures: Callable,
create_withdraw_signatures: Callable,
) -> None:
(A, B, C) = get_accounts(3)
deposit_A = 20
Expand All @@ -28,66 +28,74 @@ def test_cooperative_settle_channel_call(
(signature_A, signature_B) = create_cooperative_settle_signatures(
[A, B], channel_identifier, A, balance_A, B, balance_B
)
(signature_A1, signature_B1) = create_withdraw_signatures(
[A, B], channel_identifier, A, balance_A, 100
)
(signature_A2, signature_B2) = create_withdraw_signatures(
[A, B], channel_identifier, B, balance_B, 100
)

with pytest.raises(ValidationError):
token_network.functions.cooperativeSettle(
channel_identifier, A, -1, B, balance_B, signature_A, signature_B
)
with pytest.raises(ValidationError):
token_network.functions.cooperativeSettle(
channel_identifier, A, balance_A, B, -1, signature_A, signature_B
)
with pytest.raises(ValidationError):
token_network.functions.cooperativeSettle(
channel_identifier, 0x0, balance_A, B, balance_B, signature_A, signature_B
)
with pytest.raises(ValidationError):
token_network.functions.cooperativeSettle(
channel_identifier, A, balance_A, 0x0, balance_B, signature_A, signature_B
)
with pytest.raises(ValidationError):
token_network.functions.cooperativeSettle(
channel_identifier, A, balance_A, B, balance_B, 0x0, signature_B
)
with pytest.raises(ValidationError):
token_network.functions.cooperativeSettle(
channel_identifier, A, balance_A, B, balance_B, signature_A, 0x0
)

with pytest.raises(TransactionFailed):
call_and_transact(
token_network.functions.cooperativeSettle(
channel_identifier,
EMPTY_ADDRESS,
balance_A,
B,
balance_B,
signature_A,
signature_B,
),
{"from": C},
)
with pytest.raises(TransactionFailed):
call_and_transact(
token_network.functions.cooperativeSettle(
channel_identifier,
A,
balance_A,
EMPTY_ADDRESS,
balance_B,
signature_A,
signature_B,
),
{"from": C},
)
# with pytest.raises(ValidationError):
# token_network.functions.cooperativeSettle(
# channel_identifier, A, -1, B, balance_B, signature_A, signature_B
# )
# with pytest.raises(ValidationError):
# token_network.functions.cooperativeSettle(
# channel_identifier, A, balance_A, B, -1, signature_A, signature_B
# )
# with pytest.raises(ValidationError):
# token_network.functions.cooperativeSettle(
# channel_identifier, 0x0, balance_A, B, balance_B, signature_A, signature_B
# )
# with pytest.raises(ValidationError):
# token_network.functions.cooperativeSettle(
# channel_identifier, A, balance_A, 0x0, balance_B, signature_A, signature_B
# )
# with pytest.raises(ValidationError):
# token_network.functions.cooperativeSettle(
# channel_identifier, A, balance_A, B, balance_B, 0x0, signature_B
# )
# with pytest.raises(ValidationError):
# token_network.functions.cooperativeSettle(
# channel_identifier, A, balance_A, B, balance_B, signature_A, 0x0
# )
#
# with pytest.raises(TransactionFailed):
# call_and_transact(
# token_network.functions.cooperativeSettle(
# channel_identifier,
# EMPTY_ADDRESS,
# balance_A,
# B,
# balance_B,
# signature_A,
# signature_B,
# ),
# {"from": C},
# )
# with pytest.raises(TransactionFailed):
# call_and_transact(
# token_network.functions.cooperativeSettle(
# channel_identifier,
# A,
# balance_A,
# EMPTY_ADDRESS,
# balance_B,
# signature_A,
# signature_B,
# ),
# {"from": C},
# )

call_and_transact(
token_network.functions.cooperativeSettle(
channel_identifier, A, balance_A, B, balance_B, signature_A, signature_B
channel_identifier, (A, balance_A, 100, signature_A1, signature_B1), (B, balance_B, 100, signature_B2, signature_A2)
),
{"from": C},
)

# breakpoint()


@pytest.mark.skip(reason="Delayed until another milestone")
def test_cooperative_settle_channel_signatures(
Expand Down

0 comments on commit 142cec5

Please sign in to comment.