Hi there again beautiful Bitcoin Community
I am making IT project explaining how does Bitcoin work, with simple client (using BitcoinJ) on my university, and i got stuck on couple of things - probably i am too dumb to find answer
i googled problems multiple ways, and also wiki doesn't explain me them well. I really appreciate any help for any of those questions.
1. Fetching blocks - how does it work?
I know there is possibility to fetch simple blocks by their hash, but to fetch any blocks i need to KNOW these hashes - thats why other option was implemented (getblocks
). Here comes problem - how does algorithm of block fetching works? As far as i've read - i got it this way(for example i am at block 10 000 and network is on 19 900):
i issue getblocks
packet by sending block locator hash as hash of block 10 000 and i get in response inv
packet with list of blocks 10 001 - 10 500? And i repeat that with hash 10 500 to get another 500 pack etc etc until i hit 19 900?
Also - when i get any invalid blocks in my chain (lets say it is block 10005 and network is also on 10005, but mine 2 newest blocks are invalid) i send getblocks packet with newest block hash as locator (mine 10005) and in response i get block 10005 hash that i dont know, so i send hash of block 10004 and in response again i get hash i don't know, then i send good block hash #10003 which then comes with an inv
packet for 2 last blocks?
To be sure - MSG_BLOCK in Inventory Vector is exactly hash of block that i download then just by using getdata
2. I dont get algorithms in transactions - how does bitcoin:
a) check that someone really have coins stating all transactions behind to prove they really exist
b) check that someone isn't spending same bitcoins 2nd time?
The problem for a) is mainly - how does TxIn in one block locate TxOut from other block having only hash of it? It can't go through all the blocks to finally find proper transaction because it will be too time consuming for HDD! And there comes other problem - even if we could just JUMP from one transaction to another(locating transaction by BLOCK number where it is located to prevent consuming hdd - probably it is done that way, i hope you people help me find answer
) - how will double spending check work?
As vector76 explained in one of his posts:
Tx1 input 1 comes from a previous tx with a value of 1
Tx1 input 2 comes from a previous tx with a value of 2
Tx1 output 1 has a value of 2.5 and requires key A to be claimed.
Tx1 output 2 has a value of 0.5 and requires key B to be claimed.
Person with key A creates a transaction and submits it to the miners:
Tx2 input 1 comes from Tx1/output 1.
Tx2 output 1 has a value of 2.0 and requires key C to be claimed
Tx2 output 2 has a value of 0.5 and requires key A to be claimed
Other nodes observe that Tx1/output 1 has not yet been spent and has a value of 2.5. They verify that the outputs are not greater than 2.5. They mark Tx1/output1 as used.
Person with key A creates another (double spend) transaction:
Tx3 input 1 comes from Tx1/output 1
Tx3 output 1 has a value of 1.0 and requires key D to be claimed
Tx3 output 2 has a value of 1.5 and requires key E to be claimed
Other nodes observe that Tx1/output 1 has already been spent, and they reject this transaction.
I've got in bold word "observe" because i don't know what it does really mean here? Let's say:
I spend cash from Tx1/output 1(which is for example in block 5 000) in block 10 000(Tx2), and then i try to spend again in block 15 000(Tx3) - what does miners really do? They check ALL blocks (and transactions in them) starting from block 5 000 from whom it is spent to find any inputs for that output? That would consume HDD a LOT for every transaction verification, and i can't imagine how it would work in future with a lot of transactions per second and milions of blocks.
Thanks in advance for any replies, probably i will have other problems again, i hope i get them resolved in here (not only for me!