예제 #1
0
파일: app.py 프로젝트: expsam/hydrachain
def rundummy(ctx, num_validators, node_num, seed):

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

    config = ctx.obj['config']

    # create bootstrap node priv_key and enode
    bootstrap_node_privkey = mk_privkey('%d:udp:%d' % (seed, 0))
    bootstrap_node_pubkey = privtopub_raw(bootstrap_node_privkey)
    assert len(bootstrap_node_pubkey) == 64,  len(bootstrap_node_pubkey)
    base_port = 29870
    host = b'0.0.0.0'

    bootstrap_node = host_port_pubkey_to_uri(host, base_port, bootstrap_node_pubkey)
    config['discovery']['bootstrap_nodes'] = [bootstrap_node]

    # create this node priv_key
    config['node']['privkey_hex'] = mk_privkey('%d:udp:%d' % (seed, node_num)).encode('hex')

    # create validator addresses
    validators = [privtoaddr(mk_privkey('%d:account:%d' % (seed, i)))
                  for i in range(num_validators)]
    config['hdc']['validators'] = validators

    # create this node account
    account = Account.new(password='', key=mk_privkey('%d:account:%d' % (seed, node_num)))

    # set ports based on node
    config['discovery']['listen_port'] = base_port + node_num
    config['p2p']['listen_port'] = base_port + node_num
    config['p2p']['min_peers'] = 2
    config['jsonrpc']['listen_port'] += node_num

    _start_app(account, config)
예제 #2
0
def signed_tx_example():
    from ethereum.transactions import Transaction
    from pyethapp.accounts import mk_privkey, privtoaddr
    secret_seed = 'wow'
    privkey = mk_privkey(secret_seed)
    sender = privtoaddr(privkey)
    # fetch nonce
    nonce = quantity_decoder(JSONRPCClient().call('eth_getTransactionCount',
                                                  address_encoder(sender),
                                                  'pending'))
    # create transaction
    tx = Transaction(nonce,
                     default_gasprice,
                     default_startgas,
                     to=z_address,
                     value=100,
                     data='')
    tx.sign(privkey)
    tx_dict = tx.to_dict()
    tx_dict.pop('hash')
    res = JSONRPCClient().eth_sendTransaction(**tx_dict)
    if len(res) == 20:
        print 'contract created @', res.encode('hex')
    else:
        assert len(res) == 32
        print 'tx hash', res.encode('hex')
예제 #3
0
파일: mk_enode.py 프로젝트: 1600/hydrachain
def mk_enode(seed, node_num, host, port):
    print(
        host_port_pubkey_to_uri(
            host, 
            port, 
            privtopub_raw(
                mk_privkey(
                    '%d:udp:%d' % (seed, node_num)))))
예제 #4
0
파일: app.py 프로젝트: ms83/hydrachain
def _configure_node_network(config, num_validators, node_num, seed):
    assert node_num < num_validators

    # reduce key derivation iterations
    PBKDF2_CONSTANTS["c"] = 100

    # create this node priv_key
    config["node"]["privkey_hex"] = mk_privkey("%d:udp:%d" % (seed, node_num)).encode("hex")

    # create validator addresses
    validators = [privtoaddr(mk_privkey("%d:account:%d" % (seed, i))) for i in range(num_validators)]
    config["hdc"]["validators"] = validators

    # create this node account
    account = Account.new(password="", key=mk_privkey("%d:account:%d" % (seed, node_num)))
    assert account.address in validators
    return config, account
예제 #5
0
def _configure_node_network(config, num_validators, node_num, seed):
    assert node_num < num_validators

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

    # create this node priv_key
    config['node']['privkey_hex'] = mk_privkey('%d:udp:%d' % (seed, node_num)).encode('hex')

    # create validator addresses
    validators = [privtoaddr(mk_privkey('%d:account:%d' % (seed, i)))
                  for i in range(num_validators)]
    config['hdc']['validators'] = validators

    # create this node account
    account = Account.new(password='', key=mk_privkey('%d:account:%d' % (seed, node_num)))
    assert account.address in validators
    return config, account
예제 #6
0
파일: app.py 프로젝트: expsam/hydrachain
def _configure_node_network(config, num_validators, node_num, seed):
    assert node_num < num_validators

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

    # create this node priv_key
    config['node']['privkey_hex'] = mk_privkey('%d:udp:%d' % (seed, node_num)).encode('hex')

    # create validator addresses
    validators = [privtoaddr(mk_privkey('%d:account:%d' % (seed, i)))
                  for i in range(num_validators)]
    config['hdc']['validators'] = validators

    # create this node account
    account = Account.new(password='', key=mk_privkey('%d:account:%d' % (seed, node_num)))
    assert account.address in validators
    return config, account
