<p>A coinbase transaction uses an input to store metadata. Some types of metadata are the block height and extra nonce. See:</p>
<ul>
<li><a href="https://github.com/bitcoin/bips/blob/master/bip-0034.mediawiki" rel="nofollow noreferrer">BIP 34: Block v2, Height in Coinbase</a></li>
<li><a href="https://en.bitcoin.it/wiki/Transaction#Generation" rel="nofollow noreferrer">Bitcoin Wiki: Transaction - Generation</a></li>
<li><a href="https://bitcoin.stackexchange.com/questions/90127/why-does-this-coinbase-transaction-have-two-op-return-outputs/90156#90156">Bitcoin Stack Exchange: Why does this coinbase transaction have two op_return outputs?</a></li>
</ul>

CC BY-SA 4.0
2022-07-05T22:18:03.210Z
2022-07-05T22:18:03.210Z
Nayuki
<p>A coinbase transaction is not allowed to have any other inputs, hence this check. Furthermore, because this check is part of consensus code, it is the definition for a coinbase transaction, so this ends up being a bit circular - the function says that a coinbase transaction must only have one input, and it checks that.</p>

CC BY-SA 4.0
2022-07-05T03:22:23.210Z
2022-07-05T03:22:23.210Z
Ava Chow
<p>In the Bitcoin Core source code there is a <a href="https://github.com/bitcoin/bitcoin/blob/master/src/primitives/transaction.h#L335" rel="nofollow noreferrer">function</a> to check if a given transaction is a coinbase or not:</p>
<pre><code>    bool IsCoinBase() const
    {
        return (vin.size() == 1 &amp;&amp; vin[0].prevout.IsNull());
    }
</code></pre>
<p>Does it really need to check if <code>vin.size()</code> is equal to 1? Isn't <code>vin[0].prevout.IsNull()</code> a sufficient condition for a transaction to be a coinbase?
Is there any situation that the later condition is true but we have more than one input?</p>
<p>The only reason I can guess is to avoid run-time error when <code>vin</code> is empty (so there is no <code>vin[0]</code>).</p>

CC BY-SA 4.0
2022-07-05T10:55:16.500Z
Hi Amir, I edited the title of this topic because the previous felt a bit vague. I tried to capture the main idea of your question. Please feel free to revert or otherwise edit it if this doesn't align with what you wanted to express.
Murch
CC BY-SA 4.0
2022-07-05T11:17:11.463Z
@Murch Hi Murch, thanks for your edit. It's now really better.
Amir reza Riahi
<bitcoin-core-development><transaction-verification><coinbase-transaction>
Why does IsCoinBase() check if there is exactly one input?
Amir reza Riahi
# Why does IsCoinBase() check if there is exactly one input?

(Asked by: username_0 on 2022-07-05)

In the Bitcoin Core source code there is a [function](https://github.com/bitcoin/bitcoin/blob/master/src/primitives/transaction.h#L335) to check if a given transaction is a coinbase or not:

```
    bool IsCoinBase() const
    {
        return (vin.size() == 1 && vin[0].prevout.IsNull());
    }

```

Does it really need to check if `vin.size()` is equal to 1? Isn't `vin[0].prevout.IsNull()` a sufficient condition for a transaction to be a coinbase?
Is there any situation that the later condition is true but we have more than one input?

The only reason I can guess is to avoid run-time error when `vin` is empty (so there is no `vin[0]`).

**Tags:** bitcoin-core-development, transaction-verification, coinbase-transaction

> **Comments:**
> **username_1**: Hi username_0, I edited the title of this topic because the previous felt a bit vague. I tried to capture the main idea of your question. Please feel free to revert or otherwise edit it if this doesn't align with what you wanted to express.  
> **username_0**: @username_1 Hi username_1, thanks for your edit. It's now really better.  

---

## 2 answers

### Answer by username_3 on 2022-07-05. Score: 8 | Accepted answer

A coinbase transaction is not allowed to have any other inputs, hence this check. Furthermore, because this check is part of consensus code, it is the definition for a coinbase transaction, so this ends up being a bit circular - the function says that a coinbase transaction must only have one input, and it checks that.


---

### Answer by username_2 on 2022-07-05. Score: 0

A coinbase transaction uses an input to store metadata. Some types of metadata are the block height and extra nonce. See:

* [BIP 34: Block v2, Height in Coinbase](https://github.com/bitcoin/bips/blob/master/bip-0034.mediawiki)
* [Bitcoin Wiki: Transaction - Generation](https://en.bitcoin.it/wiki/Transaction#Generation)
* [Bitcoin Stack Exchange: Why does this coinbase transaction have two op\_return outputs?](https://bitcoin.stackexchange.com/questions/90127/why-does-this-coinbase-transaction-have-two-op-return-outputs/90156#90156)


---

