Skip to content

Commit

Permalink
Simplified JSONRPCClient constructor
Browse files Browse the repository at this point in the history
The arguments host and port were ignored the argument web3 was given,
this could lead to inconsistencies among the web3 instance and the
JSONRPCClient's attributes host/port. This change removes the extranous
arguments and requires the web3 instance.

[ci integration]
  • Loading branch information
hackaugusto committed Aug 6, 2018
1 parent acf7eec commit 54b5be5
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 141 deletions.
10 changes: 2 additions & 8 deletions raiden/network/rpc/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

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,19 +188,14 @@ 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:
Expand All @@ -220,7 +215,6 @@ def __init__(

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
9 changes: 4 additions & 5 deletions raiden/ui/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
)
from mirakuru import ProcessExitedWithError
from requests.exceptions import RequestException
from web3 import Web3, HTTPProvider

from raiden import constants
from raiden.accounts import AccountManager
Expand Down Expand Up @@ -62,7 +63,6 @@
)
from raiden.tasks import check_version, check_gas_reserve
from raiden.utils import (
eth_endpoint_to_hostport,
get_system_spec,
merge_dict,
split_endpoint,
Expand Down Expand Up @@ -597,13 +597,12 @@ 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))

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

blockchain_service = BlockChainService(privatekey_bin, rpc_client)
Expand Down
8 changes: 5 additions & 3 deletions tools/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import click
import structlog
from eth_utils import to_checksum_address
from web3 import Web3, HTTPProvider

from raiden.log_config import configure_logging
from raiden.network.rpc.client import JSONRPCClient
Expand Down Expand Up @@ -33,6 +34,7 @@ class RemoveLibraryDeref(ast.NodeTransformer):
Removes the AST node representing the line
` libraries = dict(libraries)`
"""

def visit_Assign(self, node): # pylint: disable=no-self-use
is_libraries = (
len(node.targets) == 1 and
Expand Down Expand Up @@ -84,11 +86,11 @@ def main(keystore_path, pretty, gas_price, port):
gas_price_in_wei = gas_price * 1000000000
patch_deploy_solidity_contract()
host = '127.0.0.1'
web3 = Web3(HTTPProvider(f'http://{host}:{port}'))
client = JSONRPCClient(
host,
port,
web3,
privatekey,
gas_price_in_wei,
gasprice=gas_price_in_wei,
)

deployed = deploy_contracts(client)
Expand Down
11 changes: 5 additions & 6 deletions tools/init_blockchain.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import gevent

from web3 import Web3, HTTPProvider
from eth_utils import to_checksum_address, encode_hex

from raiden.network.rpc.client import JSONRPCClient
from raiden.utils import get_contract_path, sha3
from raiden.utils.solc import compile_files_cwd


def connect(host='127.0.0.1', port=8545):
"""Create a jsonrpcclient instance, using the 'zero-privatekey'. """
client = JSONRPCClient(
host,
port,
privkey=b'1' * 64,
)
privkey = b'1' * 64
web3 = Web3(HTTPProvider(f'http://{host}:{port}'))
client = JSONRPCClient(web3, privkey)
return client


Expand Down

0 comments on commit 54b5be5

Please sign in to comment.