예제 #1
0
def test_configuration():
    config_string = ':inFO,a:trace,a.b:debug'
    slogging.configure(config_string=config_string)
    log = slogging.get_logger()
    log_a = slogging.get_logger('a')
    log_a_b = slogging.get_logger('a.b')
    assert log.is_active('info')
    assert not log.is_active('debug')
    assert log_a.is_active('trace')
    assert log_a_b.is_active('debug')
    assert not log_a_b.is_active('trace')
예제 #2
0
def test_configuration():
    config_string = ":inFO,a:trace,a.b:debug"
    slogging.configure(config_string=config_string)
    log = slogging.get_logger()
    log_a = slogging.get_logger("a")
    log_a_b = slogging.get_logger("a.b")
    assert log.is_active("info")
    assert not log.is_active("debug")
    assert log_a.is_active("trace")
    assert log_a_b.is_active("debug")
    assert not log_a_b.is_active("trace")
예제 #3
0
def test_namespaces():
    config_string = ':inFO,a:trace,a.b:debug'
    th = setup_logging(config_string=config_string)
    # log level info
    log = slogging.get_logger()
    log_a = slogging.get_logger('a')
    log_a_b = slogging.get_logger('a.b')
    assert th.does_log(log.info)
    assert not th.does_log(log.debug)
    assert th.does_log(log_a.trace)
    assert th.does_log(log_a_b.debug)
    assert not th.does_log(log_a_b.trace)
예제 #4
0
def toggle_trace_profiler(raiden):
    try:
        from raiden.utils.profiling.trace import TraceProfiler
    except ImportError:
        slogging.get_logger(__name__).exception('cannot start tracer profiler')
        return

    if hasattr(raiden, 'profiler') and isinstance(raiden.profiler, TraceProfiler):
        raiden.profiler.stop()
        raiden.profiler = None

    elif not hasattr(raiden, 'profiler') and raiden.config['database_path'] != ':memory:':
        raiden.profiler = TraceProfiler(raiden.config['database_path'])
        raiden.profiler.start()
예제 #5
0
def test_listeners():
    th = setup_logging()
    log = slogging.get_logger()

    called = []

    def log_cb(event_dict):
        called.append(event_dict)

    # activate listener
    slogging.log_listeners.listeners.append(log_cb)
    log.error('test listener', abc='thislistener')
    assert 'thislistener' in th.logged
    r = called.pop()
    assert r == dict(event='test listener', abc='thislistener')

    log.trace('trace is usually filtered', abc='thislistener')
    assert th.logged is None
    assert 'abc' in called.pop()

    # deactivate listener
    slogging.log_listeners.listeners.remove(log_cb)
    log.error('test listener', abc='nolistener')
    assert 'nolistener' in th.logged
    assert not called
예제 #6
0
def test_howto_use_in_tests():
    # select what you want to see.
    # e.g. TRACE from vm except for pre_state :DEBUG otherwise
    config_string = ':DEBUG,eth.vm:TRACE,vm.pre_state:INFO'
    slogging.configure(config_string=config_string)
    log = slogging.get_logger('tests.logging')
    log.info('test starts')
예제 #7
0
def test_kvprinter():
    # we can not test formatting
    config_string = ':inFO,a:trace,a.b:debug'
    th = setup_logging(config_string=config_string)
    # log level info
    log = slogging.get_logger('foo')
    log.info('baz', arg=2)
    l = th.logged
    assert 'baz' in l
예제 #8
0
def test_basic(caplog, level_name):
    slogging.configure(":trace")
    log = slogging.get_logger()
    with caplog.at_level("TRACE"):
        getattr(log, level_name)(level_name)

    assert len(caplog.records) == 1
    assert caplog.records[0].levelname == level_name.upper()
    assert level_name in caplog.records[0].msg
예제 #9
0
def test_logger_filter(caplog, logger_name, filter, should_log):
    slogging.configure()
    log = slogging.get_logger(logger_name)
    if filter:
        log.addFilter(logging.Filter(filter))
    log.info("testlogmessage", v=1)
    if should_log:
        assert "testlogmessage" in caplog.text
    else:
        assert "testlogmessage" not in caplog.text
예제 #10
0
def test_how_to_use_as_vm_logger():
    """
    don't log until there was an error
    """
    slogging.configure(":DEBUG,eth.vm:INFO")
    log = slogging.get_logger("eth.vm")

    # record all logs
    def run_vm(raise_error=False):
        log.trace("op", pc=1)
        log.trace("op", pc=2)
        if raise_error:
            raise Exception

    recorder = slogging.LogRecorder()
    try:
        run_vm(raise_error=True)
    except:
        log = slogging.get_logger("eth.vm")
        for x in recorder.pop_records():
            log.info(x.pop("event"), **x)
예제 #11
0
def test_testhandler():
    th = get_test_handler()
    assert th.logged == None
    th = setup_logging()
    assert th.logged is None
    log = slogging.get_logger('a')
    log.warn('abc')
    assert 'abc' in th.logged
    assert th.logged is None
    # same with does_log
    assert th.does_log(log.warn)
    assert not th.does_log(log.debug)
예제 #12
0
def test_incremental():
    config_string = ':trace'
    th = setup_logging(config_string=config_string)
    log = slogging.get_logger()
    # incremental context
    log = log.bind(first='one')
    log.error('nice', a=1, b=2)
    assert 'first' in th.logged
    log = log.bind(second='two')
    log.error('nice', a=1, b=2)
    l = th.logged
    assert 'first' in l and 'two' in l
예제 #13
0
def test_baseconfig():
    # test default loglevel INFO
    th = setup_logging()
    log = slogging.get_logger()
    assert th.does_log(log.error)
    assert th.does_log(log.critical)
    assert th.does_log(log.warn)
    assert th.does_log(log.warn)
    assert th.does_log(log.info)
    assert not th.does_log(log.debug)
    assert not th.does_log(log.trace)
    config_string = ':inFO,a:trace,a.b:debug'
    th = setup_logging(config_string=config_string)
