I was wondering about the following scenario.
- Alice sends 1 BTC to Bob named TX1
- Alice double spends the 1 BTC and sends it to Charlie named TX2
Ok, so far, so good.
Alice has broadcast two competing transactions onto the peer-to-peer network.
Some peers received TX1. Most of those peers will refuse to accept TX2, and will refuse to relay it. So, until they see a confirmation, they will simply assume that TX1 is the "valid" unconfirmed transaction.
Some peers received TX2. Most of those peers will refuse to accept TX1, and will refuse to relay it. So, until they see a confirmation, they will simply assume that TX2 is the "valid" unconfirmed transaction.
- Both of them get confirmation at roughly the same rate.
- TX1 has 1 confirm TX2 has 2 confirm
Not in the same blockchain they don't. That's not possible since it violates the consensus rules. Therefore, I must assume that you are talking about 2 different blockchains. Let's look at how that can happen.
Miner Edward received TX2 before he received TX1. Therefore, he included TX2 in the block that he was working on (we'll call his block Block_Ed) and he completely ignored TX1.
Miner Frank was also working on a block (we'll call his block Block_Fr), but he decided not to include either of the transactions in his block.
Edward and Frank both solve their blocks at almost the exact same time. They both transmit their blocks to their peers. Some nodes and miners receive Block_Ed first, so they completely ignore Block_Fr and refuse to add it to their chain. Other nodes and miners receive Block_Fr first, so they completely ignore Block_Ed and refuse to add it to their chain.
There is now a temporarily forked blockchain. Some percentage of the network has the blockchain that includes Block_Ed. They see 1 confirmation for TX2, and they reject TX1 as invalid. The rest of the network has the blockchain that includes Block_Fr. They see no confirmations at all for TX2 or TX1. Some of them received TX2 first, and others received TX1 first. Each node thinks that it has the valid blockchain, and the miners work on extending the chain they think is valid.
Lets say miner George is building on top of Block_Ed. Since George received Block_Ed first, he sees that TX2 has 1 confirmation and that TX1 is rejected because it is invalid. We'll call George's block Block_Geo.
Meanwhile, miner Henry is building on top of Block_Fr. Miner Henry received TX1 before he received TX2, so he thinks that TX1 is an unconfirmed valid transaction, and he has rejected TX2 (he also rejected Block_Ed, since he received Block_Fr first). We'll call Henry's block Block_Hen.
George and Henry both solve their blocks at almost the exact same time. They both transmit their blocks to their peers. Some nodes and miners receive Block_Geo first, so they reject Block_Fr and Block_Hen and refuse to include either of them in their blockchains. Other nodes and miners receive Block_Hen first, so they reject Block_Ed and Block_Geo and refuse to include either of them in their blockchains.
There is now still a temporarily forked blockchain. Some percentage of the network has the blockchain that includes Block_Ed and Block_Geo They see 2 confirmations for TX2, and they have rejected TX1 because it is invalid. The rest of the network has the blockchain that includes Block_Fr and Block_Hen. They see 1 confirmation for TX1 and they have rejected TX2 because it is invalid.
This temporary fork will continue until a block is solved by a miner on one of the two chains while a competing block is not solved on the other chain at nearly the same time. That miner will broadcast his block and the entire network will switch over to the then longer chain. Typically, splits like this don't last more than a block or two, but in an extreme case it *might* be possible for it to last a few blocks. This is why *some* people like to wait for 6 confirmations. That gives the network enough time to sort out which of the two forks will become the longest.
If the chain with Block_Ed and Block_Geo ever becomes the longest chain, then TX2 will be valid and confirmed. TX1, Block_Fr, and Block_Hen will all simply cease to exist in the blokchcain.
if the chain with Block_Fr and Block_Hen ever becomes the longest chain, then TX1 will be valid and confirmed. TX2, Block_Ed, and Block_Geo will all simply cease to exist in the blockchain.
-Bob sends the 1 BTC to Jimmy as TX3
-Charlie sends the 1 BTC to David as TX4
The odds of this occurring are pretty slim. It would require that:
Bob receives TX1 from a peer before he receives TX2 from a peer
AND that Jimmy receives TX1 from a peer before he receives TX2 from a peer
AND that Jimmy receives TX3 from a peer before he receives TX4 from a peer
AND that Charlie receives TX2 from a peer before he receives TX1 from a peer
AND that David receives TX2 from a peer before he receives TX1 from a peer
AND that David receives TX4 from a peer before he receives TX3 from a peer.
Additionally, if Bob or Jimmy receives Block_Fr before they receive Block_Ed or Block_Hen before they receive Block_Geo, then they will no longer see TX1 or TX3, and Jimmy will not think he was paid. If Charlie or David receives Block_Ed before they receive Block_Fr or Block_Geo before they receive Block_Hen, then they will no longer see TX2 or TX4, and David will not think he was paid.
-TX3 gets 7 confirms
-TX4 gets only 1 confirm
However by this time TX2 will have 8 confirm while TX1 will have only 2
This would require the extremely unlikely event that the blockchain remain forked with at least two chains of equal length for at least 8 blocks. Furthermore, since you already said that TX2 has 1 more confirmation than TX1, TX1 should have 7 confirmations. Perhaps there was a third block that competed with Block_Geo and Block_Hen? It would have to be a block that built on top of Block_Fr and still didn't confirm either of the two transactions.
So
TX1-> TX3 (chain1)
2c 7c
and
TX2 -> TX4 (chain2)
8c 1c
Wait, I think I missed something (or got some transactions mixed up here)? It is not possible for TX3 (which spent TX1) to have MORE confirmations than TX1. Since TX3 spends TX1, it will not be allowed in a block if TX1 is not in a block yet.
I suppose you could (theoretically) have a 3 way fork like this:
-> Block2a -> Block3a -> Block4a -> Block5a -> Block6a -> Block7a -> Block8a -> Block9a
/ TX2 TX4
/
/
Block1 ---------> Block2b -> Block3b -> Block4b -> Block5b -> Block6b -> Block7b -> Block8b -> Block9b
\ TX1 TX3
\
\
-> Block2c -> Block3c -> Block4c -> Block5c -> Block6c -> Block7c -> Block8c -> Block9c
TX1
In that case:
TX1 would have in fork C the 2 confirmations you mention (but would have 8 confirmations in fork B, and wouldn't exist in fork A).
TX2 would have in fork A the 8 confirmations you mention (but wouldn't exist in fork B or fork C).
TX3 would have in fork B the 7 confirmations you mention (but wouldn't exist in fork A or fork C).
TX4 would have in fork A the 1 confirmation you mention (but wouldn't exist in fork B or fork C).
Each node and miner on the network would only accept one of these forks as the valid fork and would reject the others. Eventually Block10 would be solved by a miner somewhere on the network. Which ever fork that miner was using as his valid fork would then become the longest and the other forks would simply cease to exist. The entire network would consolidate on that fork that included Block10. The odds of a three way split lasting 9 blocks like that are astonomically small unless someone had control over more than 66% of the global hashpower and used it to intentionally maintain the 2 additional forks.
This is a big dilema, as the one with the more confirms should be inside the blockchain as the real transaction, but on 1st degree you think TX2 will be the real one, but by the time TX2 gets 8 confirms to supercede TX1 , the 1st chain will have 7 confirms on the 2nd degree.
Nope. That's not how it works.
So long as the blockchain remains forked, there is no "real one" and the confirmations within the forks are meaningless. Eventually one fork becomes the longest. Then all the other forks disappear and the confirmations in the one remaining fork become the "real" confirmations.
So which one will be the real chain 1 or 2?
Whichever chain becomes longer.
Also how many confirmations are needed for a transaction for it's double spend counterpart transaction to be cutoff from the blockchain?
One. As long as the blockchain isn't forked. If it is forked, then the doublespend will be cut off as soon as all remaining forks contain only 1 of the two competing transactions
Or do they both get included?
Never in the same fork.
Also if what you say is true then why do you need to wait 6 confirms
You don't. I have never yet felt a need to wait more than 1 confirmation before considering any transaction that I've ever received to be "safe".
Some people choose to wait additional confirmations because they are concerned that there could be a fork that they are unaware of.
because if only 1 chain can be included then it would be enough for 1 confirmation to happen?
Correct. As long as the blockchain isn't forked, then 1 confirmation is enough.
Well which miner because its not only 1 miner but many. So if miner A sees TX1 first but miner B sees TX2 first, then how they decide which one is valid?
Each miner considers the transaction that they saw first to be the valid one until they see one of the transactions in a block. Once they one of the transactions in a block, then they consider whatever block they see first to be valid. If there is a fork, then they see the chain that contains the next block they receive to be the valid chain. This continues until the blockchain is no longer forked and the remaining chain is the valid one.
I see,but if all miners and nodes are honest, and the transaction both get accepted at the same time, how are they decided which ones is valid?
They cannot both be accepted into the same block. That would create an invalid block which the entire network would reject. Each miner chooses the transaction that they see first. Then when one of them solves a block, the transaction in that block becomes the valid transaction. If two of them solve blocks at almost the exact same time and they were each using a different competing transaction then the process continues with each miner building on top of the block that they receive first until one chain is longer than all the rest.
If they are timestamped, then what if the clock of the nodes and the miners is not in synch, then what?
Timestamps are not considered. Each miner simply pays attention to the first transaction (or block) that they receive with there are competing transactions (or blocks).