4

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.

2
  • "... 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). Commented Jul 7, 2016 at 8:30
  • 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. Commented Jul 7, 2016 at 8:31

3 Answers 3

6

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.

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.

1
  • My question assumes that the miner needs to understand the transaction well enough to know for certain what the fee will be. Commented Jul 6, 2016 at 14:38
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.