Does a Bitcoin node need to scan through the whole blockchain in order to be 100% sure that a transaction sent to it is genuine (especially when some inputs are derived from coins generated in the very early days)?
2 Answers
Yes. It doesn't have to do all the work when it receives the transaction, of course. If it's in sync with the network, it's already scanned the whole blockchain.
-
What do you mean by "it's already scanned the whole blockchain" ? Does the node build some kind of index of the transactions in the blockchain so that he can look it up faster than linear scan of the blockchain ? Dec 4, 2016 at 22:19
-
@user368507 Yes. What do you think it's doing when it gets all the blocks? It's building the UTXO (unspent transaction output) database. Dec 5, 2016 at 0:11
Yes, the whole blockchain needs to be scanned. But as David already says, it does so when it syncs/imports the whole blockchain.
So you can see it like this. When a new node is set up, it gets all the blocks from other nodes in the network and analyses them one by one. Every transaction is read and from all the transactions it creates a list of accounts that are on the network and how much bitcoins they have. So every bitcoin node has a list which more or less looks like this:
account number amount of bitcoins in account
-----------------------------------------------------------------------------
188CnLx8fqfS8jhFCqv7QF346L9V1136tp | 0.329 BTC
1MmTK7DnVSeFQQJm6gd1txk8HEYZ2rQnLg | 1.70908 BTC
15dEBJMy3GTJ8Fd2TMqaXcDB9qt9MPqNWc | 1.299 BTC
1FtKHoadhEeD1eyh4jUXjfGNctCSQFxExb | 0.93993 BTC
1KoE88YBCP6ZuPq39uCTJNiBGM6XKzQZQS | 0.393 BTC
34gRACXhU1SBR42d7B6BoJJ1U5M9mn5vXA | 1.39899 BTC
37MEbe4MwSesFo3A4uJXuUHZ1mn4Gp6Z56 | 22.41484615 BTC
etc.
When a node receives a new transaction it can easily verify it by looking up the account number and see if it has enough bitcoins to do the transaction it wants to do.
I hope that makes it a bit more clear.
-
Talking about addresses as "accounts" having "balances" is extremely misleading. The node tracks Unspent Transaction Outputs (UTXOs) not "balances".– meshcollider ♦Jul 7, 2017 at 10:35
-
@MeshCollider - yes, but the idea is the same: it knows how much can be spent from each address. This is just a beginners explanation which gives an idea of how it works.– kramer65Jul 7, 2017 at 11:44
-
That idea leads to a lot of confusion later on, for example: bitcoin.stackexchange.com/questions/55043/…– meshcollider ♦Jul 7, 2017 at 13:06