<blockquote>
  <p>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 ?</p>
</blockquote>

<p>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.</p>

<p><a href="https://bitzuma.com/posts/bitcoin-think-of-it-as-electronic-cash/" rel="nofollow noreferrer">https://bitzuma.com/posts/bitcoin-think-of-it-as-electronic-cash/</a></p>

<p>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).</p>

<p>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.</p>

<p>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:</p>

<p><a href="https://bitzuma.com/posts/five-ways-to-lose-money-with-bitcoin-change-addresses/" rel="nofollow noreferrer">https://bitzuma.com/posts/five-ways-to-lose-money-with-bitcoin-change-addresses/</a></p>

CC BY-SA 4.0
2018-06-04T14:13:25.753Z
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
CC BY-SA 4.0
2018-06-04T14:16:07.723Z
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.
Rich Apodaca
CC BY-SA 4.0
2018-06-04T14:19:51.177Z
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
CC BY-SA 4.0
2018-06-04T14:45:31.687Z
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.
Rich Apodaca
CC BY-SA 4.0
2018-06-04T14:07:03.920Z
2018-06-04T14:13:55.533Z
2018-06-04T14:13:55.533Z
Rich Apodaca
<p>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.</p>

<p>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.</p>

CC BY-SA 4.0
2018-06-04T14:04:06.757Z
What do you mean "the rest of the network would reject it" ? How does this rejection happen ?
Frotaur
CC BY-SA 4.0
2018-06-04T14:17:44.640Z
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.
Raghav Sood
CC BY-SA 4.0
2018-06-04T14:21:56.060Z
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
CC BY-SA 4.0
2018-06-04T14:34:13.327Z
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
Raghav Sood
CC BY-SA 4.0
2018-06-04T14:02:12.437Z
2018-06-04T14:02:12.437Z
Raghav Sood
<blockquote>
  <p>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 ?</p>
</blockquote>

<p>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:</p>

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

<p>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.</p>

<p>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).</p>

<p>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.</p>

<blockquote>
  <p>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 ?</p>
</blockquote>

<p>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.</p>

<hr>

<p>Somewhat related, you also can't pay someone a negative output:</p>

<pre><code>    if (txout.nValue &lt; 0)
        return state.DoS(100, false, REJECT_INVALID, "bad-txns-vout-negative");
</code></pre>

CC BY-SA 4.0
2018-06-05T00:08:45.820Z
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
CC BY-SA 4.0
2018-06-05T01:08:59.297Z
@Mark The difference is paid as fees to the miner
Raghav Sood
CC BY-SA 4.0
2018-06-04T14:24:01.077Z
2018-06-04T14:24:01.077Z
David A. Harding
<p>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 !</p>

<p>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.</p>

<p>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.</p>

<p>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 ?</p>

<p>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).</p>

<p>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 ?</p>

CC BY-SA 4.0
2018-06-04T18:31:59.240Z
Next to the detailed answers below, I wanted to link to the wiki as well, which shows the protocol rules: https://en.bitcoin.it/wiki/Protocol_rules#.22tx.22_messages
pebwindkraft
<transactions><blockchain><nodes>
What is preventing negative balance?
Frotaur
# What is preventing negative balance?

(Asked by: username_0 on 2018-06-04)

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 ?

**Tags:** transactions, blockchain, nodes

> **Comments:**
> **username_1**: Next to the detailed answers below, I wanted to link to the wiki as well, which shows the protocol rules: https://en.bitcoin.it/wiki/Protocol_rules#.22tx.22_messages  

---

## 3 answers

### Answer by username_4 on 2018-06-04. Score: 9 | Accepted answer

> 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");

```

> **Comments:**
> **username_5**: 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?  
> **username_3**: @username_5 The difference is paid as fees to the miner  

---

### Answer by username_2 on 2018-06-04. Score: 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/>

> **Comments:**
> **username_0**: 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.  
> **username_2**: 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.  
> **username_0**: 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 ?  
> **username_2**: 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.  

---

### Answer by username_3 on 2018-06-04. Score: 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.

> **Comments:**
> **username_0**: What do you mean "the rest of the network would reject it" ? How does this rejection happen ?  
> **username_3**: 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.  
> **username_0**: 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 ?  
> **username_3**: 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  

---

