<p>Transaction outputs have output values. That is, an exact amount of satoshis (1/100,000,000th of a bitcoin) assigned to a particular script. The scripts don't have to be evaluated when the transaction is accepted, though for a miner, it may be advantageous to (in order to redeem that amount). Generally, the only thing the miner needs to check for in a transaction are:</p>

<ol>
<li>The inputs are not spent. That is, no other transaction spends those inputs.</li>
<li>The inputs are valid. That is, the scriptSig on each input satisfies the outputs that they're spending.</li>
</ol>

<p>The outputs don't have to be evaluated until a transaction spending the output is created.</p>

CC BY-SA 3.0
2016-07-06T14:38:59.933Z
My question assumes that the miner needs to understand the transaction well enough to know for certain what the fee will be.
Harry Pillsbury
CC BY-SA 3.0
2016-07-06T13:27:21.887Z
2016-07-06T13:27:21.887Z
Jimmy Song
<p>Miners do need to understand what a transaction does, but this is true for every node in the network. In particular, it is also true for all transactions that other miners choose to accept into the chain.</p>

<p>To determine the fee they want to charge for it, they look at the size and only the size. They sort transaction by fee per byte, and include the highest-ranked transactions first. This maximizes their income.</p>

CC BY-SA 3.0
2016-07-07T08:13:27.787Z
2016-07-07T08:13:27.787Z
Pieter Wuille
<p>Determining the fee of a transaction is actually very easy.</p>

<p>As you know, a transaction has inputs and outputs, where each output has an associated amount and a script, and each input is a link to an output of a previous transaction, along with whatever data is needed to satisfy the previous output's script.</p>

<p>A transaction fee is implemented by simply having the total value of the outputs be less than the total value of the inputs.  The difference represents the transaction fee and can be added to the block's coinbase transaction, to be collected as part of the miner's reward.</p>

<p>So in order to calculate the fee of a transaction, the miner simply has to:</p>

<ul>
<li><p>Sum up the values of the outputs of the transaction.</p></li>
<li><p>For the inputs of the transaction, find the associated previous outputs and sum up their values.  (These will normally have already been stored in an indexed database, so it's easy to find them.) This gives the total input value.</p></li>
<li><p>Subtract.</p></li>
</ul>

<p>Of course, the miner also has to check that the transaction is valid, since any block containing an invalid transaction is itself invalid, and won't be included in the block chain.  This is a little harder, because it does require actually "decoding" and executing the script for each of the previous outputs, and verifying that the input data and signatures provided in the current transaction satisfy those scripts.  But by design, the instructions available for those scripts is quite limited (for instance, there are no loops).  So this verification is of bounded complexity, and it can't be too hard in a computational sense.  Of course, the code to actually do the verification is already implemented in Bitcoin Core, so in practice all the miner has to do is run it.</p>

CC BY-SA 3.0
2016-07-06T15:05:19.277Z
2016-07-06T15:05:19.277Z
Nate Eldredge
<p>Bitcoins are spent via transaction scripts that can be quite complicated. How do miners understand what a transaction does?</p>

<p>A miner has to decide whether to include a transaction in their block, but this could be quite a difficult decision, because scripts can be complicated. How could you even know, for example, that a transaction has a fee in it, without decoding the transaction? A person could create a transaction with a big fee, but it could be hard to determine that due to the script complexity.</p>

CC BY-SA 3.0
2016-07-07T08:30:17.707Z
"... without decoding the transaction" Miners have to decode transactions, and also fully validate them. If they don't, they could be tricked into creating invalid blocks that the network does not accept (wasted money for them).
Pieter Wuille
CC BY-SA 3.0
2016-07-07T08:31:14.280Z
Script complexity matters, and miners need to validate scripts, but scripts have nothing to do with determining the fee: the fee is the difference between input amounts and output amounts.
Pieter Wuille
<transactions><transaction-fees><transaction-verification>
How do miners understand what a transaction does?
Harry Pillsbury
# How do miners understand what a transaction does?

(Asked by: username_0 on 2016-07-06)

Bitcoins are spent via transaction scripts that can be quite complicated. How do miners understand what a transaction does?

A miner has to decide whether to include a transaction in their block, but this could be quite a difficult decision, because scripts can be complicated. How could you even know, for example, that a transaction has a fee in it, without decoding the transaction? A person could create a transaction with a big fee, but it could be hard to determine that due to the script complexity.

**Tags:** transactions, transaction-fees, transaction-verification

> **Comments:**
> **username_1**: "... without decoding the transaction" Miners have to decode transactions, and also fully validate them. If they don't, they could be tricked into creating invalid blocks that the network does not accept (wasted money for them).  
> **username_1**: Script complexity matters, and miners need to validate scripts, but scripts have nothing to do with determining the fee: the fee is the difference between input amounts and output amounts.  

---

## 3 answers

### Answer by username_3 on 2016-07-06. Score: 6 | Accepted answer

Determining the fee of a transaction is actually very easy.

As you know, a transaction has inputs and outputs, where each output has an associated amount and a script, and each input is a link to an output of a previous transaction, along with whatever data is needed to satisfy the previous output's script.

A transaction fee is implemented by simply having the total value of the outputs be less than the total value of the inputs. The difference represents the transaction fee and can be added to the block's coinbase transaction, to be collected as part of the miner's reward.

So in order to calculate the fee of a transaction, the miner simply has to:

* Sum up the values of the outputs of the transaction.
* For the inputs of the transaction, find the associated previous outputs and sum up their values. (These will normally have already been stored in an indexed database, so it's easy to find them.) This gives the total input value.
* Subtract.

Of course, the miner also has to check that the transaction is valid, since any block containing an invalid transaction is itself invalid, and won't be included in the block chain. This is a little harder, because it does require actually "decoding" and executing the script for each of the previous outputs, and verifying that the input data and signatures provided in the current transaction satisfy those scripts. But by design, the instructions available for those scripts is quite limited (for instance, there are no loops). So this verification is of bounded complexity, and it can't be too hard in a computational sense. Of course, the code to actually do the verification is already implemented in Bitcoin Core, so in practice all the miner has to do is run it.


---

### Answer by username_2 on 2016-07-06. Score: 3

Transaction outputs have output values. That is, an exact amount of satoshis (1/100,000,000th of a bitcoin) assigned to a particular script. The scripts don't have to be evaluated when the transaction is accepted, though for a miner, it may be advantageous to (in order to redeem that amount). Generally, the only thing the miner needs to check for in a transaction are:

1. The inputs are not spent. That is, no other transaction spends those inputs.
2. The inputs are valid. That is, the scriptSig on each input satisfies the outputs that they're spending.

The outputs don't have to be evaluated until a transaction spending the output is created.

> **Comments:**
> **username_0**: My question assumes that the miner needs to understand the transaction well enough to know for certain what the fee will be.  

---

### Answer by username_1 on 2016-07-07. Score: 2

Miners do need to understand what a transaction does, but this is true for every node in the network. In particular, it is also true for all transactions that other miners choose to accept into the chain.

To determine the fee they want to charge for it, they look at the size and only the size. They sort transaction by fee per byte, and include the highest-ranked transactions first. This maximizes their income.


---

