working with the blockchain: transaction fees & sum(inputs) != sum(outputs) (newbie questions)

working with the blockchain: transaction fees & sum(inputs) != sum(outputs) (newbie questions)
2014-02-14 12:42 ` Wladimir
       [not found] ` <52FE4782.6020001@monetize.io>
  
From: Denis Andrejew @ 2014-02-14 12:20 UTC (permalink / raw)
  To: bitcoin-development

[-- Attachment #1: Type: text/plain, Size: 1857 bytes --]

Hi all!

I'm new to development with bitcoin and I have a question for you guys:

What I'm trying to do is read the blockchain in order to find all unspent
outputs. I'm using bitcoind via rpc as my source of information about the
blockchain.

Now one thing that I don't understand fully yet when working with the
transaction data I'm being given is:

How do transaction fees work? More specifically: I can see that there is
often a gap between the sum of the ins and the sum of the outs in a
transaction and I understand that this is the transaction fee. But how can
the miner spend it if it is not assigned as an output of any transaction?
Is there special code somewhere that keeps track of all the btc not spent
in all the transactions of a block and allows the miner's address (ie the
address in the first tx of that block) to spend that? But if so, how would
that work then? I should be finding transactions then where the sum of the
ouputs is higher than the sum of the inputs and one of the inputs should
have the miner's address attached to it or what?

I am a bit confused about this part. Other than that it's quite fascinating
to see how bitcoin works and work with it. :)

So if one of you could kindly explain to me the technical view of
transaction fees (i.e. how exactly do I find them and account for them in
the blockchain), that would be brilliant!

And the other question would be this one (directly related):

What are the cases where the sum of the input values (from the referenced
transactions) can be different from the sum of the output values?

These cases I have found and understand:

1) "coinbase" transactions that "print money" have no inputs, only 50/25
coins output (less in the future)
2) transaction fees when in > out

Are there other cases?

Thanks,
Denis

"Be the change you want to see in the world." (Mahatma Gandhi)

[-- Attachment #2: Type: text/html, Size: 2314 bytes --]

 permalink raw reply	[|nested]
2014-02-14 12:20 [Bitcoin-development] working with the blockchain: transaction fees & sum(inputs) != sum(outputs) (newbie questions) Denis Andrejew

       [not found] ` <52FE4782.6020001@monetize.io>
  
From: Wladimir @ 2014-02-14 12:42 UTC (permalink / raw)
  To: Denis Andrejew;  Bitcoin Dev

[-- Attachment #1: Type: text/plain, Size: 1232 bytes --]

>
> Is there special code somewhere that keeps track of all the btc not spent
> in all the transactions of a block and allows the miner's address (ie the
> address in the first tx of that block) to spend that?
>

Something like that:
Every block has a coinbase transaction at the beginning.
The coinbase transaction is special, in that it can pay coins to its
outputs without having to specify an input.

The total value of the outputs of the coinbase transaction can be at most
the block value (the fixed amount per block) + the fees (summed over all
transactions in the block).

This is checked in ConnectBlock in
https://github.com/bitcoin/bitcoin/blob/master/src/main.cpp#L1711
The value of the coinbase transaction is being checked on line 1731.

But if so, how would that work then? I should be finding transactions then
> where the sum of the ouputs is higher than the sum of the inputs and one of
> the inputs should have the miner's address attached to it or what?
>

The only transactions in which the sum of the outputs can be larger than
the sum of the inputs are the aforementioned coinbase transactions (which
cannot have inputs).

The outputs from the coinbase transaction can be spent as any other output.

Wladimir

[-- Attachment #2: Type: text/html, Size: 2037 bytes --]

 permalink raw reply	[|nested]
[not found] ` <52FE4782.6020001@monetize.io>

  2014-02-14 23:01     ` Mark Friedenbach
  
From: Denis Andrejew @ 2014-02-14 20:56 UTC (permalink / raw)
  To: Mark Friedenbach, bitcoin-development

[-- Attachment #1: Type: text/plain, Size: 891 bytes --]

Thanks Wladimir, perfect info!

Mark, sounds good. But most likely this DB is keeping this information only
for the current state of the blockchain and what I need really is to be
able to get the unspent outputs (and calculate the balance for all
addresses) for any particular block I happen to be interested in :)

- Denis

"Be the change you want to see in the world." (Mahatma Gandhi)


On Fri, Feb 14, 2014 at 5:42 PM, Mark Friedenbach <mark@monetize•io> wrote:

> On 02/14/2014 04:20 AM, Denis Andrejew wrote:
> > What I'm trying to do is read the blockchain in order to find all
> > unspent outputs. I'm using bitcoind via rpc as my source of
> > information about the blockchain.
>
> By the way, bitcoind keeps this information in a special LevelDB
> database in the chainstate directory. It would be rather simple to
> iterate over the database for the list of al unspent outputs.
>

[-- Attachment #2: Type: text/html, Size: 1338 bytes --]

 permalink raw reply	[|nested]
2014-02-14 20:56   ` Denis Andrejew

  
From: Mark Friedenbach @ 2014-02-14 23:01 UTC ( / )
  To: Denis Andrejew, bitcoin-development

Still straightforward: get a list of transaction hashes for the block
from bitcoind, then query these transactions from the UTXO changestate
database.

On 02/14/2014 12:56 PM, Denis Andrejew wrote:
> Thanks Wladimir, perfect info!
> 
> Mark, sounds good. But most likely this DB is keeping this
> information only for the current state of the blockchain and what I
> need really is to be able to get the unspent outputs (and calculate
> the balance for all addresses) for any particular block I happen to
> be interested in :)
> 
> - Denis
> 
> "Be the change you want to see in the world." (Mahatma Gandhi)
> 
> 
> On Fri, Feb 14, 2014 at 5:42 PM, Mark Friedenbach
> <mark@monetize•io <mailto:mark@monetize•io>> wrote:
> 
> On 02/14/2014 04:20 AM, Denis Andrejew wrote:
>> What I'm trying to do is read the blockchain in order to find
>> all unspent outputs. I'm using bitcoind via rpc as my source of 
>> information about the blockchain.
> 
> By the way, bitcoind keeps this information in a special LevelDB 
> database in the chainstate directory. It would be rather simple to 
> iterate over the database for the list of al unspent outputs.
> 
>