예제 #14
0
def test_is_active():
    th = setup_logging()
    log = slogging.get_logger()
    assert not log.is_active('trace')
    assert not log.is_active('debug')
    assert log.is_active('info')
    assert log.is_active('warn')

    # activate w/ listner
    slogging.log_listeners.listeners.append(lambda x: x)
    assert log.is_active('trace')
    slogging.log_listeners.listeners.pop()
    assert not log.is_active('trace')
예제 #15
0
def test_lazy_log():
    """
    test lacy evaluation of json log data
    e.g.
    class LogState
    class LogMemory
    """

    called_json = []
    called_print = []

    class Expensive(object):

        def __structlog__(self):
            called_json.append(1)
            return 'expensive data preparation'

        def __repr__(self):
            called_print.append(1)
            return 'expensive data preparation'

    th = setup_logging(log_json=True)
    log = slogging.get_logger()
    log.trace('no', data=Expensive())
    assert not called_print
    assert not called_json
    log.info('yes', data=Expensive())
    assert called_json.pop()
    assert not called_print

    th = setup_logging()
    log = slogging.get_logger()
    log.trace('no', data=Expensive())
    assert not called_print
    assert not called_json
    log.info('yes', data=Expensive())
    assert not called_json
    assert called_print.pop()
예제 #16
0
def test_tracebacks():
    th = setup_logging()
    log = slogging.get_logger()

    def div(a, b):
        try:
            r = a / b
            log.error('heres the stack', stack_info=True)
        except Exception as e:
            log.error('an Exception trace should preceed this msg', exc_info=True)
    div(1, 0)
    assert 'an Exception' in th.logged
    div(1, 1)
    assert 'the stack' in th.logged
예제 #17
0
def test_how_to_use_as_vm_logger():
    """
    don't log until there was an error
    """

    config_string = ':DEBUG,eth.vm:INFO'
    slogging.configure(config_string=config_string)
    log = slogging.get_logger('eth.vm')

    # record all logs
    def run_vm(raise_error=False):
        log.trace('op', pc=1)
        log.trace('op', pc=2)
        if raise_error:
            raise Exception

    recorder = slogging.LogRecorder()
    try:
        run_vm(raise_error=True)
    except:
        log = slogging.get_logger('eth.vm')
        for x in recorder.pop_records():
            log.info(x.pop('event'), **x)
예제 #18
0
def test_tracebacks(caplog):
    slogging.configure()
    log = slogging.get_logger()

    def div(a, b):
        try:
            _ = a / b
            log.error('heres the stack', stack_info=True)
        except Exception as e:
            log.error('an Exception trace should preceed this msg', exc_info=True)
    div(1, 0)
    assert 'an Exception trace' in caplog.text()
    assert 'Traceback' in caplog.text()
    div(1, 1)
    assert 'the stack' in caplog.text()
예제 #19
0
def test_tracebacks(caplog):
    slogging.configure()
    log = slogging.get_logger()

    def div(a, b):
        try:
            _ = a // b
            log.error("heres the stack", stack_info=True)
        except Exception as e:
            log.error("an Exception trace should preceed this msg", exc_info=True)

    div(1, 0)
    assert "an Exception trace" in caplog.text
    assert "Traceback" in caplog.text
    div(1, 1)
    assert "the stack" in caplog.text
예제 #20
0
def test_recorder():
    th = setup_logging()
    log = slogging.get_logger()

    # test info
    recorder = slogging.LogRecorder()
    assert len(slogging.log_listeners.listeners) == 1
    log.info('a', v=1)
    assert th.logged
    r = recorder.pop_records()
    assert r[0] == dict(event='a', v=1)
    assert len(slogging.log_listeners.listeners) == 0

    # test trace
    recorder = slogging.LogRecorder()
    assert len(slogging.log_listeners.listeners) == 1
    log.trace('a', v=1)
    assert not th.logged
    r = recorder.pop_records()
    assert r[0] == dict(event='a', v=1)
    assert len(slogging.log_listeners.listeners) == 0
예제 #21
0
def test_lazy_log():
    """
    test lacy evaluation of json log data
    e.g.
    class LogState
    class LogMemory
    """

    called_print = []

    class Expensive(object):
        def __repr__(self):
            called_print.append(1)
            return "expensive data preparation"

    slogging.configure(log_json=True)
    log = slogging.get_logger()
    log.trace("no", data=Expensive())
    assert not called_print
    log.info("yes", data=Expensive())  # !!!!!!!!!!!!!
    assert called_print.pop()
예제 #22
0
def test_recorder(caplog):
    slogging.configure(log_json=True)
    log = slogging.get_logger()

    # test info
    recorder = slogging.LogRecorder()
    assert len(slogging.log_listeners) == 1
    log.info("a", v=1)
    assert "a" in caplog.text
    r = recorder.pop_records()
    assert r[0] == dict(event="a", v=1)
    assert len(slogging.log_listeners) == 0

    # test trace
    log.setLevel(logging.TRACE)
    recorder = slogging.LogRecorder()
    assert len(slogging.log_listeners) == 1
    log.trace("a", v=2)
    assert '"v": 2' in caplog.text
    r = recorder.pop_records()
    assert r[0] == dict(event="a", v=2)
    assert len(slogging.log_listeners) == 0
예제 #23
0
def test_listeners(caplog):
    slogging.configure()
    log = slogging.get_logger()

    called = []

    def log_cb(event_dict):
        called.append(event_dict)

    # activate listener
    slogging.log_listeners.append(log_cb)  # Add handlers
    log.error("test listener", abc="thislistener")
    assert "thislistener" in caplog.text
    r = called.pop()
    assert r == dict(event="test listener", abc="thislistener")

    log.trace("trace is usually filtered", abc="thislistener")  # this handler for function log_cb does not work
    assert "trace is usually filtered" not in caplog.text

    # deactivate listener
    slogging.log_listeners.remove(log_cb)
    log.error("test listener", abc="nolistener")
    assert "nolistener" in caplog.text
    assert not called
