def app(request): app = BaseApp() ChainServiceMock.register_with_app(app) PoWService.register_with_app(app) app.start() request.addfinalizer(lambda: app.stop()) return app
def test_app_restart(): host, port = '127.0.0.1', 3020 a_config = dict(p2p=dict(listen_host=host, listen_port=port), node=dict(privkey_hex=crypto.sha3('a').encode('hex'))) a_app = BaseApp(a_config) peermanager.PeerManager.register_with_app(a_app) # Restart app 10-times: there should be no exception for i in xrange(0, 10): a_app.start() assert a_app.services.peermanager.server.started try_tcp_connect((host, port)) assert a_app.services.peermanager.num_peers() == 0 a_app.stop() assert a_app.services.peermanager.is_stopped # Start the app 10-times: there should be no exception like 'Bind error' for i in xrange(0, 10): a_app.start() assert a_app.services.peermanager.server.started try_tcp_connect((host, port)) a_app.stop() assert a_app.services.peermanager.is_stopped
def test_baseservice(): app = BaseApp() class TestService(service.BaseService): counter = 0 def _run(self): while True: self.counter += 1 gevent.sleep(0.01) s = TestService.register_with_app(app) assert hasattr(s, "name") # register another services TestService.name = "other" s2 = TestService.register_with_app(app) # start app app.start() gevent.sleep(0.1) app.stop() # check if they ran concurrently assert s.counter > 0 and s.counter <= 10 assert s2.counter > 0 and s2.counter <= 10
def get_connected_apps(): a_config = dict(p2p=dict(listen_host='127.0.0.1', listen_port=3005), node=dict(privkey_hex=encode_hex(crypto.sha3(b'a')))) b_config = copy.deepcopy(a_config) b_config['p2p']['listen_port'] = 3006 b_config['node']['privkey_hex'] = encode_hex(crypto.sha3(b'b')) a_app = BaseApp(a_config) peermanager.PeerManager.register_with_app(a_app) a_app.start() b_app = BaseApp(b_config) peermanager.PeerManager.register_with_app(b_app) b_app.start() a_peermgr = a_app.services.peermanager b_peermgr = b_app.services.peermanager # connect host = b_config['p2p']['listen_host'] port = b_config['p2p']['listen_port'] pubkey = crypto.privtopub(decode_hex(b_config['node']['privkey_hex'])) a_peermgr.connect((host, port), remote_pubkey=pubkey) return a_app, b_app
def connect_go(): a_config = dict(p2p=dict(listen_host='127.0.0.1', listen_port=3000), node=dict(privkey_hex=crypto.sha3('a').encode('hex'))) a_app = BaseApp(a_config) peermanager.PeerManager.register_with_app(a_app) a_app.start() a_peermgr = a_app.services.peermanager # connect pubkey = "6ed2fecb28ff17dec8647f08aa4368b57790000e0e9b33a7b91f32c41b6ca9ba21600e9a8c44248ce63a71544388c6745fa291f88f8b81e109ba3da11f7b41b9".decode( 'hex') a_peermgr.connect(('127.0.0.1', 30303), remote_pubkey=pubkey) gevent.sleep(50) a_app.stop()
def test_protocol(): peer = PeerMock() proto = P2PProtocol(peer, WiredService(BaseApp())) # ping pong proto.send_ping() ping_packet = peer.packets.pop() proto._receive_ping(ping_packet) pong_packet = peer.packets.pop() proto._receive_pong(pong_packet) assert not peer.packets # hello (fails same nodeid) proto.send_hello() hello_packet = peer.packets.pop() proto._receive_hello(hello_packet) disconnect_packet = peer.packets.pop() # same nodeid assert disconnect_packet.cmd_id == P2PProtocol.disconnect.cmd_id assert not peer.stopped # FIXME: @heikoheiko this fails currently # hello (works) proto.send_hello() hello_packet = peer.packets.pop() peer.config['node']['id'] = '\x01' * 64 # change nodeid proto._receive_hello(hello_packet) assert not peer.packets assert not peer.stopped # FIXME: @heikoheiko this fails currently assert peer.hello_received # disconnect proto.send_disconnect(reason=proto.disconnect.reason.disconnect_requested) disconnect_packet = peer.packets.pop() proto._receive_disconnect(disconnect_packet) assert not peer.packets assert peer.stopped
def __init__(self, keystore_dir): # must be imported after `patch_find_library_android()` from devp2p.app import BaseApp from pyethapp.accounts import AccountsService self.keystore_dir = keystore_dir self.app = BaseApp( config=dict(accounts=dict(keystore_dir=self.keystore_dir))) AccountsService.register_with_app(self.app) self.patch_ethereum_tools_keys()
def setup(): peer = PeerMock() proto = ETHProtocol(peer, WiredService(BaseApp())) chain = tester.state() cb_data = [] def cb(proto, **data): cb_data.append((proto, data)) return peer, proto, chain, cb_data, cb
def _init_pyethapp(self, keystore_dir=None): if keystore_dir is None: keystore_dir = self.get_keystore_path() # must be imported after `patch_find_library_android()` from devp2p.app import BaseApp from pyethapp.accounts import AccountsService self.pyethapp = BaseApp(config=dict(accounts=dict( keystore_dir=keystore_dir))) AccountsService.register_with_app(self.pyethapp)
def get_app(port, seed): config = ConfigParser.ConfigParser() config.add_section('p2p') config.set('p2p', 'listen_host', '127.0.0.1') config.set('p2p', 'listen_port', str(port)) config.set('p2p', 'privkey_hex', crypto.sha3(seed).encode('hex')) # create app app = BaseApp(config) discovery.NodeDiscovery.register_with_app(app) return app
def setup(): peer = PeerMock() proto = ETHProtocol(peer, WiredService(BaseApp())) proto.service.app.config['eth'] = dict(network_id=1337) chain = tester.state() cb_data = [] def cb(proto, **data): cb_data.append((proto, data)) return peer, proto, chain, cb_data, cb
def test_eip8_hello(): peer = PeerMock() proto = P2PProtocol(peer, WiredService(BaseApp())) test_packet = Packet(cmd_id=1, payload=eip8_hello) proto._receive_hello(test_packet) assert peer.hello_received assert peer.remote_client_version == "kneth/v0.91/plan9" assert peer.remote_hello_version == 22 assert peer.remote_pubkey == 'fda1cff674c90c9a197539fe3dfb53086ace64f83ed7c6eabec741f7f381cc803e52ab2cd55d5569bce4347107a310dfd5f88a010cd2ffd1005ca406f1842877'.decode( 'hex')
def get_app(port, seed): config = dict(discovery=dict(), node=dict(privkey_hex=crypto.sha3(seed).encode('hex'))) config_discovery = config['discovery'] config_discovery['listen_host'] = '127.0.0.1' config_discovery['listen_port'] = port config_discovery['bootstrap_nodes'] = [] # create app app = BaseApp(config) discovery.NodeDiscovery.register_with_app(app) return app
def get_app(port, seed): config = dict(p2p=dict()) config_p2p = config['p2p'] config_p2p['listen_host'] = '127.0.0.1' config_p2p['listen_port'] = port config_p2p['privkey_hex'] = crypto.sha3(seed).encode('hex') # create app app = BaseApp(config) discovery.NodeDiscovery.register_with_app(app) return app
def app(request): app = BaseApp(config=dict(accounts=dict(keystore_dir=tempfile.mkdtemp()))) AccountsService.register_with_app(app) def fin(): # cleanup temporary keystore directory assert app.config['accounts']['keystore_dir'].startswith(tempfile.gettempdir()) shutil.rmtree(app.config['accounts']['keystore_dir']) log.debug('cleaned temporary keystore dir', dir=app.config['accounts']['keystore_dir']) request.addfinalizer(fin) return app
def get_connected_apps(): a_config = dict(p2p=dict(listen_host='127.0.0.1', listen_port=3000), node=dict(privkey_hex=crypto.sha3('a').encode('hex'))) b_config = copy.deepcopy(a_config) b_config['p2p']['listen_port'] = 3001 b_config['node']['privkey_hex'] = crypto.sha3('b').encode('hex') a_app = BaseApp(a_config) peermanager.PeerManager.register_with_app(a_app) a_app.start() b_app = BaseApp(b_config) peermanager.PeerManager.register_with_app(b_app) b_app.start() a_peermgr = a_app.services.peermanager b_peermgr = b_app.services.peermanager # connect host = b_config['p2p']['listen_host'] port = b_config['p2p']['listen_port'] pubkey = crypto.privtopub(b_config['node']['privkey_hex'].decode('hex')) a_peermgr.connect((host, port), remote_pubkey=pubkey) return a_app, b_app
def test_baseservice(): app = BaseApp() class TestService(service.BaseService): counter = 0 def _run(self): while True: self.counter += 1 gevent.sleep(0.01) s = TestService.register_with_app(app) assert hasattr(s, 'name') # register another services TestService.name = 'other' s2 = TestService.register_with_app(app) # start app app.start() gevent.sleep(.1) app.stop() # check if they ran concurrently assert s.counter > 0 and s2.counter > 0 assert abs(s.counter - s2.counter) <= 2
def test_callback(): peer = PeerMock() proto = P2PProtocol(peer, WiredService(BaseApp())) # setup callback r = [] def cb(_proto, **data): assert _proto == proto r.append(data) proto.receive_pong_callbacks.append(cb) # trigger proto.send_ping() ping_packet = peer.packets.pop() proto._receive_ping(ping_packet) pong_packet = peer.packets.pop() proto._receive_pong(pong_packet) assert not peer.packets assert len(r) == 1 assert r[0] == dict()
def main(): import gevent import signal client_name = 'exampleapp' version = '0.1' client_version = '%s/%s/%s' % (version, sys.platform, 'py%d.%d.%d' % sys.version_info[:3]) client_version_string = '%s/v%s' % (client_name, client_version) default_config = dict(BaseApp.default_config) update_config_with_defaults(default_config, peermanager.PeerManager.default_config) update_config_with_defaults(default_config, NodeDiscovery.default_config) default_config['client_version_string'] = client_version_string default_config['post_app_start_callback'] = None default_config['node'][ 'privkey_hex'] = "b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291" default_config['discovery']['bootstrap_nodes'] = [ "enode://762dd8a0636e07a54b31169eba0c7a20a1ac1ef68596f1f283b5c676bae4064abfcce24799d09f67e392632d3ffdc12e3d6430dcb0ea19c318343ffa7aae74d4@127.0.0.1:22332" ] app = BaseApp(default_config) peermanager.PeerManager.register_with_app(app) NodeDiscovery.register_with_app(app) gevent.get_hub( ).SYSTEM_ERROR = BaseException # (KeyboardInterrupt, SystemExit, SystemError) app.start() # wait for interupt evt = gevent.event.Event() gevent.signal(signal.SIGQUIT, evt.set) gevent.signal(signal.SIGTERM, evt.set) gevent.signal(signal.SIGINT, evt.set) evt.wait() # finally stop app.stop()
def __init__(self, keystore_dir=None): if keystore_dir is None: keystore_dir = PyWalib.get_default_keystore_path() self.app = BaseApp(config=dict(accounts=dict( keystore_dir=keystore_dir))) AccountsService.register_with_app(self.app)
blk = self.app.services.chain.chain.get(blockhash) for i in range(blk.transaction_count): tx_lst_serialized, sr, _ = blk.get_transaction(i) txhash_hex = sha3(rlp.encode(tx_lst_serialized)).encode('hex') txs.append(self.trace_transaction(txhash_hex, exclude)) return txs if __name__ == '__main__': import inspect from devp2p.app import BaseApp # deactivate service availability check for cls in JSONRPCServer.subdispatcher_classes(): cls.required_services = [] app = BaseApp(JSONRPCServer.default_config) jrpc = JSONRPCServer(app) dispatcher = jrpc.dispatcher def show_methods(dispatcher, prefix=''): # https://github.com/micheles/decorator/blob/3.4.1/documentation.rst for name, method in dispatcher.method_map.items(): print prefix + name, inspect.formatargspec(public_methods[name]) for sub_prefix, subdispatcher_list in dispatcher.subdispatchers.items( ): for sub in subdispatcher_list: show_methods(sub, prefix + sub_prefix) show_methods(dispatcher)
def recv_found_nonce(self, bin_nonce, mixhash, mining_hash): log.info('nonce found', mining_hash=mining_hash.encode('hex')) blk = self.app.services.chain.chain.head_candidate if blk.mining_hash != mining_hash: log.warn('mining_hash does not match') self.mine_head_candidate() return blk.mixhash = mixhash blk.nonce = bin_nonce self.app.services.chain.add_mined_block(blk) def mine_head_candidate(self): self.on_new_head_candidate(self.app.services.chain.chain.head_candidate) def _run(self): self.mine_head_candidate() while True: cmd, kargs = self.ppipe.get() assert isinstance(kargs, dict) getattr(self, 'recv_' + cmd)(**kargs) def stop(self): self.worker_process.terminate() self.worker_process.join() super(PoWService, self).stop() if __name__ == "__main__": app = BaseApp() PoWService.register_with_app(app) app.start()
def test_wiredservice(): app = BaseApp(config=dict()) s = service.WiredService.register_with_app(app)
def test_session(): proto = P2PProtocol(peer=PeerMock(), service=WiredService(BaseApp())) hello_packet = proto.create_hello() responder_privkey = mk_privkey('secret1') responder = MultiplexedSession(responder_privkey, hello_packet=hello_packet) p0 = 0 responder.add_protocol(p0) initiator_privkey = mk_privkey('secret2') initiator = MultiplexedSession(initiator_privkey, hello_packet=hello_packet, remote_pubkey=privtopub(responder_privkey)) initiator.add_protocol(p0) # send auth msg = initiator.message_queue.get_nowait() assert msg # auth_init assert initiator.packet_queue.empty() assert not responder.is_initiator # receive auth responder.add_message(msg) assert responder.packet_queue.empty() assert responder.is_ready # send auth ack and hello ack_msg = responder.message_queue.get_nowait() hello_msg = responder.message_queue.get_nowait() assert hello_msg # receive auth ack & hello initiator.add_message(ack_msg + hello_msg) assert initiator.is_ready hello_packet = initiator.packet_queue.get_nowait() assert isinstance(hello_packet, Packet) # initiator sends hello hello_msg = initiator.message_queue.get_nowait() assert hello_msg # hello received by responder responder.add_message(hello_msg) hello_packet = responder.packet_queue.get_nowait() assert isinstance(hello_packet, Packet) # assert we received an actual hello packet data = proto.hello.decode_payload(hello_packet.payload) assert data['version'] # test normal operation ping = proto.create_ping() initiator.add_packet(ping) msg = initiator.message_queue.get_nowait() # receive ping responder.add_message(msg) ping_packet = responder.packet_queue.get_nowait() assert isinstance(ping_packet, Packet) data = proto.ping.decode_payload(ping_packet.payload) # reply with pong pong = proto.create_ping() responder.add_packet(pong) msg = responder.message_queue.get_nowait() # receive pong initiator.add_message(msg) pong_packet = initiator.packet_queue.get_nowait() assert isinstance(pong_packet, Packet) data = proto.pong.decode_payload(pong_packet.payload)