Esempio n. 1
0
def blockchain_listener(web3, contracts_manager):
    blockchain_listener = BlockchainListener(
        web3,
        contracts_manager,
        'TokenNetwork',
        poll_interval=0,
    )
    return blockchain_listener
def blockchain_listener(web3, contracts_manager):
    blockchain_listener = BlockchainListener(
        web3=web3,
        contract_manager=contracts_manager,
        contract_name=CONTRACT_TOKEN_NETWORK,
        poll_interval=0,
    )
    return blockchain_listener
Esempio n. 3
0
def blockchain_listener(web3, contracts_manager):
    blockchain_listener = BlockchainListener(
        web3,
        contracts_manager,
        'TokenNetwork',
        poll_interval=0,
    )
    blockchain_listener.required_confirmations = 1
    blockchain_listener.start()
    yield blockchain_listener
    blockchain_listener.stop()
Esempio n. 4
0
def main(
    eth_rpc,
    monitoring_channel,
    matrix_homeserver,
    matrix_username,
    matrix_password,
    token_network_addresses,
):
    """Console script for pathfinder."""

    # setup logging
    logging.basicConfig(level=logging.INFO)
    # logging.getLogger('urllib3.connectionpool').setLevel(logging.DEBUG)

    log.info("Starting Raiden Pathfinding Service")

    token_network_addresses = check_supplied_token_network_addresses(token_network_addresses)
    if token_network_addresses:
        log.info('Following {} networks.')
    else:
        log.info('Following all networks.')

    with no_ssl_verification():
        service = None
        try:
            log.info('Starting Matrix Transport...')
            transport = MatrixTransport(
                matrix_homeserver,
                matrix_username,
                matrix_password,
                monitoring_channel
            )

            log.info('Starting Web3 client...')
            web3 = Web3(HTTPProvider(eth_rpc))

            module_dir = os.path.dirname(pathfinder.__file__)
            contracts_path = os.path.join(module_dir, 'contract', 'contracts_12032018.json')
            contract_manager = ContractManager(contracts_path)

            log.info('Starting TokenNetwork Listener...')
            token_network_listener = BlockchainListener(
                web3,
                contract_manager,
                'TokenNetwork',
            )

            log.info('Starting Pathfinding Service...')
            if token_network_addresses:
                service = PathfindingService(
                    web3,
                    contract_manager,
                    transport,
                    token_network_listener,
                    follow_networks=token_network_addresses)
            else:
                log.info('Starting TokenNetworkRegistry Listener...')
                token_network_registry_listener = BlockchainListener(
                    web3,
                    contract_manager,
                    'TokenNetworkRegistry',
                )

                service = PathfindingService(
                    web3,
                    contract_manager,
                    transport,
                    token_network_listener,
                    token_network_registry_listener=token_network_registry_listener)

            service.run()
        except (KeyboardInterrupt, SystemExit):
            print('Exiting...')
        finally:
            if service:
                log.info('Stopping Pathfinding Service...')
                service.stop()

    return 0
def test_reorg(
    web3: Web3,
    wait_for_blocks,
    generate_raiden_clients,
    blockchain_listener: BlockchainListener,
    ethereum_tester,
):
    blockchain_listener.required_confirmations = 5
    blockchain_listener.start()
    blockchain_listener.wait_sync()

    unconfirmed_channel_open_events = []
    blockchain_listener.add_unconfirmed_listener(
        'ChannelOpened',
        lambda e: unconfirmed_channel_open_events.append(e),
    )

    c1, c2 = generate_raiden_clients(2)
    snapshot_id = web3.testing.snapshot()

    # create unconfirmed channel
    c1.open_channel(c2.address)
    wait_for_blocks(0)
    assert len(unconfirmed_channel_open_events) == 1
    assert unconfirmed_channel_open_events[0]['args'][
        'participant1'] == c1.address
    assert unconfirmed_channel_open_events[0]['args'][
        'participant2'] == c2.address

    # remove unconfirmed channel opening with reorg
    web3.testing.revert(snapshot_id)

    # run the BlockchainListener again, it should have a lower head_number now
    old_head_number = blockchain_listener.unconfirmed_head_number
    wait_for_blocks(0)
    new_head_number = blockchain_listener.unconfirmed_head_number

    assert old_head_number > new_head_number

    # test that a chain reorg of one block is handled
    # this created a channel first, then reverts and creates a different channel
    unconfirmed_channel_open_events.clear()
    c1.open_channel(c2.address)
    wait_for_blocks(0)
    assert len(unconfirmed_channel_open_events) == 1
    assert unconfirmed_channel_open_events[0]['args'][
        'participant1'] == c1.address
    assert unconfirmed_channel_open_events[0]['args'][
        'participant2'] == c2.address
    web3.testing.revert(snapshot_id)
    c2.open_channel(c1.address)
    wait_for_blocks(0)
    assert len(unconfirmed_channel_open_events) == 2
    assert unconfirmed_channel_open_events[1]['args'][
        'participant1'] == c2.address
    assert unconfirmed_channel_open_events[1]['args'][
        'participant2'] == c1.address

    web3.testing.revert(snapshot_id)

    # test a big chain reorg (> required_confirmations)
    confirmed_channel_open_events = []
    blockchain_listener.add_confirmed_listener(
        'ChannelOpened',
        lambda e: confirmed_channel_open_events.append(e),
    )
    c1.open_channel(c2.address)

    # create a new event and wait till it's confirmed
    wait_for_blocks(5)

    assert len(confirmed_channel_open_events) == 1

    # revert the chain, this should kill the process
    web3.testing.revert(snapshot_id)

    with pytest.raises(SystemExit):
        wait_for_blocks(0)

    blockchain_listener.stop()
