Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: zoltanb on September 18, 2017, 10:59:22 AM



Title: Bitcoin transaction help
Post by: zoltanb on September 18, 2017, 10:59:22 AM
I was playing around with my Bitcoin Node, to try and understand how does Bitcoin Transactions work.

For this purpose, I used the sendtoaddress API call for bitcoin-cli, to send some XBT to my own Bitcoin Blockchain.Info wallet. The transaction was processed, and I was trying to understand the information that I get from gettransaction bitcoin-cli command, and the information that the Blockchain.info website shows about the transaction.

So, here is what I did:

1. I've generated a receiving address for myself in Blockchain.Info (17tzZwAi722L7V8V27bUGEKwJpRYVNRKVW)
2. In my Bitcoin node, I used the following command: bitcoin-cli sendtoaddress 17tzZwAi722L7V8V27bUGEKwJpRYVNRKVW 0.001

This gave me back a transaction id: 4f299e7313af7e4e091ea519bbf5983526d18fe4765b679ce1827bd9f75dbaff

I then checked the information that the bitcoin node gives about that transaction with: bitcoin-cli gettransaction 4f299e7313af7e4e091ea519bbf5983526d18fe4765b679ce1827bd9f75dbaff

What I noticed here is that the bitcoin-cli command gave me back the address I sent the money to, the fee it took to get the address, but it didn't tell me from which address the transaction was sent from.

Then I checked the information that Blockchain.info provides about the transaction: https://blockchain.info/tx/4f299e7313af7e4e091ea519bbf5983526d18fe4765b679ce1827bd9f75dbaff

Here, it can be clearly seen that an amount of 0.06611392 BTC was sent from 12bFMAPz7dhKmLxsoipfUqmCCjqENCDzKb XBT address, to two different outputs. One of the output is the address, Blockchain.Info gave me to receive the Bitcoin. The other output is an address unknown to me.

So I started thinking: Am I correct, that when sending a bitcoin transaction, all of the amount from a specific address gets placed in the input, a part of it (the part which I specify) gets sent to the address I wish to send, and the rest gets transferred to a newly created address on the same wallet, from which the send operation was initiated?

Some other questions which arise to me: How is Blockchain.Info able to retrieve information from any kind of Bitcoin Transaction, while me, using a full Bitcoin Node, can only find transactions which was either initiated by the node, or which has a receiving address which is in the nodes wallet?


Title: Re: Bitcoin transaction help
Post by: paraboul on September 18, 2017, 11:38:52 AM
Quote
So I started thinking: Am I correct, that when sending a bitcoin transaction, all of the amount from a specific address gets placed in the input, a part of it (the part which I specify) gets sent to the address I wish to send, and the rest gets transferred to a newly created address on the same wallet, from which the send operation was initiated?

Not "all" of the amount. It uses a UTXO (Unspent Transaction Output) as input and send you back the change for it.
Basically, a UTXO is the output of a previous transaction you received. The way your wallet handle this is also called "coin control".


Title: Re: Bitcoin transaction help
Post by: TheQuin on September 18, 2017, 11:44:24 AM
I'll answer one of your questions.


So I started thinking: Am I correct, that when sending a bitcoin transaction, all of the amount from a specific address gets placed in the input, a part of it (the part which I specify) gets sent to the address I wish to send, and the rest gets transferred to a newly created address on the same wallet, from which the send operation was initiated?


You are correct that all of the amount held in a certain input that you hold is sent and then a change address is used to receive the unspent change. Different wallets have different ways of handling how that's done some reuse addresses while others create a new change address every time one is needed.


Title: Re: Bitcoin transaction help
Post by: aleksej996 on September 18, 2017, 12:01:22 PM
I then checked the information that the bitcoin node gives about that transaction with: bitcoin-cli gettransaction 4f299e7313af7e4e091ea519bbf5983526d18fe4765b679ce1827bd9f75dbaff

It would be good to understand what this command does. It only works for the in-wallet transactions and it doesn't show the input as it assumes you know it.

Here, it can be clearly seen that an amount of 0.06611392 BTC was sent from 12bFMAPz7dhKmLxsoipfUqmCCjqENCDzKb XBT address, to two different outputs. One of the output is the address, Blockchain.Info gave me to receive the Bitcoin. The other output is an address unknown to me.

So I started thinking: Am I correct, that when sending a bitcoin transaction, all of the amount from a specific address gets placed in the input, a part of it (the part which I specify) gets sent to the address I wish to send, and the rest gets transferred to a newly created address on the same wallet, from which the send operation was initiated?

You are correct. This is called change address. This is necessary due to the way transactions work. All the coins that aren't spent from an input are possible to be spent by someone else and since it is miners that ultimately add the transaction on the blockchain, they choose to take it themselves, this is what miner fee actually is. This way transactions are smaller and take less space in the blockchain as well.

