Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _find_new_partners(self) -> List[Address]:
""" Search the token network for potential channel partners. """
open_channels = views.get_channelstate_open(
chain_state=views.state_from_raiden(self.raiden),
token_network_registry_address=self.registry_address,
token_address=self.token_address,
)
known = set(channel_state.partner_state.address for channel_state in open_channels)
known.add(self.BOOTSTRAP_ADDR)
known.add(self.raiden.address)
participants_addresses = views.get_participants_addresses(
views.state_from_raiden(self.raiden), self.registry_address, self.token_address
)
available_addresses = list(participants_addresses - known)
shuffle(available_addresses)
new_partners = available_addresses
log.debug(
"Found partners",
node=to_checksum_address(self.raiden.address),
number_of_partners=len(available_addresses),
)
return new_partners
Notes about setup order:
- The filters must be polled after the node state has been primed,
otherwise the state changes won't have effect.
- The synchronization must be done before the transport is started, to
reject messages for closed/settled channels.
"""
msg = (
f"Transport must not be started before the node has synchronized "
f"with the blockchain, otherwise the node may accept transfers to a "
f"closed channel. node:{self!r}"
)
assert not self.transport, msg
assert self.wal, f"The database must have been initialized. node:{self!r}"
chain_state = views.state_from_raiden(self)
# The `Block` state change is dispatched only after all the events for
# that given block have been processed, filters can be safely installed
# starting from this position without missing events.
last_block_number = views.block_number(chain_state)
filters = smart_contract_filters_from_node_state(
chain_state,
self.contract_manager,
self.default_registry.address,
self.default_secret_registry.address,
)
blockchain_events = BlockchainEvents(
web3=self.rpc_client.web3,
chain_id=chain_state.chain_id,
contract_manager=self.contract_manager,
def get_tokens_list(self, registry_address: TokenNetworkRegistryAddress) -> List[TokenAddress]:
"""Returns a list of tokens the node knows about"""
tokens_list = views.get_token_identifiers(
chain_state=views.state_from_raiden(self.raiden),
token_network_registry_address=registry_address,
)
return tokens_list
def get_pending_transfers(
self, token_address: TokenAddress = None, partner_address: Address = None
) -> List[Dict[str, Any]]:
chain_state = views.state_from_raiden(self.raiden)
transfer_tasks = views.get_all_transfer_tasks(chain_state)
channel_id = None
confirmed_block_identifier = chain_state.block_hash
if token_address is not None:
token_network = self.raiden.default_registry.get_token_network(
token_address=token_address, block_identifier=confirmed_block_identifier
)
if token_network is None:
raise UnknownTokenAddress(f"Token {token_address} not found.")
if partner_address is not None:
partner_channel = views.get_channelstate_for(
chain_state=chain_state,
token_network_registry_address=self.raiden.default_registry.address,
token_address=token_address,
partner_address=partner_address,
)
Raises:
InvalidBinaryAddress: If either token_address or partner_address is not
20 bytes long.
RaidenRecoverableError: May happen for multiple reasons:
- If the token approval fails, e.g. the token may validate if
account has enough balance for the allowance.
- The deposit failed, e.g. the allowance did not set the token
aside for use and the user spent it before deposit was called.
- The channel was closed/settled between the allowance call and
the deposit call.
AddressWithoutCode: The channel was settled during the deposit
execution.
DepositOverLimit: The total deposit amount is higher than the limit.
UnexpectedChannelState: The channel is no longer in an open state.
"""
chain_state = views.state_from_raiden(self.raiden)
token_addresses = views.get_token_identifiers(chain_state, registry_address)
channel_state = views.get_channelstate_for(
chain_state=chain_state,
token_network_registry_address=registry_address,
token_address=token_address,
partner_address=partner_address,
)
if not is_binary_address(token_address):
raise InvalidBinaryAddress(
"Expected binary address format for token in channel deposit"
)
if not is_binary_address(partner_address):
raise InvalidBinaryAddress(
lock_timeout: BlockTimeout = None,
) -> Tuple[Optional[str], ActionInitInitiator]:
transfer_state = TransferDescriptionWithSecretState(
token_network_registry_address=raiden.default_registry.address,
payment_identifier=transfer_identifier,
amount=transfer_amount,
token_network_address=token_network_address,
initiator=InitiatorAddress(raiden.address),
target=target_address,
secret=transfer_secret,
secrethash=transfer_secrethash,
lock_timeout=lock_timeout,
)
error_msg, routes, feedback_token = routing.get_best_routes(
chain_state=views.state_from_raiden(raiden),
token_network_address=token_network_address,
one_to_n_address=raiden.default_one_to_n_address,
from_address=InitiatorAddress(raiden.address),
to_address=target_address,
amount=transfer_amount,
previous_address=None,
pfs_config=raiden.config.get("pfs_config", None),
privkey=raiden.privkey,
)
# Only prepare feedback when token is available
if feedback_token is not None:
for route_state in routes:
raiden.route_to_feedback_token[tuple(route_state.route)] = feedback_token
return error_msg, ActionInitInitiator(transfer_state, routes)
def _funds_remaining(self) -> TokenAmount:
"""The remaining funds after subtracting the already deposited amounts.
Note:
- This attribute must be accessed with the lock held.
"""
if self.funds > 0:
token = self.raiden.proxy_manager.token(self.token_address)
token_balance = token.balance_of(self.raiden.address)
sum_deposits = views.get_our_deposits_for_token_network(
views.state_from_raiden(self.raiden), self.registry_address, self.token_address
)
return TokenAmount(min(self.funds - sum_deposits, token_balance))
return TokenAmount(0)
def transfer_async(
self,
registry_address: TokenNetworkRegistryAddress,
token_address: TokenAddress,
amount: PaymentAmount,
target: TargetAddress,
identifier: PaymentID = None,
secret: Secret = None,
secrethash: SecretHash = None,
lock_timeout: BlockTimeout = None,
) -> "PaymentStatus":
current_state = views.state_from_raiden(self.raiden)
token_network_registry_address = self.raiden.default_registry.address
if not isinstance(amount, int): # pragma: no unittest
raise InvalidAmount("Amount not a number")
if amount <= 0:
raise InvalidAmount("Amount negative")
if amount > UINT256_MAX:
raise InvalidAmount("Amount too large")
if not is_binary_address(token_address):
raise InvalidBinaryAddress("token address is not valid.")
if token_address not in views.get_token_identifiers(current_state, registry_address):
raise UnknownTokenAddress("Token address is not known.")