def test_blockchain_listener(
    web3: Web3,
    wait_for_blocks,
    generate_raiden_clients,
    blockchain_listener: BlockchainListener,
    ethereum_tester,
):
    blockchain_listener.required_confirmations = 4
    blockchain_listener.start()
    blockchain_listener.wait_sync()

    unconfirmed_channel_open_events = []
    confirmed_channel_open_events = []

    blockchain_listener.add_unconfirmed_listener(
        'ChannelOpened',
        lambda e: unconfirmed_channel_open_events.append(e),
    )
    blockchain_listener.add_confirmed_listener(
        'ChannelOpened',
        lambda e: confirmed_channel_open_events.append(e),
    )

    # create unconfirmed channel
    c1, c2 = generate_raiden_clients(2)
    c1.open_channel(c2.address)

    # the unconfirmed event should be available directly
    wait_for_blocks(0)
    assert len(unconfirmed_channel_open_events) == 1
    assert unconfirmed_channel_open_events[0]['args'][
        'participant1'] == c1.address
    assert unconfirmed_channel_open_events[0]['args'][
        'participant2'] == c2.address

    # the confirmed event should be available after 4 more blocks as set above
    wait_for_blocks(4)
    assert len(confirmed_channel_open_events) == 1
    assert confirmed_channel_open_events[0]['args'][
        'participant1'] == c1.address
    assert confirmed_channel_open_events[0]['args'][
        'participant2'] == c2.address

    blockchain_listener.stop()
def test_blockchain_listener_nonexistant_contract(
    web3: Web3,
    wait_for_blocks,
    generate_raiden_clients,
    blockchain_listener: BlockchainListener,
    ethereum_tester,
):
    blockchain_listener.required_confirmations = 4
    blockchain_listener.contract_address = '0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa'
    blockchain_listener.start()
    blockchain_listener.wait_sync()

    unconfirmed_channel_open_events = []
    confirmed_channel_open_events = []

    blockchain_listener.add_unconfirmed_listener(
        'ChannelOpened',
        lambda e: unconfirmed_channel_open_events.append(e),
    )
    blockchain_listener.add_confirmed_listener(
        'ChannelOpened',
        lambda e: confirmed_channel_open_events.append(e),
    )

    # create unconfirmed channel
    c1, c2 = generate_raiden_clients(2)
    c1.open_channel(c2.address)

    # the unconfirmed event should be available directly
    wait_for_blocks(0)
    assert len(unconfirmed_channel_open_events) == 0

    # the confirmed event should be available after 4 more blocks as set above
    wait_for_blocks(4)
    assert len(confirmed_channel_open_events) == 0

    blockchain_listener.stop()
Esempio n. 8
0
def main(
    eth_rpc,
    monitoring_channel,
    matrix_homeserver,
    matrix_username,
    matrix_password,
    token_network_addresses,
):
    """Console script for pathfinder."""

    # setup logging
    logging.basicConfig(level=logging.INFO)
    # logging.getLogger('urllib3.connectionpool').setLevel(logging.DEBUG)

    log.info("Starting Raiden Pathfinding Service")

    try:
        log.info(f'Starting Web3 client for node at {eth_rpc}')
        web3 = Web3(HTTPProvider(eth_rpc))

        token_network_addresses = check_supplied_token_network_addresses(
            token_network_addresses, web3)
        if token_network_addresses:
            log.info(f'Following {len(token_network_addresses)} network(s):')
        else:
            log.info('Following all networks.')
    except ConnectionError as error:
        log.error(
            'Can not connect to the Ethereum client. Please check that it is running and that '
            'your settings are correct.')
        sys.exit()

    with no_ssl_verification():
        service = None
        try:
            log.info('Starting Matrix Transport...')
            transport = MatrixTransport(matrix_homeserver, matrix_username,
                                        matrix_password, monitoring_channel)

            log.info('Starting TokenNetwork Listener...')
            token_network_listener = BlockchainListener(
                web3,
                CONTRACT_MANAGER,
                'TokenNetwork',
            )

            log.info('Starting Pathfinding Service...')
            if token_network_addresses:
                service = PathfindingService(
                    CONTRACT_MANAGER,
                    transport,
                    token_network_listener,
                    chain_id=int(web3.net.version),
                    follow_networks=token_network_addresses)
            else:
                log.info('Starting TokenNetworkRegistry Listener...')
                token_network_registry_listener = BlockchainListener(
                    web3,
                    CONTRACT_MANAGER,
                    'TokenNetworkRegistry',
                )

                service = PathfindingService(CONTRACT_MANAGER,
                                             transport,
                                             token_network_listener,
                                             chain_id=int(web3.net.version),
                                             token_network_registry_listener=
                                             token_network_registry_listener)

            service.run()
        except (KeyboardInterrupt, SystemExit):
            print('Exiting...')
        finally:
            if service:
                log.info('Stopping Pathfinding Service...')
                service.stop()

    return 0