def handle_unlock(target_state, state_change, channel_state): """ Handles a ReceiveBalanceProof state change. """ iteration = TransitionResult(target_state, list()) if state_change.balance_proof.sender == target_state.route.node_address: is_valid, _ = channel.handle_unlock( channel_state, state_change, ) if is_valid: transfer = target_state.transfer transfer_success = EventTransferReceivedSuccess( transfer.identifier, transfer.lock.amount, transfer.initiator, ) unlock_success = EventWithdrawSuccess( transfer.identifier, transfer.lock.hashlock, ) iteration = TransitionResult(None, [transfer_success, unlock_success]) return iteration
def clear_if_finalized(iteration): """ Clears the state if the transfer was either completed or failed. """ state = iteration.new_state if state is None: return iteration if state.from_transfer.secret is None and state.block_number > state.from_transfer.expiration: failed = EventWithdrawFailed( identifier=state.from_transfer.identifier, hashlock=state.from_transfer.hashlock, reason='lock expired', ) iteration = TransitionResult(None, [failed]) elif state.state == 'balance_proof': transfer_success = EventTransferReceivedSuccess( state.from_transfer.identifier, state.from_transfer.amount, state.from_transfer.initiator, ) unlock_success = EventWithdrawSuccess( state.from_transfer.identifier, state.from_transfer.hashlock, ) iteration = TransitionResult(None, [transfer_success, unlock_success]) return iteration
def handle_unlock(target_state, state_change, channel_state): """ Handles a ReceiveUnlock state change. """ iteration = TransitionResult(target_state, list()) balance_proof_sender = state_change.balance_proof.sender if balance_proof_sender == target_state.route.node_address: is_valid, events, _ = channel.handle_unlock( channel_state, state_change, ) if is_valid: transfer = target_state.transfer transfer_success = EventTransferReceivedSuccess( transfer.payment_identifier, transfer.lock.amount, transfer.initiator, ) unlock_success = EventWithdrawSuccess( transfer.payment_identifier, transfer.lock.secrethash, ) send_processed = SendProcessed( balance_proof_sender, b'global', state_change.message_identifier, ) events.extend([transfer_success, unlock_success, send_processed]) iteration = TransitionResult(None, events) return iteration
def handle_unlock(mediator_state, state_change, channelidentifiers_to_channels): """ Handle a ReceiveUnlock state change. """ events = list() balance_proof_sender = state_change.balance_proof.sender channel_identifier = state_change.balance_proof.channel_address for pair in mediator_state.transfers_pair: if pair.payer_transfer.balance_proof.sender == balance_proof_sender: channel_state = channelidentifiers_to_channels.get( channel_identifier) if channel_state: is_valid, _ = channel.handle_unlock( channel_state, state_change, ) if is_valid: withdraw = EventWithdrawSuccess( pair.payee_transfer.identifier, pair.payee_transfer.lock.secrethash, ) events.append(withdraw) pair.payer_state = 'payer_balance_proof' iteration = TransitionResult(mediator_state, events) return iteration
def handle_contractwithdraw(state, state_change): """ Handle a NettingChannelUnlock state change. """ assert sha3( state.secret ) == state.hashlock, 'secret must be validated by the smart contract' # For all but the last pair in transfer pair a refund transfer ocurred, # meaning the same channel was used twice, once when this node sent the # mediated transfer and once when the refund transfer was received. A # ContractReceiveWithdraw state change may be used for each. events = list() # This node withdrew the refund if state_change.receiver == state.our_address: for previous_pos, pair in enumerate(state.transfers_pair, -1): if pair.payer_route.channel_address == state_change.channel_address: # always set the contract_withdraw regardless of the previous # state (even expired) pair.payer_state = 'payer_contract_withdraw' withdraw = EventWithdrawSuccess( pair.payer_transfer.identifier, pair.payer_transfer.hashlock, ) events.append(withdraw) # if the current pair is backed by a refund set the sent # mediated transfer to a 'secret known' state if previous_pos > -1: previous_pair = state.transfers_pair[previous_pos] if previous_pair.payee_state not in STATE_TRANSFER_FINAL: previous_pair.payee_state = 'payee_refund_withdraw' # A partner withdrew the mediated transfer else: for pair in state.transfers_pair: if pair.payer_route.channel_address == state_change.channel_address: unlock = EventUnlockSuccess( pair.payee_transfer.identifier, pair.payee_transfer.hashlock, ) events.append(unlock) pair.payee_state = 'payee_contract_withdraw' iteration = secret_learned( state, state_change.secret, state_change.receiver, 'payee_contract_withdraw', ) iteration.events.extend(events) return iteration
def handle_balanceproof(state, state_change): """ Handle a ReceiveBalanceProof state change. """ events = list() for pair in state.transfers_pair: if pair.payer_route.node_address == state_change.node_address: withdraw = EventWithdrawSuccess( pair.payee_transfer.identifier, pair.payee_transfer.hashlock, ) events.append(withdraw) pair.payer_state = 'payer_balance_proof' iteration = TransitionResult(state, events) return iteration