Skip to content

Commit

Permalink
Use the local address implicitly for NettingChannel creation
Browse files Browse the repository at this point in the history
  • Loading branch information
palango authored and hackaugusto committed Jan 19, 2018
1 parent eb065ae commit 5e7416b
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 46 deletions.
1 change: 0 additions & 1 deletion raiden/api/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ def open(
assert token_address in self.raiden.token_to_channelgraph

netcontract_address = channel_manager.new_netting_channel(
self.raiden.address,
partner_address,
settle_timeout,
)
Expand Down
33 changes: 15 additions & 18 deletions raiden/network/proxies/channel_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,9 @@ def token_address(self):
""" Return the token of this manager. """
return address_decoder(self.proxy.call('tokenAddress'))

def new_netting_channel(self, peer1, peer2, settle_timeout):
if not isaddress(peer1):
raise ValueError('The peer1 must be a valid address')

if not isaddress(peer2):
raise ValueError('The peer2 must be a valid address')
def new_netting_channel(self, other_peer, settle_timeout):
if not isaddress(other_peer):
raise ValueError('The other_peer must be a valid address')

invalid_timeout = (
settle_timeout < NETTINGCHANNEL_SETTLE_TIMEOUT_MIN or
Expand All @@ -87,20 +84,16 @@ def new_netting_channel(self, peer1, peer2, settle_timeout):
NETTINGCHANNEL_SETTLE_TIMEOUT_MIN, NETTINGCHANNEL_SETTLE_TIMEOUT_MAX
))

if peer1 == peer2:
raise SamePeerAddress('Peer1 and peer2 must not be equal')

if privatekey_to_address(self.client.privkey) == peer1:
other = peer2
else:
other = peer1
local_address = privatekey_to_address(self.client.privkey)
if local_address == other_peer:
raise SamePeerAddress('The other peer must not have the same address as the client.')

transaction_hash = estimate_and_transact(
self.proxy,
'newChannel',
self.startgas,
self.gasprice,
other,
other_peer,
settle_timeout,
)

Expand All @@ -111,24 +104,28 @@ def new_netting_channel(self, peer1, peer2, settle_timeout):

netting_channel_results_encoded = self.proxy.call(
'getChannelWith',
other,
other_peer,
startgas=self.startgas,
)

# address is at index 0
netting_channel_address_encoded = netting_channel_results_encoded

if not netting_channel_address_encoded:
log.error('netting_channel_address failed', peer1=pex(peer1), peer2=pex(peer2))
log.error(
'netting_channel_address failed',
peer1=pex(local_address),
peer2=pex(other_peer)
)
raise RuntimeError('netting_channel_address failed')

netting_channel_address_bin = address_decoder(netting_channel_address_encoded)

if log.isEnabledFor(logging.INFO):
log.info(
'new_netting_channel called',
peer1=pex(peer1),
peer2=pex(peer2),
peer1=pex(local_address),
peer2=pex(other_peer),
netting_channel=pex(netting_channel_address_bin),
)

Expand Down
8 changes: 1 addition & 7 deletions raiden/tests/integration/test_blockchainservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ def test_new_netting_contract(raiden_network, token_amount, settle_timeout):

# create one channel
netting_address_01 = manager0.new_netting_channel(
peer0_address,
peer1_address,
settle_timeout,
)
Expand All @@ -80,13 +79,11 @@ def test_new_netting_contract(raiden_network, token_amount, settle_timeout):
# is still open should throw an exception
with pytest.raises(Exception):
manager0.new_netting_channel(
peer0_address,
peer1_address,
settle_timeout,
)
# create other channel
netting_address_02 = manager0.new_netting_channel(
peer0_address,
peer2_address,
settle_timeout,
)
Expand Down Expand Up @@ -163,7 +160,6 @@ def test_new_netting_contract(raiden_network, token_amount, settle_timeout):

