def init_peer(self): Peer.create_upnp_mapping(self.config) my_peer = { 'host': self.config.peer_host, 'port': self.config.peer_port, 'identity': { "username": self.config.username, "username_signature": self.config.username_signature, "public_key": self.config.public_key }, 'peer_type': self.config.peer_type } if my_peer.get('peer_type') == 'seed': self.config.peer = Seed.from_dict(my_peer, is_me=True) elif my_peer.get('peer_type') == 'seed_gateway': if not self.config.username_signature in self.config.seed_gateways: raise Exception( 'You are not a valid SeedGateway. Could not find you in the list of SeedGateways' ) my_peer['seed'] = self.config.seed_gateways[ self.config.username_signature].seed self.config.peer = SeedGateway.from_dict(my_peer, is_me=True) elif my_peer.get('peer_type') == 'service_provider': self.config.peer = ServiceProvider.from_dict(my_peer, is_me=True) elif my_peer.get( 'peer_type') == 'user' or True: # default if not specified self.config.peer = User.from_dict(my_peer, is_me=True)
async def get_previous_consensus_block_from_remote(self, block): # TODO: async conversion retry = 0 peers = self.config.mongo.async_db.consensus.find({ 'block.prevHash': block.prev_hash, 'peer': { '$ne': 'me' } }) async for peer in peers: #self.app_log.warning('response code: {} {}'.format(res.status_code, res.content)) new_block = await Block.from_dict( json.loads(res.content.decode('utf-8'))) await self.insert_consensus_block(new_block, Peer.from_string(peer['peer'])) yield new_block
async def connect(self, peer: Peer): try: stream = await super(NodeSocketClient, self).connect(peer) if not stream: return await self.write_params(stream, 'connect', {'peer': self.config.peer.to_dict()}) stream.peer.token = str(uuid4()) await self.write_params(stream, 'challenge', { 'peer': self.config.peer.to_dict(), 'token': stream.peer.token }) await self.wait_for_data(stream) except StreamClosedError: get_config().app_log.error('Cannot connect to {}: {}'.format( peer.__class__.__name__, peer.to_json()))
async def connect(self, body, stream): params = body.get('params') if not params.get('peer'): stream.close() return {} generic_peer = Peer.from_dict(params.get('peer')) if isinstance(self.config.peer, Seed): if generic_peer.identity.username_signature in self.config.seeds: peerCls = Seed elif generic_peer.identity.username_signature in self.config.seed_gateways: peerCls = SeedGateway elif isinstance(self.config.peer, SeedGateway): if generic_peer.identity.username_signature in self.config.seeds: peerCls = Seed else: peerCls = ServiceProvider elif isinstance(self.config.peer, ServiceProvider): if generic_peer.identity.username_signature in self.config.seed_gateways: peerCls = SeedGateway else: peerCls = User elif isinstance(self.config.peer, User): peerCls = User else: self.config.app_log.error( 'inbound peer is not defined, disconnecting') stream.close() return {} limit = self.config.peer.__class__.type_limit(peerCls) if (len(NodeSocketServer.inbound_pending[peerCls.__name__]) + len( NodeSocketServer.inbound_streams[peerCls.__name__])) >= limit: await self.write_result(stream, 'capacity', {}, body['id']) stream.close() return {} try: stream.peer = peerCls.from_dict(params.get('peer')) except: self.config.app_log.error('invalid peer identity') stream.close() return {} if generic_peer.rid in NodeSocketServer.inbound_pending[ stream.peer.__class__.__name__]: stream.close() return {} if generic_peer.rid in NodeSocketServer.inbound_streams[ stream.peer.__class__.__name__]: stream.close() return {} if generic_peer.rid in self.config.nodeClient.outbound_ignore[ stream.peer.__class__.__name__]: stream.close() return if generic_peer.rid in self.config.nodeClient.outbound_pending[ stream.peer.__class__.__name__]: stream.close() return if generic_peer.rid in self.config.nodeClient.outbound_streams[ stream.peer.__class__.__name__]: stream.close() return try: result = verify_signature( base64.b64decode(stream.peer.identity.username_signature), stream.peer.identity.username.encode(), bytes.fromhex(stream.peer.identity.public_key)) if result: self.config.app_log.info('new {} peer is valid'.format( stream.peer.__class__.__name__)) except: self.config.app_log.error('invalid peer identity signature') stream.close() return {} NodeSocketServer.inbound_streams[peerCls.__name__][ stream.peer.rid] = stream self.config.app_log.info('Connected to {}: {}'.format( stream.peer.__class__.__name__, stream.peer.to_json())) return {}