6

I am new in the blockchain field, and I am trying to build my own block explorer from scratch to really learn about Bitcoin. I am currently running a full node in a raspberry pi, and I have already decoded the chainstate/UTXO-set with a levelDB library for Node.js. The content of this database is not exactly what I found in the documentation around, but led me well enough to fully decode this database.

Basically, the keys are the transaction ids with the index of the output, and the values contain the height, wether it is a coinbase transaction or not, the amount, and the address (before the base58 encoding) of every utxo.

However, I don't really know how to query this database in an efficient way to get all the utxos for a specific address without scanning the whole chainstate. It took me about 10 minutes to get all the utxos of a single address which is totally unacceptable.

I guess my specific question is how do I query this database through the values instead of the keys? Or if there is a better way to do this, maybe through the blockchain itself? maybe to an RPC bitcoin-cli call?

3 Answers 3

5

There isn't really a way to do this efficiently, as Bitcoin Core is not designed for that.

You can search using the the scantxoutset RPC command, but all that is doing is iterating over the entire database and returning matching entries (it's more powerful than just by address though; it can for example search for an xpub + derivation path too).

There is some work around adding support for an optional (and separate) address-based index to Bitcoin Core, but that will at the earliest be for v0.21.

Other than that, there are a number of other software packages that can build such an index (independently from Bitcoin Core).

7
  • OK. Thanks for your reply. I wonder, however, how does a wallet know where to get the inputs for an outgoing transaction efficiently if there is only the "scantxoutset" command? Do they have a parallel database where they sort the UTXO set by address instead of tx_id? Mar 19, 2020 at 17:41
  • 1
    The wallet maintains its own list of UTXOs (which is just a tiny set), which is built by simply observing every block. Mar 19, 2020 at 17:58
  • So the "balance" of an address is calculated through querying the parsed database version of the blockchain itself, and the UTXO set is used only to check double spending? Mar 19, 2020 at 19:15
  • "Balance of an address" is an illusion created by block explorer websites. Full nodes or wallets don't care about the concept - they just need a set of UTXOs that are yours. Apart from accounting reasons, knowing the balance of an address is meaningless as you still need to know the actual UTXOs in order to spend them. Furthermore, taking common privacy advice into account, addresses shouldn't be used for more than one payment, so you need the aggregate of all your addresses in any case. Mar 19, 2020 at 19:18
  • sorry, but that is not helpful. I understand what you are saying, but my question is not an abstract one. I just want to know how to get the utxo of an address so I can build my own raw transactions without the need of an external block explorer. I am running my own full node, so I should be able to achieve this myself, but I'm not sure how to do it. Should I parse the blockchain to a database, query the address for all historical transactions and then check the chainstate to check which ones are spendable? Or can I achieve this efficiently (in a second max) by only looking at the chainstate? Mar 19, 2020 at 19:46
0

The bitcoin listunspent rpc call does exactly what you are looking for:

listunspent ( minconf maxconf ["address",...] include_unsafe query_options )

Returns array of unspent transaction outputs with between minconf and maxconf (inclusive) confirmations.

Optionally filter to only include txouts paid to specified addresses.

Source.

1
  • However, listunspent works only if there is a wallet loaded, so it is not really useful for an explorer.
    – Adam B
    Oct 18, 2022 at 9:45
0

I have written a program in Python3 that allows you to search any address on bitcoin core whether it belongs to your wallet or not.

Here is the github link:

https://github.com/ORP967/Bitcoin_Core_RPC_par_address

If you have any improvements to it let me know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.