Example #1
0
def test_get_configuration():
    root_logger = slogging.getLogger()
    root_logger.manager.loggerDict = {}  # clear old loggers
    config_string = ":INFO,a:TRACE,a.b:DEBUG"
    log_json = False
    slogging.configure(config_string=config_string, log_json=log_json)
    config = slogging.get_configuration()
    assert config["log_json"] == log_json
    assert set(config["config_string"].split(",")) == set(config_string.split(","))

    log_json = True
    slogging.configure(config_string=config_string, log_json=log_json)
    config = slogging.get_configuration()
    assert config["log_json"] == log_json
    assert set(config["config_string"].split(",")) == set(config_string.split(","))

    # set config differntly
    slogging.configure(config_string=":TRACE", log_json=False)
    config2 = slogging.get_configuration()

    # test whether we get original config
    slogging.configure(**config)
    config = slogging.get_configuration()
    assert config["log_json"] == log_json
    assert set(config["config_string"].split(",")) == set(config_string.split(","))
Example #2
0
def test_logging_source_file(caplog, log_method):
    slogging.configure(":trace")
    logger = slogging.getLogger("test")
    getattr(logger, log_method)("testmessage")

    v = caplog.records[0]
    print(v.pathname, v.module, v.name)
    assert caplog.records[0].module == "test_logging"
    def _run(self):  # pylint: disable=method-hidden
        self.interrupt.wait()
        print('\n' * 2)
        print('Entering Console' + OKGREEN)
        print('Tip:' + OKBLUE)
        print_usage()

        # Remove handlers that log to stderr
        root = getLogger()
        for handler in root.handlers[:]:
            if isinstance(handler, StreamHandler) and handler.stream == sys.stderr:
                root.removeHandler(handler)

        stream = io.StringIO()
        handler = StreamHandler(stream=stream)
        handler.formatter = Formatter(u'%(levelname)s:%(name)s %(message)s')
        root.addHandler(handler)

        def lastlog(n=10, prefix=None, level=None):
            """ Print the last `n` log lines to stdout.
            Use `prefix='p2p'` to filter for a specific logger.
            Use `level=INFO` to filter for a specific level.
            Level- and prefix-filtering are applied before tailing the log.
            """
            lines = (stream.getvalue().strip().split('\n') or [])
            if prefix:
                lines = [
                    line
                    for line in lines
                    if line.split(':')[1].startswith(prefix)
                ]
            if level:
                lines = [
                    line
                    for line in lines
                    if line.split(':')[0] == level
                ]
            for line in lines[-n:]:
                print(line)

        self.console_locals['lastlog'] = lastlog

        err = io.StringIO()
        sys.stderr = err

        def lasterr(n=1):
            """ Print the last `n` entries of stderr to stdout. """
            for line in (err.getvalue().strip().split('\n') or [])[-n:]:
                print(line)

        self.console_locals['lasterr'] = lasterr

        IPython.start_ipython(argv=['--gui', 'gevent'], user_ns=self.console_locals)
        self.interrupt.clear()

        sys.exit(0)
Example #4
0
def run(ctx, node_id, console, fake_account):
    """Start the daemon"""
    config = ctx.obj['config']
    config['node']['privkey_hex'] = privkeys[node_id]
    config['discovery']['listen_port'] += node_id
    config['p2p']['listen_port'] += node_id
    log.info("starting", config=config)

    if config['node']['data_dir'] and not os.path.exists(config['node']['data_dir']):
        os.makedirs(config['node']['data_dir'])

    app = Casper(config)
    app.start_console = console

    for service in services:
        assert issubclass(service, BaseService)
        assert service.name not in app.services
        service.register_with_app(app)
        assert hasattr(app.services, service.name)
        # If this service is the account service, then attempt to unlock the coinbase
        if service is AccountsService:
            # If the fake_account flag is True, create a temparary fake account based on node_id
            if fake_account:
                account = Account.new('', decode_hex(privkeys[node_id]))
                app.services.accounts.add_account(account, store=False)
                continue
            unlock_accounts(ctx.obj['unlock'], app.services.accounts, password=ctx.obj['password'])
            try:
                app.services.accounts.coinbase
            except ValueError as e:
                log.fatal('invalid coinbase', coinbase=config.get('pow', {}).get('coinbase_hex'),
                          error=e.message)
                sys.exit()

    # start app
    log.info('starting')
    app.start()

    if ctx.obj['log_file']:
        log.info("Logging to file %s", ctx.obj['log_file'])
        # User requested file logging - remove stderr handler
        root_logger = slogging.getLogger()
        for hdl in root_logger.handlers:
            if isinstance(hdl, StreamHandler) and hdl.stream == sys.stderr:
                root_logger.removeHandler(hdl)
                break

    # wait for interrupt
    evt = Event()
    gevent.signal(signal.SIGQUIT, evt.set)
    gevent.signal(signal.SIGTERM, evt.set)
    evt.wait()

    # finally stop
    app.stop()
Example #5
0
def test_logging_reconfigure():
    config_string = ":WARNING"
    config_string1 = ":DEBUG,eth:INFO"
    config_string2 = ":DEBUG,eth.vm:INFO"
    main_logger = slogging.getLogger()

    slogging.configure(config_string)
    assert len(main_logger.handlers) == 2  # pytest-capturelog adds it's own handler
    slogging.configure(config_string)
    assert len(main_logger.handlers) == 2  # pytest-capturelog adds it's own handler

    eth_logger = slogging.getLogger("eth")
    slogging.configure(config_string1)
    assert len(eth_logger.handlers) == 0
    slogging.configure(config_string1)
    assert len(eth_logger.handlers) == 0

    eth_vm_logger = slogging.getLogger("eth.vm")
    slogging.configure(config_string2)
    assert len(eth_vm_logger.handlers) == 0
    slogging.configure(config_string2)
    assert len(eth_vm_logger.handlers) == 0
