def get_blocks(self, height, limit, serialized, with_transactions=False): blocks = (Block.select().where( Block.height.between(height, height + limit)).order_by(Block.height.asc())) if with_transactions: block_ids = [block.id for block in blocks] transactions = (Transaction.select().where( Transaction.block_id.in_(block_ids)).order_by( Transaction.block_id.asc(), Transaction.sequence.asc())) transactions_map = defaultdict(list) for trans in transactions: # TODO: implement from_object on transaction and use that, instead of # creating it from serialized data. if serialized: transactions_map[trans.block_id].append(trans.serialized) else: transactions_map[trans.block_id].append( from_serialized(trans.serialized)) crypto_blocks = [] for block in blocks: crypto_block = CryptoBlock.from_object(block) if with_transactions: crypto_block.transactions = transactions_map[block.id] crypto_blocks.append(crypto_block) return crypto_blocks
def get_block_by_id(self, block_id): try: block = Block.get(Block.id == block_id) except Block.DoesNotExist: return None else: return CryptoBlock.from_object(block)
def get_last_block(self): """Get the last block Returns None if block can't be found. """ try: block = Block.select().order_by(Block.height.desc()).get() except Block.DoesNotExist: return None else: crypto_block = CryptoBlock.from_object(block) return crypto_block
def get_blocks_by_heights(self, heights): if not isinstance(heights, list): raise Exception("heights must be a type of list") blocks = Block.select().where(Block.height.in_(heights)) return [CryptoBlock.from_object(block) for block in blocks]
def get_blocks_by_id(self, block_ids): blocks = (Block.select().where(Block.id.in_(block_ids)).order_by( Block.height.desc())) return [CryptoBlock.from_object(block) for block in blocks]