def handle_unlock_light( target_state: TargetTransferState, state_change: ReceiveUnlockLight, channel_state: NettingChannelState, ) -> TransitionResult[TargetTransferState]: """ Handles a ReceiveUnlockLight state change. """ is_valid, events, _ = channel.handle_unlock_light(channel_state, state_change) next_target_state: Optional[TargetTransferState] = target_state if is_valid: transfer = target_state.transfer payment_received_success = EventPaymentReceivedSuccess( payment_network_identifier=channel_state.payment_network_identifier, token_network_identifier=TokenNetworkID(channel_state.token_network_identifier), identifier=transfer.payment_identifier, amount=TokenAmount(transfer.lock.amount), initiator=transfer.initiator, ) unlock_success = EventUnlockClaimSuccess( transfer.payment_identifier, transfer.lock.secrethash ) store_unlock_message = StoreMessageEvent( state_change.signed_unlock.message_identifier, state_change.signed_unlock.payment_identifier, 11, state_change.signed_unlock, True ) events.extend([payment_received_success, unlock_success, store_unlock_message]) next_target_state = None return TransitionResult(next_target_state, events)
def handle_contractunlock( state, state_change, channelidentifiers_to_channels, pseudo_random_generator, block_number, ): """ Handle a NettingChannelUnlock state change. """ assert sha3( state.secret ) == state.secrethash, '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 # ContractReceiveChannelUnlock 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): payer_channel = get_payer_channel(channelidentifiers_to_channels, pair) if payer_channel.identifier == state_change.channel_identifier: # always set the contract_unlock regardless of the previous # state (even expired) pair.payer_state = 'payer_contract_unlock' unlock = EventUnlockClaimSuccess( pair.payer_transfer.payment_identifier, pair.payer_transfer.lock.secrethash, ) events.append(unlock) # 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_unlock' # A partner withdrew the mediated transfer else: for pair in state.transfers_pair: payee_channel = get_payee_channel(channelidentifiers_to_channels, pair) if payee_channel.identifier == state_change.channel_identifier: unlock = EventUnlockSuccess( pair.payee_transfer.payment_identifier, pair.payee_transfer.lock.secrethash, ) events.append(unlock) pair.payee_state = 'payee_contract_unlock' iteration = secret_learned( state, channelidentifiers_to_channels, pseudo_random_generator, block_number, state_change.secret, state_change.secrethash, state_change.receiver, 'payee_contract_unlock', False, ) iteration.events.extend(events) return iteration