Example #6
0
def test_logging_reconfigure():
    config_string = ':WARNING'
    config_string1 = ':DEBUG,eth:INFO'
    config_string2 = ':DEBUG,eth.vm:INFO'
    main_logger = slogging.getLogger()

    slogging.configure(config_string)
    assert len(main_logger.handlers) == 2  # pytest-capturelog adds it's own handler
    slogging.configure(config_string)
    assert len(main_logger.handlers) == 2  # pytest-capturelog adds it's own handler

    eth_logger = slogging.getLogger('eth')
    slogging.configure(config_string1)
    assert len(eth_logger.handlers) == 0
    slogging.configure(config_string1)
    assert len(eth_logger.handlers) == 0

    eth_vm_logger = slogging.getLogger('eth.vm')
    slogging.configure(config_string2)
    assert len(eth_vm_logger.handlers) == 0
    slogging.configure(config_string2)
    assert len(eth_vm_logger.handlers) == 0
Example #7
0
def test_bound_logger(caplog):
    slogging.configure(config_string=":trace")
    real_log = slogging.getLogger()

    bound_log_1 = real_log.bind(key1="value1")
    with caplog.at_level(slogging.TRACE):
        bound_log_1.info("test1")
        assert "test1" in caplog.text
        assert "key1=value1" in caplog.text

    bound_log_2 = bound_log_1.bind(key2="value2")
    with caplog.at_level(slogging.TRACE):
        bound_log_2.info("test2")
        assert "test2" in caplog.text
        assert "key1=value1" in caplog.text
        assert "key2=value2" in caplog.text
Example #8
0
def test_bound_logger_isolation(caplog):
    """
    Ensure bound loggers don't "contaminate" their parent
    """
    slogging.configure(config_string=":trace")
    real_log = slogging.getLogger()

    bound_log_1 = real_log.bind(key1="value1")
    with caplog.at_level(slogging.TRACE):
        bound_log_1.info("test1")
        records = caplog.records
        assert len(records) == 1
        assert "test1" in records[0].msg
        assert "key1=value1" in records[0].msg

    with caplog.at_level(slogging.TRACE):
        real_log.info("test2")
        records = caplog.records
        assert len(records) == 2
        assert "test2" in records[1].msg
        assert "key1=value1" not in records[1].msg
Example #9
0
def smoketest(ctx, debug, **kwargs):
    """ Test, that the raiden installation is sane.
    """
    from raiden.api.python import RaidenAPI
    from raiden.blockchain.abi import get_static_or_compile
    from raiden.utils import get_contract_path

    # Check the solidity compiler early in the smoketest.
    #
    # Binary distributions don't need the solidity compiler but source
    # distributions do. Since this is checked by `get_static_or_compile`
    # function, use it as a proxy for validating the setup.
    get_static_or_compile(
        get_contract_path('HumanStandardToken.sol'),
        'HumanStandardToken',
    )

    report_file = tempfile.mktemp(suffix='.log')
    open(report_file, 'w+')

    def append_report(subject, data):
        with open(report_file, 'a') as handler:
            handler.write('{:=^80}'.format(' %s ' % subject.upper()) + os.linesep)
            if data is not None:
                if isinstance(data, bytes):
                    data = data.decode()
                handler.writelines([data + os.linesep])

    append_report('raiden version', json.dumps(get_system_spec()))
    append_report('raiden log', None)

    print('[1/5] getting smoketest configuration')
    smoketest_config = load_or_create_smoketest_config()

    print('[2/5] starting ethereum')
    ethereum, ethereum_config = start_ethereum(smoketest_config['genesis'])

    print('[3/5] starting raiden')

    # setup logging to log only into our report file
    slogging.configure(':DEBUG', log_file=report_file)
    root = slogging.getLogger()
    for handler in root.handlers:
        if isinstance(handler, slogging.logging.StreamHandler):
            root.handlers.remove(handler)
            break
    # setup cli arguments for starting raiden
    args = dict(
        discovery_contract_address=smoketest_config['contracts']['discovery_address'],
        registry_contract_address=smoketest_config['contracts']['registry_address'],
        eth_rpc_endpoint='http://127.0.0.1:{}'.format(ethereum_config['rpc']),
        keystore_path=ethereum_config['keystore'],
        address=ethereum_config['address'],
    )
    for option in app.params:
        if option.name in args.keys():
            args[option.name] = option.process_value(ctx, args[option.name])
        else:
            args[option.name] = option.default

    password_file = os.path.join(args['keystore_path'], 'password')
    with open(password_file, 'w') as handler:
        handler.write('password')

    args['mapped_socket'] = None
    args['password_file'] = click.File()(password_file)
    args['datadir'] = args['keystore_path']
    args['api_address'] = 'localhost:' + str(next(get_free_port('127.0.0.1', 5001)))
    args['sync_check'] = False

    # invoke the raiden app
    app_ = ctx.invoke(app, **args)

    raiden_api = RaidenAPI(app_.raiden)
    rest_api = RestAPI(raiden_api)
    api_server = APIServer(rest_api)
    (api_host, api_port) = split_endpoint(args['api_address'])
    api_server.start(api_host, api_port)

    success = False
    try:
        print('[4/5] running smoketests...')
        error = run_smoketests(app_.raiden, smoketest_config, debug=debug)
        if error is not None:
            append_report('smoketest assertion error', error)
        else:
            success = True
    finally:
        app_.stop()
        ethereum.send_signal(2)

        err, out = ethereum.communicate()
        append_report('geth init stdout', ethereum_config['init_log_out'].decode('utf-8'))
        append_report('geth init stderr', ethereum_config['init_log_err'].decode('utf-8'))
        append_report('ethereum stdout', out)
        append_report('ethereum stderr', err)
        append_report('smoketest configuration', json.dumps(smoketest_config))
    if success:
        print('[5/5] smoketest successful, report was written to {}'.format(report_file))
    else:
        print('[5/5] smoketest had errors, report was written to {}'.format(report_file))
        sys.exit(1)
