7

The Bitcoin developer guide states that

each output of a particular transaction can only be used as an input once in the block chain. Any subsequent reference is a forbidden double spend—an attempt to spend the same satoshis twice.

So how does a Bitcoin network node actually mark a transaction's output as spent? I imagine it would need to be irreversible, so that other nodes cannot revert the "spent" marker, but I cannot think of a way that Bitcoin could ensure that.

Where in the standard Bitcoin code does this marking occur, and how does it occur?

2 Answers 2

14

Every full Bitcoin node maintains a database of which unspent outputs are left.

When verifying a transaction, all its inputs are fetched from the database. If one is missing, validation fails. Among the data retrieved is the value of those unspent outputs, and their script (od address), which define the conditions under which the output can be spent. This information is necessary to validate whether the spending transaction has the correct signatures and does not create more bitcoin than it consumes.

If all validation of all transactions in a block succeed, the consumed inputs are removed from the database, and all outputs of those transactions added as fresh unspent outputs in the database, allowing them to be spent by future blocks.

As this database only contains outputs (so no signatures, for example), and even only the unspent ones, it is much smaller than the entire blockchain (some 450 MB as of juli 2014). So, no, we don't go scan through the entire blockchain to know whether outputs are not double-spent - we keep a separate database with just the data we need from it for validation.

3
  • This answer goes more into detail than the previous one, so I'm accepting this one instead. Thanks!
    – feralin
    Jul 2, 2014 at 15:49
  • I get it about only spending one, but this transaction confuses me, which appears to reduce the balance of an address twice on separate occasions (this is the tipjar for bitref.com FYI) blockchain.info/address/… Feb 5, 2018 at 18:12
  • Block explorers will confuse you. Addresses don't hold a balance. There are only outputs. Each output has an address an and amount. Transaction inputs spend one specific previous output completely. Feb 5, 2018 at 18:43
3

The next input for this particular output is a "marker" that this output is already spent

4
  • So when I, as a miner node, try to verify a transaction, I need to look through the entire block chain to see if any of the transaction's output have been used as an input already? Is that the only way to check that an output has been "marked" as spent?
    – feralin
    Jun 30, 2014 at 15:27
  • @feralin thats the way how its done and its the only way if you want to do it on your own Jun 30, 2014 at 17:32
  • @Recall so there is nothing in the transaction data that says that a certain output has been spent?
    – feralin
    Jun 30, 2014 at 18:14
  • 1
    No its not, a transaction output cant be modified so there cant be a flag or something else to mark it Jun 30, 2014 at 18:16

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.