Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
transfers_pair: List[MediationPairState],
pseudo_random_generator: random.Random,
block_number: BlockNumber,
secret: Secret,
secrethash: SecretHash,
) -> List[Event]:
""" While it's safe do the off-chain unlock. """
events: List[Event] = list()
for pair in reversed(transfers_pair):
payee_knows_secret = pair.payee_state in STATE_SECRET_KNOWN
payee_payed = pair.payee_state in STATE_TRANSFER_PAID
payee_channel = get_payee_channel(channelidentifiers_to_channels, pair)
payee_channel_open = (
payee_channel and channel.get_status(payee_channel) == ChannelState.STATE_OPENED
)
payer_channel = get_payer_channel(channelidentifiers_to_channels, pair)
# The mediator must not send to the payee a balance proof if the lock
# is in the danger zone, because the payer may not do the same and the
# on-chain unlock may fail. If the lock is nearing it's expiration
# block, then on-chain unlock should be done, and if successful it can
# be unlocked off-chain.
is_safe_to_send_balanceproof = False
if payer_channel:
is_safe_to_send_balanceproof = is_safe_to_wait(
pair.payer_transfer.lock.expiration, payer_channel.reveal_timeout, block_number
).ok
should_send_balanceproof_to_payee = (
# don't route back
if partner_address == previous_address:
continue
channel_state = views.get_channelstate_by_token_network_and_partner(
chain_state=chain_state,
token_network_address=token_network_address,
partner_address=partner_address,
)
if not channel_state:
continue
# check channel state
if channel.get_status(channel_state) != ChannelState.STATE_OPENED:
log.info(
"Channel is not opened, ignoring",
from_address=to_checksum_address(from_address),
partner_address=to_checksum_address(partner_address),
routing_source="Pathfinding Service",
)
continue
paths.append(
RouteState(
route=canonical_path,
forward_channel_id=channel_state.identifier,
estimated_fee=estimated_fee,
)
)
)
except StopIteration:
return TransitionResult(mediator_state, list())
if mediator_state.waiting_transfer is None:
return TransitionResult(mediator_state, list())
transfer = mediator_state.waiting_transfer.transfer
payer_channel_identifier = transfer.balance_proof.channel_identifier
payer_channel = channelidentifiers_to_channels.get(payer_channel_identifier)
payee_channel = channelidentifiers_to_channels.get(route.forward_channel_id)
if not payee_channel or not payer_channel:
return TransitionResult(mediator_state, list())
payee_channel_open = channel.get_status(payee_channel) == ChannelState.STATE_OPENED
if not payee_channel_open:
return TransitionResult(mediator_state, list())
return mediate_transfer(
state=mediator_state,
candidate_route_states=mediator_state.routes,
payer_channel=payer_channel,
channelidentifiers_to_channels=channelidentifiers_to_channels,
nodeaddresses_to_networkstates={state_change.node_address: state_change.network_state},
pseudo_random_generator=pseudo_random_generator,
payer_transfer=mediator_state.waiting_transfer.transfer,
block_number=block_number,
)
pseudo_random_generator: random.Random,
) -> TransitionResult[Optional[InitiatorTransferState]]:
""" When a secret is revealed on-chain all nodes learn the secret.
This check the on-chain secret corresponds to the one used by the
initiator, and if valid a new balance proof is sent to the next hop with
the current lock removed from the pending locks and the transferred amount
updated.
"""
iteration: TransitionResult[Optional[InitiatorTransferState]]
secret = state_change.secret
secrethash = initiator_state.transfer_description.secrethash
is_valid_secret = is_valid_secret_reveal(
state_change=state_change, transfer_secrethash=secrethash
)
is_channel_open = channel.get_status(channel_state) == ChannelState.STATE_OPENED
is_lock_expired = state_change.block_number > initiator_state.transfer.lock.expiration
is_lock_unlocked = is_valid_secret and not is_lock_expired
if is_lock_unlocked:
channel.register_onchain_secret(
channel_state=channel_state,
secret=secret,
secrethash=secrethash,
secret_reveal_block_number=state_change.block_number,
)
if is_lock_unlocked and is_channel_open:
events = events_for_unlock_lock(
initiator_state,
channel_state,
def events_for_onchain_secretreveal(
channel_state: NettingChannelState,
secret: Secret,
expiration: BlockExpiration,
block_hash: BlockHash,
) -> List[Event]:
events: List[Event] = list()
if not isinstance(secret, T_Secret):
raise ValueError("secret must be a Secret instance")
if get_status(channel_state) in CHANNEL_STATES_PRIOR_TO_CLOSED + (CHANNEL_STATE_CLOSED,):
reveal_event = ContractSendSecretReveal(
expiration=expiration, secret=secret, triggered_by_block_hash=block_hash
)
events.append(reveal_event)
return events
"target_states": target_states,
}
while list_cannonical_ids:
assert raiden, ALARM_TASK_ERROR_MSG
assert raiden.alarm, ALARM_TASK_ERROR_MSG
canonical_id = list_cannonical_ids[-1]
chain_state = views.state_from_raiden(raiden)
channel_state = views.get_channelstate_by_canonical_identifier(
chain_state=chain_state, canonical_identifier=canonical_id
)
channel_is_settled = (
channel_state is None or channel.get_status(channel_state) in target_states
)
if channel_is_settled:
list_cannonical_ids.pop()
else:
log.debug("wait_for_channel_in_states", **log_details)
gevent.sleep(retry_timeout)
def _set_channel_reveal_timeout(
self,
registry_address: TokenNetworkRegistryAddress,
channel_state: NettingChannelState,
reveal_timeout: BlockTimeout,
) -> Response:
log.debug(
"Set channel reveal timeout",
node=to_checksum_address(self.raiden_api.address),
channel_identifier=channel_state.identifier,
reveal_timeout=reveal_timeout,
)
if channel.get_status(channel_state) != ChannelState.STATE_OPENED:
return api_error(
errors="Can't update the reveal timeout of a closed channel",
status_code=HTTPStatus.CONFLICT,
)
try:
self.raiden_api.set_reveal_timeout(
registry_address=registry_address,
token_address=channel_state.token_address,
partner_address=channel_state.partner_state.address,
reveal_timeout=reveal_timeout,
)
except (NonexistingChannel, UnknownTokenAddress, InvalidBinaryAddress) as e:
return api_error(errors=str(e), status_code=HTTPStatus.BAD_REQUEST)
except InvalidRevealTimeout as e:
return api_error(errors=str(e), status_code=HTTPStatus.CONFLICT)
def filter_channels_by_status(
channel_states: List[NettingChannelState], exclude_states: Optional[List[str]] = None
) -> List[NettingChannelState]:
""" Filter the list of channels by excluding ones
for which the state exists in `exclude_states`. """
if exclude_states is None:
exclude_states = []
states = []
for channel_state in channel_states:
if channel.get_status(channel_state) not in exclude_states:
states.append(channel_state)
return states
lambda channel_state: channel.get_status(channel_state) == CHANNEL_STATE_CLOSED,
)