def __init__(self, app): self.config = app.config sce = self.config['eth'] if int(sce['pruning']) >= 0: self.db = RefcountDB(app.services.db) if "I am not pruning" in self.db.db: raise Exception("This database was initialized as non-pruning." " Kinda hard to start pruning now.") self.db.ttl = int(sce['pruning']) self.db.db.put("I am pruning", "1") else: self.db = app.services.db if "I am pruning" in self.db: raise Exception("This database was initialized as pruning." " Kinda hard to stop pruning now.") self.db.put("I am not pruning", "1") if 'network_id' in self.db: db_network_id = self.db.get('network_id') if db_network_id != str(sce['network_id']): raise Exception("This database was initialized with network_id {} " "and can not be used when connecting to network_id {}".format( db_network_id, sce['network_id']) ) else: self.db.put('network_id', str(sce['network_id'])) self.db.commit() assert self.db is not None WiredService.__init__(self, app) log.info('initializing chain') coinbase = app.services.accounts.coinbase env = Env(self.db, sce['block']) self.chain = Chain(env, new_head_cb=self._on_new_head, coinbase=coinbase) log.info('chain at', number=self.chain.head.number) if 'genesis_hash' in sce: assert sce['genesis_hash'] == self.chain.genesis.hex_hash() self.transaction_queue = Queue(maxsize=self.transaction_queue_size) self.add_blocks_lock = False self.add_transaction_lock = gevent.lock.BoundedSemaphore() self.broadcast_filter = DuplicatesFilter() self.on_new_head_cbs = [] self.on_new_head_candidate_cbs = [] self.newblock_processing_times = deque(maxlen=1000) # Consensus self.consensus_contract = ConsensusContract(validators=self.config['hdc']['validators']) self.consensus_manager = ConsensusManager(self, self.consensus_contract, self.consensus_privkey) # lock blocks that where proposed, so they don't get mutated self.proposal_lock = ProposalLock() assert not self.proposal_lock.is_locked()
def __init__(self, app): log.info('PeerManager init') WiredService.__init__(self, app) self.peers = [] self.errors = PeerErrors( ) if self.config['log_disconnects'] else PeerErrorsBase() self.listen_addr = (self.config['p2p']['listen_host'], self.config['p2p']['listen_port']) self.server = StreamServer(self.listen_addr, handle=self._on_new_connection)
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 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 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 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 __init__(self, app): log.info('PeerManager init') WiredService.__init__(self, app) self.peers = [] self.errors = PeerErrors( ) if self.config['log_disconnects'] else PeerErrorsBase() self.result_dir = self.config["result_dir"] if self.config[ "result_dir"] != "" else self.result_dir self.prev_routing_table = self.config[ "prev_routing_table"] if self.config[ "prev_routing_table"] != "" else self.prev_routing_table self.prev_peers = self.config["prev_peers"] if self.config[ "prev_peers"] != "" else self.prev_peers # setup nodeid based on privkey print self.config if 'id' not in self.config['p2p']: self.config['node']['id'] = crypto.privtopub( self.config['node']['privkey_hex'].decode('hex')) self.listen_addr = (self.config['p2p']['listen_host'], self.config['p2p']['listen_port'])
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 __init__(self, app): self.config = app.config sce = self.config['eth'] if int(sce['pruning']) >= 0: self.db = RefcountDB(app.services.db) if "I am not pruning" in self.db.db: raise Exception("This database was initialized as non-pruning." " Kinda hard to start pruning now.") self.db.ttl = int(sce['pruning']) self.db.db.put("I am pruning", "1") else: self.db = app.services.db if "I am pruning" in self.db: raise Exception("This database was initialized as pruning." " Kinda hard to stop pruning now.") self.db.put("I am not pruning", "1") if 'network_id' in self.db: db_network_id = self.db.get('network_id') if db_network_id != str(sce['network_id']): raise Exception( "This database was initialized with network_id {} " "and can not be used when connecting to network_id {}". format(db_network_id, sce['network_id'])) else: self.db.put('network_id', str(sce['network_id'])) self.db.commit() assert self.db is not None WiredService.__init__(self, app) log.info('initializing chain') coinbase = app.services.accounts.coinbase env = Env(self.db, sce['block']) self.chain = Chain(env, new_head_cb=self._on_new_head, coinbase=coinbase) log.info('chain at', number=self.chain.head.number) if 'genesis_hash' in sce: assert sce['genesis_hash'] == self.chain.genesis.hex_hash() self.transaction_queue = Queue(maxsize=self.transaction_queue_size) self.add_blocks_lock = False self.add_transaction_lock = gevent.lock.BoundedSemaphore() self.broadcast_filter = DuplicatesFilter() self.on_new_head_cbs = [] self.on_new_head_candidate_cbs = [] self.newblock_processing_times = deque(maxlen=1000) # Consensus self.consensus_contract = ConsensusContract( validators=self.config['hdc']['validators']) self.consensus_manager = ConsensusManager(self, self.consensus_contract, self.consensus_privkey) # lock blocks that where proposed, so they don't get mutated self.proposal_lock = ProposalLock() assert not self.proposal_lock.is_locked()
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)
def __init__(self, app): log.info('PeerCrawler init') WiredService.__init__(self, app) self.peers = [] self.errors = PeerErrors( ) if self.config['log_disconnects'] else PeerErrorsBase()