def get(self, id=None, height=0, range=None): database = lib.get_db() print("id={} , height={}, range = {}".format(id, height, range)) if range == 0: range = grin.get_current_height() if height == 0: height = grin.get_current_height() return Pool_shares.count(height, range, id)
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 = Pool_stats.get_by_height(height) if stat is None: return None return stat.to_json(fields) else: stats = [] for stat in Pool_stats.get_by_height(height, range): stats.append(stat.to_json(fields)) return stats
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: block = Blocks.get_by_height(height) if block is None: return None return block.to_json(fields) else: blocks = [] for block in Blocks.get_by_height(height, range): blocks.append(block.to_json(fields)) return blocks
def get(self, id=None, height=0, range=None, fields=None): database = lib.get_db() print("id={} , height={}, range = {}, fields = {}".format( id, height, range, fields)) if height == 0: height = grin.get_current_height() + 1 if fields != None: fields = fields.split(',') shares = [] if id is None: for share in Pool_shares.get_by_height(height, range): shares.append(share.to_json(fields)) return shares else: for share in Pool_shares.get_by_user_and_height(id, height, range): shares.append(share.to_json(fields)) return shares
def get(self, id=None, height=0, range=None, fields=None): database = lib.get_db() fields = lib.fields_to_list(fields) if height == 0: height = grin.get_current_height() stats = [] if id == None: for stat in Worker_stats.get_by_height(height, range): stats.append(stat.to_json(fields)) return stats else: if range == None: res = Worker_stats.get_by_height_and_id(id, height) if res is None: return res return res.to_json(fields) else: for stat in Worker_stats.get_by_height_and_id( id, height, range): stats.append(stat.to_json(fields)) return stats
def main(): global LOGGER global CONFIG CONFIG = lib.get_config() LOGGER = lib.get_logger(PROCESS) LOGGER.warn("=== Starting {}".format(PROCESS)) # Connect to DB database = lib.get_db() atexit.register(lib.teardown_db) validation_depth = int(CONFIG[PROCESS]["validation_depth"]) latest = grin.get_current_height( ) - 10 # stop 10 blocks from current to avoid overrunning the blockWatcher last_block_record = Blocks.get_latest() if last_block_record == None: last_block_record_height = 0 else: last_block_record_height = last_block_record.height last = min(latest - validation_depth, last_block_record_height - validation_depth) # start a reasonable distance back if last < 0: last = 0 LOGGER.warn("Starting from block #{}".format(last)) for i in range(last, latest + 1): if i % 100 == 0: LOGGER.warn("Processing #{}".format(i)) response = grin.blocking_get_block_by_height(i) assert (response is not None) assert (int(response["header"]["height"]) == i) #print("{}: {}".format(response["header"]["height"], response["header"]["hash"])) try: database.db.initializeSession() rec = Blocks.get_by_height( i) # Get existing entry from the DB (if any) if rec is not None: # Test if we have an orphan thats not already marked # Dont update any block info in the orphan, just mark the state if rec.hash != response["header"][ "hash"] and rec.state != "orphan": LOGGER.warn( "Found an orphan - height: {}, hash: {} vs {}".format( rec.height, rec.hash, response["header"]["hash"])) rec.state = "orphan" database.db.getSession().commit() else: # If it was not in the DB then we should add it now LOGGER.warn("Adding missing block - height: {}".format( response["header"]["height"])) missing_block = Blocks( hash=response["header"]["hash"], version=response["header"]["version"], height=response["header"]["height"], previous=response["header"]["previous"], timestamp=datetime.strptime( response["header"]["timestamp"][:-1], "%Y-%m-%dT%H:%M:%S+00:0"), output_root=response["header"]["output_root"], range_proof_root=response["header"]["range_proof_root"], kernel_root=response["header"]["kernel_root"], nonce=response["header"]["nonce"], edge_bits=response["header"]["edge_bits"], total_difficulty=response["header"]["total_difficulty"], secondary_scaling=response["header"]["secondary_scaling"], num_inputs=len(response["inputs"]), num_outputs=len(response["outputs"]), num_kernels=len(response["kernels"]), fee=sum(k["fee"] for k in response["kernels"]), lock_height=response["kernels"][0]["lock_height"] if len(response["kernels"]) > 0 else 0, total_kernel_offset=response["header"] ["total_kernel_offset"], state="missing") database.db.createDataObj(missing_block) except Exception as e: LOGGER.error("Something went wrong: {} - {}".format( e, traceback.print_stack())) database.db.getSession().rollback() database.db.destroySession() sys.stdout.flush() time.sleep(0.1) # dont be too aggressive LOGGER.warn("=== Completed {}".format(PROCESS))