예제 #24
0
import os
import ethereum.testutils as testutils
from ethereum.slogging import get_logger
import ethereum.abi as abi
logger = get_logger()

def test_abi_encode_var_sized_array():
    abi.encode_abi(['address[]'], [['\x00' * 20] * 3])

def test_abi_encode_fixed_size_array():
    abi.encode_abi(['uint16[2]'], [[5, 6]])


# SETUP TESTS IN GLOBAL NAME SPACE
def gen_func(filename, testname, testdata):
    return lambda: do_test_state(filename, testname, testdata)

def do_test_state(filename, testname=None, testdata=None, limit=99999999):
    logger.debug('running test:%r in %r' % (testname, filename))
    testutils.check_abi_test(testutils.fixture_to_bytes(testdata))

fixtures = testutils.get_tests_from_file_or_dir(
    os.path.join(testutils.fixture_path, 'ABITests'))

filenames = sorted(list(fixtures.keys()))
for filename in filenames:
    tests = fixtures[filename]
    for testname, testdata in list(tests.items()):
        func_name = 'test_%s_%s' % (filename, testname)
        globals()[func_name] = gen_func(filename, testname, testdata)
예제 #25
0
    TransferToTargetResource,
    ConnectionsResource,
    ConnectionManagersResource,
)
from raiden.transfer.state import (
    CHANNEL_STATE_OPENED,
    CHANNEL_STATE_CLOSED,
    CHANNEL_STATE_SETTLED,
)
from raiden.raiden_service import (
    create_default_identifier,
)
from raiden.api.objects import ChannelList, PartnersPerTokenList, AddressList
from raiden.utils import address_encoder, channel_to_api_dict, split_endpoint

log = slogging.get_logger(__name__)


def normalize_events_list(old_list):
    """Internally the `event_type` key is prefixed with underscore but the API
    returns an object without that prefix"""
    new_list = []
    for _event in old_list:
        new_event = dict(_event)
        new_event['event_type'] = new_event.pop('_event_type')
        # Some of the raiden events contain accounts and as such need to
        # be exported in hex to the outside world
        if new_event['event_type'] == 'EventTransferReceivedSuccess':
            new_event['initiator'] = address_encoder(new_event['initiator'])[2:]
        if new_event['event_type'] == 'EventTransferSentSuccess':
            new_event['target'] = address_encoder(new_event['target'])[2:]
예제 #26
0
# -*- coding: utf-8 -*-
"""
This module contains the classes responsible to implement the network
communication.
"""
import time

import gevent
from gevent.server import DatagramServer
from ethereum import slogging

from raiden.network.protocol import RaidenProtocol
from raiden.utils import pex, sha3

log = slogging.get_logger('raiden.network.transport')  # pylint: disable=invalid-name


class DummyPolicy(object):
    """Dummy implementation for the throttling policy that always
    returns a wait_time of 0.
    """
    def __init__(self):
        pass

    def consume(self, tokens):
        return 0.


class TokenBucket(object):
    """Implementation of the token bucket throttling algorithm.
    """
import ethereum.keys
import ethereum.config
from ethereum.slogging import get_logger
from pyethapp.accounts import Account, AccountsService, mk_random_privkey
from pyethapp.app import EthApp
from pyethapp.config import update_config_with_defaults, get_default_config
from pyethapp.db_service import DBService
from pyethapp.eth_service import ChainService
from pyethapp.jsonrpc import JSONRPCServer, quantity_encoder, address_encoder, data_decoder,   \
    data_encoder
from pyethapp.pow_service import PoWService

# reduce key derivation iterations
ethereum.keys.PBKDF2_CONSTANTS['c'] = 100

log = get_logger('test.jsonrpc')

