Skip to content

Commit

Permalink
Add index when parsing block transactions as dict
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryp Toon committed Feb 13, 2024
1 parent a3ae415 commit fa04fcb
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
7 changes: 5 additions & 2 deletions bitcoinlib/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/test_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit fa04fcb

Please sign in to comment.