diff --git a/beem/block.py b/beem/block.py index e8657c4b1e9c3575702835aa76d47a499b4e3550..f7a70c8497d2084825c05feb8ad4e197562e57d7 100644 --- a/beem/block.py +++ b/beem/block.py @@ -139,6 +139,8 @@ class Block(BlockchainObject): """ Even though blocks never change, you freshly obtain its contents from an API with this method """ + if self.identifier is None and "id" in self: + self.identifier = self["id"] if not isinstance(self.identifier, int): self.identifier = int(self.identifier) if not self.steem.is_connected(): @@ -170,6 +172,7 @@ class Block(BlockchainObject): if not block: raise BlockDoesNotExistsException(str(self.identifier)) block = self._parse_json_data(block) + block["id"] = self.identifier super(Block, self).__init__(block, lazy=self.lazy, full=self.full, steem_instance=self.steem) @property @@ -186,13 +189,15 @@ class Block(BlockchainObject): """ Returns all transactions as list""" if self.only_ops or self.only_virtual_ops: return list() - trxs = self["transactions"] - for i in range(len(trxs)): - trx = trxs[i] - trx['transaction_id'] = self['transaction_ids'][i] - trx['block_num'] = self.block_num - trx['transaction_num'] = i - trxs[i] = trx + trxs = [] + i = 0 + for trx in self["transactions"]: + trx_new = trx.copy() + trx_new['transaction_id'] = self['transaction_ids'][i] + trx_new['block_num'] = self.block_num + trx_new['transaction_num'] = i + trxs.append(trx_new) + i += 1 return trxs @property @@ -214,19 +219,21 @@ class Block(BlockchainObject): """ Returns all transactions as list, all dates are strings.""" if self.only_ops or self.only_virtual_ops: return list() - trxs = self["transactions"] - for i in range(len(trxs)): - trx = trxs[i] - trx['transaction_id'] = self['transaction_ids'][i] - trx['block_num'] = self.block_num - trx['transaction_num'] = i + trxs = [] + i = 0 + for trx in self["transactions"]: + trx_new = trx.copy() + trx_new['transaction_id'] = self['transaction_ids'][i] + trx_new['block_num'] = self.block_num + trx_new['transaction_num'] = i if 'expiration' in trx: p_date = trx.get('expiration', datetime(1970, 1, 1, 0, 0)) if isinstance(p_date, (datetime, date)): - trx['expiration'] = formatTimeString(p_date) + trx_new['expiration'] = formatTimeString(p_date) else: - trx['expiration'] = p_date - trxs[i] = trx + trx_new['expiration'] = p_date + trxs.append(trx_new) + i += 1 return trxs @property @@ -235,8 +242,7 @@ class Block(BlockchainObject): if self.only_ops or self.only_virtual_ops: return self["operations"] ops = [] - trxs = self["transactions"] - for tx in trxs: + for tx in self["transactions"]: for op in tx["operations"]: # Replace opid by op name # op[0] = getOperationNameForId(op[0]) diff --git a/beem/blockchain.py b/beem/blockchain.py index a57c667fcc1f2b110387ed56463690ce303adfe3..b6f37ccec8975c8eabc45dd22bcfd79532f86697 100644 --- a/beem/blockchain.py +++ b/beem/blockchain.py @@ -246,6 +246,7 @@ class Blockchain(object): auto_clean = current_block.get_cache_auto_clean() current_block.set_cache_auto_clean(False) latest_block = 0 + starting_block = 0 for blocknum in range(start, head_block + 1, thread_num): futures = [] i = blocknum @@ -254,12 +255,31 @@ class Blockchain(object): i += 1 results = [r.result() for r in as_completed(futures)] block_nums = [] + missing_nums = [] + starting_block = int(results[0].identifier) for b in results: block_nums.append(int(b.identifier)) if latest_block < int(b.identifier): latest_block = int(b.identifier) + if starting_block > int(b.identifier): + starting_block = int(b.identifier) + if starting_block > blocknum: + for i in range(blocknum, starting_block): + missing_nums.append(i) + from operator import itemgetter blocks = sorted(results, key=itemgetter('id')) + last_i = blocks[0].identifier - 1 + for b in blocks: + if b.identifier - last_i > 1: + for j in range(last_i + 1, b.identifier): + missing_nums.append(j) + last_i = b.identifier + if len(missing_nums) > 0: + for block_num in missing_nums: + block = Block(block_num, only_ops=only_ops, only_virtual_ops=only_virtual_ops, steem_instance=self.steem) + results.append(block) + blocks = sorted(results, key=itemgetter('id')) for b in blocks: yield b current_block.clear_cache_from_expired_items()