def get(self, height=0, range=None, fields=None): database = lib.get_db() fields = lib.fields_to_list(fields) if height == 0: height = grin.get_current_height() if range == None: stat = Grin_stats.get_by_height(height) if stat is None: return None return stat.to_json(fields) else: stats = [] for stat in Grin_stats.get_by_height(height, range): stats.append(stat.to_json(fields)) return stats
def calculate(height, avg_range=DIFFICULTY_ADJUST_WINDOW): # Get the most recent blocks from which to generate the stats recent_blocks = [] previous_stats_record = Grin_stats.get_by_height(height-1) print("XXX: {}".format(previous_stats_record)) assert previous_stats_record is not None, "No provious stats record found" recent_blocks = Blocks.get_by_height(height, avg_range) if len(recent_blocks) < min(avg_range, height): # We dont have all of these blocks in the DB raise AssertionError("Missing blocks in range: {}:{}".format(height-avg_range, height)) assert recent_blocks[-1].height == height, "Invalid height in recent_blocks[-1]" assert recent_blocks[-2].height == height - 1, "Invalid height in recent_blocks[-2]: {} vs {}".format(recent_blocks[-2].height, height - 1) # Calculate the stats data first_block = recent_blocks[0] last_block = recent_blocks[-1] timestamp = last_block.timestamp difficulty = recent_blocks[-1].total_difficulty - recent_blocks[-2].total_difficulty new_stats = Grin_stats( height = height, timestamp = timestamp, difficulty = difficulty, ) # Caclulate estimated GPS for recent edge_bits sizes all_gps = estimate_all_gps(recent_blocks) for gps in all_gps: gps_rec = Gps( edge_bits = gps[0], gps = gps[1], ) new_stats.gps.append(gps_rec) return new_stats
def recalculate(start_height, avg_range): database = lib.get_db() height = start_height while height <= grin.blocking_get_current_height(): old_stats = Grin_stats.get_by_height(height) new_stats = calculate(height, avg_range) if old_stats is None: database.db.createDataObj(new_stats) else: old_stats.timestamp = new_stats.timestamp old_stats.difficulty = new_stats.difficulty old_stats.gps = new_stats.gps old_stats.difficulty = new_stats.difficulty old_stats.total_utxoset_size = new_stats.total_utxoset_size database.db.getSession().commit() height = height + 1
def avg_network_gps(height=0, range=60): if height == 0: height = Blocks.get_latest().height if range <= 0: range = 1 grinstats = Grin_stats.get_by_height(height, range) gpslists = [stat.gps for stat in grinstats] gpslists_len = len(gpslists) if gpslists_len == 0: return 0 gpstotals = {} for gpslist in gpslists: for gps in gpslist: if gps.edge_bits not in gpstotals: gpstotals[gps.edge_bits] = 0 gpstotals[gps.edge_bits] += gps.gps gpsavgs = {} for sz, gpstotal in gpstotals.items(): gpsavgs[sz] = gpstotal / gpslists_len return gpsavgs
def get(self, height=None, range=None, fields=None): LOGGER = lib.get_logger(PROCESS) LOGGER.warn("GrinAPI_stats get height:{} range:{} fields:{}".format( height, range, fields)) fields = lib.fields_to_list(fields) if height is None or height == 0: stats = Grin_stats.get_latest(range) else: stats = Grin_stats.get_by_height(height, range) #pp.pprint(stats) if range == None: if stats is None: return None return stats.to_json(fields) else: st = [] for stat in stats: st.append(stat.to_json(fields)) return st
def calculate(height, avg_range): # Get the most recent blocks from which to generate the stats recent_blocks = [] previous_stats_record = Grin_stats.get_by_height(height - 1) print("XXX: {}".format(previous_stats_record)) assert previous_stats_record is not None, "No provious stats record found" recent_blocks = Blocks.get_by_height(height, avg_range) if len(recent_blocks) < min(avg_range, height): # We dont have all of these blocks in the DB raise AssertionError("Missing blocks in range: {}:{}".format( height - avg_range, height)) print(recent_blocks[-1]) print(recent_blocks[-2]) print(recent_blocks[-3]) print(recent_blocks[-4]) assert recent_blocks[ -1].height == height, "Invalid height in recent_blocks[-1]" assert recent_blocks[ -2].height == height - 1, "Invalid height in recent_blocks[-2]: {} vs {}".format( recent_blocks[-2].height, height - 1) # Calculate the stats data first_block = recent_blocks[0] last_block = recent_blocks[-1] timestamp = last_block.timestamp difficulty = recent_blocks[-1].total_difficulty - recent_blocks[ -2].total_difficulty gps = lib.calculate_graph_rate(difficulty, first_block.timestamp, last_block.timestamp, len(recent_blocks)) # utxo set size = sum outputs - sum inputs total_utxoset_size = previous_stats_record.total_utxoset_size + last_block.num_outputs - last_block.num_inputs return Grin_stats( height=height, timestamp=timestamp, gps=gps, difficulty=difficulty, total_utxoset_size=total_utxoset_size, )
def get_stats(height): ## # Get requested grin network stats as seen by our grin node return Grin_stats.get_by_height(height)