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

Simplify jsonrpc constructor #2066

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 3 additions & 14 deletions raiden/network/rpc/client.py
@@ -1,13 +1,12 @@
import copy
import os
import sys
import warnings
from binascii import unhexlify
from json.decoder import JSONDecodeError

from requests import ConnectTimeout
from pkg_resources import DistributionNotFound
from web3 import Web3, HTTPProvider
from web3 import Web3
from web3.middleware import geth_poa_middleware
from web3.utils.filters import Filter
from eth_utils import (
Expand Down Expand Up @@ -188,39 +187,29 @@ class JSONRPCClient:

def __init__(
self,
host: str,
port: int,
web3: Web3,
privkey: bytes,
gasprice: int = None,
nonce_offset: int = 0,
web3: Web3 = None,
):
if privkey is None or len(privkey) != 32:
raise ValueError('Invalid private key')

endpoint = 'http://{}:{}'.format(host, port)
web3: Web3 = web3 or Web3(HTTPProvider(endpoint))

monkey_patch_web3(web3, self)

try:
version = web3.version.node
except ConnectTimeout:
raise EthNodeCommunicationError('couldnt reach the ethereum node')

supported, eth_node = is_supported_client(version)

if not supported:
print('You need a Byzantium enabled ethereum node. Parity >= 1.7.6 or Geth >= 1.7.2')
sys.exit(1)
_, eth_node = is_supported_client(version)

sender = privatekey_to_address(privkey)
transaction_count = web3.eth.getTransactionCount(to_checksum_address(sender), 'pending')
_available_nonce = transaction_count + nonce_offset

self.eth_node = eth_node
self.given_gas_price = gasprice
self.port = port
self.privkey = privkey
self.sender = sender
# Needs to be initialized to None in the beginning since JSONRPCClient
Expand Down
6 changes: 3 additions & 3 deletions raiden/network/rpc/smartcontract_proxy.py
Expand Up @@ -57,9 +57,9 @@ def inspect_client_error(val_err: ValueError, eth_node: str) -> ClientErrorInspe

class ContractProxy:
def __init__(
self,
jsonrpc_client,
contract: Contract,
self,
jsonrpc_client,
contract: Contract,
):
if contract is None:
raise ValueError('Contract must not be None')
Expand Down
26 changes: 4 additions & 22 deletions raiden/tests/integration/contracts/test_payment_channel.py
Expand Up @@ -23,25 +23,14 @@
def test_payment_channel_proxy_basics(
token_network_proxy,
private_keys,
blockchain_rpc_ports,
token_proxy,
chain_id,
web3,
):
token_network_address = to_canonical_address(token_network_proxy.proxy.contract.address)

c1_client = JSONRPCClient(
'0.0.0.0',
blockchain_rpc_ports[0],
private_keys[1],
web3=web3,
)
c2_client = JSONRPCClient(
'0.0.0.0',
blockchain_rpc_ports[0],
private_keys[2],
web3=web3,
)
c1_client = JSONRPCClient(web3, private_keys[1])
c2_client = JSONRPCClient(web3, private_keys[2])
c1_token_network_proxy = TokenNetwork(
c1_client,
token_network_address,
Expand All @@ -62,7 +51,7 @@ def test_payment_channel_proxy_basics(
channel_proxy_1 = PaymentChannel(c1_token_network_proxy, channel_identifier)
channel_proxy_2 = PaymentChannel(c2_token_network_proxy, channel_identifier)

channel_filter, unlock_filter = channel_proxy_1.all_events_filter(
channel_filter, _ = channel_proxy_1.all_events_filter(
from_block=web3.eth.blockNumber,
to_block='latest',
)
Expand Down Expand Up @@ -149,21 +138,14 @@ def test_payment_channel_proxy_basics(
def test_payment_channel_outdated_channel_close(
token_network_proxy,
private_keys,
blockchain_rpc_ports,
token_proxy,
chain_id,
web3,
):
token_network_address = to_canonical_address(token_network_proxy.proxy.contract.address)

partner = privatekey_to_address(private_keys[0])

client = JSONRPCClient(
'0.0.0.0',
blockchain_rpc_ports[0],
private_keys[1],
web3=web3,
)
client = JSONRPCClient(web3, private_keys[1])
token_network_proxy = TokenNetwork(
client,
token_network_address,
Expand Down
19 changes: 7 additions & 12 deletions raiden/tests/integration/contracts/test_token.py
@@ -1,25 +1,20 @@
from raiden.utils import privatekey_to_address
from eth_utils import to_canonical_address, to_checksum_address

from raiden.network.proxies import Token
from raiden.network.rpc.client import JSONRPCClient
from raiden.utils import privatekey_to_address


def test_token(
deploy_client,
token_proxy,
private_keys,
blockchain_rpc_ports,
web3,
deploy_client,
token_proxy,
private_keys,
web3,
):
privkey = private_keys[1]
address = privatekey_to_address(privkey)
address = to_canonical_address(address)
other_client = JSONRPCClient(
'0.0.0.0',
blockchain_rpc_ports[0],
privkey,
web3=web3,
)
other_client = JSONRPCClient(web3, privkey)
other_token_proxy = Token(
other_client,
to_canonical_address(token_proxy.proxy.contract.address),
Expand Down
45 changes: 6 additions & 39 deletions raiden/tests/integration/contracts/test_token_network.py
Expand Up @@ -29,7 +29,6 @@
def test_token_network_deposit_race(
token_network_proxy,
private_keys,
blockchain_rpc_ports,
token_proxy,
web3,
):
Expand All @@ -38,18 +37,8 @@ def test_token_network_deposit_race(

token_network_address = to_canonical_address(token_network_proxy.proxy.contract.address)

c1_client = JSONRPCClient(
'0.0.0.0',
blockchain_rpc_ports[0],
private_keys[1],
web3=web3,
)
c2_client = JSONRPCClient(
'0.0.0.0',
blockchain_rpc_ports[0],
private_keys[2],
web3=web3,
)
c1_client = JSONRPCClient(web3, private_keys[1])
c2_client = JSONRPCClient(web3, private_keys[2])
c1_token_network_proxy = TokenNetwork(
c1_client,
token_network_address,
Expand Down Expand Up @@ -77,7 +66,6 @@ def test_token_network_deposit_race(
def test_token_network_proxy_basics(
token_network_proxy,
private_keys,
blockchain_rpc_ports,
token_proxy,
chain_id,
web3,
Expand All @@ -88,18 +76,8 @@ def test_token_network_proxy_basics(

token_network_address = to_canonical_address(token_network_proxy.proxy.contract.address)

c1_client = JSONRPCClient(
'0.0.0.0',
blockchain_rpc_ports[0],
private_keys[1],
web3=web3,
)
c2_client = JSONRPCClient(
'0.0.0.0',
blockchain_rpc_ports[0],
private_keys[2],
web3=web3,
)
c1_client = JSONRPCClient(web3, private_keys[1])
c2_client = JSONRPCClient(web3, private_keys[2])
c1_token_network_proxy = TokenNetwork(
c1_client,
token_network_address,
Expand Down Expand Up @@ -249,26 +227,15 @@ def test_token_network_proxy_basics(
def test_token_network_proxy_update_transfer(
token_network_proxy,
private_keys,
blockchain_rpc_ports,
token_proxy,
chain_id,
web3,
):
"""Tests channel lifecycle, with `update_transfer` before settling"""
token_network_address = to_canonical_address(token_network_proxy.proxy.contract.address)

c1_client = JSONRPCClient(
'0.0.0.0',
blockchain_rpc_ports[0],
private_keys[1],
web3=web3,
)
c2_client = JSONRPCClient(
'0.0.0.0',
blockchain_rpc_ports[0],
private_keys[2],
web3=web3,
)
c1_client = JSONRPCClient(web3, private_keys[1])
c2_client = JSONRPCClient(web3, private_keys[2])
c1_token_network_proxy = TokenNetwork(
c1_client,
token_network_address,
Expand Down
14 changes: 2 additions & 12 deletions raiden/tests/integration/fixtures/blockchain.py
Expand Up @@ -32,9 +32,7 @@ def endpoint_discovery_services(blockchain_services, endpoint_registry_address):


@pytest.fixture(scope='session')
def ethereum_tester(
patch_genesis_gas_limit,
):
def ethereum_tester(patch_genesis_gas_limit):
"""Returns an instance of an Ethereum tester"""
tester = EthereumTester(PyEVMBackend())
tester.set_fork_block('FORK_BYZANTIUM', 0)
Expand Down Expand Up @@ -127,15 +125,7 @@ def web3(

@pytest.fixture
def deploy_client(blockchain_rpc_ports, deploy_key, web3):
host = '0.0.0.0'
rpc_port = blockchain_rpc_ports[0]

return JSONRPCClient(
host,
rpc_port,
deploy_key,
web3=web3,
)
return JSONRPCClient(web3, deploy_key)


@pytest.fixture
Expand Down
4 changes: 1 addition & 3 deletions raiden/tests/integration/rpc/test_assumptions.py
Expand Up @@ -123,10 +123,8 @@ def test_duplicated_transaction_raises(deploy_client):
assert len(deploy_client.web3.eth.getCode(to_checksum_address(address))) > 0

second_client = JSONRPCClient(
'0.0.0.0',
deploy_client.port,
deploy_client.web3,
deploy_client.privkey,
web3=deploy_client.web3,
)

second_proxy = second_client.new_contract_proxy(
Expand Down
11 changes: 2 additions & 9 deletions raiden/tests/utils/network.py
Expand Up @@ -341,21 +341,14 @@ def jsonrpc_services(
private_keys,
secret_registry_address,
token_network_registry_address,
web3=None,
web3,
):
secret_registry = deploy_service.secret_registry(secret_registry_address)
deploy_registry = deploy_service.token_network_registry(token_network_registry_address)

host = '0.0.0.0'
blockchain_services = list()
for privkey in private_keys:
rpc_client = JSONRPCClient(
host,
deploy_service.client.port,
privkey,
web3=web3,
)

rpc_client = JSONRPCClient(web3, privkey)
blockchain = BlockChainService(privkey, rpc_client)
blockchain_services.append(blockchain)

Expand Down
9 changes: 2 additions & 7 deletions raiden/tests/utils/smoketest.py
Expand Up @@ -312,13 +312,8 @@ def setup_testchain_and_raiden(smoketest_config, transport, matrix_server, print
geth_wait_and_check(web3_client, privatekeys, random_marker)

print_step('Deploying Raiden contracts')
host = '0.0.0.0'
client = JSONRPCClient(
host,
ethereum_config['rpc'],
get_private_key(),
web3=web3_client,
)

client = JSONRPCClient(web3_client, get_private_key())
contract_addresses = deploy_smoketest_contracts(client, 627)
token_contract = deploy_token(client)
token = token_contract(1000, 0, 'TKN', 'TKN')
Expand Down
22 changes: 16 additions & 6 deletions raiden/ui/cli.py
Expand Up @@ -30,7 +30,8 @@
to_normalized_address,
)
from mirakuru import ProcessExitedWithError
from requests.exceptions import RequestException
from requests.exceptions import ConnectTimeout, RequestException
from web3 import Web3, HTTPProvider

from raiden import constants
from raiden.accounts import AccountManager
Expand Down Expand Up @@ -62,8 +63,8 @@
)
from raiden.tasks import check_version, check_gas_reserve
from raiden.utils import (
eth_endpoint_to_hostport,
get_system_spec,
is_supported_client,
merge_dict,
split_endpoint,
typing,
Expand Down Expand Up @@ -597,13 +598,22 @@ def run_app(
privatekey_hex = hexlify(privatekey_bin)
config['privatekey_hex'] = privatekey_hex

rpc_host, rpc_port = eth_endpoint_to_hostport(eth_rpc_endpoint)
web3 = Web3(HTTPProvider(eth_rpc_endpoint))

try:
node_version = web3.version.node # pylint: disable=no-member
except ConnectTimeout:
raise EthNodeCommunicationError("Couldn't connect to the ethereum node")

supported, _ = is_supported_client(node_version)
if not supported:
print('You need a Byzantium enabled ethereum node. Parity >= 1.7.6 or Geth >= 1.7.2')
sys.exit(1)

rpc_client = JSONRPCClient(
rpc_host,
rpc_port,
web3,
privatekey_bin,
gas_price,
gasprice=gas_price,
)

blockchain_service = BlockChainService(privatekey_bin, rpc_client)
Expand Down