Example #10
0
def run(ctx, **kwargs):
    # pylint: disable=too-many-locals,too-many-branches,too-many-statements

    if ctx.invoked_subcommand is None:
        print('Welcome to Raiden, version {}!'.format(get_system_spec()['raiden']))
        from raiden.ui.console import Console
        from raiden.api.python import RaidenAPI

        slogging.configure(
            kwargs['logging'],
            log_json=kwargs['log_json'],
            log_file=kwargs['logfile']
        )
        if kwargs['logfile']:
            # Disable stream logging
            root = slogging.getLogger()
            for handler in root.handlers:
                if isinstance(handler, slogging.logging.StreamHandler):
                    root.handlers.remove(handler)
                    break

        # TODO:
        # - Ask for confirmation to quit if there are any locked transfers that did
        # not timeout.
        (listen_host, listen_port) = split_endpoint(kwargs['listen_address'])
        try:
            with SocketFactory(listen_host, listen_port, strategy=kwargs['nat']) as mapped_socket:
                kwargs['mapped_socket'] = mapped_socket

                app_ = ctx.invoke(app, **kwargs)

                domain_list = []
                if kwargs['rpccorsdomain']:
                    if ',' in kwargs['rpccorsdomain']:
                        for domain in kwargs['rpccorsdomain'].split(','):
                            domain_list.append(str(domain))
                    else:
                        domain_list.append(str(kwargs['rpccorsdomain']))

                if ctx.params['rpc']:
                    raiden_api = RaidenAPI(app_.raiden)
                    rest_api = RestAPI(raiden_api)
                    api_server = APIServer(
                        rest_api,
                        cors_domain_list=domain_list,
                        web_ui=ctx.params['web_ui'],
                        eth_rpc_endpoint=ctx.params['eth_rpc_endpoint'],
                    )
                    (api_host, api_port) = split_endpoint(kwargs['api_address'])
                    api_server.start(api_host, api_port)

                    print(
                        'The Raiden API RPC server is now running at http://{}:{}/.\n\n'
                        'See the Raiden documentation for all available endpoints at\n'
                        'http://raiden-network.readthedocs.io/en/stable/rest_api.html'.format(
                            api_host,
                            api_port,
                        )
                    )

                if ctx.params['console']:
                    console = Console(app_)
                    console.start()

                # wait for interrupt
                event = gevent.event.Event()
                gevent.signal(signal.SIGQUIT, event.set)
                gevent.signal(signal.SIGTERM, event.set)
                gevent.signal(signal.SIGINT, event.set)

                gevent.signal(signal.SIGUSR1, toogle_cpu_profiler)
                gevent.signal(signal.SIGUSR2, toggle_trace_profiler)

                event.wait()
                print('Signal received. Shutting down ...')
                try:
                    api_server.stop()
                except NameError:
                    pass
        except socket.error as v:
            if v.args[0] == errno.EADDRINUSE:
                print('ERROR: Address %s:%s is in use. '
                      'Use --listen-address <host:port> to specify port to listen on.' %
                      (listen_host, listen_port))
                sys.exit(1)
            raise
        app_.stop(leave_channels=False)
    else:
        # Pass parsed args on to subcommands.
        ctx.obj = kwargs
# -*- coding: utf-8 -*-
import itertools
import os

import pytest
import gevent
from ethereum import slogging

from raiden.transfer.state import CHANNEL_STATE_SETTLED

log = slogging.getLogger(__name__)


@pytest.mark.parametrize('number_of_nodes', [2])
@pytest.mark.parametrize('number_of_tokens', [1])
@pytest.mark.parametrize('channels_per_node', [1])
@pytest.mark.parametrize('settle_timeout', [6])
@pytest.mark.parametrize('reveal_timeout', [3])
@pytest.mark.parametrize('in_memory_database', [False])
def test_close_raiden_app_leave_channels(raiden_network):
    for app in raiden_network:
        app.stop(leave_channels=True)
        assert os.path.exists(app.raiden.serialization_file)


@pytest.mark.parametrize('number_of_nodes', [2])
@pytest.mark.parametrize('number_of_tokens', [1])
@pytest.mark.parametrize('channels_per_node', [1])
@pytest.mark.parametrize('settle_timeout', [6])
@pytest.mark.parametrize('reveal_timeout', [3])
def test_leaving(raiden_network, token_addresses):
# -*- coding: utf-8 -*-
import socket

from ethereum import slogging

from raiden.exceptions import UnknownAddress
from raiden.utils import (
    host_port_to_endpoint,
    isaddress,
    pex,
    split_endpoint,
)
from raiden.exceptions import InvalidAddress

log = slogging.getLogger(__name__)


class Discovery(object):
    """ Mock mapping address: host, port """
    def __init__(self):
        self.nodeid_to_hostport = dict()

    def register(self, node_address, host, port):
        if not isaddress(node_address):
            raise ValueError('node_address must be a valid address')

        try:
            socket.inet_pton(socket.AF_INET, host)
        except OSError:
            raise ValueError('invalid ip address provided: {}'.format(host))
Example #13
0
def test_set_level():
    slogging.set_level("test", "CRITICAL")
    assert slogging.getLogger("test").level == logging.CRITICAL
Example #14
0
from ethereum.utils import sha3