# open channel with same peer again
netting_address_01_reopened = manager0.new_netting_channel(
peer0_address,
peer1_address,
settle_timeout,
)
Expand Down Expand Up @@ -192,7 +188,6 @@ def test_channelmanager_graph_building(
for app0, app1 in pairs:
manager = app0.raiden.default_registry.manager_by_token(token_address)
manager.new_netting_channel(
app0.raiden.address,
app1.raiden.address,
settle_timeout,
)
Expand Down Expand Up @@ -342,12 +337,11 @@ def test_channel_with_self(raiden_network, settle_timeout):

with pytest.raises(SamePeerAddress) as excinfo:
graph0.new_netting_channel(
app0.raiden.address,
app0.raiden.address,
settle_timeout,
)

assert 'Peer1 and peer2 must not be equal' in str(excinfo.value)
assert 'The other peer must not have the same address as the client.' in str(excinfo.value)

transaction_hash = graph0.proxy.transact(
'newChannel',
Expand Down
2 changes: 0 additions & 2 deletions raiden/tests/integration/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ def test_event_new_channel(raiden_chain, deposit, settle_timeout, events_poll_ti
token1 = app1.raiden.chain.token(token_address)

netcontract_address = graph0.new_netting_channel(
app0.raiden.address,
app1.raiden.address,
settle_timeout,
)
Expand Down Expand Up @@ -157,7 +156,6 @@ def test_query_events(raiden_chain, deposit, settle_timeout, events_poll_timeout
assert not events

netcontract_address = manager0.new_netting_channel(
app0.raiden.address,
app1.raiden.address,
settle_timeout,
)
Expand Down
1 change: 0 additions & 1 deletion raiden/tests/utils/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def setup_channels(token_address, app_pairs, deposit, settle_timeout):
manager = first.raiden.default_registry.manager_by_token(token_address)

netcontract_address = manager.new_netting_channel(
first.raiden.address,
second.raiden.address,
settle_timeout,
)
Expand Down
2 changes: 0 additions & 2 deletions raiden/tests/utils/smoketest.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,8 @@ def deploy_and_open_channel_alloc(deployment_key):

manager = registry.manager_by_token(token_address)
assert manager.private_key == deployment_key_bin
our_address = TEST_ACCOUNT['address']

channel_address = manager.new_netting_channel(
unhexlify(our_address),
unhexlify(TEST_PARTNER_ADDRESS),
50
)
Expand Down
23 changes: 8 additions & 15 deletions raiden/tests/utils/tester_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,20 +507,18 @@ def token_address(self):
token_address = address_decoder(token_address_hex)
return token_address

def new_netting_channel(self, peer1, peer2, settle_timeout):
def new_netting_channel(self, other_peer, settle_timeout):
""" Creates a new netting contract between peer1 and peer2.
Raises:
ValueError: If peer1 or peer2 is not a valid address.
ValueError: If other_peer is not a valid address.
"""
if not isaddress(peer1):
raise ValueError('The peer1 must be a valid address')
if not isaddress(other_peer):
raise ValueError('The other_peer must be a valid address')

if not isaddress(peer2):
raise ValueError('The peer2 must be a valid address')

if peer1 == peer2:
raise SamePeerAddress('peer1 and peer2 must not be equal')
local_address = privatekey_to_address(self.private_key)
if local_address == other_peer:
raise SamePeerAddress('The other peer must not have the same address as the client.')

invalid_timeout = (
settle_timeout < NETTINGCHANNEL_SETTLE_TIMEOUT_MIN or
Expand All @@ -531,14 +529,9 @@ def new_netting_channel(self, peer1, peer2, settle_timeout):
NETTINGCHANNEL_SETTLE_TIMEOUT_MIN, NETTINGCHANNEL_SETTLE_TIMEOUT_MAX
))

if privatekey_to_address(self.private_key) == peer1:
other = peer2
else:
other = peer1

try:
netting_channel_address_hex = self.proxy.newChannel(
other,
other_peer,
settle_timeout,
sender=self.private_key
)
Expand Down

0 comments on commit 5e7416b

Please sign in to comment.