예제 #7
0
def tx_example():
    """
    unsigned txs is signed on the server which needs to know
    the secret key associated with the sending account
    it can be added in the config
    """
    from pyethapp.accounts import mk_privkey, privtoaddr
    secret_seed = 'wow'
    sender = privtoaddr(mk_privkey(secret_seed))
    res = JSONRPCClient().eth_sendTransaction(sender=sender, to=z_address, value=1000)
    if len(res) == 20:
        print 'contract created @', res.encode('hex')
    else:
        assert len(res) == 32
        print 'tx hash', res.encode('hex')
예제 #8
0
def runlocal(ctx, num_validators, node_num, seed):

    assert node_num < num_validators

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

    config = ctx.obj['config']

    # create this node priv_key
    config['node']['privkey_hex'] = mk_privkey('%d:udp:%d' % (seed, node_num)).encode('hex')

    # create validator addresses
    validators = [privtoaddr(mk_privkey('%d:account:%d' % (seed, i)))
                  for i in range(num_validators)]
    config['hdc']['validators'] = validators

    # create this node account
    account = Account.new(password='', key=mk_privkey('%d:account:%d' % (seed, node_num)))
    assert account.address in validators

    config['p2p']['min_peers'] = 2

    _start_app(account, config, validators)
예제 #9
0
def tx_example():
    """
    unsigned txs is signed on the server which needs to know
    the secret key associated with the sending account
    it can be added in the config
    """
    from pyethapp.accounts import mk_privkey, privtoaddr
    secret_seed = 'wow'
    sender = privtoaddr(mk_privkey(secret_seed))
    res = JSONRPCClient().eth_sendTransaction(sender=sender,
                                              to=z_address,
                                              value=1000)
    if len(res) == 20:
        print 'contract created @', res.encode('hex')
    else:
        assert len(res) == 32
        print 'tx hash', res.encode('hex')
예제 #10
0
def signed_tx_example():
    from ethereum.transactions import Transaction
    from pyethapp.accounts import mk_privkey, privtoaddr

    secret_seed = "wow"
    privkey = mk_privkey(secret_seed)
    sender = privtoaddr(privkey)
    # fetch nonce
    nonce = quantity_decoder(JSONRPCClient().call("eth_getTransactionCount", address_encoder(sender), "pending"))
    # create transaction
    tx = Transaction(nonce, default_gasprice, default_startgas, to=z_address, value=100, data="")
    tx.sign(privkey)
    tx_dict = tx.to_dict()
    tx_dict.pop("hash")
    res = JSONRPCClient().eth_sendTransaction(**tx_dict)
    if len(res) == 20:
        print "contract created @", res.encode("hex")
    else:
        assert len(res) == 32
        print "tx hash", res.encode("hex")
예제 #11
0
def signed_tx_example(to=z_address, value=100):
    from ethereum.transactions import Transaction
    from pyethapp.accounts import mk_privkey, privtoaddr
    secret_seed = 'wow'
    privkey = mk_privkey(secret_seed)
    sender = privtoaddr(privkey)
    # fetch nonce
    nonce = quantity_decoder(
        JSONRPCClient().call('eth_getTransactionCount', address_encoder(sender), 'pending'))
    # create transaction
    tx = Transaction(nonce, default_gasprice, default_startgas, to=z_address, value=value, data='')
    tx.sign(privkey)
    tx_dict = tx.to_dict()
    tx_dict.pop('hash')
    res = JSONRPCClient().eth_sendTransaction(**tx_dict)
    if len(res) == 20:
        print 'contract created @', res.encode('hex')
    else:
        assert len(res) == 32
        print 'tx hash', res.encode('hex')
예제 #12
0
파일: network.py 프로젝트: nikelius/raiden
def hydrachain_network(private_keys, base_port, base_datadir):
    """ Initializes a hydrachain network used for testing. """
    # pylint: disable=too-many-locals
    from hydrachain.app import services, start_app, HPCApp
    import pyethapp.config as konfig

    gevent.get_hub().SYSTEM_ERROR = BaseException
    PBKDF2_CONSTANTS['c'] = 100
    quantity = len(private_keys)

    def privkey_to_uri(private_key):
        host = b'0.0.0.0'
        pubkey = privtopub(private_key)
        return host_port_pubkey_to_uri(host, base_port, pubkey)

    addresses = [
        privtoaddr(priv)
        for priv in private_keys
    ]

    bootstrap_nodes = [
        privkey_to_uri(private_keys[0]),
    ]

    validator_keys = [
        mk_privkey('raidenvalidator:{}'.format(position))
        for position in range(quantity)
    ]

    validator_addresses = [
        privtoaddr(validator_keys[position])
        for position in range(quantity)
    ]

    alloc = {
        addr.encode('hex'): {
            'balance': '1606938044258990275541962092341162602522202993782792835301376',
        }
        for addr in addresses
    }

    genesis = {
        'nonce': '0x00006d6f7264656e',
        'difficulty': '0x20000',
        'mixhash': '0x00000000000000000000000000000000000000647572616c65787365646c6578',
        'coinbase': '0x0000000000000000000000000000000000000000',
        'timestamp': '0x00',
        'parentHash': '0x0000000000000000000000000000000000000000000000000000000000000000',
        'extraData': '0x',
        'gasLimit': '0x5FEFD8',
        'alloc': alloc,
    }

    all_apps = []
    for number in range(quantity):
        port = base_port + number

        config = konfig.get_default_config(services + [HPCApp])

        # del config['eth']['genesis_hash']
        config = update_config_from_genesis_json(config, genesis)

        datadir = os.path.join(base_datadir, str(number))
        konfig.setup_data_dir(datadir)

        account = Account.new(
            password='',
            key=validator_keys[number],
        )

        config['data_dir'] = datadir
        config['node']['privkey_hex'] = private_keys[number].encode('hex')
        config['hdc']['validators'] = validator_addresses
        config['jsonrpc']['listen_port'] += number
        config['client_version_string'] = 'NODE{}'.format(number)

        # setting to 0 so that the CALLCODE opcode works at the start of the
        # network
        config['eth']['block']['HOMESTEAD_FORK_BLKNUM'] = 0

        config['discovery']['bootstrap_nodes'] = bootstrap_nodes
        config['discovery']['listen_port'] = port

        config['p2p']['listen_port'] = port
        config['p2p']['min_peers'] = min(10, quantity - 1)
        config['p2p']['max_peers'] = quantity * 2

        # only one of the nodes should have the Console service running
        if number != 0:
            config['deactivated_services'].append(Console.name)

        hydrachain_app = start_app(config, accounts=[account])
        all_apps.append(hydrachain_app)

    return all_apps
예제 #13
0
파일: app.py 프로젝트: ms83/hydrachain
def get_bootstrap_node(seed, base_port=29870, host=b"0.0.0.0"):
    # create bootstrap node priv_key and enode
    bootstrap_node_privkey = mk_privkey("%d:udp:%d" % (seed, 0))
    bootstrap_node_pubkey = privtopub_raw(bootstrap_node_privkey)
    return host_port_pubkey_to_uri(host, base_port, bootstrap_node_pubkey)
예제 #14
0
def get_bootstrap_node(seed, base_port=29870, host=b'0.0.0.0'):
    # create bootstrap node priv_key and enode
    bootstrap_node_privkey = mk_privkey('%d:udp:%d' % (seed, 0))
    bootstrap_node_pubkey = privtopub_raw(bootstrap_node_privkey)
    return host_port_pubkey_to_uri(host, base_port, bootstrap_node_pubkey)
예제 #15
0
def mk_enode(seed, node_num, host, port):
    print(
        host_port_pubkey_to_uri(
            host, port,
            privtopub_raw(mk_privkey('%d:udp:%d' % (seed, node_num)))))
예제 #16
0
def privkey(seed):
    return mk_privkey('42:account:{seed}'.format(seed=seed))
예제 #17
0
def test_blockchain(request):
    # pylint: disable=too-many-locals
    from hydrachain import app
    app.slogging.configure(':ERROR')

    quantity = 3
    base_port = 29870
    timeout = 3  # seconds
    tmp_datadir = tempfile.mktemp()

    private_keys = [
        mk_privkey('raidentest:{}'.format(position))
        for position in range(quantity)
    ]

    addresses = [privtoaddr(priv) for priv in private_keys]

    hydrachain_apps = hydrachain_network(private_keys, base_port, tmp_datadir)

    privatekey = private_keys[0]
    address = privtoaddr(privatekey)

    jsonrpc_client = JSONRPCClient(privkey=private_keys[0],
                                   print_communication=False)

    humantoken_path = get_contract_path('HumanStandardToken.sol')
    humantoken_contracts = compile_file(humantoken_path, libraries=dict())
    token_abi = jsonrpc_client.deploy_solidity_contract(
        address,
        'HumanStandardToken',
        humantoken_contracts,
        dict(),
        (9999, 'raiden', 2, 'Rd'),
        timeout=timeout,
    )

    registry_path = get_contract_path('Registry.sol')
    registry_contracts = compile_file(registry_path, libraries=dict())
    registry_abi = jsonrpc_client.deploy_solidity_contract(
        address,
        'Registry',
        registry_contracts,
        dict(),
        tuple(),
        timeout=timeout,
    )

    log_list = jsonrpc_client.call(
        'eth_getLogs',
        {
            'fromBlock': '0x0',
            'toBlock': 'latest',
            'topics': [],
        },
    )
    assert len(log_list) == 0

    # pylint: disable=no-member

    assert token_abi.balanceOf(address) == 9999
    transaction_hash = registry_abi.addAsset(token_abi.address)
    jsonrpc_client.poll(transaction_hash.decode('hex'), timeout=timeout)

    log_list = jsonrpc_client.call(
        'eth_getLogs',
        {
            'fromBlock': '0x0',
            'toBlock': 'latest',
            'topics': [],
        },
    )
    assert len(log_list) == 1

    channel_manager_address_encoded = registry_abi.channelManagerByAsset.call(
        token_abi.address)
    channel_manager_address = channel_manager_address_encoded.decode('hex')

    log_channel_manager_address_encoded = log_list[0]['data']
    log_channel_manager_address = log_channel_manager_address_encoded[
        2:].lstrip('0').rjust(40, '0').decode('hex')

    assert channel_manager_address == log_channel_manager_address

    channel_manager_abi = jsonrpc_client.new_contract_proxy(
        registry_contracts['ChannelManagerContract']['abi'],
        channel_manager_address,
    )

    transaction_hash = channel_manager_abi.newChannel(addresses[1], 10)
    jsonrpc_client.poll(transaction_hash.decode('hex'), timeout=timeout)

    log_list = jsonrpc_client.call(
        'eth_getLogs',
        {
            'fromBlock': '0x0',
            'toBlock': 'latest',
            'topics': [],
        },
    )
    assert len(log_list) == 2

    channel_manager_abi.get.call(
        address.encode('hex'),
        addresses[1].encode('hex'),
    )