# EVM code corresponding to the following solidity code:
#
#     contract LogTest {
#         event Log();
#
#         function () {
#             Log();
#         }
#     }
#
# (compiled with online Solidity compiler at https://chriseth.github.io/browser-solidity/ version
# 0.1.1-34172c3b/RelWithDebInfo-Emscripten/clang/int)
LOG_EVM = (
    '606060405260448060116000396000f30060606040523615600d57600d565b60425b7f5e7df75d54'
예제 #28
0
import os
from devp2p.service import BaseService
from gevent.event import Event
import leveldb
from ethereum import slogging
import time

slogging.set_level('db', 'debug')
log = slogging.get_logger('db')

compress = decompress = lambda x: x


"""
memleak in py-leveldb


25140 ralf      20   0 3360m 1.3g  53m S    3  4.2   4:12.71 pyethapp
26167 ralf      20   0 2943m 1.0g  44m S    1  3.3   3:19.51 pyethapp


25140 ralf      20   0 3531m 1.5g  61m S    1  4.7   5:07.49 pyethapp
26167 ralf      20   0 3115m 1.1g  47m S    1  3.6   4:03.54 pyethapp


mit reload_db()
 4096 ralf      20   0 1048m 362m  14m S    2  1.1   1:21.97 pyethapp
 4109 ralf      20   0  975m 307m  14m S    2  1.0   1:16.03 pyethapp

 4096 ralf      20   0  903m 431m 9484 S    2  1.3   1:54.29 pyethapp
 4109 ralf      20   0  807m 367m 8852 S    1  1.1   1:47.01 pyethapp
예제 #29
0
verify_stack_after_op = False

#  ######################################
import sys
sys.setrecursionlimit(10000)

from ethereum import utils
from ethereum.abi import is_numeric
import copy
from ethereum import opcodes
import time
from ethereum.slogging import get_logger
from rlp.utils import encode_hex, ascii_chr
from ethereum.utils import to_string, encode_int, zpad, bytearray_to_bytestr

log_log = get_logger('eth.vm.log')
log_msg = get_logger('eth.pb.msg')
log_vm_exit = get_logger('eth.vm.exit')
log_vm_op = get_logger('eth.vm.op')
log_vm_op_stack = get_logger('eth.vm.op.stack')
log_vm_op_memory = get_logger('eth.vm.op.memory')
log_vm_op_storage = get_logger('eth.vm.op.storage')

TT256 = 2**256
TT256M1 = 2**256 - 1
TT255 = 2**255

MAX_DEPTH = 1024

JUMPDEST = 0x5b  # Hardcoded, change if needed
예제 #30
0
from hashlib import md5
import os
from CodernityDB.database import Database, DatabasePathException,  \
    RecordNotFound
from CodernityDB.hash_index import HashIndex
from devp2p.service import BaseService
from ethereum.db import BaseDB
from gevent.event import Event
from ethereum import compress
from ethereum.slogging import get_logger


log = get_logger('db')


class MD5Index(HashIndex):

    def __init__(self, *args, **kwargs):
        kwargs['key_format'] = '16s'
        super(MD5Index, self).__init__(*args, **kwargs)

    def make_key_value(self, data):
        return md5(data['key']).digest(), None

    def make_key(self, key):
        return md5(key).digest()


class CodernityDB(BaseDB, BaseService):

    """A service providing a codernity db interface."""
예제 #31
0
파일: messages.py 프로젝트: maciekf/raiden
CONFIRMTRANSFER_CMDID = 10

ACK = to_bigendian(ACK_CMDID)
PING = to_bigendian(PING_CMDID)
# REJECTED = to_bigendian(REJECTED_CMDID)
SECRETREQUEST = to_bigendian(SECRETREQUEST_CMDID)
SECRET = to_bigendian(SECRET_CMDID)
DIRECTTRANSFER = to_bigendian(DIRECTTRANSFER_CMDID)
LOCKEDTRANSFER = to_bigendian(LOCKEDTRANSFER_CMDID)
MEDIATEDTRANSFER = to_bigendian(MEDIATEDTRANSFER_CMDID)
CANCELTRANSFER = to_bigendian(CANCELTRANSFER_CMDID)
TRANSFERTIMEOUT = to_bigendian(TRANSFERTIMEOUT_CMDID)
CONFIRMTRANSFER = to_bigendian(CONFIRMTRANSFER_CMDID)

# pylint: disable=invalid-name
log = slogging.get_logger('messages')

nonce = make_field('nonce', 8, '8s', integer(0, BYTE**8))
expiration = make_field('expiration', 8, '8s', integer(0, BYTE**8))

asset = make_field('asset', 20, '20s')
recipient = make_field('recipient', 20, '20s')
target = make_field('target', 20, '20s')
initiator = make_field('initiator', 20, '20s')
sender = make_field('sender', 20, '20s')

locksroot = make_field('locksroot', 32, '32s')
hashlock = make_field('hashlock', 32, '32s')
secret = make_field('secret', 32, '32s')
echo = make_field('echo', 32, '32s')
balance = make_field('balance', 32, '32s', integer(0, BYTE**32))
예제 #32
0
from gevent.event import AsyncResult
import gevent
import time
from eth_protocol import TransientBlock
from ethereum.slogging import get_logger
import traceback

log = get_logger('eth.sync')
log_st = get_logger('eth.sync.task')


class SyncTask(object):

    """
    synchronizes a the chain starting from a given blockhash
    blockchain hash is fetched from a single peer (which led to the unknown blockhash)
    blocks are fetched from the best peers

    with missing block:
        fetch hashes
            until known block
    for hashes
        fetch blocks
            for each block
                chainservice.add_blocks() # blocks if queue is full
    """
    max_blocks_per_request = 256
    initial_blockhashes_per_request = 16
    max_blockhashes_per_request = 2048
    blocks_request_timeout = 8.  # 256 * ~2KB = 512KB
    blockhashes_request_timeout = 8.  # 32 * 2048 = 65KB
예제 #33
0
# https://github.com/ethereum/go-ethereum/wiki/Blockpool
import time
from ethereum.utils import privtoaddr, sha3
import rlp
from rlp.utils import encode_hex
from ethereum import processblock
from synchronizer import Synchronizer
from ethereum.slogging import get_logger
from ethereum.chain import Chain
from devp2p.service import WiredService
import eth_protocol
import gevent
from gevent.queue import Queue
log = get_logger('eth.chainservice')


# patch to get context switches between tx replay
processblock_apply_transaction = processblock.apply_transaction


def apply_transaction(block, tx):
    log.debug('apply_transaction ctx switch', at=time.time())
    gevent.sleep(0.001)
    return processblock_apply_transaction(block, tx)
processblock.apply_transaction = apply_transaction


rlp_hash_hex = lambda data: encode_hex(sha3(rlp.encode(data)))


class DuplicatesFilter(object):
예제 #34
0
파일: app.py 프로젝트: expsam/hydrachain
from pyethapp.profiles import PROFILES
from pyethapp.jsonrpc import JSONRPCServer
from pyethapp.accounts import AccountsService, Account
import ethereum.slogging as slogging
import pyethapp.app as pyethapp_app
from pyethapp.accounts import mk_privkey, privtopub
from devp2p.crypto import privtopub as privtopub_raw
from devp2p.utils import host_port_pubkey_to_uri
from ethereum.keys import privtoaddr, PBKDF2_CONSTANTS

# local
from hydrachain.hdc_service import ChainService
from hydrachain import __version__

slogging.configure(config_string=':debug')
log = slogging.get_logger('app')


services = [DBService,
            AccountsService,
            NodeDiscovery,
            PeerManager,
            ChainService,
            JSONRPCServer,
            Console]

pyethapp_app.services = services


class HPCApp(pyethapp_app.EthApp):
    client_name = 'HydraChain'
예제 #35
0
"""
import random
import os
import sys
import click
from devp2p.utils import update_config_with_defaults
import yaml
import ethereum.slogging as slogging
from importlib import import_module
import inspect
from devp2p.service import BaseService
from devp2p.app import BaseApp

slogging.configure(config_string=':debug')
log = slogging.get_logger('config')

default_data_dir = click.get_app_dir('pyethapp')


def get_config_path(data_dir=default_data_dir):
    return os.path.join(data_dir, 'config.yaml')


default_config_path = get_config_path(default_data_dir)


def setup_data_dir(data_dir=None):
    if data_dir and not os.path.exists(data_dir):
        os.makedirs(data_dir)
        setup_required_config(data_dir)
예제 #36
0
파일: chain.py 프로젝트: vaizguy/pyethereum
import os
import time
from ethereum import utils
from ethereum.utils import to_string, is_string
import rlp
from rlp.utils import encode_hex
from ethereum import blocks
from ethereum import processblock
from ethereum.slogging import get_logger
log = get_logger('eth.chain')


class Index(object):

    """"
    Collection of indexes

    children:
        - needed to get the uncles of a block
    blocknumbers:
        - needed to mark the longest chain (path to top)
    transactions:
        - optional to resolve txhash to block:tx

    """

    def __init__(self, db, index_transactions=True):
        self.db = db
        self._index_transactions = index_transactions

    def add_block(self, blk):
예제 #37
0
# -*- coding: utf8 -*-
import rlp
from pybitcoin import encode_pubkey, N, encode_privkey
from rlp.sedes import big_endian_int, binary
from rlp.utils import encode_hex, str_to_bytes, ascii_chr
from secp256k1 import PublicKey, ALL_FLAGS, PrivateKey

from ethereum.exceptions import InvalidTransaction
from ethereum import bloom
from ethereum import opcodes
from ethereum import utils
from ethereum.slogging import get_logger
from ethereum.utils import TT256, mk_contract_address, zpad, int_to_32bytearray, big_endian_to_int


log = get_logger('eth.chain.tx')

# in the yellow paper it is specified that s should be smaller than secpk1n (eq.205)
secpk1n = 115792089237316195423570985008687907852837564279074904382605163141518161494337


class Transaction(rlp.Serializable):

    """
    A transaction is stored as:
    [nonce, gasprice, startgas, to, value, data, v, r, s]

    nonce is the number of transactions already sent by that account, encoded
    in binary form (eg.  0 -> '', 7 -> '\x07', 1000 -> '\x03\xd8').

    (v,r,s) is the raw Electrum-style signature of the transaction without the
예제 #38
0
from ethereum.utils import zpad
import gevent.event
import ethereum.slogging as slogging
from ethereum import transactions
from pyethapp.rpc_client import ABIContract
from hydrachain import native_contracts as nc
log = slogging.get_logger('nc.utils')

STATUS = 'uint16'
FORBIDDEN = 403
NOTFOUND = 404
OK = 200
ERROR = 500
BADREQEST = 400
INSUFFICIENTFUNDS = PAYMENTREQUIRED = 402


def isaddress(x):
    return len(x) == 20 and x != '\0' * 20


def lhexenc(lst):
    return [l.encode('hex') for l in lst]


def transact(app, sender, to_, value=0, data=''):
    head_candidate = app.services.chain.chain.head_candidate
    default_gasprice = 1
    default_startgas = head_candidate.gas_limit - head_candidate.gas_used
    nonce = head_candidate.get_nonce(sender)
    tx = transactions.Transaction(nonce=nonce,
예제 #39
0
                                   NETTING_CHANNEL_ABI)
from raiden.network.channelgraph import ChannelGraph
from raiden.tasks import AlarmTask, StartExchangeTask, HealthcheckTask
from raiden.encoding import messages
from raiden.messages import SignedMessage
from raiden.network.protocol import RaidenProtocol
from raiden.utils import (
    isaddress,
    pex,
    privatekey_to_address,
    safe_address_decode,
    GLOBAL_CTX,
    sha3,
)

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

EventListener = namedtuple('EventListener',
                           ('event_name', 'filter_', 'translator'))


class RaidenError(Exception):
    pass


class NoPathError(RaidenError):
    pass


class InvalidAddress(RaidenError):
    pass
예제 #40
0
import logging
import warnings

try:
    from ethereum.slogging import get_logger, configure, configure_logging, getLogger
except ImportError:
    warnings.warn('Ethereum not available, could not import slogging',
                  ImportWarning)
    # patch logging to support kargs
    _log_orig = logging.Logger._log

    def _kargs_log(self, level, msg, args, exc_info=None, extra=None, **kargs):
        kwmsg = ''.join(' %s=%s' % (k, str(v)) for k, v in kargs.items())
        _log_orig(self, level, str(msg) + kwmsg, args, exc_info, extra)

    logging.Logger._log = _kargs_log
    get_logger = logging.getLogger

if __name__ == '__main__':
    logging.basicConfig()
    log = get_logger('test')
    log.warn('miner.new_block', block_hash='abcdef123', nonce=2234231)
예제 #41
0
from ethereum.config import default_config
from ethereum.block import Block, BlockHeader
from ethereum import trie
from ethereum.db import EphemDB
from ethereum.utils import sha3, encode_hex
import rlp
from ethereum.slogging import get_logger
from ethereum.exceptions import InsufficientBalance, BlockGasLimitReached, \
    InsufficientStartGas, InvalidNonce, UnsignedTransaction
from ethereum.messages import apply_transaction
log = get_logger('eth.block')


# Gas limit adjustment algo
def calc_gaslimit(parent, config=default_config):
    decay = parent.gas_limit // config['GASLIMIT_EMA_FACTOR']
    new_contribution = ((parent.gas_used * config['BLKLIM_FACTOR_NOM']) //
                        config['BLKLIM_FACTOR_DEN'] //
                        config['GASLIMIT_EMA_FACTOR'])
    gl = max(parent.gas_limit - decay + new_contribution,
             config['MIN_GAS_LIMIT'])
    if gl < config['GENESIS_GAS_LIMIT']:
        gl2 = parent.gas_limit + decay
        gl = min(config['GENESIS_GAS_LIMIT'], gl2)
    assert check_gaslimit(parent, gl, config=config)
    return gl


def check_gaslimit(parent, gas_limit, config=default_config):
    #  block.gasLimit - parent.gasLimit <= parent.gasLimit // GasLimitBoundDivisor
    gl = parent.gas_limit // config['GASLIMIT_ADJMAX_FACTOR']
예제 #42
0
from ethereum.slogging import get_logger
log = get_logger('eth.block_creation')
from ethereum.block import Block, BlockHeader
from ethereum.common import mk_block_from_prevstate, validate_header, \
    verify_execution_results, validate_transaction_tree, \
    set_execution_results, add_transactions, post_finalize
from ethereum.consensus_strategy import get_consensus_strategy
from ethereum.messages import apply_transaction
from ethereum.state import State
from ethereum.utils import sha3, encode_hex
import rlp


# Applies the block-level state transition function
def apply_block(state, block):
    # Pre-processing and verification
    snapshot = state.snapshot()
    cs = get_consensus_strategy(state.config)
    try:
        # Start a new block context
        cs.initialize(state, block)
        # Basic validation
        assert validate_header(state, block.header)
        assert cs.check_seal(state, block.header)
        assert cs.validate_uncles(state, block)
        assert validate_transaction_tree(state, block)
        # Process transactions
        for tx in block.transactions:
            apply_transaction(state, tx)
        # Finalize (incl paying block rewards)
        cs.finalize(state, block)
예제 #43
0
from gevent.event import Event
from web3 import Web3, KeepAliveRPCProvider
from ethereum import slogging

from devp2p.service import BaseService

log = slogging.get_logger('chain')


class ChainService(BaseService):

    name = 'chain'
    default_config = dict(chain=dict(
        provider='jsonrpc', jsonrpc=dict(host='127.0.0.1', port=8545)))

    def __init__(self, app):
        log.info("Chain service init")
        super(ChainService, self).__init__(app)

        if self.app.config['chain']['provider'] == 'jsonrpc':
            self.web3 = Web3(
                KeepAliveRPCProvider(
                    host=self.app.config['chain']['jsonrpc']['host'],
                    port=self.app.config['chain']['jsonrpc']['port']))
        else:
            raise ValueError("unsupported chain provider %s" %
                             self.app.config['chain']['provider'])

    def _run(self):
        self.start_filters()
예제 #44
0
import time
from ethereum.utils import sha3
import rlp
from rlp.utils import encode_hex
from ethereum import processblock
from synchronizer import Synchronizer
from ethereum.slogging import get_logger
from ethereum.chain import Chain
from ethereum.blocks import Block, VerificationFailed
from ethereum.transactions import Transaction
from devp2p.service import WiredService
import eth_protocol
import gevent
import gevent.lock
from gevent.queue import Queue
log = get_logger('eth.chainservice')

# patch to get context switches between tx replay
processblock_apply_transaction = processblock.apply_transaction


def apply_transaction(block, tx):
    # import traceback
    # print traceback.print_stack()
    log.debug('apply_transaction ctx switch', at=time.time())
    gevent.sleep(0.001)
    return processblock_apply_transaction(block, tx)


processblock.apply_transaction = apply_transaction
예제 #45
0
import json
import os
import random
import shutil
from uuid import UUID
from devp2p.service import BaseService
from ethereum import keys
from ethereum.slogging import get_logger
from ethereum.utils import privtopub  # this is different  than the one used in devp2p.crypto
from ethereum.utils import sha3

log = get_logger('accounts')

DEFAULT_COINBASE = 'de0b295669a9fd93d5f28d9ec85e40f4cb697bae'.decode('hex')


def mk_privkey(seed):
    return sha3(seed)


def mk_random_privkey():
    k = hex(random.getrandbits(256))[2:-1].zfill(64)
    assert len(k) == 64
    return k.decode('hex')


class Account(object):
    """Represents an account.

    :ivar keystore: the key store as a dictionary (as decoded from json)
    :ivar locked: `True` if the account is locked and neither private nor public keys can be
예제 #46
0
# -*- coding: utf8 -*-
import random

from messages import Transfer, MediatedTransfer, LockedTransfer, SecretRequest
from tasks import Task, TransferTask, ForwardSecretTask
from utils import sha3
from ethereum import slogging

log = slogging.get_logger('transfermanager')


class TransferManager(object):
    """
    mediates transfers, for a fee
    """
    def __init__(self, assetmanager):
        import assetmanager as assetmanagermodul
        assert isinstance(assetmanager, assetmanagermodul.AssetManager)
        self.raiden = assetmanager.raiden
        self.assetmanager = assetmanager
        self.transfertasks = dict()  # hashlock > TransferTask
        self.on_task_completed_callbacks = []

    def on_task_started(self, task):
        assert isinstance(task, Task)
        self.transfertasks[task.hashlock] = task

    def on_task_completed(self, task, success):
        assert isinstance(task, Task)
        del self.transfertasks[task.hashlock]
        for cb in self.on_task_completed_callbacks:
예제 #47
0
from ethereum.pow import ethash
from ethereum import utils
import time
import sys
import warnings
from collections import OrderedDict
from ethereum import utils
from ethereum.slogging import get_logger
import rlp

log = get_logger('eth.pow')

if sys.version_info.major == 2:
    from repoze.lru import lru_cache
else:
    from functools import lru_cache

try:
    import pyethash
    ETHASH_LIB = 'pyethash'  # the C++ based implementation
except ImportError:
    ETHASH_LIB = 'ethash'
    warnings.warn('using pure python implementation', ImportWarning)

if ETHASH_LIB == 'ethash':
    mkcache = ethash.mkcache
    EPOCH_LENGTH = 30000
    hashimoto_light = ethash.hashimoto_light
elif ETHASH_LIB == 'pyethash':
    mkcache = pyethash.mkcache_bytes
    EPOCH_LENGTH = 30000
import pytest
import logging

from ethereum.state import State
from ethereum.transaction_queue import TransactionQueue
from ethereum import utils
from ethereum.slogging import get_logger
from ethereum.common import mk_transaction_sha, mk_receipt_sha
from ethereum import trie

from sharding.collation import Collation, CollationHeader
from sharding import state_transition
from sharding.tools import tester

log = get_logger('test.shard_chain')
log.setLevel(logging.DEBUG)

shard_id = 1


@pytest.fixture(scope='function')
def chain(shard_id):
    t = tester.Chain(env='sharding')
    t.mine(5)
    t.add_test_shard(shard_id)
    return t


def test_mk_collation_from_prevstate():
    """Test mk_collation_from_prevstate(shard_chain, state, coinbase)
    """
예제 #49
0
import messages
from messages import Ack, Secret, BaseError
from utils import isaddress, sha3, pex
from ethereum import slogging
import gevent
log = slogging.get_logger('protocol')


class RaidenProtocol(object):
    """
    each message sent or received is stored by hash
    if message is received twice, resent previous answer
    if there is no response to a message, message gets repeated max N times
    """

    try_interval = 1.
    max_tries = 5
    max_message_size = 1200
    repeat_messages = False  # default for testing, w/o packet loss

    def __init__(self, transport, discovery, raiden):
        self.transport = transport
        self.discovery = discovery
        self.raiden = raiden

        self.tries = dict()  # msg hash: count_tries
        self.sent_acks = dict()  # msghash: Ack

    def send(self, receiver_address, msg):
        assert isaddress(receiver_address)
        assert not isinstance(msg, (Ack, BaseError)), msg
예제 #50
0
    RevealSecret,
    SignedMessage,
)
from raiden.network.protocol import (
    RaidenProtocol,
)
from raiden.constants import ROPSTEN_REGISTRY_ADDRESS
from raiden.connection_manager import ConnectionManager
from raiden.utils import (
    isaddress,
    pex,
    privatekey_to_address,
    sha3,
)

