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 open(self): user = User.from_dict({ 'host': None, 'port': None, 'identity': { 'username': self.get_secure_cookie('username'), 'username_signature': self.get_secure_cookie('username_signature'), 'public_key': self.get_secure_cookie('public_key') } }) RCPWebSocketServer.inbound_streams[User.__name__][user.rid] = self self.peer = user self.peer.groups = {}
async def connect(self, body): params = body.get('params') if not params.get('identity'): self.close() return {} peer = User.from_dict({ 'host': None, 'port': None, 'identity': params.get('identity') }) self.peer = peer self.peer.groups = {} RCPWebSocketServer.inbound_streams[User.__name__][peer.rid] = self try: result = verify_signature( base64.b64decode(peer.identity.username_signature), peer.identity.username.encode(), bytes.fromhex(peer.identity.public_key)) if not result: self.close() except: self.config.app_log.error('invalid peer identity signature') self.close() return {} try: self.config.app_log.info('new {} is valid'.format( peer.__class__.__name__)) await self.write_result('connect_confirm', { 'identity': self.config.peer.identity.to_dict, 'shares_required': self.config.shares_required, 'credit_balance': await self.get_credit_balance(), 'server_pool_address': f'{self.config.peer_host}:{self.config.stratum_pool_port}' }, body=body) except: self.config.app_log.error('invalid peer identity signature') self.close() return {}
async def route(self, body): # our peer SHOULD only ever been a service provider if we're offering a websocket but we'll give other options here params = body.get('params') transaction = Transaction.from_dict(params['transaction']) await self.config.mongo.async_db.miner_transactions.replace_one( {'id': transaction.transaction_signature}, transaction.to_dict(), upsert=True) if isinstance(self.config.peer, Seed): pass # for rid, peer_stream in self.config.nodeServer.inbound_streams[Seed.__name__].items(): # await BaseRPC().write_params(peer_stream, 'route', params) # for rid, peer_stream in self.config.nodeServer.inbound_streams[SeedGateway.__name__].items(): # await BaseRPC().write_params(peer_stream, 'route', params) # for rid, peer_stream in self.config.nodeClient.outbound_streams[Seed.__name__].items(): # await BaseRPC().write_params(peer_stream, 'route', params) elif isinstance(self.config.peer, SeedGateway): pass # for rid, peer_stream in self.config.nodeServer.inbound_streams[ServiceProvider.__name__].items(): # await BaseRPC().write_params(peer_stream, 'route', params) # for rid, peer_stream in self.config.nodeClient.outbound_streams[Seed.__name__].items(): # await BaseRPC().write_params(peer_stream, 'route', params) elif isinstance(self.config.peer, ServiceProvider): pass # for rid, peer_stream in self.config.nodeServer.inbound_streams[User.__name__].items(): # await BaseRPC().write_params(peer_stream, 'route', params) # for rid, peer_stream in self.config.nodeClient.outbound_streams[SeedGateway.__name__].items(): # await BaseRPC().write_params(peer_stream, 'route', params) if transaction.requested_rid in self.config.websocketServer.inbound_streams[ Group.__name__]: for rid, peer_stream in self.config.websocketServer.inbound_streams[ Group.__name__][transaction.requested_rid].items(): if rid == transaction.requester_rid: continue await peer_stream.write_params('route', params) if transaction.requested_rid in self.config.websocketServer.inbound_streams[ User.__name__]: peer_stream = self.config.websocketServer.inbound_streams[ User.__name__][transaction.requested_rid] if peer_stream.peer.rid != transaction.requester_rid: await peer_stream.write_params('route', params) if 'group' in params: group = Group.from_dict({ 'host': None, 'port': None, 'identity': params['group'] }) params2 = params.copy() to = User.from_dict({ 'host': None, 'port': None, 'identity': params['to'] }) peer_stream = self.config.websocketServer.inbound_streams[ Group.__name__][group.rid].get(to.rid) if peer_stream: await peer_stream.write_params('route', params2) elif isinstance(self.config.peer, User): pass # for rid, peer_stream in self.config.nodeClient.outbound_streams[ServiceProvider.__name__].items(): # await BaseRPC().write_params(peer_stream, 'route', params) else: self.config.app_log.error( 'inbound peer is not defined, disconnecting') self.close() return {} await self.write_result('route_server_confirm', {}, body=body)