def get_status(self): try: state = self.mongo_cli.get_one(table=FLAGS.chain_status, cond={FLAGS.block_height: 0}) except Exception as e: raise exception.DBError(e) return None if state is None else state
def get_tx_by_hash(self, txhash): try: tx = self.mongo_cli.get_one(table=FLAGS.transaction_info, cond={FLAGS.tx_id: txhash}) except Exception, e: raise exception.DBError(e)
def select_txout_by_hash(self, txhash): try: tx = self.mongo_cli.get_one(table=FLAGS.transaction_info, cond={FLAGS.tx_id: txhash}) txouts = tx.get[FLAGS.transaction_out] except Exception, e: raise exception.DBError(e)
def get_latest_block_miner(self): height = self.get_recent_height() try: miner = self.mongo_cli.get_one(table=FLAGS.block_info, cond={FLAGS.block_height: height}, fields={'miner': 1, '_id': 0}) except Exception as e: raise exception.DBError(e) return miner.get('miner')
def get_difficulty(self): try: height = self.get_recent_height() difficulty = self.mongo_cli.get_one(FLAGS.block_info, {'height': height}, fields={'difficulty': 1, '_id': 0}) except Exception as e: raise exception.DBError(e) return difficulty.get('difficulty')
def request_node_status(self): try: stats = self.mongo_cli.get_many(table=FLAGS.node_status, n=1, sort_key=FLAGS.timestamp, ascend=False) except Exception as e: raise exception.DBError(e) return stats
def get_block_latest_in_range(self, start, end): try: blocks = self.mongo_cli.get_many(table=FLAGS.block_info, n=end - start, sort_key=FLAGS.block_height, ascend=False, skip=start) except Exception as e: raise exception.DBError(e) return blocks
def get_transactions_in_range(self, low, high): try: coinbase = self.mongo_cli.get_many( table=FLAGS.block_info, items={"transactions": 1, "_id": 0}, n=high - low, skip=low) except Exception as e: raise exception.DBError(e) return coinbase
def get_timestamps_in_range(self, start, end): try: timestamps = self.mongo_cli.get_many( table=FLAGS.block_info, items={"timestamp": 1, "_id": 0}, n=end-start, skip=start) except Exception as e: raise exception.DBError(e) return timestamps
def get_hash_rate_in_range(self, start, end): try: hash_rates = self.mongo_cli.get_many( table=FLAGS.block_info, items={"hash_rate": 1, "_id": 0}, n=end-start, skip=start) except Exception as e: raise exception.DBError(e) return hash_rates
def get_last_block_interval(self): height = self.get_recent_height() if height < 1: return None last_height = height - 1 try: time = self.mongo_cli.get_one(table=FLAGS.block_info, cond={FLAGS.block_height: height}, fields={'timestamp': 1, '_id': 0}) last_time = self.mongo_cli.get_one(table=FLAGS.block_info, cond={FLAGS.block_height: last_height}, fields={'timestamp': 1, '_id': 0}) interval = time.get('timestamp') - last_time.get('timestamp') except Exception as e: raise exception.DBError(e) return interval
def get_block_by_hash(self, blockhash): try: if not is_hash_prefix(blockhash): raise Exception("Block hash is wrong!") block = self.mongo_cli.get_one(table=FLAGS.block_info, cond={FLAGS.block_id: blockhash}) if block is None: raise Exception("Block not found!") block_info = self._show_block(block) except Exception as e: raise exception.DBError(e) return block_info
def select_txout_by_id(self, txout_id): try: txout = self.mongo_cli.get_one(table=FLAGS.transaction_info, cond={FLAGS.txout_id: txout_id}) except Exception, e: raise exception.DBError(e)
def save_node(self, status): try: self.mongo_cli.insert(flags.FLAGS.node_status, status) except Exception as e: raise exception.DBError(e)
def save_chain_patch(self, status_list): try: self.mongo_cli.insert_many(flags.FLAGS.chain_status, status_list) except Exception as e: raise exception.DBError(e)
def get_node_stats_list(self): try: stats = self.mongo_cli.get_many(table=FLAGS.node_status) except Exception as e: raise exception.DBError(e) return None if stats is None else stats
def get_recent_height(self): try: state = self.mongo_cli.get(flags.FLAGS.db_status) except Exception as e: raise exception.DBError(e) return None if state is None else state[flags.FLAGS.block_height]
def get_total_addr_num(self): try: total_num = self.mongo_cli.count(FLAGS.address_info) except Exception as e: raise exception.DBError(e) return total_num
def get_total_tx_num(self): try: total_num = self.mongo_cli.count(FLAGS.transaction_info) except Exception as e: raise exception.DBError(e) return total_num