Best Python code snippet using localstack_python
test_integration_events.py
Source:test_integration_events.py
1from typing import Dict, List2import gevent3import pytest4from eth_utils import is_list_like, to_checksum_address5from web3.utils.events import construct_event_topic_set6from raiden import waiting7from raiden.api.python import RaidenAPI8from raiden.blockchain.events import (9 ALL_EVENTS,10 get_all_netting_channel_events,11 get_contract_events,12 get_token_network_events,13 get_token_network_registry_events,14)15from raiden.constants import GENESIS_BLOCK_NUMBER16from raiden.network.blockchain_service import BlockChainService17from raiden.settings import DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS18from raiden.tests.utils.events import must_contain_entry, must_have_event19from raiden.tests.utils.geth import wait_until_block20from raiden.tests.utils.network import CHAIN21from raiden.tests.utils.protocol import HoldOffChainSecretRequest, dont_handle_secret_request_mock22from raiden.tests.utils.transfer import assert_synced_channel_state, get_channelstate23from raiden.transfer import channel, views24from raiden.transfer.mediated_transfer.events import SendLockedTransfer25from raiden.utils import sha3, wait_until26from raiden.utils.typing import Address, BlockSpecification, ChannelID27from raiden_contracts.constants import (28 CONTRACT_TOKEN_NETWORK,29 EVENT_TOKEN_NETWORK_CREATED,30 ChannelEvent,31)32from raiden_contracts.contract_manager import ContractManager33def get_netting_channel_closed_events(34 chain: BlockChainService,35 token_network_address: Address,36 netting_channel_identifier: ChannelID,37 contract_manager: ContractManager,38 from_block: BlockSpecification = GENESIS_BLOCK_NUMBER,39 to_block: BlockSpecification = 'latest',40) -> List[Dict]:41 closed_event_abi = contract_manager.get_event_abi(42 CONTRACT_TOKEN_NETWORK,43 ChannelEvent.CLOSED,44 )45 topic_set = construct_event_topic_set(46 event_abi=closed_event_abi,47 arguments={'channel_identifier': netting_channel_identifier},48 )49 if len(topic_set) == 1 and is_list_like(topic_set[0]):50 topics = topic_set[0]51 else:52 topics = topic_set53 return get_contract_events(54 chain=chain,55 abi=contract_manager.get_contract_abi(CONTRACT_TOKEN_NETWORK),56 contract_address=token_network_address,57 topics=topics,58 from_block=from_block,59 to_block=to_block,60 )61def get_netting_channel_deposit_events(62 chain: BlockChainService,63 token_network_address: Address,64 netting_channel_identifier: ChannelID,65 contract_manager: ContractManager,66 from_block: BlockSpecification = GENESIS_BLOCK_NUMBER,67 to_block: BlockSpecification = 'latest',68) -> List[Dict]:69 deposit_event_abi = contract_manager.get_event_abi(70 CONTRACT_TOKEN_NETWORK,71 ChannelEvent.DEPOSIT,72 )73 topic_set = construct_event_topic_set(74 event_abi=deposit_event_abi,75 arguments={'channel_identifier': netting_channel_identifier},76 )77 if len(topic_set) == 1 and is_list_like(topic_set[0]):78 topics = topic_set[0]79 else:80 topics = topic_set81 return get_contract_events(82 chain,83 contract_manager.get_contract_abi(CONTRACT_TOKEN_NETWORK),84 token_network_address,85 topics,86 from_block,87 to_block,88 )89def get_netting_channel_settled_events(90 chain: BlockChainService,91 token_network_address: Address,92 netting_channel_identifier: ChannelID,93 contract_manager: ContractManager,94 from_block: BlockSpecification = GENESIS_BLOCK_NUMBER,95 to_block: BlockSpecification = 'latest',96) -> List[Dict]:97 settled_event_abi = contract_manager.get_event_abi(98 CONTRACT_TOKEN_NETWORK,99 ChannelEvent.SETTLED,100 )101 topic_set = construct_event_topic_set(102 event_abi=settled_event_abi,103 arguments={'channel_identifier': netting_channel_identifier},104 )105 if len(topic_set) == 1 and is_list_like(topic_set[0]):106 topics = topic_set[0]107 else:108 topics = topic_set109 return get_contract_events(110 chain,111 contract_manager.get_contract_abi(CONTRACT_TOKEN_NETWORK),112 token_network_address,113 topics,114 from_block,115 to_block,116 )117def wait_both_channel_open(118 app0,119 app1,120 registry_address,121 token_address,122 retry_timeout,123):124 waiting.wait_for_newchannel(125 app1.raiden,126 registry_address,127 token_address,128 app0.raiden.address,129 retry_timeout,130 )131 waiting.wait_for_newchannel(132 app0.raiden,133 registry_address,134 token_address,135 app1.raiden.address,136 retry_timeout,137 )138def wait_both_channel_deposit(139 app_deposit,140 app_partner,141 registry_address,142 token_address,143 total_deposit,144 retry_timeout,145):146 waiting.wait_for_participant_newbalance(147 app_deposit.raiden,148 registry_address,149 token_address,150 app_partner.raiden.address,151 app_deposit.raiden.address,152 total_deposit,153 retry_timeout,154 )155 waiting.wait_for_participant_newbalance(156 app_partner.raiden,157 registry_address,158 token_address,159 app_deposit.raiden.address,160 app_deposit.raiden.address,161 total_deposit,162 retry_timeout,163 )164@pytest.mark.parametrize('number_of_nodes', [2])165@pytest.mark.parametrize('channels_per_node', [0])166def test_channel_new(raiden_chain, retry_timeout, token_addresses):167 app0, app1 = raiden_chain # pylint: disable=unbalanced-tuple-unpacking168 registry_address = app0.raiden.default_registry.address169 token_address = token_addresses[0]170 channelcount0 = views.total_token_network_channels(171 views.state_from_app(app0),172 registry_address,173 token_address,174 )175 RaidenAPI(app0.raiden).channel_open(176 registry_address,177 token_address,178 app1.raiden.address,179 )180 wait_both_channel_open(app0, app1, registry_address, token_address, retry_timeout)181 # The channel is created but without funds182 channelcount1 = views.total_token_network_channels(183 views.state_from_app(app0),184 registry_address,185 token_address,186 )187 assert channelcount0 + 1 == channelcount1188@pytest.mark.parametrize('privatekey_seed', ['event_new_channel:{}'])189@pytest.mark.parametrize('number_of_nodes', [2])190@pytest.mark.parametrize('channels_per_node', [0])191def test_channel_deposit(raiden_chain, deposit, retry_timeout, token_addresses):192 app0, app1 = raiden_chain193 token_address = token_addresses[0]194 registry_address = app0.raiden.default_registry.address195 token_network_identifier = views.get_token_network_identifier_by_token_address(196 views.state_from_app(app0),197 app0.raiden.default_registry.address,198 token_address,199 )200 channel0 = get_channelstate(app0, app1, token_network_identifier)201 channel1 = get_channelstate(app1, app0, token_network_identifier)202 assert channel0 is None203 assert channel1 is None204 RaidenAPI(app0.raiden).channel_open(205 registry_address,206 token_address,207 app1.raiden.address,208 )209 wait_both_channel_open(app0, app1, registry_address, token_address, retry_timeout)210 assert_synced_channel_state(211 token_network_identifier,212 app0, 0, [],213 app1, 0, [],214 )215 RaidenAPI(app0.raiden).set_total_channel_deposit(216 registry_address,217 token_address,218 app1.raiden.address,219 deposit,220 )221 wait_both_channel_deposit(222 app0,223 app1,224 registry_address,225 token_address,226 deposit,227 retry_timeout,228 )229 assert_synced_channel_state(230 token_network_identifier,231 app0, deposit, [],232 app1, 0, [],233 )234 RaidenAPI(app1.raiden).set_total_channel_deposit(235 registry_address,236 token_address,237 app0.raiden.address,238 deposit,239 )240 wait_both_channel_deposit(241 app1,242 app0,243 registry_address,244 token_address,245 deposit,246 retry_timeout,247 )248 assert_synced_channel_state(249 token_network_identifier,250 app0, deposit, [],251 app1, deposit, [],252 )253@pytest.mark.parametrize('number_of_nodes', [2])254@pytest.mark.parametrize('channels_per_node', [0])255def test_query_events(256 raiden_chain,257 token_addresses,258 deposit,259 settle_timeout,260 retry_timeout,261 contract_manager,262):263 app0, app1 = raiden_chain # pylint: disable=unbalanced-tuple-unpacking264 registry_address = app0.raiden.default_registry.address265 token_address = token_addresses[0]266 token_network_identifier = views.get_token_network_identifier_by_token_address(267 views.state_from_app(app0),268 registry_address,269 token_address,270 )271 token_network_address = app0.raiden.default_registry.get_token_network(token_address)272 manager0 = app0.raiden.chain.token_network(token_network_address)273 channelcount0 = views.total_token_network_channels(274 views.state_from_app(app0),275 registry_address,276 token_address,277 )278 events = get_token_network_registry_events(279 chain=app0.raiden.chain,280 token_network_registry_address=registry_address,281 contract_manager=contract_manager,282 events=ALL_EVENTS,283 )284 assert must_have_event(285 events,286 {287 'event': EVENT_TOKEN_NETWORK_CREATED,288 'args': {289 'token_network_address': to_checksum_address(manager0.address),290 'token_address': to_checksum_address(token_address),291 },292 },293 )294 events = get_token_network_registry_events(295 chain=app0.raiden.chain,296 token_network_registry_address=app0.raiden.default_registry.address,297 contract_manager=contract_manager,298 events=ALL_EVENTS,299 from_block=999999998,300 to_block=999999999,301 )302 assert not events303 RaidenAPI(app0.raiden).channel_open(304 registry_address,305 token_address,306 app1.raiden.address,307 )308 wait_both_channel_open(app0, app1, registry_address, token_address, retry_timeout)309 events = get_token_network_events(310 chain=app0.raiden.chain,311 token_network_address=manager0.address,312 contract_manager=contract_manager,313 events=ALL_EVENTS,314 )315 _event = must_have_event(316 events,317 {318 'event': ChannelEvent.OPENED,319 'args': {320 'participant1': to_checksum_address(app0.raiden.address),321 'participant2': to_checksum_address(app1.raiden.address),322 'settle_timeout': settle_timeout,323 },324 },325 )326 assert _event327 channel_id = _event['args']['channel_identifier']328 events = get_token_network_events(329 chain=app0.raiden.chain,330 token_network_address=manager0.address,331 contract_manager=contract_manager,332 events=ALL_EVENTS,333 from_block=999999998,334 to_block=999999999,335 )336 assert not events337 # channel is created but not opened and without funds338 channelcount1 = views.total_token_network_channels(339 views.state_from_app(app0),340 registry_address,341 token_address,342 )343 assert channelcount0 + 1 == channelcount1344 assert_synced_channel_state(345 token_network_identifier,346 app0, 0, [],347 app1, 0, [],348 )349 RaidenAPI(app0.raiden).set_total_channel_deposit(350 registry_address,351 token_address,352 app1.raiden.address,353 deposit,354 )355 all_netting_channel_events = get_all_netting_channel_events(356 chain=app0.raiden.chain,357 token_network_address=token_network_identifier,358 netting_channel_identifier=channel_id,359 contract_manager=app0.raiden.contract_manager,360 )361 deposit_events = get_netting_channel_deposit_events(362 chain=app0.raiden.chain,363 token_network_address=token_network_identifier,364 netting_channel_identifier=channel_id,365 contract_manager=contract_manager,366 )367 total_deposit_event = {368 'event': ChannelEvent.DEPOSIT,369 'args': {370 'participant': to_checksum_address(app0.raiden.address),371 'total_deposit': deposit,372 'channel_identifier': channel_id,373 },374 }375 assert must_have_event(deposit_events, total_deposit_event)376 assert must_have_event(all_netting_channel_events, total_deposit_event)377 RaidenAPI(app0.raiden).channel_close(378 registry_address,379 token_address,380 app1.raiden.address,381 )382 all_netting_channel_events = get_all_netting_channel_events(383 chain=app0.raiden.chain,384 token_network_address=token_network_identifier,385 netting_channel_identifier=channel_id,386 contract_manager=app0.raiden.contract_manager,387 )388 closed_events = get_netting_channel_closed_events(389 chain=app0.raiden.chain,390 token_network_address=token_network_identifier,391 netting_channel_identifier=channel_id,392 contract_manager=contract_manager,393 )394 closed_event = {395 'event': ChannelEvent.CLOSED,396 'args': {397 'channel_identifier': channel_id,398 'closing_participant': to_checksum_address(app0.raiden.address),399 },400 }401 assert must_have_event(closed_events, closed_event)402 assert must_have_event(all_netting_channel_events, closed_event)403 settle_expiration = app0.raiden.chain.block_number() + settle_timeout + 5404 wait_until_block(app0.raiden.chain, settle_expiration)405 all_netting_channel_events = get_all_netting_channel_events(406 chain=app0.raiden.chain,407 token_network_address=token_network_identifier,408 netting_channel_identifier=channel_id,409 contract_manager=app0.raiden.contract_manager,410 )411 settled_events = get_netting_channel_settled_events(412 chain=app0.raiden.chain,413 token_network_address=token_network_identifier,414 netting_channel_identifier=channel_id,415 contract_manager=contract_manager,416 )417 settled_event = {418 'event': ChannelEvent.SETTLED,419 'args': {420 'channel_identifier': channel_id,421 },422 }423 assert must_have_event(settled_events, settled_event)424 assert must_have_event(all_netting_channel_events, settled_event)425@pytest.mark.xfail(reason='out-of-gas for unlock and settle')426@pytest.mark.parametrize('number_of_nodes', [3])427@pytest.mark.parametrize('channels_per_node', [CHAIN])428def test_secret_revealed(raiden_chain, deposit, settle_timeout, token_addresses):429 app0, app1, app2 = raiden_chain430 registry_address = app0.raiden.default_registry.address431 token_address = token_addresses[0]432 token_network_identifier = views.get_token_network_identifier_by_token_address(433 views.state_from_app(app0),434 app0.raiden.default_registry.address,435 token_address,436 )437 hold_event_handler = HoldOffChainSecretRequest()438 app2.raiden.raiden_event_handler = hold_event_handler439 amount = 10440 identifier = 1441 target = app2.raiden.address442 secret = sha3(target)443 secrethash = sha3(secret)444 hold_event_handler.hold_secretrequest_for(secret)445 app0.raiden.start_mediated_transfer_with_secret(446 token_network_identifier,447 amount,448 target,449 identifier,450 secret,451 )452 gevent.sleep(.1) # wait for the messages453 # The secret hasn't been revealed yet454 channel_state2_1 = get_channelstate(app2, app1, token_network_identifier)455 assert len(channel_state2_1.our_state.secrethashes_to_lockedlocks) == 1456 channel.register_offchain_secret(channel_state2_1, secret, secrethash)457 # Close the channel458 # This needs to register the secrets on chain459 netting_channel_proxy = app2.raiden.chain.payment_channel(460 token_network_identifier,461 channel_state2_1.identifier,462 )463 netting_channel_proxy.channel_close(464 registry_address,465 channel_state2_1.partner_state.balance_proof,466 )467 settle_expiration = (468 app0.raiden.chain.block_number() +469 settle_timeout +470 DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS471 )472 wait_until_block(app0.raiden.chain, settle_expiration)473 assert_synced_channel_state(474 token_address,475 app1, deposit - amount, [],476 app2, deposit + amount, [],477 )478 assert_synced_channel_state(479 token_address,480 app0, deposit - amount, [],481 app1, deposit + amount, [],482 )483@pytest.mark.parametrize('number_of_nodes', [2])484def test_clear_closed_queue(raiden_network, token_addresses, deposit, network_wait):485 """ Closing a channel clears the respective message queue. """486 app0, app1 = raiden_network487 registry_address = app0.raiden.default_registry.address488 token_address = token_addresses[0]489 chain_state0 = views.state_from_app(app0)490 token_network_identifier = views.get_token_network_identifier_by_token_address(491 chain_state0,492 app0.raiden.default_registry.address,493 token_address,494 )495 token_network = views.get_token_network_by_identifier(496 chain_state0,497 token_network_identifier,498 )499 channel_identifier = get_channelstate(app0, app1, token_network_identifier).identifier500 assert channel_identifier in token_network.partneraddresses_to_channelidentifiers[501 app1.raiden.address502 ]503 with dont_handle_secret_request_mock(app0):504 # make an unconfirmed transfer to ensure the nodes have communicated505 amount = 10506 payment_identifier = 1337507 app0.raiden.mediated_transfer_async(508 token_network_identifier=token_network_identifier,509 amount=amount,510 target=app1.raiden.address,511 identifier=payment_identifier,512 )513 app1.raiden.transport.stop()514 app1.raiden.transport.get()515 # make sure to wait until the queue is created516 def has_initiator_events():517 initiator_events = app0.raiden.wal.storage.get_events()518 return must_contain_entry(initiator_events, SendLockedTransfer, {})519 assert wait_until(has_initiator_events, network_wait)520 # assert the specific queue is present521 chain_state0 = views.state_from_app(app0)522 queues0 = views.get_all_messagequeues(chain_state=chain_state0)523 assert [524 (queue_id, queue)525 for queue_id, queue in queues0.items()526 if queue_id.recipient == app1.raiden.address and527 queue_id.channel_identifier == channel_identifier and queue528 ]529 # A ChannelClose event will be generated, this will be polled by both apps530 RaidenAPI(app0.raiden).channel_close(531 registry_address,532 token_address,533 app1.raiden.address,534 )535 exception = ValueError('Could not get close event')536 with gevent.Timeout(seconds=30, exception=exception):537 waiting.wait_for_close(538 app0.raiden,539 registry_address,540 token_address,541 [channel_identifier],542 app0.raiden.alarm.sleep_time,543 )544 # assert all queues with this partner are gone or empty545 chain_state0 = views.state_from_app(app0)546 queues0 = views.get_all_messagequeues(chain_state=chain_state0)547 assert not [548 (queue_id, queue)549 for queue_id, queue in queues0.items()550 if queue_id.recipient == app1.raiden.address and queue551 ]552 chain_state1 = views.state_from_app(app1)553 queues1 = views.get_all_messagequeues(chain_state=chain_state1)554 assert not [555 (queue_id, queue)556 for queue_id, queue in queues1.items()557 if queue_id.recipient == app0.raiden.address and queue...
test_mediatedtransfer_invalid.py
Source:test_mediatedtransfer_invalid.py
1import random2import gevent3import pytest4from raiden.api.python import RaidenAPI5from raiden.constants import UINT64_MAX6from raiden.messages import Lock, LockedTransfer7from raiden.tests.utils.factories import (8 UNIT_CHAIN_ID,9 UNIT_SECRETHASH,10 make_address,11 make_privkey_address,12)13from raiden.tests.utils.geth import wait_until_block14from raiden.tests.utils.network import CHAIN15from raiden.tests.utils.transfer import (16 assert_synced_channel_state,17 get_channelstate,18 mediated_transfer,19 sign_and_inject,20 wait_assert,21)22from raiden.transfer import views23@pytest.mark.parametrize('channels_per_node', [1])24@pytest.mark.parametrize('number_of_nodes', [2])25def test_failsfast_lockedtransfer_exceeding_distributable(26 raiden_network,27 token_addresses,28 deposit,29):30 app0, app1 = raiden_network31 token_address = token_addresses[0]32 payment_network_identifier = app0.raiden.default_registry.address33 token_network_identifier = views.get_token_network_identifier_by_token_address(34 views.state_from_app(app0),35 payment_network_identifier,36 token_address,37 )38 result = app0.raiden.mediated_transfer_async(39 token_network_identifier,40 deposit * 2,41 app1.raiden.address,42 identifier=1,43 )44 assert result.successful()45 assert result.get_nowait() is False46 assert_synced_channel_state(47 token_network_identifier,48 app0, deposit, [],49 app1, deposit, [],50 )51@pytest.mark.parametrize('number_of_nodes', [2])52@pytest.mark.parametrize('channels_per_node', [0])53def test_failfast_lockedtransfer_nochannel(raiden_network, token_addresses):54 """When the node has no channels it should fail without raising exceptions."""55 token_address = token_addresses[0]56 app0, app1 = raiden_network57 amount = 1058 payment_network_identifier = app0.raiden.default_registry.address59 token_network_identifier = views.get_token_network_identifier_by_token_address(60 views.state_from_app(app0),61 payment_network_identifier,62 token_address,63 )64 async_result = app0.raiden.mediated_transfer_async(65 token_network_identifier,66 amount,67 app1.raiden.address,68 identifier=1,69 )70 assert async_result.wait() is False71@pytest.mark.parametrize('number_of_nodes', [3])72@pytest.mark.parametrize('channels_per_node', [CHAIN])73def test_receive_lockedtransfer_invalidnonce(74 raiden_network,75 number_of_nodes,76 deposit,77 token_addresses,78 reveal_timeout,79 network_wait,80):81 app0, app1, app2 = raiden_network82 token_address = token_addresses[0]83 token_network_identifier = views.get_token_network_identifier_by_token_address(84 views.state_from_app(app0),85 app0.raiden.default_registry.address,86 token_address,87 )88 channel0 = get_channelstate(app0, app1, token_network_identifier)89 amount = 1090 mediated_transfer(91 app0,92 app2,93 token_network_identifier,94 amount,95 timeout=network_wait * number_of_nodes,96 )97 amount = 1098 payment_identifier = 199 repeated_nonce = 1100 expiration = reveal_timeout * 2101 mediated_transfer_message = LockedTransfer(102 chain_id=UNIT_CHAIN_ID,103 message_identifier=random.randint(0, UINT64_MAX),104 payment_identifier=payment_identifier,105 nonce=repeated_nonce,106 token_network_address=token_network_identifier,107 token=token_address,108 channel_identifier=channel0.identifier,109 transferred_amount=amount,110 locked_amount=amount,111 recipient=app1.raiden.address,112 locksroot=UNIT_SECRETHASH,113 lock=Lock(amount, expiration, UNIT_SECRETHASH),114 target=app2.raiden.address,115 initiator=app0.raiden.address,116 fee=0,117 )118 sign_and_inject(119 mediated_transfer_message,120 app0.raiden.private_key,121 app0.raiden.address,122 app1,123 )124 with gevent.Timeout(network_wait):125 wait_assert(126 assert_synced_channel_state,127 token_network_identifier,128 app0, deposit - amount, [],129 app1, deposit + amount, [],130 )131@pytest.mark.parametrize('number_of_nodes', [2])132@pytest.mark.parametrize('channels_per_node', [1])133def test_receive_lockedtransfer_invalidsender(134 raiden_network,135 token_addresses,136 deposit,137 reveal_timeout,138):139 app0, app1 = raiden_network140 token_address = token_addresses[0]141 other_key, other_address = make_privkey_address()142 token_network_identifier = views.get_token_network_identifier_by_token_address(143 views.state_from_app(app0),144 app0.raiden.default_registry.address,145 token_address,146 )147 channel0 = get_channelstate(app0, app1, token_network_identifier)148 lock_amount = 10149 expiration = reveal_timeout * 2150 mediated_transfer_message = LockedTransfer(151 chain_id=UNIT_CHAIN_ID,152 message_identifier=random.randint(0, UINT64_MAX),153 payment_identifier=1,154 nonce=1,155 token_network_address=token_network_identifier,156 token=token_address,157 channel_identifier=channel0.identifier,158 transferred_amount=0,159 locked_amount=lock_amount,160 recipient=app0.raiden.address,161 locksroot=UNIT_SECRETHASH,162 lock=Lock(lock_amount, expiration, UNIT_SECRETHASH),163 target=app0.raiden.address,164 initiator=other_address,165 fee=0,166 )167 sign_and_inject(168 mediated_transfer_message,169 other_key,170 other_address,171 app0,172 )173 assert_synced_channel_state(174 token_network_identifier,175 app0, deposit, [],176 app1, deposit, [],177 )178@pytest.mark.parametrize('number_of_nodes', [2])179@pytest.mark.parametrize('channels_per_node', [CHAIN])180def test_receive_lockedtransfer_invalidrecipient(181 raiden_network,182 token_addresses,183 reveal_timeout,184 deposit,185):186 app0, app1 = raiden_network187 token_address = token_addresses[0]188 token_network_identifier = views.get_token_network_identifier_by_token_address(189 views.state_from_app(app0),190 app0.raiden.default_registry.address,191 token_address,192 )193 channel0 = get_channelstate(app0, app1, token_network_identifier)194 payment_identifier = 1195 invalid_recipient = make_address()196 lock_amount = 10197 expiration = reveal_timeout * 2198 mediated_transfer_message = LockedTransfer(199 chain_id=UNIT_CHAIN_ID,200 message_identifier=random.randint(0, UINT64_MAX),201 payment_identifier=payment_identifier,202 nonce=1,203 token_network_address=token_network_identifier,204 token=token_address,205 channel_identifier=channel0.identifier,206 transferred_amount=0,207 locked_amount=lock_amount,208 recipient=invalid_recipient,209 locksroot=UNIT_SECRETHASH,210 lock=Lock(lock_amount, expiration, UNIT_SECRETHASH),211 target=app1.raiden.address,212 initiator=app0.raiden.address,213 fee=0,214 )215 sign_and_inject(216 mediated_transfer_message,217 app0.raiden.private_key,218 app0.raiden.address,219 app1,220 )221 assert_synced_channel_state(222 token_network_identifier,223 app0, deposit, [],224 app1, deposit, [],225 )226@pytest.mark.parametrize('number_of_nodes', [2])227@pytest.mark.parametrize('channels_per_node', [1])228@pytest.mark.parametrize('settle_timeout', [30])229def test_received_lockedtransfer_closedchannel(230 raiden_network,231 reveal_timeout,232 token_addresses,233 deposit,234):235 app0, app1 = raiden_network236 registry_address = app0.raiden.default_registry.address237 token_address = token_addresses[0]238 token_network_identifier = views.get_token_network_identifier_by_token_address(239 views.state_from_app(app0),240 app0.raiden.default_registry.address,241 token_address,242 )243 channel0 = get_channelstate(app0, app1, token_network_identifier)244 RaidenAPI(app1.raiden).channel_close(245 registry_address,246 token_address,247 app0.raiden.address,248 )249 wait_until_block(250 app0.raiden.chain,251 app0.raiden.chain.block_number() + 1,252 )253 # Now receive one mediated transfer for the closed channel254 lock_amount = 10255 payment_identifier = 1256 expiration = reveal_timeout * 2257 mediated_transfer_message = LockedTransfer(258 chain_id=UNIT_CHAIN_ID,259 message_identifier=random.randint(0, UINT64_MAX),260 payment_identifier=payment_identifier,261 nonce=1,262 token_network_address=token_network_identifier,263 token=token_address,264 channel_identifier=channel0.identifier,265 transferred_amount=0,266 locked_amount=lock_amount,267 recipient=app1.raiden.address,268 locksroot=UNIT_SECRETHASH,269 lock=Lock(lock_amount, expiration, UNIT_SECRETHASH),270 target=app1.raiden.address,271 initiator=app0.raiden.address,272 fee=0,273 )274 sign_and_inject(275 mediated_transfer_message,276 app0.raiden.private_key,277 app0.raiden.address,278 app1,279 )280 # The local state must not change since the channel is already closed281 assert_synced_channel_state(282 token_network_identifier,283 app0, deposit, [],284 app1, deposit, [],...
test_recovery.py
Source:test_recovery.py
1import pytest2from gevent import server3from raiden import waiting4from raiden.api.python import RaidenAPI5from raiden.app import App6from raiden.message_handler import MessageHandler7from raiden.network.transport import UDPTransport8from raiden.raiden_event_handler import RaidenEventHandler9from raiden.tests.utils.events import must_contain_entry10from raiden.tests.utils.network import CHAIN11from raiden.tests.utils.transfer import assert_synced_channel_state, mediated_transfer12from raiden.transfer import views13from raiden.transfer.state_change import (14 ContractReceiveChannelClosed,15 ContractReceiveChannelSettled,16)17@pytest.mark.parametrize('deposit', [10])18@pytest.mark.parametrize('channels_per_node', [CHAIN])19@pytest.mark.parametrize('number_of_nodes', [3])20def test_recovery_happy_case(21 raiden_network,22 number_of_nodes,23 deposit,24 token_addresses,25 network_wait,26 skip_if_not_udp,27):28 app0, app1, app2 = raiden_network29 token_address = token_addresses[0]30 chain_state = views.state_from_app(app0)31 payment_network_id = app0.raiden.default_registry.address32 token_network_identifier = views.get_token_network_identifier_by_token_address(33 chain_state,34 payment_network_id,35 token_address,36 )37 # make a few transfers from app0 to app238 amount = 139 spent_amount = deposit - 240 for _ in range(spent_amount):41 mediated_transfer(42 app0,43 app2,44 token_network_identifier,45 amount,46 timeout=network_wait * number_of_nodes,47 )48 app0.raiden.stop()49 host_port = (50 app0.raiden.config['transport']['udp']['host'],51 app0.raiden.config['transport']['udp']['port'],52 )53 socket = server._udp_socket(host_port)54 new_transport = UDPTransport(55 app0.raiden.address,56 app0.discovery,57 socket,58 app0.raiden.transport.throttle_policy,59 app0.raiden.config['transport']['udp'],60 )61 raiden_event_handler = RaidenEventHandler()62 message_handler = MessageHandler()63 app0_restart = App(64 config=app0.config,65 chain=app0.raiden.chain,66 query_start_block=0,67 default_registry=app0.raiden.default_registry,68 default_secret_registry=app0.raiden.default_secret_registry,69 transport=new_transport,70 raiden_event_handler=raiden_event_handler,71 message_handler=message_handler,72 discovery=app0.raiden.discovery,73 )74 app0.stop()75 del app0 # from here on the app0_restart should be used76 app0_restart.start()77 assert_synced_channel_state(78 token_network_identifier,79 app0_restart, deposit - spent_amount, [],80 app1, deposit + spent_amount, [],81 )82 assert_synced_channel_state(83 token_network_identifier,84 app1, deposit - spent_amount, [],85 app2, deposit + spent_amount, [],86 )87 # wait for the nodes' healthcheck to update the network statuses88 waiting.wait_for_healthy(89 app0_restart.raiden,90 app1.raiden.address,91 network_wait,92 )93 waiting.wait_for_healthy(94 app1.raiden,95 app0_restart.raiden.address,96 network_wait,97 )98 mediated_transfer(99 app2,100 app0_restart,101 token_network_identifier,102 amount,103 timeout=network_wait * number_of_nodes * 2,104 )105 mediated_transfer(106 app0_restart,107 app2,108 token_network_identifier,109 amount,110 timeout=network_wait * number_of_nodes * 2,111 )112 assert_synced_channel_state(113 token_network_identifier,114 app0_restart, deposit - spent_amount, [],115 app1, deposit + spent_amount, [],116 )117 assert_synced_channel_state(118 token_network_identifier,119 app1, deposit - spent_amount, [],120 app2, deposit + spent_amount, [],121 )122@pytest.mark.parametrize('deposit', [10])123@pytest.mark.parametrize('channels_per_node', [CHAIN])124@pytest.mark.parametrize('number_of_nodes', [3])125def test_recovery_unhappy_case(126 raiden_network,127 number_of_nodes,128 deposit,129 token_addresses,130 network_wait,131 skip_if_not_udp,132 retry_timeout,133):134 app0, app1, app2 = raiden_network135 token_address = token_addresses[0]136 chain_state = views.state_from_app(app0)137 payment_network_id = app0.raiden.default_registry.address138 token_network_identifier = views.get_token_network_identifier_by_token_address(139 chain_state,140 payment_network_id,141 token_address,142 )143 # make a few transfers from app0 to app2144 amount = 1145 spent_amount = deposit - 2146 for _ in range(spent_amount):147 mediated_transfer(148 app0,149 app2,150 token_network_identifier,151 amount,152 timeout=network_wait * number_of_nodes,153 )154 app0.raiden.stop()155 host_port = (156 app0.raiden.config['transport']['udp']['host'],157 app0.raiden.config['transport']['udp']['port'],158 )159 socket = server._udp_socket(host_port)160 new_transport = UDPTransport(161 app0.raiden.address,162 app0.discovery,163 socket,164 app0.raiden.transport.throttle_policy,165 app0.raiden.config['transport']['udp'],166 )167 app0.stop()168 RaidenAPI(app1.raiden).channel_close(169 app1.raiden.default_registry.address,170 token_address,171 app0.raiden.address,172 )173 channel01 = views.get_channelstate_for(174 views.state_from_app(app1),175 app1.raiden.default_registry.address,176 token_address,177 app0.raiden.address,178 )179 waiting.wait_for_settle(180 app1.raiden,181 app1.raiden.default_registry.address,182 token_address,183 [channel01.identifier],184 retry_timeout,185 )186 raiden_event_handler = RaidenEventHandler()187 message_handler = MessageHandler()188 app0_restart = App(189 config=app0.config,190 chain=app0.raiden.chain,191 query_start_block=0,192 default_registry=app0.raiden.default_registry,193 default_secret_registry=app0.raiden.default_secret_registry,194 transport=new_transport,195 raiden_event_handler=raiden_event_handler,196 message_handler=message_handler,197 discovery=app0.raiden.discovery,198 )199 del app0 # from here on the app0_restart should be used200 app0_restart.start()201 state_changes = app0_restart.raiden.wal.storage.get_statechanges_by_identifier(202 from_identifier=0,203 to_identifier='latest',204 )205 assert must_contain_entry(state_changes, ContractReceiveChannelSettled, {206 'token_network_identifier': token_network_identifier,207 'channel_identifier': channel01.identifier,208 })209@pytest.mark.parametrize('deposit', [10])210@pytest.mark.parametrize('channels_per_node', [CHAIN])211@pytest.mark.parametrize('number_of_nodes', [2])212def test_recovery_blockchain_events(213 raiden_network,214 number_of_nodes,215 deposit,216 token_addresses,217 network_wait,218 skip_if_not_udp,219):220 """ Close one of the two raiden apps that have a channel between them,221 have the counterparty close the channel and then make sure the restarted222 app sees the change223 """224 app0, app1 = raiden_network225 token_address = token_addresses[0]226 app0.raiden.stop()227 host_port = (228 app0.raiden.config['transport']['udp']['host'],229 app0.raiden.config['transport']['udp']['port'],230 )231 socket = server._udp_socket(host_port)232 new_transport = UDPTransport(233 app0.raiden.address,234 app0.discovery,235 socket,236 app0.raiden.transport.throttle_policy,237 app0.raiden.config['transport']['udp'],238 )239 app1_api = RaidenAPI(app1.raiden)240 app1_api.channel_close(241 registry_address=app0.raiden.default_registry.address,242 token_address=token_address,243 partner_address=app0.raiden.address,244 )245 app0.stop()246 import gevent247 gevent.sleep(1)248 raiden_event_handler = RaidenEventHandler()249 message_handler = MessageHandler()250 app0_restart = App(251 config=app0.config,252 chain=app0.raiden.chain,253 query_start_block=0,254 default_registry=app0.raiden.default_registry,255 default_secret_registry=app0.raiden.default_secret_registry,256 transport=new_transport,257 raiden_event_handler=raiden_event_handler,258 message_handler=message_handler,259 discovery=app0.raiden.discovery,260 )261 del app0 # from here on the app0_restart should be used262 app0_restart.raiden.start()263 # wait for the nodes' healthcheck to update the network statuses264 waiting.wait_for_healthy(265 app0_restart.raiden,266 app1.raiden.address,267 network_wait,268 )269 waiting.wait_for_healthy(270 app1.raiden,271 app0_restart.raiden.address,272 network_wait,273 )274 restarted_state_changes = app0_restart.raiden.wal.storage.get_statechanges_by_identifier(275 0,276 'latest',277 )...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!