log = slogging.get_logger(__name__)  # pylint: disable=invalid-name
# register filelock logger
filelock.logger = slogging.get_logger('filelock')


def create_default_identifier():
    """ Generates a random identifier. """
    return random.randint(0, UINT64_MAX)


def load_snapshot(serialization_file):
    if os.path.exists(serialization_file):
        with open(serialization_file, 'rb') as handler:
            return pickle.load(handler)

예제 #51
0
import gevent
import simpy
from ethereum import slogging
from ethereum.db import EphemDB
from ethereum.utils import big_endian_to_int, sha3, privtoaddr
from pyethapp.accounts import Account, AccountsService

from hydrachain import hdc_service
from hydrachain.consensus import protocol as hdc_protocol
from hydrachain.consensus.base import Block
from hydrachain.consensus.manager import ConsensusManager
from hydrachain.consensus.utils import num_colors, phx
from hydrachain.hdc_service import ChainService


log = slogging.get_logger('hdc.sim')

# stop on exception
gevent.get_hub().SYSTEM_ERROR = BaseException

# reduce key derivation iterations
ethereum.keys.PBKDF2_CONSTANTS['c'] = 100

privkeys = [chr(i) * 32 for i in range(1, 11)]
validators = privkeys[:]


empty = object()

random.seed(42)
예제 #52
0
import random
from devp2p.app import BaseApp
from devp2p.protocol import BaseProtocol
from devp2p.service import WiredService
from devp2p.crypto import privtopub as privtopub_raw, sha3
from devp2p.utils import colors, COLOR_END
from devp2p import app_helper
import rlp
from quarkchain.rlp.utils import encode_hex, decode_hex, is_integer
import gevent
try:
    import ethereum.slogging as slogging
    slogging.configure(config_string=':info,p2p.protocol:info,p2p.peer:info,p2p.full_app:info')
