1200wd / bitcoinlib

Bitcoin and other Cryptocurrencies Library for Python. Includes a fully functional wallet, Mnemonic key generation and management and connection with various service providers to receive and send blockchain and transaction information.
http://bitcoinlib.readthedocs.io/
GNU General Public License v3.0
596 stars 199 forks source link

Error on number of transactions retrieved on getblock(i) #357

Closed developer-betandeal closed 8 months ago

developer-betandeal commented 9 months ago

I have code that load the blocks searching for transactions with some hash, I use the service.getblock(number-of-block) to retrieve the block transactions.

s = Service(network, strict=False)
block = s.getblock(2541949)
block = block.as_dict()
for txid in block['transactions']:
...

The log says this:

2023-12-07 16:57:48,723 - bitcoinlib.services.services - INFO - Retrieved block with height 2541949 from cache
2023-12-07 16:57:48,724 - bitcoinlib.services.services - INFO - Retrieved transaction f95ea7eba15e9737fb3a0b37a95c8eca494f1bc4c4d65232277773efe4c103d1 from cache
2023-12-07 16:57:48,725 - bitcoinlib.services.services - INFO - Retrieved transaction d7104e8dbd3a3976bba93d928dc7ff6f256b492f6205bb932d1b898f98f5b87d from cache
2023-12-07 16:57:48,725 - bitcoinlib.services.services - INFO - Retrieved transaction 80590b6af0ba12ec1cd49329f67356b0d90ff67813a34f83bde5f92fe10a72ce from cache
2023-12-07 16:57:48,726 - bitcoinlib.services.services - INFO - Retrieved transaction 738dd830c5573aadd035984fec50ead3f56d5c7448fd25f3e37cc23c8bc2cd5d from cache
2023-12-07 16:57:48,726 - bitcoinlib.services.services - INFO - Retrieved transaction 60106a40a46e2b4846752cf88979e9d6d731462e55b24046abc94052fa63c7a9 from cache
2023-12-07 16:57:48,727 - bitcoinlib.services.services - INFO - Retrieved transaction 48d45c4af4283af9df4d6bcfd4b609939c75465765847796a706ece00367f553 from cache
2023-12-07 16:57:48,727 - bitcoinlib.services.services - INFO - Retrieved transaction 1791054c2f14810b51c8fb238c181babb70ce62ed756c0241eb97e07ff977bdb from cache
2023-12-07 16:57:48,728 - bitcoinlib.services.services - INFO - Retrieved transaction 45ccb80ed4095375d9f97c37760340cd9bbdc8f5eebadadc634d003f1dfd50e9 from cache
2023-12-07 16:57:48,728 - bitcoinlib.services.services - INFO - Retrieved transaction cd28ba4b634eaee463ac6c2c159445167a9c0403d499c655c776b4a04cf4f60f from cache
2023-12-07 16:57:48,729 - bitcoinlib.services.services - INFO - Retrieved transaction d206e5f0df20af1755abeac172dd05f33872186869575ef32636c48b280e2c78 from cache
2023-12-07 16:57:48,729 - bitcoinlib.services.services - INFO - Retrieved transaction ced5e89c303aa9dfb39ca88820f36141643467eeeba5bb65a33930e370bc8a81 from cache
2023-12-07 16:57:48,730 - bitcoinlib.services.services - INFO - Retrieved transaction 4e5985fe933d95a9c64038c5c9a2ddba714ff406e14c4c74ffa42e9c05fb3405 from cache
2023-12-07 16:57:48,730 - bitcoinlib.services.services - INFO - Retrieved transaction a11b06947a753fabf5f07583b7a603040b081a85cce5d60007b1d9ec1c92cfe9 from cache
2023-12-07 16:57:48,731 - bitcoinlib.services.services - INFO - Retrieved transaction 3d4469e7dfe7bedb484ad3429f232e4ceae118905bb094ab243af2b3ccef7932 from cache
2023-12-07 16:57:48,731 - bitcoinlib.services.services - INFO - Retrieved transaction ddb45232169bcb79a1c889b760a047b0a9dfd07bc7a7775edee6d2104a504dd5 from cache
2023-12-07 16:57:48,732 - bitcoinlib.services.services - INFO - Retrieved transaction 4599a31b24052fb25c3a7bef1e9e0922498d695299d671e1530b10b67f1ffcec from cache
2023-12-07 16:57:48,732 - bitcoinlib.services.services - INFO - Retrieved transaction fc8f1d32d4931d5ee7cff2d49be857901636747f2218bcce5ce988c25ea4baac from cache
2023-12-07 16:57:48,733 - bitcoinlib.services.services - INFO - Retrieved transaction fc1778b40eaf1d922ac8c490b3dd9c0539b7f5f852e9725feb8b0307610d4c3f from cache
2023-12-07 16:57:48,733 - bitcoinlib.services.services - INFO - Retrieved transaction a1a4f6c8272f3ae06da452756ae8c0745ace6b538ab1298c8c78cb96109b9a02 from cache
2023-12-07 16:57:48,734 - bitcoinlib.services.services - INFO - Retrieved transaction e426dde333d58c2787a7c3e72d87f8b04eb10b1993ec50b997f0bbf1d2b5b564 from cache
2023-12-07 16:57:48,734 - bitcoinlib.services.services - INFO - Retrieved transaction e5040b8d973933e96ad1bf427a7d1a9c8496e8b6402cde3bc1e294834cf27f25 from cache
2023-12-07 16:57:48,735 - bitcoinlib.services.services - INFO - Retrieved transaction 5d9947568229c15bcb9873f4adada24201aaa9d7b78000a4a749eca2ba2489af from cache
2023-12-07 16:57:48,735 - bitcoinlib.services.services - INFO - Retrieved transaction a902f22729ce9041180921714eda70193ee4ce6aa321142903342a7c5e2a5165 from cache
2023-12-07 16:57:48,736 - bitcoinlib.services.services - INFO - Retrieved transaction 564f0032946aea8d0fbb43469e01e8bcf2fd220b131b9a147eb8810dfc573879 from cache
2023-12-07 16:57:48,736 - bitcoinlib.services.services - INFO - Retrieved transaction 3796126e63ea1b809a18e64e85b86d4c877def9333c86373adc003e1bcd9a2e8 from cache