Some other questions which arise to me: How is Blockchain.Info able to retrieve information from any kind of Bitcoin Transaction, while me, using a full Bitcoin Node, can only find transactions which was either initiated by the node, or which has a receiving address which is in the nodes wallet?

What you are looking for is two other commands. One command that can retrieve any transaction from the blockchain in hex and other that will decode that hex in a human readable format.

The first one is "getrawtransaction" and the second one is "decoderawtransaction". They will show you all the inputs and everything else in the transaction as well. "gettransaction" retrieves data from your wallet, as all the in-wallet transactions are stored in it as well, while these other two look for it in the whole blockchain.


Title: Re: Bitcoin transaction help
Post by: pebwindkraft on September 18, 2017, 12:12:45 PM
same question here, answered in the same sense:

https://bitcoin.stackexchange.com/questions/59678/questions-about-bitcoin-transactions/59684#59684

try to avoid, it's only double work. The experts are in both areas  :)


Title: Re: Bitcoin transaction help
Post by: DannyHamilton on September 18, 2017, 02:18:14 PM
What I noticed here is that the bitcoin-cli command gave me back the address I sent the money to, the fee it took to get the address, but it didn't tell me from which address the transaction was sent from.

Bitcoin transactions don't send from addresses.  They spend previously unspent transaction outputs , and they create new unspent transaction outputs.

Here, it can be clearly seen that an amount of 0.06611392 BTC was sent from 12bFMAPz7dhKmLxsoipfUqmCCjqENCDzKb XBT address,

That is not correct. blockchain.info looks up what script was used when a transaction output was previously created, converts that script to an address, and displays that address to you as if the bitcoins were "sent from" that address.  This is not how the transaction actually works, but people seem to like to see it that way, so blockchain.info displays it that way.  This tends to cause confusion as people start thinking of bitcoin addresses as "account numbers" or as something that bitcoins can be sent from.  Then when they are introduced to how bitcoin actually works, they struggle to fit reality into their existing preconceptions.

to two different outputs. One of the output is the address, Blockchain.Info gave me to receive the Bitcoin. The other output is an address unknown to me.

That other output is commonly called a "change output". That other address is an address that bitcoin-cli has created in your wallet and is keeping track of.

So I started thinking: Am I correct, that when sending a bitcoin transaction, all of the amount from a specific address gets placed in the input,

Not necessarily.  The wallet software will choose specific unspent outputs that it has access to. Those outputs may have been created using multiple different addresses, or may all have been created using the same address.  They may be all the outputs created using an address, or they may only be some of the outputs created using an address.  The wallet doesn't care what addresses were used when creating the outputs that it has control over.

As an analogy, think of it a bit like a cash business.  In this analogy, the business has multiple cash registers (addresses).  They receive cash (outputs) from customers into the various registers (addresses). Later, when they go to pay a supplier (send a transaction), they don't keep track of which register (address) each of those cash bills (outputs) were received at.  They just choose enough cash bills (outputs) to cover the cost (transaction value).  The cash bills (outputs) might be all the bills from a single register (single address), they might be all the bills (outputs) from multiple registers (addresses), they might only be some from each register (address). It doesn't matter what register (address) it was received at, only that the total is enough to cover the cost (transaction value).

a part of it (the part which I specify) gets sent to the address I wish to send,

More specifically, an output script gets created using information extracted from the address.

and the rest gets transferred to a newly created address on the same wallet, from which the send operation was initiated?

Correct. (Or more specifically, gets assigned to an output built using information extracted from a newly created address on the same wallet).

Each transaction output must be spent in its entirety.  A transaction output is either spent, or unspent.  There is no way to partially spend an output.  This fits well with our analogy above.  Think of each output as a single cash bill.  You can't rip a $20 bill in half to pay someone $10.  You have to give them the entire $20. Then you can receive $10 back in "change".  Bitcoin handles this in the transaction itself so that the recipient doesn't need to send you back a separate transaction with your change.  Previously unspent outputs are chosen as inputs to supply enough value to cover all the outputs you want to create.  If the wallet isn't able to find previously unspent outputs that add up to exactly the amount you want to send plus the transaction fee, then an additional output is created to place the extra back under the control of the wallet.

Some other questions which arise to me: How is Blockchain.Info able to retrieve information from any kind of Bitcoin Transaction, while me, using a full Bitcoin Node, can only find transactions which was either initiated by the node, or which has a receiving address which is in the nodes wallet?

You can get this additional information from your full Bitcoin Node by creating a bitcoin.conf file in the correct directory with:
Code:
tindex=1


Title: Re: Bitcoin transaction help
Post by: zoltanb on September 18, 2017, 05:52:30 PM
Thanks for all the responses.