except:
    import devp2p.slogging as slogging
log = slogging.get_logger('full_app')


class Token(rlp.Serializable):

    "Object with the information to update a decentralized counter"
    fields = [
        ('counter', rlp.sedes.big_endian_int),
        ('sender', rlp.sedes.binary)
    ]

    def __init__(self, counter=0, sender=''):
        assert is_integer(counter)
        assert isinstance(sender, bytes)
        super(Token, self).__init__(counter, sender)
예제 #53
0
        returns 0

    EXTCODECOPY on an address with a NativeContract
        returns ''
"""
import inspect
import traceback
import ethereum.specials as specials
import ethereum.utils as utils
import ethereum.processblock as processblock
import ethereum.vm as vm
import ethereum.abi as abi
from ethereum.utils import encode_int, zpad, big_endian_to_int, int_to_big_endian
from ethereum import slogging
slogging.configure(config_string=':debug')
log = slogging.get_logger('nc')


class Registry(object):

    """
    NativeContracts:
    0000|000000000000|0123

    NativeContract Instances:
    0000|0123456789ab|0123
    """

    native_contract_address_prefix = '\0' * 16
    native_contract_instance_address_prefix = '\0' * 4
예제 #54
0
from pyethapp.app import EthApp
from pyethapp.accounts import Account

from ethereum.slogging import get_logger, configure_logging
from ethereum.pow.ethpow import mine
from ethereum.tools import tester

from itertools import count

configure_logging(':trace')
log = get_logger('test.console_service')


class EdgeChainApp(EthApp):
    def start(self):
        super(EdgeChainApp, self).start()
        log.debug('adding test accounts')
        self.services.accounts.add_account(Account.new('ADMIN',
                                                       tester.keys[0]),
                                           store=False)
        self.services.accounts.add_account(Account.new('MES', tester.keys[1]),
                                           store=False)
        self.services.accounts.add_account(Account.new('REQUESTER1',
                                                       tester.keys[2]),
                                           store=False)
        self.services.accounts.add_account(Account.new('REQUESTER2',
                                                       tester.keys[3]),
                                           store=False)

    def add_accounts(self, user_id, locked=False):
        account = Account.new(user_id)
예제 #55
0
import os
import pytest
import json
import ethereum.blocks as blocks
import ethereum.testutils as testutils
from ethereum.testutils import fixture_to_bytes
import ethereum.utils as utils
from rlp.utils import encode_hex
from ethereum.tests.utils import new_env
from ethereum.slogging import get_logger
logger = get_logger()


@pytest.fixture(scope="module")
def genesis_fixture():
    """
    Read genesis block from fixtures.
    """
    genesis_fixture = None
    fn = os.path.join(testutils.fixture_path, 'BasicTests', 'genesishashestest.json')
    with open(fn, 'r') as f:
        genesis_fixture = json.load(f)
    assert genesis_fixture is not None, "Could not read genesishashtest.json from fixtures. Make sure you did 'git submodule init'!"
    # FIXME: assert that link is uptodate
    for k in ('genesis_rlp_hex', 'genesis_state_root', 'genesis_hash'):
        assert k in genesis_fixture
    return fixture_to_bytes(genesis_fixture)


@pytest.mark.xfail  # code not in sync with genesis fixtures
def test_genesis_state_root(genesis_fixture):
예제 #56
0
from devp2p.app import BaseApp
from devp2p.protocol import BaseProtocol
from devp2p.service import WiredService
from devp2p.crypto import privtopub as privtopub_raw, sha3
from devp2p.utils import colors, COLOR_END
from devp2p import app_helper
import rlp
from rlp.utils import encode_hex, decode_hex, is_integer
import gevent
try:
    import ethereum.slogging as slogging
    slogging.configure(config_string=':debug,p2p.discovery:info')
except:
    import devp2p.slogging as slogging
    print 'into exception..'
log = slogging.get_logger('shanxuantest')
print 'logger : '
print log


class Token(rlp.Serializable):
    "Object with the information to update a decentralized counter"
    fields = [('counter', rlp.sedes.big_endian_int),
              ('sender', rlp.sedes.binary)]

    def __init__(self, counter=0, sender=''):
        assert is_integer(counter)
        assert isinstance(sender, bytes)
        super(Token, self).__init__(counter, sender)

    @property
예제 #57
0
import os
from devp2p.service import BaseService
from ethereum.db import BaseDB
from gevent.event import Event
import leveldb
from ethereum import slogging

slogging.set_level('db', 'debug')
log = slogging.get_logger('db')

compress = decompress = lambda x: x


"""
memleak in py-leveldb


25140 ralf      20   0 3360m 1.3g  53m S    3  4.2   4:12.71 pyethapp
26167 ralf      20   0 2943m 1.0g  44m S    1  3.3   3:19.51 pyethapp


25140 ralf      20   0 3531m 1.5g  61m S    1  4.7   5:07.49 pyethapp
26167 ralf      20   0 3115m 1.1g  47m S    1  3.6   4:03.54 pyethapp


mit reload_db()
 4096 ralf      20   0 1048m 362m  14m S    2  1.1   1:21.97 pyethapp
 4109 ralf      20   0  975m 307m  14m S    2  1.0   1:16.03 pyethapp

 4096 ralf      20   0  903m 431m 9484 S    2  1.3   1:54.29 pyethapp
 4109 ralf      20   0  807m 367m 8852 S    1  1.1   1:47.01 pyethapp
예제 #58
0
# -*- coding: utf-8 -*-
import getpass
import json
import os
import sys
from binascii import hexlify, unhexlify

from bitcoin import privtopub
from ethereum import keys
from ethereum.slogging import get_logger

log = get_logger(__name__)


def find_datadir():
    home = os.path.expanduser('~')
    if home == '~':  # Could not expand user path
        return None
    datadir = None

    if sys.platform == 'darwin':
        datadir = os.path.join(home, 'Library', 'Ethereum')
    # NOTE: Not really sure about cygwin here
    elif sys.platform == 'win32' or sys.platform == 'cygwin':
        datadir = os.path.join(home, 'AppData', 'Roaming', 'Ethereum')
    elif os.name == 'posix':
        datadir = os.path.join(home, '.ethereum')
    else:
        raise RuntimeError('Unsupported Operating System')

    if not os.path.isdir(datadir):
