2

As far as I understand, one can count the UTXOs by iterating through the blockchain starting at block height 0 and counting the number of outputs and subtracting the number of inputs for each block and summing up the result. In addition to this, subtract any provably unspendable transactions such as OP_RETURNs. I believe the list of provably unspendable transactions is something a long the lines of:

  • transaction outputs that start with OP_RETURNS
  • transaction outputs that are greater than the transaction size limit

Are there any knowledge gaps above?

If not, how do you count the list of provably unspendable transactions.

  • OP_RETURNS: Is it just count the TX that has OP_RETURN in their TX? Is there anything else to do here?
  • TX size restriction: How does one go about counting this?
3

When you say "transactions", I assume you mean "transaction output".

It is literally not possible to have a transaction output that is larger than the transaction size limit. Otherwise the transaction containing that output would not be in the blockchain.

Rather I think what you are looking for is that the output script is larger than the maximum script size. Such scripts are provably unspendable and can be excluded from the UTXO set.

Bitcoin Core has a function to determine whether outputs are unspendable. It is not an exhaustive list and is just the two that you mention: Scripts beginning with OP_RETURN, and scripts larger than the max script size.

However there are other scripts that are provably unspendable, just that they require a little more work to prove. For example scripts that use invalid or disabled opcodes are provably unspendable. The script interpreter will fail if any such opcode is found in the script when it is verifying the spend.

But even that has a catch. Unknown opcodes that are not executed (e.g. in an unexecuted IF branch), the script can still pass validation. However if the script contains any of the disabled opcodes anywhere (there are a few exceptions to this as some "disabled" opcodes were actually just outright removed), it is invalid.

There are also potentially scripts that require something larger than the maximum stack item size to be pushed to the stack. Or something that manipulates a stack item so that it becomes a negative locktime when used with OP_CHECKLOCKTIMEVERIFY or OP_CHECKSEQUENCEVERIFY.

Then there are output scripts that are programmed invalidly such as having just an OP_ELSE or OP_ENDIF with no OP_IF/OP_NOTIF coming before it.

All of these things are hard to determine and really require inspecting the individual scripts. They also require having a consensus conforming script interpreter, which itself is hard to do because the exact semantics of scripts are actually unclear.

3
  • then how does websites like blockchain.com create the UTXO charts? – LeanMan Jan 16 at 4:38
  • @LeanMan They do the easy thing of just looking for OP_RETURNs and (maybe) scripts larger than the max script size. – Andrew Chow Jan 16 at 4:44
  • I did try to calculate it like that and I was off by a few thousand or so UTXO – LeanMan Jan 16 at 4:59
0

While I was searching for something else yesterday and getting errors , I found this code which may be helpful in your case:

https://github.com/in3rsha/bitcoin-utxo-dump

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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