Skip to content

Commit

Permalink
Check the onchain bytecode
Browse files Browse the repository at this point in the history
  • Loading branch information
pirapira committed Jul 2, 2019
1 parent 2c9378c commit 5cb5d04
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 14 deletions.
9 changes: 6 additions & 3 deletions raiden/network/proxies/secret_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ def __init__(self, jsonrpc_client, secret_registry_address, contract_manager: Co
raise InvalidAddress("Expected binary address format for secret registry")

self.contract_manager = contract_manager
check_address_has_code(jsonrpc_client, secret_registry_address, CONTRACT_SECRET_REGISTRY)
check_address_has_code(
jsonrpc_client,
secret_registry_address,
CONTRACT_SECRET_REGISTRY,
expected_code=contract_manager.get_runtime_hexcode(CONTRACT_SECRET_REGISTRY),
)

proxy = jsonrpc_client.new_contract_proxy(
self.contract_manager.get_contract_abi(CONTRACT_SECRET_REGISTRY),
Expand All @@ -59,8 +64,6 @@ def __init__(self, jsonrpc_client, secret_registry_address, contract_manager: Co
# There should be only one smart contract deployed, to avoid race
# conditions for on-chain unlocks.

# TODO: check what's onchain

self.address = secret_registry_address
self.proxy = proxy
self.client = jsonrpc_client
Expand Down
9 changes: 6 additions & 3 deletions raiden/network/proxies/service_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@ def __init__(
raise InvalidAddress("Expected binary address for service registry")

self.contract_manager = contract_manager
check_address_has_code(jsonrpc_client, service_registry_address, CONTRACT_SERVICE_REGISTRY)
check_address_has_code(
jsonrpc_client,
service_registry_address,
CONTRACT_SERVICE_REGISTRY,
expected_code=contract_manager.get_runtime_hexcode(CONTRACT_SERVICE_REGISTRY),
)

proxy = jsonrpc_client.new_contract_proxy(
self.contract_manager.get_contract_abi(CONTRACT_SERVICE_REGISTRY),
to_canonical_address(service_registry_address),
)

# TODO: check what's onchain

self.address = service_registry_address
self.proxy = proxy
self.client = jsonrpc_client
Expand Down
2 changes: 1 addition & 1 deletion raiden/network/proxies/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __init__(
if not is_binary_address(token_address):
raise ValueError("token_address must be a valid address")

check_address_has_code(jsonrpc_client, Address(token_address), "Token")
check_address_has_code(jsonrpc_client, Address(token_address), "Token", expected_code=None)

self.address = token_address
self.client = jsonrpc_client
Expand Down
7 changes: 4 additions & 3 deletions raiden/network/proxies/token_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@ def __init__(
raise InvalidAddress("Expected binary address format for token nework")

check_address_has_code(
jsonrpc_client, Address(token_network_address), CONTRACT_TOKEN_NETWORK
jsonrpc_client,
Address(token_network_address),
CONTRACT_TOKEN_NETWORK,
expected_code=contract_manager.get_runtime_hexcode(CONTRACT_TOKEN_NETWORK),
)

self.contract_manager = contract_manager
Expand All @@ -163,8 +166,6 @@ def __init__(
to_normalized_address(token_network_address),
)

# TODO: check what's onchain

self.gas_measurements = gas_measurements(self.contract_manager.contracts_version)

self.address = token_network_address
Expand Down
3 changes: 1 addition & 2 deletions raiden/network/proxies/token_network_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def __init__(
client=jsonrpc_client,
address=Address(registry_address),
contract_name=CONTRACT_TOKEN_NETWORK_REGISTRY,
expected_code=contract_manager.get_runtime_hexcode(CONTRACT_TOKEN_NETWORK_REGISTRY),
)

self.contract_manager = contract_manager
Expand All @@ -67,8 +68,6 @@ def __init__(
to_normalized_address(registry_address),
)

# TODO: check what's onchain

self.gas_measurements = gas_measurements(self.contract_manager.contracts_version)

self.blockchain_service = blockchain_service
Expand Down
5 changes: 4 additions & 1 deletion raiden/network/proxies/user_deposit.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ def __init__(
raise InvalidAddress("Expected binary address format for token nework")

check_address_has_code(
jsonrpc_client, Address(user_deposit_address), CONTRACT_USER_DEPOSIT
jsonrpc_client,
Address(user_deposit_address),
CONTRACT_USER_DEPOSIT,
expected_code=contract_manager.get_runtime_hexcode(CONTRACT_USER_DEPOSIT),
)

self.client = jsonrpc_client
Expand Down
10 changes: 9 additions & 1 deletion raiden/network/rpc/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from raiden import constants
from raiden.exceptions import (
AddressWithoutCode,
ContractCodeMismatch,
EthNodeCommunicationError,
EthNodeInterfaceError,
InsufficientFunds,
Expand Down Expand Up @@ -166,7 +167,9 @@ def geth_discover_next_available_nonce(web3: Web3, address: AddressHex) -> Nonce
return web3.eth.getTransactionCount(address, "latest")


def check_address_has_code(client: "JSONRPCClient", address: Address, contract_name: str = ""):
def check_address_has_code(
client: "JSONRPCClient", address: Address, contract_name: str = "", expected_code: str = None
):
""" Checks that the given address contains code. """
result = client.web3.eth.getCode(to_checksum_address(address), "latest")

Expand All @@ -182,6 +185,11 @@ def check_address_has_code(client: "JSONRPCClient", address: Address, contract_n
)
)

if expected_code is not None and result != expected_code:
raise ContractCodeMismatch(
f"{formated_contract_name}Address {to_checksum_address(address)} has wrong code."
)


class ParityCallType(Enum):
ESTIMATE_GAS = 1
Expand Down

0 comments on commit 5cb5d04

Please sign in to comment.