libbitcoin / libbitcoin-client

Bitcoin Client Query Library
Other
37 stars 42 forks source link

How to get all addresses with positive balance? #278

Closed Stonica closed 3 years ago

Stonica commented 3 years ago

Hi,

First thanks for your useful tool. I am developing an application which need all Bitcoin addresses with positive balance in near real-time I appreciate if you help or point me to a document on how to do that suing libbitcoin, libbitcoin-server and libbitcoin-client?

evoskuil commented 3 years ago

It would be helpful if you could clarify "real time", since "all Bitcoin addresses" are a vast amount of historical data.

evoskuil commented 3 years ago

In other words, if something has happened in the distant past, "real time" isn't a meaningful concept.

Stonica commented 3 years ago

"realtime" here: I need to update my list of bitcoin addresses with positive balance just any address balance turned to zero or a new address got non-zero balance, as soon as possible. It was better to say: I need a list of bitcoin addresses with non-zero balance which updates if any address balance is changed or new address got balance.

evoskuil commented 3 years ago

That's helpful.

To obtain balances for all addresses, you need to query all addresses. There is an index of all addresses, with payments and therefore balance. You can query any address to obtain its balance. However there is no query to obtain all addresses. The only way to obtain all addresses is to query all transactions (which means all blocks).

Getting real-time changes is straightforward, you just subscribe to confirmed blocks or transactions. You will however have to handle the case of reorganization, since blocks/transactions can become unconfirmed.

evoskuil commented 3 years ago

In Version3 you can query only standard addresses, basically p2sh and p2pkh. Version4 will support query for any type of payment, with or without any address format. But given your requirements, I would not query for addresses at all, I would simply query all blocks and then subscribe to new blocks (which includes reorgs).

I would then maintain a database of output scripts with an entry for each input and output against that script. To obtain balance you would simply run a summary query against each unique output script.

Note that for inputs this means obtaining the corresponding output script. This is done by querying for the transaction output that is referenced by the input. This is also easily accomplished using a transaction query.

Reorgs remove existing entries from the table. You would need to maintain the transaction hash for all entries to make this possible.

For standard payments (i.e. ones that actually have addresses) you can compute the address from the script and store it in the table. But not all transfers correspond to addresses.

evoskuil commented 3 years ago

Note that libbitcoin's store is indexed for the purpose of validating the chain (i.e. as a node) to support mining and wallet queries. Its additional payments index exists so that, given an address, one can query for all payments to/from that address (and therefore may also obtain balance). This provides support for block explorers and SPV wallets.

Arbitrary queries against blockchain data (such as all addresses with a certain balance) require a general purpose query engine or creation of a custom index for each query.

Stonica commented 3 years ago

Thanks for your help