Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Using get_unordered_blocks from example throws: UnboundLocalError: local variable 'transaction' referenced before assignment` #95

@misaleh

Description

@misaleh

Snippet

tx=faa008e846efd7a89ede1ff96687781e2dbafb3af3f5a8813e68b8bbae463e5c outputno=0 type=pubkeyhash value=1000357
tx=faa008e846efd7a89ede1ff96687781e2dbafb3af3f5a8813e68b8bbae463e5c outputno=1 type=pubkeyhash value=2088051
Traceback (most recent call last):
File "btc.py", line 15, in
for tx in block.transactions:
File "/home/dragon/.local/lib/python3.8/site-packages/blockchain_parser/block.py", line 89, in transactions
self._transactions = list(get_block_transactions(self.hex))
File "/home/dragon/.local/lib/python3.8/site-packages/blockchain_parser/block.py", line 41, in get_block_transactions
offset += transaction.size
UnboundLocalError: local variable 'transaction' referenced before assignment

It seems that in block.py the second for loop is skipped by exception

     for i in range(n_transactions):
         # Try from 1024 (1KiB) -> 1073741824 (1GiB) slice widths
         for j in range(0, 20):
             try:
                 offset_e = offset + (1024 * 2 ** j)
                 transaction = Transaction.from_hex(
                     transaction_data[offset:offset_e])
                 yield transaction
                 break
             except:
                 continue

I did a local fix to skip this error

     for i in range(n_transactions):
         # Try from 1024 (1KiB) -> 1073741824 (1GiB) slice widths
+        skp  = 0
         for j in range(0, 20):
             try:
                 offset_e = offset + (1024 * 2 ** j)
                 transaction = Transaction.from_hex(
                     transaction_data[offset:offset_e])
+                skp  = transaction.size
                 yield transaction
                 break
             except:
                 continue
 
         # Skipping to the next transaction
-        offset += transaction.size
+        offset += skp
 
 

I will try to do more analysis and make a pull request if find the root cause

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions