Skip to content

Commit

Permalink
Remove mostly duplicate functions
Browse files Browse the repository at this point in the history
create_balance_proof_closing_countersignature() and
create_balance_proof_updating_countersignature()
into create_balance_proof_countersignature()
  • Loading branch information
pirapira committed Aug 13, 2019
1 parent c47f840 commit 83b62b9
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 186 deletions.
50 changes: 8 additions & 42 deletions raiden_contracts/tests/fixtures/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,7 @@ def get(
def close_and_update_channel(
token_network: Contract,
create_balance_proof: Callable,
create_balance_proof_closing_countersignature: Callable,
create_balance_proof_updating_countersignature: Callable,
create_balance_proof_countersignature: Callable,
) -> Callable:
def get(
channel_identifier: int,
Expand Down Expand Up @@ -203,11 +202,11 @@ def get(
participant2_values.locksroot,
additional_hash2,
)
balance_proof_close_signature_1 = create_balance_proof_closing_countersignature(
participant1, channel_identifier, *balance_proof_2
balance_proof_close_signature_1 = create_balance_proof_countersignature(
participant1, channel_identifier, MessageTypeId.BALANCE_PROOF, *balance_proof_2
)
balance_proof_update_signature_2 = create_balance_proof_updating_countersignature(
participant2, channel_identifier, *balance_proof_1
balance_proof_update_signature_2 = create_balance_proof_countersignature(
participant2, channel_identifier, MessageTypeId.BALANCE_PROOF_UPDATE, *balance_proof_1
)

token_network.functions.closeChannel(
Expand Down Expand Up @@ -660,12 +659,13 @@ def get(


@pytest.fixture(scope="session")
def create_balance_proof_closing_countersignature(
def create_balance_proof_countersignature(
token_network: Contract, get_private_key: Callable
) -> Callable:
def get(
participant: HexAddress,
channel_identifier: int,
msg_type: MessageTypeId,
balance_hash: bytes,
nonce: int,
additional_hash: bytes,
Expand All @@ -681,41 +681,7 @@ def get(
_token_network.address,
int(_token_network.functions.chain_id().call()),
channel_identifier,
MessageTypeId.BALANCE_PROOF,
balance_hash,
nonce,
additional_hash,
original_signature,
v,
)
return non_closing_signature

return get


@pytest.fixture(scope="session")
def create_balance_proof_updating_countersignature(
token_network: Contract, get_private_key: Callable
) -> Callable:
def get(
participant: HexAddress,
channel_identifier: int,
balance_hash: bytes,
nonce: int,
additional_hash: bytes,
original_signature: bytes,
v: int = 27,
other_token_network: Optional[Contract] = None,
) -> bytes:
_token_network = other_token_network or token_network
private_key = get_private_key(participant)

non_closing_signature = sign_balance_proof_message(
private_key,
_token_network.address,
int(_token_network.functions.chain_id().call()),
channel_identifier,
MessageTypeId.BALANCE_PROOF_UPDATE,
msg_type,
balance_hash,
nonce,
additional_hash,
Expand Down
51 changes: 28 additions & 23 deletions raiden_contracts/tests/test_channel_close.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
from web3 import Web3
from web3.contract import Contract

from raiden_contracts.constants import TEST_SETTLE_TIMEOUT_MIN, ChannelEvent, ChannelState
from raiden_contracts.constants import (
TEST_SETTLE_TIMEOUT_MIN,
ChannelEvent,
ChannelState,
MessageTypeId,
)
from raiden_contracts.tests.utils import (
EMPTY_ADDITIONAL_HASH,
EMPTY_BALANCE_HASH,
Expand Down Expand Up @@ -107,7 +112,7 @@ def test_close_wrong_signature(
channel_deposit: Callable,
get_accounts: Callable,
create_balance_proof: Callable,
create_balance_proof_closing_countersignature: Callable,
create_balance_proof_countersignature: Callable,
) -> None:
""" Closing a channel with a balance proof of the third party should fail """
(A, B, C) = get_accounts(3)
Expand All @@ -123,8 +128,8 @@ def test_close_wrong_signature(
balance_proof = create_balance_proof(
channel_identifier, C, transferred_amount, 0, nonce, locksroot
)
closing_signature_A = create_balance_proof_closing_countersignature(
A, channel_identifier, *balance_proof
closing_signature_A = create_balance_proof_countersignature(
A, channel_identifier, MessageTypeId.BALANCE_PROOF, *balance_proof
)

with pytest.raises(TransactionFailed):
Expand Down Expand Up @@ -201,7 +206,7 @@ def test_close_nonce_zero(
token_network: Contract,
create_channel: Callable,
create_balance_proof: Callable,
create_balance_proof_closing_countersignature: Callable,
create_balance_proof_countersignature: Callable,
event_handler: Callable,
) -> None:
""" closeChannel with a balance proof with nonce zero should not change the channel state """
Expand All @@ -225,8 +230,8 @@ def test_close_nonce_zero(
vals_B.nonce,
vals_B.locksroot,
)
close_sig_A = create_balance_proof_closing_countersignature(
A, channel_identifier, *balance_proof_B
close_sig_A = create_balance_proof_countersignature(
A, channel_identifier, MessageTypeId.BALANCE_PROOF, *balance_proof_B
)

(
Expand Down Expand Up @@ -277,7 +282,7 @@ def test_close_first_argument_is_for_partner_transfer(
create_channel: Callable,
get_accounts: Callable,
create_balance_proof: Callable,
create_balance_proof_closing_countersignature: Callable,
create_balance_proof_countersignature: Callable,
) -> None:
""" closeChannel fails on a self-submitted balance proof """
(A, B) = get_accounts(2)
Expand All @@ -287,11 +292,11 @@ def test_close_first_argument_is_for_partner_transfer(

# Create balance proofs
balance_proof = create_balance_proof(channel_identifier, B)
closing_sig_A = create_balance_proof_closing_countersignature(
A, channel_identifier, *balance_proof
closing_sig_A = create_balance_proof_countersignature(
A, channel_identifier, MessageTypeId.BALANCE_PROOF, *balance_proof
)
closing_sig_B = create_balance_proof_closing_countersignature(
B, channel_identifier, *balance_proof
closing_sig_B = create_balance_proof_countersignature(
B, channel_identifier, MessageTypeId.BALANCE_PROOF, *balance_proof
)

# closeChannel fails, if the provided balance proof is from the same participant who closes
Expand Down Expand Up @@ -397,7 +402,7 @@ def test_close_channel_state(
get_block: Callable,
create_balance_proof: Callable,
txn_cost: Callable,
create_balance_proof_closing_countersignature: Callable,
create_balance_proof_countersignature: Callable,
) -> None:
""" Observe the effect of a successful closeChannel
Expand Down Expand Up @@ -466,8 +471,8 @@ def test_close_channel_state(
vals_B.nonce,
vals_B.locksroot,
)
closing_sig_A = create_balance_proof_closing_countersignature(
A, channel_identifier, *balance_proof_B
closing_sig_A = create_balance_proof_countersignature(
A, channel_identifier, MessageTypeId.BALANCE_PROOF, *balance_proof_B
)

txn_hash = token_network.functions.closeChannel(
Expand Down Expand Up @@ -558,7 +563,7 @@ def test_close_replay_reopened_channel(
create_channel: Callable,
channel_deposit: Callable,
create_balance_proof: Callable,
create_balance_proof_closing_countersignature: Callable,
create_balance_proof_countersignature: Callable,
) -> None:
""" The same balance proof cannot close another channel between the same participants """
(A, B) = get_accounts(2)
Expand All @@ -576,8 +581,8 @@ def test_close_replay_reopened_channel(
nonce,
values_B.locksroot,
)
closing_sig_A = create_balance_proof_closing_countersignature(
A, channel_identifier1, *balance_proof_B
closing_sig_A = create_balance_proof_countersignature(
A, channel_identifier1, MessageTypeId.BALANCE_PROOF, *balance_proof_B
)
token_network.functions.closeChannel(
channel_identifier1, B, A, *balance_proof_B, closing_sig_A
Expand Down Expand Up @@ -614,8 +619,8 @@ def test_close_replay_reopened_channel(
nonce,
values_B.locksroot,
)
closing_sig_A2 = create_balance_proof_closing_countersignature(
A, channel_identifier2, *balance_proof_B2
closing_sig_A2 = create_balance_proof_countersignature(
A, channel_identifier2, MessageTypeId.BALANCE_PROOF, *balance_proof_B2
)
token_network.functions.closeChannel(
channel_identifier2, B, A, *balance_proof_B2, closing_sig_A2
Expand All @@ -629,7 +634,7 @@ def test_close_channel_event(
channel_deposit: Callable,
create_balance_proof: Callable,
event_handler: Callable,
create_balance_proof_closing_countersignature: Callable,
create_balance_proof_countersignature: Callable,
) -> None:
""" A successful closeChannel call produces a CLOSED event """
ev_handler = event_handler(token_network)
Expand All @@ -641,8 +646,8 @@ def test_close_channel_event(
balance_proof = create_balance_proof(
channel_identifier, B, transferred_amount=5, locked_amount=0, nonce=3
)
close_sig = create_balance_proof_closing_countersignature(
A, channel_identifier, *balance_proof
close_sig = create_balance_proof_countersignature(
A, channel_identifier, MessageTypeId.BALANCE_PROOF, *balance_proof
)

txn_hash = token_network.functions.closeChannel(
Expand Down
30 changes: 17 additions & 13 deletions raiden_contracts/tests/test_channel_settle.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
from web3 import Web3
from web3.contract import Contract

from raiden_contracts.constants import TEST_SETTLE_TIMEOUT_MIN, ChannelEvent, ChannelState
from raiden_contracts.constants import (
TEST_SETTLE_TIMEOUT_MIN,
ChannelEvent,
ChannelState,
MessageTypeId,
)
from raiden_contracts.tests.fixtures.channel import call_settle
from raiden_contracts.tests.utils import (
EMPTY_ADDITIONAL_HASH,
Expand Down Expand Up @@ -160,7 +165,7 @@ def test_settle_single_direct_transfer_for_closing_party(
create_channel: Callable,
channel_deposit: Callable,
create_balance_proof: Callable,
create_balance_proof_closing_countersignature: Callable,
create_balance_proof_countersignature: Callable,
) -> None:
""" Test settle of a channel with one direct transfer to the participant
that called close.
Expand All @@ -184,8 +189,8 @@ def test_settle_single_direct_transfer_for_closing_party(
1,
LOCKSROOT_OF_NO_LOCKS,
)
closing_sig_A = create_balance_proof_closing_countersignature(
A, channel_identifier, *balance_proof_B
closing_sig_A = create_balance_proof_countersignature(
A, channel_identifier, MessageTypeId.BALANCE_PROOF, *balance_proof_B
)
token_network.functions.closeChannel(
channel_identifier, B, A, *balance_proof_B, closing_sig_A
Expand Down Expand Up @@ -230,7 +235,7 @@ def test_settle_single_direct_transfer_for_counterparty(
create_channel: Callable,
channel_deposit: Callable,
create_balance_proof: Callable,
create_balance_proof_updating_countersignature: Callable,
create_balance_proof_countersignature: Callable,
create_close_signature_for_no_balance_proof: Callable,
) -> None:
""" Test settle of a channel with one direct transfer to the participant
Expand Down Expand Up @@ -267,8 +272,8 @@ def test_settle_single_direct_transfer_for_counterparty(
LOCKSROOT_OF_NO_LOCKS,
)

balance_proof_update_signature_B = create_balance_proof_updating_countersignature(
B, channel_identifier, *balance_proof_A
balance_proof_update_signature_B = create_balance_proof_countersignature(
B, channel_identifier, MessageTypeId.BALANCE_PROOF_UPDATE, *balance_proof_A
)
token_network.functions.updateNonClosingBalanceProof(
channel_identifier, A, B, *balance_proof_A, balance_proof_update_signature_B
Expand Down Expand Up @@ -571,8 +576,7 @@ def test_settle_channel_event(
create_channel: Callable,
channel_deposit: Callable,
create_balance_proof: Callable,
create_balance_proof_closing_countersignature: Callable,
create_balance_proof_updating_countersignature: Callable,
create_balance_proof_countersignature: Callable,
event_handler: Callable,
) -> None:
""" A successful settleChannel() call causes a SETTLED event """
Expand All @@ -586,11 +590,11 @@ def test_settle_channel_event(

balance_proof_A = create_balance_proof(channel_identifier, A, 10, 0, 1, LOCKSROOT_OF_NO_LOCKS)
balance_proof_B = create_balance_proof(channel_identifier, B, 5, 0, 3, LOCKSROOT_OF_NO_LOCKS)
balance_proof_update_signature_B = create_balance_proof_updating_countersignature(
B, channel_identifier, *balance_proof_A
balance_proof_update_signature_B = create_balance_proof_countersignature(
B, channel_identifier, MessageTypeId.BALANCE_PROOF_UPDATE, *balance_proof_A
)
close_sig_A = create_balance_proof_closing_countersignature(
A, channel_identifier, *balance_proof_B
close_sig_A = create_balance_proof_countersignature(
A, channel_identifier, MessageTypeId.BALANCE_PROOF, *balance_proof_B
)

token_network.functions.closeChannel(
Expand Down
Loading

0 comments on commit 83b62b9

Please sign in to comment.