python-bitcoin-blockchain-parser icon indicating copy to clipboard operation
python-bitcoin-blockchain-parser copied to clipboard

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

Open misaleh opened this issue 4 years ago • 1 comments

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

misaleh avatar Jun 14 '21 11:06 misaleh

I believe I am experiencing the same problem with get_ordered_blocks.

Traceback (most recent call last):
  File "/home/pi/Projects/python/btcsearch/make_funded_address_list.py", line 12, in <module>
    for transaction in block.transactions:
                       ^^^^^^^^^^^^^^^^^^
  File "/home/pi/Projects/python/btcsearch/venv/lib/python3.11/site-packages
blockchain_parser/block.py", line 90, in transactions
    self._transactions = list(get_block_transactions(self.hex))
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/pi/Projects/python/btcsearch/venv/lib/python3.11/site-packages
blockchain_parser/block.py", line 41, in get_block_transactions
    offset += transaction.size
    ^^^^^^
UnboundLocalError: cannot access local variable 'transaction' where it is not associated with a value

dkeate-dev avatar Aug 23 '25 20:08 dkeate-dev