from raiden.app import DEFAULT_SETTLE_TIMEOUT
from raiden.tests.utils.mock_client import (
    BlockChainServiceMock,
    MOCK_REGISTRY_ADDRESS,
)
from raiden.network.transport import UDPTransport
from raiden.tests.utils.network import create_network
from raiden.tests.benchmark.utils import (
    print_serialization,
    print_slow_function,
    print_slow_path,
)

log = slogging.getLogger('test.speed')  # pylint: disable=invalid-name


def setup_apps(amount, assets, num_transfers, num_nodes, channels_per_node):
    assert len(assets) <= num_nodes

    deposit = amount * num_transfers

    private_keys = [
        sha3('mediated_transfer:{}'.format(position))
        for position in range(num_nodes)
    ]

    BlockChainServiceMock.reset()
    blockchain_services = list()
    for privkey in private_keys:
Example #15
0
def smoketest(ctx, debug, **kwargs):  # pylint: disable=unused-argument
    """ Test, that the raiden installation is sane."""
    from raiden.api.python import RaidenAPI
    from raiden.blockchain.abi import get_static_or_compile
    from raiden.utils import get_contract_path

    # Check the solidity compiler early in the smoketest.
    #
    # Binary distributions don't need the solidity compiler but source
    # distributions do. Since this is checked by `get_static_or_compile`
    # function, use it as a proxy for validating the setup.
    get_static_or_compile(
        get_contract_path('HumanStandardToken.sol'),
        'HumanStandardToken',
    )

    report_file = tempfile.mktemp(suffix='.log')
    open(report_file, 'w+')

    def append_report(subject, data):
        with open(report_file, 'a', encoding='UTF-8') as handler:
            handler.write('{:=^80}'.format(' %s ' % subject.upper()) +
                          os.linesep)
            if data is not None:
                if isinstance(data, bytes):
                    data = data.decode()
                handler.writelines([data + os.linesep])

    append_report('raiden version', json.dumps(get_system_spec()))
    append_report('raiden log', None)

    print('[1/5] getting smoketest configuration')
    smoketest_config = load_or_create_smoketest_config()

    print('[2/5] starting ethereum')
    ethereum, ethereum_config = start_ethereum(smoketest_config['genesis'])

    print('[3/5] starting raiden')

    # setup logging to log only into our report file
    slogging.configure(':DEBUG', log_file=report_file)
    root = slogging.getLogger()
    for handler in root.handlers:
        if isinstance(handler, slogging.logging.StreamHandler):
            root.handlers.remove(handler)
            break
    # setup cli arguments for starting raiden
    args = dict(
        discovery_contract_address=smoketest_config['contracts']
        ['discovery_address'],
        registry_contract_address=smoketest_config['contracts']
        ['registry_address'],
        eth_rpc_endpoint='http://127.0.0.1:{}'.format(ethereum_config['rpc']),
        keystore_path=ethereum_config['keystore'],
        address=ethereum_config['address'],
    )
    for option in app.params:
        if option.name in args.keys():
            args[option.name] = option.process_value(ctx, args[option.name])
        else:
            args[option.name] = option.default

    password_file = os.path.join(args['keystore_path'], 'password')
    with open(password_file, 'w') as handler:
        handler.write('password')

    port = next(get_free_port('127.0.0.1', 5001))
    args['password_file'] = click.File()(password_file)
    args['datadir'] = args['keystore_path']
    args['api_address'] = 'localhost:' + str(port)
    args['sync_check'] = False

    with SocketFactory('127.0.0.1', port, strategy='none') as mapped_socket:
        args['mapped_socket'] = mapped_socket

        # invoke the raiden app
        app_ = ctx.invoke(app, **args)

        raiden_api = RaidenAPI(app_.raiden)
        rest_api = RestAPI(raiden_api)
        api_server = APIServer(rest_api)
        (api_host, api_port) = split_endpoint(args['api_address'])
        api_server.start(api_host, api_port)

        success = False
        try:
            print('[4/5] running smoketests...')
            error = run_smoketests(app_.raiden, smoketest_config, debug=debug)
            if error is not None:
                append_report('smoketest assertion error', error)
            else:
                success = True
        finally:
            app_.stop()
            ethereum.send_signal(2)

            err, out = ethereum.communicate()
            append_report('geth init stdout',
                          ethereum_config['init_log_out'].decode('utf-8'))
            append_report('geth init stderr',
                          ethereum_config['init_log_err'].decode('utf-8'))
            append_report('ethereum stdout', out)
            append_report('ethereum stderr', err)
            append_report('smoketest configuration',
                          json.dumps(smoketest_config))
        if success:
            print(
                '[5/5] smoketest successful, report was written to {}'.format(
                    report_file))
        else:
            print(
                '[5/5] smoketest had errors, report was written to {}'.format(
                    report_file))
            sys.exit(1)
Example #16
0
def test_logging_reconfigure_levels(config, logger, level):
    slogging.configure(config)
    assert slogging.getLogger(logger).level == getattr(logging, level)
Example #17
0
from cachetools.func import ttl_cache
from matrix_client.errors import MatrixRequestError
from matrix_client.room import Room as MatrixRoom
from typing import List

from matrix_client.user import User

from .utils import geventify_callback
from ethereum.slogging import getLogger

log = getLogger(__name__)


class Room(MatrixRoom):
    """ Matrix `Room` subclass that invokes listener callbacks in separate greenlets """

    def __init__(self, client, room_id):
        super().__init__(client, room_id)
        self._members = {}

    def add_listener(self, callback, event_type=None):
        return super().add_listener(geventify_callback(callback), event_type)

    def add_ephemeral_listener(self, callback, event_type=None):
        return super().add_ephemeral_listener(geventify_callback(callback), event_type)

    def add_state_listener(self, callback, event_type=None):
        super().add_state_listener(geventify_callback(callback), event_type)

    @ttl_cache(ttl=10)
    def get_joined_members(self) -> List[User]:
Example #18
0
# -*- coding: utf8 -*-
import time

import gevent

from raiden.transport import UDPTransport
from raiden.app import create_network
from raiden.tasks import TransferTask
from ethereum import slogging

log = slogging.getLogger('test.speed')
slogging.configure(":error")


def test_mediated_transfer(num_transfers=100, num_nodes=10, num_assets=1, channels_per_node=2):

    apps = create_network(
        num_nodes=num_nodes,
        num_assets=num_assets,
        channels_per_node=channels_per_node,
        transport_class=UDPTransport)

    def start_transfers(idx, num_transfers):
        a0 = apps[idx]

        # channels
        assets = sorted(a0.raiden.assetmanagers.keys())
        asset = assets[idx]
        am0 = a0.raiden.assetmanagers[asset]

        # search for a path of length=2 A > B > C
Example #19
0
def test_set_level():
    slogging.set_level('test', 'CRITICAL')
    assert slogging.getLogger('test').level == logging.CRITICAL
Example #20
0
def test_highlight(caplog):
    slogging.configure(log_json=False)
    log = slogging.getLogger()

    log.DEV("testmessage")
    assert "\033[91mtestmessage \033[0m" in caplog.records[0].msg
Example #21
0
def run(ctx, **kwargs):
    # pylint: disable=too-many-locals,too-many-branches,too-many-statements

    if ctx.invoked_subcommand is None:
        print('Welcome to Raiden, version {}!'.format(
            get_system_spec()['raiden']))
        from raiden.ui.console import Console
        from raiden.api.python import RaidenAPI

        slogging.configure(kwargs['logging'],
                           log_json=kwargs['log_json'],
                           log_file=kwargs['logfile'])
        if kwargs['logfile']:
            # Disable stream logging
            root = slogging.getLogger()
            for handler in root.handlers:
                if isinstance(handler, slogging.logging.StreamHandler):
                    root.handlers.remove(handler)
                    break

        # TODO:
        # - Ask for confirmation to quit if there are any locked transfers that did
        # not timeout.
        (listen_host, listen_port) = split_endpoint(kwargs['listen_address'])
        try:
            with SocketFactory(listen_host,
                               listen_port,
                               strategy=kwargs['nat']) as mapped_socket:
                kwargs['mapped_socket'] = mapped_socket

                app_ = ctx.invoke(app, **kwargs)

                domain_list = []
                if kwargs['rpccorsdomain']:
                    if ',' in kwargs['rpccorsdomain']:
                        for domain in kwargs['rpccorsdomain'].split(','):
                            domain_list.append(str(domain))
                    else:
                        domain_list.append(str(kwargs['rpccorsdomain']))

                if ctx.params['rpc']:
                    raiden_api = RaidenAPI(app_.raiden)
                    rest_api = RestAPI(raiden_api)
                    api_server = APIServer(
                        rest_api,
                        cors_domain_list=domain_list,
                        web_ui=ctx.params['web_ui'],
                        eth_rpc_endpoint=ctx.params['eth_rpc_endpoint'],
                    )
                    (api_host,
                     api_port) = split_endpoint(kwargs["api_address"])
                    api_server.start(api_host, api_port)

                    print(
                        "The Raiden API RPC server is now running at http://{}:{}/.\n\n"
                        "See the Raiden documentation for all available endpoints at\n"
                        "http://raiden-network.readthedocs.io/en/stable/rest_api.html"
                        .format(
                            api_host,
                            api_port,
                        ))

                if ctx.params['console']:
                    console = Console(app_)
                    console.start()

                # wait for interrupt
                event = gevent.event.Event()
                gevent.signal(signal.SIGQUIT, event.set)
                gevent.signal(signal.SIGTERM, event.set)
                gevent.signal(signal.SIGINT, event.set)

                gevent.signal(signal.SIGUSR1, toogle_cpu_profiler)
                gevent.signal(signal.SIGUSR2, toggle_trace_profiler)

                event.wait()

                try:
                    api_server.stop()
                except NameError:
                    pass
        except socket.error as v:
            if v.args[0] == errno.EADDRINUSE:
                print(
                    "ERROR: Address %s:%s is in use. "
                    "Use --listen-address <host:port> to specify port to listen on."
                    % (listen_host, listen_port))
                sys.exit(1)
            raise
        app_.stop(leave_channels=False)
    else:
        # Pass parsed args on to subcommands.
        ctx.obj = kwargs
