diff --git a/bitcoinlib/blocks.py b/bitcoinlib/blocks.py index d0526d13..4384d646 100644 --- a/bitcoinlib/blocks.py +++ b/bitcoinlib/blocks.py @@ -300,11 +300,13 @@ def parse_transactions_dict(self): """ transactions_dict = [] txs_data_orig = deepcopy(self.txs_data) + index = 0 while self.txs_data and len(self.transactions) < self.tx_count: - tx = self.parse_transaction_dict() + tx = self.parse_transaction_dict(index) if not tx: break transactions_dict.append(tx) + index += 1 self.txs_data = txs_data_orig return transactions_dict @@ -321,7 +323,7 @@ def parse_transaction(self): return t return False - def parse_transaction_dict(self): + def parse_transaction_dict(self, index=None): """ Parse a single transaction from Block, if transaction data is available in txs_data attribute. Add Transaction object in Block and return the transaction @@ -407,6 +409,7 @@ def parse_transaction_dict(self): tx['txid'] = double_sha256(tx['version'][::-1] + raw_n_inputs + inputs_raw + raw_n_outputs + outputs_raw + tx_locktime)[::-1] tx['size'] = len(tx['rawtx']) + tx['index'] = index # TODO: tx['vsize'] = len(tx['rawtx']) return tx return False diff --git a/tests/test_blocks.py b/tests/test_blocks.py index e107cf9c..e4d0ecb3 100644 --- a/tests/test_blocks.py +++ b/tests/test_blocks.py @@ -208,5 +208,6 @@ def test_block_parse_transaction_dict(self): self.assertEqual(2668, len(b.transactions)) i = 0 for tx in tx_dict: + self.assertEqual(tx['index'], i) assert(tx['txid'].hex() == b.transactions[i].txid) i += 1