def test_update_config(): conf = config.get_default_config([BaseApp] + base_services) conf_copy = copy.deepcopy(conf) conf2 = config.update_config_with_defaults(conf, dict(p2p=dict(new_key=1))) assert conf is conf2 assert conf == conf2 assert conf_copy != conf
def test_set_config_param(): conf = config.get_default_config([BaseApp] + base_services) assert conf['p2p']['min_peers'] conf = config.set_config_param(conf, 'p2p.min_peers=3') # strict assert conf['p2p']['min_peers'] == 3 with pytest.raises(KeyError): conf = config.set_config_param(conf, 'non.existent=1') conf = config.set_config_param(conf, 'non.existent=1', strict=False) assert conf['non']['existent'] == 1
def test_set_config_param(): conf = config.get_default_config([BaseApp] + base_services) assert conf['p2p']['min_peers'] conf = config.set_config_param(conf, 'p2p.min_peers=3') # strict assert conf['p2p']['min_peers'] == 3 try: conf = config.set_config_param(conf, 'non.existent=1') except KeyError: errored = True assert errored conf = config.set_config_param(conf, 'non.existent=1', strict=False) assert conf['non']['existent'] == 1
def test_dump_config_does_not_leak_privkey(capsys): conf = config.get_default_config([BaseApp] + base_services) SECRET = "thisshouldnotbedumpedincleartext" conf['accounts'] = {} conf['accounts']['privkeys_hex'] = [SECRET] conf['node'] = {} conf['node']['privkey_hex'] = SECRET config.dump_config(conf) output = capsys.readouterr()[0] assert 'node' in output # sanity check assert SECRET not in output
def test_save_load_config(): conf = config.get_default_config([BaseApp] + base_services) assert conf # from fn or base path config.write_config(conf) conf2 = config.load_config() print 'b', conf print 'a', conf2 assert conf == conf2 path = tempfile.mktemp() assert path config.write_config(conf, path=path) conf3 = config.load_config(path=path) assert conf == conf3
def test_save_load_config(): conf = config.get_default_config([BaseApp] + base_services) assert conf garbage_conf = tempfile.mktemp() # do not use default paths in tests! # from fn or base path config.write_config(conf, path=garbage_conf) conf2 = config.load_config(path=garbage_conf) print('b', conf) print('a', conf2) assert conf == conf2 path = tempfile.mktemp() assert path config.write_config(conf, path=path) conf3 = config.load_config(path=path) assert conf == conf3
def setting_config(app, tmpdir, bootstrap_nodes=None): # bootstrap_nodes example: ['enode://288b97262895b1c7ec61cf314c2e2004407d0a5dc77566877aad # 1f2a36659c8b698f4b56fd06c4a0c0bf007b4cfb3e7122d907da3b005fa # 90e724441902eb19e # @192.168.43.149:30303'] config['data_dir'] = str(tmpdir) if bootstrap_nodes is not None: config['p2p']['bootstrap_nodes'] = bootstrap_nodes config['discovery']['bootstrap_nodes'] = bootstrap_nodes config['node'] = {'privkey_hex': encode_hex(mk_random_privkey())} config['pow'] = {'activated': True} config['eth']['network_id'] = 1337 config['eth']['block']['GENESIS_INITIAL_ALLOC'] = { encode_hex(tester.accounts[0]): { 'balance': 10**24 }, encode_hex(tester.accounts[1]): { 'balance': 10**24 }, encode_hex(tester.accounts[2]): { 'balance': 10**24 }, encode_hex(tester.accounts[3]): { 'balance': 10**24 }, } config['p2p']['listen_host'] = '127.0.0.1' services = [ DBService, AccountsService, PeerManager, ChainService, PoWService, Console ] update_config_with_defaults(config, get_default_config([app] + services)) update_config_with_defaults(config, {'eth': {'block': default_config}}) return config, services
def test_app(request, tmpdir): class TestApp(EthApp): def start(self): super(TestApp, self).start() log.debug('adding test accounts') # high balance account self.services.accounts.add_account(Account.new('', tester.keys[0]), store=False) # low balance account self.services.accounts.add_account(Account.new('', tester.keys[1]), store=False) # locked account locked_account = Account.new('', tester.keys[2]) locked_account.lock() self.services.accounts.add_account(locked_account, store=False) assert set(acct.address for acct in self.services.accounts) == set( tester.accounts[:3]) def mine_next_block(self): """Mine until a valid nonce is found. :returns: the new head """ log.debug('mining next block') block = self.services.chain.chain.head_candidate delta_nonce = 10**6 for start_nonce in count(0, delta_nonce): bin_nonce, mixhash = mine(block.number, block.difficulty, block.mining_hash, start_nonce=start_nonce, rounds=delta_nonce) if bin_nonce: break self.services.pow.recv_found_nonce(bin_nonce, mixhash, block.mining_hash) log.debug('block mined') return self.services.chain.chain.head def rpc_request(self, method, *args): """Simulate an incoming JSON RPC request and return the result. Example:: >>> assert test_app.rpc_request('eth_getBalance', '0x' + 'ff' * 20) == '0x0' """ log.debug('simulating rpc request', method=method) method = self.services.jsonrpc.dispatcher.get_method(method) res = method(*args) log.debug('got response', response=res) return res # genesis block with reduced difficulty, increased gas limit, and allocations to test accounts genesis_block = { "nonce": "0x0000000000000042", "difficulty": "0x1", "alloc": { tester.accounts[0].encode('hex'): { 'balance': 10**24 }, tester.accounts[1].encode('hex'): { 'balance': 1 }, tester.accounts[2].encode('hex'): { 'balance': 10**24 }, }, "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000", "coinbase": "0x0000000000000000000000000000000000000000", "timestamp": "0x00", "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", "extraData": "0x", "gasLimit": "0x2fefd8" } genesis_block_file = tmpdir.join('test_genesis_block.json') genesis_block_file.write(json.dumps(genesis_block)) config = { 'data_dir': str(tmpdir), 'db': { 'implementation': 'EphemDB' }, 'pow': { 'activated': False }, 'p2p': { 'min_peers': 0, 'max_peers': 0, 'listen_port': 29873 }, 'node': { 'privkey_hex': mk_random_privkey().encode('hex') }, 'discovery': { 'boostrap_nodes': [], 'listen_port': 29873 }, 'eth': { 'genesis': str(genesis_block_file), 'block': ethereum.config.default_config }, 'jsonrpc': { 'listen_port': 29873 } } services = [ DBService, AccountsService, PeerManager, ChainService, PoWService, JSONRPCServer ] update_config_with_defaults(config, get_default_config([TestApp] + services)) app = TestApp(config) for service in services: service.register_with_app(app) def fin(): log.debug('stopping test app') app.stop() request.addfinalizer(fin) log.debug('starting test app') app.start() return app
def default_config(): config = konfig.get_default_config(services + [hydrachain.app.HPCApp]) return config
def test_default_config(): conf = config.get_default_config([BaseApp] + base_services) assert 'p2p' in conf assert 'app' not in conf
def create_hydrachain_cluster(private_keys, hydrachain_private_keys, p2p_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 def privkey_to_uri(private_key): host = b'0.0.0.0' pubkey = privtopub(private_key) return host_port_pubkey_to_uri(host, p2p_base_port, pubkey) account_addresses = [ privtoaddr(priv) for priv in private_keys ] alloc = { encode_hex(address): { 'balance': DEFAULT_BALANCE, } for address in account_addresses } genesis = { 'nonce': '0x00006d6f7264656e', 'difficulty': '0x20000', 'mixhash': '0x00000000000000000000000000000000000000647572616c65787365646c6578', 'coinbase': '0x0000000000000000000000000000000000000000', 'timestamp': '0x00', 'parentHash': '0x0000000000000000000000000000000000000000000000000000000000000000', 'extraData': '0x', 'gasLimit': GAS_LIMIT_HEX, 'alloc': alloc, } bootstrap_nodes = [ privkey_to_uri(hydrachain_private_keys[0]), ] validators_addresses = [ privtoaddr(private_key) for private_key in hydrachain_private_keys ] all_apps = [] for number, private_key in enumerate(hydrachain_private_keys): config = konfig.get_default_config(services + [HPCApp]) 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=private_key, ) config['data_dir'] = datadir config['hdc']['validators'] = validators_addresses config['node']['privkey_hex'] = encode_hex(private_key) 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'] = p2p_base_port + number config['p2p']['listen_port'] = p2p_base_port + number config['p2p']['min_peers'] = min(10, len(hydrachain_private_keys) - 1) config['p2p']['max_peers'] = len(hydrachain_private_keys) * 2 # only one of the nodes should have the Console service running if number != 0 and Console.name not in config['deactivated_services']: config['deactivated_services'].append(Console.name) hydrachain_app = start_app(config, accounts=[account]) all_apps.append(hydrachain_app) hydrachain_wait(private_keys, len(hydrachain_private_keys) - 1) return all_apps
def test_app(request, tmpdir): class TestApp(EthApp): def start(self): super(TestApp, self).start() log.debug("adding test accounts") # high balance account self.services.accounts.add_account(Account.new("", tester.keys[0]), store=False) # low balance account self.services.accounts.add_account(Account.new("", tester.keys[1]), store=False) # locked account locked_account = Account.new("", tester.keys[2]) locked_account.lock() self.services.accounts.add_account(locked_account, store=False) assert set(acct.address for acct in self.services.accounts) == set(tester.accounts[:3]) def mine_next_block(self): """Mine until a valid nonce is found. :returns: the new head """ log.debug("mining next block") block = self.services.chain.chain.head_candidate delta_nonce = 10 ** 6 for start_nonce in count(0, delta_nonce): bin_nonce, mixhash = mine( block.number, block.difficulty, block.mining_hash, start_nonce=start_nonce, rounds=delta_nonce ) if bin_nonce: break self.services.pow.recv_found_nonce(bin_nonce, mixhash, block.mining_hash) log.debug("block mined") return self.services.chain.chain.head def rpc_request(self, method, *args): """Simulate an incoming JSON RPC request and return the result. Example:: >>> assert test_app.rpc_request('eth_getBalance', '0x' + 'ff' * 20) == '0x0' """ log.debug("simulating rpc request", method=method) method = self.services.jsonrpc.dispatcher.get_method(method) res = method(*args) log.debug("got response", response=res) return res # genesis block with reduced difficulty, increased gas limit, and allocations to test accounts genesis_block = { "nonce": "0x0000000000000042", "difficulty": "0x1", "alloc": { tester.accounts[0].encode("hex"): {"balance": 10 ** 24}, tester.accounts[1].encode("hex"): {"balance": 1}, tester.accounts[2].encode("hex"): {"balance": 10 ** 24}, }, "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000", "coinbase": "0x0000000000000000000000000000000000000000", "timestamp": "0x00", "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", "extraData": "0x", "gasLimit": "0x2fefd8", } genesis_block_file = tmpdir.join("test_genesis_block.json") genesis_block_file.write(json.dumps(genesis_block)) config = { "data_dir": str(tmpdir), "db": {"implementation": "EphemDB"}, "pow": {"activated": False}, "p2p": {"min_peers": 0, "max_peers": 0, "listen_port": 29873}, "node": {"privkey_hex": mk_random_privkey().encode("hex")}, "discovery": {"boostrap_nodes": [], "listen_port": 29873}, "eth": {"genesis": str(genesis_block_file), "block": ethereum.config.default_config}, "jsonrpc": {"listen_port": 29873}, } services = [DBService, AccountsService, PeerManager, ChainService, PoWService, JSONRPCServer] update_config_with_defaults(config, get_default_config([TestApp] + services)) app = TestApp(config) for service in services: service.register_with_app(app) def fin(): log.debug("stopping test app") app.stop() request.addfinalizer(fin) log.debug("starting test app") app.start() return app
def test_app(request, tmpdir): class TestApp(EthApp): def start(self): super(TestApp, self).start() log.debug('adding test accounts') # high balance account self.services.accounts.add_account(Account.new('', tester.keys[0]), store=False) # low balance account self.services.accounts.add_account(Account.new('', tester.keys[1]), store=False) # locked account locked_account = Account.new('', tester.keys[2]) locked_account.lock() self.services.accounts.add_account(locked_account, store=False) assert set(acct.address for acct in self.services.accounts) == set( tester.accounts[:3]) def mine_next_block(self): """Mine until a valid nonce is found. :returns: the new head """ log.debug('mining next block') block = self.services.chain.chain.head_candidate delta_nonce = 10**6 for start_nonce in count(0, delta_nonce): bin_nonce, mixhash = mine(block.number, block.difficulty, block.mining_hash, start_nonce=start_nonce, rounds=delta_nonce) if bin_nonce: break self.services.pow.recv_found_nonce(bin_nonce, mixhash, block.mining_hash) log.debug('block mined') assert self.services.chain.chain.head.difficulty == 1 return self.services.chain.chain.head config = { 'data_dir': str(tmpdir), 'db': { 'implementation': 'EphemDB' }, 'pow': { 'activated': False }, 'p2p': { 'min_peers': 0, 'max_peers': 0, 'listen_port': 29873 }, 'node': { 'privkey_hex': mk_random_privkey().encode('hex') }, 'discovery': { 'boostrap_nodes': [], 'listen_port': 29873 }, 'eth': { 'block': { # reduced difficulty, increased gas limit, allocations to test accounts 'GENESIS_DIFFICULTY': 1, 'BLOCK_DIFF_FACTOR': 2, # greater than difficulty, thus difficulty is constant 'GENESIS_GAS_LIMIT': 3141592, 'GENESIS_INITIAL_ALLOC': { tester.accounts[0].encode('hex'): { 'balance': 10**24 }, tester.accounts[1].encode('hex'): { 'balance': 1 }, tester.accounts[2].encode('hex'): { 'balance': 10**24 }, } } }, 'jsonrpc': { 'listen_port': 29873 } } services = [ DBService, AccountsService, PeerManager, ChainService, PoWService, Console ] update_config_with_defaults(config, get_default_config([TestApp] + services)) update_config_with_defaults( config, {'eth': { 'block': ethereum.config.default_config }}) app = TestApp(config) for service in services: service.register_with_app(app) def fin(): log.debug('stopping test app') app.stop() request.addfinalizer(fin) log.debug('starting test app') app.start() return app
def test_app(request, tmpdir): class TestApp(EthApp): def start(self): super(TestApp, self).start() log.debug('adding test accounts') # high balance account self.services.accounts.add_account(Account.new('', tester.keys[0]), store=False) # low balance account self.services.accounts.add_account(Account.new('', tester.keys[1]), store=False) # locked account locked_account = Account.new('', tester.keys[2]) locked_account.lock() self.services.accounts.add_account(locked_account, store=False) assert set(acct.address for acct in self.services.accounts) == set( tester.accounts[:3]) def mine_next_block(self): """Mine until a valid nonce is found. :returns: the new head """ log.debug('mining next block') block = self.services.chain.head_candidate chain = self.services.chain.chain head_number = chain.head.number delta_nonce = 10**6 for start_nonce in count(0, delta_nonce): bin_nonce, mixhash = mine(block.number, block.difficulty, block.mining_hash, start_nonce=start_nonce, rounds=delta_nonce) if bin_nonce: break self.services.pow.recv_found_nonce(bin_nonce, mixhash, block.mining_hash) if len(chain.time_queue) > 0: # If we mine two blocks within one second, pyethereum will # force the new block's timestamp to be in the future (see # ethereum1_setup_block()), and when we try to add that block # to the chain (via Chain.add_block()), it will be put in a # queue for later processing. Since we need to ensure the # block has been added before we continue the test, we # have to manually process the time queue. log.debug('block mined too fast, processing time queue') chain.process_time_queue(new_time=block.timestamp) log.debug('block mined') assert chain.head.difficulty == 1 assert chain.head.number == head_number + 1 return chain.head config = { 'data_dir': str(tmpdir), 'db': { 'implementation': 'EphemDB' }, 'pow': { 'activated': False }, 'p2p': { 'min_peers': 0, 'max_peers': 0, 'listen_port': 29873 }, 'node': { 'privkey_hex': encode_hex(mk_random_privkey()) }, 'discovery': { 'boostrap_nodes': [], 'listen_port': 29873 }, 'eth': { 'block': { # reduced difficulty, increased gas limit, allocations to test accounts 'GENESIS_DIFFICULTY': 1, 'BLOCK_DIFF_FACTOR': 2, # greater than difficulty, thus difficulty is constant 'GENESIS_GAS_LIMIT': 3141592, 'GENESIS_INITIAL_ALLOC': { encode_hex(tester.accounts[0]): { 'balance': 10**24 }, encode_hex(tester.accounts[1]): { 'balance': 1 }, encode_hex(tester.accounts[2]): { 'balance': 10**24 }, } } }, 'jsonrpc': { 'listen_port': 29873 } } services = [ DBService, AccountsService, PeerManager, ChainService, PoWService, Console ] update_config_with_defaults(config, get_default_config([TestApp] + services)) update_config_with_defaults( config, {'eth': { 'block': ethereum.config.default_config }}) app = TestApp(config) for service in services: service.register_with_app(app) def fin(): log.debug('stopping test app') app.stop() request.addfinalizer(fin) log.debug('starting test app') app.start() return app
def test_app(tmpdir): class TestApp(EthApp): def start(self): super(TestApp, self).start() log.debug('adding test accounts') # high balance account self.services.accounts.add_account(Account.new( 'Andy', tester.keys[0]), store=False) # low balance account self.services.accounts.add_account(Account.new( 'Choi', tester.keys[1]), store=False) # locked account locked_account = Account.new('Joseph', tester.keys[2]) locked_account.lock() self.services.accounts.add_account(locked_account, store=False) assert set(acct.address for acct in self.services.accounts) == set( tester.accounts[:3]) def mine_next_block(self): """Mine until a valid nonce is found. :returns: the new head """ log.debug('mining next block') block = self.services.chain.head_candidate chain = self.services.chain.chain head_number = chain.head.number delta_nonce = 10**6 for start_nonce in count(0, delta_nonce): bin_nonce, mixhash = mine(block.number, block.difficulty, block.mining_hash, start_nonce=start_nonce, rounds=delta_nonce) if bin_nonce: break self.services.pow.recv_found_nonce(bin_nonce, mixhash, block.mining_hash) if len(chain.time_queue) > 0: # If we mine two blocks within one second, pyethereum will # force the new block's timestamp to be in the future (see # ethereum1_setup_block()), and when we try to add that block # to the chain (via Chain.add_block()), it will be put in a # queue for later processing. Since we need to ensure the # block has been added before we continue the test, we # have to manually process the time queue. log.debug('block mined too fast, processing time queue') chain.process_time_queue(new_time=block.timestamp) log.debug('block mined') # assert chain.head.difficulty == 400 assert chain.head.number == head_number + 1 return chain.head config = { 'data_dir': str(tmpdir), 'db': { 'implementation': 'EphemDB' }, 'pow': { 'activated': False }, 'p2p': { 'min_peers': 1, 'max_peers': 3, 'listen_port': 30303, 'bootstrap_nodes': [ 'enode://94dd98401cf6ca0418580bb77ad0606a35ae8f442b22ab0f81011d8d2c78e70ff779c1d53d015640d79a13f0be99161ae3ff3adb60633ca03a7b542cbe116882@192.168.219.105:30303' ] }, 'node': { 'privkey_hex': '091bd6067cb4612df85d9c1ff85cc47f259ced4d4cd99816b14f35650f59c322' }, #encode_hex(mk_random_privkey())}, 'discovery': { 'bootstrap_nodes': [ 'enode://94dd98401cf6ca0418580bb77ad0606a35ae8f442b22ab0f81011d8d2c78e70ff779c1d53d015640d79a13f0be99161ae3ff3adb60633ca03a7b542cbe116882@192.168.219.105:30303' ], 'listen_port': 30303 }, 'eth': { 'block': { # reduced difficulty, increased gas limit, allocations to test accounts 'GENESIS_DIFFICULTY': 400, 'BLOCK_DIFF_FACTOR': 500, # greater than difficulty, thus difficulty is constant 'GENESIS_GAS_LIMIT': 3141592, 'GENESIS_INITIAL_ALLOC': { encode_hex(tester.accounts[0]): { 'balance': 10**24 }, encode_hex(tester.accounts[1]): { 'balance': 1 }, encode_hex(tester.accounts[2]): { 'balance': 10**24 }, } } }, 'jsonrpc': { 'listen_port': 30303 } } services = [ DBService, AccountsService, PeerManager, ChainService, PoWService, Console ] update_config_with_defaults(config, get_default_config([TestApp] + services)) update_config_with_defaults( config, {'eth': { 'block': ethereum.config.default_config }}) config['eth']['network_id'] = 1337 config['p2p']['listen_host'] = '192.168.0.3' config['discovery']['listen_host'] = '192.168.0.3' app = TestApp(config) for service in services: service.register_with_app(app) def fin(): log.debug('stopping test app') app.stop() log.debug('starting test app') app.start() return app
def test_app(request, tmpdir): class TestTransport(object): def __init__(self, call_func): self.call_func = call_func def send_message(self, request): request = json.loads(request) method = request.get('method') args = request.get('params') if not args: return self.call_func(method) else: return self.call_func(method, *args) class TestApp(EthApp): def start(self): super(TestApp, self).start() log.debug('adding test accounts') # high balance account self.services.accounts.add_account(Account.new('', tester.keys[0]), store=False) log.debug('added unlocked account %s' % tester.keys[0]) # low balance account self.services.accounts.add_account(Account.new('', tester.keys[1]), store=False) log.debug('added unlocked account %s' % tester.keys[1]) # locked account locked_account = Account.new('', tester.keys[2]) locked_account.lock() log.debug('added locked account %s' % tester.keys[1]) self.services.accounts.add_account(locked_account, store=False) self.privkey = None assert set(acct.address for acct in self.services.accounts) == set( tester.accounts[:3]) test_transport = TestTransport(call_func=self.rpc_request) self.client = JSONRPCClient(transport=test_transport) def mine_next_block(self): """Mine until a valid nonce is found. :returns: the new head """ log.debug('mining next block') chain = self.services.chain.chain head_number = chain.head.number block = self.services.chain.head_candidate delta_nonce = 10**6 for start_nonce in count(0, delta_nonce): bin_nonce, mixhash = mine(block.number, block.difficulty, block.mining_hash, start_nonce=start_nonce, rounds=delta_nonce) if bin_nonce: break self.services.pow.recv_found_nonce(bin_nonce, mixhash, block.mining_hash) if len(chain.time_queue) > 0: # If we mine two blocks within one second, pyethereum will # force the new block's timestamp to be in the future (see # ethereum1_setup_block()), and when we try to add that block # to the chain (via Chain.add_block()), it will be put in a # queue for later processing. Since we need to ensure the # block has been added before we continue the test, we # have to manually process the time queue. log.debug('block mined too fast, processing time queue') chain.process_time_queue(new_time=block.timestamp) log.debug('block mined') assert chain.head.difficulty == 1 assert chain.head.number == head_number + 1 return chain.head def rpc_request(self, method, *args, **kwargs): """Simulate an incoming JSON RPC request and return the result. Example:: >>> assert test_app.rpc_request('eth_getBalance', '0x' + 'ff' * 20) == '0x0' """ log.debug('simulating rpc request', method=method, call_args=args, call_kwargs=kwargs) method = self.services.jsonrpc.dispatcher.get_method(method) res = method(*args, **kwargs) log.debug('got response', response=res) return json.dumps( dict(result=res, jsonrpc=JSONRPCProtocol.JSON_RPC_VERSION, id=42)) config = { 'data_dir': str(tmpdir), 'db': { 'implementation': 'EphemDB' }, 'pow': { 'activated': False }, 'p2p': { 'min_peers': 0, 'max_peers': 0, 'listen_port': 29873 }, 'node': { 'privkey_hex': encode_hex(mk_random_privkey()) }, 'discovery': { 'boostrap_nodes': [], 'listen_port': 29873 }, 'eth': { 'block': { # reduced difficulty, increased gas limit, allocations to test accounts 'ACCOUNT_INITIAL_NONCE': request.param, 'GENESIS_DIFFICULTY': 1, 'BLOCK_DIFF_FACTOR': 2, # greater than difficulty, thus difficulty is constant 'GENESIS_GAS_LIMIT': 3141592, 'GENESIS_INITIAL_ALLOC': { encode_hex(tester.accounts[0]): { 'balance': 10**24 }, encode_hex(tester.accounts[1]): { 'balance': 1 }, encode_hex(tester.accounts[2]): { 'balance': 10**24 }, } } }, 'jsonrpc': { 'listen_port': 4488, 'listen_host': '127.0.0.1' } } services = [ DBService, AccountsService, PeerManager, ChainService, PoWService, JSONRPCServer ] update_config_with_defaults(config, get_default_config([TestApp] + services)) update_config_with_defaults( config, {'eth': { 'block': ethereum.config.default_config }}) app = TestApp(config) for service in services: service.register_with_app(app) def fin(): log.debug('stopping test app') for service in app.services: gevent.sleep(.1) try: app.services[service].stop() except Exception as e: log.DEV(str(e), exc_info=e) pass app.stop() gevent.killall(task for task in gc.get_objects() if isinstance(task, gevent.Greenlet)) request.addfinalizer(fin) log.debug('starting test app') app.start() return app
def hydrachain_create_blockchain(private_keys, hydrachain_private_keys, p2p_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 def privkey_to_uri(private_key): host = b'0.0.0.0' pubkey = privtopub(private_key) return host_port_pubkey_to_uri(host, p2p_base_port, pubkey) account_addresses = [ privatekey_to_address(priv) for priv in private_keys ] alloc = { encode_hex(address): { 'balance': DEFAULT_BALANCE_BIN, } for address in account_addresses } genesis = { 'nonce': '0x00006d6f7264656e', 'difficulty': '0x20000', 'mixhash': '0x00000000000000000000000000000000000000647572616c65787365646c6578', 'coinbase': '0x0000000000000000000000000000000000000000', 'timestamp': '0x00', 'parentHash': '0x0000000000000000000000000000000000000000000000000000000000000000', 'extraData': '0x', 'gasLimit': GAS_LIMIT_HEX, 'alloc': alloc, } bootstrap_nodes = [ privkey_to_uri(hydrachain_private_keys[0]), ] validators_addresses = [ privatekey_to_address(private_key) for private_key in hydrachain_private_keys ] all_apps = [] for number, private_key in enumerate(hydrachain_private_keys): config = konfig.get_default_config(services + [HPCApp]) 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=private_key, ) config['data_dir'] = datadir config['hdc']['validators'] = validators_addresses config['node']['privkey_hex'] = encode_hex(private_key) 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'] = p2p_base_port + number config['p2p']['listen_port'] = p2p_base_port + number config['p2p']['min_peers'] = min(10, len(hydrachain_private_keys) - 1) config['p2p']['max_peers'] = len(hydrachain_private_keys) * 2 # only one of the nodes should have the Console service running if number != 0 and Console.name not in config['deactivated_services']: config['deactivated_services'].append(Console.name) hydrachain_app = start_app(config, accounts=[account]) all_apps.append(hydrachain_app) hydrachain_wait(private_keys, len(hydrachain_private_keys) - 1) return all_apps
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