7

I know that UTXOs are stored in chainstate database.

But how can I get them in simple txt view? Not balances, but each UTXO.

2 Answers 2

10

With vanilla Bitcoin Core, there is no efficient way to do this.

I see two options:

a) Slow and very inefficient RPC loop (not recommended)

  1. Get the genesis block hash (RPC getblockhash 0)
  2. Get the block with all transaction (RPC getblock <hash> 2, 2 stands for verbosity with transaction)
  3. Loop through all transactions and all its outputs, call gettxout <txid> <n> (where n is the outputs index)
  4. If gettxout returns an object, the output is unspent (UTXO), dump it to a text file

This may take a couple of hours or days (depending on your machine).

b) Fast way by patching Core (0.17.1)

  1. Change the code of the function call GetUTXOStats(), apply a text file dump in the utxo set loop (while (pcursor->Valid()) {)
  2. Compile
  3. Call RPC gettxoutsetinfo and let your added code dump data per UTXO to a file (or similar)
1

https://github.com/in3rsha/bitcoin-utxo-dump seems to work well too, if you don't want to patch Bitcoin Core.

As per README, once you've fully synced the blockchain, quit the Bitcoin server and run:

sudo apt install libleveldb-dev
go get github.com/in3rsha/bitcoin-utxo-dump
bitcoin-utxo-dump -db /path/to/.bitcoin/chainstate/

This produces a large utxodump.csv file with about 8GB size (about 2x the chainstate/ directory size) in 14 minutes when I tried it, with format:

count,txid,vout,amount,type,address
1,033e83e3204b0cc28724e147f6fd140529b2537249f9c61c9de9972750030000,0,65279,p2pkh,1KaPHfvVWNZADup3Yc26SfVdkTDvvHySVX
2,e1c9467a885a156e56a29d9c854e65674d581ad75611b02290454b4862060000,1,9466355,p2pkh,1LpCmEejWLNfZigApMPwUY9nZTS8NTJCNS
3,a1f28c43f1f3d4821d0db42707737ea90616613099234f905dfc6ae2b4060000,1,339500,p2pkh,1FuphZ7xVPGrxthQT1S8X7nNQNByYxAT3V
4,818f5b9e3ede69da765d4c24684e813057c9b1f059e098661369b0a2ee060000,0,300000,p2pkh,18Y9yhjU9g2jjJmvaUy7TmUNZH9iPzQ4dd
5,d2f5e439152d076593a145581f8d76ea2e48ed155285b9a245cd42dd06070000,0,100000,p2pkh,1EKHTvovYWHfUJ6i9vsoidyTPQauCPH1qC
6,ea0c69fbd2389556b01771948ffc0507cf303bdc5a1b91b31acf9ecf6a070000,1,27668,p2pkh,1fkEhLpPKdmKtaxKdp4yDp1c87dF7GDub
7,05eafead65250a24b1592f8a006cbeab16a7b17ed2616507c5e0bd67bd070000,1,32000,p2pkh,15KmfJcGNfL29vpsSJ37uPzTQfr8Qe17Gq
8,2c0c985d384160d8c50c438bc67e639fe6047a7f2bac00a1238ca6a6d3070000,0,41936,p2pkh,17up1oPxBMTfZdehzy4v81KzLRHGDNX8ff
9,8261170b7ae26be70bd9e8f0e4bf19ce3571bb6464cdf9e478c471d372080000,1,4528208,p2pkh,1P6Ae7unrSjtx9J5SjWuwAdZBoWcbcjzBZ
...

Here's a quick script to import that CSV into sqlite3 which is more manageable:

echo | sqlite3 utxodump.sqlite3 \
-cmd 'CREATE TABLE utxo(
  count INT,
  txid CHAR(64),
  vout INT,
  amount INT,
  type CHAR(12),
  address CHAR(34)
)' \
-cmd 'CREATE UNIQUE INDEX utxo_txid_vout
  ON utxo(txid, vout);' \
-cmd '.mode csv' \
-cmd '.import utxodump.csv utxo'

I used that here BTW: https://github.com/cirosantilli/bitcoin-strings-with-txids/tree/7e95546479508e9fe5158dad6bc8601e2b4e02ee#utxo_nonstandard to try and find unspent puzzles that I might be able to solve, but I didn't :-(

3
  • Currently utxodump gives error: Couldn't open LevelDB. file missing.
    – pbies
    Commented Oct 18 at 1:56
  • @pbies are you sure your bitcoin data isn't in a custom directory and try pointing to it with -db? E.g. bitcoin snap on Ubuntu has custom location ~/snap/bitcoin-core/ Commented Oct 18 at 7:39
  • Folders are ok.
    – pbies
    Commented Oct 18 at 12:07

Your Answer

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

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