def __init__(self, raiden, token_network_identifier): chain_state = views.state_from_raiden(raiden) token_network_state = views.get_token_network_by_identifier( chain_state, token_network_identifier, ) token_network_registry = views.get_token_network_registry_by_token_network_identifier( chain_state, token_network_identifier, ) # TODO: # - Add timeout for transaction polling, used to overwrite the RaidenAPI # defaults # - Add a proper selection strategy (#576) self.funds = 0 self.initial_channel_target = 0 self.joinable_funds_target = 0 self.raiden = raiden self.registry_address = token_network_registry.address self.token_network_identifier = token_network_identifier self.token_address = token_network_state.token_address self.lock = Semaphore() #: protects self.funds and self.initial_channel_target self.api = RaidenAPI(raiden)
def handle_token_network_action( chain_state: ChainState, state_change: StateChange, ) -> TransitionResult: token_network_state = views.get_token_network_by_identifier( chain_state, state_change.token_network_identifier, ) payment_network_state = views.get_token_network_registry_by_token_network_identifier( chain_state, state_change.token_network_identifier, ) assert payment_network_state, 'We should always get a payment_network_state' payment_network_id = payment_network_state.address events = list() if token_network_state: pseudo_random_generator = chain_state.pseudo_random_generator iteration = token_network.state_transition( payment_network_id, token_network_state, state_change, pseudo_random_generator, chain_state.block_number, ) assert iteration.new_state, 'No token network state transition leads to None' events = iteration.events return TransitionResult(chain_state, events)
def handle_channel_batch_unlock( chain_state: ChainState, state_change: ContractReceiveChannelBatchUnlock, ) -> TransitionResult: token_network_identifier = state_change.token_network_identifier token_network_state = views.get_token_network_by_identifier( chain_state, token_network_identifier, ) events = [] if token_network_state: payment_network_state = views.get_token_network_registry_by_token_network_identifier( chain_state, token_network_state.address, ) pseudo_random_generator = chain_state.pseudo_random_generator participant1 = state_change.participant participant2 = state_change.partner for channel_state in token_network_state.channelidentifiers_to_channels.values(): are_addresses_valid1 = ( channel_state.our_state.address == participant1 and channel_state.partner_state.address == participant2 ) are_addresses_valid2 = ( channel_state.our_state.address == participant2 and channel_state.partner_state.address == participant1 ) is_valid_locksroot = True is_valid_channel = ( (are_addresses_valid1 or are_addresses_valid2) and is_valid_locksroot ) if is_valid_channel: sub_iteration = channel.state_transition( channel_state, state_change, pseudo_random_generator, chain_state.block_number, ) events.extend(sub_iteration.events) if sub_iteration.new_state is None: del payment_network_state.tokenaddresses_to_tokennetworks[ token_network_state.token_address ] del payment_network_state.tokenidentifiers_to_tokennetworks[ token_network_identifier ] return TransitionResult(chain_state, events)