Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: floe on May 25, 2011, 12:54:37 PM



Title: How is an account/address balance calculated?
Post by: floe on May 25, 2011, 12:54:37 PM
Hello everyone, I've been reading a bit of the bitcoin wiki and I'm really impressed by the technical details.

However, I have one question: how is the balance of a single address or an account calculated?
AFAICT, this is the sum over all transactions in the longest chain involving that address, correct?

If yes, this leads to the next question: won't the amount of computation needed to scan through the
entire chain become prohibitive at some point? Especially if the number of transactions increases at
a similar rate as, e.g. the global hash rate?

Florian


Title: Re: How is an account/address balance calculated?
Post by: Garrett Burgwardt on May 25, 2011, 01:03:00 PM
Hello everyone, I've been reading a bit of the bitcoin wiki and I'm really impressed by the technical details.

However, I have one question: how is the balance of a single address or an account calculated?
AFAICT, this is the sum over all transactions in the longest chain involving that address, correct?

If yes, this leads to the next question: won't the amount of computation needed to scan through the
entire chain become prohibitive at some point? Especially if the number of transactions increases at
a similar rate as, e.g. the global hash rate?

Florian

1. Yep, that's how it works, kind of...

2. The only thing you're missing is that you don't need to go back through the entire chain- just to the previous transaction where it came under that address' control. Since every time you send coins it verifies that there is a tx where the coins came under your control, the block chain forms a transaction chain that is self building.

basically, you only need to look at the tx that gave the coins to the address that is spending them, not all the way back to generation- the blockchain already does that.


Title: Re: How is an account/address balance calculated?
Post by: floe on May 25, 2011, 03:17:04 PM
1. Yep, that's how it works, kind of...
OK, so I got that right :-)

2. The only thing you're missing is that you don't need to go back through the entire chain- just to the previous transaction where it came under that address' control. Since every time you send coins it verifies that there is a tx where the coins came under your control, the block chain forms a transaction chain that is self building.

basically, you only need to look at the tx that gave the coins to the address that is spending them, not all the way back to generation- the blockchain already does that.
OK, I think I got that too. So scanning the entire block chain is only necessary if, say, you want to know the balance for an address that is not your own? Even then, you probably can scan backwards from the current block and stop when you've found all generation transactions.

Florian


Title: Re: How is an account/address balance calculated?
Post by: Garrett Burgwardt on May 25, 2011, 03:51:51 PM
It doesn't matter who owns the address- you only need to see what addresses have coins credited to them- miners take care of making sure double spends don't happen. Just look to the block that last credits addressses.


Title: Re: How is an account/address balance calculated?
Post by: compro01 on May 25, 2011, 04:47:11 PM
OK, I think I got that too. So scanning the entire block chain is only necessary if, say, you want to know the balance for an address that is not your own? Even then, you probably can scan backwards from the current block and stop when you've found all generation transactions.

Florian

or if you think the client missed a transaction announcement (e.g. internet outage, power outage, or something else that removes you from the network for a considerable length of time) and somehow missed it when you came back up and pulled the intervening blocks.  that's what the -rescan flag is for.  it forces the client to scan through the whole blockchain looking for transactions involving an address/keypair that you have in your wallet file.