7

This is probably a stupid question that has an answer somewhere, but I was unable to find anything, so please feel free to redirect me !

I have a basic understanding of how the bitcoin blockchain works, namely, you sign and broadcast your transaction, it gets picked up by the nodes and is eventally included in a block that is mined.

I understand that as long as there isn't someone controlling more than 50% of the mining power, it is unlikely to be able to perform a double spend attempt.

However, what prevents someone from spending more bitcoins that they have ? For example, if I have 1 BTC on my adress, suppose I run a mining node, what prevents me from trying to mine a block in which I spend 1.2 BTC ? At what point in the process is the balance of my adress checked to make sure I do have the bitcoin that I want to spend ?

At first I thought that it was the nodes that verified that the transaction on the mempool are valid before starting to mine them but this can't be it, since you could have a malicious node that does whatever (although it would be difficult to pull off without enough mining power).

This check must be somehow "hard-coded" somewhere, in the sense that it is actually impossible to create a transaction of inexistent bitcoin. Could someone point me to a resource or elaborate how does this work ?

1

3 Answers 3

9

if I have 1 BTC on my address, suppose I run a mining node, what prevents me from trying to mine a block in which I spend 1.2 BTC ?

This is the specific set of code in Bitcoin Core that rejects anyone's transaction who's is putting less money into the transaction (inputs) than the recipients are receiving (outputs), e.g. spending more money than they have added to the transaction:

const CAmount value_out = tx.GetValueOut();
if (nValueIn < value_out) {
    return state.DoS(100, false, REJECT_INVALID, "bad-txns-in-belowout", false,
        strprintf("value in (%s) < value out (%s)", FormatMoney(nValueIn), FormatMoney(value_out)));
}

Technically, this isn't checking the "balance" of an address, since in Bitcoin system itself, addresses don't have balances. When you receive bitcoins, you receive them to an individually-identifiable output (payment). When you spend bitcoins, you identify exactly which output (or outputs) you want to spend and spend all of that value at once.

If you want to pay someone less than the full value of one of your outputs, you add an extra output on to your payment that returns the remainder to yourself. This is like paying for a $2 snack with a $5 bill and receiving $3 change back to yourself (and it Bitcoin, we call that extra output paying yourself the "change output" by analogy).

So in the code above, by checking every single spend in the system to prevent anyone from receivng more bitcoins than were initially spent (except for newly-minted bitcoins), we prevent negative balances.

At what point in the process is the balance of my adress checked to make sure I do have the bitcoin that I want to spend ?

As explained above, inputs and outputs (rather than address balances) are what's checked. They're ultimately checked when a block is received; however, the software may check them for unconfirmed transactions and cache the result so that it can process newly-received blocks faster.


Somewhat related, you also can't pay someone a negative output:

    if (txout.nValue < 0)
        return state.DoS(100, false, REJECT_INVALID, "bad-txns-vout-negative");
2
  • Is that the actual code, and if so, why does it check for "less than" rather than "not equal"? Wouldn't it make more sense to require that each transaction cause no net change in the total amount of Bitcoin available?
    – Mark
    Jun 5, 2018 at 0:08
  • 2
    @Mark The difference is paid as fees to the miner Jun 5, 2018 at 1:08
2

Each output on the chain is assigned a certain value. When you spend outputs in a transaction, the sum of the new outputs created cannot exceed the sum of the outputs being consumed (the inputs). This is part of transaction validation.

Moreover, even if a malicious miner were to mine a block in which they created extra bitcoins, the rest of the network would reject it as it would not longer meet the consensus rules which includes the transaction amount check. Thus, the miner's block would be rejected, and they would lose any money they spent on mining that block, as the block reward is invalidated as well.

4
  • What do you mean "the rest of the network would reject it" ? How does this rejection happen ?
    – Frotaur
    Jun 4, 2018 at 14:04
  • Any nodes that receive a transaction or block that doesn't follow the rules will simply refuse to broadcast it any further. It'll be like it never existed. Other miners will continue mining on a chain that doesn't include that block. Jun 4, 2018 at 14:17
  • I see. Does that mean that there could be potentially a "node attack", in the sense that you run several full nodes that broadcast transactions that don't follow the rules ?
    – Frotaur
    Jun 4, 2018 at 14:21
  • 1
    You would pretty much just end up running a minor network then, since the larger btc network would still not accept your transactions. All you would achieve is a fork that only you are on Jun 4, 2018 at 14:34
2

However, what prevents someone from spending more bitcoins that they have ? For example, if I have 1 BTC on my adress, suppose I run a mining node, what prevents me from trying to mine a block in which I spend 1.2 BTC ? At what point in the process is the balance of my adress checked to make sure I do have the bitcoin that I want to spend ?

You may be working from a faulty mental model. Bitcoin is an electronic cash system. This probably doesn't mean what you think it does.

https://bitzuma.com/posts/bitcoin-think-of-it-as-electronic-cash/

Electronic cash is a system in which users exchange digital tokens for goods and services. In Bitcoin these are called "coins," or more technically unspent transaction outputs (UTXOs).

A UTXO will be completely spent in a transaction. The state of a transaction output can, in other words, only be spent or unspent. An output (coin) can never have a negative value - just like in physical cash systems it's impossible for a bank note or coin to have a negative value.

There are no accounts in Bitcoin. Addresses are a higher-level construct that make it appear as if accounts exist. It's a potentially dangerous misconception:

https://bitzuma.com/posts/five-ways-to-lose-money-with-bitcoin-change-addresses/

4
  • Thanks for the precisions, they are helpful. What I still struggle to understand is where is the verification "is the output negative" done. If I have a program running on my computer it is my computer that will do the computations and verify if the output is negative. If this is the role of nodes, what prevents a node from being malicious ? In other words, what enforces it to use the code that checks "is output negative" ? Maybe there isn't any, but my understanding was that even if you have full mining power, you cannot do "anything", i.e. cannot create more bitcoins for example.
    – Frotaur
    Jun 4, 2018 at 14:13
  • The verification is done by a full node (yours, for example). If a transaction contains an output with a negative value, the block containing it will be rejected as invalid. This applies even if the block has sufficient proof-of-work. Overall network behavior is a composite of individual node behavior. In other words, all honest nodes will also reject such a block. Jun 4, 2018 at 14:16
  • Thank you, that clears things up. Does that mean that if a networks contains more malicious nodes than honest ones, anything like spending inexistent bitcoins or creating bitcoins would be possible ?
    – Frotaur
    Jun 4, 2018 at 14:19
  • No. The purpose of the network is to allow users to detect double spending. A node will never accept a block spending nonexistent coins or creating money out of thin air. But it does need the network as a check on double spending. The network does this by creating a cost for rewriting the block chain in the form of proof-of-work. Jun 4, 2018 at 14:45

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.