Example #22
0
    def _run(self):
        self.interrupt.wait()
        print('\n' * 2)
        print("Entering Console" + bc.OKGREEN)
        print("Tip:" + bc.OKBLUE)
        print(
            "\tuse `{}lastlog(n){}` to see n lines of log-output. [default 10] "
            .format(bc.HEADER, bc.OKBLUE))
        print("\tuse `{}lasterr(n){}` to see n lines of stderr.".format(
            bc.HEADER, bc.OKBLUE))
        print("\tuse `{}help(eth){}` for help on accessing the live chain.".
              format(bc.HEADER, bc.OKBLUE))
        print("\n" + bc.ENDC)

        # runmultiple hack in place?
        if hasattr(self.console_locals['eth'].app, 'apps'):
            print('\n' * 2 + bc.OKGREEN)
            print("Hint:" + bc.OKBLUE)
            print((
                '\tOther nodes are accessible from {}`eth.app.apps`{}').format(
                    bc.HEADER, bc.OKBLUE))
            print('\tThey where automatically assigned to:')
            print("\t`{}eth1{}`".format(bc.HEADER, bc.OKBLUE))
            if len(self.console_locals['eth'].app.apps) > 3:
                print("\t {}...{}".format(bc.HEADER, bc.OKBLUE))
            print("\t`{}eth{}{}`".format(
                bc.HEADER,
                len(self.console_locals['eth'].app.apps) - 1, bc.OKBLUE))
            print("\n" + bc.ENDC)

            # automatically assign different nodes to 'eth1.', 'eth2.'' , ....
            Eth = self.console_locals['Eth']
            for x in range(1, len(self.console_locals['eth'].app.apps)):
                self.console_locals['eth' + str(x)] = Eth(
                    self.console_locals['eth'].app.apps[x])

        # Remove handlers that log to stderr
        root = getLogger()
        for handler in root.handlers[:]:
            if isinstance(handler,
                          StreamHandler) and handler.stream == sys.stderr:
                root.removeHandler(handler)

        stream = io.StringIO()
        handler = StreamHandler(stream=stream)
        handler.formatter = Formatter("%(levelname)s:%(name)s %(message)s")
        root.addHandler(handler)

        def lastlog(n=10, prefix=None, level=None):
            """Print the last `n` log lines to stdout.
            Use `prefix='p2p'` to filter for a specific logger.
            Use `level=INFO` to filter for a specific level.

            Level- and prefix-filtering are applied before tailing the log.
            """
            lines = (stream.getvalue().strip().split('\n') or [])
            if prefix:
                lines = [
                    line for line in lines
                    if line.split(':')[1].startswith(prefix)
                ]
            if level:
                lines = [line for line in lines if line.split(':')[0] == level]
            for line in lines[-n:]:
                print(line)

        self.console_locals['lastlog'] = lastlog

        err = io.StringIO()
        sys.stderr = err

        def lasterr(n=1):
            """Print the last `n` entries of stderr to stdout.
            """
            for line in (err.getvalue().strip().split('\n') or [])[-n:]:
                print(line)

        self.console_locals['lasterr'] = lasterr

        IPython.start_ipython(argv=['--gui', 'gevent'],
                              user_ns=self.console_locals)
        self.interrupt.clear()

        sys.exit(0)
Example #23
0
def test_highlight(caplog):
    slogging.configure()
    log = slogging.getLogger()

    log.DEV('testmessage')
    assert "\033[91mtestmessage \033[0m" in caplog.records()[0].msg
Example #24
0
def test_initial_config():
    slogging.getLogger().handlers = []
    slogging.configure()
    assert len(slogging.getLogger().handlers) == 1
    assert isinstance(slogging.getLogger().handlers[0], logging.StreamHandler)
Example #25
0
def run(ctx, dev, nodial, fake, console):
    """Start the client ( --dev to stop on error)"""
    config = ctx.obj['config']
    if nodial:
        # config['deactivated_services'].append(PeerManager.name)
        # config['deactivated_services'].append(NodeDiscovery.name)
        config['discovery']['bootstrap_nodes'] = []
        config['discovery']['listen_port'] = 29873
        config['p2p']['listen_port'] = 29873
        config['p2p']['min_peers'] = 0

    if fake:
        config['eth']['block']['GENESIS_DIFFICULTY'] = 1024
        config['eth']['block']['BLOCK_DIFF_FACTOR'] = 16

    # create app
    app = EthApp(config)

    # development mode
    if dev:
        enable_greenlet_debugger()
        try:
            config['client_version'] += '/' + os.getlogin()
        except:
            log.warn("can't get and add login name to client_version")
            pass

    # dump config
    if log.is_active('debug'):
        dump_config(config)

    # init and unlock accounts first to check coinbase
    if AccountsService in services:
        AccountsService.register_with_app(app)
        unlock_accounts(ctx.obj['unlock'],
                        app.services.accounts,
                        password=ctx.obj['password'])
        try:
            app.services.accounts.coinbase
        except ValueError as e:
            log.fatal('invalid coinbase',
                      coinbase=config.get('pow', {}).get('coinbase_hex'),
                      error=e.message)
            sys.exit()

    app.start_console = console

    # register services
    contrib_services = load_contrib_services(config)

    for service in services + contrib_services:
        assert issubclass(service, BaseService)
        if service.name not in app.config['deactivated_services'] + [
                AccountsService.name
        ]:
            assert service.name not in app.services
            service.register_with_app(app)
            assert hasattr(app.services, service.name)

    # start app
    log.info('starting')
    app.start()

    if ctx.obj['log_file']:
        log.info("Logging to file %s", ctx.obj['log_file'])
        # User requested file logging - remove stderr handler
        root_logger = slogging.getLogger()
        for hndlr in root_logger.handlers:
            if isinstance(hndlr, StreamHandler) and hndlr.stream == sys.stderr:
                root_logger.removeHandler(hndlr)
                break

    if config['post_app_start_callback'] is not None:
        config['post_app_start_callback'](app)

    # wait for interrupt
    evt = Event()
    gevent.signal(signal.SIGQUIT, evt.set)
    gevent.signal(signal.SIGTERM, evt.set)
    evt.wait()

    # finally stop
    app.stop()
Example #26
0
def test_highlight(caplog):
    slogging.configure(log_json=False)
    log = slogging.getLogger()

    log.DEV('testmessage')
    assert "\033[91mtestmessage \033[0m" in caplog.records[0].msg
Example #27
0
def test_logging_reconfigure_levels(config, logger, level):
    slogging.configure(config)
    assert slogging.getLogger(logger).level == getattr(logging, level)
Example #28
0
from ethereum.utils import sha3

from raiden.settings import DEFAULT_SETTLE_TIMEOUT
from raiden.tests.utils.mock_client import (
    BlockChainServiceMock,
    MOCK_REGISTRY_ADDRESS,
)
from raiden.network.transport import UDPTransport
from raiden.tests.utils.network import create_network
from raiden.tests.benchmark.utils import (
    print_serialization,
    print_slow_function,
    print_slow_path,
)

