## What is preventing negative balance?

6

1

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 ?

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 – 2018-06-04T18:31:59.240

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)


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 – 2018-06-05T00:08:45.820

2@Mark The difference is paid as fees to the miner – Raghav Sood – 2018-06-05T01:08:59.297

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.

What do you mean "the rest of the network would reject it" ? How does this rejection happen ? – Frotaur – 2018-06-04T14:04:06.757

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 – 2018-06-04T14:17:44.640

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 – 2018-06-04T14:21:56.060

1You 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 – 2018-06-04T14:34:13.327

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: