def __init__(self): self.parser = SafeConfigParser() self.parser.read('settings.INI') self.api = async_httpapi.AHttpApi() self.redis_client = redis.StrictRedis(host='localhost', port=6379, db=12)
def __init__(self): self.parser = SafeConfigParser() self.parser.read('settings.INI') hostname = self.parser.get("blockexplorer", "node") self.api = async_httpapi.AHttpApi(baseurl=hostname) self.redis_client = redis.StrictRedis(host='localhost', port=6379, db=12)
def __init__(self, *args, **kwargs): super(BaseHandler, self).__init__(*args, **kwargs) self.user = self.session.get('user', None) locale = self.session.get('locale', "") self.parser = SafeConfigParser() self.parser.read("settings.INI") self.redis_client = RedisConnector.RedisConnector.redis_client self.api = async_httpapi.AHttpApi()
def __init__(self, *args, **kwargs): super(BaseHandler, self).__init__(*args, **kwargs) self.user = self.session.get('user', None) locale = self.session.get('locale', "") self.parser = SafeConfigParser() self.parser.read("settings.INI") self.redis_client = redis.StrictRedis(host='localhost', port=6379, db=12) hostname = self.parser.get("blockexplorer", "node") self.api = async_httpapi.AHttpApi(baseurl=hostname)
def run(self): try: #get neighbors active_nodes = {} response = yield self.api.getpeerlist() for node in json.loads(response.body)['active']: active_nodes[node['endpoint']['host']] = node #discover nodes beyond neighbors for host, data in active_nodes.items(): target_host = '%s://%s:%d' % (data['endpoint']['protocol'], host, data['endpoint']['port']) node_api = async_httpapi.AHttpApi(target_host) try: response = yield node_api.getpeerlist() except: print 'Error communitcating with %s' % target_host node_peers = json.loads(response.body)['active'] for peer in node_peers: if peer['endpoint']['host'] not in active_nodes.keys(): active_nodes[peer['endpoint']['host']] = peer self.redis_client.set('active_nodes', json.dumps(active_nodes)) except: traceback.print_exc()
class RedisConnector(): parser = SafeConfigParser() parser.read("settings.INI") api = async_httpapi.AHttpApi() redis_client = redis.StrictRedis(host='localhost', port=6379, db=12) refresh_after = int(parser.get("api", "blocks_to_reindex")) def _get_height(self): if self.redis_client.zcard('blocks') == 0: return 1 else: return int( json.loads( self.redis_client.zrange('blocks', 0, 1, 'desc')[0])['height']) def _calc_timestamp(self, timestamp): return time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(timestamp / 1000)) @tornado.gen.coroutine def update_redischain(self): try: response = yield self.api.getblocksafter(self._get_height()) for block in json.loads(response.body)['data']: block['timestamp_unix'] = block['timeStamp'] block['timestamp'] = self._calc_timestamp(block['timeStamp']) fees_total = 0 for tx in block['txes']: timestamps_unix = tx['timeStamp'] tx['timestamp'] = self._calc_timestamp(tx['timeStamp']) tx['block'] = block['height'] fees_total += tx['fee'] #save tx in redis self.redis_client.zadd('tx', timestamps_unix, tornado.escape.json_encode(tx)) self.redis_client.set(tx['hash'], tornado.escape.json_encode(tx)) self.redis_client.publish('tx_channel', tornado.escape.json_encode(tx)) #save blocks in redis self.redis_client.zadd('blocks', block['height'], tornado.escape.json_encode(block)) self.redis_client.set(block['hash'], tornado.escape.json_encode(block)) self.redis_client.publish('block_channel', tornado.escape.json_encode(block)) #stats self.redis_client.zincrby('harvesters', block['harvester'], 1.0) self.redis_client.zincrby('fees_earned', block['harvester'], fees_total) except: traceback.print_exc()
def run(self): try: oldNodes = json.loads((self.redis_client.get('active_nodes') or "{}")) self.activeNodes = {} self.allHosts = set() self.processedHosts = set() self.realActiveNodes = {} #get neighbors response = yield self.api.getPeerList() self.addActiveNodes('localhost', response) print 'running network discovery' start = datetime.utcnow() while len(self.allHosts) > 0: success("HOSTS LEFT %d PROCESSED %d" % (len(self.allHosts), len(self.processedHosts))) target_host = self.allHosts.pop() self.processedHosts.add(target_host) node_api = async_httpapi.AHttpApi(target_host) seenByOthers = self.activeNodes[target_host] try: response = yield node_api.getNodeInfo() seenByOthers = json.loads(response.body) except: error('ERROR[0] communitcating with %s' % target_host) pass if 'nisInfo' in seenByOthers: temp = seenByOthers['nisInfo'] seenByOthers = seenByOthers['node'] seenByOthers['nisInfo'] = temp self.realActiveNodes[target_host] = seenByOthers if self.realActiveNodes and ( target_host in self.realActiveNodes ) and self.realActiveNodes[target_host] and ( 'name' in self.realActiveNodes[target_host]['identity']): if self.realActiveNodes[target_host]['identity'][ 'name'] is None: self.realActiveNodes[target_host]['identity'][ 'name'] = "" print "HOST: ", target_host print self.realActiveNodes[target_host] self.realActiveNodes[target_host]['identity'][ 'name'] = self.realActiveNodes[target_host][ 'identity']['name'][0:128] self.fillChecks(target_host, self.realActiveNodes[target_host], oldNodes) self.realActiveNodes[target_host]['checks'][0] += 1 if isValidIp(seenByOthers['endpoint']['host']): seenByOthers['endpoint']['host'] = '.'.join( seenByOthers['endpoint']['host'].split('.')[0:2] + ['xx', 'xx']) print 'HOST %s nem name %s ' % ( target_host, self.realActiveNodes[target_host]['identity']['name']) try: response = yield node_api.getLastBlock() height = json.loads(response.body)['height'] print '%s last block: %s' % (target_host, height) self.realActiveNodes[target_host]['metaData'][ 'height'] = height self.realActiveNodes[target_host]['checks'][1] += 1 except: self.realActiveNodes[target_host]['metaData']['height'] = 0 error('ERROR[1] communitcating with %s' % target_host) continue try: response = yield node_api.getPeerList() print 'adding nodes seen by HOST %s' % (target_host) self.addActiveNodes(target_host, response) except: error('ERROR[2] communitcating with %s' % target_host) self.redis_client.set('active_nodes', json.dumps(self.realActiveNodes)) print self.realActiveNodes end = datetime.utcnow() success("Time taken: %s" % str(end - start)) self.redis_client.set('nodes_last_time', end) except: traceback.print_exc()