def register_registry(self, registry): """ Register the registry and intialize all the related assets and channels. """ translator = ContractTranslator(REGISTRY_ABI) assetadded = registry.assetadded_filter() all_manager_addresses = registry.manager_addresses() task_name = 'Registry {}'.format(pex(registry.address)) asset_listener = LogListenerTask( task_name, assetadded, self.on_event, translator, ) asset_listener.start() self.event_listeners.append(asset_listener) self.registries.append(registry) for manager_address in all_manager_addresses: channel_manager = self.chain.manager(manager_address) self.register_channel_manager(channel_manager)
def register_channel_manager(self, channel_manager): """ Discover and register the channels for the given asset. """ translator = ContractTranslator(CHANNEL_MANAGER_ABI) # To avoid missing changes, first create the filter, call the # contract and then start polling. channelnew = channel_manager.channelnew_filter() all_netting_contracts = channel_manager.channels_by_participant( self.address) task_name = 'ChannelManager {}'.format(pex(channel_manager.address)) channel_listener = LogListenerTask( task_name, channelnew, self.on_event, translator, ) channel_listener.start() self.event_listeners.append(channel_listener) asset_address_bin = channel_manager.asset_address() channel_manager_address_bin = channel_manager.address edges = channel_manager.channels_addresses() channel_graph = ChannelGraph(edges) asset_manager = AssetManager( self, asset_address_bin, channel_manager_address_bin, channel_graph, ) self.managers_by_asset_address[asset_address_bin] = asset_manager self.managers_by_address[channel_manager_address_bin] = asset_manager for netting_contract_address in all_netting_contracts: asset_manager.register_channel_by_address( netting_contract_address, self.config['reveal_timeout'], )
def register_channel_manager(self, channel_manager): """ Discover and register the channels for the given asset. """ translator = ContractTranslator(CHANNEL_MANAGER_ABI) # To avoid missing changes, first create the filter, call the # contract and then start polling. channelnew = channel_manager.channelnew_filter() all_netting_contracts = channel_manager.channels_by_participant(self.address) task_name = 'ChannelManager {}'.format(pex(channel_manager.address)) channel_listener = LogListenerTask( task_name, channelnew, self.on_event, translator, ) channel_listener.start() self.event_listeners.append(channel_listener) asset_address_bin = channel_manager.asset_address() channel_manager_address_bin = channel_manager.address edges = channel_manager.channels_addresses() channel_graph = ChannelGraph(edges) asset_manager = AssetManager( self, asset_address_bin, channel_manager_address_bin, channel_graph, ) self.managers_by_asset_address[asset_address_bin] = asset_manager self.managers_by_address[channel_manager_address_bin] = asset_manager for netting_contract_address in all_netting_contracts: asset_manager.register_channel_by_address( netting_contract_address, self.config['reveal_timeout'], )
def register_channel(self, netting_channel, reveal_timeout): """ Register a new channel. Args: netting_channel (network.rpc.client.NettingChannel): The netting channel proxy. reveal_timeout (int): Minimum number of blocks required by this node to see a secret. Raises: ValueError: - If raiden.address is not one of the participants in the netting channel. - If the contract's settle_timeout is smaller than the reveal_timeout. """ # pylint: disable=too-many-locals translator = ContractTranslator(NETTING_CHANNEL_ABI) # Race condition: # - If the filter is installed after the call to `details` a deposit # could be missed in the meantime, to avoid this we first install the # filter listener and then call `deposit`. # - Because of the above strategy a `deposit` event could be handled # twice, for this reason we must not use `deposit` in the events but # the resulting `balance`. task_name = "ChannelNewBalance {}".format(pex(netting_channel.address)) newbalance = netting_channel.channelnewbalance_filter() newbalance_listener = LogListenerTask(task_name, newbalance, self.raiden.on_event, translator) task_name = "ChannelSecretRevelead {}".format(pex(netting_channel.address)) secretrevealed = netting_channel.channelsecretrevealed_filter() secretrevealed_listener = LogListenerTask(task_name, secretrevealed, self.raiden.on_event, translator) task_name = "ChannelClosed {}".format(pex(netting_channel.address)) close = netting_channel.channelclosed_filter() close_listener = LogListenerTask(task_name, close, self.raiden.on_event, translator) task_name = "ChannelSettled {}".format(pex(netting_channel.address)) settled = netting_channel.channelsettled_filter() settled_listener = LogListenerTask(task_name, settled, self.raiden.on_event, translator) channel_details = netting_channel.detail(self.raiden.address) our_state = ChannelEndState(channel_details["our_address"], channel_details["our_balance"]) partner_state = ChannelEndState(channel_details["partner_address"], channel_details["partner_balance"]) external_state = ChannelExternalState( self.raiden.alarm.register_callback, self.register_channel_for_hashlock, self.raiden.chain.block_number, netting_channel, ) channel = Channel( our_state, partner_state, external_state, self.asset_address, reveal_timeout, channel_details["settle_timeout"], ) self.partneraddress_channel[partner_state.address] = channel self.address_channel[netting_channel.address] = channel newbalance_listener.start() secretrevealed_listener.start() close_listener.start() settled_listener.start() self.raiden.event_listeners.append(newbalance_listener) self.raiden.event_listeners.append(secretrevealed_listener) self.raiden.event_listeners.append(close_listener) self.raiden.event_listeners.append(settled_listener)
def register_channel(self, netting_channel, reveal_timeout): """ Register a new channel. Args: netting_channel (network.rpc.client.NettingChannel): The netting channel proxy. reveal_timeout (int): Minimum number of blocks required by this node to see a secret. Raises: ValueError: If raiden.address is not one of the participants in the netting channel. """ translator = ContractTranslator(NETTING_CHANNEL_ABI) # Race condition: # - If the filter is installed after the call to `details` a deposit # could be missed in the meantime, to avoid this we first install the # filter listener and then call `deposit`. # - Because of the above strategy a `deposit` event could be handled # twice, for this reason we must not use `deposit` in the events but # the resulting `balance`. task_name = 'ChannelNewBalance {}'.format(pex(netting_channel.address)) newbalance = netting_channel.channelnewbalance_filter() newbalance_listener = LogListenerTask( task_name, newbalance, self.raiden.on_event, translator, ) task_name = 'ChannelSecretRevelead {}'.format( pex(netting_channel.address)) secretrevealed = netting_channel.channelsecretrevealed_filter() secretrevealed_listener = LogListenerTask( task_name, secretrevealed, self.raiden.on_event, translator, ) task_name = 'ChannelClosed {}'.format(pex(netting_channel.address)) close = netting_channel.channelclosed_filter() close_listener = LogListenerTask( task_name, close, self.raiden.on_event, translator, ) task_name = 'ChannelSettled {}'.format(pex(netting_channel.address)) settled = netting_channel.channelsettled_filter() settled_listener = LogListenerTask( task_name, settled, self.raiden.on_event, translator, ) channel_details = netting_channel.detail(self.raiden.address) our_state = ChannelEndState( channel_details['our_address'], channel_details['our_balance'], ) partner_state = ChannelEndState( channel_details['partner_address'], channel_details['partner_balance'], ) external_state = ChannelExternalState( self.raiden.alarm.register_callback, self.register_channel_for_hashlock, self.raiden.chain.block_number, netting_channel, ) channel = Channel( our_state, partner_state, external_state, self.asset_address, reveal_timeout, channel_details['settle_timeout'], ) self.partneraddress_channel[partner_state.address] = channel self.address_channel[netting_channel.address] = channel newbalance_listener.start() secretrevealed_listener.start() close_listener.start() settled_listener.start() self.raiden.event_listeners.append(newbalance_listener) self.raiden.event_listeners.append(secretrevealed_listener) self.raiden.event_listeners.append(close_listener) self.raiden.event_listeners.append(settled_listener)
def register_channel(self, netting_channel, reveal_timeout): """ Register a new channel. Args: netting_channel (network.rpc.client.NettingChannel): The netting channel proxy. reveal_timeout (int): Minimum number of blocks required by this node to see a secret. Raises: ValueError: If raiden.address is not one of the participants in the netting channel. """ translator = ContractTranslator(NETTING_CHANNEL_ABI) # race condition: # - if the filter is installed after a deposit is made it could be # missed, to avoid that we first install the filter, then request the # state from the node and then poll the filter. # - with the above strategy the same deposit could be handled twice, # once from the status received from the netting contract and once from # the event, to avoid problems the we use the balance instead of the # deposit is used. newbalance = netting_channel.channelnewbalance_filter() newbalance_listener = LogListenerTask( newbalance, self.raiden.on_event, translator, ) secretrevealed = netting_channel.channelsecretrevealed_filter() secretrevealed_listener = LogListenerTask( secretrevealed, self.raiden.on_event, translator, ) close = netting_channel.channelclosed_filter() close_listener = LogListenerTask( close, self.raiden.on_event, translator, ) settled = netting_channel.channelsettled_filter() settled_listener = LogListenerTask( settled, self.raiden.on_event, translator, ) channel_details = netting_channel.detail(self.raiden.address) our_state = ChannelEndState( channel_details['our_address'], channel_details['our_balance'], ) partner_state = ChannelEndState( channel_details['partner_address'], channel_details['partner_balance'], ) external_state = ChannelExternalState( self.register_channel_for_hashlock, self.raiden.chain.block_number, netting_channel, ) channel = Channel( our_state, partner_state, external_state, self.asset_address, reveal_timeout, channel_details['settle_timeout'], ) self.partneraddress_channel[partner_state.address] = channel self.address_channel[netting_channel.address] = channel self.channelgraph.add_path( channel_details['our_address'], channel_details['partner_address'], ) newbalance_listener.start() secretrevealed_listener.start() close_listener.start() settled_listener.start() self.raiden.event_listeners.append(newbalance_listener) self.raiden.event_listeners.append(secretrevealed_listener) self.raiden.event_listeners.append(close_listener) self.raiden.event_listeners.append(settled_listener)