예제 #18
0
def test_blockchain(request):
    # pylint: disable=too-many-locals
    from hydrachain import app
    app.slogging.configure(':ERROR')

    quantity = 3
    base_port = 29870
    timeout = 3  # seconds
    tmp_datadir = tempfile.mktemp()

    private_keys = [
        mk_privkey('raidentest:{}'.format(position))
        for position in range(quantity)
    ]

    addresses = [
        privtoaddr(priv)
        for priv in private_keys
    ]

    hydrachain_apps = hydrachain_network(private_keys, base_port, tmp_datadir)

    privatekey = private_keys[0]
    address = privtoaddr(privatekey)

    jsonrpc_client = JSONRPCClient(privkey=private_keys[0], print_communication=False)

    humantoken_path = get_contract_path('HumanStandardToken.sol')
    humantoken_contracts = compile_file(humantoken_path, libraries=dict())
    token_abi = jsonrpc_client.deploy_solidity_contract(
        address,
        'HumanStandardToken',
        humantoken_contracts,
        dict(),
        (9999, 'raiden', 2, 'Rd'),
        timeout=timeout,
    )

    registry_path = get_contract_path('Registry.sol')
    registry_contracts = compile_file(registry_path, libraries=dict())
    registry_abi = jsonrpc_client.deploy_solidity_contract(
        address,
        'Registry',
        registry_contracts,
        dict(),
        tuple(),
        timeout=timeout,
    )

    log_list = jsonrpc_client.call(
        'eth_getLogs',
        {
            'fromBlock': '0x0',
            'toBlock': 'latest',
            'topics': [],
        },
    )
    assert len(log_list) == 0

    # pylint: disable=no-member

    assert token_abi.balanceOf(address) == 9999
    transaction_hash = registry_abi.addAsset(token_abi.address)
    jsonrpc_client.poll(transaction_hash.decode('hex'), timeout=timeout)

    log_list = jsonrpc_client.call(
        'eth_getLogs',
        {
            'fromBlock': '0x0',
            'toBlock': 'latest',
            'topics': [],
        },
    )
    assert len(log_list) == 1

    channel_manager_address_encoded = registry_abi.channelManagerByAsset.call(token_abi.address)
    channel_manager_address = channel_manager_address_encoded.decode('hex')

    log_channel_manager_address_encoded = log_list[0]['data']
    log_channel_manager_address = log_channel_manager_address_encoded[2:].lstrip('0').rjust(40, '0').decode('hex')

    assert channel_manager_address == log_channel_manager_address

    channel_manager_abi = jsonrpc_client.new_contract_proxy(
        registry_contracts['ChannelManagerContract']['abi'],
        channel_manager_address,
    )

    transaction_hash = channel_manager_abi.newChannel(addresses[1], 10)
    jsonrpc_client.poll(transaction_hash.decode('hex'), timeout=timeout)

    log_list = jsonrpc_client.call(
        'eth_getLogs',
        {
            'fromBlock': '0x0',
            'toBlock': 'latest',
            'topics': [],
        },
    )
    assert len(log_list) == 2

    channel_manager_abi.get.call(
        address.encode('hex'),
        addresses[1].encode('hex'),
    )
예제 #19
0
def privkey(seed):
    return mk_privkey('42:account:{seed}'.format(seed=seed))