log = slogging.getLogger('test.speed')  # pylint: disable=invalid-name


def setup_apps(amount, tokens, num_transfers, num_nodes, channels_per_node):
    assert len(tokens) <= num_nodes

    deposit = amount * num_transfers

    private_keys = [
        sha3('mediated_transfer:{}'.format(position))
        for position in range(num_nodes)
    ]

    BlockChainServiceMock.reset()
    blockchain_services = list()
    for privkey in private_keys:
Example #29
0
def test_initial_config():
    slogging.getLogger().handlers = []
    slogging.configure()
    assert len(slogging.getLogger().handlers) == 1
    assert isinstance(slogging.getLogger().handlers[0], logging.StreamHandler)
Example #30
0
def smoketest(ctx, debug, **kwargs):
    """ Test, that the raiden installation is sane.
    """
    from raiden.api.python import RaidenAPI

    report_file = tempfile.mktemp(suffix=".log")
    open(report_file, 'w+')

    def append_report(subject, data):
        with open(report_file, 'a') as handler:
            handler.write('{:=^80}'.format(' %s ' % subject.upper()) +
                          os.linesep)
            if data is not None:
                handler.writelines([(data + os.linesep).encode('utf-8')])

    append_report('raiden version', json.dumps(get_system_spec()))
    append_report('raiden log', None)

    print("[1/5] getting smoketest configuration")
    smoketest_config = load_or_create_smoketest_config()

    print("[2/5] starting ethereum")
    ethereum, ethereum_config = start_ethereum(smoketest_config['genesis'])

    print('[3/5] starting raiden')

    # setup logging to log only into our report file
    slogging.configure(':DEBUG', log_file=report_file)
    root = slogging.getLogger()
    for handler in root.handlers:
        if isinstance(handler, slogging.logging.StreamHandler):
            root.handlers.remove(handler)
            break
    # setup cli arguments for starting raiden
    args = dict(
        discovery_contract_address=smoketest_config['contracts']
        ['discovery_address'],
        registry_contract_address=smoketest_config['contracts']
        ['registry_address'],
        eth_rpc_endpoint='http://127.0.0.1:{}'.format(ethereum_config['rpc']),
        keystore_path=ethereum_config['keystore'],
        address=ethereum_config['address'],
    )
    for option in app.params:
        if option.name in args.keys():
            args[option.name] = option.process_value(ctx, args[option.name])
        else:
            args[option.name] = option.default

    password_file = os.path.join(args['keystore_path'], 'password')
    with open(password_file, 'w') as handler:
        handler.write('password')

    args['mapped_socket'] = None
    args['password_file'] = click.File()(password_file)
    args['datadir'] = args['keystore_path']
    args['api_address'] = 'localhost:' + str(
        get_free_port('127.0.0.1', 5001).next())
    args['sync_check'] = False

    # invoke the raiden app
    app_ = ctx.invoke(app, **args)

    raiden_api = RaidenAPI(app_.raiden)
    rest_api = RestAPI(raiden_api)
    api_server = APIServer(rest_api)
    (api_host, api_port) = split_endpoint(args["api_address"])
    api_server.start(api_host, api_port)

    success = False
    try:
        print('[4/5] running smoketests...')
        error = run_smoketests(app_.raiden, smoketest_config, debug=debug)
        if error is not None:
            append_report('smoketest assertion error', error)
        else:
            success = True
    finally:
        app_.stop()
        ethereum.send_signal(2)

        err, out = ethereum.communicate()
        append_report('geth init stdout',
                      ethereum_config['init_log_out'].decode('utf-8'))
        append_report('geth init stderr',
                      ethereum_config['init_log_err'].decode('utf-8'))
        append_report('ethereum stdout', out)
        append_report('ethereum stderr', err)
        append_report('smoketest configuration', json.dumps(smoketest_config))
    if success:
        print('[5/5] smoketest successful, report was written to {}'.format(
            report_file))
    else:
        print('[5/5] smoketest had errors, report was written to {}'.format(
            report_file))
        sys.exit(1)
Example #31
0
def test_set_level():
    slogging.set_level('test', 'CRITICAL')
    assert slogging.getLogger('test').level == logging.CRITICAL
Example #32
0
"""
A pure python implementation of a contract responsable to open a channel.
"""
from ethereum import slogging

from raiden.utils import sha3, pex
from raiden.mtree import check_proof
from raiden.messages import MediatedTransfer, CancelTransfer, DirectTransfer, Lock, LockedTransfer
from raiden.encoding.messages import (
    DIRECTTRANSFER,
    LOCKEDTRANSFER,
    MEDIATEDTRANSFER,
    CANCELTRANSFER,
)

log = slogging.getLogger(__name__)  # pylint: disable=invalid-name

# Blockspam attack mitigation:
#     - Oracles, certifying, that previous blocks were full.
#     - Direct access to gasused of previous blocks.
#     - Heuristic, no settlements in the previous blocks.
# Todos:
#     Compatible Asset/Token/Coin Contract
#     Channel Opening sequence
#     Channel Fees (i.e. Accounts w/ higher reputation could charge a fee/deposit).
#     use channel.opened to collect reputation of an account (long lasting channels == good)