Total: 25 transactions

BUT if you search this block (testnet) https://live.blockcypher.com/btc-testnet/block/00000000000000129c29a48c1c2606cb09ad90ce26865a30f0e118c184cc50fc/

You can see that there are 120 transactions in the block... How can it be? What I'm doing wrong?

developer-betandeal commented 9 months ago

More interesting code:

>>>from bitcoinlib.services.services import Service
>>>s = Service ('testnet',strict=False)
>>>block = s.getblock(2541949)
>>> len(block.transactions)
25
>>> block
<Block(00000000000000129c29a48c1c2606cb09ad90ce26865a30f0e118c184cc50fc, 2541949, transactions: 120)>

As you can see block.transactions has only 25 elements BUT the block object says that have 120 transactions

developer-betandeal commented 9 months ago

I found the problem...

From documentation:

limit (int) – Maximum number of transactions to parse. Default is 0: parse all transactions. Only used if parse_transaction is set to True

But this is not true, the default seems to be 25. The code works correctly if you add limit=1000 in the getblock function.

mccwdev commented 9 months ago

If you searching for a specific transaction hash inside a block you do not have to parse transactions. Otherwise you generate a lot of extra requests to service providers.

from bitcoinlib.services.services import Service
s = Service ('testnet',strict=False)
block = s.getblock(2541949, parse_transactions=False)

This will return the block with all transaction id's.

developer-betandeal commented 8 months ago

Thank you,

But I'm searching for outputs to one known hash. I suppose this cannot be searched if you do not parse the transactions. Right?

mccwdev commented 8 months ago

No then you need to parse transactions. Best to use your own bitcoin or bcoin node, or get an API key somewhere because this will generate a lot of requests.

developer-betandeal commented 8 months ago

No then you need to parse transactions. Best to use your own bitcoin or bcoin node, or get an API key somewhere because this will generate a lot of requests.

Yes, I have my own node. I have configured in the providers.json and I see calls, but not all. There are some priority value to maximize the calls to our own node?

mccwdev commented 8 months ago

Yes you set the priority a bit higher then other providers. Then it first tries you own node, if it fails it switches to a provider with a lower priority.