Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions raiden/network/blockchain_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
)
from raiden.network.rpc.client import JSONRPCClient
from raiden.utils import privatekey_to_address
from raiden.utils.typing import Address, ChannelID, T_ChannelID, TokenNetworkAddress
from raiden.utils.typing import (
Address,
ChannelID,
PaymentNetworkID,
T_ChannelID,
TokenNetworkAddress,
)
from raiden_contracts.contract_manager import ContractManager


Expand Down Expand Up @@ -143,7 +149,7 @@ def token_network_registry(self, address: Address) -> TokenNetworkRegistry:
if address not in self.address_to_token_network_registry:
self.address_to_token_network_registry[address] = TokenNetworkRegistry(
jsonrpc_client=self.client,
registry_address=address,
registry_address=PaymentNetworkID(address),
contract_manager=self.contract_manager,
)

Expand Down
49 changes: 30 additions & 19 deletions raiden/network/proxies/payment_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,18 @@
from raiden.constants import UINT256_MAX
from raiden.network.proxies import TokenNetwork
from raiden.network.proxies.token_network import ChannelDetails
from raiden.utils import typing
from raiden.utils.filters import decode_event, get_filter_args_for_specific_event_from_channel
from raiden.utils.typing import (
AdditionalHash,
Address,
BalanceHash,
BlockSpecification,
ChannelID,
Locksroot,
Nonce,
Signature,
TokenAmount,
)
from raiden_contracts.constants import CONTRACT_TOKEN_NETWORK, ChannelEvent
from raiden_contracts.contract_manager import ContractManager

Expand All @@ -16,7 +26,7 @@ class PaymentChannel:
def __init__(
self,
token_network: TokenNetwork,
channel_identifier: typing.ChannelID,
channel_identifier: ChannelID,
contract_manager: ContractManager,
):

Expand Down Expand Up @@ -53,7 +63,7 @@ def __init__(
self.participant2 = participant2
self.token_network = token_network

def token_address(self) -> typing.Address:
def token_address(self) -> Address:
""" Returns the address of the token for the channel. """
return self.token_network.token_address()

Expand Down Expand Up @@ -87,7 +97,7 @@ def settle_timeout(self) -> int:
)
return event['args']['settle_timeout']

def close_block_number(self) -> typing.Optional[int]:
def close_block_number(self) -> Optional[int]:
""" Returns the channel's closed block number. """

# The closed block number is not in the smart contract storage to save
Expand Down Expand Up @@ -132,9 +142,10 @@ def settled(self) -> bool:
participant1=self.participant1,
participant2=self.participant2,
channel_identifier=self.channel_identifier,
block_identifier='latest',
)

def closing_address(self) -> Optional[typing.Address]:
def closing_address(self) -> Optional[Address]:
""" Returns the address of the closer of the channel. """
return self.token_network.closing_address(
participant1=self.participant1,
Expand All @@ -150,7 +161,7 @@ def can_transfer(self) -> bool:
channel_identifier=self.channel_identifier,
)

def set_total_deposit(self, total_deposit: typing.TokenAmount):
def set_total_deposit(self, total_deposit: TokenAmount):
self.token_network.set_total_deposit(
channel_identifier=self.channel_identifier,
total_deposit=total_deposit,
Expand All @@ -159,10 +170,10 @@ def set_total_deposit(self, total_deposit: typing.TokenAmount):

def close(
self,
nonce: typing.Nonce,
balance_hash: typing.BalanceHash,
additional_hash: typing.AdditionalHash,
signature: typing.Signature,
nonce: Nonce,
balance_hash: BalanceHash,
additional_hash: AdditionalHash,
signature: Signature,
):
""" Closes the channel using the provided balance proof. """
self.token_network.close(
Expand All @@ -176,11 +187,11 @@ def close(

def update_transfer(
self,
nonce: typing.Nonce,
balance_hash: typing.BalanceHash,
additional_hash: typing.AdditionalHash,
partner_signature: typing.Signature,
signature: typing.Signature,
nonce: Nonce,
balance_hash: BalanceHash,
additional_hash: AdditionalHash,
partner_signature: Signature,
signature: Signature,
):
""" Updates the channel using the provided balance proof. """
self.token_network.update_transfer(
Expand All @@ -204,10 +215,10 @@ def settle(
self,
transferred_amount: int,
locked_amount: int,
locksroot: typing.Locksroot,
locksroot: Locksroot,
partner_transferred_amount: int,
partner_locked_amount: int,
partner_locksroot: typing.Locksroot,
partner_locksroot: Locksroot,
):
""" Settles the channel. """
self.token_network.settle(
Expand All @@ -223,8 +234,8 @@ def settle(

def all_events_filter(
self,
from_block: typing.BlockSpecification = None,
to_block: typing.BlockSpecification = None,
from_block: BlockSpecification = None,
to_block: BlockSpecification = None,
) -> Filter:

channel_topics = [
Expand Down
84 changes: 49 additions & 35 deletions raiden/network/proxies/secret_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
from gevent.event import AsyncResult

from raiden.constants import GAS_REQUIRED_PER_SECRET_IN_BATCH, GENESIS_BLOCK_NUMBER
from raiden.exceptions import InvalidAddress, TransactionThrew
from raiden.exceptions import InvalidAddress, RaidenUnrecoverableError
from raiden.network.proxies.utils import compare_contract_versions
from raiden.network.rpc.client import StatelessFilter, check_address_has_code
from raiden.network.rpc.transactions import check_transaction_threw
from raiden.utils import pex, privatekey_to_address, safe_gas_limit, sha3, typing
from raiden.utils import pex, privatekey_to_address, safe_gas_limit, sha3
from raiden.utils.typing import BlockSpecification, Keccak256, Secret
from raiden_contracts.constants import CONTRACT_SECRET_REGISTRY, EVENT_SECRET_REVEALED
from raiden_contracts.contract_manager import ContractManager

Expand Down Expand Up @@ -47,10 +48,10 @@ def __init__(
self.node_address = privatekey_to_address(self.client.privkey)
self.open_secret_transactions = dict()

def register_secret(self, secret: typing.Secret):
def register_secret(self, secret: Secret):
self.register_secret_batch([secret])

def register_secret_batch(self, secrets: List[typing.Secret]):
def register_secret_batch(self, secrets: List[Secret]):
secrets_to_register = list()
secrethashes_to_register = list()
secrethashes_not_sent = list()
Expand Down Expand Up @@ -82,43 +83,56 @@ def register_secret_batch(self, secrets: List[typing.Secret]):
log.debug('registerSecretBatch skipped', **log_details)
return

log.debug('registerSecretBatch called', **log_details)

try:
transaction_hash = self._register_secret_batch(secrets_to_register)
except Exception as e:
log.critical('registerSecretBatch failed', **log_details)
secret_registry_transaction.set_exception(e)
raise
else:
log.info('registerSecretBatch successful', **log_details)
secret_registry_transaction.set(transaction_hash)
finally:
for secret in secrets_to_register:
self.open_secret_transactions.pop(secret, None)

def _register_secret_batch(self, secrets):
gas_limit = self.proxy.estimate_gas('registerSecretBatch', secrets)
gas_limit = safe_gas_limit(gas_limit, len(secrets) * GAS_REQUIRED_PER_SECRET_IN_BATCH)
transaction_hash = self.proxy.transact('registerSecretBatch', gas_limit, secrets)
self.client.poll(transaction_hash)
receipt_or_none = check_transaction_threw(self.client, transaction_hash)

if receipt_or_none:
raise TransactionThrew('registerSecretBatch', receipt_or_none)

return transaction_hash

def get_register_block_for_secrethash(self, secrethash: typing.Keccak256) -> int:
error_prefix = 'Call to registerSecretBatch will fail'
gas_limit = self.proxy.estimate_gas('pending', 'registerSecretBatch', secrets)
if gas_limit:
error_prefix = 'Call to registerSecretBatch failed'
try:
gas_limit = safe_gas_limit(
gas_limit,
len(secrets) * GAS_REQUIRED_PER_SECRET_IN_BATCH,
)
transaction_hash = self.proxy.transact('registerSecretBatch', gas_limit, secrets)
self.client.poll(transaction_hash)
receipt_or_none = check_transaction_threw(self.client, transaction_hash)
except Exception as e:
secret_registry_transaction.set_exception(e)
msg = 'Unexpected exception at sending registerSecretBatch transaction'
else:
secret_registry_transaction.set(transaction_hash)
finally:
for secret in secrets_to_register:
self.open_secret_transactions.pop(secret, None)

transaction_executed = gas_limit is not None
if not transaction_executed or receipt_or_none:
if transaction_executed:
block = receipt_or_none['blockNumber']
else:
block = 'pending'

self.proxy.jsonrpc_client.check_for_insufficient_eth(
transaction_name='registerSecretBatch',
transaction_executed=transaction_executed,
required_gas=len(secrets) * GAS_REQUIRED_PER_SECRET_IN_BATCH,
block_identifier=block,
)
error_msg = f'{error_prefix}. {msg}'
log.critical(error_msg, **log_details)
raise RaidenUnrecoverableError(error_msg)

log.info('registerSecretBatch successful', **log_details)

def get_register_block_for_secrethash(self, secrethash: Keccak256) -> int:
return self.proxy.contract.functions.getSecretRevealBlockHeight(secrethash).call()

def check_registered(self, secrethash: typing.Keccak256) -> bool:
def check_registered(self, secrethash: Keccak256) -> bool:
return self.get_register_block_for_secrethash(secrethash) > 0

def secret_registered_filter(
self,
from_block: typing.BlockSpecification = GENESIS_BLOCK_NUMBER,
to_block: typing.BlockSpecification = 'latest',
from_block: BlockSpecification = GENESIS_BLOCK_NUMBER,
to_block: BlockSpecification = 'latest',
) -> StatelessFilter:
event_abi = self.contract_manager.get_event_abi(
CONTRACT_SECRET_REGISTRY,
Expand Down
Loading