예제 #59
0
# -*- coding: utf8 -*-
from ethereum import slogging
from ethereum.abi import ContractTranslator
from ethereum.utils import encode_hex
from pyethapp.jsonrpc import address_decoder

from raiden.assetmanager import AssetManager
from raiden.blockchain.abi import CHANNEL_MANAGER_ABI, REGISTRY_ABI
from raiden.channelgraph import ChannelGraph
from raiden.tasks import LogListenerTask
from raiden.encoding import messages
from raiden.messages import Ack, SignedMessage
from raiden.raiden_protocol import RaidenProtocol
from raiden.utils import privtoaddr, isaddress, pex

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


def safe_address_decode(address):
    try:
        address = address.decode('hex')
    except TypeError:
        pass

    return address


class RaidenError(Exception):
    pass

예제 #60
0
from devp2p.discovery import NodeDiscovery
from devp2p.app import BaseApp
from eth_service import ChainService
from console_service import Console
from ethereum.blocks import Block
import ethereum.slogging as slogging
import config as konfig
from db_service import DBService
from jsonrpc import JSONRPCServer
from pyethapp import __version__
import utils

from rno_service import RNOService

slogging.configure(config_string=':debug')
log = slogging.get_logger('app')

services = [
    DBService, NodeDiscovery, PeerManager, ChainService, JSONRPCServer,
    Console, RNOService
]
services += utils.load_contrib_services()


class EthApp(BaseApp):
    client_version = 'pyethapp/v%s/%s/%s' % (
        __version__, sys.platform, 'py%d.%d.%d' % sys.version_info[:3])
    default_config = dict(BaseApp.default_config)
    default_config['client_version'] = client_version