def is_newer_transfer(transfer, sender_state):
    """ Helper to check if `transfer` is from a newer block than
    sender_state's lastest transfer.
Example #33
0
def runner(ctx, **kwargs):
    """ Start a raiden Echo Node that will send received transfers back to the initiator. """
    # This is largely a copy&paste job from `raiden.ui.cli::run`, with the difference that
    # an `EchoNode` is instantiated from the App's `RaidenAPI`.
    print('Welcome to Raiden, version {} [Echo Node]'.format(
        get_system_spec()['raiden']))
    slogging.configure(kwargs['logging'],
                       log_json=kwargs['log_json'],
                       log_file=kwargs['logfile'])
    if kwargs['logfile']:
        # Disable stream logging
        root = slogging.getLogger()
        for handler in root.handlers:
            if isinstance(handler, slogging.logging.StreamHandler):
                root.handlers.remove(handler)
                break

    token_address = kwargs.pop('token_address')

    (listen_host, listen_port) = split_endpoint(kwargs['listen_address'])
    with SocketFactory(listen_host, listen_port,
                       strategy=kwargs['nat']) as mapped_socket:
        kwargs['mapped_socket'] = mapped_socket

        app_ = ctx.invoke(app, **kwargs)

        domain_list = []
        if kwargs['rpccorsdomain']:
            if ',' in kwargs['rpccorsdomain']:
                for domain in kwargs['rpccorsdomain'].split(','):
                    domain_list.append(str(domain))
            else:
                domain_list.append(str(kwargs['rpccorsdomain']))

        raiden_api = RaidenAPI(app_.raiden)
        if ctx.params['rpc']:
            rest_api = RestAPI(raiden_api)
            api_server = APIServer(
                rest_api,
                cors_domain_list=domain_list,
                web_ui=ctx.params['web_ui'],
            )
            (api_host, api_port) = split_endpoint(kwargs['api_address'])
            api_server.start(api_host, api_port)

            print(
                'The Raiden API RPC server is now running at http://{}:{}/.\n\n'
                'See the Raiden documentation for all available endpoints at\n'
                'http://raiden-network.readthedocs.io/en/stable/rest_api.html'.
                format(
                    api_host,
                    api_port,
                ))

        # This will install the EchoNode callback in the alarm task:
        echo = EchoNode(raiden_api, token_address)

        event = gevent.event.Event()
        gevent.signal(signal.SIGQUIT, event.set)
        gevent.signal(signal.SIGTERM, event.set)
        gevent.signal(signal.SIGINT, event.set)
        event.wait()

        # This will remove the EchoNode callback from the alarm task:
        echo.stop()

        try:
            api_server.stop()
        except NameError:
            pass
    app_.stop(leave_channels=False)
import termios
import time
import gevent

from ethereum import slogging
from ethereum.utils import denoms, encode_hex
from requests import ConnectionError

from raiden.utils import (
    address_encoder,
    privatekey_to_address,
)
from raiden.utils.crypto import privtopub
from raiden.tests.utils.genesis import GENESIS_STUB

log = slogging.getLogger(__name__)  # pylint: disable=invalid-name

DEFAULT_BALANCE = denoms.ether * 10000000
DEFAULT_BALANCE_BIN = str(denoms.ether * 10000000)
DEFAULT_PASSPHRASE = 'notsosecret'  # Geth's account passphrase


def wait_until_block(chain, block):
    # we expect `next_block` to block until the next block, but, it could
    # advance miss and advance two or more
    curr_block = chain.block_number()
    while curr_block < block:
        curr_block = chain.next_block()


def clique_extradata(extra_vanity, extra_seal):
def runner(ctx, **kwargs):
    """ Start a raiden Echo Node that will send received transfers back to the initiator. """
    # This is largely a copy&paste job from `raiden.ui.cli::run`, with the difference that
    # an `EchoNode` is instantiated from the App's `RaidenAPI`.
    print('Welcome to Raiden, version {} [Echo Node]'.format(get_system_spec()['raiden']))
    slogging.configure(
        kwargs['logging'],
        log_json=kwargs['log_json'],
        log_file=kwargs['logfile']
    )
    if kwargs['logfile']:
        # Disable stream logging
        root = slogging.getLogger()
        for handler in root.handlers:
            if isinstance(handler, slogging.logging.StreamHandler):
                root.handlers.remove(handler)
                break

    token_address = kwargs.pop('token_address')

    (listen_host, listen_port) = split_endpoint(kwargs['listen_address'])
    with SocketFactory(listen_host, listen_port, strategy=kwargs['nat']) as mapped_socket:
        kwargs['mapped_socket'] = mapped_socket

        app_ = ctx.invoke(app, **kwargs)

        domain_list = []
        if kwargs['rpccorsdomain']:
            if ',' in kwargs['rpccorsdomain']:
                for domain in kwargs['rpccorsdomain'].split(','):
                    domain_list.append(str(domain))
            else:
                domain_list.append(str(kwargs['rpccorsdomain']))

        raiden_api = RaidenAPI(app_.raiden)
        if ctx.params['rpc']:
            rest_api = RestAPI(raiden_api)
            api_server = APIServer(
                rest_api,
                cors_domain_list=domain_list,
                web_ui=ctx.params['web_ui'],
            )
            (api_host, api_port) = split_endpoint(kwargs['api_address'])
            api_server.start(api_host, api_port)

            print(
                'The Raiden API RPC server is now running at http://{}:{}/.\n\n'
                'See the Raiden documentation for all available endpoints at\n'
                'http://raiden-network.readthedocs.io/en/stable/rest_api.html'.format(
                    api_host,
                    api_port,
                )
            )

        # This will install the EchoNode callback in the alarm task:
        echo = EchoNode(raiden_api, token_address)

        event = gevent.event.Event()
        gevent.signal(signal.SIGQUIT, event.set)
        gevent.signal(signal.SIGTERM, event.set)
        gevent.signal(signal.SIGINT, event.set)
        event.wait()

        # This will remove the EchoNode callback from the alarm task:
        echo.stop()

        try:
            api_server.stop()
        except NameError:
            pass
    app_.stop(leave_channels=False)